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 * Sets header directly
40 * mainly used to override it for tests
42 * @param string $header
44 protected function setHeader(string $header)
50 * Prints output directly to the caller
51 * mainly used to override it for tests
53 * @param string $output
55 protected function printOutput(string $output)
62 * Creates the XML from a JSON style array
64 * @param array $data JSON style array
65 * @param string $root_element Name of the root element
67 * @return string The XML data
69 public function createXML(array $data, string $root_element): string
71 $childname = key($data);
72 $data2 = array_pop($data);
75 '' => 'http://api.twitter.com',
76 'statusnet' => 'http://status.net/schema/api/1/',
77 'friendica' => 'http://friendi.ca/schema/api/1/',
78 'georss' => 'http://www.georss.org/georss'
81 /// @todo Auto detection of needed namespaces
82 if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) {
86 if (is_array($data2)) {
88 Arrays::walkRecursive($data2, ['Friendica\Module\Api\ApiResponse', 'reformatXML']);
94 foreach ($data2 as $item) {
95 $data4[$i++ . ':' . $childname] = $item;
102 $data3 = [$root_element => $data2];
104 return XML::fromArray($data3, $xml, false, $namespaces);
108 * Formats the data according to the data type
110 * @param string $root_element Name of the root element
111 * @param string $type Return type (atom, rss, xml, json)
112 * @param array $data JSON style array
114 * @return array|string (string|array) XML data or JSON data
116 public function formatData(string $root_element, string $type, array $data)
122 return $this->createXML($data, $root_element);
130 * Callback function to transform the array in an array that can be transformed in a XML file
132 * @param mixed $item Array item value
133 * @param string $key Array key
137 public static function reformatXML(&$item, string &$key): bool
139 if (is_bool($item)) {
140 $item = ($item ? 'true' : 'false');
143 if (substr($key, 0, 10) == 'statusnet_') {
144 $key = 'statusnet:' . substr($key, 10);
145 } elseif (substr($key, 0, 10) == 'friendica_') {
146 $key = 'friendica:' . substr($key, 10);
152 * Exit with error code
155 * @param string $description
156 * @param string $message
157 * @param string|null $format
161 public function error(int $code, string $description, string $message, string $format = null)
164 'error' => $message ?: $description,
165 'code' => $code . ' ' . $description,
166 'request' => $this->args->getQueryString()
169 $this->setHeader(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
171 $this->exit('status', ['status' => $error], $format);
175 * Outputs formatted data according to the data type and then exits the execution.
177 * @param string $root_element
178 * @param array $data An array with a single element containing the returned result
179 * @param string|null $format Output format (xml, json, rss, atom)
183 public function exit(string $root_element, array $data, string $format = null)
185 $format = $format ?? 'json';
187 $return = $this->formatData($root_element, $format, $data);
191 $this->setHeader('Content-Type: text/xml');
194 $this->setHeader('Content-Type: application/json');
195 if (!empty($return)) {
196 $json = json_encode(end($return));
197 if (!empty($_GET['callback'])) {
198 $json = $_GET['callback'] . '(' . $json . ')';
204 $this->setHeader('Content-Type: application/rss+xml');
205 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
208 $this->setHeader('Content-Type: application/atom+xml');
209 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
213 $this->printOutput($return);
217 * Quit execution with the message that the endpoint isn't implemented
219 * @param string $method
224 public function unsupported(string $method = 'all')
226 $path = $this->args->getQueryString();
227 $this->logger->info('Unimplemented API call',
231 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
232 'request' => HTTPInputData::process()
234 $error = $this->l10n->t('API endpoint %s %s is not implemented', strtoupper($method), $path);
235 $error_description = $this->l10n->t('The API endpoint is currently not implemented but might be in the future.');
236 $errorobj = new Error($error, $error_description);
237 System::jsonError(501, $errorobj->toArray());