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