CRM-16373 - Move formatUnitSize() from CRM_Core_Config to CRM_Utils_Number
[civicrm-core.git] / CRM / Utils / Number.php
index 343a73203d855121e3a2f032ac6206a1b5b050c8..f3e427acd2a93eeb7d3605dc82fdf7b8bae6a9fc 100644 (file)
@@ -5,9 +5,10 @@
  */
 class CRM_Utils_Number {
   /**
-   * Create a random number with a given precision
+   * Create a random number with a given precision.
    *
-   * @param array $precision (int $significantDigits, int $postDecimalDigits)
+   * @param array $precision
+   *   (int $significantDigits, int $postDecimalDigits).
    *
    * @return float
    * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
@@ -24,7 +25,8 @@ class CRM_Utils_Number {
    * and/or move the decimal place.
    *
    * @param int|float $keyValue
-   * @param array $precision (int $significantDigits, int $postDecimalDigits)
+   * @param array $precision
+   *   (int $significantDigits, int $postDecimalDigits).
    * @return float
    * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html
    */
@@ -43,4 +45,38 @@ class CRM_Utils_Number {
       return $sign * $val;
     }
   }
+
+  /**
+   * Some kind of numbery-looky-printy thing.
+   */
+  public static function formatUnitSize($size, $checkForPostMax = FALSE) {
+    if ($size) {
+      $last = strtolower($size{strlen($size) - 1});
+      switch ($last) {
+        // The 'G' modifier is available since PHP 5.1.0
+
+        case 'g':
+          $size *= 1024;
+        case 'm':
+          $size *= 1024;
+        case 'k':
+          $size *= 1024;
+      }
+
+      if ($checkForPostMax) {
+        $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize'));
+        $postMaxSize = self::formatUnitSize(ini_get('post_max_size'));
+        if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) {
+          CRM_Core_Session::setStatus(ts("Note: Upload max filesize ('upload_max_filesize') should not exceed Post max size ('post_max_size') as defined in PHP.ini, please check with your system administrator."), ts("Warning"), "alert");
+        }
+        //respect php.ini upload_max_filesize
+        if ($size > $maxImportFileSize && $size !== $postMaxSize) {
+          $size = $maxImportFileSize;
+          CRM_Core_Session::setStatus(ts("Note: Please verify your configuration for Maximum File Size (in MB) <a href='%1'>Administrator >> System Settings >> Misc</a>. It should support 'upload_max_size' as defined in PHP.ini.Please check with your system administrator.", array(1 => CRM_Utils_System::url('civicrm/admin/setting/misc', 'reset=1'))), ts("Warning"), "alert");
+        }
+      }
+      return $size;
+    }
+  }
+
 }