ApiUser.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. Lib::loadClass('User');
  3. Lib::loadClass('LDAP');
  4. class ApiUser {
  5. public $_user;
  6. public function auth() {
  7. if (User::logged()) {
  8. $this->_user = User::getCurrentUserObject();
  9. }
  10. else {
  11. $login = V::get('PHP_AUTH_USER', '', $_SERVER);
  12. $pass = V::get('PHP_AUTH_PW', '', $_SERVER);
  13. if (!$login) {
  14. $this->exitUnauthorized();
  15. }
  16. try {
  17. $ldap = LDAP::getInstance();
  18. if ($ldap != null && $ldap->isConnected()) {
  19. $this->_user = User::loginByLDAP($login, $pass);
  20. } else {
  21. $this->_user = User::loginByDB($login, $pass);
  22. }
  23. } catch (Exception $e) {
  24. $this->exitUnauthorized();
  25. }
  26. if (!$this->_user) {
  27. $this->exitUnauthorized();
  28. }
  29. $this->_saveToSession();
  30. }
  31. }
  32. private function _saveToSession() {
  33. $_SESSION['ADM_ID'] = $this->_user->ID;
  34. $_SESSION['AUTHORIZE_USER'] = $this->_user->ADM_ACCOUNT;
  35. $_SESSION['ADM_ACCOUNT'] = $this->_user->ADM_ACCOUNT;
  36. //$_SESSION['ADM_AREA'] = $this->_user->ADM_AREA;
  37. $_SESSION['ADM_NAME'] = $this->_user->ADM_NAME;
  38. $_SESSION['ADM_TECH_WORKER'] = $this->_user->ADM_TECH_WORKER;
  39. $_SESSION['ADM_COMPANY'] = $this->_user->ADM_COMPANY;
  40. $_SESSION['ADM_ADMIN_LEVEL'] = $this->_user->ADM_ADMIN_LEVEL;
  41. $_SESSION['ADM_PHONE'] = $this->_user->ADM_PHONE;
  42. $_SESSION['ADM_ADMIN_EXPIRE'] = $this->_user->ADM_ADMIN_EXPIRE;
  43. $_SESSION['ADM_ADMIN_DESC'] = $this->_user->ADM_ADMIN_DESC;
  44. $_SESSION['EMPLOYEE_TYPE'] = $this->_user->EMPLOYEE_TYPE;
  45. // save user pass in encrypted form
  46. //Lib::loadClass('Crypt');
  47. //$_SESSION['ADM_PASS_HASH'] = Crypt::encrypt($pass);
  48. //$_SESSION['EMAIL_IMAP_IMPORT_PASSWD_HASH'] = Crypt::encrypt($this->_user->EMAIL_IMAP_IMPORT_PASSWD);
  49. //$_SESSION['EMAIL_IMAP_IMPORT_HOST'] = $this->_user->EMAIL_IMAP_IMPORT_HOST;
  50. //$_SESSION['EMAIL_IMAP_IMPORT_USERNAME'] = $this->_user->EMAIL_IMAP_IMPORT_USERNAME;
  51. }
  52. public function logout() {
  53. header('WWW-Authenticate: Basic realm="API"');
  54. header('HTTP/1.0 401 Unauthorized');
  55. $apiUrl = "https://{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}";
  56. // $_SERVER[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
  57. $httpAccept = V::get('HTTP_ACCEPT', '', $_SERVER);
  58. if (false !== strpos($httpAccept, 'text/html')) {
  59. ?><!DOCTYPE html>
  60. <html>
  61. <head>
  62. <meta http-equiv="refresh" content="0; url=<?php echo $apiUrl; ?>" />
  63. </head>
  64. <body>
  65. Unauthorized - Go to <a href="<?php echo $apiUrl; ?>"><?php echo $apiUrl; ?></a>
  66. </body>
  67. </html>
  68. <?php
  69. } else {
  70. echo "Unauthorized - Go to {$apiUrl}";
  71. }
  72. exit;
  73. }
  74. public function exitUnauthorized() {
  75. header('WWW-Authenticate: Basic realm="API"');
  76. header('HTTP/1.0 401 Unauthorized');
  77. echo 'Unauthorized';
  78. exit;
  79. }
  80. public function isAdmin() {
  81. if (isset($this->_user->ADM_ADMIN_LEVEL) && in_array($this->_user->ADM_ADMIN_LEVEL, array(0, 1))) {
  82. return true;
  83. }
  84. return false;
  85. }
  86. public function getID() {
  87. return $this->_user->ID;
  88. }
  89. }