normal-functions.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 coookieSeconds($type, $name, $value = '', $ms, $http = true)
  125. {
  126. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  127. $Secure = true;
  128. $HTTPOnly = true;
  129. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' && $_SERVER['HTTPS'] !== '') {
  130. $Secure = true;
  131. $HTTPOnly = true;
  132. } else {
  133. $Secure = false;
  134. $HTTPOnly = false;
  135. }
  136. if (!$http) {
  137. $HTTPOnly = false;
  138. }
  139. $Path = '/';
  140. $Domain = parseDomain($_SERVER['HTTP_HOST']);
  141. $DomainTest = parseDomain($_SERVER['HTTP_HOST'], true);
  142. if ($type == 'set') {
  143. $_COOKIE[$name] = $value;
  144. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  145. . (empty($ms) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() + ($ms / 1000)) . ' GMT')
  146. . (empty($Path) ? '' : '; path=' . $Path)
  147. . (empty($Domain) ? '' : '; domain=' . $Domain)
  148. . (!$Secure ? '' : '; secure')
  149. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  150. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  151. . (empty($ms) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() + ($ms / 1000)) . ' GMT')
  152. . (empty($Path) ? '' : '; path=' . $Path)
  153. . (empty($Domain) ? '' : '; domain=' . $DomainTest)
  154. . (!$Secure ? '' : '; secure')
  155. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  156. } elseif ($type == 'delete') {
  157. unset($_COOKIE[$name]);
  158. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  159. . (empty($ms) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() - 3600) . ' GMT')
  160. . (empty($Path) ? '' : '; path=' . $Path)
  161. . (empty($Domain) ? '' : '; domain=' . $Domain)
  162. . (!$Secure ? '' : '; secure')
  163. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  164. header('Set-Cookie: ' . rawurlencode($name) . '=' . rawurlencode($value)
  165. . (empty($ms) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', time() - 3600) . ' GMT')
  166. . (empty($Path) ? '' : '; path=' . $Path)
  167. . (empty($Domain) ? '' : '; domain=' . $DomainTest)
  168. . (!$Secure ? '' : '; secure')
  169. . (!$HTTPOnly ? '' : '; HttpOnly'), false);
  170. }
  171. }
  172. function getOS()
  173. {
  174. if (PHP_SHLIB_SUFFIX == "dll") {
  175. return "win";
  176. } else {
  177. return "*nix";
  178. }
  179. }
  180. if (!function_exists('getallheaders')) {
  181. function getallheaders()
  182. {
  183. $headers = array();
  184. foreach ($_SERVER as $name => $value) {
  185. if (substr($name, 0, 5) == 'HTTP_') {
  186. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  187. }
  188. }
  189. return $headers;
  190. }
  191. }
  192. function random_ascii_string($length)
  193. {
  194. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  195. $charactersLength = strlen($characters);
  196. $randomString = '';
  197. for ($i = 0; $i < $length; $i++) {
  198. $randomString .= $characters[rand(0, $charactersLength - 1)];
  199. }
  200. return $randomString;
  201. }
  202. // Generate Random string
  203. function randString($length = 10, $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  204. {
  205. $tmp = '';
  206. for ($i = 0; $i < $length; $i++) {
  207. $tmp .= substr(str_shuffle($chars), 0, 1);
  208. }
  209. return $tmp;
  210. }
  211. function isEncrypted($password)
  212. {
  213. switch (strlen($password)) {
  214. case '24':
  215. return (strpos($password, '==') !== false) ? true : false;
  216. break;
  217. case '44':
  218. return (substr($password, -1, 1) == '=') ? true : false;
  219. break;
  220. case '64':
  221. return true;
  222. case '88':
  223. return (strpos($password, '==') !== false) ? true : false;
  224. break;
  225. case '108':
  226. return (substr($password, -1, 1) == '=') ? true : false;
  227. break;
  228. default:
  229. return false;
  230. }
  231. }
  232. function encrypt($password, $key = null)
  233. {
  234. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  235. return openssl_encrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  236. }
  237. function decrypt($password, $key = null)
  238. {
  239. if (empty($password)) {
  240. return '';
  241. }
  242. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  243. return openssl_decrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  244. }
  245. function fillString($string, $length)
  246. {
  247. $filler = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*';
  248. if (strlen($string) < $length) {
  249. $diff = $length - strlen($string);
  250. $filler = substr($filler, 0, $diff);
  251. return $string . $filler;
  252. } elseif (strlen($string) > $length) {
  253. return substr($string, 0, $length);
  254. } else {
  255. return $string;
  256. }
  257. }
  258. function userIP()
  259. {
  260. if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  261. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  262. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  263. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  264. } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
  265. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  266. } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  267. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  268. } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
  269. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  270. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  271. $ipaddress = $_SERVER['REMOTE_ADDR'];
  272. } else {
  273. $ipaddress = 'UNKNOWN';
  274. }
  275. if (strpos($ipaddress, ',') !== false) {
  276. list($first, $last) = explode(",", $ipaddress);
  277. unset($last);
  278. return $first;
  279. } else {
  280. return $ipaddress;
  281. }
  282. }
  283. function arrayIP($string)
  284. {
  285. if (strpos($string, ',') !== false) {
  286. $result = explode(",", $string);
  287. } else {
  288. $result = array($string);
  289. }
  290. foreach ($result as &$ip) {
  291. $ip = is_numeric(substr($ip, 0, 1)) ? $ip : gethostbyname($ip);
  292. }
  293. return $result;
  294. }
  295. function getCert()
  296. {
  297. $url = 'http://curl.haxx.se/ca/cacert.pem';
  298. $file = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'cacert.pem';
  299. if (!file_exists($file)) {
  300. file_put_contents($file, fopen($url, 'r'));
  301. } elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  302. file_put_contents($file, fopen($url, 'r'));
  303. }
  304. return $file;
  305. }
  306. function curl($curl, $url, $headers = array(), $data = array())
  307. {
  308. // Initiate cURL
  309. $curlReq = curl_init($url);
  310. if (in_array(trim(strtoupper($curl)), ["GET", "POST", "PUT", "DELETE"])) {
  311. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  312. } else {
  313. return null;
  314. }
  315. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  316. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  317. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  318. if (localURL($url)) {
  319. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  320. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  321. }
  322. // Format Headers
  323. $cHeaders = array();
  324. foreach ($headers as $k => $v) {
  325. $cHeaders[] = $k . ': ' . $v;
  326. }
  327. if (count($cHeaders)) {
  328. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  329. }
  330. // Format Data
  331. switch (isset($headers['Content-Type']) ? $headers['Content-Type'] : '') {
  332. case 'application/json':
  333. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  334. break;
  335. case 'application/x-www-form-urlencoded':
  336. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  337. break;
  338. default:
  339. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  340. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  341. }
  342. // Execute
  343. $result = curl_exec($curlReq);
  344. $httpcode = curl_getinfo($curlReq);
  345. // Close
  346. curl_close($curlReq);
  347. // Return
  348. return array('content' => $result, 'http_code' => $httpcode);
  349. }
  350. function getHeaders($url)
  351. {
  352. $ch = curl_init($url);
  353. curl_setopt($ch, CURLOPT_NOBODY, true);
  354. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  355. curl_setopt($ch, CURLOPT_HEADER, false);
  356. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  357. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  358. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  359. if (localURL($url)) {
  360. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  361. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  362. }
  363. curl_exec($ch);
  364. $headers = curl_getinfo($ch);
  365. curl_close($ch);
  366. return $headers;
  367. }
  368. function download($url, $path)
  369. {
  370. ini_set('max_execution_time', 0);
  371. set_time_limit(0);
  372. $ch = curl_init($url);
  373. curl_setopt($ch, CURLOPT_HEADER, 1);
  374. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  375. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  376. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  377. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  378. if (localURL($url)) {
  379. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  380. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  381. }
  382. $raw_file_data = curl_exec($ch);
  383. curl_close($ch);
  384. file_put_contents($path, $raw_file_data);
  385. return (filesize($path) > 0) ? true : false;
  386. }
  387. function localURL($url)
  388. {
  389. if (strpos($url, 'https') !== false) {
  390. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  391. $result = (!empty($result) ? true : false);
  392. return $result;
  393. }
  394. return false;
  395. }
  396. function array_filter_key(array $array, $callback)
  397. {
  398. $matchedKeys = array_filter(array_keys($array), $callback);
  399. return array_intersect_key($array, array_flip($matchedKeys));
  400. }
  401. function searchArray($array, $field, $value)
  402. {
  403. foreach ($array as $key => $item) {
  404. if ($item[$field] === $value)
  405. return $key;
  406. }
  407. return false;
  408. }
  409. // Qualify URL
  410. function qualifyURL($url, $return = false)
  411. {
  412. //local address?
  413. if (substr($url, 0, 1) == "/") {
  414. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  415. $protocol = "https://";
  416. } else {
  417. $protocol = "http://";
  418. }
  419. $url = $protocol . getServer() . $url;
  420. }
  421. // Get Digest
  422. $digest = parse_url(rtrim(preg_replace('/\s+/', '', $url), '/'));
  423. // http/https
  424. if (!isset($digest['scheme'])) {
  425. $scheme = 'http';
  426. } else {
  427. $scheme = $digest['scheme'];
  428. }
  429. // Host
  430. $host = (isset($digest['host']) ? $digest['host'] : '');
  431. // Port
  432. $port = (isset($digest['port']) ? ':' . $digest['port'] : '');
  433. // Path
  434. $path = (isset($digest['path']) ? $digest['path'] : '');
  435. // Output
  436. $array = array(
  437. 'scheme' => $scheme,
  438. 'host' => $host,
  439. 'port' => $port,
  440. 'path' => $path
  441. );
  442. return ($return) ? $array : $scheme . '://' . $host . $port . $path;
  443. }
  444. function getServerPath($over = false)
  445. {
  446. if ($over) {
  447. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  448. return $GLOBALS['PHPMAILER-domain'];
  449. }
  450. }
  451. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  452. $protocol = "https://";
  453. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  454. $protocol = "https://";
  455. } else {
  456. $protocol = "http://";
  457. }
  458. $domain = '';
  459. if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false) {
  460. $domain = $_SERVER['SERVER_NAME'];
  461. } elseif (isset($_SERVER['HTTP_HOST'])) {
  462. if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
  463. $domain = explode(':', $_SERVER['HTTP_HOST'])[0];
  464. $port = explode(':', $_SERVER['HTTP_HOST'])[1];
  465. if ($port !== "80" && $port !== "443") {
  466. $domain = $_SERVER['HTTP_HOST'];
  467. }
  468. } else {
  469. $domain = $_SERVER['HTTP_HOST'];
  470. }
  471. }
  472. $url = $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  473. if (strpos($url, '/api') !== false) {
  474. $url = explode('/api', $url);
  475. return $url[0] . '/';
  476. } else {
  477. return $url;
  478. }
  479. }
  480. function get_browser_name()
  481. {
  482. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  483. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
  484. return 'Opera';
  485. } elseif (strpos($user_agent, 'Edge')) {
  486. return 'Edge';
  487. } elseif (strpos($user_agent, 'Chrome')) {
  488. return 'Chrome';
  489. } elseif (strpos($user_agent, 'Safari')) {
  490. return 'Safari';
  491. } elseif (strpos($user_agent, 'Firefox')) {
  492. return 'Firefox';
  493. } elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
  494. return 'Internet Explorer';
  495. }
  496. return 'Other';
  497. }
  498. function getServer($over = false)
  499. {
  500. if ($over) {
  501. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  502. return $GLOBALS['PHPMAILER-domain'];
  503. }
  504. }
  505. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  506. }
  507. /* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
  508. function getIcsEventsAsArray($file)
  509. {
  510. $icalString = file_get_contents_curl($file);
  511. $icsDates = array();
  512. /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
  513. $icsData = explode("BEGIN:", $icalString);
  514. /* Iterating the icsData value to make all the start end dates as sub array */
  515. foreach ($icsData as $key => $value) {
  516. $icsDatesMeta [$key] = explode("\n", $value);
  517. }
  518. /* Itearting the Ics Meta Value */
  519. foreach ($icsDatesMeta as $key => $value) {
  520. foreach ($value as $subKey => $subValue) {
  521. /* to get ics events in proper order */
  522. $icsDates = getICSDates($key, $subKey, $subValue, $icsDates);
  523. }
  524. }
  525. return $icsDates;
  526. }
  527. /* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
  528. function getICSDates($key, $subKey, $subValue, $icsDates)
  529. {
  530. if ($key != 0 && $subKey == 0) {
  531. $icsDates [$key] ["BEGIN"] = $subValue;
  532. } else {
  533. $subValueArr = explode(":", $subValue, 2);
  534. if (isset ($subValueArr [1])) {
  535. $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
  536. }
  537. }
  538. return $icsDates;
  539. }
  540. function file_get_contents_curl($url)
  541. {
  542. $ch = curl_init();
  543. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  544. curl_setopt($ch, CURLOPT_HEADER, 0);
  545. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  546. curl_setopt($ch, CURLOPT_URL, $url);
  547. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  548. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  549. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  550. $data = curl_exec($ch);
  551. curl_close($ch);
  552. return $data;
  553. }
  554. function getExtension($string)
  555. {
  556. return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string);
  557. }
  558. function safe_json_encode($value, $options = 0, $depth = 512)
  559. {
  560. $encoded = json_encode($value, $options, $depth);
  561. if ($encoded === false && $value && json_last_error() == JSON_ERROR_UTF8) {
  562. $encoded = json_encode(utf8ize($value), $options, $depth);
  563. }
  564. return $encoded;
  565. }
  566. function utf8ize($mixed)
  567. {
  568. if (is_array($mixed)) {
  569. foreach ($mixed as $key => $value) {
  570. $mixed[$key] = utf8ize($value);
  571. }
  572. } elseif (is_string($mixed)) {
  573. return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
  574. }
  575. return $mixed;
  576. }
  577. function gen_uuid()
  578. {
  579. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  580. // 32 bits for "time_low"
  581. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  582. // 16 bits for "time_mid"
  583. mt_rand(0, 0xffff),
  584. // 16 bits for "time_hi_and_version",
  585. // four most significant bits holds version number 4
  586. mt_rand(0, 0x0fff) | 0x4000,
  587. // 16 bits, 8 bits for "clk_seq_hi_res",
  588. // 8 bits for "clk_seq_low",
  589. // two most significant bits holds zero and one for variant DCE1.1
  590. mt_rand(0, 0x3fff) | 0x8000,
  591. // 48 bits for "node"
  592. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  593. );
  594. }
  595. function dbExtension($string)
  596. {
  597. return (substr($string, -3) == '.db') ? $string : $string . '.db';
  598. }
  599. function localIPRanges()
  600. {
  601. $mainArray = array(
  602. array(
  603. 'from' => '10.0.0.0',
  604. 'to' => '10.255.255.255'
  605. ),
  606. array(
  607. 'from' => '172.16.0.0',
  608. 'to' => '172.31.255.255'
  609. ),
  610. array(
  611. 'from' => '192.168.0.0',
  612. 'to' => '192.168.255.255'
  613. ),
  614. array(
  615. 'from' => '127.0.0.1',
  616. 'to' => '127.255.255.255'
  617. ),
  618. );
  619. $override = false;
  620. if ($GLOBALS['localIPFrom']) {
  621. $from = trim($GLOBALS['localIPFrom']);
  622. $override = true;
  623. }
  624. if ($GLOBALS['localIPTo']) {
  625. $to = trim($GLOBALS['localIPTo']);
  626. }
  627. if ($override) {
  628. $newArray = array(
  629. 'from' => $from,
  630. 'to' => (isset($to)) ? $to : $from
  631. );
  632. array_push($mainArray, $newArray);
  633. }
  634. return $mainArray;
  635. }
  636. function isLocal($checkIP = null)
  637. {
  638. $isLocal = false;
  639. $userIP = ($checkIP) ? ip2long($checkIP) : ip2long(userIP());
  640. $range = localIPRanges();
  641. foreach ($range as $ip) {
  642. $low = ip2long($ip['from']);
  643. $high = ip2long($ip['to']);
  644. if ($userIP <= $high && $low <= $userIP) {
  645. $isLocal = true;
  646. }
  647. }
  648. return $isLocal;
  649. }
  650. function checkOverrideURL($url, $override)
  651. {
  652. if (strpos($override, $url) !== false) {
  653. return $override;
  654. } else {
  655. return $url . $override;
  656. }
  657. }
  658. function clearPOSTPassword($array)
  659. {
  660. if (isset($array['data'])) {
  661. foreach ($array['data'] as $k => $v) {
  662. // clear password from array
  663. if ($k == 'password') {
  664. $array['data'][$k] = '*******';
  665. }
  666. }
  667. }
  668. if (isset($array['data']['data'])) {
  669. foreach ($array['data']['data'] as $k => $v) {
  670. // clear password from array
  671. if ($k == 'password') {
  672. $array['data']['data'][$k] = '*******';
  673. }
  674. }
  675. }
  676. return $array;
  677. }
  678. function timeExecution($previous = null)
  679. {
  680. if (!$previous) {
  681. return microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
  682. } else {
  683. return (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) - $previous;
  684. }
  685. }
  686. function formatSeconds($seconds)
  687. {
  688. $hours = 0;
  689. $milliseconds = str_replace("0.", '', $seconds - floor($seconds));
  690. if ($seconds > 3600) {
  691. $hours = floor($seconds / 3600);
  692. }
  693. $seconds = $seconds % 3600;
  694. $time = str_pad($hours, 2, '0', STR_PAD_LEFT)
  695. . gmdate(':i:s', $seconds)
  696. . ($milliseconds ? '.' . $milliseconds : '');
  697. $parts = explode(':', $time);
  698. $timeExtra = explode('.', $parts[2]);
  699. if ($parts[0] !== '00') { // hours
  700. return $time;
  701. } elseif ($parts[1] !== '00') { // mins
  702. return $parts[1] . 'min(s) ' . $timeExtra[0] . 's';
  703. } elseif ($timeExtra[0] !== '00') { // secs
  704. return substr($parts[2], 0, 5) . 's | ' . substr($parts[2], 0, 7) * 1000 . 'ms';
  705. } else {
  706. return substr($parts[2], 0, 7) * 1000 . 'ms';
  707. }
  708. //return $timeExtra[0] . 's ' . (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  709. //return (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  710. }