Skip to content
Newer
Older
100644 238 lines (192 sloc) 7.18 KB
4f9b238 @zagraves initial commit
zagraves authored Sep 22, 2009
1 <?php
e54b9da @zagraves added license headers
zagraves authored Sep 22, 2009
2 /**
3 * Yahoo! Updates Wordpress Plugin
4 *
5 * Find documentation and support on Yahoo! Developer Network: http://developer.yahoo.com
6 *
7 * Hosted on GitHub: http://github.com/yahoo/yos-updates-wordpress/tree/master
8 *
9 * @package yos-updates-wordpress
10 * @subpackage yahoo
11 *
2428313 @zagraves updated authors
zagraves authored Jan 22, 2010
12 * @author Ryan Kennedy
13 * @author Lawrence Morrisroe <lem@yahoo-inc.com>,
6304536 @zagraves fixed my email address in the comments, also updated the ico warning
zagraves authored Feb 24, 2010
14 * @author Zach Graves <zachg@yahoo-inc.com>
e54b9da @zagraves added license headers
zagraves authored Sep 23, 2009
15 * @copyright Copyrights for code authored by Yahoo! Inc. is licensed under the following terms:
16 * @license BSD Open Source License
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this software and associated documentation files (the "Software"), to deal
20 * in the Software without restriction, including without limitation the rights
21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 * copies of the Software, and to permit persons to whom the Software is
23 * furnished to do so, subject to the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c51abce @zagraves adding better error handling for oauth problems
zagraves authored Sep 25, 2009
30 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
e54b9da @zagraves added license headers
zagraves authored Sep 23, 2009
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 * THE SOFTWARE.
3ea505c @zagraves CCK API support
zagraves authored Jan 22, 2010
35 **/
36 $yupdates_session_store = NULL;
37
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
38 function yupdates_has_session($session)
39 {
40 if($session->store->hasAccessToken()) {
41 $access_token = $session->store->fetchAccessToken();
42
43 if(!$access_token->key || !$access_token->secret) {
44 return false;
45 }
46
47 // refresh the token.
48 $access_token = yupdates_get_accessToken($session, $access_token);
49
50 $token = $session->application->token;
51
52 return ($token && $token->key);
53 }
54 else if($session->store->hasRequestToken())
55 {
76e676e @zagraves fixed req token invalidating an access token, fixed bit.ly shorten qu…
zagraves authored Jan 27, 2010
56 // $request_token = yupdates_get_requestToken($session);
57 $request_token = $session->store->fetchRequestToken();
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
58
59 if(!$request_token->key || !$request_token->secret) {
60 $session->store->clearRequestToken();
61 $token = yupdates_get_requestToken($session);
62
63 return false;
64 }
65
66 if(array_key_exists("oauth_token", $_REQUEST) && array_key_exists("oauth_verifier", $_REQUEST)) {
67 $oauth_verifier = $_REQUEST["oauth_verifier"];
68 $access_token = $session->application->getAccessToken($request_token, $oauth_verifier);
76e676e @zagraves fixed req token invalidating an access token, fixed bit.ly shorten qu…
zagraves authored Jan 28, 2010
69
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
70 if($access_token->key && $access_token->secret) {
71 $session->store->clearRequestToken();
72 $session->store->storeAccessToken($access_token);
73
74 return TRUE;
75 }
76 }
77
78 return false;
79 }
80 else if($session->application->consumer_key && $session->application->consumer_secret)
81 {
82 $token = yupdates_get_requestToken($session);
83
84 return false;
85 }
86
87 return false;
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
88 }
89
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
90 function yupdates_get_requestToken($session)
91 {
92 $callback_params = array("auth_popup"=>"true");
93 $callback = yupdates_get_oauthCallback($callback_params);
94
95 $request_token = $session->application->getRequestToken($callback);
96 $session->store->storeRequestToken($request_token);
97
98 return $request_token;
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
99 }
100
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
101 function yupdates_get_accessToken($session, $access_token=NULL)
102 {
103 $access_token = $session->application->getAccessToken($access_token);
104 $session->store->storeAccessToken($access_token);
105
106 return $access_token;
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
107 }
108
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
109 function yupdates_clear_session()
110 {
111 global $current_user;
112 get_currentuserinfo();
113
114 $user = $current_user->user_login;
115 $session_store = yupdates_get_sessionStore($user);
116
117 $session_store->clearRequestToken();
118 $session_store->clearAccessToken();
119
120 /* delete keys
121 go to /wp-admin/options.php to update the array with any yupdates_* keys.
122 $options = array();
123 foreach($options as $name) {
124 delete_option($name);
125 }
126 */
127
26a4185 @zagraves updated wording, yql use
zagraves authored Jan 26, 2010
128 header(sprintf("Location: %s", get_bloginfo('url')));
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
129 exit();
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
130 }
131
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
132 function yupdates_get_oauthCallback($parameters=array())
133 {
134 return sprintf("http://%s%s&%s",$_SERVER["HTTP_HOST"], $_SERVER["REQUEST_URI"], http_build_query($parameters));
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
135 }
136
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
137 function yupdates_get_currentUserSessionStore()
138 {
139 if(!$yupdates_session_store) {
140 global $current_user;
141 get_currentuserinfo();
142
143 $user = $current_user->user_login;
144 $yupdates_session_store = yupdates_get_sessionStore($user);
145 }
146 return $yupdates_session_store;
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
147 }
148
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
149 function yupdates_get_sessionStore($user)
150 {
151 $consumer_key = get_option("yupdates_consumer_key");
152 return new WordPressSessionStore($user, $consumer_key);
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
153 }
154
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
155 function yupdates_get_application()
156 {
157 // fetch application keys from user options
158 $consumer_key = get_option("yupdates_consumer_key");
159 $consumer_secret = get_option("yupdates_consumer_secret");
160 $appid = get_option("yupdates_application_id");
161
162 return new YahooOAuthApplication($consumer_key, $consumer_secret, $appid);
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
163 }
164
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
165 function yupdates_get_session($user=NULL)
166 {
167 // create session object with application, token store
168 $session = new stdclass();
169 $session->application = yupdates_get_application();
170 $session->store = (is_null($user)) ? yupdates_get_currentUserSessionStore() : yupdates_get_sessionStore($user);
171 $session->user = $user;
172
173 // pass the session off to check for tokens in the store.
174 // updates tokens as needed and returns true/false if a session exists
175 // (if access token exists)
176 $session->hasSession = yupdates_has_session($session);
177
178 return $session;
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
179 }
29b9bf3 @zagraves added bit.ly support to update links using the post permalink
zagraves authored Oct 30, 2009
180
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
181 function yupdates_get_bitly_options()
182 {
183 $options = new stdclass();
184 $options->apiKey = get_option("yupdates_bitly_apiKey");
185 $options->login = get_option("yupdates_bitly_login");
186
187 return $options;
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
188 }
29b9bf3 @zagraves added bit.ly support to update links using the post permalink
zagraves authored Oct 30, 2009
189
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
190 function yupdates_bitly_shorten($permalink, $apiKey, $login)
191 {
26a4185 @zagraves updated wording, yql use
zagraves authored Jan 26, 2010
192 $query = "SELECT statusCode, results FROM bit.ly.shorten where login='%s' and apiKey='%s' and longUrl='%s' and history='1'";
193 $query = sprintf($query, $login, $apiKey, $permalink);
194
6fe832f @zagraves alter bit.ly shorten YQL query to use standalone class, rather than g…
zagraves authored Jan 26, 2010
195 // $session = yupdates_get_session();
196 $yql = new YahooYQLQuery();
76e676e @zagraves fixed req token invalidating an access token, fixed bit.ly shorten qu…
zagraves authored Jan 28, 2010
197 $rsp = $yql->execute($query);
26a4185 @zagraves updated wording, yql use
zagraves authored Jan 26, 2010
198
e86aeed @zagraves final update
zagraves authored Feb 22, 2010
199 if(isset($rsp->query) && isset($rsp->query->results)) {
200 $results = $rsp->query->results;
201
202 $bitly = (isset($results->bitly)) ? $results->bitly : false;
26a4185 @zagraves updated wording, yql use
zagraves authored Jan 26, 2010
203
e86aeed @zagraves final update
zagraves authored Feb 22, 2010
204 if($bitly && isset($bitly->results) && $bitly->statusCode == 'OK') {
205 $results = $bitly->results->nodeKeyVal;
206
207 if($results && isset($results->shortUrl)) {
208 return $results->shortUrl;
209 }
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
210 }
211 }
e86aeed @zagraves final update
zagraves authored Feb 22, 2010
212
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
213 return $permalink;
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
214 }
4f9b238 @zagraves initial commit
zagraves authored Sep 23, 2009
215
e86aeed @zagraves final update
zagraves authored Feb 22, 2010
216 function yupdates_insertUpdate($update)
217 {
218 $session = yupdates_get_session();
219
220 if($update && $session->hasSession) {
221 $suid = null;
222 $results = $session->application->insertUpdate(null, $update->description, $update->title, $update->link, $suid);
223
224 return ($results) ? $suid : false;
225 }
226
227 return false;
228 }
229
9e4d21d @zagraves soft tabs
zagraves authored Jan 25, 2010
230 function yupdates_close_popup()
231 {
4f9b238 @zagraves initial commit
zagraves authored Sep 23, 2009
232 ?>
e86aeed @zagraves final update
zagraves authored Feb 22, 2010
233 <script type="text/javascript">
234 window.close();
235 </script>
4f9b238 @zagraves initial commit
zagraves authored Sep 23, 2009
236 <?php
3ea505c @zagraves CCK API support
zagraves authored Jan 23, 2010
237 }
4f9b238 @zagraves initial commit
zagraves authored Sep 23, 2009
238 ?>
Something went wrong with that request. Please try again.