Update copyright
[friendica.git/.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 user info array.
96          *
97          * @param int|string $contact_id Contact ID or URL
98          * @return array|bool
99          * @throws HTTPException\BadRequestException
100          * @throws HTTPException\InternalServerErrorException
101          * @throws HTTPException\UnauthorizedException
102          * @throws \ImagickException
103          */
104         protected static function getUser($contact_id = null)
105         {
106                 return api_get_user(DI::app(), $contact_id);
107         }
108
109         /**
110          * Formats the data according to the data type
111          *
112          * @param string $root_element
113          * @param array $data An array with a single element containing the returned result
114          * @return false|string
115          */
116         protected static function format(string $root_element, array $data)
117         {
118                 $return = api_format_data($root_element, self::$format, $data);
119
120                 switch (self::$format) {
121                         case "xml":
122                                 header("Content-Type: text/xml");
123                                 break;
124                         case "json":
125                                 header("Content-Type: application/json");
126                                 if (!empty($return)) {
127                                         $json = json_encode(end($return));
128                                         if (!empty($_GET['callback'])) {
129                                                 $json = $_GET['callback'] . "(" . $json . ")";
130                                         }
131                                         $return = $json;
132                                 }
133                                 break;
134                         case "rss":
135                                 header("Content-Type: application/rss+xml");
136                                 $return  = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
137                                 break;
138                         case "atom":
139                                 header("Content-Type: application/atom+xml");
140                                 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
141                                 break;
142                 }
143                 
144                 return $return;
145         }
146
147         /**
148          * Creates the XML from a JSON style array
149          *
150          * @param $data
151          * @param $root_element
152          * @return string
153          */
154         protected static function createXml($data, $root_element)
155         {
156                 return api_create_xml($data, $root_element);
157         }
158 }