Issue 8254: Length restriction for "title" and "uri"
[friendica.git/.git] / mod / api.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\Renderer;
24 use Friendica\Database\DBA;
25 use Friendica\DI;
26 use Friendica\Module\Security\Login;
27
28 require_once __DIR__ . '/../include/api.php';
29
30 function oauth_get_client(OAuthRequest $request)
31 {
32         $params = $request->get_parameters();
33         $token = $params['oauth_token'];
34
35         $r = q("SELECT `clients`.*
36                         FROM `clients`, `tokens`
37                         WHERE `clients`.`client_id`=`tokens`.`client_id`
38                         AND `tokens`.`id`='%s' AND `tokens`.`scope`='request'", DBA::escape($token));
39
40         if (!DBA::isResult($r)) {
41                 return null;
42         }
43
44         return $r[0];
45 }
46
47 function api_post(App $a)
48 {
49         if (!local_user()) {
50                 notice(DI::l10n()->t('Permission denied.') . EOL);
51                 return;
52         }
53
54         if (count($a->user) && !empty($a->user['uid']) && $a->user['uid'] != local_user()) {
55                 notice(DI::l10n()->t('Permission denied.') . EOL);
56                 return;
57         }
58 }
59
60 function api_content(App $a)
61 {
62         if (DI::args()->getCommand() == 'api/oauth/authorize') {
63                 /*
64                  * api/oauth/authorize interact with the user. return a standard page
65                  */
66
67                 DI::page()['template'] = "minimal";
68
69                 // get consumer/client from request token
70                 try {
71                         $request = OAuthRequest::from_request();
72                 } catch (Exception $e) {
73                         echo "<pre>";
74                         var_dump($e);
75                         exit();
76                 }
77
78                 if (!empty($_POST['oauth_yes'])) {
79                         $app = oauth_get_client($request);
80                         if (is_null($app)) {
81                                 return "Invalid request. Unknown token.";
82                         }
83                         $consumer = new OAuthConsumer($app['client_id'], $app['pw'], $app['redirect_uri']);
84
85                         $verifier = md5($app['secret'] . local_user());
86                         DI::config()->set("oauth", $verifier, local_user());
87
88                         if ($consumer->callback_url != null) {
89                                 $params = $request->get_parameters();
90                                 $glue = "?";
91                                 if (strstr($consumer->callback_url, $glue)) {
92                                         $glue = "?";
93                                 }
94                                 DI::baseUrl()->redirect($consumer->callback_url . $glue . 'oauth_token=' . OAuthUtil::urlencode_rfc3986($params['oauth_token']) . '&oauth_verifier=' . OAuthUtil::urlencode_rfc3986($verifier));
95                                 exit();
96                         }
97
98                         $tpl = Renderer::getMarkupTemplate("oauth_authorize_done.tpl");
99                         $o = Renderer::replaceMacros($tpl, [
100                                 '$title' => DI::l10n()->t('Authorize application connection'),
101                                 '$info' => DI::l10n()->t('Return to your app and insert this Securty Code:'),
102                                 '$code' => $verifier,
103                         ]);
104
105                         return $o;
106                 }
107
108                 if (!local_user()) {
109                         /// @TODO We need login form to redirect to this page
110                         notice(DI::l10n()->t('Please login to continue.') . EOL);
111                         return Login::form(DI::args()->getQueryString(), false, $request->get_parameters());
112                 }
113                 //FKOAuth1::loginUser(4);
114
115                 $app = oauth_get_client($request);
116                 if (is_null($app)) {
117                         return "Invalid request. Unknown token.";
118                 }
119
120                 $tpl = Renderer::getMarkupTemplate('oauth_authorize.tpl');
121                 $o = Renderer::replaceMacros($tpl, [
122                         '$title' => DI::l10n()->t('Authorize application connection'),
123                         '$app' => $app,
124                         '$authorize' => DI::l10n()->t('Do you want to authorize this application to access your posts and contacts, and/or create new posts for you?'),
125                         '$yes' => DI::l10n()->t('Yes'),
126                         '$no' => DI::l10n()->t('No'),
127                 ]);
128
129                 return $o;
130         }
131
132         echo api_call($a);
133         exit();
134 }