3 namespace Friendica\Module\Api;
5 use Friendica\App\Arguments;
6 use Friendica\Core\L10n;
7 use Friendica\Module\BaseApi;
8 use Friendica\Util\Arrays;
9 use Friendica\Util\HTTPInputData;
10 use Friendica\Util\XML;
11 use Psr\Log\LoggerInterface;
14 * This class is used to format and return API responses
22 /** @var LoggerInterface */
27 * @param Arguments $args
28 * @param LoggerInterface $logger
30 public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger)
34 $this->logger = $logger;
38 * Sets header directly
39 * mainly used to override it for tests
41 * @param string $header
43 protected function setHeader(string $header)
49 * Prints output directly to the caller
50 * mainly used to override it for tests
52 * @param string $output
54 protected function printOutput(string $output)
61 * Creates the XML from a JSON style array
63 * @param array $data JSON style array
64 * @param string $root_element Name of the root element
66 * @return string The XML data
68 public function createXML(array $data, string $root_element): string
70 $childname = key($data);
71 $data2 = array_pop($data);
74 '' => 'http://api.twitter.com',
75 'statusnet' => 'http://status.net/schema/api/1/',
76 'friendica' => 'http://friendi.ca/schema/api/1/',
77 'georss' => 'http://www.georss.org/georss'
80 /// @todo Auto detection of needed namespaces
81 if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) {
85 if (is_array($data2)) {
87 Arrays::walkRecursive($data2, ['Friendica\Module\Api\ApiResponse', 'reformatXML']);
93 foreach ($data2 as $item) {
94 $data4[$i++ . ':' . $childname] = $item;
101 $data3 = [$root_element => $data2];
103 return XML::fromArray($data3, $xml, false, $namespaces);
107 * Formats the data according to the data type
109 * @param string $root_element Name of the root element
110 * @param string $type Return type (atom, rss, xml, json)
111 * @param array $data JSON style array
112 * @param int $cid ID of the contact for RSS
114 * @return array|string (string|array) XML data or JSON data
116 public function formatData(string $root_element, string $type, array $data, int $cid = 0)
120 $data = BaseApi::addRSSValues($data, $cid);
123 return $this->createXML($data, $root_element);
131 * Callback function to transform the array in an array that can be transformed in a XML file
133 * @param mixed $item Array item value
134 * @param string $key Array key
138 public static function reformatXML(&$item, string &$key): bool
140 if (is_bool($item)) {
141 $item = ($item ? 'true' : 'false');
144 if (substr($key, 0, 10) == 'statusnet_') {
145 $key = 'statusnet:' . substr($key, 10);
146 } elseif (substr($key, 0, 10) == 'friendica_') {
147 $key = 'friendica:' . substr($key, 10);
153 * Exit with error code
156 * @param string $description
157 * @param string $message
158 * @param string|null $format
162 public function error(int $code, string $description, string $message, string $format = null)
165 'error' => $message ?: $description,
166 'code' => $code . ' ' . $description,
167 'request' => $this->args->getQueryString()
170 $this->setHeader(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
172 $this->exit('status', ['status' => $error], $format);
176 * Outputs formatted data according to the data type and then exits the execution.
178 * @param string $root_element
179 * @param array $data An array with a single element containing the returned result
180 * @param string|null $format Output format (xml, json, rss, atom)
184 public function exit(string $root_element, array $data, string $format = null)
186 $format = $format ?? 'json';
188 $return = $this->formatData($root_element, $format, $data);
192 $this->setHeader('Content-Type: text/xml');
195 $this->setHeader('Content-Type: application/json');
196 if (!empty($return)) {
197 $json = json_encode(end($return));
198 if (!empty($_GET['callback'])) {
199 $json = $_GET['callback'] . '(' . $json . ')';
205 $this->setHeader('Content-Type: application/rss+xml');
208 $this->setHeader('Content-Type: application/atom+xml');
212 $this->printOutput($return);
216 * Quit execution with the message that the endpoint isn't implemented
218 * @param string $method
223 public function unsupported(string $method = 'all')
225 $path = $this->args->getQueryString();
226 $this->logger->info('Unimplemented API call',
230 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
231 'request' => HTTPInputData::process()
233 $error = $this->l10n->t('API endpoint %s %s is not implemented', strtoupper($method), $path);
234 $error_description = $this->l10n->t('The API endpoint is currently not implemented but might be in the future.');
236 $this->exit('error', ['error' => ['error' => $error, 'error_description' => $error_description]]);