Commit | Line | Data |
---|---|---|
ac5f2ccd | 1 | <?php |
50bfb460 SB |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | |
0f03f337 | 30 | * @copyright CiviCRM LLC (c) 2004-2017 |
50bfb460 | 31 | */ |
5bc392e6 EM |
32 | |
33 | /** | |
34 | * Class CRM_Utils_Number | |
35 | */ | |
ac5f2ccd TO |
36 | class CRM_Utils_Number { |
37 | /** | |
fe482240 | 38 | * Create a random number with a given precision. |
ac5f2ccd | 39 | * |
77855840 TO |
40 | * @param array $precision |
41 | * (int $significantDigits, int $postDecimalDigits). | |
f4aaa82a EM |
42 | * |
43 | * @return float | |
50bfb460 | 44 | * |
ac5f2ccd TO |
45 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html |
46 | */ | |
00be9182 | 47 | public static function createRandomDecimal($precision) { |
ac5f2ccd TO |
48 | list ($sigFigs, $decFigs) = $precision; |
49 | $rand = rand(0, pow(10, $sigFigs) - 1); | |
50 | return $rand / pow(10, $decFigs); | |
51 | } | |
52 | ||
53 | /** | |
54 | * Given a number, coerce it to meet the precision requirement. If possible, it should | |
55 | * keep the number as-is. If necessary, this may drop the least-significant digits | |
56 | * and/or move the decimal place. | |
57 | * | |
58 | * @param int|float $keyValue | |
77855840 TO |
59 | * @param array $precision |
60 | * (int $significantDigits, int $postDecimalDigits). | |
ac5f2ccd TO |
61 | * @return float |
62 | * @link https://dev.mysql.com/doc/refman/5.1/en/fixed-point-types.html | |
63 | */ | |
00be9182 | 64 | public static function createTruncatedDecimal($keyValue, $precision) { |
ac5f2ccd TO |
65 | list ($sigFigs, $decFigs) = $precision; |
66 | $sign = ($keyValue < 0) ? '-1' : 1; | |
67 | $val = str_replace('.', '', abs($keyValue)); // ex: -123.456 ==> 123456 | |
50bfb460 | 68 | $val = substr($val, 0, $sigFigs); // ex: 123456 => 1234 |
ac5f2ccd TO |
69 | |
70 | // Move any extra digits after decimal | |
71 | $extraFigs = strlen($val) - ($sigFigs - $decFigs); | |
72 | if ($extraFigs > 0) { | |
73 | return $sign * $val / pow(10, $extraFigs); // ex: 1234 => 1.234 | |
74 | } | |
75 | else { | |
76 | return $sign * $val; | |
77 | } | |
78 | } | |
96025800 | 79 | |
2e966dd5 TO |
80 | /** |
81 | * Some kind of numbery-looky-printy thing. | |
54957108 | 82 | * |
83 | * @param string $size | |
84 | * @param bool $checkForPostMax | |
85 | * | |
86 | * @return int | |
2e966dd5 TO |
87 | */ |
88 | public static function formatUnitSize($size, $checkForPostMax = FALSE) { | |
89 | if ($size) { | |
90 | $last = strtolower($size{strlen($size) - 1}); | |
91 | switch ($last) { | |
92 | // The 'G' modifier is available since PHP 5.1.0 | |
93 | ||
94 | case 'g': | |
95 | $size *= 1024; | |
96 | case 'm': | |
97 | $size *= 1024; | |
98 | case 'k': | |
99 | $size *= 1024; | |
100 | } | |
101 | ||
102 | if ($checkForPostMax) { | |
103 | $maxImportFileSize = self::formatUnitSize(ini_get('upload_max_filesize')); | |
104 | $postMaxSize = self::formatUnitSize(ini_get('post_max_size')); | |
105 | if ($maxImportFileSize > $postMaxSize && $postMaxSize == $size) { | |
106 | 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"); | |
107 | } | |
50bfb460 | 108 | // respect php.ini upload_max_filesize |
2e966dd5 TO |
109 | if ($size > $maxImportFileSize && $size !== $postMaxSize) { |
110 | $size = $maxImportFileSize; | |
111 | 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"); | |
112 | } | |
113 | } | |
114 | return $size; | |
115 | } | |
116 | } | |
117 | ||
f4aaa82a | 118 | } |