ApiUser.php 3.4 KB

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