]> git.mxchange.org Git - friendica.git/blob - mod/api.php
oauthapi: authorize app
[friendica.git] / mod / api.php
1 <?php
2
3 require_once('include/api.php');
4
5 function oauth_get_client(){
6         // get consumer/client from request token
7         try {
8                 $request = OAuthRequest::from_request();
9         } catch(Exception $e) {
10                 echo "<pre>"; var_dump($e); killme();
11         }
12         
13         $params = $request->get_parameters();
14         $token = $params['oauth_token'];
15         
16         $r = q("SELECT `clients`.* 
17                         FROM `clients`, `tokens` 
18                         WHERE `clients`.`client_id`=`tokens`.`client_id` 
19                         AND `tokens`.`id`='%s' AND `tokens`.`scope`='request'",
20                         dbesc($token));
21
22         if (!count($r))
23                 return null;
24         
25         return $r[0];
26 }
27
28 function api_post(&$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
42 function api_content(&$a) {
43         if ($a->cmd=='api/oauth/authorize'){
44                 /* 
45                  * api/oauth/authorize interact with the user. return a standard page
46                  */
47                 
48                 
49                 if (x($_POST,'oauth_yes')){
50                 
51                 
52                         $app = oauth_get_client();
53                         if (is_null($app)) return "Invalid request. Unknown token.";
54                         $consumer = new OAuthConsumer($app['key'], $app['secret']);
55                         
56                         // Rev A change
57                         $request = OAuthRequest::from_request();
58                         $callback = $request->get_parameter('oauth_callback');
59                         $datastore = new FKOAuthDataStore();
60                         $new_token = $datastore->new_request_token($consumer, $callback);
61                         
62                         $tpl = get_markup_template("oauth_authorize_done.tpl");
63                         $o = replace_macros($tpl, array(
64                                 '$title' => t('Authorize application connection'),
65                                 '$info' => t('Return to your app and insert this Securty Code:'),
66                                 '$code' => $new_token->key,
67                         ));
68                 
69                         return $o;
70                 
71                 
72                 }
73         
74                 
75                 
76                 if(! local_user()) {
77                         //TODO: we need login form to redirect to this page
78                         notice( t('Please login to continue.') . EOL );
79                         return login(false);
80                 }
81                 
82                 $app = oauth_get_client();
83                 if (is_null($app)) return "Invalid request. Unknown token.";
84                 
85                 
86                 $tpl = get_markup_template('oauth_authorize.tpl');
87                 $o = replace_macros($tpl, array(
88                         '$title' => t('Authorize application connection'),
89                         '$app' => $app,
90                         '$authorize' => t('Do you want to authorize this application to access your posts and contacts, and/or create new posts for you?'),
91                         '$yes'  => t('Yes'),
92                         '$no'   => t('No'),
93                 ));
94                 
95                 //echo "<pre>"; var_dump($app); killme();
96                 
97                 return $o;
98         }
99         
100         echo api_call($a);
101         killme();
102 }
103
104
105