/html/ops/admin/libraries/ip_allow_deny.lib.php

https://github.com/jackygrahamez/DrugDiscovery-Home · PHP · 190 lines · 92 code · 28 blank · 70 comment · 30 complexity · d7e953c729ca3f1565d9fe91a27dc2f8 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * This library is used with the server IP allow/deny host authentication
  5. * feature
  6. *
  7. * @version $Id: ip_allow_deny.lib.php 10142 2007-03-20 10:32:13Z cybot_tm $
  8. */
  9. /**
  10. * Gets the "true" IP address of the current user
  11. *
  12. * @return string the ip of the user
  13. *
  14. * @access private
  15. */
  16. function PMA_getIp()
  17. {
  18. /* Get the address of user */
  19. if (!empty($_SERVER['REMOTE_ADDR'])) {
  20. $direct_ip = $_SERVER['REMOTE_ADDR'];
  21. } else {
  22. /* We do not know remote IP */
  23. return false;
  24. }
  25. /* Do we trust this IP as a proxy? If yes we will use it's header. */
  26. if (isset($GLOBALS['cfg']['TrustedProxies'][$direct_ip])) {
  27. $proxy_ip = PMA_getenv($GLOBALS['cfg']['TrustedProxies'][$direct_ip]);
  28. // the $ checks that the header contains only one IP address
  29. $is_ip = preg_match('|^([0-9]{1,3}\.){3,3}[0-9]{1,3}$|', $proxy_ip, $regs);
  30. if ($is_ip && (count($regs) > 0)) {
  31. // True IP behind a proxy
  32. return $regs[0];
  33. }
  34. }
  35. /* Return true IP */
  36. return $direct_ip;
  37. } // end of the 'PMA_getIp()' function
  38. /**
  39. * Based on IP Pattern Matcher
  40. * Originally by J.Adams <jna@retina.net>
  41. * Found on <http://www.php.net/manual/en/function.ip2long.php>
  42. * Modified by Robbat2 <robbat2@users.sourceforge.net>
  43. *
  44. * Matches:
  45. * xxx.xxx.xxx.xxx (exact)
  46. * xxx.xxx.xxx.[yyy-zzz] (range)
  47. * xxx.xxx.xxx.xxx/nn (CIDR)
  48. *
  49. * Does not match:
  50. * xxx.xxx.xxx.xx[yyy-zzz] (range, partial octets not supported)
  51. *
  52. * @param string string of IP range to match
  53. * @param string string of IP to test against range
  54. *
  55. * @return boolean always true
  56. *
  57. * @access public
  58. */
  59. function PMA_ipMaskTest($testRange, $ipToTest)
  60. {
  61. $result = true;
  62. if (preg_match('|([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/([0-9]+)|', $testRange, $regs)) {
  63. // performs a mask match
  64. $ipl = ip2long($ipToTest);
  65. $rangel = ip2long($regs[1] . '.' . $regs[2] . '.' . $regs[3] . '.' . $regs[4]);
  66. $maskl = 0;
  67. for ($i = 0; $i < 31; $i++) {
  68. if ($i < $regs[5] - 1) {
  69. $maskl = $maskl + PMA_pow(2, (30 - $i));
  70. } // end if
  71. } // end for
  72. if (($maskl & $rangel) == ($maskl & $ipl)) {
  73. return true;
  74. } else {
  75. return false;
  76. }
  77. } else {
  78. // range based
  79. $maskocts = explode('.', $testRange);
  80. $ipocts = explode('.', $ipToTest);
  81. // perform a range match
  82. for ($i = 0; $i < 4; $i++) {
  83. if (preg_match('|\[([0-9]+)\-([0-9]+)\]|', $maskocts[$i], $regs)) {
  84. if (($ipocts[$i] > $regs[2])
  85. || ($ipocts[$i] < $regs[1])) {
  86. $result = false;
  87. } // end if
  88. } else {
  89. if ($maskocts[$i] <> $ipocts[$i]) {
  90. $result = false;
  91. } // end if
  92. } // end if/else
  93. } //end for
  94. } //end if/else
  95. return $result;
  96. } // end of the "PMA_IPMaskTest()" function
  97. /**
  98. * Runs through IP Allow/Deny rules the use of it below for more information
  99. *
  100. * @param string 'allow' | 'deny' type of rule to match
  101. *
  102. * @return bool Matched a rule ?
  103. *
  104. * @access public
  105. *
  106. * @see PMA_getIp()
  107. */
  108. function PMA_allowDeny($type)
  109. {
  110. global $cfg;
  111. // Grabs true IP of the user and returns if it can't be found
  112. $remote_ip = PMA_getIp();
  113. if (empty($remote_ip)) {
  114. return false;
  115. }
  116. // copy username
  117. $username = $cfg['Server']['user'];
  118. // copy rule database
  119. $rules = $cfg['Server']['AllowDeny']['rules'];
  120. // lookup table for some name shortcuts
  121. $shortcuts = array(
  122. 'all' => '0.0.0.0/0',
  123. 'localhost' => '127.0.0.1/8'
  124. );
  125. // Provide some useful shortcuts if server gives us address:
  126. if (PMA_getenv('SERVER_ADDR')) {
  127. $shortcuts['localnetA'] = PMA_getenv('SERVER_ADDR') . '/8';
  128. $shortcuts['localnetB'] = PMA_getenv('SERVER_ADDR') . '/16';
  129. $shortcuts['localnetC'] = PMA_getenv('SERVER_ADDR') . '/24';
  130. }
  131. foreach ($rules as $rule) {
  132. // extract rule data
  133. $rule_data = explode(' ', $rule);
  134. // check for rule type
  135. if ($rule_data[0] != $type) {
  136. continue;
  137. }
  138. // check for username
  139. if (($rule_data[1] != '%') //wildcarded first
  140. && ($rule_data[1] != $username)) {
  141. continue;
  142. }
  143. // check if the config file has the full string with an extra
  144. // 'from' in it and if it does, just discard it
  145. if ($rule_data[2] == 'from') {
  146. $rule_data[2] = $rule_data[3];
  147. }
  148. // Handle shortcuts with above array
  149. // DON'T use "array_key_exists" as it's only PHP 4.1 and newer.
  150. if (isset($shortcuts[$rule_data[2]])) {
  151. $rule_data[2] = $shortcuts[$rule_data[2]];
  152. }
  153. // Add code for host lookups here
  154. // Excluded for the moment
  155. // Do the actual matching now
  156. if (PMA_ipMaskTest($rule_data[2], $remote_ip)) {
  157. return true;
  158. }
  159. } // end while
  160. return false;
  161. } // end of the "PMA_AllowDeny()" function
  162. ?>