MDL-14864 Removed old $db variable, replaced by global $DB. This was breaking install...
[moodle.git] / lib / setuplib.php
CommitLineData
d3f9f1f8 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.
5
6
6a525ce2 7/**
8 * Simple class
9 */
10class object {};
11
12
d3f9f1f8 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 */
22function init_performance_info() {
23
6fc4ad72 24 global $PERF, $CFG, $USER;
b65567f4 25
d3f9f1f8 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 }
b65567f4 38 if (function_exists('apd_set_pprof_trace')) {
39 // APD profiling
6fc4ad72 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 }
b65567f4 46 }
d3f9f1f8 47}
48
76f3815b 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 */
60function 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 */
92function 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
d3f9f1f8 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 */
122function 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 }
d100b8a0 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')) {
d3f9f1f8 142 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
d389b128 143 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
d3f9f1f8 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
419e1d93 167function init_memcached() {
168 global $CFG, $MCACHE;
169
f917d0ea 170 include_once($CFG->libdir . '/memcached.class.php');
171 $MCACHE = new memcached;
172 if ($MCACHE->status()) {
173 return true;
174 }
175 unset($MCACHE);
176 return false;
419e1d93 177}
178
2142d492 179function init_eaccelerator() {
180 global $CFG, $MCACHE;
181
182 include_once($CFG->libdir . '/eaccelerator.class.php');
183 $MCACHE = new eaccelerator;
f917d0ea 184 if ($MCACHE->status()) {
2142d492 185 return true;
186 }
187 unset($MCACHE);
188 return false;
189}
190
191
192
d3f9f1f8 193?>