Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
migrating applicable social methods to use YQL, insertUpdate sets a *…
Browse files Browse the repository at this point in the history
…better* suid by reference, added new methods (getIdentity, setSmallView, getConnectionUpdates)
  • Loading branch information
zagraves committed Feb 5, 2010
1 parent 86eef28 commit 82d73fd
Showing 1 changed file with 98 additions and 88 deletions.
186 changes: 98 additions & 88 deletions lib/Yahoo/YahooOAuthApplication.class.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @subpackage yahoo * @subpackage yahoo
* *
* @author Dustin Whittle <dustin@yahoo-inc.com> * @author Dustin Whittle <dustin@yahoo-inc.com>
* @author Zach Graves <zachg@yahoo-inc.com>
* @copyright Copyrights for code authored by Yahoo! Inc. is licensed under the following terms: * @copyright Copyrights for code authored by Yahoo! Inc. is licensed under the following terms:
* @license BSD Open Source License * @license BSD Open Source License
* *
Expand Down Expand Up @@ -154,7 +155,7 @@ public function refreshAccessToken($oauth_access_token)


public static function fromYAP($consumer_key, $consumer_secret, $application_id) public static function fromYAP($consumer_key, $consumer_secret, $application_id)
{ {
$is_canvas = (isset($_POST['yap_appid']) && isset($_POST['yap_view']) && isset($_POST['oauth_signature'])); $is_canvas = (isset($_POST['yap_appid']) && isset($_POST['yap_view']) && isset($_POST['oauth_signature']));
if($is_canvas === false) { if($is_canvas === false) {
throw new YahooOAuthException('YAP application environment not found in request.'); throw new YahooOAuthException('YAP application environment not found in request.');
} }
Expand All @@ -170,61 +171,71 @@ public static function fromYAP($consumer_key, $consumer_secret, $application_id)


$signature_valid = $application->signature_method_hmac_sha1->check_signature(OAuthRequest::from_request(), $consumer, $token, $_POST['oauth_signature']); $signature_valid = $application->signature_method_hmac_sha1->check_signature(OAuthRequest::from_request(), $consumer, $token, $_POST['oauth_signature']);
if($signature_valid === false) { if($signature_valid === false) {
// temporary fix to allow newer versions of OAuth.php to work with YAP.
// return false; // return false;
} }


return $application; return $application;
} }


public function getIdentity($yid)
{
$rsp = $this->yql(sprintf('SELECT * FROM yahoo.identity where yid="%s"', $yid));
return isset($rsp->query->results) ? $rsp->query->results : false;
}

public function getProfile($guid = null) public function getProfile($guid = null)
{ {
if($guid == null && !is_null($this->token)) if($guid == null && !is_null($this->token))
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }
$url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/profile', $guid);
$parameters = array('format' => 'json'); $rsp = $this->yql(sprintf('SELECT * FROM social.profile where guid="%s"', $guid));
$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $url, $parameters);
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);

$data = json_decode($this->client->access_resource($oauth_request));


return ($data) ? $data->profile : false; return isset($rsp->query->results) ? $rsp->query->results : false;
} }


public function getStatus($guid = null) public function getProfileImages($guid = null, $size = null)
{ {
if($guid == null && !is_null($this->token)) if($guid == null && !is_null($this->token))
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }

if($size) {
$query = sprintf('SELECT * FROM social.profile.image WHERE guid="%s" and size="%s"', $guid, $size);
} else {
$query = sprintf('SELECT * FROM social.profile.image WHERE guid="%s"', $guid);
}


$url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/profile/status', $guid); $rsp = $this->yql($query);
$parameters = array('format' => 'json');
$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $url, $parameters);
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);


return json_decode($this->client->access_resource($oauth_request)); return isset($rsp->query->results) ? $rsp->query->results : false;
} }


public function setStatus($guid = null, $status) public function getStatus($guid = null)
{ {
if($guid == null && !is_null($this->token)) if($guid == null && !is_null($this->token))
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }

$rsp = $this->yql(sprintf('UPDATE social.profile.status SET status="%s" WHERE guid="%s"', $status, $guid));

return isset($rsp->query->results) ? $rsp->query->results : false;
}


$body = '{"status":{"message":"'.$status.'"}}'; public function setStatus($guid = null, $status)

{
$url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/profile/status', $guid); if($guid == null && !is_null($this->token))
$parameters = array('format' => 'json'); {

$guid = $this->token->yahoo_guid;
$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'PUT', $url, $parameters); }
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);

$rsp = $this->yql(sprintf('UPDATE social.profile.status SET status="%s" WHERE guid="%s"', $status, $guid), array(), YahooCurl::PUT);
$http = YahooCurl::fetch($oauth_request->to_url(), array(), array('Content-Type: application/x-www-form-urlencoded', 'Accept: *'), $oauth_request->get_normalized_http_method(), $body);

return isset($rsp->query->results) ? $rsp->query->results : false;
return $http['response_body'];
} }


public function getConnections($guid = null, $offset = 0, $limit = 10) public function getConnections($guid = null, $offset = 0, $limit = 10)
Expand All @@ -233,15 +244,10 @@ public function getConnections($guid = null, $offset = 0, $limit = 10)
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }


$url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/connections', $guid); $rsp = $this->yql(sprintf('SELECT * FROM social.connections WHERE owner_guid="%s"', $guid));
$parameters = array('format' => 'json', 'view' => 'usercard', 'start' => $offset, 'count' => $limit);
$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $url, $parameters); return isset($rsp->query->results) ? $rsp->query->results : false;
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);

$data = json_decode($this->client->access_resource($oauth_request));

return ($data) ? $data->connections->connection : false;
} }


public function getContacts($guid = null, $offset = 0, $limit = 10) public function getContacts($guid = null, $offset = 0, $limit = 10)
Expand All @@ -250,15 +256,10 @@ public function getContacts($guid = null, $offset = 0, $limit = 10)
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }


$url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/contacts', $guid); $rsp = $this->yql(sprintf('SELECT * FROM social.contacts WHERE guid="%s"', $guid));
$parameters = array('format' => 'json', 'view' => 'tinyusercard', 'start' => $offset, 'count' => $limit);
$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $url, $parameters); return isset($rsp->query->results) ? $rsp->query->results : false;
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);

$data = json_decode($this->client->access_resource($oauth_request));

return ($data) ? $data->contacts->contact : false;
} }


public function getContact($guid = NULL, $cid) public function getContact($guid = NULL, $cid)
Expand All @@ -269,7 +270,7 @@ public function getContact($guid = NULL, $cid)
} }


$url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/contact/%s', $guid, $cid); $url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/contact/%s', $guid, $cid);
$parameters = array('format' => 'json'); $parameters = array('format' => 'json');


$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $url, $parameters); $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $url, $parameters);
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token); $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);
Expand Down Expand Up @@ -339,55 +340,52 @@ public function addContact($guid = null, $contact)
return $http['response_body']; return $http['response_body'];
} }


public function getUpdates($guid = null, $offset = 0, $limit = 10, $transform = null) public function getConnectionUpdates($guid = null, $offset = 0, $limit = 10)
{ {
if($guid == null && !is_null($this->token)) if($guid == null && !is_null($this->token))
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }


$url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/updates', $guid); $rsp = $this->yql(sprintf('SELECT * FROM social.connections.updates(%s, %s) WHERE guid="%s"', $offset, $limit, $guid));
$parameters = array('format' => 'json', 'start' => $offset, 'count' => $limit, 'transform' => ($transform) ? $transform : '( sort "pubDate" numeric descending (all) )');
$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'GET', $url, $parameters);
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);


$data = json_decode($this->client->access_resource($oauth_request)); return isset($rsp->query->results) ? $rsp->query->results : false;

return ($data) ? $data->updates : false;
} }


public function insertUpdate($guid = null, $description, $title, $link) public function getUpdates($guid = null, $offset = 0, $limit = 10)
{ {
if($guid == null && !is_null($this->token)) if($guid == null && !is_null($this->token))
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }

$rsp = $this->yql(sprintf('SELECT * FROM social.updates(%s, %s) WHERE guid="%s"', $offset, $limit, $guid));

return isset($rsp->query->results) ? $rsp->query->results : false;
}


public function insertUpdate($guid = null, $description, $title, $link, &$suid = null)
{
if($guid == null && !is_null($this->token))
{
$guid = $this->token->yahoo_guid;
}

if($suid == null)
{
// $suid = 'ugc'.rand(0, 1000);
$suid = sha1(uniqid(mt_rand()));
}

$source = 'APP.'.$this->application_id; $source = 'APP.'.$this->application_id;
$suid = 'ugc'.rand(0, 1000); $pubDate = time();
$body = sprintf('
{ "updates": [ { $query = 'INSERT INTO social.updates (guid, title, description, link, pubDate, source, suid) VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s");';
"class": "app", $query = sprintf($query, $guid, $title, $description, $link, $pubDate, $source, $suid);
"collectionType": "guid",
"description": "%s", $rsp = $this->yql($query, array(), YahooCurl::PUT);
"suid": "%s",
"link": "%s", return isset($rsp->query->results) ? $rsp->query->results : false;
"source": "%s",
"pubDate": "%s",
"title": "%s",
"type": "appActivity",
"collectionID": "%s"
} ] }', $description, $suid, $link, $source, time(), $title, $guid);

$url = sprintf('%s/user/%s/updates/%s/%s', YahooOAuthClient::SOCIAL_API_URL, $guid, $source, $suid);
$parameters = array('format' => 'json');

$oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'PUT', $url, $parameters);
$oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);

$http = YahooCurl::fetch($oauth_request->to_url(), array(), array('Content-Type: application/x-www-form-urlencoded', 'Accept: *'), $oauth_request->get_normalized_http_method(), $body);

return $http['response_body'];
} }


public function getSocialGraph($guid = null, $offset = 0, $limit = 10) public function getSocialGraph($guid = null, $offset = 0, $limit = 10)
Expand All @@ -397,10 +395,10 @@ public function getSocialGraph($guid = null, $offset = 0, $limit = 10)
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }


$query = sprintf("select * from social.profile where guid in (select guid from social.connections (%s, %s) where owner_guid='%s')", $offset, $limit, $guid); $query = sprintf('SELECT * FROM social.profile where guid in (SELECT guid from social.connections (%s, %s) WHERE owner_guid="%s");', $offset, $limit, $guid);
$data = $this->yql($query); $rsp = $this->yql($query);


return isset($data->query->results) ? $data->query->results : false; return isset($rsp->query->results) ? $rsp->query->results : false;
} }


public function getProfileLocation($guid = null) public function getProfileLocation($guid = null)
Expand All @@ -409,25 +407,37 @@ public function getProfileLocation($guid = null)
{ {
$guid = $this->token->yahoo_guid; $guid = $this->token->yahoo_guid;
} }

$rsp = $this->yql(sprintf('SELECT * FROM geo.places WHERE text IN (SELECT location FROM social.profile WHERE guid="%s");', $guid));


$data = $this->yql(sprintf('select * from geo.places where text in (select location from social.profile where guid="%s")', $guid)); return isset($rsp->query->results) ? $rsp->query->results : false;

return isset($data->query->results) ? $data->query->results : false;
} }


public function getGeoPlaces($location) public function getGeoPlaces($location)
{ {
$data = $this->yql(sprintf('select * from geo.places where text="%s"', $location)); $rsp = $this->yql(sprintf('SELECT * FROM geo.places where text="%s"', $location));

return isset($rsp->query->results) ? $rsp->query->results : false;
return isset($data->query->results) ? $data->query->results : false; }

public function setSmallView($guid = null, $content)
{
if($guid == null && !is_null($this->token))
{
$guid = $this->token->yahoo_guid;
}

$rsp = $this->yql(sprintf('UPDATE yap.setsmallview SET content="%s" where guid="%s" and ck="%s" and cks="%s";',
$content, $guid, $this->consumer->key, $this->consumer->secret), array(), YahooCurl::PUT);

return isset($rsp->query->results) ? $rsp->query->results : false;
} }


public function yql($query, $parameters = array(), $method = YahooCurl::GET) public function yql($query, $parameters = array(), $method = YahooCurl::GET)
{ {
if(is_array($query)) if(is_array($query))
{ {
// handle multi queries // handle multi queries
$query = sprintf('select * from query.multi where queries="%s"', implode(';', str_replace('"', "'", $query))); $query = sprintf('SELECT * FROM query.multi WHERE queries="%s"', implode(';', str_replace('"', "'", $query)));
} }


$parameters = array_merge(array('q' => $query, 'format' => 'json', 'env' => YahooYQLQuery::DATATABLES_URL), $parameters); $parameters = array_merge(array('q' => $query, 'format' => 'json', 'env' => YahooYQLQuery::DATATABLES_URL), $parameters);
Expand Down

0 comments on commit 82d73fd

Please sign in to comment.