PageRenderTime 63ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/src/classes/helpers.class.php

https://github.com/CharlesTati/Minicraft
PHP | 861 lines | 699 code | 123 blank | 39 comment | 95 complexity | ad2c3fc36c29daabe01187fb46a206a6 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. class Helpers {
  3. public static $icon_collapse = 'iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAEJGlDQ1BJQ0MgUHJvZmlsZQAAOBGFVd9v21QUPolvUqQWPyBYR4eKxa9VU1u5GxqtxgZJk6XtShal6dgqJOQ6N4mpGwfb6baqT3uBNwb8AUDZAw9IPCENBmJ72fbAtElThyqqSUh76MQPISbtBVXhu3ZiJ1PEXPX6yznfOec7517bRD1fabWaGVWIlquunc8klZOnFpSeTYrSs9RLA9Sr6U4tkcvNEi7BFffO6+EdigjL7ZHu/k72I796i9zRiSJPwG4VHX0Z+AxRzNRrtksUvwf7+Gm3BtzzHPDTNgQCqwKXfZwSeNHHJz1OIT8JjtAq6xWtCLwGPLzYZi+3YV8DGMiT4VVuG7oiZpGzrZJhcs/hL49xtzH/Dy6bdfTsXYNY+5yluWO4D4neK/ZUvok/17X0HPBLsF+vuUlhfwX4j/rSfAJ4H1H0qZJ9dN7nR19frRTeBt4Fe9FwpwtN+2p1MXscGLHR9SXrmMgjONd1ZxKzpBeA71b4tNhj6JGoyFNp4GHgwUp9qplfmnFW5oTdy7NamcwCI49kv6fN5IAHgD+0rbyoBc3SOjczohbyS1drbq6pQdqumllRC/0ymTtej8gpbbuVwpQfyw66dqEZyxZKxtHpJn+tZnpnEdrYBbueF9qQn93S7HQGGHnYP7w6L+YGHNtd1FJitqPAR+hERCNOFi1i1alKO6RQnjKUxL1GNjwlMsiEhcPLYTEiT9ISbN15OY/jx4SMshe9LaJRpTvHr3C/ybFYP1PZAfwfYrPsMBtnE6SwN9ib7AhLwTrBDgUKcm06F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tfGVMWEq3QWWdufk6ZSNsjG2PQjp3ZcnOWWing6noonSInvi0/Ex+IzAreevPhe+CawpgP1/pMTMDo64G0sTCXIM+KdOnFWRfQKdJvQzV1+Bt8OokmrdtY2yhVX2a+qrykJfMq4Ml3VR4cVzTQVz+UoNne4vcKLoyS+gyKO6EHe+75Fdt0Mbe5bRIf/wjvrVmhbqBN97RD1vxrahvBOfOYzoosH9bq94uejSOQGkVM6sN/7HelL4t10t9F4gPdVzydEOx83Gv+uNxo7XyL/FtFl8z9ZAHF4bBsrEwAAAPxJREFUWAljZACCnW5ussycnF1MTEz2QK4kSIyG4Pm/f/8O/v3+vcx9167HjCDLWbm4LjIwMgrS0FIMo//9///u77dvBiwgn9PbcpBrmBgZhRggoQ4OdgwX0kMAFOVMQItoHef4/CIJcsCAAhZybXdav54RWe++wMD/yHxi2QMeAqMOGA2B0RBgxJV/0fM5sfkalzpc9oxGwYCHAM40gCsuYeLoaQRXHMPU46IHPARGHTAaAqMhQHY5gCtfkyo+KKLgOamupqL650ygjiIVDSTJKJDdTKBeKqijSJJOKigGd06BdjOBusigXirQNSuA5tIjOkDd8xUgO0F2AwDAIFg55NaPHAAAAABJRU5ErkJggg==';
  5. // Bolt - http://bolt.cm
  6. public static function htmlentities($str, $preserve_encoded_entities = false) {
  7. if ($preserve_encoded_entities) {
  8. $translation_table = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
  9. $translation_table[chr(38)] = '&';
  10. return preg_replace('#&(?![A-Za-z]{0,4}\w{2,3};|\#[0-9]{2,3};)#', '&amp;', strtr($str, $translation_table));
  11. } else {
  12. return htmlentities($str, ENT_QUOTES);
  13. }
  14. }
  15. // http://stackoverflow.com/a/3349792
  16. public static function removeDirectory($path) {
  17. if (is_dir($path)) {
  18. if (substr($path, strlen($pth) - 1, 1) != '/') {
  19. $dirPath .= '/';
  20. }
  21. $files = glob($path . '*', GLOB_MARK);
  22. foreach ($files as $file) {
  23. if (is_dir($file)) {
  24. self::removeDirectory($file);
  25. } else {
  26. unlink($file);
  27. }
  28. }
  29. $result = rmdir($path);
  30. }
  31. return $result;
  32. }
  33. public static function camelCase($str) {
  34. $str = lcfirst(str_replace(' ', '', ucwords(str_replace(array(
  35. '-',
  36. '_'
  37. ), ' ', $str))));
  38. return $str;
  39. }
  40. public static function underscoreCase($str) {
  41. return strtolower(preg_replace('#([a-z])([A-Z])#', '$1_$2', $str));
  42. }
  43. static function generateSlug($str, $replace = array('\''), $delimiter = '-', $limit = 64, $css_mode = false) {
  44. // Replaces some characters (such as apostrophes) with spaces
  45. if (!empty($replace)) {
  46. $str = str_replace((array) $replace, ' ', $str);
  47. }
  48. // Transforms unknown characters into a similar letter
  49. $clean = self::removeAccents($str);
  50. $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
  51. $clean = preg_replace('#[^a-zA-Z0-9/_|+ -]#', '', $clean);
  52. $clean = strtolower(trim($clean, '-'));
  53. $clean = preg_replace('#[/_|+ -]+#', $delimiter, $clean);
  54. // Smart trim
  55. if (strlen($clean) > $limit) {
  56. $clean = substr($clean, 0, $limit);
  57. $hyphen = strrpos($clean, '-');
  58. $clean = substr($clean, 0, $hyphen);
  59. }
  60. if ($css_mode) {
  61. $digits = array(
  62. 'zero',
  63. 'one',
  64. 'two',
  65. 'three',
  66. 'four',
  67. 'five',
  68. 'six',
  69. 'seven',
  70. 'eight',
  71. 'nine'
  72. );
  73. if (is_numeric(substr($clean, 0, 1))) {
  74. $clean = $digits[substr($slug, 0, 1)] . substr($clean, 1);
  75. }
  76. }
  77. return $clean;
  78. }
  79. // Bolt - http://bolt.cm
  80. public static function generateExtract($str, $length = 64, $append = '…') {
  81. $ret = substr($str, 0, $length);
  82. $last_space = strrpos($ret, ' ');
  83. if ($last_space and $str != $ret) {
  84. $ret = substr($ret, 0, $last_space);
  85. }
  86. if ($ret != $str) {
  87. $ret .= $append;
  88. }
  89. return $ret;
  90. }
  91. public static function addHTTP($url) {
  92. return preg_match("#^(?:f|ht)tps?://#i", $url) ? $url : 'http://' . $url;
  93. }
  94. // Bolt - http://bolt.cm
  95. public static function getClientIp($trust_proxy_headers = false) {
  96. if (!$trust_proxy_headers) {
  97. return ip2long($_SERVER['REMOTE_ADDR']);
  98. } elseif (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  99. return ip2long($_SERVER['HTTP_CLIENT_IP']);
  100. } else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  101. return ip2long($_SERVER['HTTP_X_FORWARDED_FOR']);
  102. } else {
  103. return ip2long($_SERVER['REMOTE_ADDR']);
  104. }
  105. }
  106. public static function tooShort($str, $min) {
  107. return strlen($str) < $min;
  108. }
  109. public static function tooLong($str, $max) {
  110. return strlen($str) > $max;
  111. }
  112. public static function usernameIsValid($username) {
  113. $regex_username = '#^(?![_-])(?!.*[_-]{2})([\wàáâãäåçèéêëìíîïðòóôõöùúûüýÿñ-]+)(?<![_-])$#';
  114. return preg_match($regex_username, $username);
  115. }
  116. public static function emailIsValid($email) {
  117. if (function_exists('idn_to_ascii')) {
  118. $parts = explode('@', $email);
  119. $user = $parts[0];
  120. $domain = idn_to_ascii($parts[1]);
  121. $email = $user . '@' . $domain;
  122. }
  123. return filter_var($email, FILTER_VALIDATE_EMAIL);
  124. }
  125. public static function setFlash($type, $message) {
  126. $_SESSION['MinicraftFlash'] = array(
  127. 'type' => $type,
  128. 'message' => $message
  129. );
  130. }
  131. // Générer une clé
  132. public static function randomKey($length = 32) {
  133. return substr(sha1(uniqid(rand(), true)), 0, $length > 40 ? 40 : $length);
  134. }
  135. /* ========== */
  136. // Wordpress - https://wordpress.org
  137. public static function removeAccents($str) {
  138. if (!preg_match('/[\x80-\xff]/', $str)) {
  139. return $str;
  140. }
  141. if (self::seemsUtf8($str)) {
  142. $chars = array(
  143. // Decompositions for Latin-1 Supplement
  144. chr(194) . chr(170) => 'a',
  145. chr(194) . chr(186) => 'o',
  146. chr(195) . chr(128) => 'A',
  147. chr(195) . chr(129) => 'A',
  148. chr(195) . chr(130) => 'A',
  149. chr(195) . chr(131) => 'A',
  150. chr(195) . chr(132) => 'A',
  151. chr(195) . chr(133) => 'A',
  152. chr(195) . chr(134) => 'AE',
  153. chr(195) . chr(135) => 'C',
  154. chr(195) . chr(136) => 'E',
  155. chr(195) . chr(137) => 'E',
  156. chr(195) . chr(138) => 'E',
  157. chr(195) . chr(139) => 'E',
  158. chr(195) . chr(140) => 'I',
  159. chr(195) . chr(141) => 'I',
  160. chr(195) . chr(142) => 'I',
  161. chr(195) . chr(143) => 'I',
  162. chr(195) . chr(144) => 'D',
  163. chr(195) . chr(145) => 'N',
  164. chr(195) . chr(146) => 'O',
  165. chr(195) . chr(147) => 'O',
  166. chr(195) . chr(148) => 'O',
  167. chr(195) . chr(149) => 'O',
  168. chr(195) . chr(150) => 'O',
  169. chr(195) . chr(153) => 'U',
  170. chr(195) . chr(154) => 'U',
  171. chr(195) . chr(155) => 'U',
  172. chr(195) . chr(156) => 'U',
  173. chr(195) . chr(157) => 'Y',
  174. chr(195) . chr(158) => 'TH',
  175. chr(195) . chr(159) => 's',
  176. chr(195) . chr(160) => 'a',
  177. chr(195) . chr(161) => 'a',
  178. chr(195) . chr(162) => 'a',
  179. chr(195) . chr(163) => 'a',
  180. chr(195) . chr(164) => 'a',
  181. chr(195) . chr(165) => 'a',
  182. chr(195) . chr(166) => 'ae',
  183. chr(195) . chr(167) => 'c',
  184. chr(195) . chr(168) => 'e',
  185. chr(195) . chr(169) => 'e',
  186. chr(195) . chr(170) => 'e',
  187. chr(195) . chr(171) => 'e',
  188. chr(195) . chr(172) => 'i',
  189. chr(195) . chr(173) => 'i',
  190. chr(195) . chr(174) => 'i',
  191. chr(195) . chr(175) => 'i',
  192. chr(195) . chr(176) => 'd',
  193. chr(195) . chr(177) => 'n',
  194. chr(195) . chr(178) => 'o',
  195. chr(195) . chr(179) => 'o',
  196. chr(195) . chr(180) => 'o',
  197. chr(195) . chr(181) => 'o',
  198. chr(195) . chr(182) => 'o',
  199. chr(195) . chr(184) => 'o',
  200. chr(195) . chr(185) => 'u',
  201. chr(195) . chr(186) => 'u',
  202. chr(195) . chr(187) => 'u',
  203. chr(195) . chr(188) => 'u',
  204. chr(195) . chr(189) => 'y',
  205. chr(195) . chr(190) => 'th',
  206. chr(195) . chr(191) => 'y',
  207. chr(195) . chr(152) => 'O',
  208. // Decompositions for Latin Extended-A
  209. chr(196) . chr(128) => 'A',
  210. chr(196) . chr(129) => 'a',
  211. chr(196) . chr(130) => 'A',
  212. chr(196) . chr(131) => 'a',
  213. chr(196) . chr(132) => 'A',
  214. chr(196) . chr(133) => 'a',
  215. chr(196) . chr(134) => 'C',
  216. chr(196) . chr(135) => 'c',
  217. chr(196) . chr(136) => 'C',
  218. chr(196) . chr(137) => 'c',
  219. chr(196) . chr(138) => 'C',
  220. chr(196) . chr(139) => 'c',
  221. chr(196) . chr(140) => 'C',
  222. chr(196) . chr(141) => 'c',
  223. chr(196) . chr(142) => 'D',
  224. chr(196) . chr(143) => 'd',
  225. chr(196) . chr(144) => 'D',
  226. chr(196) . chr(145) => 'd',
  227. chr(196) . chr(146) => 'E',
  228. chr(196) . chr(147) => 'e',
  229. chr(196) . chr(148) => 'E',
  230. chr(196) . chr(149) => 'e',
  231. chr(196) . chr(150) => 'E',
  232. chr(196) . chr(151) => 'e',
  233. chr(196) . chr(152) => 'E',
  234. chr(196) . chr(153) => 'e',
  235. chr(196) . chr(154) => 'E',
  236. chr(196) . chr(155) => 'e',
  237. chr(196) . chr(156) => 'G',
  238. chr(196) . chr(157) => 'g',
  239. chr(196) . chr(158) => 'G',
  240. chr(196) . chr(159) => 'g',
  241. chr(196) . chr(160) => 'G',
  242. chr(196) . chr(161) => 'g',
  243. chr(196) . chr(162) => 'G',
  244. chr(196) . chr(163) => 'g',
  245. chr(196) . chr(164) => 'H',
  246. chr(196) . chr(165) => 'h',
  247. chr(196) . chr(166) => 'H',
  248. chr(196) . chr(167) => 'h',
  249. chr(196) . chr(168) => 'I',
  250. chr(196) . chr(169) => 'i',
  251. chr(196) . chr(170) => 'I',
  252. chr(196) . chr(171) => 'i',
  253. chr(196) . chr(172) => 'I',
  254. chr(196) . chr(173) => 'i',
  255. chr(196) . chr(174) => 'I',
  256. chr(196) . chr(175) => 'i',
  257. chr(196) . chr(176) => 'I',
  258. chr(196) . chr(177) => 'i',
  259. chr(196) . chr(178) => 'IJ',
  260. chr(196) . chr(179) => 'ij',
  261. chr(196) . chr(180) => 'J',
  262. chr(196) . chr(181) => 'j',
  263. chr(196) . chr(182) => 'K',
  264. chr(196) . chr(183) => 'k',
  265. chr(196) . chr(184) => 'k',
  266. chr(196) . chr(185) => 'L',
  267. chr(196) . chr(186) => 'l',
  268. chr(196) . chr(187) => 'L',
  269. chr(196) . chr(188) => 'l',
  270. chr(196) . chr(189) => 'L',
  271. chr(196) . chr(190) => 'l',
  272. chr(196) . chr(191) => 'L',
  273. chr(197) . chr(128) => 'l',
  274. chr(197) . chr(129) => 'L',
  275. chr(197) . chr(130) => 'l',
  276. chr(197) . chr(131) => 'N',
  277. chr(197) . chr(132) => 'n',
  278. chr(197) . chr(133) => 'N',
  279. chr(197) . chr(134) => 'n',
  280. chr(197) . chr(135) => 'N',
  281. chr(197) . chr(136) => 'n',
  282. chr(197) . chr(137) => 'N',
  283. chr(197) . chr(138) => 'n',
  284. chr(197) . chr(139) => 'N',
  285. chr(197) . chr(140) => 'O',
  286. chr(197) . chr(141) => 'o',
  287. chr(197) . chr(142) => 'O',
  288. chr(197) . chr(143) => 'o',
  289. chr(197) . chr(144) => 'O',
  290. chr(197) . chr(145) => 'o',
  291. chr(197) . chr(146) => 'OE',
  292. chr(197) . chr(147) => 'oe',
  293. chr(197) . chr(148) => 'R',
  294. chr(197) . chr(149) => 'r',
  295. chr(197) . chr(150) => 'R',
  296. chr(197) . chr(151) => 'r',
  297. chr(197) . chr(152) => 'R',
  298. chr(197) . chr(153) => 'r',
  299. chr(197) . chr(154) => 'S',
  300. chr(197) . chr(155) => 's',
  301. chr(197) . chr(156) => 'S',
  302. chr(197) . chr(157) => 's',
  303. chr(197) . chr(158) => 'S',
  304. chr(197) . chr(159) => 's',
  305. chr(197) . chr(160) => 'S',
  306. chr(197) . chr(161) => 's',
  307. chr(197) . chr(162) => 'T',
  308. chr(197) . chr(163) => 't',
  309. chr(197) . chr(164) => 'T',
  310. chr(197) . chr(165) => 't',
  311. chr(197) . chr(166) => 'T',
  312. chr(197) . chr(167) => 't',
  313. chr(197) . chr(168) => 'U',
  314. chr(197) . chr(169) => 'u',
  315. chr(197) . chr(170) => 'U',
  316. chr(197) . chr(171) => 'u',
  317. chr(197) . chr(172) => 'U',
  318. chr(197) . chr(173) => 'u',
  319. chr(197) . chr(174) => 'U',
  320. chr(197) . chr(175) => 'u',
  321. chr(197) . chr(176) => 'U',
  322. chr(197) . chr(177) => 'u',
  323. chr(197) . chr(178) => 'U',
  324. chr(197) . chr(179) => 'u',
  325. chr(197) . chr(180) => 'W',
  326. chr(197) . chr(181) => 'w',
  327. chr(197) . chr(182) => 'Y',
  328. chr(197) . chr(183) => 'y',
  329. chr(197) . chr(184) => 'Y',
  330. chr(197) . chr(185) => 'Z',
  331. chr(197) . chr(186) => 'z',
  332. chr(197) . chr(187) => 'Z',
  333. chr(197) . chr(188) => 'z',
  334. chr(197) . chr(189) => 'Z',
  335. chr(197) . chr(190) => 'z',
  336. chr(197) . chr(191) => 's',
  337. // Decompositions for Latin Extended-B
  338. chr(200) . chr(152) => 'S',
  339. chr(200) . chr(153) => 's',
  340. chr(200) . chr(154) => 'T',
  341. chr(200) . chr(155) => 't',
  342. // Euro Sign
  343. chr(226) . chr(130) . chr(172) => 'E',
  344. // GBP (Pound) Sign
  345. chr(194) . chr(163) => ''
  346. );
  347. $str = strtr($str, $chars);
  348. } else {
  349. // Assume ISO-8859-1 if not UTF-8
  350. $chars['in'] = chr(128) . chr(131) . chr(138) . chr(142) . chr(154) . chr(158) . chr(159) . chr(162) . chr(165) . chr(181) . chr(192) . chr(193) . chr(194) . chr(195) . chr(196) . chr(197) . chr(199) . chr(200) . chr(201) . chr(202) . chr(203) . chr(204) . chr(205) . chr(206) . chr(207) . chr(209) . chr(210) . chr(211) . chr(212) . chr(213) . chr(214) . chr(216) . chr(217) . chr(218) . chr(219) . chr(220) . chr(221) . chr(224) . chr(225) . chr(226) . chr(227) . chr(228) . chr(229) . chr(231) . chr(232) . chr(233) . chr(234) . chr(235) . chr(236) . chr(237) . chr(238) . chr(239) . chr(241) . chr(242) . chr(243) . chr(244) . chr(245) . chr(246) . chr(248) . chr(249) . chr(250) . chr(251) . chr(252) . chr(253) . chr(255);
  351. $chars['out'] = 'EfSZszYcYuAAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy';
  352. $str = strtr($str, $chars['in'], $chars['out']);
  353. $double_chars['in'] = array(
  354. chr(140),
  355. chr(156),
  356. chr(198),
  357. chr(208),
  358. chr(222),
  359. chr(223),
  360. chr(230),
  361. chr(240),
  362. chr(254)
  363. );
  364. $double_chars['out'] = array(
  365. 'OE',
  366. 'oe',
  367. 'AE',
  368. 'DH',
  369. 'TH',
  370. 'ss',
  371. 'ae',
  372. 'dh',
  373. 'th'
  374. );
  375. $str = str_replace($double_chars['in'], $double_chars['out'], $str);
  376. }
  377. return $str;
  378. }
  379. // Bolt - http://bolt.cm
  380. public static function seemsUtf8($str) {
  381. if (function_exists('mb_check_encoding')) {
  382. // If mbstring is available, this is significantly faster than
  383. // using PHP regexps.
  384. return mb_check_encoding($str, 'UTF-8');
  385. }
  386. $regex = '/(
  387. | [\xF8-\xFF] # Invalid UTF-8 Bytes
  388. | [\xC0-\xDF](?![\x80-\xBF]) # Invalid UTF-8 Sequence Start
  389. | [\xE0-\xEF](?![\x80-\xBF]{2}) # Invalid UTF-8 Sequence Start
  390. | [\xF0-\xF7](?![\x80-\xBF]{3}) # Invalid UTF-8 Sequence Start
  391. | (?<=[\x0-\x7F\xF8-\xFF])[\x80-\xBF] # Invalid UTF-8 Sequence Middle
  392. | (?<![\xC0-\xDF]|[\xE0-\xEF]|[\xE0-\xEF][\x80-\xBF]|[\xF0-\xF7]|[\xF0-\xF7][\x80-\xBF]|[\xF0-\xF7][\x80-\xBF]{2})[\x80-\xBF] # Overlong Sequence
  393. | (?<=[\xE0-\xEF])[\x80-\xBF](?![\x80-\xBF]) # Short 3 byte sequence
  394. | (?<=[\xF0-\xF7])[\x80-\xBF](?![\x80-\xBF]{2}) # Short 4 byte sequence
  395. | (?<=[\xF0-\xF7][\x80-\xBF])[\x80-\xBF](?![\x80-\xBF]) # Short 4 byte sequence (2)
  396. )/x';
  397. return !preg_match($regex, $str);
  398. }
  399. // Bolt - http://bolt.cm
  400. public static function forceDownload($filename, $content = false) {
  401. if (!headers_sent()) {
  402. // Required for some browsers
  403. if (ini_get('zlib.output_compression')) {
  404. @ini_set('zlib.output_compression', 'Off');
  405. }
  406. header('Pragma: public');
  407. header('Expires: 0');
  408. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  409. // Required for certain browsers
  410. header('Cache-Control: private', false);
  411. header('Content-Disposition: attachment; filename="' . basename(str_replace('"', '', $filename)) . '";');
  412. header('Content-Type: application/force-download');
  413. header('Content-Transfer-Encoding: binary');
  414. if ($content) {
  415. header('Content-Length: ' . strlen($content));
  416. }
  417. ob_clean();
  418. flush();
  419. if ($content) {
  420. echo $content;
  421. }
  422. return true;
  423. } else {
  424. return false;
  425. }
  426. }
  427. // Bolt - http://bolt.cm
  428. public static function randomString($length = 32, $human_friendly = true, $include_symbols = false, $no_duplicate_chars = false) {
  429. $nice_chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefhjkmnprstuvwxyz23456789';
  430. $all_an = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
  431. $symbols = '!@#$%^&*()~_-=+{}[]|:;<>,.?/"\'\\`';
  432. $str = '';
  433. // Determines the pool of available characters based on the given parameters
  434. if ($human_friendly) {
  435. $pool = $nice_chars;
  436. } else {
  437. $pool = $all_an;
  438. if ($include_symbols) {
  439. $pool .= $symbols;
  440. }
  441. }
  442. // Don't allow duplicate letters to be disabled if the length is longer than the available characters
  443. if ($no_duplicate_chars and strlen($pool) < $length) {
  444. throw new LengthException('$length exceeds the size of the pool and $no_duplicate_chars is enabled');
  445. }
  446. // Converts the pool of characters into an array of characters and shuffle the array
  447. $pool = str_split($pool);
  448. shuffle($pool);
  449. // Generates the string
  450. for ($i = 0; $i < $length; $i++) {
  451. if ($no_duplicate_chars) {
  452. $str .= array_shift($pool);
  453. } else {
  454. $str .= $pool[0];
  455. shuffle($pool);
  456. }
  457. }
  458. return $str;
  459. }
  460. public static function getGravatar($email, $size = 32) {
  461. return 'http://www.gravatar.com/avatar/' . md5($email) . '?s=' . self::absint($size);
  462. }
  463. // Bolt - http://bolt.cm
  464. public static function absint($maybeint) {
  465. return abs(intval($maybeint));
  466. }
  467. // Bolt - http://bolt.cm
  468. function emailIsValidAlt($email) {
  469. if (!empty($email) or is_string($email)) {
  470. $mail_array = explode('@', $email);
  471. }
  472. if (!is_array($mail_array)) {
  473. return false;
  474. }
  475. if (count($mail_array) == 2) {
  476. $localpart = $mail_array[0];
  477. $domain_array = explode('.', $mail_array[1]);
  478. } else {
  479. return false;
  480. }
  481. if (!is_array($domain_array)) {
  482. return false;
  483. }
  484. if (count($domain_array) == 1) {
  485. return false;
  486. }
  487. $domain_toplevel = array_pop($domain_array);
  488. if (is_string($domain_toplevel) and (strlen($domain_toplevel) > 1)) {
  489. $domain_array[] = $domain_toplevel;
  490. $domain = implode('', $domain_array);
  491. $domain = preg_replace('/[a-z0-9]/i', '', $domain);
  492. $domain = preg_replace('/[-|\_]/', '', $domain);
  493. $localpart = preg_replace('/[a-z0-9]/i', '', $localpart);
  494. $localpart = preg_replace('#[-.|\!|\#|\$|\%|\&|\'|\*|\+|\/|\=|\? |\^|\_|\`|\{|\or\}|\~]#', '', $localpart);
  495. // If there are no characters left in localpart or domain, the email address is valid
  496. return (empty($domain) and empty($localpart));
  497. }
  498. return false;
  499. }
  500. public static function blockAccess() {
  501. if (!in_array(@$_SERVER['REMOTE_ADDR'], array(
  502. '127.0.0.1',
  503. '::1'
  504. ))) {
  505. header('HTTP/1.0 403 Forbidden');
  506. die('<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Unauthorized access</title></head><body><h1>Unauthorized access</h1><p>You can\'t see this page.</p></body></html>');
  507. }
  508. }
  509. // Bolt - http://bolt.cm
  510. public static function var_dump($var, $return = false) {
  511. $html = '<pre style="margin: 0 0 10px;' . 'background: #373b41;' . 'padding: 9.5px;' . 'border: 1px solid #1d1f21;' . 'border-radius: 4px;' . 'display: block;' . 'font-size: 13px;' . 'white-space: pre-wrap;' . 'word-wrap: break-word;' . 'word-break: break-all;' . 'color: #c5c8c6;' . 'text-align: left;' . 'line-height: 1.42857143;' . 'page-break-inside: avoid;' . 'max-height: 340px;' . 'overflow-y: scroll;' . 'font-family: Menlo, Monaco, Consolas, "Courier New", monospace;">';
  512. $html .= self::var_dump_plain($var, true);
  513. $html .= '</pre>';
  514. if (!$return) {
  515. echo $html;
  516. } else {
  517. return $html;
  518. }
  519. }
  520. // Bolt - http://bolt.cm
  521. public static function var_dump_plain($var, $traversedeeper = false) {
  522. $html = '';
  523. if (is_bool($var)) {
  524. $html .= '<span style="color:#81a2be;">boo</span><span style="color:#c5c8c6;">(</span><span style="color: #cc6666;">' . (($var) ? 'true' : 'false') . '</span><span style="color:#c5c8c6;">)</span>';
  525. } else if (is_int($var)) {
  526. $html .= '<span style="color:#81a2be;">int</span><span style="color:#c5c8c6;">(</span><span style="color: #cc6666;">' . $var . '</span><span style="color:#c5c8c6;">)</span>';
  527. } else if (is_float($var)) {
  528. $html .= '<span style="color:#81a2be;">flo</span><span style="color:#c5c8c6;">(</span><span style="color: #cc6666;">' . $var . '</span><span style="color:#c5c8c6;">)</span>';
  529. } else if (is_string($var)) {
  530. $html .= '<span style="color:#81a2be;">str</span><span style="color:#c5c8c6;">(</span><span style="color: #cc6666;">' . strlen($var) . '</span><span style="color:#c5c8c6;">)</span>"<span style="color: #df9f4f;">' . self::htmlentities($var) . '</span>"';
  531. } else if (is_null($var)) {
  532. $html .= 'NULL';
  533. } else if (is_resource($var)) {
  534. $html .= '<span style="color:#81a2be;">res</span>("' . get_resource_type($var) . '") "' . $var;
  535. } else if (is_array($var)) {
  536. $uuid = uniqid('include-php-', true);
  537. $html .= '<span style="color:#81a2be;">arr</span>(<span style="color: #cc6666;">' . count($var) . '</span>)';
  538. if (!empty($var)) {
  539. $html .= ' <img id="' . $uuid . '" data-collapse="data:image/png;base64,' . self::$icon_collapse . '" style="position:relative;cursor:pointer;width:16px!important;height:16px!important;" src="data:image/png;base64,' . self::$icon_expand . '"> <span id="' . $uuid . '-collapsable" style="display: none;">[<br>';
  540. $indent = 2;
  541. $longest_key = 0;
  542. foreach ($var as $key => $value) {
  543. if (is_string($key)) {
  544. $longest_key = max($longest_key, strlen($key) + 2);
  545. } else {
  546. $longest_key = max($longest_key, strlen($key));
  547. }
  548. }
  549. foreach ($var as $key => $value) {
  550. if (is_numeric($key)) {
  551. $html .= str_repeat(' ', $indent) . str_pad($key, $longest_key, '_');
  552. } else {
  553. $html .= str_repeat(' ', $indent) . str_pad('"<span style="color: #8abeb7;">' . self::htmlentities($key) . '</span>"', $longest_key, ' ');
  554. }
  555. $html .= ' => ';
  556. $value = explode('<br>', self::var_dump_plain($value));
  557. foreach ($value as $line => $val) {
  558. if ($line != 0) {
  559. $value[$line] = str_repeat(' ', $indent * 2) . $val;
  560. }
  561. }
  562. $html .= implode('<br>', $value) . '<br>';
  563. }
  564. $html .= ']</span>';
  565. $html .= preg_replace('/ +/', ' ', '<script type="text/javascript">(function() {
  566. var img = document.getElementById("' . $uuid . '");
  567. img.onclick = function() {
  568. if ( document.getElementById("' . $uuid . '-collapsable").style.display == "none" ) {
  569. document.getElementById("' . $uuid . '-collapsable").style.display = "inline";
  570. img.setAttribute( "data-expand", img.getAttribute("src") );
  571. img.src = img.getAttribute("data-collapse");
  572. var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
  573. } else {
  574. document.getElementById("' . $uuid . '-collapsable").style.display = "none";
  575. img.src = img.getAttribute("data-expand");
  576. var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
  577. }
  578. };
  579. })();
  580. </script>');
  581. }
  582. } else if (is_object($var)) {
  583. $uuid = uniqid('include-php-', true);
  584. $html .= '<span style="color:#81a2be;">object</span>(' . get_class($var) . ') <img id="' . $uuid . '" data-collapse="data:image/png;base64,' . self::$icon_collapse . '" style="position:relative;vertical-align:middle;cursor:pointer;width:16px!important;height:16px!important;" src="data:image/png;base64,' . self::$icon_expand . '"> <span id="' . $uuid . '-collapsable" style="display: none;">[<br>';
  585. $original = $var;
  586. $var = (array) $var;
  587. $indent = 2;
  588. $longest_key = 0;
  589. foreach ($var as $key => $value) {
  590. if (substr($key, 0, 2) == "\0*") {
  591. unset($var[$key]);
  592. $key = 'protected:' . substr($key, 2);
  593. $var[$key] = $value;
  594. } else if (substr($key, 0, 1) == "\0") {
  595. unset($var[$key]);
  596. $key = 'private:' . substr($key, 1, strpos(substr($key, 1), "\0")) . ':' . substr($key, strpos(substr($key, 1), "\0") + 1);
  597. $var[$key] = $value;
  598. }
  599. if (is_string($key)) {
  600. $longest_key = max($longest_key, strlen($key) + 2);
  601. } else {
  602. $longest_key = max($longest_key, strlen($key));
  603. }
  604. }
  605. foreach ($var as $key => $value) {
  606. if (is_numeric($key)) {
  607. $html .= str_repeat(' ', $indent) . str_pad($key, $longest_key, ' ');
  608. } else {
  609. $html .= str_repeat(' ', $indent) . str_pad('"<span style="color: #8abeb7;">' . self::htmlentities($key) . '</span>"', $longest_key, ' ');
  610. }
  611. $html .= ' => ';
  612. $value = explode('<br>', self::var_dump_plain($value));
  613. foreach ($value as $line => $val) {
  614. if ($line != 0) {
  615. $value[$line] = str_repeat(' ', $indent * 2) . $val;
  616. }
  617. }
  618. $html .= implode('<br>', $value) . '<br>';
  619. }
  620. $html .= ']</span>';
  621. $html .= preg_replace('/ +/', ' ', '<script type="text/javascript">(function() {
  622. var img = document.getElementById("' . $uuid . '");
  623. img.onclick = function() {
  624. if ( document.getElementById("' . $uuid . '-collapsable").style.display == "none" ) {
  625. document.getElementById("' . $uuid . '-collapsable").style.display = "inline";
  626. img.setAttribute( "data-expand", img.getAttribute("src") );
  627. img.src = img.getAttribute("data-collapse");
  628. var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
  629. } else {
  630. document.getElementById("' . $uuid . '-collapsable").style.display = "none";
  631. img.src = img.getAttribute("data-expand");
  632. var previousSibling = document.getElementById("' . $uuid . '-collapsable").previousSibling;
  633. }
  634. };
  635. })();
  636. </script>');
  637. }
  638. return $html;
  639. }
  640. public static function isHttps() {
  641. return isset($_SERVER['HTTPS']) and !empty($_SERVER['HTTPS']) and $_SERVER['HTTPS'] != 'off';
  642. }
  643. function checkVersion($currentversion, $requiredversion) {
  644. return version_compare($currentversion, $requiredversion) > -1;
  645. }
  646. public static function generateUniqueId() {
  647. $data = $_SERVER['REQUEST_TIME'];
  648. $data .= $_SERVER['HTTP_USER_AGENT'];
  649. $data .= $_SERVER['LOCAL_ADDR'];
  650. $data .= $_SERVER['LOCAL_PORT'];
  651. $data .= $_SERVER['REMOTE_ADDR'];
  652. $data .= $_SERVER['REMOTE_PORT'];
  653. $hash = sha1($data);
  654. }
  655. // Bolt - http://bolt.cm
  656. function generateToken() {
  657. $seed = $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT'] . $_SESSION['Minicraft'];
  658. $token = substr(md5($seed), 0, 8);
  659. return $token;
  660. }
  661. // Bolt - http://bolt.cm
  662. function checkToken($token = '') {
  663. return $token === generateToken();
  664. }
  665. //Bolt
  666. function stripTrailingSlash($path) {
  667. if (substr($path, -1, 1) == '/') {
  668. $path = substr($path, 0, -1);
  669. }
  670. return $path;
  671. }
  672. // Bolt - http://bolt.cm
  673. function getExtension($filename) {
  674. $pos = strrpos($filename, '.');
  675. if ($pos === false) {
  676. return '';
  677. } else {
  678. $ext = substr($filename, $pos + 1);
  679. return $ext;
  680. }
  681. }
  682. // Bolt - http://bolt.cm
  683. function isHtml($html) {
  684. $len = strlen($html);
  685. $trimlen = strlen(strip_tags($html));
  686. $factor = $trimlen / $len;
  687. if ($factor < 0.97) {
  688. return true;
  689. } else {
  690. return false;
  691. }
  692. }
  693. // http://stackoverflow.com/a/2606638
  694. function strReplaceFirst($search, $replace, $subject) {
  695. $pos = strpos($subject, $search);
  696. if ($pos !== false) {
  697. $subject = substr_replace($subject, $replace, $pos, strlen($search));
  698. }
  699. return $subject;
  700. }
  701. // Bolt - http://bolt.cm
  702. public static function createDirectory($path) {
  703. if (file_exists($path)) {
  704. return true;
  705. }
  706. if (dirname($path) != '.') {
  707. $success = makeDir(dirname($path));
  708. if (!$success) {
  709. return false;
  710. }
  711. }
  712. $mode_dec = octdec('0777');
  713. $oldumask = umask(0);
  714. $success = @mkdir($path, $mode_dec);
  715. @chmod($name, $mode_dec);
  716. umask($oldumask);
  717. return $success;
  718. }
  719. public static function throw404($twig, $config, $user) {
  720. header('HTTP/1.0 404 Not Found');
  721. die($twig->render('misc/404.twig', array(
  722. 'pageTitle' => '404',
  723. 'config' => $config,
  724. 'user' => $user,
  725. 'flash' => new Flash
  726. )));
  727. }
  728. public static function redirect($router, $page, $args = array()) {
  729. $url = $router->getController($page)->getUrl();
  730. if (!empty($args)) {
  731. for ($i = 0; $i < count($args); $i++) {
  732. $url = preg_replace('#%m' . ($i + 1) . '%#', $args[$i], $url);
  733. }
  734. }
  735. header('Location: ' . URL . '/' . $url);
  736. die();
  737. }
  738. }