authentication.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * authentication
  4. *
  5. * @method validate uses authentication plugins to check if a user is authorized to login
  6. * @method get_domain used to get the domain name from the URL or username and then sets both domain_name and domain_uuid
  7. */
  8. class authentication {
  9. /**
  10. * Define variables and their scope
  11. */
  12. public $debug;
  13. public $db;
  14. public $domain_uuid;
  15. public $domain_name;
  16. public $username;
  17. public $password;
  18. public $plugins;
  19. public $key;
  20. /**
  21. * Called when the object is created
  22. */
  23. public function __construct() {
  24. }
  25. /**
  26. * Called when there are no references to a particular object
  27. * unset the variables used in the class
  28. */
  29. public function __destruct() {
  30. foreach ($this as $key => $value) {
  31. unset($this->$key);
  32. }
  33. }
  34. /**
  35. * validate uses authentication plugins to check if a user is authorized to login
  36. * @return array [plugin] => last plugin used to authenticate the user [authorized] => true or false
  37. */
  38. public function validate() {
  39. //set the default authentication method to the database
  40. if (!is_array($_SESSION['authentication']['methods'])) {
  41. $_SESSION['authentication']['methods'][] = 'database';
  42. }
  43. //get the domain_name and domain_uuid
  44. if (!isset($this->domain_name) || !isset($this->domain_uuid)) {
  45. $this->get_domain();
  46. }
  47. //set the database as the default plugin
  48. if (!isset($_SESSION['authentication']['methods'])) {
  49. $_SESSION['authentication']['methods'][] = 'database';
  50. }
  51. //use the authentication plugins
  52. foreach ($_SESSION['authentication']['methods'] as $name) {
  53. $class_name = "plugin_".$name;
  54. $base = realpath(dirname(__FILE__)) . "/plugins";
  55. $plugin = $base."/".$name.".php";
  56. if (file_exists($plugin)) {
  57. include_once $plugin;
  58. $object = new $class_name();
  59. $object->debug = $this->debug;
  60. $object->domain_name = $this->domain_name;
  61. $object->domain_uuid = $this->domain_uuid;
  62. if (strlen($this->key) > 0) {
  63. $object->key = $this->key;
  64. }
  65. if (strlen($this->username) > 0) {
  66. $object->username = $this->username;
  67. $object->password = $this->password;
  68. }
  69. $array = $object->$name();
  70. $result['plugin'] = $array["plugin"];
  71. $result['domain_name'] = $array["domain_name"];
  72. $result['username'] = $array["username"];
  73. if ($this->debug) {
  74. $result["password"] = $this->password;
  75. }
  76. $result['user_uuid'] = $array["user_uuid"];
  77. $result['contact_uuid'] = $array["contact_uuid"];
  78. $result['domain_uuid'] = $array["domain_uuid"];
  79. $result['authorized'] = $array["authorized"];
  80. if (count($_SESSION['authentication']['methods']) > 1) {
  81. $result['results'][] = $array;
  82. }
  83. if ($result["authorized"] == "true") {
  84. //add the username to the session
  85. $_SESSION['username'] = $result["username"];
  86. //end the loop
  87. break;
  88. }
  89. }
  90. }
  91. //add user logs
  92. if (file_exists($_SERVER["PROJECT_ROOT"]."/app/user_logs/app_config.php")) {
  93. user_logs::add($result);
  94. }
  95. //return the result
  96. return $result;
  97. }
  98. /**
  99. * get_domain used to get the domain name from the URL or username and then sets both domain_name and domain_uuid
  100. */
  101. function get_domain() {
  102. //get the domain from the url
  103. $this->domain_name = $_SERVER["HTTP_HOST"];
  104. //get the domain name from the username
  105. if ($_SESSION["users"]["unique"]["text"] != "global") {
  106. $username_array = explode("@", $_REQUEST["username"]);
  107. if (count($username_array) > 1) {
  108. //get the domain name
  109. $domain_name = $username_array[count($username_array) -1];
  110. //check if the domain from the username exists then set the domain_uuid
  111. $domain_exists = false;
  112. foreach ($_SESSION['domains'] as $row) {
  113. if (lower_case($row['domain_name']) == lower_case($domain_name)) {
  114. $this->domain_uuid = $row['domain_uuid'];
  115. $domain_exists = true;
  116. break;
  117. }
  118. }
  119. //if the domain exists then set domain_name and update the username
  120. if ($domain_exists) {
  121. $this->domain_name = $domain_name;
  122. $this->username = substr($_REQUEST["username"], 0, -(strlen($domain_name)+1));
  123. $_SESSION['domain_uuid'] = $this->domain_uuid;
  124. }
  125. //unset the domain name variable
  126. unset($domain_name);
  127. }
  128. }
  129. //get the domain name from the http value
  130. if (strlen($_REQUEST["domain_name"]) > 0) {
  131. $this->domain_name = $_REQUEST["domain_name"];
  132. }
  133. //remote port number from the domain name
  134. $domain_array = explode(":", $this->domain_name);
  135. if (count($domain_array) > 1) {
  136. $this->domain_name = $domain_array[0];
  137. }
  138. //get the domain uuid and domain settings
  139. if (isset($this->domain_name) && !isset($this->domain_uuid)) {
  140. foreach ($_SESSION['domains'] as $row) {
  141. if (lower_case($row['domain_name']) == lower_case($this->domain_name)) {
  142. $this->domain_uuid = $row['domain_uuid'];
  143. $_SESSION['domain_uuid'] = $row['domain_uuid'];
  144. break;
  145. }
  146. }
  147. }
  148. //set the setting arrays
  149. $obj = new domains();
  150. $obj->db = $db;
  151. $obj->set();
  152. //set the domain settings
  153. $_SESSION['domain_name'] = $this->domain_name;
  154. $_SESSION['domain_parent_uuid'] = $_SESSION["domain_uuid"];
  155. //set the domain name
  156. return $this->domain_name;
  157. }
  158. }
  159. /*
  160. $auth = new authentication;
  161. $auth->username = "user";
  162. $auth->password = "password";
  163. $auth->domain_name = "sip.fusionpbx.com";
  164. $auth->debug = false;
  165. $response = $auth->validate();
  166. print_r($response);
  167. */
  168. ?>