2 // These functions are required very early in the Moodle
3 // setup process, before any of the main libraries are
8 * Initializes our performance info early.
10 * Pairs up with get_performance_info() which is actually
11 * in moodlelib.php. This function is here so that we can
12 * call it before all the libs are pulled in.
16 function init_performance_info() {
18 global $PERF, $CFG, $USER;
23 if (function_exists('microtime')) {
24 $PERF->starttime
= microtime();
26 if (function_exists('memory_get_usage')) {
27 $PERF->startmemory
= memory_get_usage();
29 if (function_exists('posix_times')) {
30 $PERF->startposixtimes
= posix_times();
32 if (function_exists('apd_set_pprof_trace')) {
34 if ($USER->id
> 0 && $CFG->perfdebug
>= 15) {
35 $tempdir = $CFG->dataroot
. '/temp/profile/' . $USER->id
;
37 apd_set_pprof_trace($tempdir);
38 $PERF->profiling
= true;
44 * Function to raise the memory limit to a new value.
45 * Will respect the memory limit if it is higher, thus allowing
46 * settings in php.ini, apache conf or command line switches
49 * The memory limit should be expressed with a string (eg:'64M')
51 * @param string $newlimit the new memory limit
54 function raise_memory_limit ($newlimit) {
56 if (empty($newlimit)) {
60 $cur = @ini_get
('memory_limit');
62 // if php is compiled without --enable-memory-limits
63 // apparently memory_limit is set to ''
67 return true; // unlimited mem!
69 $cur = get_real_size($cur);
72 $new = get_real_size($newlimit);
74 ini_set('memory_limit', $newlimit);
81 * Converts numbers like 10M into bytes.
83 * @param mixed $size The size to be converted
86 function get_real_size($size=0) {
90 $scan['MB'] = 1048576;
91 $scan['Mb'] = 1048576;
99 while (list($key) = each($scan)) {
100 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
101 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
109 * Create a directory.
112 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
113 * param bool $shownotices If true then notification messages will be printed out on error.
114 * @return string|false Returns full path to directory if successful, false if not
116 function make_upload_directory($directory, $shownotices=true) {
120 $currdir = $CFG->dataroot
;
124 if (!file_exists($currdir)) {
125 if (! mkdir($currdir, $CFG->directorypermissions
)) {
127 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
128 $currdir .' with web server write access</div>'."<br />\n";
134 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
135 if (!file_exists($currdir.'/.htaccess')) {
136 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
137 @fwrite
($handle, "deny from all\r\nAllowOverride None\r\n");
142 $dirarray = explode('/', $directory);
144 foreach ($dirarray as $dir) {
145 $currdir = $currdir .'/'. $dir;
146 if (! file_exists($currdir)) {
147 if (! mkdir($currdir, $CFG->directorypermissions
)) {
149 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
150 $currdir .')</div>'."<br />\n";
154 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
162 * This function will introspect inside DB to detect it it's a UTF-8 DB or no
163 * Used from setup.php to set correctly "set names" when the installation
164 * process is performed without the initial and beautiful installer
166 function setup_is_unicodedb() {
168 global $CFG, $db, $INSTALL;
172 // Calculate $CFG->dbfamily
173 $dbfamily = set_dbfamily();
177 $rs = $db->Execute("SHOW VARIABLES LIKE 'character_set_database'");
178 if ($rs && $rs->RecordCount() > 0) {
179 $records = $rs->GetAssoc(true);
180 $encoding = $records['character_set_database']['Value'];
181 if (strtoupper($encoding) == 'UTF8') {
187 /// Get PostgreSQL server_encoding value
188 $rs = $db->Execute("SHOW server_encoding");
189 if ($rs && $rs->RecordCount() > 0) {
190 $encoding = $rs->fields
['server_encoding'];
191 if (strtoupper($encoding) == 'UNICODE' ||
strtoupper($encoding) == 'UTF8') {
197 /// MSSQL only runs under UTF8 + the proper ODBTP driver (both for Unix and Win32)
201 /// Get Oracle DB character set value
202 $rs = $db->Execute("SELECT parameter, value FROM nls_database_parameters where parameter = 'NLS_CHARACTERSET'");
203 if ($rs && $rs->RecordCount() > 0) {
204 $encoding = $rs->fields
['value'];
205 if (strtoupper($encoding) == 'AL32UTF8') {
215 * This internal function sets and returns the proper value for $CFG->dbfamily based on $CFG->dbtype
216 * It's called by configure_dbconnection() and at install time. Shouldn't be used
217 * in other places. Code should rely on dbfamily to perform conditional execution
218 * instead of using dbtype directly. This allows quicker adoption of different
219 * drivers going against the same DB backend.
221 * This function must contain the init code needed for each dbtype supported.
223 * return string dbfamily value (mysql, postgres, oracle, mssql)
225 function set_dbfamily() {
227 global $CFG, $INSTALL;
229 // Since this function is also used during installation process, i.e. during install.php before $CFG->dbtype is set.
230 // we need to get dbtype from the right variable
231 if (!empty($INSTALL['dbtype'])) {
232 $dbtype = $INSTALL['dbtype'];
234 $dbtype = $CFG->dbtype
;
240 $CFG->dbfamily
='mysql';
243 $CFG->dbfamily
='postgres';
248 $CFG->dbfamily
='mssql';
251 $CFG->dbfamily
='oracle';
255 return $CFG->dbfamily
;
258 function init_memcached() {
259 global $CFG, $MCACHE;
261 include_once($CFG->libdir
. '/memcached.class.php');
262 $MCACHE = new memcached
;
263 if ($MCACHE->status()) {
270 function init_eaccelerator() {
271 global $CFG, $MCACHE;
273 include_once($CFG->libdir
. '/eaccelerator.class.php');
274 $MCACHE = new eaccelerator
;
275 if ($MCACHE->status()) {