]> git.mxchange.org Git - friendica.git/blob - mod/api.php
Merge pull request #4123 from Rudloff/feature/ruleset
[friendica.git] / mod / api.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
6 use Friendica\Module\Login;
7
8 require_once('include/api.php');
9
10 function oauth_get_client($request)
11 {
12
13
14         $params = $request->get_parameters();
15         $token = $params['oauth_token'];
16
17         $r = q("SELECT `clients`.*
18                         FROM `clients`, `tokens`
19                         WHERE `clients`.`client_id`=`tokens`.`client_id`
20                         AND `tokens`.`id`='%s' AND `tokens`.`scope`='request'", dbesc($token));
21
22         if (!DBM::is_result($r))
23                 return null;
24
25         return $r[0];
26 }
27
28 function api_post(App $a)
29 {
30         if (!local_user()) {
31                 notice(t('Permission denied.') . EOL);
32                 return;
33         }
34
35         if (count($a->user) && x($a->user, 'uid') && $a->user['uid'] != local_user()) {
36                 notice(t('Permission denied.') . EOL);
37                 return;
38         }
39 }
40
41 function api_content(App $a)
42 {
43         if ($a->cmd == 'api/oauth/authorize') {
44                 /*
45                  * api/oauth/authorize interact with the user. return a standard page
46                  */
47
48                 $a->page['template'] = "minimal";
49
50                 // get consumer/client from request token
51                 try {
52                         $request = OAuthRequest::from_request();
53                 } catch (Exception $e) {
54                         echo "<pre>";
55                         var_dump($e);
56                         killme();
57                 }
58
59                 if (x($_POST, 'oauth_yes')) {
60                         $app = oauth_get_client($request);
61                         if (is_null($app)) {
62                                 return "Invalid request. Unknown token.";
63                         }
64                         $consumer = new OAuthConsumer($app['client_id'], $app['pw'], $app['redirect_uri']);
65
66                         $verifier = md5($app['secret'] . local_user());
67                         Config::set("oauth", $verifier, local_user());
68
69                         if ($consumer->callback_url != null) {
70                                 $params = $request->get_parameters();
71                                 $glue = "?";
72                                 if (strstr($consumer->callback_url, $glue)) {
73                                         $glue = "?";
74                                 }
75                                 goaway($consumer->callback_url . $glue . "oauth_token=" . OAuthUtil::urlencode_rfc3986($params['oauth_token']) . "&oauth_verifier=" . OAuthUtil::urlencode_rfc3986($verifier));
76                                 killme();
77                         }
78
79                         $tpl = get_markup_template("oauth_authorize_done.tpl");
80                         $o = replace_macros($tpl, array(
81                                 '$title' => t('Authorize application connection'),
82                                 '$info' => t('Return to your app and insert this Securty Code:'),
83                                 '$code' => $verifier,
84                         ));
85
86                         return $o;
87                 }
88
89                 if (!local_user()) {
90                         /// @TODO We need login form to redirect to this page
91                         notice(t('Please login to continue.') . EOL);
92                         return Login::form($a->query_string, false, $request->get_parameters());
93                 }
94                 //FKOAuth1::loginUser(4);
95
96                 $app = oauth_get_client($request);
97                 if (is_null($app)) {
98                         return "Invalid request. Unknown token.";
99                 }
100
101                 $tpl = get_markup_template('oauth_authorize.tpl');
102                 $o = replace_macros($tpl, array(
103                         '$title' => t('Authorize application connection'),
104                         '$app' => $app,
105                         '$authorize' => t('Do you want to authorize this application to access your posts and contacts, and/or create new posts for you?'),
106                         '$yes' => t('Yes'),
107                         '$no' => t('No'),
108                 ));
109
110                 return $o;
111         }
112
113         echo api_call($a);
114         killme();
115 }