Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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