- /**
- * Get user info array.
- *
- * @param int|string $contact_id Contact ID or URL
- * @return array|bool
- * @throws HTTPException\BadRequestException
- * @throws HTTPException\InternalServerErrorException
- * @throws HTTPException\UnauthorizedException
- * @throws \ImagickException
- */
- protected static function getUser($contact_id = null)
- {
- return api_get_user($contact_id);
- }
-
- /**
- * Exit with error code
- *
- * @param int $code
- * @param string $description
- * @param string $message
- * @param string|null $format
- * @return void
- */
- public static function error(int $code, string $description, string $message, string $format = null)
- {
- $error = [
- 'error' => $message ?: $description,
- 'code' => $code . ' ' . $description,
- 'request' => DI::args()->getQueryString()
- ];
-
- header(($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.1') . ' ' . $code . ' ' . $description);
-
- self::exit('status', ['status' => $error], $format);
- }
-
- /**
- * Outputs formatted data according to the data type and then exits the execution.
- *
- * @param string $root_element
- * @param array $data An array with a single element containing the returned result
- * @param string $format Output format (xml, json, rss, atom)
- * @return false|string
- */
- protected static function exit(string $root_element, array $data, string $format = null)
- {
- $format = $format ?? 'json';
-
- $return = self::formatData($root_element, $format, $data);
-
- switch ($format) {
- case 'xml':
- header('Content-Type: text/xml');
- break;
- case 'json':
- header('Content-Type: application/json');
- if (!empty($return)) {
- $json = json_encode(end($return));
- if (!empty($_GET['callback'])) {
- $json = $_GET['callback'] . '(' . $json . ')';
- }
- $return = $json;
- }
- break;
- case 'rss':
- header('Content-Type: application/rss+xml');
- $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
- break;
- case 'atom':
- header('Content-Type: application/atom+xml');
- $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
- break;
- }
-
- echo $return;
- exit;
- }
-
- /**
- * Formats the data according to the data type
- *
- * @param string $root_element Name of the root element
- * @param string $type Return type (atom, rss, xml, json)
- * @param array $data JSON style array
- *
- * @return array|string (string|array) XML data or JSON data
- */
- public static function formatData($root_element, string $type, array $data)
- {
- switch ($type) {
- case 'atom':
- case 'rss':
- case 'xml':
- $ret = self::createXML($data, $root_element);
- break;
- case 'json':
- default:
- $ret = $data;
- break;
- }
- return $ret;
- }
-
- /**
- * Callback function to transform the array in an array that can be transformed in a XML file
- *
- * @param mixed $item Array item value
- * @param string $key Array key
- *
- * @return boolean
- */
- public static function reformatXML(&$item, &$key)