4 use Friendica\Core\Config;
5 use Friendica\Database\DBM;
7 require_once('include/api.php');
9 function oauth_get_client($request){
12 $params = $request->get_parameters();
13 $token = $params['oauth_token'];
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'",
21 if (!DBM::is_result($r))
27 function api_post(App $a) {
30 notice( t('Permission denied.') . EOL);
34 if(count($a->user) && x($a->user,'uid') && $a->user['uid'] != local_user()) {
35 notice( t('Permission denied.') . EOL);
41 function api_content(App $a) {
42 if ($a->cmd=='api/oauth/authorize'){
44 * api/oauth/authorize interact with the user. return a standard page
47 $a->page['template'] = "minimal";
50 // get consumer/client from request token
52 $request = OAuthRequest::from_request();
53 } catch(Exception $e) {
54 echo "<pre>"; var_dump($e); killme();
58 if (x($_POST,'oauth_yes')){
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']);
64 $verifier = md5($app['secret'].local_user());
65 Config::set("oauth", $verifier, local_user());
68 if ($consumer->callback_url!=null) {
69 $params = $request->get_parameters();
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));
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:'),
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());
96 //FKOAuth1::loginUser(4);
98 $app = oauth_get_client($request);
99 if (is_null($app)) return "Invalid request. Unknown token.";
104 $tpl = get_markup_template('oauth_authorize.tpl');
105 $o = replace_macros($tpl, array(
106 '$title' => t('Authorize application connection'),
108 '$authorize' => t('Do you want to authorize this application to access your posts and contacts, and/or create new posts for you?'),