MDL-26502 check browser - fix for Symbian (backported)
[moodle.git] / lib / setuplib.php
blob32f9a7042328709858ed350f035b02028c6633d9
1 <?php // $Id$
2 // These functions are required very early in the Moodle
3 // setup process, before any of the main libraries are
4 // loaded.
7 /**
8 * Simple class
9 */
10 class object {};
13 /**
14 * Initializes our performance info early.
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.
20 * @uses $PERF
22 function init_performance_info() {
24 global $PERF, $CFG, $USER;
26 $PERF = new Object;
27 $PERF->dbqueries = 0;
28 $PERF->logwrites = 0;
29 if (function_exists('microtime')) {
30 $PERF->starttime = microtime();
32 if (function_exists('memory_get_usage')) {
33 $PERF->startmemory = memory_get_usage();
35 if (function_exists('posix_times')) {
36 $PERF->startposixtimes = posix_times();
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;
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
55 * The memory limit should be expressed with a string (eg:'64M')
57 * @param string $newlimit the new memory limit
58 * @return bool
60 function raise_memory_limit ($newlimit) {
62 if (empty($newlimit)) {
63 return false;
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!
75 $cur = get_real_size($cur);
78 $new = get_real_size($newlimit);
79 if ($new > $cur) {
80 ini_set('memory_limit', $newlimit);
81 return true;
83 return false;
86 /**
87 * Function to reduce the memory limit to a new value.
88 * Will respect the memory limit if it is lower, thus allowing
89 * settings in php.ini, apache conf or command line switches
90 * to override it
92 * The memory limit should be expressed with a string (eg:'64M')
94 * @param string $newlimit the new memory limit
95 * @return bool
97 function reduce_memory_limit ($newlimit) {
98 if (empty($newlimit)) {
99 return false;
101 $cur = @ini_get('memory_limit');
102 if (empty($cur)) {
103 // if php is compiled without --enable-memory-limits
104 // apparently memory_limit is set to ''
105 $cur=0;
106 } else {
107 if ($cur == -1){
108 return true; // unlimited mem!
110 $cur = get_real_size($cur);
113 $new = get_real_size($newlimit);
114 // -1 is smaller, but it means unlimited
115 if ($new < $cur && $new != -1) {
116 ini_set('memory_limit', $newlimit);
117 return true;
119 return false;
123 * Converts numbers like 10M into bytes.
125 * @param mixed $size The size to be converted
126 * @return mixed
128 function get_real_size($size=0) {
129 if (!$size) {
130 return 0;
132 $scan['MB'] = 1048576;
133 $scan['Mb'] = 1048576;
134 $scan['M'] = 1048576;
135 $scan['m'] = 1048576;
136 $scan['KB'] = 1024;
137 $scan['Kb'] = 1024;
138 $scan['K'] = 1024;
139 $scan['k'] = 1024;
141 while (list($key) = each($scan)) {
142 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
143 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
144 break;
147 return $size;
151 * Create a directory.
153 * @uses $CFG
154 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
155 * param bool $shownotices If true then notification messages will be printed out on error.
156 * @return string|false Returns full path to directory if successful, false if not
158 function make_upload_directory($directory, $shownotices=true) {
160 global $CFG;
162 $currdir = $CFG->dataroot;
164 umask(0000);
166 if (!file_exists($currdir)) {
167 if (! mkdir($currdir, $CFG->directorypermissions)) {
168 if ($shownotices) {
169 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
170 $currdir .' with web server write access</div>'."<br />\n";
172 return false;
176 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
177 if (!file_exists($currdir.'/.htaccess')) {
178 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
179 @fwrite($handle, "deny from all\r\nAllowOverride None\r\nNote: this file is broken intentionally, we do not want anybody to undo it in subdirectory!\r\n");
180 @fclose($handle);
184 $dirarray = explode('/', $directory);
186 foreach ($dirarray as $dir) {
187 $currdir = $currdir .'/'. $dir;
188 if (! file_exists($currdir)) {
189 if (! mkdir($currdir, $CFG->directorypermissions)) {
190 if ($shownotices) {
191 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
192 $currdir .')</div>'."<br />\n";
194 return false;
196 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
200 return $currdir;
204 * This function will introspect inside DB to detect it it's a UTF-8 DB or no
205 * Used from setup.php to set correctly "set names" when the installation
206 * process is performed without the initial and beautiful installer
208 function setup_is_unicodedb() {
210 global $CFG, $db, $INSTALL;
212 $unicodedb = false;
214 // Calculate $CFG->dbfamily
215 $dbfamily = set_dbfamily();
217 switch ($dbfamily) {
218 case 'mysql':
219 $rs = $db->Execute("SHOW LOCAL VARIABLES LIKE 'character_set_database'");
220 if ($rs && !$rs->EOF) { // rs_EOF() not available yet
221 $records = $rs->GetAssoc(true);
222 $encoding = $records['character_set_database']['Value'];
223 if (strtoupper($encoding) == 'UTF8') {
224 $unicodedb = true;
227 break;
228 case 'postgres':
229 /// Get PostgreSQL server_encoding value
230 $rs = $db->Execute("SHOW server_encoding");
231 if ($rs && !$rs->EOF) { // rs_EOF() not available yet
232 $encoding = $rs->fields['server_encoding'];
233 if (strtoupper($encoding) == 'UNICODE' || strtoupper($encoding) == 'UTF8') {
234 $unicodedb = true;
237 break;
238 case 'mssql':
239 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
240 $unicodedb = true;
241 break;
242 case 'oracle':
243 /// Get Oracle DB character set value
244 $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
245 if ($rs && !$rs->EOF) { // rs_EOF() not available yet
246 $encoding = $rs->fields['value'];
247 if (strtoupper($encoding) == 'AL32UTF8') {
248 $unicodedb = true;
251 break;
253 return $unicodedb;
257 * This internal function sets and returns the proper value for $CFG->dbfamily based on $CFG->dbtype
258 * It's called by preconfigure_dbconnection() and at install time. Shouldn't be used
259 * in other places. Code should rely on dbfamily to perform conditional execution
260 * instead of using dbtype directly. This allows quicker adoption of different
261 * drivers going against the same DB backend.
263 * This function must contain the init code needed for each dbtype supported.
265 * return string dbfamily value (mysql, postgres, oracle, mssql)
267 function set_dbfamily() {
269 global $CFG, $INSTALL;
271 // Since this function is also used during installation process, i.e. during install.php before $CFG->dbtype is set.
272 // we need to get dbtype from the right variable
273 if (!empty($INSTALL['dbtype'])) {
274 $dbtype = $INSTALL['dbtype'];
275 } else {
276 $dbtype = $CFG->dbtype;
279 switch ($dbtype) {
280 case 'mysql':
281 case 'mysqli':
282 $CFG->dbfamily='mysql';
283 break;
284 case 'postgres7':
285 $CFG->dbfamily='postgres';
286 break;
287 case 'mssql':
288 case 'mssql_n':
289 case 'odbc_mssql':
290 $CFG->dbfamily='mssql';
291 break;
292 case 'oci8po':
293 $CFG->dbfamily='oracle';
294 break;
297 return $CFG->dbfamily;
301 * This internal function, called from setup.php BEFORE stabilishing the DB
302 * connection, defines the $CFG->dbfamily global -by calling set_dbfamily()-
303 * and predefines some constants needed by ADOdb to switch some default
304 * behaviours.
306 * This function must contain all the pre-connection code needed for each
307 * dbtype supported.
309 function preconfigure_dbconnection() {
311 global $CFG;
313 /// Define dbfamily
314 set_dbfamily();
316 /// Based on $CFG->dbfamily, set some ADOdb settings
317 switch ($CFG->dbfamily) {
318 /// list here family types where we know
319 /// the fieldnames will come in lowercase
320 /// so we can avoid expensive tolower()
321 case 'postgres':
322 case 'mysql':
323 case 'mssql':
324 define ('ADODB_ASSOC_CASE', 2);
325 break;
326 case 'oracle':
327 define ('ADODB_ASSOC_CASE', 0); /// Use lowercase fieldnames for ADODB_FETCH_ASSOC
328 /// (only meaningful for oci8po, it's the default
329 /// for other DB drivers so this won't affect them)
330 /// Row prefetching uses a bit of memory but saves a ton
331 /// of network latency. With current AdoDB and PHP, only
332 /// Oracle uses this setting.
333 define ('ADODB_PREFETCH_ROWS', 1000);
334 break;
335 default:
336 /// if we have to lowercase it, set to 0
337 /// - note that the lowercasing is very expensive
338 define ('ADODB_ASSOC_CASE', 0); //Use lowercase fieldnames for ADODB_FETCH_ASSOC
342 function init_memcached() {
343 global $CFG, $MCACHE;
345 include_once($CFG->libdir . '/memcached.class.php');
346 $MCACHE = new memcached;
347 if ($MCACHE->status()) {
348 return true;
350 unset($MCACHE);
351 return false;
354 function init_eaccelerator() {
355 global $CFG, $MCACHE;
357 include_once($CFG->libdir . '/eaccelerator.class.php');
358 $MCACHE = new eaccelerator;
359 if ($MCACHE->status()) {
360 return true;
362 unset($MCACHE);
363 return false;