3 namespace Friendica\Module\Base;
5 use Friendica\App\Arguments;
6 use Friendica\BaseModule;
7 use Friendica\Core\L10n;
8 use Friendica\Network\HTTPException;
10 require_once __DIR__ . '/../../../include/api.php';
12 class Api extends BaseModule
15 * @var string json|xml|rss|atom
17 protected static $format = 'json';
21 protected static $current_user_id;
23 public static function init(array $parameters = [])
25 $Arguments = self::getClass(Arguments::class);
27 if (substr($Arguments->getQueryString(), -4) === '.xml') {
28 self::$format = 'xml';
30 if (substr($Arguments->getQueryString(), -4) === '.rss') {
31 self::$format = 'rss';
33 if (substr($Arguments->getQueryString(), -4) === '.atom') {
34 self::$format = 'atom';
38 public static function post(array $parameters = [])
41 throw new HTTPException\UnauthorizedException(L10n::t('Permission denied.'));
46 if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) {
47 throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
52 * Log in user via OAuth1 or Simple HTTP Auth.
53 * Simple Auth allow username in form of <pre>user@server</pre>, ignoring server part
55 * @brief Login API user
57 * @return bool Was a user authenticated?
58 * @throws HTTPException\ForbiddenException
59 * @throws HTTPException\UnauthorizedException
60 * @throws HTTPException\InternalServerErrorException
61 * @hook 'authenticate'
63 * 'username' => username from login form
64 * 'password' => password from login form
65 * 'authenticated' => return status,
66 * 'user_record' => return authenticated user record
68 protected static function login()
70 api_login(self::getApp());
72 self::$current_user_id = api_user();
74 return (bool)self::$current_user_id;
78 * @brief Get user info array.
80 * @param int|string $contact_id Contact ID or URL
82 * @throws HTTPException\BadRequestException
83 * @throws HTTPException\InternalServerErrorException
84 * @throws HTTPException\UnauthorizedException
85 * @throws \ImagickException
87 protected static function getUser($contact_id = null)
89 return api_get_user(self::getApp(), $contact_id);
92 protected static function format($root_element, $data)
94 switch (self::$format) {
98 $ret = api_create_xml($data, $root_element);