PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/components/com_travelbook/helpers/travelbook.php

https://github.com/ponlue/abktours
PHP | 130 lines | 74 code | 15 blank | 41 comment | 31 complexity | faacd9f21da3296280eb1f35796a848f MD5 | raw file
  1. <?php
  2. /**
  3. * "TRAVELbook - JOOMLA! on Tour"
  4. *
  5. * @version $Id: travelbook.php 2 2010-04-13 13:37:46Z WEB $
  6. * @copyright Copyright 2009-2010, $Author: WEB $
  7. * @license GNU General Public License (GNU GPL) GPLv2,
  8. * - see http://www.demo-page.de/en/license-conditions.html
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @link http://www.demo-page.de
  23. * @package TRAVELbook Component
  24. * @revision $Revision: 2 $
  25. * @lastmodified $Date: 2010-04-13 15:37:46 +0200 (Di, 13 Apr 2010) $
  26. */
  27. /*** No direct access ***/
  28. defined('_JEXEC') or die('Restricted access');
  29. /**
  30. * TRAVELbook Component Helper
  31. *
  32. * @static
  33. * @package Joomla
  34. * @subpackage Content
  35. * @since 1.5
  36. */
  37. class TravelbookHelperTravelbook
  38. {
  39. /**
  40. *
  41. * @param $VisitorIp - valid only when true is returned
  42. * @return true on success
  43. */
  44. function getVisitorIp(&$VisitorIp)
  45. {
  46. $Ip_tmp = null;
  47. // get usefull vars:
  48. $client_ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : NULL;
  49. $x_forwarded_for = isset($_SERVER["HTTP_X_FORWARDED_FOR"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : NULL;
  50. $remote_addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : NULL;
  51. // then the script itself
  52. if (!empty($x_forwarded_for) && strrpos($x_forwarded_for, '.') > 0)
  53. {
  54. $arr = explode(',', $x_forwarded_for);
  55. $Ip_tmp = trim(end($arr));
  56. }
  57. if (!TravelbookHelperTravelbook::isIpAddressValidRfc3330($Ip_tmp) && !empty($client_ip))
  58. {
  59. $ip_expl = explode('.', $client_ip);
  60. $referer = explode('.', $remote_addr);
  61. if ($referer[0] != $ip_expl[0])
  62. {
  63. $Ip_tmp = trim(implode('.', array_reverse($ip_expl)));
  64. }
  65. else
  66. {
  67. $arr = explode(',', $client_ip);
  68. $Ip_tmp = trim(end($arr));
  69. }
  70. }
  71. if (!TravelbookHelperTravelbook::isIpAddressValidRfc3330($Ip_tmp) && !empty($remote_addr))
  72. {
  73. $arr = explode(',', $remote_addr);
  74. $Ip_tmp = trim(end($arr));
  75. }
  76. unset($client_ip, $x_forwarded_for, $remote_addr, $ip_expl, $referer);
  77. $VisitorIp = $Ip_tmp;
  78. return true;
  79. }
  80. function isIpAddressValidRfc3330( $ipAddress ) {
  81. $substr2 = substr( $ipAddress, 0, 2 );
  82. $substr3 = substr( $ipAddress, 0, 3 );
  83. $substr4 = substr( $ipAddress, 0, 4 );
  84. $substr6 = substr( $ipAddress, 0, 6 );
  85. $substr8 = substr( $ipAddress, 0, 8 );
  86. $substr10 = substr( $ipAddress, 0, 10 );
  87. $substr12 = substr( $ipAddress, 0, 12 );
  88. $IpAsLong = sprintf( "%u", ip2long( $ipAddress ) );
  89. return ( ( $ipAddress != NULL ) &&
  90. ( $substr2 !== '0.' ) // Reserved IP block
  91. && ( $substr3 !== '10.' ) // Reserved for private networks
  92. && ( $substr3 !== '14.' ) // IANA Public Data Network
  93. && ( $substr3 !== '24.' ) // Reserved IP block
  94. && ( $substr3 !== '27.' ) // Reserved IP block
  95. && ( $substr3 !== '39.' ) // Reserved IP block
  96. && ( $substr4 !== '127.' ) // Reserved IP block
  97. && ( $substr6 !== '128.0.' ) // Reserved IP block
  98. && ( $substr8 !== '169.254.' ) // Reserved IP block
  99. && ( ( $IpAsLong < sprintf( "%u", ip2long( '172.16.0.0' ) ) ) // Private networks
  100. || $IpAsLong > sprintf( "%u", ip2long( '172.31.255.255' ) ) )
  101. && ( $substr8 !== '191.255.' ) // Reserved IP block
  102. && ( $substr8 !== '192.0.0.' ) // Reserved IP block
  103. && ( $substr8 !== '192.0.2.' ) // Reserved IP block
  104. && ( $substr10 !== '192.88.99.' ) // Reserved IP block
  105. && ( $substr8 !== '192.168.' ) // Reserved IP block
  106. && ( ( $IpAsLong < sprintf( "%u", ip2long( '198.18.0.0' ) ) ) // Multicast addresses
  107. || ( $IpAsLong > sprintf( "%u", ip2long( '198.19.255.255' ) ) ) )
  108. && ( $substr12 !== '223.255.255.' ) // Reserved IP block
  109. && ( ( $IpAsLong < sprintf( "%u", ip2long( '224.0.0.0' ) ) ) // Multicast addresses
  110. || ( $IpAsLong > sprintf( "%u", ip2long( '239.255.255.255' ) ) ) )
  111. && ( ( $IpAsLong < sprintf( "%u", ip2long( '240.0.0.0' ) ) ) // Reserved IP blocks
  112. || ( $IpAsLong > sprintf( "%u", ip2long( '255.255.255.255' ) ) ) )
  113. );
  114. }
  115. }