normal-functions.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. if(empty($password)){ return ''; }
  115. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  116. return openssl_decrypt($password, 'AES-256-CBC', $key, 0, fillString($key,16));
  117. }
  118. function fillString($string, $length){
  119. $filler = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*';
  120. if(strlen($string) < $length){
  121. $diff = $length - strlen($string);
  122. $filler = substr($filler,0,$diff);
  123. return $string.$filler;
  124. }elseif(strlen($string) > $length){
  125. return substr($string,0,$length);
  126. }else{
  127. return $string;
  128. }
  129. return $diff;
  130. }
  131. function userIP() {
  132. if (isset($_SERVER['HTTP_CLIENT_IP']))
  133. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  134. else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  135. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  136. else if(isset($_SERVER['HTTP_X_FORWARDED']))
  137. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  138. else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
  139. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  140. else if(isset($_SERVER['HTTP_FORWARDED']))
  141. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  142. else if(isset($_SERVER['REMOTE_ADDR']))
  143. $ipaddress = $_SERVER['REMOTE_ADDR'];
  144. else
  145. $ipaddress = 'UNKNOWN';
  146. if (strpos($ipaddress, ',') !== false) {
  147. list($first, $last) = explode(",", $ipaddress);
  148. return $first;
  149. }else{
  150. return $ipaddress;
  151. }
  152. }
  153. function arrayIP($string){
  154. if (strpos($string, ',') !== false) {
  155. $result = explode(",", $string);
  156. }else{
  157. $result = array($string);
  158. }
  159. foreach($result as &$ip){
  160. $ip = is_numeric(substr($ip, 0, 1)) ? $ip : gethostbyname($ip);
  161. }
  162. return $result;
  163. }
  164. function getCert(){
  165. $url = 'http://curl.haxx.se/ca/cacert.pem';
  166. $file = __DIR__.DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'cacert.pem';
  167. $directory = __DIR__.DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR;
  168. if(!file_exists($file)){
  169. file_put_contents( $file, fopen($url, 'r'));
  170. }elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  171. file_put_contents( $file, fopen($url, 'r'));
  172. }
  173. return $file;
  174. }
  175. function curl($curl, $url, $headers=array(), $data=array()){
  176. // Initiate cURL
  177. $curlReq = curl_init($url);
  178. if (in_array(trim(strtoupper($curl)), ["GET","POST","PUT","DELETE"])) {
  179. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  180. } else {
  181. return null;
  182. }
  183. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  184. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  185. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  186. if(localURL($url)){
  187. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  188. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  189. }
  190. // Format Headers
  191. $cHeaders = array();
  192. foreach ($headers as $k => $v) {
  193. $cHeaders[] = $k.': '.$v;
  194. }
  195. if (count($cHeaders)) {
  196. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  197. }
  198. // Format Data
  199. switch (isset($headers['Content-Type'])?$headers['Content-Type']:'') {
  200. case 'application/json':
  201. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  202. break;
  203. case 'application/x-www-form-urlencoded':
  204. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  205. break;
  206. default:
  207. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  208. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  209. }
  210. // Execute
  211. $result = curl_exec($curlReq);
  212. $httpcode = curl_getinfo($curlReq);
  213. // Close
  214. curl_close($curlReq);
  215. // Return
  216. return array('content'=>$result, 'http_code'=>$httpcode);
  217. }
  218. function getHeaders($url){
  219. $ch = curl_init($url);
  220. curl_setopt( $ch, CURLOPT_NOBODY, true );
  221. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
  222. curl_setopt( $ch, CURLOPT_HEADER, false );
  223. curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  224. curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
  225. curl_setopt( $ch, CURLOPT_CAINFO, getCert());
  226. if(localURL($url)){
  227. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  228. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  229. }
  230. curl_exec( $ch );
  231. $headers = curl_getinfo( $ch );
  232. curl_close( $ch );
  233. return $headers;
  234. }
  235. function download($url, $path){
  236. ini_set('max_execution_time',0);
  237. set_time_limit(0);
  238. $ch = curl_init($url);
  239. curl_setopt($ch, CURLOPT_HEADER, 1);
  240. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  241. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  242. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  243. curl_setopt( $ch, CURLOPT_CAINFO, getCert());
  244. if(localURL($url)){
  245. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  246. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  247. }
  248. $raw_file_data = curl_exec($ch);
  249. curl_close($ch);
  250. file_put_contents($path, $raw_file_data);
  251. return (filesize($path) > 0)? true : false;
  252. }
  253. function localURL($url){
  254. if (strpos($url, 'https') !== false) {
  255. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  256. $result = (!empty($result) ? true : false);
  257. return $result;
  258. }
  259. }
  260. function array_filter_key(array $array, $callback){
  261. $matchedKeys = array_filter(array_keys($array), $callback);
  262. return array_intersect_key($array, array_flip($matchedKeys));
  263. }
  264. // Qualify URL
  265. function qualifyURL($url, $return=false) {
  266. //local address?
  267. if(substr($url, 0,1) == "/"){
  268. if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  269. $protocol = "https://";
  270. } else {
  271. $protocol = "http://";
  272. }
  273. $url = $protocol.getServer().$url;
  274. }
  275. // Get Digest
  276. $digest = parse_url($url);
  277. // http/https
  278. if (!isset($digest['scheme'])) {
  279. if (isset($digest['port']) && in_array($digest['port'], array(80,8080,8096,32400,7878,8989,8182,8081,6789))) {
  280. $scheme = 'http';
  281. } else {
  282. $scheme = 'https';
  283. }
  284. } else {
  285. $scheme = $digest['scheme'];
  286. }
  287. // Host
  288. $host = (isset($digest['host'])?$digest['host']:'');
  289. // Port
  290. $port = (isset($digest['port'])?':'.$digest['port']:'');
  291. // Path
  292. $path = (isset($digest['path']) && $digest['path'] !== '/'?$digest['path']:'');
  293. // Output
  294. $array = array(
  295. 'scheme' => $scheme,
  296. 'host' => $host,
  297. 'port' => $port,
  298. 'path' => $path
  299. );
  300. return ($return) ? $array : $scheme.'://'.$host.$port.$path;
  301. }
  302. function getServerPath($over=false) {
  303. if($over){
  304. if($GLOBALS['PHPMAILER-domain'] !== ''){
  305. return $GLOBALS['PHPMAILER-domain'];
  306. }
  307. }
  308. if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == "https"){
  309. $protocol = "https://";
  310. }elseif (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
  311. $protocol = "https://";
  312. } else {
  313. $protocol = "http://";
  314. }
  315. $domain = '';
  316. if (isset($_SERVER['SERVER_NAME']) && strpos($_SERVER['SERVER_NAME'], '.') !== false){
  317. $domain = $_SERVER['SERVER_NAME'];
  318. }elseif(isset($_SERVER['HTTP_HOST'])){
  319. if (strpos($_SERVER['HTTP_HOST'], ':') !== false) {
  320. $domain = explode(':', $_SERVER['HTTP_HOST'])[0];
  321. $port = explode(':', $_SERVER['HTTP_HOST'])[1];
  322. if ($port == "80" || $port == "443"){
  323. $domain = $domain;
  324. }else{
  325. $domain = $_SERVER['HTTP_HOST'];
  326. }
  327. }else{
  328. $domain = $_SERVER['HTTP_HOST'];
  329. }
  330. }
  331. return $protocol . $domain . str_replace("\\", "/", dirname($_SERVER['REQUEST_URI']));
  332. }
  333. function get_browser_name() {
  334. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  335. if (strpos($user_agent, 'Opera') || strpos($user_agent, 'OPR/')) return 'Opera';
  336. elseif (strpos($user_agent, 'Edge')) return 'Edge';
  337. elseif (strpos($user_agent, 'Chrome')) return 'Chrome';
  338. elseif (strpos($user_agent, 'Safari')) return 'Safari';
  339. elseif (strpos($user_agent, 'Firefox')) return 'Firefox';
  340. elseif (strpos($user_agent, 'MSIE') || strpos($user_agent, 'Trident/7')) return 'Internet Explorer';
  341. return 'Other';
  342. }
  343. function getServer(){
  344. return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : $_SERVER["SERVER_NAME"];
  345. }