normal-functions.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. <?php
  2. // Print output all purrty
  3. function prettyPrint($v)
  4. {
  5. $trace = debug_backtrace()[0];
  6. echo '<pre style="white-space: pre; text-overflow: ellipsis; overflow: hidden; background-color: #f2f2f2; border: 2px solid black; border-radius: 5px; padding: 5px; margin: 5px;">' . $trace['file'] . ':' . $trace['line'] . ' ' . gettype($v) . "\n\n" . print_r($v, 1) . '</pre><br/>';
  7. }
  8. // Clean Directory string
  9. function cleanDirectory($path)
  10. {
  11. $path = str_replace(array('/', '\\'), '/', $path);
  12. if (substr($path, -1) != '/') {
  13. $path = $path . '/';
  14. }
  15. if ($path[0] != '/' && $path[1] != ':') {
  16. $path = '/' . $path;
  17. }
  18. return $path;
  19. }
  20. // Get Gravatar Email Image
  21. function gravatar($email = '')
  22. {
  23. $email = md5(strtolower(trim($email)));
  24. $gravurl = "https://www.gravatar.com/avatar/$email?s=100&d=mm";
  25. return $gravurl;
  26. }
  27. function parseDomain($value, $force = false)
  28. {
  29. $badDomains = array('ddns.net', 'ddnsking.com', '3utilities.com', 'bounceme.net', 'duckdns.org', 'freedynamicdns.net', 'freedynamicdns.org', 'gotdns.ch', 'hopto.org', 'myddns.me', 'myds.me', 'myftp.biz', 'myftp.org', 'myvnc.com', 'noip.com', 'onthewifi.com', 'redirectme.net', 'serveblog.net', 'servecounterstrike.com', 'serveftp.com', 'servegame.com', 'servehalflife.com', 'servehttp.com', 'serveirc.com', 'serveminecraft.net', 'servemp3.com', 'servepics.com', 'servequake.com', 'sytes.net', 'viewdns.net', 'webhop.me', 'zapto.org');
  30. $Domain = $value;
  31. $Port = strpos($Domain, ':');
  32. if ($Port !== false) {
  33. $Domain = substr($Domain, 0, $Port);
  34. $value = $Domain;
  35. }
  36. $check = substr_count($Domain, '.');
  37. if ($check >= 3) {
  38. if (is_numeric($Domain[0])) {
  39. $Domain = '';
  40. } else {
  41. if (in_array(strtolower(explode('.', $Domain)[2] . '.' . explode('.', $Domain)[3]), $badDomains)) {
  42. $Domain = '.' . explode('.', $Domain)[0] . '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2] . '.' . explode('.', $Domain)[3];
  43. } else {
  44. $Domain = '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2] . '.' . explode('.', $Domain)[3];
  45. }
  46. }
  47. } elseif ($check == 2) {
  48. if (in_array(strtolower(explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2]), $badDomains)) {
  49. $Domain = '.' . explode('.', $Domain)[0] . '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  50. } elseif (explode('.', $Domain)[0] == 'www') {
  51. $Domain = '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  52. } elseif (explode('.', $Domain)[1] == 'co') {
  53. $Domain = '.' . explode('.', $Domain)[0] . '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  54. } else {
  55. $Domain = '.' . explode('.', $Domain)[1] . '.' . explode('.', $Domain)[2];
  56. }
  57. } elseif ($check == 1) {
  58. $Domain = '.' . $Domain;
  59. } else {
  60. $Domain = '';
  61. }
  62. /*
  63. if (is_numeric($Domain[0]) || strpos($Domain, '.') == false) {
  64. $Domain = '';
  65. } else {
  66. if (substr($Domain, 0, 3) == 'www') {
  67. $Domain = substr($Domain, 3, strlen($Domain) - 3);
  68. } else {
  69. $Domain = '.' . $Domain;
  70. }
  71. }
  72. */
  73. return ($force) ? $value : $Domain;
  74. }
  75. // Cookie Custom Function
  76. function coookie($type, $name, $value = '', $days = -1, $http = true)
  77. {
  78. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  79. $Secure = true;
  80. $HTTPOnly = true;
  81. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' && $_SERVER['HTTPS'] !== '') {
  82. $Secure = true;
  83. $HTTPOnly = true;
  84. } else {
  85. $Secure = false;
  86. $HTTPOnly = false;
  87. }
  88. if (!$http) {
  89. $HTTPOnly = false;
  90. }
  91. $Path = '/';
  92. $Domain = parseDomain($_SERVER['HTTP_HOST']);
  93. $DomainTest = parseDomain($_SERVER['HTTP_HOST'], true);
  94. if ($type == 'set') {
  95. $_COOKIE[$name] = $value;
  96. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  97. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() + (86400 * $days)) . ' GMT')
  98. . (empty($Path) ? '' : '; path=' . $Path)
  99. . (empty($Domain) ? '' : '; domain=' . $Domain)
  100. . (!$Secure ? '' : '; secure')
  101. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  102. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  103. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() + (86400 * $days)) . ' GMT')
  104. . (empty($Path) ? '' : '; path=' . $Path)
  105. . (empty($Domain) ? '' : '; domain=' . $DomainTest)
  106. . (!$Secure ? '' : '; secure')
  107. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  108. } elseif ($type == 'delete') {
  109. unset($_COOKIE[$name]);
  110. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  111. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() - 3600) . ' GMT')
  112. . (empty($Path) ? '' : '; path=' . $Path)
  113. . (empty($Domain) ? '' : '; domain=' . $Domain)
  114. . (!$Secure ? '' : '; secure')
  115. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  116. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  117. . (empty($days) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() - 3600) . ' GMT')
  118. . (empty($Path) ? '' : '; path=' . $Path)
  119. . (empty($Domain) ? '' : '; domain=' . $DomainTest)
  120. . (!$Secure ? '' : '; secure')
  121. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  122. }
  123. }
  124. function getOS()
  125. {
  126. if (PHP_SHLIB_SUFFIX == "dll") {
  127. return "win";
  128. } else {
  129. return "*nix";
  130. }
  131. }
  132. if (!function_exists('getallheaders')) {
  133. function getallheaders()
  134. {
  135. $headers = array();
  136. foreach ($_SERVER as $name => $value) {
  137. if (substr($name, 0, 5) == 'HTTP_') {
  138. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  139. }
  140. }
  141. return $headers;
  142. }
  143. }
  144. function random_ascii_string($length)
  145. {
  146. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  147. $charactersLength = strlen($characters);
  148. $randomString = '';
  149. for ($i = 0; $i < $length; $i++) {
  150. $randomString .= $characters[rand(0, $charactersLength - 1)];
  151. }
  152. return $randomString;
  153. }
  154. // Generate Random string
  155. function randString($length = 10, $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  156. {
  157. $tmp = '';
  158. for ($i = 0; $i < $length; $i++) {
  159. $tmp .= substr(str_shuffle($chars), 0, 1);
  160. }
  161. return $tmp;
  162. }
  163. function isEncrypted($password)
  164. {
  165. switch (strlen($password)) {
  166. case '24':
  167. return (strpos($password, '==') !== false) ? true : false;
  168. break;
  169. case '44':
  170. return (substr($password, -1, 1) == '=') ? true : false;
  171. break;
  172. case '64':
  173. return true;
  174. case '88':
  175. return (strpos($password, '==') !== false) ? true : false;
  176. break;
  177. case '108':
  178. return (substr($password, -1, 1) == '=') ? true : false;
  179. break;
  180. default:
  181. return false;
  182. }
  183. }
  184. function encrypt($password, $key = null)
  185. {
  186. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  187. return openssl_encrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  188. }
  189. function decrypt($password, $key = null)
  190. {
  191. if (empty($password)) {
  192. return '';
  193. }
  194. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  195. return openssl_decrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  196. }
  197. function fillString($string, $length)
  198. {
  199. $filler = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*';
  200. if (strlen($string) < $length) {
  201. $diff = $length - strlen($string);
  202. $filler = substr($filler, 0, $diff);
  203. return $string . $filler;
  204. } elseif (strlen($string) > $length) {
  205. return substr($string, 0, $length);
  206. } else {
  207. return $string;
  208. }
  209. }
  210. function userIP()
  211. {
  212. if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  213. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  214. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  215. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  216. } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
  217. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  218. } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  219. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  220. } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
  221. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  222. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  223. $ipaddress = $_SERVER['REMOTE_ADDR'];
  224. } else {
  225. $ipaddress = 'UNKNOWN';
  226. }
  227. if (strpos($ipaddress, ',') !== false) {
  228. list($first, $last) = explode(",", $ipaddress);
  229. unset($last);
  230. return $first;
  231. } else {
  232. return $ipaddress;
  233. }
  234. }
  235. function arrayIP($string)
  236. {
  237. if (strpos($string, ',') !== false) {
  238. $result = explode(",", $string);
  239. } else {
  240. $result = array($string);
  241. }
  242. foreach ($result as &$ip) {
  243. $ip = is_numeric(substr($ip, 0, 1)) ? $ip : gethostbyname($ip);
  244. }
  245. return $result;
  246. }
  247. function getCert()
  248. {
  249. $url = 'http://curl.haxx.se/ca/cacert.pem';
  250. $file = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'cacert.pem';
  251. if (!file_exists($file)) {
  252. file_put_contents($file, fopen($url, 'r'));
  253. } elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  254. file_put_contents($file, fopen($url, 'r'));
  255. }
  256. return $file;
  257. }
  258. function curl($curl, $url, $headers = array(), $data = array())
  259. {
  260. // Initiate cURL
  261. $curlReq = curl_init($url);
  262. if (in_array(trim(strtoupper($curl)), ["GET", "POST", "PUT", "DELETE"])) {
  263. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  264. } else {
  265. return null;
  266. }
  267. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  268. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  269. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  270. if (localURL($url)) {
  271. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  272. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  273. }
  274. // Format Headers
  275. $cHeaders = array();
  276. foreach ($headers as $k => $v) {
  277. $cHeaders[] = $k . ': ' . $v;
  278. }
  279. if (count($cHeaders)) {
  280. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  281. }
  282. // Format Data
  283. switch (isset($headers['Content-Type']) ? $headers['Content-Type'] : '') {
  284. case 'application/json':
  285. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  286. break;
  287. case 'application/x-www-form-urlencoded':
  288. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  289. break;
  290. default:
  291. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  292. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  293. }
  294. // Execute
  295. $result = curl_exec($curlReq);
  296. $httpcode = curl_getinfo($curlReq);
  297. // Close
  298. curl_close($curlReq);
  299. // Return
  300. return array('content' => $result, 'http_code' => $httpcode);
  301. }
  302. function getHeaders($url)
  303. {
  304. $ch = curl_init($url);
  305. curl_setopt($ch, CURLOPT_NOBODY, true);
  306. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  307. curl_setopt($ch, CURLOPT_HEADER, false);
  308. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  309. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  310. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  311. if (localURL($url)) {
  312. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  313. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  314. }
  315. curl_exec($ch);
  316. $headers = curl_getinfo($ch);
  317. curl_close($ch);
  318. return $headers;
  319. }
  320. function download($url, $path)
  321. {
  322. ini_set('max_execution_time', 0);
  323. set_time_limit(0);
  324. $ch = curl_init($url);
  325. curl_setopt($ch, CURLOPT_HEADER, 1);
  326. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  327. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  328. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  329. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  330. if (localURL($url)) {
  331. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  332. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  333. }
  334. $raw_file_data = curl_exec($ch);
  335. curl_close($ch);
  336. file_put_contents($path, $raw_file_data);
  337. return (filesize($path) > 0) ? true : false;
  338. }
  339. function localURL($url)
  340. {
  341. if (strpos($url, 'https') !== false) {
  342. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  343. $result = (!empty($result) ? true : false);
  344. return $result;
  345. }
  346. return false;
  347. }
  348. function array_filter_key(array $array, $callback)
  349. {
  350. $matchedKeys = array_filter(array_keys($array), $callback);
  351. return array_intersect_key($array, array_flip($matchedKeys));
  352. }
  353. function searchArray($array, $field, $value)
  354. {
  355. foreach ($array as $key => $item) {
  356. if ($item[$field] === $value)
  357. return $key;
  358. }
  359. return false;
  360. }
  361. // Qualify URL
  362. function qualifyURL($url, $return = false)
  363. {
  364. //local address?
  365. if (substr($url, 0, 1) == "/") {
  366. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  367. $protocol = "https://";
  368. } else {
  369. $protocol = "http://";
  370. }
  371. $url = $protocol . getServer() . $url;
  372. }
  373. // Get Digest
  374. $digest = parse_url(rtrim(preg_replace('/\s+/', '', $url), '/'));
  375. // http/https
  376. if (!isset($digest['scheme'])) {
  377. $scheme = 'http';
  378. } else {
  379. $scheme = $digest['scheme'];
  380. }
  381. // Host
  382. $host = (isset($digest['host']) ? $digest['host'] : '');
  383. // Port
  384. $port = (isset($digest['port']) ? ':' . $digest['port'] : '');
  385. // Path
  386. $path = (isset($digest['path']) ? $digest['path'] : '');
  387. // Output
  388. $array = array(
  389. 'scheme' => $scheme,
  390. 'host' => $host,
  391. 'port' => $port,
  392. 'path' => $path
  393. );
  394. return ($return) ? $array : $scheme . '://' . $host . $port . $path;
  395. }
  396. function getServerPath($over = false)
  397. {
  398. if ($over) {
  399. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  400. return $GLOBALS['PHPMAILER-domain'];
  401. }
  402. }
  403. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  404. $protocol = "https://";
  405. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  406. $protocol = "https://";
  407. } else {
  408. $protocol = "http://";
  409. }
  410. $domain = '';
  411. if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false) {
  412. $domain = $_SERVER['SERVER_NAME'];
  413. } elseif (isset($_SERVER['HTTP_HOST'])) {
  414. if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
  415. $domain = explode(':', $_SERVER['HTTP_HOST'])[0];
  416. $port = explode(':', $_SERVER['HTTP_HOST'])[1];
  417. if ($port !== "80" && $port !== "443") {
  418. $domain = $_SERVER['HTTP_HOST'];
  419. }
  420. } else {
  421. $domain = $_SERVER['HTTP_HOST'];
  422. }
  423. }
  424. $url = $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  425. if (strpos($url, '/api') !== false) {
  426. $url = explode('/api', $url);
  427. return $url[0] . '/';
  428. } else {
  429. return $url;
  430. }
  431. }
  432. function get_browser_name()
  433. {
  434. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  435. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
  436. return 'Opera';
  437. } elseif (strpos($user_agent, 'Edge')) {
  438. return 'Edge';
  439. } elseif (strpos($user_agent, 'Chrome')) {
  440. return 'Chrome';
  441. } elseif (strpos($user_agent, 'Safari')) {
  442. return 'Safari';
  443. } elseif (strpos($user_agent, 'Firefox')) {
  444. return 'Firefox';
  445. } elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
  446. return 'Internet Explorer';
  447. }
  448. return 'Other';
  449. }
  450. function getServer($over = false)
  451. {
  452. if ($over) {
  453. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  454. return $GLOBALS['PHPMAILER-domain'];
  455. }
  456. }
  457. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  458. }
  459. /* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
  460. function getIcsEventsAsArray($file)
  461. {
  462. $icalString = file_get_contents_curl($file);
  463. $icsDates = array();
  464. /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
  465. $icsData = explode("BEGIN:", $icalString);
  466. /* Iterating the icsData value to make all the start end dates as sub array */
  467. foreach ($icsData as $key => $value) {
  468. $icsDatesMeta [$key] = explode("\n", $value);
  469. }
  470. /* Itearting the Ics Meta Value */
  471. foreach ($icsDatesMeta as $key => $value) {
  472. foreach ($value as $subKey => $subValue) {
  473. /* to get ics events in proper order */
  474. $icsDates = getICSDates($key, $subKey, $subValue, $icsDates);
  475. }
  476. }
  477. return $icsDates;
  478. }
  479. /* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
  480. function getICSDates($key, $subKey, $subValue, $icsDates)
  481. {
  482. if ($key != 0 && $subKey == 0) {
  483. $icsDates [$key] ["BEGIN"] = $subValue;
  484. } else {
  485. $subValueArr = explode(":", $subValue, 2);
  486. if (isset ($subValueArr [1])) {
  487. $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
  488. }
  489. }
  490. return $icsDates;
  491. }
  492. function file_get_contents_curl($url)
  493. {
  494. $ch = curl_init();
  495. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  496. curl_setopt($ch, CURLOPT_HEADER, 0);
  497. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  498. curl_setopt($ch, CURLOPT_URL, $url);
  499. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  500. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  501. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  502. $data = curl_exec($ch);
  503. curl_close($ch);
  504. return $data;
  505. }
  506. function getExtension($string)
  507. {
  508. return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string);
  509. }
  510. function safe_json_encode($value, $options = 0, $depth = 512)
  511. {
  512. $encoded = json_encode($value, $options, $depth);
  513. if ($encoded === false && $value && json_last_error() == JSON_ERROR_UTF8) {
  514. $encoded = json_encode(utf8ize($value), $options, $depth);
  515. }
  516. return $encoded;
  517. }
  518. function utf8ize($mixed)
  519. {
  520. if (is_array($mixed)) {
  521. foreach ($mixed as $key => $value) {
  522. $mixed[$key] = utf8ize($value);
  523. }
  524. } elseif (is_string($mixed)) {
  525. return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
  526. }
  527. return $mixed;
  528. }
  529. function gen_uuid()
  530. {
  531. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  532. // 32 bits for "time_low"
  533. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  534. // 16 bits for "time_mid"
  535. mt_rand(0, 0xffff),
  536. // 16 bits for "time_hi_and_version",
  537. // four most significant bits holds version number 4
  538. mt_rand(0, 0x0fff) | 0x4000,
  539. // 16 bits, 8 bits for "clk_seq_hi_res",
  540. // 8 bits for "clk_seq_low",
  541. // two most significant bits holds zero and one for variant DCE1.1
  542. mt_rand(0, 0x3fff) | 0x8000,
  543. // 48 bits for "node"
  544. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  545. );
  546. }
  547. function dbExtension($string)
  548. {
  549. return (substr($string, -3) == '.db') ? $string : $string . '.db';
  550. }
  551. function localIPRanges()
  552. {
  553. $mainArray = array(
  554. array(
  555. 'from' => '10.0.0.0',
  556. 'to' => '10.255.255.255'
  557. ),
  558. array(
  559. 'from' => '172.16.0.0',
  560. 'to' => '172.31.255.255'
  561. ),
  562. array(
  563. 'from' => '192.168.0.0',
  564. 'to' => '192.168.255.255'
  565. ),
  566. array(
  567. 'from' => '127.0.0.1',
  568. 'to' => '127.255.255.255'
  569. ),
  570. );
  571. $override = false;
  572. if ($GLOBALS['localIPFrom']) {
  573. $from = trim($GLOBALS['localIPFrom']);
  574. $override = true;
  575. }
  576. if ($GLOBALS['localIPTo']) {
  577. $to = trim($GLOBALS['localIPTo']);
  578. }
  579. if ($override) {
  580. $newArray = array(
  581. 'from' => $from,
  582. 'to' => (isset($to)) ? $to : $from
  583. );
  584. array_push($mainArray, $newArray);
  585. }
  586. return $mainArray;
  587. }
  588. function isLocal($checkIP = null)
  589. {
  590. $isLocal = false;
  591. $userIP = ($checkIP) ? ip2long($checkIP) : ip2long(userIP());
  592. $range = localIPRanges();
  593. foreach ($range as $ip) {
  594. $low = ip2long($ip['from']);
  595. $high = ip2long($ip['to']);
  596. if ($userIP <= $high && $low <= $userIP) {
  597. $isLocal = true;
  598. }
  599. }
  600. return $isLocal;
  601. }
  602. function checkOverrideURL($url, $override)
  603. {
  604. if (strpos($override, $url) !== false) {
  605. return $override;
  606. } else {
  607. return $url . $override;
  608. }
  609. }
  610. function clearPOSTPassword($array)
  611. {
  612. if (isset($array['data'])) {
  613. foreach ($array['data'] as $k => $v) {
  614. // clear password from array
  615. if ($k == 'password') {
  616. $array['data'][$k] = '*******';
  617. }
  618. }
  619. }
  620. if (isset($array['data']['data'])) {
  621. foreach ($array['data']['data'] as $k => $v) {
  622. // clear password from array
  623. if ($k == 'password') {
  624. $array['data']['data'][$k] = '*******';
  625. }
  626. }
  627. }
  628. return $array;
  629. }
  630. function timeExecution($previous = null)
  631. {
  632. if (!$previous) {
  633. return microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
  634. } else {
  635. return (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) - $previous;
  636. }
  637. }
  638. function formatSeconds($seconds)
  639. {
  640. $hours = 0;
  641. $milliseconds = str_replace("0.", '', $seconds - floor($seconds));
  642. if ($seconds > 3600) {
  643. $hours = floor($seconds / 3600);
  644. }
  645. $seconds = $seconds % 3600;
  646. $time = str_pad($hours, 2, '0', STR_PAD_LEFT)
  647. . gmdate(':i:s', $seconds)
  648. . ($milliseconds ? '.' . $milliseconds : '');
  649. $parts = explode(':', $time);
  650. $timeExtra = explode('.', $parts[2]);
  651. if ($parts[0] !== '00') { // hours
  652. return $time;
  653. } elseif ($parts[1] !== '00') { // mins
  654. return $parts[1] . 'min(s) ' . $timeExtra[0] . 's';
  655. } elseif ($timeExtra[0] !== '00') { // secs
  656. return substr($parts[2], 0, 5) . 's | ' . substr($parts[2], 0, 7) * 1000 . 'ms';
  657. } else {
  658. return substr($parts[2], 0, 7) * 1000 . 'ms';
  659. }
  660. //return $timeExtra[0] . 's ' . (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  661. //return (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  662. }