PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/gui/library/calc-functions.php

https://bitbucket.org/droidzone/i-mscp
PHP | 338 lines | 178 code | 30 blank | 130 comment | 31 complexity | 6e0620c46a9cf30e0023e63c22f219a7 MD5 | raw file
  1. <?php
  2. /**
  3. * i-MSCP - internet Multi Server Control Panel
  4. *
  5. * The contents of this file are subject to the Mozilla Public License
  6. * Version 1.1 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS"
  11. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  12. * License for the specific language governing rights and limitations
  13. * under the License.
  14. *
  15. * The Original Code is "VHCS - Virtual Hosting Control System".
  16. *
  17. * The Initial Developer of the Original Code is moleSoftware GmbH.
  18. * Portions created by Initial Developer are Copyright (C) 2001-2006
  19. * by moleSoftware GmbH. All Rights Reserved.
  20. * Portions created by the ispCP Team are Copyright (C) 2006-2010 by
  21. * isp Control Panel. All Rights Reserved.
  22. * Portions created by the i-MSCP Team are Copyright (C) 2010 by
  23. * i-MSCP - internet Multi Server Control Panel. All Rights Reserved.
  24. *
  25. * @copyright 2001-2006 by moleSoftware GmbH
  26. * @copyright 2006-2010 by ispCP | http://isp-control.net
  27. * @copyright 2010-2013 by i-MSCP | http://i-mscp.net
  28. * @link http://i-mscp.net
  29. * @author ispCP Team
  30. * @author i-MSCP Team
  31. */
  32. /**
  33. * @param $crnt
  34. * @param $max
  35. * @param $bars_max
  36. * @return array
  37. */
  38. function calc_bars($crnt, $max, $bars_max)
  39. {
  40. if ($max != 0) {
  41. $percent_usage = (100 * $crnt) / $max;
  42. } else {
  43. $percent_usage = 0;
  44. }
  45. $bars = ($percent_usage * $bars_max) / 100;
  46. if ($bars > $bars_max) {
  47. $bars = $bars_max;
  48. }
  49. return array(
  50. sprintf("%.2f", $percent_usage),
  51. sprintf("%d", $bars)
  52. );
  53. }
  54. /**
  55. * Turns byte counts to usual readable format.
  56. *
  57. * If you feel like a hard-drive manufacturer, you can start counting bytes by power
  58. * of 1000 (instead of the generous 1024). Just set $power to 1000.
  59. *
  60. * But if you are a floppy disk manufacturer and want to start counting in units of
  61. * 1024 (for your "1.44 MB" disks ?) let the default value for $power.
  62. *
  63. * The units for power 1000 are:
  64. * ('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB')
  65. *
  66. * Those for power 1024 are:
  67. *
  68. * ('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB')
  69. *
  70. * with the horrible names: bytes, kibibytes, mebibytes, etc.
  71. *
  72. * @see http://physics.nist.gov/cuu/Units/binary.html
  73. * @throws iMSCP_Exception if $base is wrong
  74. * @param int|float $bytes Bytes value to convert
  75. * @param string $unit OPTIONAL Unit to format
  76. * @param int $decimals OPTIONAL Number of decimal to be show
  77. * @param int $power OPTIONAL Power to use for conversion (1024 or 1000)
  78. * @return string
  79. */
  80. function bytesHuman($bytes, $unit = null, $decimals = 2, $power = 1024)
  81. {
  82. if ($power == 1000) {
  83. $units = array('B' => 0, 'kB' => 1, 'MB' => 2, 'GB' => 3, 'TB' => 4,
  84. 'PB' => 5, 'EB' => 6, 'ZB' => 7, 'YB' => 8);
  85. } elseif ($power == 1024) {
  86. $units = array('B' => 0, 'kiB' => 1, 'MiB' => 2, 'GiB' => 3, 'TiB' => 4,
  87. 'PiB' => 5, 'EiB' => 6, 'ZiB' => 7, 'YiB' => 8);
  88. } else {
  89. throw new iMSCP_Exception('Wrong value given for $base.');
  90. }
  91. $value = 0;
  92. if ($bytes > 0) {
  93. if (!array_key_exists($unit, $units)) {
  94. $pow = floor(log($bytes) / log($power));
  95. $unit = array_search($pow, $units);
  96. }
  97. $value = ($bytes / pow($power, floor($units[$unit])));
  98. } else {
  99. $unit = 'B';
  100. }
  101. // If decimals is not numeric or decimals is less than 0
  102. // then set default value
  103. if (!is_numeric($decimals) || $decimals < 0) {
  104. $decimals = 2;
  105. }
  106. // units Translation
  107. switch ($unit) {
  108. case 'B':
  109. $unit = tr('B');
  110. break;
  111. case 'kB':
  112. $unit = tr('kB');
  113. break;
  114. case 'kiB':
  115. $unit = tr('kiB');
  116. break;
  117. case 'MB':
  118. $unit = tr('MB');
  119. break;
  120. case 'MiB':
  121. $unit = tr('MiB');
  122. break;
  123. case 'GB':
  124. $unit = tr('GB');
  125. break;
  126. case 'GiB':
  127. $unit = tr('GiB');
  128. break;
  129. case 'TB':
  130. $unit = tr('TB');
  131. break;
  132. case 'TiB':
  133. $unit = tr('TiB');
  134. break;
  135. case 'PB':
  136. $unit = tr('PB');
  137. break;
  138. case 'PiB':
  139. $unit = tr('PiB');
  140. break;
  141. case 'EB':
  142. $unit = tr('EB');
  143. break;
  144. case 'EiB':
  145. $unit = tr('EiB');
  146. break;
  147. case 'ZB':
  148. $unit = tr('ZB');
  149. break;
  150. case 'ZiB':
  151. $unit = tr('ZiB');
  152. break;
  153. case 'YB':
  154. $unit = tr('YB');
  155. break;
  156. case 'YiB':
  157. $unit = tr('YiB');
  158. break;
  159. }
  160. return sprintf('%.' . $decimals . 'f ' . $unit, $value);
  161. }
  162. /**
  163. * Humanize a mebibyte value.
  164. *
  165. * @param int $value mebibyte value
  166. * @param $unit
  167. * @return string
  168. */
  169. function mebibyteHuman($value, $unit = null)
  170. {
  171. return bytesHuman($value * 1048576, $unit);
  172. }
  173. /**
  174. * Translates '-1', 'no', 'yes', '0' or mebibyte value string into human readable string.
  175. *
  176. * @param int $value variable to be translated
  177. * @param bool $autosize calculate value in different unit (default false)
  178. * @param string $to unit to calclulate to (default 'MiB')
  179. * @return String
  180. */
  181. function translate_limit_value($value, $autosize = false, $to = 'MiB')
  182. {
  183. switch ($value) {
  184. case '-1':
  185. return tr('disabled');
  186. case '0':
  187. return tr('unlimited');
  188. default:
  189. return (!$autosize) ? $value : mebibyteHuman($value, $to);
  190. }
  191. }
  192. /**
  193. * Generates a random salt for password using the best available algorithm.
  194. *
  195. * Note: Only algorithms present in the mainline glibc >= 2.7 (Debian) are supported (SHA512, SHA256, MD5 and DES)
  196. *
  197. * @author Laurent Declercq <l.declercq@nuxwin.com>
  198. * @return string Random salt
  199. */
  200. function generateRandomSalt()
  201. {
  202. /*if(defined('CRYPT_BLOWFISH') && CRYPT_BLOWFISH) {
  203. $saltLength = 21;
  204. if(version_compare(PHP_VERSION, '3.5.7') == -1) {
  205. $salt = '$2a$07$';
  206. } else {
  207. $salt = array_rand(array('$2x$07$' =>'', '$2y$07$' => ''));
  208. }
  209. } else*/
  210. if (defined('CRYPT_SHA512') && CRYPT_SHA512) {
  211. $saltLength = 16;
  212. $salt = '$6$rounds=' . mt_rand(1500, 5000) . '$';
  213. } elseif (defined('CRYPT_SHA256') && CRYPT_SHA256) {
  214. $saltLength = 16;
  215. $salt = '$5$rounds=' . mt_rand(1500, 5000) . '$';
  216. } elseif (defined('CRYPT_MD5') && CRYPT_MD5) {
  217. $saltLength = 8;
  218. $salt = '$1$';
  219. } elseif (defined('CRYPT_STD_DES') && CRYPT_STD_DES) {
  220. $saltLength = 2;
  221. $salt = '';
  222. } else {
  223. throw new iMSCP_Exception('No encryption algorithm available.');
  224. }
  225. #if ($saltLength > 2 && $saltLength < 21) {
  226. if ($saltLength > 2) {
  227. $chars = array_merge(range(0x21, 0x7e));
  228. } else {
  229. $chars = array_merge(range(0x2E, 0x2F), range(0x30, 0x39), range(0x41, 0x5a), range(0x61, 0x7a));
  230. }
  231. for ($i = 0; $i < $saltLength; $i++) {
  232. $salt .= chr($chars[array_rand($chars)]);
  233. }
  234. return $salt;
  235. }
  236. /**
  237. * Encrypts the given password with salt.
  238. *
  239. * @param string $password the password in clear text
  240. * @param string $salt OPTIONAL Salt to use
  241. * @return string the password encrypted with salt
  242. */
  243. function cryptPasswordWithSalt($password, $salt = '')
  244. {
  245. return crypt($password, ($salt) ? $salt : generateRandomSalt());
  246. }
  247. /**
  248. * Generates random password of size specified in Config Var 'PASSWD_CHARS'
  249. *
  250. * @return String password
  251. */
  252. function _passgen()
  253. {
  254. /** @var $cfg iMSCP_Config_Handler_File */
  255. $cfg = iMSCP_Registry::get('config');
  256. $passwordLength = $cfg->PASSWD_CHARS;
  257. $password = '';
  258. for ($i = 0; $i <= $passwordLength; $i++) {
  259. do {
  260. $z = mt_rand(42, 123);
  261. } while ($z >= 91 && $z <= 96);
  262. $password .= chr($z);
  263. }
  264. return $password;
  265. }
  266. /**
  267. * Generates random password matching the checkPasswordSyntax() criteria.
  268. *
  269. * @see _passgen()
  270. * @return String password
  271. */
  272. function passgen()
  273. {
  274. $password = null;
  275. while ($password == null || ! checkPasswordSyntax($password)) {
  276. $password = _passgen();
  277. }
  278. if(Zend_Session::namespaceIsset('pageMessages')) {
  279. Zend_Session::namespaceUnset('pageMessages');
  280. }
  281. return $password;
  282. }
  283. /**
  284. * Return UNIX timestamp representing first day of $month for $year.
  285. *
  286. * @param int $month OPTIONAL a month
  287. * @param int $year OPTIONAL A year (two or 4 digits, whatever)
  288. * @return int
  289. */
  290. function getFirstDayOfMonth($month = null, $year = null)
  291. {
  292. $month = $month ? : date('m');
  293. $year = $year ? : date('y');
  294. return mktime(0, 0, 0, $month, 1, $year);
  295. }
  296. /**
  297. * Return UNIX timestamp representing last day of month for $year.
  298. *
  299. * @param int $month OPTIONAL a month
  300. * @param int $year OPTIONAL A year (two or 4 digits, whatever)
  301. * @return int
  302. */
  303. function getLastDayOfMonth($month = null, $year = null)
  304. {
  305. $month = $month ? : date('m');
  306. $year = $year ? : date('y');
  307. return mktime(1, 0, 0, $month + 1, 0, $year);
  308. }