Commit | Line | Data |
---|---|---|
ac5f2ccd | 1 | <?php |
50bfb460 SB |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
50bfb460 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
50bfb460 SB |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * @package CRM | |
6b83d5bd | 30 | * @copyright CiviCRM LLC (c) 2004-2019 |
50bfb460 | 31 | */ |
5bc392e6 EM |
32 | |
33 | /** | |
34 | * Class CRM_Utils_Number | |
35 | */ | |
ac5f2ccd | 36 | class CRM_Utils_Number { |
6714d8d2 | 37 | |
ac5f2ccd | 38 | /** |
fe482240 | 39 | * Create a random number with a given precision. |
ac5f2ccd | 40 | * |
77855840 TO |
41 | * @param array $precision |
42 | * (int $significantDigits, int $postDecimalDigits). | |
f4aaa82a EM |
43 | * |
44 | * @return float | |
50bfb460 | 45 | * |
ac5f2ccd TO |
46 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html |
47 | */ | |
00be9182 | 48 | public static function createRandomDecimal($precision) { |
ac5f2ccd TO |
49 | list ($sigFigs, $decFigs) = $precision; |
50 | $rand = rand(0, pow(10, $sigFigs) - 1); | |
51 | return $rand / pow(10, $decFigs); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Given a number, coerce it to meet the precision requirement. If possible, it should | |
56 | * keep the number as-is. If necessary, this may drop the least-significant digits | |
57 | * and/or move the decimal place. | |
58 | * | |
59 | * @param int|float $keyValue | |
77855840 TO |
60 | * @param array $precision |
61 | * (int $significantDigits, int $postDecimalDigits). | |
ac5f2ccd TO |
62 | * @return float |
63 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html | |
64 | */ | |
00be9182 | 65 | public static function createTruncatedDecimal($keyValue, $precision) { |
ac5f2ccd TO |
66 | list ($sigFigs, $decFigs) = $precision; |
67 | $sign = ($keyValue < 0) ? '-1' : 1; | |
6714d8d2 SL |
68 | // ex: -123.456 ==> 123456 |
69 | $val = str_replace('.', '', abs($keyValue)); | |
70 | // ex: 123456 => 1234 | |
71 | $val = substr($val, 0, $sigFigs); | |
ac5f2ccd TO |
72 | |
73 | // Move any extra digits after decimal | |
74 | $extraFigs = strlen($val) - ($sigFigs - $decFigs); | |
75 | if ($extraFigs > 0) { | |
6714d8d2 SL |
76 | // ex: 1234 => 1.234 |
77 | return $sign * $val / pow(10, $extraFigs); | |
ac5f2ccd TO |
78 | } |
79 | else { | |
80 | return $sign * $val; | |
81 | } | |
82 | } | |
96025800 | 83 | |
2e966dd5 TO |
84 | /** |
85 | * Some kind of numbery-looky-printy thing. | |
54957108 | 86 | * |
87 | * @param string $size | |
88 | * @param bool $checkForPostMax | |
89 | * | |
90 | * @return int | |
2e966dd5 TO |
91 | */ |
92 | public static function formatUnitSize($size, $checkForPostMax = FALSE) { | |
93 | if ($size) { | |
94 | $last = strtolower($size{strlen($size) - 1}); | |
8bdaf803 | 95 | $size = (int) $size; |
2e966dd5 TO |
96 | switch ($last) { |
97 | // The 'G' modifier is available since PHP 5.1.0 | |
98 | ||
99 | case 'g': | |
100 | $size *= 1024; | |
101 | case 'm': | |
102 | $size *= 1024; | |
103 | case 'k': | |
104 | $size *= 1024; | |
105 | } | |
106 | ||
107 | if ($checkForPostMax) { | |
108 | $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize')); | |
109 | $postMaxSize = self::formatUnitSize(ini_get('post_max_size')); | |
110 | if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) { | |
111 | 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"); | |
112 | } | |
50bfb460 | 113 | // respect php.ini upload_max_filesize |
2e966dd5 TO |
114 | if ($size > $maxImportFileSize && $size !== $postMaxSize) { |
115 | $size = $maxImportFileSize; | |
be2fb01f | 116 | 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 |
117 | } |
118 | } | |
119 | return $size; | |
120 | } | |
121 | } | |
122 | ||
f4aaa82a | 123 | } |