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