]> git.mxchange.org Git - friendica.git/blob - src/Module/BaseApi.php
Merge pull request #8163 from MrPetovan/task/7817-custom-fields-part-3
[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         /**
91          * Formats the data according to the data type
92          *
93          * @param string $root_element
94          * @param array $data An array with a single element containing the returned result
95          * @return false|string
96          */
97         protected static function format(string $root_element, array $data)
98         {
99                 $return = api_format_data($root_element, self::$format, $data);
100
101                 switch (self::$format) {
102                         case "xml":
103                                 header("Content-Type: text/xml");
104                                 break;
105                         case "json":
106                                 header("Content-Type: application/json");
107                                 if (!empty($return)) {
108                                         $json = json_encode(end($return));
109                                         if (!empty($_GET['callback'])) {
110                                                 $json = $_GET['callback'] . "(" . $json . ")";
111                                         }
112                                         $return = $json;
113                                 }
114                                 break;
115                         case "rss":
116                                 header("Content-Type: application/rss+xml");
117                                 $return  = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
118                                 break;
119                         case "atom":
120                                 header("Content-Type: application/atom+xml");
121                                 $return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
122                                 break;
123                 }
124                 
125                 return $return;
126         }
127
128         /**
129          * Creates the XML from a JSON style array
130          *
131          * @param $data
132          * @param $root_element
133          * @return string
134          */
135         protected static function createXml($data, $root_element)
136         {
137                 return api_create_xml($data, $root_element);
138         }
139 }