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