root.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2012
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. // make sure the PATH_SEPARATOR is defined
  22. umask(2);
  23. if (!defined("PATH_SEPARATOR")) {
  24. if (strpos($_ENV["OS"], "Win") !== false) {
  25. define("PATH_SEPARATOR", ";");
  26. } else {
  27. define("PATH_SEPARATOR", ":");
  28. }
  29. }
  30. if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html';
  31. // make sure the document_root is set
  32. $_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]);
  33. if(PHP_SAPI == 'cli'){
  34. chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME));
  35. $script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]);
  36. $dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME));
  37. if (file_exists('/project_root.php')) {
  38. $path = '/';
  39. } else {
  40. $i = 1;
  41. $path = '';
  42. while ($i < count($dirs)) {
  43. $path .= '/' . $dirs[$i];
  44. if (file_exists($path. '/project_root.php')) {
  45. break;
  46. }
  47. $i++;
  48. }
  49. }
  50. $_SERVER["DOCUMENT_ROOT"] = $path;
  51. }else{
  52. $_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]);
  53. }
  54. $_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]);
  55. // try to detect if a project path is being used
  56. if (!defined('PROJECT_PATH')) {
  57. if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) {
  58. define('PROJECT_PATH', '/fusionpbx');
  59. } elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) {
  60. define('PROJECT_PATH', '');
  61. } else {
  62. $dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME)));
  63. $i = 1;
  64. $path = $_SERVER["DOCUMENT_ROOT"];
  65. while ($i < count($dirs)) {
  66. $path .= '/' . $dirs[$i];
  67. if (file_exists($path. '/project_root.php')) {
  68. break;
  69. }
  70. $i++;
  71. }
  72. if(!file_exists($path. '/project_root.php')){
  73. die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance");
  74. }
  75. $project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path);
  76. define('PROJECT_PATH', $project_path);
  77. }
  78. $_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH);
  79. set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]);
  80. }
  81. if (!class_exists('IP4Filter')) {
  82. class IP4Filter {
  83. private static $_IP_TYPE_SINGLE = 'single';
  84. private static $_IP_TYPE_WILDCARD = 'wildcard';
  85. private static $_IP_TYPE_MASK = 'mask';
  86. private static $_IP_TYPE_CIDR = 'CIDR';
  87. private static $_IP_TYPE_SECTION = 'section';
  88. private $_allowed_ips = array();
  89. public function __construct($allowed_ips) {
  90. $this->_allowed_ips = $allowed_ips;
  91. }
  92. public function check($ip, $allowed_ips = null) {
  93. $allowed_ips = $allowed_ips ? $allowed_ips : $this->_allowed_ips;
  94. foreach ($allowed_ips as $allowed_ip) {
  95. $type = $this->_judge_ip_type($allowed_ip);
  96. $sub_rst = call_user_func(array($this, '_sub_checker_' . $type), $allowed_ip, $ip);
  97. if ($sub_rst) {
  98. return true;
  99. }
  100. }
  101. return false;
  102. }
  103. private function _judge_ip_type($ip) {
  104. if (strpos($ip, '*')) {
  105. return self :: $_IP_TYPE_WILDCARD;
  106. }
  107. if (strpos($ip, '/')) {
  108. $tmp = explode('/', $ip);
  109. if (strpos($tmp[1], '.')) {
  110. return self :: $_IP_TYPE_MASK;
  111. } else {
  112. return self :: $_IP_TYPE_CIDR;
  113. }
  114. }
  115. if (strpos($ip, '-')) {
  116. return self :: $_IP_TYPE_SECTION;
  117. }
  118. if (ip2long($ip)) {
  119. return self :: $_IP_TYPE_SINGLE;
  120. }
  121. return false;
  122. }
  123. private function _sub_checker_single($allowed_ip, $ip) {
  124. return (ip2long($allowed_ip) == ip2long($ip));
  125. }
  126. private function _sub_checker_wildcard($allowed_ip, $ip) {
  127. $allowed_ip_arr = explode('.', $allowed_ip);
  128. $ip_arr = explode('.', $ip);
  129. for ($i = 0; $i < count($allowed_ip_arr); $i++) {
  130. if ($allowed_ip_arr[$i] == '*') {
  131. return true;
  132. } else {
  133. if (false == ($allowed_ip_arr[$i] == $ip_arr[$i])) {
  134. return false;
  135. }
  136. }
  137. }
  138. }
  139. private function _sub_checker_mask($allowed_ip, $ip) {
  140. list($allowed_ip_ip, $allowed_ip_mask) = explode('/', $allowed_ip);
  141. $begin = (ip2long($allowed_ip_ip) & ip2long($allowed_ip_mask)) + 1;
  142. $end = (ip2long($allowed_ip_ip) | (~ ip2long($allowed_ip_mask))) + 1;
  143. $ip = ip2long($ip);
  144. return ($ip >= $begin && $ip <= $end);
  145. }
  146. private function _sub_checker_section($allowed_ip, $ip) {
  147. list($begin, $end) = explode('-', $allowed_ip);
  148. $begin = ip2long($begin);
  149. $end = ip2long($end);
  150. $ip = ip2long($ip);
  151. return ($ip >= $begin && $ip <= $end);
  152. }
  153. private function _sub_checker_CIDR($CIDR, $IP) {
  154. list ($net, $mask) = explode('/', $CIDR);
  155. return ( ip2long($IP) & ~((1 << (32 - $mask)) - 1) ) == ip2long($net);
  156. }
  157. }
  158. function check_acl(){
  159. global $db, $debug, $domain_uuid, $domain_name;
  160. //select node_cidr from v_access_control_nodes where node_cidr != '';
  161. $sql = "select node_cidr from v_access_control_nodes where node_cidr != '' and node_type = 'allow'";
  162. $prep_statement = $db->prepare(check_sql($sql));
  163. $prep_statement->execute();
  164. $result = $prep_statement->fetchAll(PDO::FETCH_NAMED);
  165. if (count($result) == 0) {
  166. die("No ACL's");
  167. }
  168. foreach ($result as &$row) {
  169. $allowed_ips[] = $row['node_cidr'];
  170. }
  171. $acl = new IP4Filter($allowed_ips);
  172. return $acl->check($_SERVER['REMOTE_ADDR'],$allowed_ips);
  173. }
  174. }
  175. ?>