database.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * plugin_database
  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 plugin_database {
  9. /**
  10. * Define variables and their scope
  11. */
  12. public $debug;
  13. public $domain_name;
  14. public $domain_uuid;
  15. public $user_uuid;
  16. public $contact_uuid;
  17. public $username;
  18. public $password;
  19. public $key;
  20. /**
  21. * database checks the local database to authenticate the user or key
  22. * @return array [authorized] => true or false
  23. */
  24. function database() {
  25. //set the default status
  26. $user_authorized = false;
  27. //check the username and password if they don't match then redirect to the login
  28. $sql = "select * from v_users ";
  29. if (strlen($this->key) > 30) {
  30. $sql .= "where api_key = :api_key ";
  31. $parameters['api_key'] = $this->key;
  32. }
  33. else {
  34. $sql .= "where lower(username) = lower(:username) ";
  35. $parameters['username'] = $this->username;
  36. }
  37. if ($_SESSION["users"]["unique"]["text"] == "global") {
  38. //unique username - global (example: email address)
  39. }
  40. else {
  41. //unique username - per domain
  42. $sql .= "and domain_uuid = :domain_uuid ";
  43. $parameters['domain_uuid'] = $this->domain_uuid;
  44. }
  45. $sql .= "and (user_enabled = 'true' or user_enabled is null) ";
  46. $database = new database;
  47. $row = $database->select($sql, $parameters, 'row');
  48. if (is_array($row) && @sizeof($row) != 0) {
  49. //get the domain uuid when users are unique globally
  50. if ($_SESSION["users"]["unique"]["text"] == "global" && $row["domain_uuid"] != $this->domain_uuid) {
  51. //set the domain_uuid
  52. $this->domain_uuid = $row["domain_uuid"];
  53. $this->domain_name = $_SESSION['domains'][$this->domain_uuid]['domain_name'];
  54. //set the domain session variables
  55. $_SESSION["domain_uuid"] = $this->domain_uuid;
  56. $_SESSION["domain_name"] = $this->domain_name;
  57. //set the setting arrays
  58. $domain = new domains();
  59. $domain->db = $db;
  60. $domain->set();
  61. }
  62. //set the user_uuid
  63. $this->user_uuid = $row['user_uuid'];
  64. $this->contact_uuid = $row['contact_uuid'];
  65. //if salt is not defined then use the default salt for backwards compatibility
  66. if (strlen($row["salt"]) == 0) {
  67. $row["salt"] = 'e3.7d.12';
  68. }
  69. //compare the password provided by the user with the one in the database
  70. if (md5($row["salt"].$this->password) == $row["password"]) {
  71. $user_authorized = true;
  72. }
  73. else if (strlen($this->key) > 30 && $this->key == $row["api_key"]) {
  74. $user_authorized = true;
  75. }
  76. else {
  77. $user_authorized = false;
  78. }
  79. }
  80. unset($result);
  81. //result array
  82. $result["plugin"] = "database";
  83. $result["domain_name"] = $this->domain_name;
  84. $result["username"] = $this->username;
  85. if ($this->debug) {
  86. $result["password"] = $this->password;
  87. }
  88. $result["user_uuid"] = $this->user_uuid;
  89. $result["domain_uuid"] = $this->domain_uuid;
  90. $result["contact_uuid"] = $this->contact_uuid;
  91. $result["sql"] = $sql;
  92. if ($user_authorized) {
  93. $result["authorized"] = "true";
  94. }
  95. else {
  96. $result["authorized"] = "false";
  97. }
  98. return $result;
  99. }
  100. }
  101. ?>