]> git.mxchange.org Git - friendica.git/blob - src/Security/BasicAuth.php
e55700bf9e8c350f8c3c069dc842da024001711f
[friendica.git] / src / Security / BasicAuth.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Security;
23
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26
27 /**
28  * Authentification via the basic auth method
29  */
30 class BasicAuth
31 {
32         /**
33          * @var bool|int
34          */
35         protected static $current_user_id = 0;
36         /**
37          * @var array
38          */
39         protected static $current_token = [];
40
41         /**
42          * Get current user id, returns 0 if $login is set to false and not logged in.
43          * When $login is true, the execution will stop when not logged in.
44          *
45          * @param bool $login Perform a login request if "true"
46          *
47          * @return int User ID
48          */
49         public static function getCurrentUserID(bool $login = true)
50         {
51                 if (empty(self::$current_user_id)) {
52                         api_login(DI::app(), $login);
53
54                         self::$current_user_id = api_user();
55                 }
56
57                 return (int)self::$current_user_id;
58         }
59
60         /**
61          * Fetch a dummy application token
62          *
63          * @return array token
64          */
65         public static function getCurrentApplicationToken()
66         {
67                 if (empty(self::getCurrentUserID())) {
68                         return [];
69                 }
70
71                 if (!empty(self::$current_token)) {
72                         return self::$current_token;
73                 }
74
75                 self::$current_token = [
76                         'uid'        => self::$current_user_id,
77                         'id'         => 0,
78                         'name'       => api_source(),
79                         'website'    => '',
80                         'created_at' => DBA::NULL_DATETIME,
81                         'read'       => true,
82                         'write'      => true,
83                         'follow'     => true,
84                         'push'       => false];
85
86                 return self::$current_token;
87         }
88 }