From bce71973219edf3d3b940a64dbeb55b8ce7fd5ce Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Thu, 18 Nov 2004 02:55:06 +0000 Subject: [PATCH] Merged from MOODLE_14_STABLE moodle--eduforge--1.3.3--patch-335 Now memory_limit can be overriden by higher config settings from php.ini, commandline, httpd.conf, .htaccess --- admin/cron.php | 2 +- backup/backup.php | 2 +- backup/restore.php | 2 +- backup/try.php | 2 +- lib/moodlelib.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++ lib/setup.php | 2 +- 6 files changed, 57 insertions(+), 5 deletions(-) diff --git a/admin/cron.php b/admin/cron.php index 9972b2918f..574886e632 100644 --- a/admin/cron.php +++ b/admin/cron.php @@ -144,7 +144,7 @@ //Execute backup's cron //Perhaps a long time and memory could help in large sites @set_time_limit(0); - @ini_set("memory_limit","128M"); + raise_memory_limit("128M"); if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and file_exists("$CFG->dirroot/backup/backuplib.php") and file_exists("$CFG->dirroot/backup/lib.php") and diff --git a/backup/backup.php b/backup/backup.php index c81fcbed3e..8ccdf1adbe 100644 --- a/backup/backup.php +++ b/backup/backup.php @@ -88,7 +88,7 @@ //Adjust some php variables to the execution of this script @ini_set("max_execution_time","3000"); - @ini_set("memory_limit","128M"); + raise_memory_limit("128M"); //Call the form, depending the step we are if (!$launch) { diff --git a/backup/restore.php b/backup/restore.php index 300fb7bcb6..0a2c011100 100644 --- a/backup/restore.php +++ b/backup/restore.php @@ -95,7 +95,7 @@ //Adjust some php variables to the execution of this script @ini_set("max_execution_time","3000"); - @ini_set("memory_limit","128M"); + raise_memory_limit("memory_limit","128M"); //Call the form, depending the step we are if (!$launch) { diff --git a/backup/try.php b/backup/try.php index 60cd825402..34ad2ff407 100644 --- a/backup/try.php +++ b/backup/try.php @@ -21,7 +21,7 @@ //Adjust some php variables to the execution of this script @ini_set("max_execution_time","3000"); - @ini_set("memory_limit","128M"); + raise_memory_limit("128M"); echo "
\n";
 
diff --git a/lib/moodlelib.php b/lib/moodlelib.php
index 4792523e26..30d22943d7 100644
--- a/lib/moodlelib.php
+++ b/lib/moodlelib.php
@@ -3230,6 +3230,58 @@ function document_file($file, $include=true) {
     return false;
 }
 
+/**
+* Function to raise the memory limit to a new value.
+* Will respect the memory limit if it is higher, thus allowing
+* settings in php.ini, apache conf or command line switches
+* to override it
+*
+* The memory limit should be expressed with a string (eg:'64M')
+* 
+* Return boolean
+*
+* @param    value    string with the new memory limit
+*/
+function raise_memory_limit ($newlimit) {
+
+    if (empty($newlimit)) { 
+        return false;
+    }
+    
+    $cur = return_bytes(@ini_get('memory_limit'));
+    $new = return_bytes($newlimit);
+    
+    if ($new > $cur) {
+        ini_set('memory_limit', $newlimit);
+        return true;    
+    }
+    return false;
+}
+
+/**
+* Function to transform strings like 5M or 128k into bytes.
+* Taken from PHP's documentation (see entry for ini_get())
+*
+* Return integer (in bytes)
+*
+* @param    value    string with the value
+*/
+function return_bytes($val) {
+   $val = trim($val);
+   $last = $val{strlen($val)-1};
+   switch($last) {
+       case 'k':
+       case 'K':
+           return (int) $val * 1024;
+           break;
+       case 'm':
+       case 'M':
+           return (int) $val * 1048576;
+           break;
+       default:
+           return $val;
+   }
+}
 
 /// ENCRYPTION  ////////////////////////////////////////////////
 
diff --git a/lib/setup.php b/lib/setup.php
index 0eb29ecb87..f5cd653f1a 100644
--- a/lib/setup.php
+++ b/lib/setup.php
@@ -126,7 +126,7 @@ global $THEME;
 
 /// Increase memory limits if possible
 
-    @ini_set('memory_limit' , '64M');    // We should never NEED this much but just in case...
+    raise_memory_limit('64M');    // We should never NEED this much but just in case...        
 
 
 /// Load up any configuration from the config table
-- 
2.39.5