normal-functions.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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($len){
  93. $string = "";
  94. $max = strlen($this->ascii)-1;
  95. while($len-->0) { $string .= $this->ascii[mt_rand(0, $max)]; }
  96. return $string;
  97. }
  98. function encrypt($password, $key = null) {
  99. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  100. return openssl_encrypt($password, 'AES-256-CBC', $key, 0, fillString($key,16));
  101. }
  102. function decrypt($password, $key = null) {
  103. $key = (isset($GLOBALS['organizrHash'])) ? $GLOBALS['organizrHash'] : $key;
  104. return openssl_decrypt($password, 'AES-256-CBC', $key, 0, fillString($key,16));
  105. }
  106. function fillString($string, $length){
  107. $filler = '0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*';
  108. if(strlen($string) < $length){
  109. $diff = $length - strlen($string);
  110. $filler = substr($filler,0,$diff);
  111. return $string.$filler;
  112. }elseif(strlen($string) > $length){
  113. return substr($string,0,$length);
  114. }else{
  115. return $string;
  116. }
  117. return $diff;
  118. }
  119. function userIP() {
  120. if (isset($_SERVER['HTTP_CLIENT_IP']))
  121. $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
  122. else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  123. $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
  124. else if(isset($_SERVER['HTTP_X_FORWARDED']))
  125. $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
  126. else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
  127. $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
  128. else if(isset($_SERVER['HTTP_FORWARDED']))
  129. $ipaddress = $_SERVER['HTTP_FORWARDED'];
  130. else if(isset($_SERVER['REMOTE_ADDR']))
  131. $ipaddress = $_SERVER['REMOTE_ADDR'];
  132. else
  133. $ipaddress = 'UNKNOWN';
  134. if (strpos($ipaddress, ',') !== false) {
  135. list($first, $last) = explode(",", $ipaddress);
  136. return $first;
  137. }else{
  138. return $ipaddress;
  139. }
  140. }
  141. function arrayIP($string){
  142. if (strpos($string, ',') !== false) {
  143. $result = explode(",", $string);
  144. }else{
  145. $result = array($string);
  146. }
  147. foreach($result as &$ip){
  148. $ip = is_numeric(substr($ip, 0, 1)) ? $ip : gethostbyname($ip);
  149. }
  150. return $result;
  151. }
  152. function getCert(){
  153. $url = 'http://curl.haxx.se/ca/cacert.pem';
  154. $file = __DIR__.DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR.'cacert.pem';
  155. $directory = __DIR__.DIRECTORY_SEPARATOR.'cert'.DIRECTORY_SEPARATOR;
  156. if(!file_exists($file)){
  157. file_put_contents( $file, fopen($url, 'r'));
  158. }elseif (file_exists($file) && time() - 2592000 > filemtime($file)) {
  159. file_put_contents( $file, fopen($url, 'r'));
  160. }
  161. return $file;
  162. }
  163. function curl($curl, $url, $headers=array(), $data=array()){
  164. // Initiate cURL
  165. $curlReq = curl_init($url);
  166. if (in_array(trim(strtoupper($curl)), ["GET","POST","PUT","DELETE"])) {
  167. curl_setopt($curlReq, CURLOPT_CUSTOMREQUEST, trim(strtoupper($curl)));
  168. } else {
  169. return null;
  170. }
  171. curl_setopt($curlReq, CURLOPT_RETURNTRANSFER, true);
  172. curl_setopt($curlReq, CURLOPT_CAINFO, getCert());
  173. curl_setopt($curlReq, CURLOPT_CONNECTTIMEOUT, 5);
  174. if(localURL($url)){
  175. curl_setopt($curlReq, CURLOPT_SSL_VERIFYHOST, 0);
  176. curl_setopt($curlReq, CURLOPT_SSL_VERIFYPEER, 0);
  177. }
  178. // Format Headers
  179. $cHeaders = array();
  180. foreach ($headers as $k => $v) {
  181. $cHeaders[] = $k.': '.$v;
  182. }
  183. if (count($cHeaders)) {
  184. curl_setopt($curlReq, CURLOPT_HTTPHEADER, $cHeaders);
  185. }
  186. // Format Data
  187. switch (isset($headers['Content-Type'])?$headers['Content-Type']:'') {
  188. case 'application/json':
  189. curl_setopt($curlReq, CURLOPT_POSTFIELDS, json_encode($data));
  190. break;
  191. case 'application/x-www-form-urlencoded':
  192. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  193. break;
  194. default:
  195. $headers['Content-Type'] = 'application/x-www-form-urlencoded';
  196. curl_setopt($curlReq, CURLOPT_POSTFIELDS, http_build_query($data));
  197. }
  198. // Execute
  199. $result = curl_exec($curlReq);
  200. $httpcode = curl_getinfo($curlReq);
  201. // Close
  202. curl_close($curlReq);
  203. // Return
  204. return array('content'=>$result, 'http_code'=>$httpcode);
  205. }
  206. function getHeaders($url){
  207. $ch = curl_init($url);
  208. curl_setopt( $ch, CURLOPT_NOBODY, true );
  209. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
  210. curl_setopt( $ch, CURLOPT_HEADER, false );
  211. curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  212. curl_setopt( $ch, CURLOPT_MAXREDIRS, 3 );
  213. curl_setopt( $ch, CURLOPT_CAINFO, getCert());
  214. if(localURL($url)){
  215. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  216. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  217. }
  218. curl_exec( $ch );
  219. $headers = curl_getinfo( $ch );
  220. curl_close( $ch );
  221. return $headers;
  222. }
  223. function download($url, $path){
  224. ini_set('max_execution_time',0);
  225. set_time_limit(0);
  226. $ch = curl_init($url);
  227. curl_setopt($ch, CURLOPT_HEADER, 1);
  228. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  229. curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
  230. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  231. curl_setopt( $ch, CURLOPT_CAINFO, getCert());
  232. if(localURL($url)){
  233. curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
  234. curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
  235. }
  236. $raw_file_data = curl_exec($ch);
  237. curl_close($ch);
  238. file_put_contents($path, $raw_file_data);
  239. return (filesize($path) > 0)? true : false;
  240. }
  241. function localURL($url){
  242. if (strpos($url, 'https') !== false) {
  243. preg_match("/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/", $url, $result);
  244. $result = (!empty($result) ? true : false);
  245. return $result;
  246. }
  247. }