Commit | Line | Data |
---|---|---|
ac5f2ccd | 1 | <?php |
50bfb460 SB |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
50bfb460 | 5 | | | |
bc77d7c0 TO |
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 | | |
50bfb460 SB |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * @package CRM | |
ca5cec67 | 14 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
50bfb460 | 15 | */ |
5bc392e6 EM |
16 | |
17 | /** | |
18 | * Class CRM_Utils_Number | |
19 | */ | |
ac5f2ccd | 20 | class CRM_Utils_Number { |
6714d8d2 | 21 | |
ac5f2ccd | 22 | /** |
fe482240 | 23 | * Create a random number with a given precision. |
ac5f2ccd | 24 | * |
77855840 TO |
25 | * @param array $precision |
26 | * (int $significantDigits, int $postDecimalDigits). | |
f4aaa82a EM |
27 | * |
28 | * @return float | |
50bfb460 | 29 | * |
ac5f2ccd TO |
30 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html |
31 | */ | |
00be9182 | 32 | public static function createRandomDecimal($precision) { |
ac5f2ccd TO |
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 | |
77855840 TO |
44 | * @param array $precision |
45 | * (int $significantDigits, int $postDecimalDigits). | |
ac5f2ccd TO |
46 | * @return float |
47 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html | |
48 | */ | |
00be9182 | 49 | public static function createTruncatedDecimal($keyValue, $precision) { |
ac5f2ccd TO |
50 | list ($sigFigs, $decFigs) = $precision; |
51 | $sign = ($keyValue < 0) ? '-1' : 1; | |
6714d8d2 SL |
52 | // ex: -123.456 ==> 123456 |
53 | $val = str_replace('.', '', abs($keyValue)); | |
54 | // ex: 123456 => 1234 | |
55 | $val = substr($val, 0, $sigFigs); | |
ac5f2ccd TO |
56 | |
57 | // Move any extra digits after decimal | |
58 | $extraFigs = strlen($val) - ($sigFigs - $decFigs); | |
59 | if ($extraFigs > 0) { | |
6714d8d2 SL |
60 | // ex: 1234 => 1.234 |
61 | return $sign * $val / pow(10, $extraFigs); | |
ac5f2ccd TO |
62 | } |
63 | else { | |
64 | return $sign * $val; | |
65 | } | |
66 | } | |
96025800 | 67 | |
2e966dd5 TO |
68 | /** |
69 | * Some kind of numbery-looky-printy thing. | |
54957108 | 70 | * |
71 | * @param string $size | |
72 | * @param bool $checkForPostMax | |
73 | * | |
74 | * @return int | |
2e966dd5 TO |
75 | */ |
76 | public static function formatUnitSize($size, $checkForPostMax = FALSE) { | |
77 | if ($size) { | |
84ce59b1 | 78 | $last = strtolower($size[strlen($size) - 1]); |
8bdaf803 | 79 | $size = (int) $size; |
2e966dd5 TO |
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 | } | |
50bfb460 | 97 | // respect php.ini upload_max_filesize |
2e966dd5 TO |
98 | if ($size > $maxImportFileSize && $size !== $postMaxSize) { |
99 | $size = $maxImportFileSize; | |
be2fb01f | 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"); |
2e966dd5 TO |
101 | } |
102 | } | |
103 | return $size; | |
104 | } | |
105 | } | |
106 | ||
f4aaa82a | 107 | } |