3 namespace Friendica\Module\Api;
5 use Friendica\App\Arguments;
6 use Friendica\Core\L10n;
7 use Friendica\Util\Arrays;
8 use Friendica\Util\HTTPInputData;
9 use Friendica\Util\XML;
10 use Psr\Log\LoggerInterface;
13 * This class is used to format and return API responses
21 /** @var LoggerInterface */
26 * @param Arguments $args
27 * @param LoggerInterface $logger
29 public function __construct(L10n $l10n, Arguments $args, LoggerInterface $logger)
33 $this->logger = $logger;
37 * Sets header directly
38 * mainly used to override it for tests
40 * @param string $header
42 protected function setHeader(string $header)
48 * Prints output directly to the caller
49 * mainly used to override it for tests
51 * @param string $output
53 protected function printOutput(string $output)
60 * Creates the XML from a JSON style array
62 * @param array $data JSON style array
63 * @param string $root_element Name of the root element
65 * @return string The XML data
67 public function createXML(array $data, string $root_element): string
69 $childname = key($data);
70 $data2 = array_pop($data);
73 '' => 'http://api.twitter.com',
74 'statusnet' => 'http://status.net/schema/api/1/',
75 'friendica' => 'http://friendi.ca/schema/api/1/',
76 'georss' => 'http://www.georss.org/georss'
79 /// @todo Auto detection of needed namespaces
80 if (in_array($root_element, ['ok', 'hash', 'config', 'version', 'ids', 'notes', 'photos'])) {
84 if (is_array($data2)) {
86 Arrays::walkRecursive($data2, ['Friendica\Module\Api\ApiResponse', 'reformatXML']);
92 foreach ($data2 as $item) {
93 $data4[$i++ . ':' . $childname] = $item;
100 $data3 = [$root_element => $data2];
102 return XML::fromArray($data3, $xml, false, $namespaces);
106 * Formats the data according to the data type
108 * @param string $root_element Name of the root element
109 * @param string $type Return type (atom, rss, xml, json)
110 * @param array $data JSON style array
112 * @return array|string (string|array) XML data or JSON data
114 public function formatData(string $root_element, string $type, array $data)
120 return $this->createXML($data, $root_element);
128 * Callback function to transform the array in an array that can be transformed in a XML file
130 * @param mixed $item Array item value
131 * @param string $key Array key
135 public static function reformatXML(&$item, string &$key): bool
137 if (is_bool($item)) {
138 $item = ($item ? 'true' : 'false');
141 if (substr($key, 0, 10) == 'statusnet_') {
142 $key = 'statusnet:' . substr($key, 10);
143 } elseif (substr($key, 0, 10) == 'friendica_') {
144 $key = 'friendica:' . substr($key, 10);
150 * Exit with error code
153 * @param string $description
154 * @param string $message
155 * @param string|null $format
159 public function error(int $code, string $description, string $message, string $format = null)
162 'error' => $message ?: $description,
163 'code' => $code . ' ' . $description,
164 'request' => $this->args->getQueryString()
167 $this->setHeader(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
169 $this->exit('status', ['status' => $error], $format);
173 * Outputs formatted data according to the data type and then exits the execution.
175 * @param string $root_element
176 * @param array $data An array with a single element containing the returned result
177 * @param string|null $format Output format (xml, json, rss, atom)
181 public function exit(string $root_element, array $data, string $format = null)
183 $format = $format ?? 'json';
185 $return = $this->formatData($root_element, $format, $data);
189 $this->setHeader('Content-Type: text/xml');
192 $this->setHeader('Content-Type: application/json');
193 if (!empty($return)) {
194 $json = json_encode(end($return));
195 if (!empty($_GET['callback'])) {
196 $json = $_GET['callback'] . '(' . $json . ')';
202 $this->setHeader('Content-Type: application/rss+xml');
205 $this->setHeader('Content-Type: application/atom+xml');
209 $this->printOutput($return);
213 * Quit execution with the message that the endpoint isn't implemented
215 * @param string $method
220 public function unsupported(string $method = 'all')
222 $path = $this->args->getQueryString();
223 $this->logger->info('Unimplemented API call',
227 'agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
228 'request' => HTTPInputData::process()
230 $error = $this->l10n->t('API endpoint %s %s is not implemented', strtoupper($method), $path);
231 $error_description = $this->l10n->t('The API endpoint is currently not implemented but might be in the future.');
233 $this->exit('error', ['error' => ['error' => $error, 'error_description' => $error_description]]);