These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Check $_GET |
||
| 4 | * |
||
| 5 | * @example if(chk_get("s") == "a") instead of if(isset($_GET["s"]) && $_GET["s"] == "a") |
||
| 6 | * |
||
| 7 | * @param string $key Get-key... |
||
| 8 | * @return bool |
||
| 9 | */ |
||
| 10 | function chk_get($key) { |
||
| 11 | if (!isset($_GET[$key])) { |
||
| 12 | return false; |
||
| 13 | } |
||
| 14 | return $_GET[$key]; |
||
| 15 | } |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Check $_POST |
||
| 19 | * |
||
| 20 | * @example if(chk_post("s") == "a") instead of if(isset($_POST["s"]) && $_POST["s"] == "a") |
||
| 21 | */ |
||
| 22 | function chk_post($key) { |
||
|
0 ignored issues
–
show
|
|||
| 23 | if (!isset($_POST[$key])) { |
||
| 24 | return false; |
||
| 25 | } |
||
| 26 | return $_POST[$key]; |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Check multiple $_GET-keys |
||
| 31 | * |
||
| 32 | * @example if(chk_get_all(array("a","b"))) instead of if(!empty($_GET["a"]) && !empty($_GET["b"])) |
||
| 33 | */ |
||
| 34 | function chk_get_all($keys) { |
||
| 35 | $s = true; |
||
| 36 | |||
| 37 | foreach($keys AS $key) { |
||
| 38 | |||
| 39 | if (empty($_GET[$key])) { |
||
| 40 | $s = false; |
||
| 41 | } |
||
| 42 | } |
||
| 43 | return $s; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Check multiple $_POST-keys |
||
| 48 | * |
||
| 49 | * @example if(chk_post_all(array("a","b"))) instead of if(!empty($_POST["a"]) && !empty($_POST["b"])) |
||
| 50 | */ |
||
| 51 | function chk_post_all($keys) { |
||
| 52 | $s = true; |
||
| 53 | |||
| 54 | foreach($keys AS $key) { |
||
| 55 | |||
| 56 | if (empty($_POST[$key])) { |
||
| 57 | $s = false; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | return $s; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Convert timestamp to HTTP-date (RFC2616) |
||
| 65 | * |
||
| 66 | * For use in "Last-Modified" headers. |
||
| 67 | */ |
||
| 68 | function timestamp_to_http_date($timestamp) { |
||
| 69 | if($timestamp == NULL) { return NULL; } |
||
| 70 | return gmdate("D, d M Y H:i:s T", strtotime($timestamp)); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get client ip-address |
||
| 75 | * |
||
| 76 | * @return string User ip-address |
||
| 77 | */ |
||
| 78 | function get_client_ip_address() { |
||
| 79 | |||
| 80 | if (getenv('HTTP_CLIENT_IP')) |
||
| 81 | return getenv('HTTP_CLIENT_IP'); |
||
| 82 | else if(getenv('HTTP_X_FORWARDED_FOR')) |
||
| 83 | return getenv('HTTP_X_FORWARDED_FOR'); |
||
| 84 | else if(getenv('HTTP_X_FORWARDED')) |
||
| 85 | return getenv('HTTP_X_FORWARDED'); |
||
| 86 | else if(getenv('HTTP_FORWARDED_FOR')) |
||
| 87 | return getenv('HTTP_FORWARDED_FOR'); |
||
| 88 | else if(getenv('HTTP_FORWARDED')) |
||
| 89 | return getenv('HTTP_FORWARDED'); |
||
| 90 | else if(getenv('REMOTE_ADDR')) |
||
| 91 | return getenv('REMOTE_ADDR'); |
||
| 92 | else |
||
| 93 | return '127.0.0.1'; // Unknown IP |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * parse url, try to correct errors and return valid url + display-url. |
||
| 98 | * |
||
| 99 | * @example http:/wwww.example.com/lorum.html => http://www.example.com/lorum.html |
||
| 100 | * @example gopher:/ww.example.com => gopher://www.example.com |
||
| 101 | * @example http:/www3.example.com/?q=asd&f=#asd =>http://www3.example.com/?q=asd&f=#asd |
||
| 102 | * @example asd://.example.com/folder/folder/ =>http://example.com/folder/folder/ |
||
| 103 | * @example .example.com/ => http://example.com/ |
||
| 104 | * @example example.com =>http://example.com |
||
| 105 | * @example subdomain.example.com => http://subdomain.example.com |
||
| 106 | * |
||
| 107 | * @param string $url Any somewhat valid url. |
||
| 108 | * @return string[] "url" contains an auto-corrected url. "url_display" host.tld or subdomain.host.tld |
||
| 109 | */ |
||
| 110 | function url_parser($url) { |
||
| 111 | |||
| 112 | // multiple /// messes up parse_url, replace 3 or more with 2 |
||
| 113 | $url = preg_replace('/(\/{2,})/','//',$url); |
||
| 114 | |||
| 115 | $parse_url = parse_url($url); |
||
| 116 | |||
| 117 | if(empty($parse_url["scheme"])) { |
||
| 118 | $parse_url["scheme"] = "http"; |
||
| 119 | } |
||
| 120 | if(empty($parse_url["host"]) && !empty($parse_url["path"])) { |
||
| 121 | // Strip slash from the beginning of path |
||
| 122 | $parse_url["host"] = ltrim($parse_url["path"], '\/'); |
||
| 123 | $parse_url["path"] = ""; |
||
| 124 | } |
||
| 125 | |||
| 126 | $url_array = array("url" => "", "url_display" => ""); |
||
| 127 | |||
| 128 | // Check if scheme is correct |
||
| 129 | if(!in_array($parse_url["scheme"], array("http", "https", "gopher"))) { |
||
| 130 | $url_array["url"] .= 'http'.'://'; |
||
| 131 | } else { |
||
| 132 | $url_array["url"] .= $parse_url["scheme"].'://'; |
||
| 133 | } |
||
| 134 | |||
| 135 | // Check if the right amount of "www" is set. |
||
| 136 | $explode_host = explode(".", $parse_url["host"]); |
||
| 137 | |||
| 138 | // Remove empty entries |
||
| 139 | $explode_host = array_filter($explode_host); |
||
| 140 | // And reassign indexes |
||
| 141 | $explode_host = array_values($explode_host); |
||
| 142 | |||
| 143 | // Contains subdomain |
||
| 144 | if(count($explode_host) > 2) { |
||
| 145 | // Check if subdomain only contains the letter w(then not any other subdomain). |
||
| 146 | if(substr_count($explode_host[0], 'w') == strlen($explode_host[0])) { |
||
| 147 | // Replace with "www" to avoid "ww" or "wwww", etc. |
||
| 148 | $explode_host[0] = "www"; |
||
| 149 | |||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $url_array["url"] .= implode(".",$explode_host); |
||
| 154 | $url_array["url_display"] = trim(implode(".",$explode_host), '\/'); // Removes trailing slash |
||
| 155 | |||
| 156 | if(!empty($parse_url["port"])) { |
||
| 157 | $url_array["url"] .= ":".$parse_url["port"]; |
||
| 158 | } |
||
| 159 | if(!empty($parse_url["path"])) { |
||
| 160 | $url_array["url"] .= $parse_url["path"]; |
||
| 161 | } |
||
| 162 | if(!empty($parse_url["query"])) { |
||
| 163 | $url_array["url"] .= '?'.$parse_url["query"]; |
||
| 164 | } |
||
| 165 | if(!empty($parse_url["fragment"])) { |
||
| 166 | $url_array["url"] .= '#'.$parse_url["fragment"]; |
||
| 167 | } |
||
| 168 | |||
| 169 | |||
| 170 | return $url_array; |
||
| 171 | } |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.