normal-functions.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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, $force = false)
  393. {
  394. if($force){
  395. return true;
  396. }
  397. if (strpos($url, 'https') !== false) {
  398. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  399. $result = (!empty($result) ? true : false);
  400. return $result;
  401. }
  402. return false;
  403. }
  404. function array_filter_key(array $array, $callback)
  405. {
  406. $matchedKeys = array_filter(array_keys($array), $callback);
  407. return array_intersect_key($array, array_flip($matchedKeys));
  408. }
  409. function searchArray($array, $field, $value)
  410. {
  411. foreach ($array as $key => $item) {
  412. if ($item[$field] === $value)
  413. return $key;
  414. }
  415. return false;
  416. }
  417. // Qualify URL
  418. function qualifyURL($url, $return = false)
  419. {
  420. //local address?
  421. if (substr($url, 0, 1) == "/") {
  422. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  423. $protocol = "https://";
  424. } else {
  425. $protocol = "http://";
  426. }
  427. $url = $protocol . getServer() . $url;
  428. }
  429. // Get Digest
  430. $digest = parse_url(rtrim(preg_replace('/\s+/', '', $url), '/'));
  431. // http/https
  432. if (!isset($digest['scheme'])) {
  433. $scheme = 'http';
  434. } else {
  435. $scheme = $digest['scheme'];
  436. }
  437. // Host
  438. $host = (isset($digest['host']) ? $digest['host'] : '');
  439. // Port
  440. $port = (isset($digest['port']) ? ':' . $digest['port'] : '');
  441. // Path
  442. $path = (isset($digest['path']) ? $digest['path'] : '');
  443. // Output
  444. $array = array(
  445. 'scheme' => $scheme,
  446. 'host' => $host,
  447. 'port' => $port,
  448. 'path' => $path
  449. );
  450. return ($return) ? $array : $scheme . '://' . $host . $port . $path;
  451. }
  452. function getServerPath($over = false)
  453. {
  454. if ($over) {
  455. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  456. return $GLOBALS['PHPMAILER-domain'];
  457. }
  458. }
  459. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  460. $protocol = "https://";
  461. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  462. $protocol = "https://";
  463. } else {
  464. $protocol = "http://";
  465. }
  466. $domain = '';
  467. if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false) {
  468. $domain = $_SERVER['SERVER_NAME'];
  469. } elseif (isset($_SERVER['HTTP_HOST'])) {
  470. if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
  471. $domain = explode(':', $_SERVER['HTTP_HOST'])[0];
  472. $port = explode(':', $_SERVER['HTTP_HOST'])[1];
  473. if ($port !== "80" && $port !== "443") {
  474. $domain = $_SERVER['HTTP_HOST'];
  475. }
  476. } else {
  477. $domain = $_SERVER['HTTP_HOST'];
  478. }
  479. }
  480. $url = $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  481. if (strpos($url, '/api') !== false) {
  482. $url = explode('/api', $url);
  483. return $url[0] . '/';
  484. } else {
  485. return $url;
  486. }
  487. }
  488. function get_browser_name()
  489. {
  490. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  491. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
  492. return 'Opera';
  493. } elseif (strpos($user_agent, 'Edge')) {
  494. return 'Edge';
  495. } elseif (strpos($user_agent, 'Chrome')) {
  496. return 'Chrome';
  497. } elseif (strpos($user_agent, 'Safari')) {
  498. return 'Safari';
  499. } elseif (strpos($user_agent, 'Firefox')) {
  500. return 'Firefox';
  501. } elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
  502. return 'Internet Explorer';
  503. }
  504. return 'Other';
  505. }
  506. function getServer($over = false)
  507. {
  508. if ($over) {
  509. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  510. return $GLOBALS['PHPMAILER-domain'];
  511. }
  512. }
  513. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  514. }
  515. /* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
  516. function getIcsEventsAsArray($file)
  517. {
  518. $icalString = file_get_contents_curl($file);
  519. $icsDates = array();
  520. /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
  521. $icsData = explode("BEGIN:", $icalString);
  522. /* Iterating the icsData value to make all the start end dates as sub array */
  523. foreach ($icsData as $key => $value) {
  524. $icsDatesMeta [$key] = explode("\n", $value);
  525. }
  526. /* Itearting the Ics Meta Value */
  527. foreach ($icsDatesMeta as $key => $value) {
  528. foreach ($value as $subKey => $subValue) {
  529. /* to get ics events in proper order */
  530. $icsDates = getICSDates($key, $subKey, $subValue, $icsDates);
  531. }
  532. }
  533. return $icsDates;
  534. }
  535. /* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
  536. function getICSDates($key, $subKey, $subValue, $icsDates)
  537. {
  538. if ($key != 0 && $subKey == 0) {
  539. $icsDates [$key] ["BEGIN"] = $subValue;
  540. } else {
  541. $subValueArr = explode(":", $subValue, 2);
  542. if (isset ($subValueArr [1])) {
  543. $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
  544. }
  545. }
  546. return $icsDates;
  547. }
  548. function file_get_contents_curl($url)
  549. {
  550. $ch = curl_init();
  551. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  552. curl_setopt($ch, CURLOPT_HEADER, 0);
  553. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  554. curl_setopt($ch, CURLOPT_URL, $url);
  555. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  556. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  557. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  558. $data = curl_exec($ch);
  559. curl_close($ch);
  560. return $data;
  561. }
  562. function getExtension($string)
  563. {
  564. return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string);
  565. }
  566. function safe_json_encode($value, $options = 0, $depth = 512)
  567. {
  568. $encoded = json_encode($value, $options, $depth);
  569. if ($encoded === false && $value && json_last_error() == JSON_ERROR_UTF8) {
  570. $encoded = json_encode(utf8ize($value), $options, $depth);
  571. }
  572. return $encoded;
  573. }
  574. function utf8ize($mixed)
  575. {
  576. if (is_array($mixed)) {
  577. foreach ($mixed as $key => $value) {
  578. $mixed[$key] = utf8ize($value);
  579. }
  580. } elseif (is_string($mixed)) {
  581. return mb_convert_encoding($mixed, "UTF-8", "UTF-8");
  582. }
  583. return $mixed;
  584. }
  585. function gen_uuid()
  586. {
  587. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  588. // 32 bits for "time_low"
  589. mt_rand(0, 0xffff), mt_rand(0, 0xffff),
  590. // 16 bits for "time_mid"
  591. mt_rand(0, 0xffff),
  592. // 16 bits for "time_hi_and_version",
  593. // four most significant bits holds version number 4
  594. mt_rand(0, 0x0fff) | 0x4000,
  595. // 16 bits, 8 bits for "clk_seq_hi_res",
  596. // 8 bits for "clk_seq_low",
  597. // two most significant bits holds zero and one for variant DCE1.1
  598. mt_rand(0, 0x3fff) | 0x8000,
  599. // 48 bits for "node"
  600. mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
  601. );
  602. }
  603. function dbExtension($string)
  604. {
  605. return (substr($string, -3) == '.db') ? $string : $string . '.db';
  606. }
  607. function localIPRanges()
  608. {
  609. $mainArray = array(
  610. array(
  611. 'from' => '10.0.0.0',
  612. 'to' => '10.255.255.255'
  613. ),
  614. array(
  615. 'from' => '172.16.0.0',
  616. 'to' => '172.31.255.255'
  617. ),
  618. array(
  619. 'from' => '192.168.0.0',
  620. 'to' => '192.168.255.255'
  621. ),
  622. array(
  623. 'from' => '127.0.0.1',
  624. 'to' => '127.255.255.255'
  625. ),
  626. );
  627. $override = false;
  628. if ($GLOBALS['localIPFrom']) {
  629. $from = trim($GLOBALS['localIPFrom']);
  630. $override = true;
  631. }
  632. if ($GLOBALS['localIPTo']) {
  633. $to = trim($GLOBALS['localIPTo']);
  634. }
  635. if ($override) {
  636. $newArray = array(
  637. 'from' => $from,
  638. 'to' => (isset($to)) ? $to : $from
  639. );
  640. array_push($mainArray, $newArray);
  641. }
  642. return $mainArray;
  643. }
  644. function isLocal($checkIP = null)
  645. {
  646. $isLocal = false;
  647. $userIP = ($checkIP) ? ip2long($checkIP) : ip2long(userIP());
  648. $range = localIPRanges();
  649. foreach ($range as $ip) {
  650. $low = ip2long($ip['from']);
  651. $high = ip2long($ip['to']);
  652. if ($userIP <= $high && $low <= $userIP) {
  653. $isLocal = true;
  654. }
  655. }
  656. return $isLocal;
  657. }
  658. function checkOverrideURL($url, $override)
  659. {
  660. if (strpos($override, $url) !== false) {
  661. return $override;
  662. } else {
  663. return $url . $override;
  664. }
  665. }
  666. function clearPOSTPassword($array)
  667. {
  668. if (isset($array['data'])) {
  669. foreach ($array['data'] as $k => $v) {
  670. // clear password from array
  671. if ($k == 'password') {
  672. $array['data'][$k] = '*******';
  673. }
  674. }
  675. }
  676. if (isset($array['data']['data'])) {
  677. foreach ($array['data']['data'] as $k => $v) {
  678. // clear password from array
  679. if ($k == 'password') {
  680. $array['data']['data'][$k] = '*******';
  681. }
  682. }
  683. }
  684. return $array;
  685. }
  686. function timeExecution($previous = null)
  687. {
  688. if (!$previous) {
  689. return microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
  690. } else {
  691. return (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) - $previous;
  692. }
  693. }
  694. function formatSeconds($seconds)
  695. {
  696. $hours = 0;
  697. $milliseconds = str_replace("0.", '', $seconds - floor($seconds));
  698. if ($seconds > 3600) {
  699. $hours = floor($seconds / 3600);
  700. }
  701. $seconds = $seconds % 3600;
  702. $time = str_pad($hours, 2, '0', STR_PAD_LEFT)
  703. . gmdate(':i:s', $seconds)
  704. . ($milliseconds ? '.' . $milliseconds : '');
  705. $parts = explode(':', $time);
  706. $timeExtra = explode('.', $parts[2]);
  707. if ($parts[0] !== '00') { // hours
  708. return $time;
  709. } elseif ($parts[1] !== '00') { // mins
  710. return $parts[1] . 'min(s) ' . $timeExtra[0] . 's';
  711. } elseif ($timeExtra[0] !== '00') { // secs
  712. return substr($parts[2], 0, 5) . 's | ' . substr($parts[2], 0, 7) * 1000 . 'ms';
  713. } else {
  714. return substr($parts[2], 0, 7) * 1000 . 'ms';
  715. }
  716. //return $timeExtra[0] . 's ' . (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  717. //return (number_format(('0.' . substr($timeExtra[1], 0, 4)), 4, '.', '') * 1000) . 'ms';
  718. }