normal-functions.php 14 KB

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