| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | Copyright CiviCRM LLC. All rights reserved. | |
| 5 | | | |
| 6 | | This work is published under the GNU AGPLv3 license with some | |
| 7 | | permitted exceptions and without any warranty. For full license | |
| 8 | | and copyright information, see https://civicrm.org/licensing | |
| 9 | +--------------------------------------------------------------------+ |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @package CRM |
| 14 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Class CRM_Utils_Number |
| 19 | */ |
| 20 | class CRM_Utils_Number { |
| 21 | |
| 22 | /** |
| 23 | * Create a random number with a given precision. |
| 24 | * |
| 25 | * @param array $precision |
| 26 | * (int $significantDigits, int $postDecimalDigits). |
| 27 | * |
| 28 | * @return float |
| 29 | * |
| 30 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html |
| 31 | */ |
| 32 | public static function createRandomDecimal($precision) { |
| 33 | list ($sigFigs, $decFigs) = $precision; |
| 34 | $rand = rand(0, pow(10, $sigFigs) - 1); |
| 35 | return $rand / pow(10, $decFigs); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Given a number, coerce it to meet the precision requirement. If possible, it should |
| 40 | * keep the number as-is. If necessary, this may drop the least-significant digits |
| 41 | * and/or move the decimal place. |
| 42 | * |
| 43 | * @param int|float $keyValue |
| 44 | * @param array $precision |
| 45 | * (int $significantDigits, int $postDecimalDigits). |
| 46 | * @return float |
| 47 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html |
| 48 | */ |
| 49 | public static function createTruncatedDecimal($keyValue, $precision) { |
| 50 | list ($sigFigs, $decFigs) = $precision; |
| 51 | $sign = ($keyValue < 0) ? '-1' : 1; |
| 52 | // ex: -123.456 ==> 123456 |
| 53 | $val = str_replace('.', '', abs($keyValue)); |
| 54 | // ex: 123456 => 1234 |
| 55 | $val = substr($val, 0, $sigFigs); |
| 56 | |
| 57 | // Move any extra digits after decimal |
| 58 | $extraFigs = strlen($val) - ($sigFigs - $decFigs); |
| 59 | if ($extraFigs > 0) { |
| 60 | // ex: 1234 => 1.234 |
| 61 | return $sign * $val / pow(10, $extraFigs); |
| 62 | } |
| 63 | else { |
| 64 | return $sign * $val; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Some kind of numbery-looky-printy thing. |
| 70 | * |
| 71 | * @param string $size |
| 72 | * @param bool $checkForPostMax |
| 73 | * |
| 74 | * @return int |
| 75 | */ |
| 76 | public static function formatUnitSize($size, $checkForPostMax = FALSE) { |
| 77 | if ($size) { |
| 78 | $last = strtolower($size[strlen($size) - 1]); |
| 79 | $size = (int) $size; |
| 80 | switch ($last) { |
| 81 | // The 'G' modifier is available since PHP 5.1.0 |
| 82 | |
| 83 | case 'g': |
| 84 | $size *= 1024; |
| 85 | case 'm': |
| 86 | $size *= 1024; |
| 87 | case 'k': |
| 88 | $size *= 1024; |
| 89 | } |
| 90 | |
| 91 | if ($checkForPostMax) { |
| 92 | $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize')); |
| 93 | $postMaxSize = self::formatUnitSize(ini_get('post_max_size')); |
| 94 | if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) { |
| 95 | 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"); |
| 96 | } |
| 97 | // respect php.ini upload_max_filesize |
| 98 | if ($size > $maxImportFileSize && $size !== $postMaxSize) { |
| 99 | $size = $maxImportFileSize; |
| 100 | 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.", [1 => CRM_Utils_System::url('civicrm/admin/setting/misc', 'reset=1')]), ts("Warning"), "alert"); |
| 101 | } |
| 102 | } |
| 103 | return $size; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | } |