normal-functions.php 15 KB

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