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 * Converts numbers like 10M into bytes.
89 * @param mixed $size The size to be converted
92 function get_real_size($size=0) {
96 $scan['MB'] = 1048576;
97 $scan['Mb'] = 1048576;
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];
115 * Create a directory.
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) {
126 $currdir = $CFG->dataroot
;
130 if (!file_exists($currdir)) {
131 if (! mkdir($currdir, $CFG->directorypermissions
)) {
133 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
134 $currdir .' with web server write access</div>'."<br />\n";
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");
148 $dirarray = explode('/', $directory);
150 foreach ($dirarray as $dir) {
151 $currdir = $currdir .'/'. $dir;
152 if (! file_exists($currdir)) {
153 if (! mkdir($currdir, $CFG->directorypermissions
)) {
155 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
156 $currdir .')</div>'."<br />\n";
160 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
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;
178 // Calculate $CFG->dbfamily
179 $dbfamily = set_dbfamily();
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') {
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') {
203 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
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') {
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'];
240 $dbtype = $CFG->dbtype
;
246 $CFG->dbfamily
='mysql';
249 $CFG->dbfamily
='postgres';
254 $CFG->dbfamily
='mssql';
257 $CFG->dbfamily
='oracle';
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
270 * This function must contain all the pre-connection code needed for each
273 function preconfigure_dbconnection() {
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()
288 define ('ADODB_ASSOC_CASE', 2);
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);
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()) {
318 function init_eaccelerator() {
319 global $CFG, $MCACHE;
321 include_once($CFG->libdir
. '/eaccelerator.class.php');
322 $MCACHE = new eaccelerator
;
323 if ($MCACHE->status()) {