normal-functions.php 14 KB

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