3 namespace Friendica\Module\Base;
5 use Friendica\App\Arguments;
6 use Friendica\BaseModule;
7 use Friendica\Core\L10n;
9 use Friendica\Network\HTTPException;
11 require_once __DIR__ . '/../../../include/api.php';
13 class Api extends BaseModule
16 * @var string json|xml|rss|atom
18 protected static $format = 'json';
22 protected static $current_user_id;
24 public static function init(array $parameters = [])
26 $arguments = DI::args();
28 if (substr($arguments->getQueryString(), -4) === '.xml') {
29 self::$format = 'xml';
31 if (substr($arguments->getQueryString(), -4) === '.rss') {
32 self::$format = 'rss';
34 if (substr($arguments->getQueryString(), -4) === '.atom') {
35 self::$format = 'atom';
39 public static function post(array $parameters = [])
42 throw new HTTPException\UnauthorizedException(L10n::t('Permission denied.'));
47 if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) {
48 throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
53 * Log in user via OAuth1 or Simple HTTP Auth.
54 * Simple Auth allow username in form of <pre>user@server</pre>, ignoring server part
56 * @brief Login API user
58 * @return bool Was a user authenticated?
59 * @throws HTTPException\ForbiddenException
60 * @throws HTTPException\UnauthorizedException
61 * @throws HTTPException\InternalServerErrorException
62 * @hook 'authenticate'
64 * 'username' => username from login form
65 * 'password' => password from login form
66 * 'authenticated' => return status,
67 * 'user_record' => return authenticated user record
69 protected static function login()
73 self::$current_user_id = api_user();
75 return (bool)self::$current_user_id;
79 * @brief Get user info array.
81 * @param int|string $contact_id Contact ID or URL
83 * @throws HTTPException\BadRequestException
84 * @throws HTTPException\InternalServerErrorException
85 * @throws HTTPException\UnauthorizedException
86 * @throws \ImagickException
88 protected static function getUser($contact_id = null)
90 return api_get_user(DI::app(), $contact_id);
93 protected static function format($root_element, $data)
95 switch (self::$format) {
99 $ret = api_create_xml($data, $root_element);