| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id: setuplib.php,v 1.22.2.4 2008/01/20 17:58:06 skodak Exp $ 2 // These functions are required very early in the Moodle 3 // setup process, before any of the main libraries are 4 // loaded. 5 6 7 /** 8 * Simple class 9 */ 10 class object {}; 11 12 13 /** 14 * Initializes our performance info early. 15 * 16 * Pairs up with get_performance_info() which is actually 17 * in moodlelib.php. This function is here so that we can 18 * call it before all the libs are pulled in. 19 * 20 * @uses $PERF 21 */ 22 function init_performance_info() { 23 24 global $PERF, $CFG, $USER; 25 26 $PERF = new Object; 27 $PERF->dbqueries = 0; 28 $PERF->logwrites = 0; 29 if (function_exists('microtime')) { 30 $PERF->starttime = microtime(); 31 } 32 if (function_exists('memory_get_usage')) { 33 $PERF->startmemory = memory_get_usage(); 34 } 35 if (function_exists('posix_times')) { 36 $PERF->startposixtimes = posix_times(); 37 } 38 if (function_exists('apd_set_pprof_trace')) { 39 // APD profiling 40 if ($USER->id > 0 && $CFG->perfdebug >= 15) { 41 $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id; 42 mkdir($tempdir); 43 apd_set_pprof_trace($tempdir); 44 $PERF->profiling = true; 45 } 46 } 47 } 48 49 /** 50 * Function to raise the memory limit to a new value. 51 * Will respect the memory limit if it is higher, thus allowing 52 * settings in php.ini, apache conf or command line switches 53 * to override it 54 * 55 * The memory limit should be expressed with a string (eg:'64M') 56 * 57 * @param string $newlimit the new memory limit 58 * @return bool 59 */ 60 function raise_memory_limit ($newlimit) { 61 62 if (empty($newlimit)) { 63 return false; 64 } 65 66 $cur = @ini_get('memory_limit'); 67 if (empty($cur)) { 68 // if php is compiled without --enable-memory-limits 69 // apparently memory_limit is set to '' 70 $cur=0; 71 } else { 72 if ($cur == -1){ 73 return true; // unlimited mem! 74 } 75 $cur = get_real_size($cur); 76 } 77 78 $new = get_real_size($newlimit); 79 if ($new > $cur) { 80 ini_set('memory_limit', $newlimit); 81 return true; 82 } 83 return false; 84 } 85 86 /** 87 * Converts numbers like 10M into bytes. 88 * 89 * @param mixed $size The size to be converted 90 * @return mixed 91 */ 92 function get_real_size($size=0) { 93 if (!$size) { 94 return 0; 95 } 96 $scan['MB'] = 1048576; 97 $scan['Mb'] = 1048576; 98 $scan['M'] = 1048576; 99 $scan['m'] = 1048576; 100 $scan['KB'] = 1024; 101 $scan['Kb'] = 1024; 102 $scan['K'] = 1024; 103 $scan['k'] = 1024; 104 105 while (list($key) = each($scan)) { 106 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { 107 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; 108 break; 109 } 110 } 111 return $size; 112 } 113 114 /** 115 * Create a directory. 116 * 117 * @uses $CFG 118 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1 119 * param bool $shownotices If true then notification messages will be printed out on error. 120 * @return string|false Returns full path to directory if successful, false if not 121 */ 122 function make_upload_directory($directory, $shownotices=true) { 123 124 global $CFG; 125 126 $currdir = $CFG->dataroot; 127 128 umask(0000); 129 130 if (!file_exists($currdir)) { 131 if (! mkdir($currdir, $CFG->directorypermissions)) { 132 if ($shownotices) { 133 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '. 134 $currdir .' with web server write access</div>'."<br />\n"; 135 } 136 return false; 137 } 138 } 139 140 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open 141 if (!file_exists($currdir.'/.htaccess')) { 142 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety 143 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n"); 144 @fclose($handle); 145 } 146 } 147 148 $dirarray = explode('/', $directory); 149 150 foreach ($dirarray as $dir) { 151 $currdir = $currdir .'/'. $dir; 152 if (! file_exists($currdir)) { 153 if (! mkdir($currdir, $CFG->directorypermissions)) { 154 if ($shownotices) { 155 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('. 156 $currdir .')</div>'."<br />\n"; 157 } 158 return false; 159 } 160 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it 161 } 162 } 163 164 return $currdir; 165 } 166 167 /** 168 * This function will introspect inside DB to detect it it's a UTF-8 DB or no 169 * Used from setup.php to set correctly "set names" when the installation 170 * process is performed without the initial and beautiful installer 171 */ 172 function setup_is_unicodedb() { 173 174 global $CFG, $db, $INSTALL; 175 176 $unicodedb = false; 177 178 // Calculate $CFG->dbfamily 179 $dbfamily = set_dbfamily(); 180 181 switch ($dbfamily) { 182 case 'mysql': 183 $rs = $db->Execute("SHOW LOCAL VARIABLES LIKE 'character_set_database'"); 184 if ($rs && !$rs->EOF) { // rs_EOF() not available yet 185 $records = $rs->GetAssoc(true); 186 $encoding = $records['character_set_database']['Value']; 187 if (strtoupper($encoding) == 'UTF8') { 188 $unicodedb = true; 189 } 190 } 191 break; 192 case 'postgres': 193 /// Get PostgreSQL server_encoding value 194 $rs = $db->Execute("SHOW server_encoding"); 195 if ($rs && !$rs->EOF) { // rs_EOF() not available yet 196 $encoding = $rs->fields['server_encoding']; 197 if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') { 198 $unicodedb = true; 199 } 200 } 201 break; 202 case 'mssql': 203 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32) 204 $unicodedb = true; 205 break; 206 case 'oracle': 207 /// Get Oracle DB character set value 208 $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'"); 209 if ($rs && !$rs->EOF) { // rs_EOF() not available yet 210 $encoding = $rs->fields['value']; 211 if (strtoupper($encoding) == 'AL32UTF8') { 212 $unicodedb = true; 213 } 214 } 215 break; 216 } 217 return $unicodedb; 218 } 219 220 /** 221 * This internal function sets and returns the proper value for $CFG->dbfamily based on $CFG->dbtype 222 * It's called by preconfigure_dbconnection() and at install time. Shouldn't be used 223 * in other places. Code should rely on dbfamily to perform conditional execution 224 * instead of using dbtype directly. This allows quicker adoption of different 225 * drivers going against the same DB backend. 226 * 227 * This function must contain the init code needed for each dbtype supported. 228 * 229 * return string dbfamily value (mysql, postgres, oracle, mssql) 230 */ 231 function set_dbfamily() { 232 233 global $CFG, $INSTALL; 234 235 // Since this function is also used during installation process, i.e. during install.php before $CFG->dbtype is set. 236 // we need to get dbtype from the right variable 237 if (!empty($INSTALL['dbtype'])) { 238 $dbtype = $INSTALL['dbtype']; 239 } else { 240 $dbtype = $CFG->dbtype; 241 } 242 243 switch ($dbtype) { 244 case 'mysql': 245 case 'mysqli': 246 $CFG->dbfamily='mysql'; 247 break; 248 case 'postgres7': 249 $CFG->dbfamily='postgres'; 250 break; 251 case 'mssql': 252 case 'mssql_n': 253 case 'odbc_mssql': 254 $CFG->dbfamily='mssql'; 255 break; 256 case 'oci8po': 257 $CFG->dbfamily='oracle'; 258 break; 259 } 260 261 return $CFG->dbfamily; 262 } 263 264 /** 265 * This internal function, called from setup.php BEFORE stabilishing the DB 266 * connection, defines the $CFG->dbfamily global -by calling set_dbfamily()- 267 * and predefines some constants needed by ADOdb to switch some default 268 * behaviours. 269 * 270 * This function must contain all the pre-connection code needed for each 271 * dbtype supported. 272 */ 273 function preconfigure_dbconnection() { 274 275 global $CFG; 276 277 /// Define dbfamily 278 set_dbfamily(); 279 280 /// Based on $CFG->dbfamily, set some ADOdb settings 281 switch ($CFG->dbfamily) { 282 /// list here family types where we know 283 /// the fieldnames will come in lowercase 284 /// so we can avoid expensive tolower() 285 case 'postgres': 286 case 'mysql': 287 case 'mssql': 288 define ('ADODB_ASSOC_CASE', 2); 289 break; 290 case 'oracle': 291 define ('ADODB_ASSOC_CASE', 0); /// Use lowercase fieldnames for ADODB_FETCH_ASSOC 292 /// (only meaningful for oci8po, it's the default 293 /// for other DB drivers so this won't affect them) 294 /// Row prefetching uses a bit of memory but saves a ton 295 /// of network latency. With current AdoDB and PHP, only 296 /// Oracle uses this setting. 297 define ('ADODB_PREFETCH_ROWS', 1000); 298 break; 299 default: 300 /// if we have to lowercase it, set to 0 301 /// - note that the lowercasing is very expensive 302 define ('ADODB_ASSOC_CASE', 0); //Use lowercase fieldnames for ADODB_FETCH_ASSOC 303 } 304 } 305 306 function init_memcached() { 307 global $CFG, $MCACHE; 308 309 include_once($CFG->libdir . '/memcached.class.php'); 310 $MCACHE = new memcached; 311 if ($MCACHE->status()) { 312 return true; 313 } 314 unset($MCACHE); 315 return false; 316 } 317 318 function init_eaccelerator() { 319 global $CFG, $MCACHE; 320 321 include_once($CFG->libdir . '/eaccelerator.class.php'); 322 $MCACHE = new eaccelerator; 323 if ($MCACHE->status()) { 324 return true; 325 } 326 unset($MCACHE); 327 return false; 328 } 329 330 331 332 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |