2 // These functions are required very early in the Moodle
3 // setup process, before any of the main libraries are
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.
22 function init_performance_info() {
24 global $PERF, $CFG, $USER;
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')) {
40 if ($USER->id
> 0 && $CFG->perfdebug
>= 15) {
41 $tempdir = $CFG->dataroot
. '/temp/profile/' . $USER->id
;
43 apd_set_pprof_trace($tempdir);
44 $PERF->profiling
= true;
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
55 * The memory limit should be expressed with a string (eg:'64M')
57 * @param string $newlimit the new memory limit
60 function raise_memory_limit ($newlimit) {
62 if (empty($newlimit)) {
66 $cur = @ini_get
('memory_limit');
68 // if php is compiled without --enable-memory-limits
69 // apparently memory_limit is set to ''
73 return true; // unlimited mem!
75 $cur = get_real_size($cur);
78 $new = get_real_size($newlimit);
80 ini_set('memory_limit', $newlimit);
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
92 * The memory limit should be expressed with a string (eg:'64M')
94 * @param string $newlimit the new memory limit
97 function reduce_memory_limit ($newlimit) {
98 if (empty($newlimit)) {
101 $cur = @ini_get
('memory_limit');
103 // if php is compiled without --enable-memory-limits
104 // apparently memory_limit is set to ''
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);
123 * Converts numbers like 10M into bytes.
125 * @param mixed $size The size to be converted
128 function get_real_size($size=0) {
132 $scan['MB'] = 1048576;
133 $scan['Mb'] = 1048576;
134 $scan['M'] = 1048576;
135 $scan['m'] = 1048576;
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];
151 * Create a directory.
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) {
162 $currdir = $CFG->dataroot
;
166 if (!file_exists($currdir)) {
167 if (! mkdir($currdir, $CFG->directorypermissions
)) {
169 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
170 $currdir .' with web server write access</div>'."<br />\n";
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");
184 $dirarray = explode('/', $directory);
186 foreach ($dirarray as $dir) {
187 $currdir = $currdir .'/'. $dir;
188 if (! file_exists($currdir)) {
189 if (! mkdir($currdir, $CFG->directorypermissions
)) {
191 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
192 $currdir .')</div>'."<br />\n";
196 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
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;
214 // Calculate $CFG->dbfamily
215 $dbfamily = set_dbfamily();
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') {
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') {
239 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
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') {
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'];
276 $dbtype = $CFG->dbtype
;
282 $CFG->dbfamily
='mysql';
285 $CFG->dbfamily
='postgres';
290 $CFG->dbfamily
='mssql';
293 $CFG->dbfamily
='oracle';
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
306 * This function must contain all the pre-connection code needed for each
309 function preconfigure_dbconnection() {
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()
324 define ('ADODB_ASSOC_CASE', 2);
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);
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()) {
354 function init_eaccelerator() {
355 global $CFG, $MCACHE;
357 include_once($CFG->libdir
. '/eaccelerator.class.php');
358 $MCACHE = new eaccelerator
;
359 if ($MCACHE->status()) {