Sfoglia il codice sorgente

Implement the api_set_memory_limit function for to change ini_set memory_limit

Julio Bonifacio 12 anni fa
parent
commit
4e3b438948

+ 1 - 1
main/admin/special_exports.php

@@ -31,7 +31,7 @@ require_once '../coursecopy/classes/CourseRestorer.class.php';
 require_once '../coursecopy/classes/CourseSelectForm.class.php';
 
 if (function_exists('ini_set')) {
-	ini_set('memory_limit','256M');
+	api_set_memory_limit('256M');
 	ini_set('max_execution_time',0);
 }
 

+ 1 - 1
main/coursecopy/copy_course.php

@@ -27,7 +27,7 @@ if (!api_is_allowed_to_edit()) {
 
 // Remove memory and time limits as much as possible as this might be a long process...
 if (function_exists('ini_set')) {
-	ini_set('memory_limit', '256M');
+	api_set_memory_limit('256M');
 	ini_set('max_execution_time', 1800);
     //ini_set('post_max_size', "512M");
 }

+ 1 - 1
main/coursecopy/copy_course_session.php

@@ -36,7 +36,7 @@ if (!api_is_allowed_to_edit() && !api_is_session_admin()) {
 
 // Remove memory and time limits as much as possible as this might be a long process...
 if (function_exists('ini_set')) {
-	ini_set('memory_limit', '256M');
+	api_set_memory_limit('256M');
 	ini_set('max_execution_time', 1800);
 }
 

+ 1 - 1
main/coursecopy/create_backup.php

@@ -27,7 +27,7 @@ if (!api_is_allowed_to_edit()) {
 
 // Remove memory and time limits as much as possible as this might be a long process...
 if (function_exists('ini_set')) {
-	ini_set('memory_limit', '256M');
+	api_set_memory_limit('256M');
 	ini_set('max_execution_time', 1800);
 }
 

+ 1 - 1
main/coursecopy/import_backup.php

@@ -26,7 +26,7 @@ if (!api_is_allowed_to_edit()) {
 
 // Remove memory and time limits as much as possible as this might be a long process...
 if (function_exists('ini_set')) {
-	ini_set('memory_limit', '256M');
+	api_set_memory_limit('256M');
 	ini_set('max_execution_time', 1800);
 }
 

+ 56 - 0
main/inc/lib/main_api.lib.php

@@ -6419,4 +6419,60 @@ function api_set_settings_and_plugins() {
 function api_set_setting_last_update() {
     //Saving latest refresh
     api_set_setting('settings_latest_update', api_get_utc_datetime());
+}
+
+/**
+ * Tries to set memory limit, if authorized and new limit is higher than current
+ * @param string New memory limit
+ * @return bool True on success, false on failure or current is higher than suggested
+ * @assert (null) === false
+ * @assert (-1) === false
+ * @assert (0) === true
+ * @assert ('1G') === true
+ */
+function api_set_memory_limit($mem){
+    //if ini_set() not available, this function is useless
+    if (!function_exists('ini_set') || is_null($mem) || $mem == -1) {
+        return false;
+    }
+
+    $memory_limit = ini_get('memory_limit');
+    if (api_get_bytes_memory_limit($mem) > api_get_bytes_memory_limit($memory_limit)){
+        ini_set('memory_limit', $mem);
+        return true;
+    }
+    return false;
+}
+/**
+ * Gets memory limit in bytes
+ * @param string The memory size (128M, 1G, 1000K, etc)
+ * @return int
+ * @assert (null) === false
+ * @assert ('1t')  === 1099511627776
+ * @assert ('1g')  === 1073741824
+ * @assert ('1m')  === 1048576
+ * @assert ('100k') === 102400
+ */
+function api_get_bytes_memory_limit($mem){
+    $size = strtolower(substr($mem,-1));
+
+    switch ($size) {
+        case 't':
+            $mem = intval(substr($mem,-1))*1024*1024*1024*1024;
+            break;
+        case 'g':
+            $mem = intval(substr($mem,0,-1))*1024*1024*1024;
+            break;
+        case 'm':
+            $mem = intval(substr($mem,0,-1))*1024*1024;
+            break;
+        case 'k':
+            $mem = intval(substr($mem,0,-1))*1024;
+            break;
+        default:
+            // we assume it's integer only
+            $mem = intval($mem);
+            break;
+    }
+    return $mem;
 }

+ 1 - 1
main/inc/lib/sessionmanager.lib.php

@@ -1715,7 +1715,7 @@ class SessionManager {
                 if ($create_new_courses) {
                     //Just in case
                     if (function_exists('ini_set')) {
-                    	ini_set('memory_limit','256M');
+                    	api_set_memory_limit('256M');
                     	ini_set('max_execution_time',0);
                     }
                     $params = array();

+ 1 - 9
main/newscorm/learnpath.class.php

@@ -8158,15 +8158,7 @@ class learnpath {
 
         // Remove memory and time limits as much as possible as this might be a long process...
         if (function_exists('ini_set')) {
-            $mem = ini_get('memory_limit');
-            if (substr($mem, -1, 1) == 'M') {
-                $mem_num = substr($mem, 0, -1);
-                if ($mem_num < 128) {
-                    ini_set('memory_limit', '128M');
-                }
-            } else {
-                ini_set('memory_limit', '128M');
-            }
+            api_set_memory_limit('128M');
             ini_set('max_execution_time', 600);
         }