3 namespace Friendica\Module\Api;
5 use Friendica\App\Arguments;
6 use Friendica\Core\L10n;
7 use Friendica\Core\System;
8 use Friendica\Object\Api\Mastodon\Error;
9 use Friendica\Util\Arrays;
10 use Friendica\Util\HTTPInputData;
11 use Friendica\Util\XML;
12 use Psr\Log\LoggerInterface;
15 * This class is used to format and return API responses
23 /** @var LoggerInterface */
28 * @param Arguments $args
29 * @param LoggerInterface $logger
31 public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger)
35 $this->logger = $logger;
39 * Creates the XML from a JSON style array
41 * @param array $data JSON style array
42 * @param string $root_element Name of the root element
44 * @return string The XML data
46 public function createXML(array $data, string $root_element): string
48 $childname = key($data);
49 $data2 = array_pop($data);
52 '' => 'http://api.twitter.com',
53 'statusnet' => 'http://status.net/schema/api/1/',
54 'friendica' => 'http://friendi.ca/schema/api/1/',
55 'georss' => 'http://www.georss.org/georss'
58 /// @todo Auto detection of needed namespaces
59 if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) {
63 if (is_array($data2)) {
65 Arrays::walkRecursive($data2, ['Friendica\Module\Api\ApiResponse', 'reformatXML']);
71 foreach ($data2 as $item) {
72 $data4[$i++ . ':' . $childname] = $item;
79 $data3 = [$root_element => $data2];
81 return XML::fromArray($data3, $xml, false, $namespaces);
85 * Formats the data according to the data type
87 * @param string $root_element Name of the root element
88 * @param string $type Return type (atom, rss, xml, json)
89 * @param array $data JSON style array
91 * @return array|string (string|array) XML data or JSON data
93 public function formatData(string $root_element, string $type, array $data)
99 return $this->createXML($data, $root_element);
107 * Callback function to transform the array in an array that can be transformed in a XML file
109 * @param mixed $item Array item value
110 * @param string $key Array key
114 public static function reformatXML(&$item, string &$key): bool
116 if (is_bool($item)) {
117 $item = ($item ? 'true' : 'false');
120 if (substr($key, 0, 10) == 'statusnet_') {
121 $key = 'statusnet:' . substr($key, 10);
122 } elseif (substr($key, 0, 10) == 'friendica_') {
123 $key = 'friendica:' . substr($key, 10);
129 * Exit with error code
132 * @param string $description
133 * @param string $message
134 * @param string|null $format
138 public function error(int $code, string $description, string $message, string $format = null)
141 'error' => $message ?: $description,
142 'code' => $code . ' ' . $description,
143 'request' => $this->args->getQueryString()
146 header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
148 $this->exit('status', ['status' => $error], $format);
152 * Outputs formatted data according to the data type and then exits the execution.
154 * @param string $root_element
155 * @param array $data An array with a single element containing the returned result
156 * @param string|null $format Output format (xml, json, rss, atom)
160 public function exit(string $root_element, array $data, string $format = null)
162 $format = $format ?? 'json';
164 $return = $this->formatData($root_element, $format, $data);
168 header('Content-Type: text/xml');
171 header('Content-Type: application/json');
172 if (!empty($return)) {
173 $json = json_encode(end($return));
174 if (!empty($_GET['callback'])) {
175 $json = $_GET['callback'] . '(' . $json . ')';
181 header('Content-Type: application/rss+xml');
182 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
185 header('Content-Type: application/atom+xml');
186 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
195 * Quit execution with the message that the endpoint isn't implemented
197 * @param string $method
202 public function unsupported(string $method = 'all')
204 $path = $this->args->getQueryString();
205 $this->logger->info('Unimplemented API call',
209 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
210 'request' => HTTPInputData::process()
212 $error = $this->l10n->t('API endpoint %s %s is not implemented', strtoupper($method), $path);
213 $error_description = $this->l10n->t('The API endpoint is currently not implemented but might be in the future.');
214 $errorobj = new Error($error, $error_description);
215 System::jsonError(501, $errorobj->toArray());