forked from ydn/yahoo-oauth-popup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
146 lines (125 loc) · 4.6 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
// 'THE MODEL'
require("config.inc.php");
require("OAuth/OAuth.php");
require("Yahoo/YahooOAuthApplication.class.php");
$app = new YahooOAuthApplication(CONSUMER_KEY, CONSUMER_SECRET, APP_ID);
if(isset($_GET['logout'])) {
// if a session exists and the logout flag is detected
// clear the session tokens and reload the page.
// YahooSession::clearSession();
oauth_unset_cookie('yos-social-rt');
oauth_unset_cookie('yos-social-at');
header("Location: /apps/popup/index.php");
}
if(array_key_exists("in_popup", $_GET)) {
// print_r($_COOKIE);
$request_token = oauth_get_cookie('yos-social-rt');
$app->token = $app->getAccessToken($request_token, $_GET['oauth_verifier']);
$app->token->expires = 'foobar';
oauth_set_cookie('yos-social-at', $app->token, $app->token->expires_in);
close_popup();
exit;
} else {
$token = oauth_get_cookie('yos-social-at');
if($token && isset($token->yahoo_guid)) {
// set the token in the SDK
$app->token = $token;
// do it!
$profile = $app->getProfile($token->yahoo_guid)->profile;
if(isset($_GET['update'])) {
$update = $app->insertUpdate(array(
'title' => "cloned the yos-social-php5 SDK on Github",
'description' => "A PHP5 SDK for YQL",
'link' => "http://github.com/yahoo/yos-social-php5",
'imgURL' => 'http://github.com/images/modules/header/logov3.png',
'imgWidth' => '100',
'imgHeight' => '45'
));
}
} else {
$callback_params = array('in_popup' => true);
$callback = sprintf("%s://%s%s?%s", ($_SERVER["HTTPS"] == 'on') ? 'https' : 'http', $_SERVER["HTTP_HOST"], $_SERVER["REQUEST_URI"], http_build_query($callback_params));
$request_token = $app->getRequestToken($callback);
oauth_set_cookie('yos-social-rt', $request_token, $request_token->expires_in);
$auth_url = $app->getAuthorizationUrl($request_token);
}
}
function close_popup()
{
?>
<script type="text/javascript">
window.close();
</script>
<?
}
function oauth_get_cookie($name)
{
return unserialize(base64_decode($_COOKIE[$name]));
}
function oauth_set_cookie($name, $data, $expires = 3600)
{
setcookie($name, base64_encode(serialize($data)), time() + $expires);
}
function oauth_unset_cookie($name)
{
setcookie($name, '', time()-600);
}
// NOW THE 'VIEW'
?>
<!DOCTYPE html>
<html>
<head>
<title>Yahoo! OAuth Example</title>
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.7.0/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="js/popupmanager.js"></script>
<!-- Combo-handled YUI CSS files: -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.7.0/build/reset-fonts-grids/reset-fonts-grids.css&2.7.0/build/base/base-min.css">
</head>
<body>
<?php
if(isset($token) && isset($profile)) {
// if a session does exist and the profile data was
// fetched without error, print out a simple usercard.
printf("<img src=\"%s\"/><p><h2>Hi <a href=\"%s\" target=\"_blank\">%s!</a></h2></p>\n",
$profile->image->imageUrl, $profile->profileUrl, $profile->nickname);
if($profile->status->message != "") {
$statusDate = date('F j, y, g:i a', strtotime($profile->status->lastStatusModified));
printf("<p><strong>“</strong>%s<strong>”</strong> on %s</p>",
$profile->status->message, $statusDate);
}
if(isset($update)) {
print '<p>';
print_r($update);
print '</p>';
}
print "<button id='uptBtn'>Post Update</button>";
print "<p><a href=\"?logout\">Logout</a></p>";
} else {
// if a session does not exist, output the
// login / share button linked to the auth_url.
printf("<a href=\"%s\" id=\"yloginLink\"><img src=\"http://l.yimg.com/a/i/ydn/social/updt-spurp.png\"></a>\n", $auth_url);
}
?>
<script type="text/javascript">
var _gel = function(el) {return document.getElementById(el)};
YAHOO.util.Event.onDOMReady(function() {
if(_gel('yloginLink')) {
YAHOO.util.Event.addListener("yloginLink", "click", function(event) {
// block the url from opening like normal
YAHOO.util.Event.preventDefault(event);
var auth_url = _gel("yloginLink").href;
// open pop-up using the auth_url
PopupManager.open(auth_url,600,435);
});
}
if(_gel('uptBtn')) {
YAHOO.util.Event.addListener('uptBtn', "click", function(event) {
document.location = document.location+'?update';
});
}
});
</script>
</body>
</html>