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