normal-functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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', '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');
  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. }
  155. function userIP()
  156. {
  157. if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  158. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  159. } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  160. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  161. } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) {
  162. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  163. } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
  164. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  165. } elseif (isset($_SERVER['HTTP_FORWARDED'])) {
  166. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  167. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  168. $ipaddress = $_SERVER['REMOTE_ADDR'];
  169. } else {
  170. $ipaddress = 'UNKNOWN';
  171. }
  172. if (strpos($ipaddress, ',') !== false) {
  173. list($first, $last) = explode(",", $ipaddress);
  174. unset($last);
  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. if (!file_exists($file)) {
  197. file_put_contents($file, fopen($url, 'r'));
  198. } elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  199. file_put_contents($file, fopen($url, 'r'));
  200. }
  201. return $file;
  202. }
  203. function curl($curl, $url, $headers = array(), $data = array())
  204. {
  205. // Initiate cURL
  206. $curlReq = curl_init($url);
  207. if (in_array(trim(strtoupper($curl)), ["GET", "POST", "PUT", "DELETE"])) {
  208. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  209. } else {
  210. return null;
  211. }
  212. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  213. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  214. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  215. if (localURL($url)) {
  216. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  217. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  218. }
  219. // Format Headers
  220. $cHeaders = array();
  221. foreach ($headers as $k => $v) {
  222. $cHeaders[] = $k . ': ' . $v;
  223. }
  224. if (count($cHeaders)) {
  225. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  226. }
  227. // Format Data
  228. switch (isset($headers['Content-Type']) ? $headers['Content-Type'] : '') {
  229. case 'application/json':
  230. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  231. break;
  232. case 'application/x-www-form-urlencoded':
  233. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  234. break;
  235. default:
  236. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  237. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  238. }
  239. // Execute
  240. $result = curl_exec($curlReq);
  241. $httpcode = curl_getinfo($curlReq);
  242. // Close
  243. curl_close($curlReq);
  244. // Return
  245. return array('content' => $result, 'http_code' => $httpcode);
  246. }
  247. function getHeaders($url)
  248. {
  249. $ch = curl_init($url);
  250. curl_setopt($ch, CURLOPT_NOBODY, true);
  251. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  252. curl_setopt($ch, CURLOPT_HEADER, false);
  253. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  254. curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
  255. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  256. if (localURL($url)) {
  257. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  258. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  259. }
  260. curl_exec($ch);
  261. $headers = curl_getinfo($ch);
  262. curl_close($ch);
  263. return $headers;
  264. }
  265. function download($url, $path)
  266. {
  267. ini_set('max_execution_time', 0);
  268. set_time_limit(0);
  269. $ch = curl_init($url);
  270. curl_setopt($ch, CURLOPT_HEADER, 1);
  271. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  272. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  273. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  274. curl_setopt($ch, CURLOPT_CAINFO, getCert());
  275. if (localURL($url)) {
  276. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  277. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  278. }
  279. $raw_file_data = curl_exec($ch);
  280. curl_close($ch);
  281. file_put_contents($path, $raw_file_data);
  282. return (filesize($path) > 0) ? true : false;
  283. }
  284. function localURL($url)
  285. {
  286. if (strpos($url, 'https') !== false) {
  287. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  288. $result = (!empty($result) ? true : false);
  289. return $result;
  290. }
  291. return false;
  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 = $_SERVER['HTTP_HOST'];
  360. }
  361. } else {
  362. $domain = $_SERVER['HTTP_HOST'];
  363. }
  364. }
  365. $url = $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  366. if (strpos($url, '/api') !== false) {
  367. $url = explode('/api', $url);
  368. return $url[0] . '/';
  369. } else {
  370. return $url;
  371. }
  372. }
  373. function get_browser_name()
  374. {
  375. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  376. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) {
  377. return 'Opera';
  378. } elseif (strpos($user_agent, 'Edge')) {
  379. return 'Edge';
  380. } elseif (strpos($user_agent, 'Chrome')) {
  381. return 'Chrome';
  382. } elseif (strpos($user_agent, 'Safari')) {
  383. return 'Safari';
  384. } elseif (strpos($user_agent, 'Firefox')) {
  385. return 'Firefox';
  386. } elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) {
  387. return 'Internet Explorer';
  388. }
  389. return 'Other';
  390. }
  391. function getServer($over = false)
  392. {
  393. if ($over) {
  394. if ($GLOBALS['PHPMAILER-domain'] !== '') {
  395. return $GLOBALS['PHPMAILER-domain'];
  396. }
  397. }
  398. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  399. }