]> git.mxchange.org Git - friendica.git/blob - src/Security/BasicAuth.php
fe42e61e91d17c7444ce4df7970b38b729ac7e72
[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          * Fetch a dummy application token
43          *
44          * @return array token
45          */
46         public static function getCurrentApplicationToken()
47         {
48                 if (empty(self::getCurrentUserID())) {
49                         return [];
50                 }
51
52                 if (!empty(self::$current_token)) {
53                         return self::$current_token;
54                 }
55
56                 self::$current_token = [
57                         'uid'        => self::$current_user_id,
58                         'id'         => 0,
59                         'name'       => api_source(),
60                         'website'    => '',
61                         'created_at' => DBA::NULL_DATETIME,
62                         'read'       => true,
63                         'write'      => true,
64                         'follow'     => true,
65                         'push'       => false];
66
67                 return self::$current_token;
68         }
69
70         /**
71          * Get current user id, returns 0 if $login is set to false and not logged in.
72          * When $login is true, the execution will stop when not logged in.
73          *
74          * @param bool $login Perform a login request if "true"
75          *
76          * @return int User ID
77          */
78         public static function getCurrentUserID(bool $login = true)
79         {
80                 if (empty(self::$current_user_id)) {
81                         api_login(DI::app(), $login);
82
83                         self::$current_user_id = api_user();
84                 }
85
86                 return (int)self::$current_user_id;
87         }
88 }