Use short form array syntax everywhere
[friendica-addons.git] / dav / friendica / dav_friendica_auth.inc.php
1 <?php
2
3 use Friendica\Model\User;
4
5 class Sabre_DAV_Auth_Backend_Std extends Sabre_DAV_Auth_Backend_AbstractBasic
6 {
7         /**
8          * @var Sabre_DAV_Auth_Backend_Std|null
9          */
10         private static $instance = null;
11
12         /**
13          * @static
14          * @return Sabre_DAV_Auth_Backend_Std
15          */
16         public static function getInstance()
17         {
18                 if (is_null(self::$instance)) {
19                         self::$instance = new Sabre_DAV_Auth_Backend_Std();
20                 }
21                 return self::$instance;
22         }
23
24         /**
25          * @return array
26          */
27         public function getUsers()
28         {
29                 return [$this->currentUser];
30         }
31
32         /**
33          * @return null|string
34          */
35         public function getCurrentUser()
36         {
37                 return $this->currentUser;
38         }
39
40         /**
41          * Authenticates the user based on the current request.
42          *
43          * If authentication is successful, true must be returned.
44          * If authentication fails, an exception must be thrown.
45          *
46          * @param Sabre_DAV_Server $server
47          * @param string $realm
48          * @throws Sabre_DAV_Exception_NotAuthenticated
49          * @return bool
50          */
51         public function authenticate(Sabre_DAV_Server $server, $realm)
52         {
53                 $a = get_app();
54                 if (isset($a->user["uid"])) {
55                         $this->currentUser = strtolower($a->user["nickname"]);
56                         return true;
57                 }
58
59                 $auth = new Sabre_HTTP_BasicAuth();
60                 $auth->setHTTPRequest($server->httpRequest);
61                 $auth->setHTTPResponse($server->httpResponse);
62                 $auth->setRealm($realm);
63                 $userpass = $auth->getUserPass();
64                 if (!$userpass) {
65                         $auth->requireLogin();
66                         throw new Sabre_DAV_Exception_NotAuthenticated('No basic authentication headers were found');
67                 }
68
69                 // Authenticates the user
70                 if (!$this->validateUserPass($userpass[0], $userpass[1])) {
71                         $auth->requireLogin();
72                         throw new Sabre_DAV_Exception_NotAuthenticated('Username or password does not match');
73                 }
74                 $this->currentUser = strtolower($userpass[0]);
75                 return true;
76         }
77
78         /**
79          * @param string $username
80          * @param string $password
81          * @return bool
82          */
83         protected function validateUserPass($username, $password)
84         {
85                 return User::authenticate($username, $password);
86         }
87 }