normal-functions.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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') {
  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 getOS()
  125. {
  126. if (PHP_SHLIB_SUFFIX == "dll") {
  127. return "win";
  128. } else {
  129. return "*nix";
  130. }
  131. }
  132. if (!function_exists('getallheaders')) {
  133. function getallheaders()
  134. {
  135. $headers = array();
  136. foreach ($_SERVER as $name => $value) {
  137. if (substr($name, 0, 5) == 'HTTP_') {
  138. $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
  139. }
  140. }
  141. return $headers;
  142. }
  143. }
  144. function random_ascii_string($length)
  145. {
  146. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  147. $charactersLength = strlen($characters);
  148. $randomString = '';
  149. for ($i = 0; $i < $length; $i++) {
  150. $randomString .= $characters[rand(0, $charactersLength - 1)];
  151. }
  152. return $randomString;
  153. }
  154. // Generate Random string
  155. function randString($length = 10, $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ')
  156. {
  157. $tmp = '';
  158. for ($i = 0; $i < $length; $i++) {
  159. $tmp .= substr(str_shuffle($chars), 0, 1);
  160. }
  161. return $tmp;
  162. }
  163. function encrypt($password, $key = null)
  164. {
  165. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  166. return openssl_encrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  167. }
  168. function decrypt($password, $key = null)
  169. {
  170. if (empty($password)) {
  171. return '';
  172. }
  173. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  174. return openssl_decrypt($password, 'AES-256-CBC', $key, 0, fillString($key, 16));
  175. }
  176. function fillString($string, $length)
  177. {
  178. $filler = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*';
  179. if (strlen($string) < $length) {
  180. $diff = $length - strlen($string);
  181. $filler = substr($filler, 0, $diff);
  182. return $string . $filler;
  183. } elseif (strlen($string) > $length) {
  184. return substr($string, 0, $length);
  185. } else {
  186. return $string;
  187. }
  188. }
  189. function userIP()
  190. {
  191. if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  192. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  193. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  194. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  195. } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
  196. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  197. } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  198. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  199. } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
  200. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  201. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  202. $ipaddress = $_SERVER['REMOTE_ADDR'];
  203. } else {
  204. $ipaddress = 'UNKNOWN';
  205. }
  206. if (strpos($ipaddress, ',') !== false) {
  207. list($first, $last) = explode(",", $ipaddress);
  208. unset($last);
  209. return $first;
  210. } else {
  211. return $ipaddress;
  212. }
  213. }
  214. function arrayIP($string)
  215. {
  216. if (strpos($string, ',') !== false) {
  217. $result = explode(",", $string);
  218. } else {
  219. $result = array($string);
  220. }
  221. foreach ($result as &$ip) {
  222. $ip = is_numeric(substr($ip, 0, 1)) ? $ip : gethostbyname($ip);
  223. }
  224. return $result;
  225. }
  226. function getCert()
  227. {
  228. $url = 'http://curl.haxx.se/ca/cacert.pem';
  229. $file = __DIR__ . DIRECTORY_SEPARATOR . 'cert' . DIRECTORY_SEPARATOR . 'cacert.pem';
  230. if (!file_exists($file)) {
  231. file_put_contents($file, fopen($url, 'r'));
  232. } elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  233. file_put_contents($file, fopen($url, 'r'));
  234. }
  235. return $file;
  236. }
  237. function curl($curl, $url, $headers = array(), $data = array())
  238. {
  239. // Initiate cURL
  240. $curlReq = curl_init($url);
  241. if (in_array(trim(strtoupper($curl)), ["GET", "POST", "PUT", "DELETE"])) {
  242. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  243. } else {
  244. return null;
  245. }
  246. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  247. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  248. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  249. if (localURL($url)) {
  250. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  251. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  252. }
  253. // Format Headers
  254. $cHeaders = array();
  255. foreach ($headers as $k => $v) {
  256. $cHeaders[] = $k . ': ' . $v;
  257. }
  258. if (count($cHeaders)) {
  259. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  260. }
  261. // Format Data
  262. switch (isset($headers['Content-Type']) ? $headers['Content-Type'] : '') {
  263. case 'application/json':
  264. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  265. break;
  266. case 'application/x-www-form-urlencoded':
  267. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  268. break;
  269. default:
  270. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  271. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  272. }
  273. // Execute
  274. $result = curl_exec($curlReq);
  275. $httpcode = curl_getinfo($curlReq);
  276. // Close
  277. curl_close($curlReq);
  278. // Return
  279. return array('content' => $result, 'http_code' => $httpcode);
  280. }
  281. function getHeaders($url)
  282. {
  283. $ch = curl_init($url);
  284. curl_setopt($ch, CURLOPT_NOBODY, true);
  285. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  286. curl_setopt($ch, CURLOPT_HEADER, false);
  287. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  288. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  289. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  290. if (localURL($url)) {
  291. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  292. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  293. }
  294. curl_exec($ch);
  295. $headers = curl_getinfo($ch);
  296. curl_close($ch);
  297. return $headers;
  298. }
  299. function download($url, $path)
  300. {
  301. ini_set('max_execution_time', 0);
  302. set_time_limit(0);
  303. $ch = curl_init($url);
  304. curl_setopt($ch, CURLOPT_HEADER, 1);
  305. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  306. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  307. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  308. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  309. if (localURL($url)) {
  310. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  311. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  312. }
  313. $raw_file_data = curl_exec($ch);
  314. curl_close($ch);
  315. file_put_contents($path, $raw_file_data);
  316. return (filesize($path) > 0) ? true : false;
  317. }
  318. function localURL($url)
  319. {
  320. if (strpos($url, 'https') !== false) {
  321. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  322. $result = (!empty($result) ? true : false);
  323. return $result;
  324. }
  325. return false;
  326. }
  327. function array_filter_key(array $array, $callback)
  328. {
  329. $matchedKeys = array_filter(array_keys($array), $callback);
  330. return array_intersect_key($array, array_flip($matchedKeys));
  331. }
  332. // Qualify URL
  333. function qualifyURL($url, $return = false)
  334. {
  335. //local address?
  336. if (substr($url, 0, 1) == "/") {
  337. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  338. $protocol = "https://";
  339. } else {
  340. $protocol = "http://";
  341. }
  342. $url = $protocol . getServer() . $url;
  343. }
  344. // Get Digest
  345. $digest = parse_url($url);
  346. // http/https
  347. if (!isset($digest['scheme'])) {
  348. if (isset($digest['port']) && in_array($digest['port'], array(80, 8080, 8096, 32400, 7878, 8989, 8182, 8081, 6789))) {
  349. $scheme = 'http';
  350. } else {
  351. $scheme = 'https';
  352. }
  353. } else {
  354. $scheme = $digest['scheme'];
  355. }
  356. // Host
  357. $host = (isset($digest['host']) ? $digest['host'] : '');
  358. // Port
  359. $port = (isset($digest['port']) ? ':' . $digest['port'] : '');
  360. // Path
  361. $path = (isset($digest['path']) && $digest['path'] !== '/' ? $digest['path'] : '');
  362. // Output
  363. $array = array(
  364. 'scheme' => $scheme,
  365. 'host' => $host,
  366. 'port' => $port,
  367. 'path' => $path
  368. );
  369. return ($return) ? $array : $scheme . '://' . $host . $port . $path;
  370. }
  371. function getServerPath($over = false)
  372. {
  373. if ($over) {
  374. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  375. return $GLOBALS['PHPMAILER-domain'];
  376. }
  377. }
  378. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https") {
  379. $protocol = "https://";
  380. } elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  381. $protocol = "https://";
  382. } else {
  383. $protocol = "http://";
  384. }
  385. $domain = '';
  386. if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false) {
  387. $domain = $_SERVER['SERVER_NAME'];
  388. } elseif (isset($_SERVER['HTTP_HOST'])) {
  389. if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
  390. $domain = explode(':', $_SERVER['HTTP_HOST'])[0];
  391. $port = explode(':', $_SERVER['HTTP_HOST'])[1];
  392. if ($port !== "80" && $port !== "443") {
  393. $domain = $_SERVER['HTTP_HOST'];
  394. }
  395. } else {
  396. $domain = $_SERVER['HTTP_HOST'];
  397. }
  398. }
  399. $url = $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  400. if (strpos($url, '/api') !== false) {
  401. $url = explode('/api', $url);
  402. return $url[0] . '/';
  403. } else {
  404. return $url;
  405. }
  406. }
  407. function get_browser_name()
  408. {
  409. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  410. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
  411. return 'Opera';
  412. } elseif (strpos($user_agent, 'Edge')) {
  413. return 'Edge';
  414. } elseif (strpos($user_agent, 'Chrome')) {
  415. return 'Chrome';
  416. } elseif (strpos($user_agent, 'Safari')) {
  417. return 'Safari';
  418. } elseif (strpos($user_agent, 'Firefox')) {
  419. return 'Firefox';
  420. } elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
  421. return 'Internet Explorer';
  422. }
  423. return 'Other';
  424. }
  425. function getServer($over = false)
  426. {
  427. if ($over) {
  428. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  429. return $GLOBALS['PHPMAILER-domain'];
  430. }
  431. }
  432. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  433. }
  434. /* Function is to get all the contents from ics and explode all the datas according to the events and its sections */
  435. function getIcsEventsAsArray($file)
  436. {
  437. $icalString = file_get_contents_curl($file);
  438. $icsDates = array();
  439. /* Explode the ICs Data to get datas as array according to string ‘BEGIN:’ */
  440. $icsData = explode("BEGIN:", $icalString);
  441. /* Iterating the icsData value to make all the start end dates as sub array */
  442. foreach ($icsData as $key => $value) {
  443. $icsDatesMeta [$key] = explode("\n", $value);
  444. }
  445. /* Itearting the Ics Meta Value */
  446. foreach ($icsDatesMeta as $key => $value) {
  447. foreach ($value as $subKey => $subValue) {
  448. /* to get ics events in proper order */
  449. $icsDates = getICSDates($key, $subKey, $subValue, $icsDates);
  450. }
  451. }
  452. return $icsDates;
  453. }
  454. /* funcion is to avaid the elements wich is not having the proper start, end and summary informations */
  455. function getICSDates($key, $subKey, $subValue, $icsDates)
  456. {
  457. if ($key != 0 && $subKey == 0) {
  458. $icsDates [$key] ["BEGIN"] = $subValue;
  459. } else {
  460. $subValueArr = explode(":", $subValue, 2);
  461. if (isset ($subValueArr [1])) {
  462. $icsDates [$key] [$subValueArr [0]] = $subValueArr [1];
  463. }
  464. }
  465. return $icsDates;
  466. }
  467. function file_get_contents_curl($url)
  468. {
  469. $ch = curl_init();
  470. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  471. curl_setopt($ch, CURLOPT_HEADER, 0);
  472. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  473. curl_setopt($ch, CURLOPT_URL, $url);
  474. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  475. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  476. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  477. $data = curl_exec($ch);
  478. curl_close($ch);
  479. return $data;
  480. }
  481. function getExtension($string)
  482. {
  483. return preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $string);
  484. }