PageRenderTime 64ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/ProxyTools.php

https://bitbucket.org/ghostfreeman/freeside-wiki
PHP | 126 lines | 54 code | 11 blank | 61 comment | 2 complexity | 3f6363b5c8427ecdfcfda06c5ca2817b MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, LGPL-3.0
  1. <?php
  2. /**
  3. * Functions for dealing with proxies.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Extracts the XFF string from the request header
  24. * Note: headers are spoofable
  25. *
  26. * @deprecated in 1.19; use $wgRequest->getHeader( 'X-Forwarded-For' ) instead.
  27. * @return string
  28. */
  29. function wfGetForwardedFor() {
  30. wfDeprecated( __METHOD__, '1.19' );
  31. global $wgRequest;
  32. return $wgRequest->getHeader( 'X-Forwarded-For' );
  33. }
  34. /**
  35. * Returns the browser/OS data from the request header
  36. * Note: headers are spoofable
  37. *
  38. * @deprecated in 1.18; use $wgRequest->getHeader( 'User-Agent' ) instead.
  39. * @return string
  40. */
  41. function wfGetAgent() {
  42. wfDeprecated( __METHOD__, '1.18' );
  43. global $wgRequest;
  44. return $wgRequest->getHeader( 'User-Agent' );
  45. }
  46. /**
  47. * Work out the IP address based on various globals
  48. * For trusted proxies, use the XFF client IP (first of the chain)
  49. *
  50. * @deprecated in 1.19; call $wgRequest->getIP() directly.
  51. * @return string
  52. */
  53. function wfGetIP() {
  54. wfDeprecated( __METHOD__, '1.19' );
  55. global $wgRequest;
  56. return $wgRequest->getIP();
  57. }
  58. /**
  59. * Checks if an IP is a trusted proxy providor.
  60. * Useful to tell if X-Fowarded-For data is possibly bogus.
  61. * Squid cache servers for the site are whitelisted.
  62. *
  63. * @param $ip String
  64. * @return bool
  65. */
  66. function wfIsTrustedProxy( $ip ) {
  67. $trusted = wfIsConfiguredProxy( $ip );
  68. wfRunHooks( 'IsTrustedProxy', array( &$ip, &$trusted ) );
  69. return $trusted;
  70. }
  71. /**
  72. * Checks if an IP matches a proxy we've configured.
  73. * @param $ip String
  74. * @return bool
  75. */
  76. function wfIsConfiguredProxy( $ip ) {
  77. global $wgSquidServers, $wgSquidServersNoPurge;
  78. $trusted = in_array( $ip, $wgSquidServers ) ||
  79. in_array( $ip, $wgSquidServersNoPurge );
  80. return $trusted;
  81. }
  82. /**
  83. * Forks processes to scan the originating IP for an open proxy server
  84. * MemCached can be used to skip IPs that have already been scanned
  85. */
  86. function wfProxyCheck() {
  87. global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
  88. global $wgMemc, $wgProxyMemcExpiry, $wgRequest;
  89. global $wgProxyKey;
  90. if ( !$wgBlockOpenProxies ) {
  91. return;
  92. }
  93. $ip = $wgRequest->getIP();
  94. # Get MemCached key
  95. $mcKey = wfMemcKey( 'proxy', 'ip', $ip );
  96. $mcValue = $wgMemc->get( $mcKey );
  97. $skip = (bool)$mcValue;
  98. # Fork the processes
  99. if ( !$skip ) {
  100. $title = SpecialPage::getTitleFor( 'Blockme' );
  101. $iphash = md5( $ip . $wgProxyKey );
  102. $url = wfExpandUrl( $title->getFullURL( 'ip='.$iphash ), PROTO_HTTP );
  103. foreach ( $wgProxyPorts as $port ) {
  104. $params = implode( ' ', array(
  105. escapeshellarg( $wgProxyScriptPath ),
  106. escapeshellarg( $ip ),
  107. escapeshellarg( $port ),
  108. escapeshellarg( $url )
  109. ));
  110. exec( "php $params >" . wfGetNull() . " 2>&1 &" );
  111. }
  112. # Set MemCached key
  113. $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
  114. }
  115. }