3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
26 use Friendica\Network\HTTPException;
28 require_once __DIR__ . '/../../include/api.php';
30 class BaseApi extends BaseModule
33 * @var string json|xml|rss|atom
35 protected static $format = 'json';
39 protected static $current_user_id;
41 public static function init(array $parameters = [])
43 $arguments = DI::args();
45 if (substr($arguments->getQueryString(), -4) === '.xml') {
46 self::$format = 'xml';
48 if (substr($arguments->getQueryString(), -4) === '.rss') {
49 self::$format = 'rss';
51 if (substr($arguments->getQueryString(), -4) === '.atom') {
52 self::$format = 'atom';
56 public static function post(array $parameters = [])
59 throw new HTTPException\UnauthorizedException(DI::l10n()->t('Permission denied.'));
64 if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) {
65 throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
70 * Log in user via OAuth1 or Simple HTTP Auth.
72 * Simple Auth allow username in form of <pre>user@server</pre>, ignoring server part
74 * @return bool Was a user authenticated?
75 * @throws HTTPException\ForbiddenException
76 * @throws HTTPException\UnauthorizedException
77 * @throws HTTPException\InternalServerErrorException
78 * @hook 'authenticate'
80 * 'username' => username from login form
81 * 'password' => password from login form
82 * 'authenticated' => return status,
83 * 'user_record' => return authenticated user record
85 protected static function login()
89 self::$current_user_id = api_user();
91 return (bool)self::$current_user_id;
95 * Get user info array.
97 * @param int|string $contact_id Contact ID or URL
99 * @throws HTTPException\BadRequestException
100 * @throws HTTPException\InternalServerErrorException
101 * @throws HTTPException\UnauthorizedException
102 * @throws \ImagickException
104 protected static function getUser($contact_id = null)
106 return api_get_user(DI::app(), $contact_id);
110 * Formats the data according to the data type
112 * @param string $root_element
113 * @param array $data An array with a single element containing the returned result
114 * @return false|string
116 protected static function format(string $root_element, array $data)
118 $return = api_format_data($root_element, self::$format, $data);
120 switch (self::$format) {
122 header("Content-Type: text/xml");
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 . ")";
135 header("Content-Type: application/rss+xml");
136 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
139 header("Content-Type: application/atom+xml");
140 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
148 * Creates the XML from a JSON style array
151 * @param $root_element
154 protected static function createXml($data, $root_element)
156 return api_create_xml($data, $root_element);