normal-functions.php 23 KB

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