normal-functions.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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($GLOBALS['selfSignedCert'] !== ''){
  300. if(file_exists($GLOBALS['selfSignedCert'])){
  301. return $GLOBALS['selfSignedCert'];
  302. }
  303. }
  304. if (!file_exists($file)) {
  305. file_put_contents($file, fopen($url, 'r'));
  306. } elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  307. file_put_contents($file, fopen($url, 'r'));
  308. }
  309. return $file;
  310. }
  311. function curl($curl, $url, $headers = array(), $data = array())
  312. {
  313. // Initiate cURL
  314. $curlReq = curl_init($url);
  315. if (in_array(trim(strtoupper($curl)), ["GET", "POST", "PUT", "DELETE"])) {
  316. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  317. } else {
  318. return null;
  319. }
  320. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  321. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  322. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  323. if (localURL($url)) {
  324. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  325. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  326. }
  327. // Format Headers
  328. $cHeaders = array();
  329. foreach ($headers as $k => $v) {
  330. $cHeaders[] = $k . ': ' . $v;
  331. }
  332. if (count($cHeaders)) {
  333. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  334. }
  335. // Format Data
  336. switch (isset($headers['Content-Type']) ? $headers['Content-Type'] : '') {
  337. case 'application/json':
  338. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  339. break;
  340. case 'application/x-www-form-urlencoded':
  341. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  342. break;
  343. default:
  344. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  345. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  346. }
  347. // Execute
  348. $result = curl_exec($curlReq);
  349. $httpcode = curl_getinfo($curlReq);
  350. // Close
  351. curl_close($curlReq);
  352. // Return
  353. return array('content' => $result, 'http_code' => $httpcode);
  354. }
  355. function getHeaders($url)
  356. {
  357. $ch = curl_init($url);
  358. curl_setopt($ch, CURLOPT_NOBODY, true);
  359. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  360. curl_setopt($ch, CURLOPT_HEADER, false);
  361. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  362. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  363. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  364. if (localURL($url)) {
  365. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  366. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  367. }
  368. curl_exec($ch);
  369. $headers = curl_getinfo($ch);
  370. curl_close($ch);
  371. return $headers;
  372. }
  373. function download($url, $path)
  374. {
  375. ini_set('max_execution_time', 0);
  376. set_time_limit(0);
  377. $ch = curl_init($url);
  378. curl_setopt($ch, CURLOPT_HEADER, 1);
  379. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  380. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  381. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  382. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  383. if (localURL($url)) {
  384. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  385. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  386. }
  387. $raw_file_data = curl_exec($ch);
  388. curl_close($ch);
  389. file_put_contents($path, $raw_file_data);
  390. return (filesize($path) > 0) ? true : false;
  391. }
  392. function localURL($url)
  393. {
  394. if (strpos($url, 'https') !== false) {
  395. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  396. $result = (!empty($result) ? true : false);
  397. return $result;
  398. }
  399. return false;
  400. }
  401. function array_filter_key(array $array, $callback)
  402. {
  403. $matchedKeys = array_filter(array_keys($array), $callback);
  404. return array_intersect_key($array, array_flip($matchedKeys));
  405. }
  406. function searchArray($array, $field, $value)
  407. {
  408. foreach ($array as $key => $item) {
  409. if ($item[$field] === $value)
  410. return $key;
  411. }
  412. return false;
  413. }
  414. // Qualify URL
  415. function qualifyURL($url, $return = false)
  416. {
  417. //local address?
  418. if (substr($url, 0, 1) == "/") {
  419. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  420. $protocol = "https://";
  421. } else {
  422. $protocol = "http://";
  423. }
  424. $url = $protocol . getServer() . $url;
  425. }
  426. // Get Digest
  427. $digest = parse_url(rtrim(preg_replace('/\s+/', '', $url), '/'));
  428. // http/https
  429. if (!isset($digest['scheme'])) {
  430. $scheme = 'http';
  431. } else {
  432. $scheme = $digest['scheme'];
  433. }
  434. // Host
  435. $host = (isset($digest['host']) ? $digest['host'] : '');
  436. // Port
  437. $port = (isset($digest['port']) ? ':' . $digest['port'] : '');
  438. // Path
  439. $path = (isset($digest['path']) ? $digest['path'] : '');
  440. // Output
  441. $array = array(
  442. 'scheme' => $scheme,
  443. 'host' => $host,
  444. 'port' => $port,
  445. 'path' => $path
  446. );
  447. return ($return) ? $array : $scheme . '://' . $host . $port . $path;
  448. }
  449. function getServerPath($over = false)
  450. {
  451. if ($over) {
  452. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  453. return $GLOBALS['PHPMAILER-domain'];
  454. }
  455. }
  456. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  457. $protocol = "https://";
  458. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  459. $protocol = "https://";
  460. } else {
  461. $protocol = "http://";
  462. }
  463. $domain = '';
  464. if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false) {
  465. $domain = $_SERVER['SERVER_NAME'];
  466. } elseif (isset($_SERVER['HTTP_HOST'])) {
  467. if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
  468. $domain = explode(':', $_SERVER['HTTP_HOST'])[0];
  469. $port = explode(':', $_SERVER['HTTP_HOST'])[1];
  470. if ($port !== "80" && $port !== "443") {
  471. $domain = $_SERVER['HTTP_HOST'];
  472. }
  473. } else {
  474. $domain = $_SERVER['HTTP_HOST'];
  475. }
  476. }
  477. $url = $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  478. if (strpos($url, '/api') !== false) {
  479. $url = explode('/api', $url);
  480. return $url[0] . '/';
  481. } else {
  482. return $url;
  483. }
  484. }
  485. function get_browser_name()
  486. {
  487. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  488. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
  489. return 'Opera';
  490. } elseif (strpos($user_agent, 'Edge')) {
  491. return 'Edge';
  492. } elseif (strpos($user_agent, 'Chrome')) {
  493. return 'Chrome';
  494. } elseif (strpos($user_agent, 'Safari')) {
  495. return 'Safari';
  496. } elseif (strpos($user_agent, 'Firefox')) {
  497. return 'Firefox';
  498. } elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
  499. return 'Internet Explorer';
  500. }
  501. return 'Other';
  502. }
  503. function getServer($over = false)
  504. {
  505. if ($over) {
  506. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  507. return $GLOBALS['PHPMAILER-domain'];
  508. }
  509. }
  510. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  511. }
  512. /* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
  513. function getIcsEventsAsArray($file)
  514. {
  515. $icalString = file_get_contents_curl($file);
  516. $icsDates = array();
  517. /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
  518. $icsData = explode("BEGIN:", $icalString);
  519. /* Iterating the icsData value to make all the start end dates as sub array */
  520. foreach ($icsData as $key => $value) {
  521. $icsDatesMeta [$key] = explode("\n", $value);
  522. }
  523. /* Itearting the Ics Meta Value */
  524. foreach ($icsDatesMeta as $key => $value) {
  525. foreach ($value as $subKey => $subValue) {
  526. /* to get ics events in proper order */
  527. $icsDates = getICSDates($key, $subKey, $subValue, $icsDates);
  528. }
  529. }
  530. return $icsDates;
  531. }
  532. /* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
  533. function getICSDates($key, $subKey, $subValue, $icsDates)
  534. {
  535. if ($key != 0 && $subKey == 0) {
  536. $icsDates [$key] ["BEGIN"] = $subValue;
  537. } else {
  538. $subValueArr = explode(":", $subValue, 2);
  539. if (isset ($subValueArr [1])) {
  540. $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
  541. }
  542. }
  543. return $icsDates;
  544. }
  545. function file_get_contents_curl($url)
  546. {
  547. $ch = curl_init();
  548. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  549. curl_setopt($ch, CURLOPT_HEADER, 0);
  550. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  551. curl_setopt($ch, CURLOPT_URL, $url);
  552. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  553. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  554. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  555. $data = curl_exec($ch);
  556. curl_close($ch);
  557. return $data;
  558. }
  559. function getExtension($string)
  560. {
  561. return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string);
  562. }
  563. function safe_json_encode($value, $options = 0, $depth = 512)
  564. {
  565. $encoded = json_encode($value, $options, $depth);
  566. if ($encoded === false && $value && json_last_error() == JSON_ERROR_UTF8) {
  567. $encoded = json_encode(utf8ize($value), $options, $depth);
  568. }
  569. return $encoded;
  570. }
  571. function utf8ize($mixed)
  572. {
  573. if (is_array($mixed)) {
  574. foreach ($mixed as $key => $value) {
  575. $mixed[$key] = utf8ize($value);
  576. }
  577. } elseif (is_string($mixed)) {
  578. return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
  579. }
  580. return $mixed;
  581. }
  582. function gen_uuid()
  583. {
  584. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  585. // 32 bits for "time_low"
  586. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  587. // 16 bits for "time_mid"
  588. mt_rand(0, 0xffff),
  589. // 16 bits for "time_hi_and_version",
  590. // four most significant bits holds version number 4
  591. mt_rand(0, 0x0fff) | 0x4000,
  592. // 16 bits, 8 bits for "clk_seq_hi_res",
  593. // 8 bits for "clk_seq_low",
  594. // two most significant bits holds zero and one for variant DCE1.1
  595. mt_rand(0, 0x3fff) | 0x8000,
  596. // 48 bits for "node"
  597. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  598. );
  599. }
  600. function dbExtension($string)
  601. {
  602. return (substr($string, -3) == '.db') ? $string : $string . '.db';
  603. }
  604. function localIPRanges()
  605. {
  606. $mainArray = array(
  607. array(
  608. 'from' => '10.0.0.0',
  609. 'to' => '10.255.255.255'
  610. ),
  611. array(
  612. 'from' => '172.16.0.0',
  613. 'to' => '172.31.255.255'
  614. ),
  615. array(
  616. 'from' => '192.168.0.0',
  617. 'to' => '192.168.255.255'
  618. ),
  619. array(
  620. 'from' => '127.0.0.1',
  621. 'to' => '127.255.255.255'
  622. ),
  623. );
  624. $override = false;
  625. if ($GLOBALS['localIPFrom']) {
  626. $from = trim($GLOBALS['localIPFrom']);
  627. $override = true;
  628. }
  629. if ($GLOBALS['localIPTo']) {
  630. $to = trim($GLOBALS['localIPTo']);
  631. }
  632. if ($override) {
  633. $newArray = array(
  634. 'from' => $from,
  635. 'to' => (isset($to)) ? $to : $from
  636. );
  637. array_push($mainArray, $newArray);
  638. }
  639. return $mainArray;
  640. }
  641. function isLocal($checkIP = null)
  642. {
  643. $isLocal = false;
  644. $userIP = ($checkIP) ? ip2long($checkIP) : ip2long(userIP());
  645. $range = localIPRanges();
  646. foreach ($range as $ip) {
  647. $low = ip2long($ip['from']);
  648. $high = ip2long($ip['to']);
  649. if ($userIP <= $high && $low <= $userIP) {
  650. $isLocal = true;
  651. }
  652. }
  653. return $isLocal;
  654. }
  655. function checkOverrideURL($url, $override)
  656. {
  657. if (strpos($override, $url) !== false) {
  658. return $override;
  659. } else {
  660. return $url . $override;
  661. }
  662. }
  663. function clearPOSTPassword($array)
  664. {
  665. if (isset($array['data'])) {
  666. foreach ($array['data'] as $k => $v) {
  667. // clear password from array
  668. if ($k == 'password') {
  669. $array['data'][$k] = '*******';
  670. }
  671. }
  672. }
  673. if (isset($array['data']['data'])) {
  674. foreach ($array['data']['data'] as $k => $v) {
  675. // clear password from array
  676. if ($k == 'password') {
  677. $array['data']['data'][$k] = '*******';
  678. }
  679. }
  680. }
  681. return $array;
  682. }
  683. function timeExecution($previous = null)
  684. {
  685. if (!$previous) {
  686. return microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
  687. } else {
  688. return (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) - $previous;
  689. }
  690. }
  691. function formatSeconds($seconds)
  692. {
  693. $hours = 0;
  694. $milliseconds = str_replace("0.", '', $seconds - floor($seconds));
  695. if ($seconds > 3600) {
  696. $hours = floor($seconds / 3600);
  697. }
  698. $seconds = $seconds % 3600;
  699. $time = str_pad($hours, 2, '0', STR_PAD_LEFT)
  700. . gmdate(':i:s', $seconds)
  701. . ($milliseconds ? '.' . $milliseconds : '');
  702. $parts = explode(':', $time);
  703. $timeExtra = explode('.', $parts[2]);
  704. if ($parts[0] !== '00') { // hours
  705. return $time;
  706. } elseif ($parts[1] !== '00') { // mins
  707. return $parts[1] . 'min(s) ' . $timeExtra[0] . 's';
  708. } elseif ($timeExtra[0] !== '00') { // secs
  709. return substr($parts[2], 0, 5) . 's | ' . substr($parts[2], 0, 7) * 1000 . 'ms';
  710. } else {
  711. return substr($parts[2], 0, 7) * 1000 . 'ms';
  712. }
  713. //return $timeExtra[0] . 's ' . (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  714. //return (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  715. }