normal-functions.php 23 KB

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