MDL-15476
[moodle-linuxchix.git] / lib / setuplib.php
blob618e6fe1caaef9213b6a82416a1b853279c496ae
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 * Converts numbers like 10M into bytes.
89 * @param mixed $size The size to be converted
90 * @return mixed
92 function get_real_size($size=0) {
93 if (!$size) {
94 return 0;
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;
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;
111 return $size;
115 * Create a directory.
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
122 function make_upload_directory($directory, $shownotices=true) {
124 global $CFG;
126 $currdir = $CFG->dataroot;
128 umask(0000);
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";
136 return false;
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);
148 $dirarray = explode('/', $directory);
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";
158 return false;
160 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
164 return $currdir;
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
172 function setup_is_unicodedb() {
174 global $CFG, $db, $INSTALL;
176 $unicodedb = false;
178 // Calculate $CFG->dbfamily
179 $dbfamily = set_dbfamily();
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;
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;
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;
215 break;
217 return $unicodedb;
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.
227 * This function must contain the init code needed for each dbtype supported.
229 * return string dbfamily value (mysql, postgres, oracle, mssql)
231 function set_dbfamily() {
233 global $CFG, $INSTALL;
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;
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;
261 return $CFG->dbfamily;
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.
270 * This function must contain all the pre-connection code needed for each
271 * dbtype supported.
273 function preconfigure_dbconnection() {
275 global $CFG;
277 /// Define dbfamily
278 set_dbfamily();
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
306 function init_memcached() {
307 global $CFG, $MCACHE;
309 include_once($CFG->libdir . '/memcached.class.php');
310 $MCACHE = new memcached;
311 if ($MCACHE->status()) {
312 return true;
314 unset($MCACHE);
315 return false;
318 function init_eaccelerator() {
319 global $CFG, $MCACHE;
321 include_once($CFG->libdir . '/eaccelerator.class.php');
322 $MCACHE = new eaccelerator;
323 if ($MCACHE->status()) {
324 return true;
326 unset($MCACHE);
327 return false;