class-thelib-array.php
5 years ago
class-thelib-core.php
5 years ago
class-thelib-debug.php
5 years ago
class-thelib-html.php
5 years ago
class-thelib-net.php
5 years ago
class-thelib-session.php
5 years ago
class-thelib-ui.php
5 years ago
class-thelib-updates.php
5 years ago
class-thelib.php
5 years ago
class-thelib-net.php
210 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The Net component. |
| 4 | * Access via function `lib3()->net`. |
| 5 | * |
| 6 | * @since 2.0.0 |
| 7 | */ |
| 8 | class TheLib_Net extends TheLib { |
| 9 | |
| 10 | /** |
| 11 | * Returns the current URL. |
| 12 | * This URL is not guaranteed to look exactly same as the user sees it. |
| 13 | * E.g. Hashtags are missing ("index.php#section-a") |
| 14 | * |
| 15 | * @since 1.0.7 |
| 16 | * @api |
| 17 | * |
| 18 | * @param string $protocol Optional. Define URL protocol ('http', 'https') |
| 19 | * @return string Full URL to current page. |
| 20 | */ |
| 21 | public function current_url( $protocol = null ) { |
| 22 | static $Url = array(); |
| 23 | |
| 24 | if ( null !== $protocol ) { |
| 25 | // Remove the "://" part, if it was provided |
| 26 | $protocol = array_shift( explode( ':', $protocol ) ); |
| 27 | } |
| 28 | |
| 29 | if ( ! isset( $Url[$protocol] ) ) { |
| 30 | if ( null === $protocol ) { |
| 31 | $cur_url = 'http'; |
| 32 | |
| 33 | if ( isset( $_SERVER['HTTPS'] ) |
| 34 | && 'on' == strtolower( $_SERVER['HTTPS'] ) |
| 35 | ) { |
| 36 | $cur_url .= 's'; |
| 37 | } |
| 38 | } else { |
| 39 | $cur_url = $protocol; |
| 40 | } |
| 41 | |
| 42 | $is_ssl = (false !== strpos( $cur_url, 'https' )); |
| 43 | $cur_url .= '://'; |
| 44 | |
| 45 | if ( isset( $_SERVER['SERVER_NAME'] ) ) { |
| 46 | $cur_url .= $_SERVER['SERVER_NAME']; |
| 47 | } elseif ( defined( 'WP_TESTS_DOMAIN' ) ) { |
| 48 | $cur_url .= WP_TESTS_DOMAIN; |
| 49 | } |
| 50 | |
| 51 | if ( ! isset( $_SERVER['SERVER_PORT'] ) ) { |
| 52 | if ( $is_ssl ) { $_SERVER['SERVER_PORT'] = '443'; } |
| 53 | else { $_SERVER['SERVER_PORT'] = '80'; } |
| 54 | } |
| 55 | |
| 56 | if ( ( ! $is_ssl && '80' != $_SERVER['SERVER_PORT'] ) || |
| 57 | ( $is_ssl && !in_array( $_SERVER['SERVER_PORT'], array( '80', '443' ) ) ) |
| 58 | ) { |
| 59 | $cur_url .= ':' . $_SERVER['SERVER_PORT']; |
| 60 | } |
| 61 | |
| 62 | if ( empty( $_SERVER['REQUEST_URI'] ) ) { |
| 63 | $cur_url = trailingslashit( $cur_url ); |
| 64 | } else { |
| 65 | $cur_url .= $_SERVER['REQUEST_URI']; |
| 66 | } |
| 67 | |
| 68 | $Url[$protocol] = $cur_url; |
| 69 | } |
| 70 | |
| 71 | return $Url[$protocol]; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Changes a relative URL to an absolute URL. |
| 76 | * This function uses WordPress `home_url()` to expand a relative URL. |
| 77 | * |
| 78 | * @uses home_url() |
| 79 | * |
| 80 | * @since 2.0.0 |
| 81 | * @api |
| 82 | * |
| 83 | * @param string $url An URL that can be absolute or relative. |
| 84 | * @return string The absolute URL. |
| 85 | */ |
| 86 | public function expand_url( $url ) { |
| 87 | if ( false === strpos( $url, '://' ) ) { |
| 88 | $url = home_url( $url ); |
| 89 | } |
| 90 | |
| 91 | return $url; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Retrieves the best guess of the client's actual IP address. |
| 96 | * Takes into account numerous HTTP proxy headers due to variations |
| 97 | * in how different ISPs handle IP addresses in headers between hops. |
| 98 | * |
| 99 | * @since 1.1.3 |
| 100 | * @api |
| 101 | * |
| 102 | * @return object { |
| 103 | * IP Address details |
| 104 | * |
| 105 | * @type string $ip The users IP address (might be spoofed, if $proxy is true) |
| 106 | * @type bool $proxy True, if a proxy was detected |
| 107 | * @type string $proxy_id The proxy-server IP address |
| 108 | * } |
| 109 | */ |
| 110 | public function current_ip() { |
| 111 | $result = (object) array( |
| 112 | 'ip' => $_SERVER['REMOTE_ADDR'], |
| 113 | 'proxy' => false, |
| 114 | 'proxy_ip' => '', |
| 115 | ); |
| 116 | |
| 117 | /* |
| 118 | * This code tries to bypass a proxy and get the actual IP address of |
| 119 | * the visitor behind the proxy. |
| 120 | * Warning: These values might be spoofed! |
| 121 | */ |
| 122 | $ip_fields = array( |
| 123 | 'HTTP_CLIENT_IP', |
| 124 | 'HTTP_X_FORWARDED_FOR', |
| 125 | 'HTTP_X_FORWARDED', |
| 126 | 'HTTP_X_CLUSTER_CLIENT_IP', |
| 127 | 'HTTP_FORWARDED_FOR', |
| 128 | 'HTTP_FORWARDED', |
| 129 | 'REMOTE_ADDR', |
| 130 | ); |
| 131 | $forwarded = false; |
| 132 | foreach ( $ip_fields as $key ) { |
| 133 | if ( true === array_key_exists( $key, $_SERVER ) ) { |
| 134 | foreach ( explode( ',', $_SERVER[$key] ) as $ip ) { |
| 135 | $ip = trim( $ip ); |
| 136 | |
| 137 | if ( false !== filter_var( $ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE ) ) { |
| 138 | $forwarded = $ip; |
| 139 | break 2; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // If we found a different IP address then REMOTE_ADDR then it's a proxy! |
| 146 | if ( ! empty( $forwarded ) && $forwarded != $result->ip ) { |
| 147 | $result->proxy = true; |
| 148 | $result->proxy_ip = $result->ip; |
| 149 | $result->ip = $forwarded; |
| 150 | } |
| 151 | |
| 152 | return $result; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Starts a file download and terminates the current request. |
| 157 | * Note that this does not work inside Ajax requests! |
| 158 | * |
| 159 | * @since 1.1.0 |
| 160 | * @api |
| 161 | * |
| 162 | * @param string $contents The file contents (text file). |
| 163 | * @param string $filename The file name. |
| 164 | */ |
| 165 | public function file_download( $contents, $filename ) { |
| 166 | // Send the download headers. |
| 167 | header( 'Pragma: public' ); |
| 168 | header( 'Expires: 0' ); |
| 169 | header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); |
| 170 | header( 'Cache-Control: private', false ); // required for certain browsers |
| 171 | header( 'Content-type: application/json' ); |
| 172 | header( 'Content-Disposition: attachment; filename="' . $filename . '"' ); |
| 173 | header( 'Content-Transfer-Encoding: binary' ); |
| 174 | header( 'Content-Length: ' . strlen( $contents ) ); |
| 175 | |
| 176 | // Finally send the export-file content. |
| 177 | echo $contents; |
| 178 | |
| 179 | exit; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Checks if the specified URL is publicly reachable. |
| 184 | * |
| 185 | * @since 1.1.0 |
| 186 | * @api |
| 187 | * |
| 188 | * @param string $url The URL to check. |
| 189 | * @return bool If URL is online or not. |
| 190 | */ |
| 191 | public function is_online( $url ) { |
| 192 | static $Checked = array(); |
| 193 | |
| 194 | if ( ! isset( $Checked[$url] ) ) { |
| 195 | $check = 'https://downforeveryoneorjustme.com/' . $url; |
| 196 | $res = wp_remote_get( $check, array( 'decompress' => false ) ); |
| 197 | |
| 198 | if ( is_wp_error( $res ) ) { |
| 199 | $state = false; |
| 200 | } else { |
| 201 | $state = ( false === stripos( $res['body'], 'not just you' ) ); |
| 202 | } |
| 203 | |
| 204 | $Checked[$url] = $state; |
| 205 | } |
| 206 | |
| 207 | return $Checked[$url]; |
| 208 | } |
| 209 | |
| 210 | } |