From 3ea505c9f5c97bb9fa55b1cbd57672eb1feca08c Mon Sep 17 00:00:00 2001 From: basictheory Date: Fri, 22 Jan 2010 16:28:53 -0800 Subject: [PATCH] CCK API support --- yupdates.php | 158 +++++++++++----------- yupdates_application.php | 56 ++++++++ yupdates_db.php | 1 + yupdates_options.php | 253 +++++++++++++++++++---------------- yupdates_sessionstore.php | 5 +- yupdates_utils.php | 275 +++++++++++++++++++++----------------- 6 files changed, 431 insertions(+), 317 deletions(-) create mode 100644 yupdates_application.php diff --git a/yupdates.php b/yupdates.php index 99786e7..2935f85 100644 --- a/yupdates.php +++ b/yupdates.php @@ -9,7 +9,7 @@ * @package yos-updates-wordpress * @subpackage yahoo * - * @author Ryan Kennedy , Lawrence Morrisroe , Zach Graves + * @author Ryan Kennedy , Lawrence Morrisroe , Zach Graves * @copyright Copyrights for code authored by Yahoo! Inc. is licensed under the following terms: * @license BSD Open Source License * @@ -41,100 +41,96 @@ */ ?> -hasSession == false) - { - if($session->store->hasRequestToken()) { - $request_token = $session->store->fetchRequestToken(); - if(is_null($request_token->key) && !is_null($request_token->oauth_problem)) { - add_action("admin_notices", "yupdates_requestTokenProblem_warning"); - } else if(stripos($_SERVER["REQUEST_URI"], USER_MENU_URI) === FALSE) { - add_action("admin_notices", "yupdates_authorization_warning"); - } - } else if(yupdatesdb_hasApplicationInfo() && stripos($_SERVER["REQUEST_URI"], USER_MENU_URI) === FALSE ) { + // show warnings + if($session->hasSession == false) { + if($session->store->hasRequestToken()) { + $request_token = $session->store->fetchRequestToken(); + if($request_token && is_null($request_token->key) && !is_null($request_token->oauth_problem)) { + add_action("admin_notices", "yupdates_requestTokenProblem_warning"); + } else if(stripos($_SERVER["REQUEST_URI"], USER_MENU_URI) === FALSE) { add_action("admin_notices", "yupdates_authorization_warning"); - } else if($session->store->hasAccessToken()) { - // do nothing, we're all set. } - } - - if(!yupdatesdb_hasApplicationInfo() - && stripos($_SERVER["REQUEST_URI"], PLUGIN_OPTIONS_URI) === FALSE) - { - add_action("admin_notices", "yupdates_appinfo_warning"); - } + }/* else if(yupdatesdb_hasApplicationInfo() && stripos($_SERVER["REQUEST_URI"], USER_MENU_URI) === FALSE ) { + echo "no request token"; + add_action("admin_notices", "yupdates_authorization_warning"); + } else if($session->store->hasAccessToken()) { + // do nothing, we're all set now. + } else { + echo "no tokens!"; + }*/ } - function yupdates_requestTokenProblem_warning() { - $session_store = yupdates_get_currentUserSessionStore(); - $token = $session_store->fetchRequestToken(); - $oauth_problem = !is_null($token->oauth_problem) ? $token->oauth_problem : "Unknown Error"; - - echo <<fetchRequestToken(); + $oauth_problem = !is_null($token->oauth_problem) ? $token->oauth_problem : "Unknown Error"; + + echo << -

OAuth Error: Request token $oauth_problem. Re-configure the plugin.

+

Yahoo! Updates - OAuth Error: Request token $oauth_problem. Re-configure the plugin.

HTML; - } - - function yupdates_appinfo_warning() { - echo << -

You haven't configured the Yahoo! Updates Plugin yet. Configure the plugin.

+

You haven't configured the Yahoo! Updates Plugin yet. Configure the plugin.

HTML; - } - - function yupdates_authorization_warning() { - echo << -

You haven't authorized the Yahoo! Updates Plugin yet. Authorize the plugin now.

+

You haven't authorized the Yahoo! Updates Plugin yet. Authorize the plugin now.

HTML; - } +} ?> diff --git a/yupdates_application.php b/yupdates_application.php new file mode 100644 index 0000000..26ad5ae --- /dev/null +++ b/yupdates_application.php @@ -0,0 +1,56 @@ + + * @author Zach Graves + * @author Micah Laaker + * @copyright Copyrights for code authored by Yahoo! Inc. is licensed under the following terms: + * @license BSD Open Source License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + **/ + +function yupdates_plugin_application() { + $application_id = $_GET['appid']; + $consumer_key = $_GET['key']; + $consumer_secret = $_GET['secret']; + + echo << +var opener = parent.opener; + +if(opener.hasOwnProperty('yupdates_setCredentials') && typeof opener.yupdates_setCredentials == 'function') { + opener.yupdates_setCredentials("{$consumer_key}","{$consumer_secret}","{$application_id}"); +} + + +HTML; +} + +yupdates_plugin_application(); +?> \ No newline at end of file diff --git a/yupdates_db.php b/yupdates_db.php index ecf6c92..3ca21bb 100644 --- a/yupdates_db.php +++ b/yupdates_db.php @@ -40,6 +40,7 @@ define("YUPDATES_TITLE_TEMPLATE_OPTION", "yupdates_title_template"); $updateUsers = get_option(YUPDATES_USER_OPTION); + if(is_bool($updateUsers) && !$updateUsers) { $updateUsers = array(); } diff --git a/yupdates_options.php b/yupdates_options.php index 1b0329e..56e8103 100644 --- a/yupdates_options.php +++ b/yupdates_options.php @@ -45,16 +45,33 @@ if ( ! defined( 'WP_PLUGIN_DIR' ) ) define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' ); +define('YUPDATES_DEFAULT_TITLE_TEMPLATE', "posted '#blog_title' on their WordPress blog '#blog_name'"); function yupdates_plugin_options() { - $ck = get_option('yupdates_consumer_key'); - $cks = get_option('yupdates_consumer_secret'); - $appid = get_option('yupdates_application_id'); - $bitly_key = get_option("yupdates_bitly_apiKey"); - $bitly_login = get_option("yupdates_bitly_login"); - $title_template = get_option('yupdates_title_template'); + $session = yupdates_get_session(); + + // oauth keys + $consumer_key = $session->application->consumer_key; + $consumer_secret = $session->application->consumer_secret; + $appid = $session->application->application_id; + + $has_application = ($consumer_key && $consumer_secret && $appid); - if($title_template == "") $title_template = "posted '#blog_title' on their WordPress blog '#blog_name'"; + // extAuth options + $extAuth_host = $_SERVER["HTTP_HOST"]; + $extAuth_application_url = get_bloginfo('wpurl'); + $extAuth_third_party = $extAuth_host; + $extAuth_scopes = 'yurw'; + $extAuth_title = sprintf("WordPress plugin for %s", $extAuth_application_url); + $extAuth_description = sprintf("This application powers the updates plugin for your WordPress blog on %s", $extAuth_host); + $extAuth_return_to_url = sprintf("%s/plugins/yupdates_wordpress/yupdates_application.php", WP_CONTENT_URL); + $extAuth_favicon_url = sprintf("http://%s/favicon.ico", $extAuth_host); + + // blog options + $title_template_opt = get_option('yupdates_title_template'); + $title_template = ($title_template_opt) ? $title_template_opt : YUPDATES_DEFAULT_TITLE_TEMPLATE; + $bitly_key = get_option("yupdates_bitly_apiKey"); + $bitly_login = get_option("yupdates_bitly_login"); ?> + +
- - - -
- +

Create a Yahoo! Application

+ - -

Yahoo! API Access Settings

-Enter your API Key, Shared Secret, and App ID from the Yahoo! Developer Network. (These are needed so your WordPress blog can read -and write data on your behalf without revealing your Yahoo! ID and password). - - - - - - - - - - - - - -
Yahoo! API Key
Yahoo! Shared Secret
Yahoo! Application ID
- -

Don't know what these are, or how to get them? - Show/hide instructions for how to get a Yahoo! API key. (It's quick and free.)

- -
+ diff --git a/yupdates_sessionstore.php b/yupdates_sessionstore.php index c254749..ba439fc 100644 --- a/yupdates_sessionstore.php +++ b/yupdates_sessionstore.php @@ -32,7 +32,7 @@ * THE SOFTWARE. **/ - class WordPressSessionStore { +class WordPressSessionStore { var $optionName = NULL; var $option = NULL; var $consumerKey = ""; @@ -59,8 +59,9 @@ function validateConsumerKey() { if($this->consumerKey != $this->option["ck"]) { $this->resetOption(); - return FALSE; + return false; } + return true; } diff --git a/yupdates_utils.php b/yupdates_utils.php index fcdc2a0..6469fb5 100644 --- a/yupdates_utils.php +++ b/yupdates_utils.php @@ -30,154 +30,191 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. - **/ - - $yupdates_session_store = NULL; - - function yupdates_has_session($session) { - if($session->store->hasAccessToken()) - { - $access_token = $session->store->fetchAccessToken(); - - if(!$access_token->key || !$access_token->secret) { - return FALSE; - } - - $access_token = $session->application->getAccessToken($access_token); - $session->store->storeAccessToken($access_token); + **/ +$yupdates_session_store = NULL; + +function yupdates_has_session($session) { + if($session->store->hasAccessToken()) + { + $access_token = $session->store->fetchAccessToken(); + + if(!$access_token->key || !$access_token->secret) { + return false; + } + + // refresh the token. + $access_token = yupdates_get_accessToken($session, $access_token); + + $token = $session->application->token; + + return ($token && $token->key); + } + else if($session->store->hasRequestToken()) + { + $request_token = yupdates_get_requestToken($session); + // $request_token = $session->store->fetchRequestToken(); + + if(!$request_token->key || !$request_token->secret) { + $session->store->clearRequestToken(); + $token = yupdates_get_requestToken($session); - return ($session->application->token && $session->application->token->key); - } - else if($session->store->hasRequestToken()) - { - yupdates_get_requestToken($session); - $request_token = $session->store->fetchRequestToken(); + return false; + } + + if(array_key_exists("oauth_token", $_REQUEST) && array_key_exists("oauth_verifier", $_REQUEST)) { + $oauth_verifier = $_REQUEST["oauth_verifier"]; + $access_token = $session->application->getAccessToken($request_token, $oauth_verifier); - if(!$request_token->key || !$request_token->secret) { + if($access_token->key && $access_token->secret) { $session->store->clearRequestToken(); - yupdates_get_requestToken($session); - return FALSE; - } - - if(array_key_exists("oauth_token", $_REQUEST) && array_key_exists("oauth_verifier", $_REQUEST)) { - $oauth_verifier = $_REQUEST["oauth_verifier"]; - $access_token = $session->application->getAccessToken($request_token, $oauth_verifier); + $session->store->storeAccessToken($access_token); - if($access_token->key && $access_token->secret) { - $session->store->clearRequestToken(); - $session->store->storeAccessToken($access_token); - return TRUE; - } + return TRUE; } - - return FALSE; - } - else - { - yupdates_get_requestToken($session); - - return FALSE; } + + return false; } + else if($session->application->consumer_key && $session->application->consumer_secret) + { + $token = yupdates_get_requestToken($session); + + return false; + } - function yupdates_get_requestToken($session) { - $callback_params = array("auth_popup"=>"true"); - $callback = yupdates_get_oauthCallback($callback_params); - $request_token = $session->application->getRequestToken($callback); - $session->store->storeRequestToken($request_token); - } + return false; +} + +function yupdates_get_requestToken($session) { + $callback_params = array("auth_popup"=>"true"); + $callback = yupdates_get_oauthCallback($callback_params); - function yupdates_clear_session() { - global $current_user; - get_currentuserinfo(); - - $user = $current_user->user_login; - $session_store = yupdates_get_sessionStore($user); - - $session_store->clearRequestToken(); - $session_store->clearAccessToken(); - - // todo: infinite looping - header(sprintf("Location: %s", $_SERVER["REQUEST_URI"])); - exit(); - } + $request_token = $session->application->getRequestToken($callback); + $session->store->storeRequestToken($request_token); - function yupdates_get_oauthCallback($parameters=array()) { - return sprintf("http://%s%s&%s",$_SERVER["HTTP_HOST"], $_SERVER["REQUEST_URI"], http_build_query($parameters)); - } + return $request_token; +} + +function yupdates_get_accessToken($session, $access_token=NULL) { + $access_token = $session->application->getAccessToken($access_token); + $session->store->storeAccessToken($access_token); - function yupdates_get_currentUserSessionStore() { - if(!$yupdates_session_store) { - global $current_user; - get_currentuserinfo(); - - $user = $current_user->user_login; - $yupdates_session_store = yupdates_get_sessionStore($user); - } - return $yupdates_session_store; - } + return $access_token; +} + +function yupdates_clear_session() { + global $current_user; + get_currentuserinfo(); - function yupdates_get_sessionStore($user) { - return new WordPressSessionStore($user, get_option("yupdates_consumer_key")); - } + $user = $current_user->user_login; + $session_store = yupdates_get_sessionStore($user); - function yupdates_get_application() { - // fetch application keys from user options - $ck = get_option("yupdates_consumer_key"); - $cks = get_option("yupdates_consumer_secret"); - $appid = get_option("yupdates_application_id"); - - return new YahooOAuthApplication($ck, $cks, $appid); - } + $session_store->clearRequestToken(); + $session_store->clearAccessToken(); - function yupdates_get_session($user=NULL) { - // create session object with application, token store - $session = new stdclass(); - $session->application = yupdates_get_application(); - $session->store = (is_null($user)) ? yupdates_get_currentUserSessionStore() : yupdates_get_sessionStore($user); - - $session->hasSession = yupdates_has_session($session); - - return $session; + /* delete keys + go to /wp-admin/options.php to update the array with any yupdates_* keys. + $options = array( + 'yupdates_application_id','yupdates_bitly_apiKey','yupdates_bitly_login','yupdates_consumer_key', + 'yupdates_consumer_secret','yupdates_title_template','yupdates_tokens_','yupdates_tokens_admin', + 'yupdates_tokens_admin_dj0yJmk9WGhERFFkSHMzWEZxJmQ9WVdrOVJXVlhXVE','yupdates_tokens__dj0yJmk9WGhERFFkSHMzWEZxJmQ9WVdrOVJXVlhXVEZWTm5', + 'yupdates_updates_widget_users','yupdates_update_title','yupdates_update_title_template','yupdates_widget_count' + ); + foreach($options as $name) { + delete_option($name); } + */ - function yupdates_get_bitly_options() { - $options = new stdclass(); - $options->apiKey = get_option("yupdates_bitly_apiKey"); - $options->login = get_option("yupdates_bitly_login"); + // todo: infinite looping + header(sprintf("Location: %s", $_SERVER["HTTP_HOST"])); + exit(); +} + +function yupdates_get_oauthCallback($parameters=array()) { + return sprintf("http://%s%s&%s",$_SERVER["HTTP_HOST"], $_SERVER["REQUEST_URI"], http_build_query($parameters)); +} + +function yupdates_get_currentUserSessionStore() { + if(!$yupdates_session_store) { + global $current_user; + get_currentuserinfo(); - return $options; + $user = $current_user->user_login; + $yupdates_session_store = yupdates_get_sessionStore($user); } + return $yupdates_session_store; +} + +function yupdates_get_sessionStore($user) { + $consumer_key = get_option("yupdates_consumer_key"); + return new WordPressSessionStore($user, $consumer_key); +} + +function yupdates_get_application() { + // fetch application keys from user options + $consumer_key = get_option("yupdates_consumer_key"); + $consumer_secret = get_option("yupdates_consumer_secret"); + $appid = get_option("yupdates_application_id"); + + return new YahooOAuthApplication($consumer_key, $consumer_secret, $appid); +} + +function yupdates_get_session($user=NULL) { + // create session object with application, token store + $session = new stdclass(); + $session->application = yupdates_get_application(); + $session->store = (is_null($user)) ? yupdates_get_currentUserSessionStore() : yupdates_get_sessionStore($user); + $session->user = $user; - function yupdates_bitly_shorten($permalink, $apiKey, $login) { - $params = array(); - $params["apiKey"] = $apiKey; - $params["login"] = $login; - $params["longUrl"] = $permalink; - $params["version"] = "2.0.1"; - $params["history"] = "1"; + // pass the session off to check for tokens in the store. + // updates tokens as needed and returns true/false if a session exists + // (if access token exists) + $session->hasSession = yupdates_has_session($session); + + return $session; +} - $base_url = "http://api.bit.ly/shorten"; +function yupdates_get_bitly_options() { + $options = new stdclass(); + $options->apiKey = get_option("yupdates_bitly_apiKey"); + $options->login = get_option("yupdates_bitly_login"); + + return $options; +} - $http = YahooCurl::fetch($base_url, $params); - $rsp = $http["response_body"]; - $data = json_decode($rsp); +function yupdates_bitly_shorten($permalink, $apiKey, $login) { + $base_url = "http://api.bit.ly/shorten"; + $params = array( + 'apiKey' => $apiKey, + 'login' => $login, + 'longUrl' => $permalink, + 'version' => '2.0.1', + 'history' => '1' + ); + + $http = YahooCurl::fetch($base_url, $params); + + $rsp = $http["response_body"]; + $data = json_decode($rsp); - if($data && $data->statusCode == "OK" && $data->results) { - $results = get_object_vars($data->results); - $site = $results[$permalink]; + if($data && $data->statusCode == "OK" && isset($data->results)) { + $results = get_object_vars($data->results); + $site = $results[$permalink]; + + if($site && isset($site->shortUrl)) { $shortUrl = $site->shortUrl; return $shortUrl; - } else { - return $permalink; } } + + return $permalink; +} - function yupdates_close_popup() { +function yupdates_close_popup() { ?> \ No newline at end of file