3 namespace Friendica\Module\Api;
5 use Friendica\App\Arguments;
6 use Friendica\Core\L10n;
7 use Friendica\Core\Logger;
8 use Friendica\Core\System;
9 use Friendica\Object\Api\Mastodon\Error;
10 use Friendica\Util\Arrays;
11 use Friendica\Util\HTTPInputData;
12 use Friendica\Util\XML;
15 * This class is used to format and return API responses
26 * @param Arguments $args
28 public function __construct(L10n $l10n, Arguments $args)
35 * Creates the XML from a JSON style array
37 * @param array $data JSON style array
38 * @param string $root_element Name of the root element
40 * @return string The XML data
42 public function createXML(array $data, string $root_element): string
44 $childname = key($data);
45 $data2 = array_pop($data);
48 '' => 'http://api.twitter.com',
49 'statusnet' => 'http://status.net/schema/api/1/',
50 'friendica' => 'http://friendi.ca/schema/api/1/',
51 'georss' => 'http://www.georss.org/georss'
54 /// @todo Auto detection of needed namespaces
55 if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) {
59 if (is_array($data2)) {
61 Arrays::walkRecursive($data2, ['Friendica\Module\Api\ApiResponse', 'reformatXML']);
67 foreach ($data2 as $item) {
68 $data4[$i++ . ':' . $childname] = $item;
75 $data3 = [$root_element => $data2];
77 return XML::fromArray($data3, $xml, false, $namespaces);
81 * Formats the data according to the data type
83 * @param string $root_element Name of the root element
84 * @param string $type Return type (atom, rss, xml, json)
85 * @param array $data JSON style array
87 * @return array|string (string|array) XML data or JSON data
89 public function formatData(string $root_element, string $type, array $data)
95 $ret = $this->createXML($data, $root_element);
106 * Callback function to transform the array in an array that can be transformed in a XML file
108 * @param mixed $item Array item value
109 * @param string $key Array key
113 public static function reformatXML(&$item, string &$key): bool
115 if (is_bool($item)) {
116 $item = ($item ? 'true' : 'false');
119 if (substr($key, 0, 10) == 'statusnet_') {
120 $key = 'statusnet:' . substr($key, 10);
121 } elseif (substr($key, 0, 10) == 'friendica_') {
122 $key = 'friendica:' . substr($key, 10);
128 * Exit with error code
131 * @param string $description
132 * @param string $message
133 * @param string|null $format
137 public function error(int $code, string $description, string $message, string $format = null)
140 'error' => $message ?: $description,
141 'code' => $code . ' ' . $description,
142 'request' => $this->args->getQueryString()
145 header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
147 $this->exit('status', ['status' => $error], $format);
151 * Outputs formatted data according to the data type and then exits the execution.
153 * @param string $root_element
154 * @param array $data An array with a single element containing the returned result
155 * @param string|null $format Output format (xml, json, rss, atom)
159 public function exit(string $root_element, array $data, string $format = null)
161 $format = $format ?? 'json';
163 $return = $this->formatData($root_element, $format, $data);
167 header('Content-Type: text/xml');
170 header('Content-Type: application/json');
171 if (!empty($return)) {
172 $json = json_encode(end($return));
173 if (!empty($_GET['callback'])) {
174 $json = $_GET['callback'] . '(' . $json . ')';
180 header('Content-Type: application/rss+xml');
181 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
184 header('Content-Type: application/atom+xml');
185 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
194 * Quit execution with the message that the endpoint isn't implemented
196 * @param string $method
201 public function unsupported(string $method = 'all')
203 $path = $this->args->getQueryString();
204 Logger::info('Unimplemented API call',
208 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
209 'request' => HTTPInputData::process()
211 $error = $this->l10n->t('API endpoint %s %s is not implemented', strtoupper($method), $path);
212 $error_description = $this->l10n->t('The API endpoint is currently not implemented but might be in the future.');
213 $errorobj = new Error($error, $error_description);
214 System::jsonError(501, $errorobj->toArray());