]> git.mxchange.org Git - friendica.git/blob - src/Module/Base/Api.php
Remove deprecated App::getHostName() - process methods to DI::baseUrl()->getHostName()
[friendica.git] / src / Module / Base / Api.php
1 <?php
2
3 namespace Friendica\Module\Base;
4
5 use Friendica\App\Arguments;
6 use Friendica\BaseModule;
7 use Friendica\Core\L10n;
8 use Friendica\DI;
9 use Friendica\Network\HTTPException;
10
11 require_once __DIR__ . '/../../../include/api.php';
12
13 class Api extends BaseModule
14 {
15         /**
16          * @var string json|xml|rss|atom
17          */
18         protected static $format = 'json';
19         /**
20          * @var bool|int
21          */
22         protected static $current_user_id;
23
24         public static function init(array $parameters = [])
25         {
26                 $arguments = DI::args();
27
28                 if (substr($arguments->getQueryString(), -4) === '.xml') {
29                         self::$format = 'xml';
30                 }
31                 if (substr($arguments->getQueryString(), -4) === '.rss') {
32                         self::$format = 'rss';
33                 }
34                 if (substr($arguments->getQueryString(), -4) === '.atom') {
35                         self::$format = 'atom';
36                 }
37         }
38
39         public static function post(array $parameters = [])
40         {
41                 if (!api_user()) {
42                         throw new HTTPException\UnauthorizedException(L10n::t('Permission denied.'));
43                 }
44
45                 $a = DI::app();
46
47                 if (!empty($a->user['uid']) && $a->user['uid'] != api_user()) {
48                         throw new HTTPException\ForbiddenException(L10n::t('Permission denied.'));
49                 }
50         }
51
52         /**
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
55          *
56          * @brief Login API user
57          *
58          * @return bool Was a user authenticated?
59          * @throws HTTPException\ForbiddenException
60          * @throws HTTPException\UnauthorizedException
61          * @throws HTTPException\InternalServerErrorException
62          * @hook  'authenticate'
63          *               array $addon_auth
64          *               'username' => username from login form
65          *               'password' => password from login form
66          *               'authenticated' => return status,
67          *               'user_record' => return authenticated user record
68          */
69         protected static function login()
70         {
71                 api_login(DI::app());
72
73                 self::$current_user_id = api_user();
74
75                 return (bool)self::$current_user_id;
76         }
77
78         /**
79          * @brief Get user info array.
80          *
81          * @param int|string $contact_id Contact ID or URL
82          * @return array|bool
83          * @throws HTTPException\BadRequestException
84          * @throws HTTPException\InternalServerErrorException
85          * @throws HTTPException\UnauthorizedException
86          * @throws \ImagickException
87          */
88         protected static function getUser($contact_id = null)
89         {
90                 return api_get_user(DI::app(), $contact_id);
91         }
92
93         protected static function format($root_element, $data)
94         {
95                 switch (self::$format) {
96                         case "atom":
97                         case "rss":
98                         case "xml":
99                                 $ret = api_create_xml($data, $root_element);
100                                 break;
101                         case "json":
102                         default:
103                                 $ret = $data;
104                                 break;
105                 }
106
107                 return $ret;
108         }
109 }