]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseApi.php
API: New function to fetch current user id
[friendica.git] / src / Module / BaseApi.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\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\DI;
26 use Friendica\Network\HTTPException;
27
28 require_once __DIR__ . '/../../include/api.php';
29
30 class BaseApi extends BaseModule
31 {
32         /**
33          * @var string json|xml|rss|atom
34          */
35         protected static $format = 'json';
36         /**
37          * @var bool|int
38          */
39         protected static $current_user_id;
40
41         public static function init(array $parameters = [])
42         {
43                 $arguments = DI::args();
44
45                 if (substr($arguments->getCommand(), -4) === '.xml') {
46                         self::$format = 'xml';
47                 }
48                 if (substr($arguments->getCommand(), -4) === '.rss') {
49                         self::$format = 'rss';
50                 }
51                 if (substr($arguments->getCommand(), -4) === '.atom') {
52                         self::$format = 'atom';
53                 }
54         }
55
56         public static function post(array $parameters = [])
57         {
58                 if (!api_user()) {
59                         throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.'));
60                 }
61
62                 $a = DI::app();
63
64                 if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) {
65                         throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
66                 }
67         }
68
69         /**
70          * Log in user via OAuth1 or Simple HTTP Auth.
71          *
72          * Simple Auth allow username in form of <pre>user@server</pre>, ignoring server part
73          *
74          * @return bool Was a user authenticated?
75          * @throws HTTPException\ForbiddenException
76          * @throws HTTPException\UnauthorizedException
77          * @throws HTTPException\InternalServerErrorException
78          * @hook  'authenticate'
79          *               array $addon_auth
80          *               'username' => username from login form
81          *               'password' => password from login form
82          *               'authenticated' => return status,
83          *               'user_record' => return authenticated user record
84          */
85         protected static function login()
86         {
87                 api_login(DI::app());
88
89                 self::$current_user_id = api_user();
90
91                 return (bool)self::$current_user_id;
92         }
93
94         /**
95          * Get current user id, returns 0 if not logged in
96          *
97          * @return int User ID
98          */
99         protected static function getCurrentUserID()
100         {
101                 if (is_null(self::$current_user_id)) {
102                         api_login(DI::app(), false);
103
104                         self::$current_user_id = api_user();
105                 }
106
107                 return (int)self::$current_user_id;
108         }
109
110         /**
111          * Get user info array.
112          *
113          * @param int|string $contact_id Contact ID or URL
114          * @return array|bool
115          * @throws HTTPException\BadRequestException
116          * @throws HTTPException\InternalServerErrorException
117          * @throws HTTPException\UnauthorizedException
118          * @throws \ImagickException
119          */
120         protected static function getUser($contact_id = null)
121         {
122                 return api_get_user(DI::app(), $contact_id);
123         }
124
125         /**
126          * Formats the data according to the data type
127          *
128          * @param string $root_element
129          * @param array $data An array with a single element containing the returned result
130          * @return false|string
131          */
132         protected static function format(string $root_element, array $data)
133         {
134                 $return = api_format_data($root_element, self::$format, $data);
135
136                 switch (self::$format) {
137                         case "xml":
138                                 header("Content-Type: text/xml");
139                                 break;
140                         case "json":
141                                 header("Content-Type: application/json");
142                                 if (!empty($return)) {
143                                         $json = json_encode(end($return));
144                                         if (!empty($_GET['callback'])) {
145                                                 $json = $_GET['callback'] . "(" . $json . ")";
146                                         }
147                                         $return = $json;
148                                 }
149                                 break;
150                         case "rss":
151                                 header("Content-Type: application/rss+xml");
152                                 $return  = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
153                                 break;
154                         case "atom":
155                                 header("Content-Type: application/atom+xml");
156                                 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
157                                 break;
158                 }
159                 
160                 return $return;
161         }
162
163         /**
164          * Creates the XML from a JSON style array
165          *
166          * @param $data
167          * @param $root_element
168          * @return string
169          */
170         protected static function createXml($data, $root_element)
171         {
172                 return api_create_xml($data, $root_element);
173         }
174 }