normal-functions.php 14 KB

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