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