normal-functions.php 11 KB

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