normal-functions.php 23 KB

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