TheLib_Net

The Net component.

Defined (1)

The class is defined in the following location(s).

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