PageRenderTime 8ms CodeModel.GetById 18ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Yahoo/YahooOAuthApplication.class.php

http://github.com/yahoo/yos-social-php5
PHP | 482 lines | 347 code | 93 blank | 42 comment | 67 complexity | 2cf2bddd28f071bff246388a12feeb6b MD5 | raw file
  1<?php
  2
  3/**
  4 * Yahoo! PHP5 SDK
  5 *
  6 *  * Yahoo! Query Language
  7 *  * Yahoo! Social API
  8 *
  9 * Find documentation and support on Yahoo! Developer Network: http://developer.yahoo.com
 10 *
 11 * Hosted on GitHub: http://github.com/yahoo/yos-social-php5/tree/master
 12 *
 13 * @package    yos-social-php5
 14 * @subpackage yahoo
 15 *
 16 * @author     Dustin Whittle <dustin@yahoo-inc.com>
 17 * @author     Zach Graves <zachg@yahoo-inc.com>
 18 * @copyright  Copyrights for code authored by Yahoo! Inc. is licensed under the following terms:
 19 * @license    BSD Open Source License
 20 *
 21 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 22 *   of this software and associated documentation files (the "Software"), to deal
 23 *   in the Software without restriction, including without limitation the rights
 24 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 25 *   copies of the Software, and to permit persons to whom the Software is
 26 *   furnished to do so, subject to the following conditions:
 27 *
 28 *   The above copyright notice and this permission notice shall be included in
 29 *   all copies or substantial portions of the Software.
 30 *
 31 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 32 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 33 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 34 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 35 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 36 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 37 *   THE SOFTWARE.
 38 **/
 39
 40require_once 'YahooCurl.class.php';
 41require_once 'YahooYQLQuery.class.php';
 42require_once 'YahooOAuthApplicationException.class.php';
 43require_once 'YahooOAuthAccessToken.class.php';
 44require_once 'YahooOAuthRequestToken.class.php';
 45require_once 'YahooOAuthClient.class.php';
 46
 47
 48class YahooOAuthApplication
 49{
 50  public function __construct($consumer_key, $consumer_secret, $application_id, $callback_url = null, $token = null, $options = array(), $client = null)
 51  {
 52    $this->client = is_null($client) ? new YahooOAuthClient() : $client;
 53
 54    $this->consumer_key               = $consumer_key;
 55    $this->consumer_secret            = $consumer_secret;
 56    $this->application_id             = $application_id;
 57    $this->callback_url               = $callback_url;
 58    $this->token                      = $token;
 59    $this->options                    = $options;
 60
 61    $this->consumer                   = new OAuthConsumer($this->consumer_key, $this->consumer_secret);
 62    $this->signature_method_plaintext = new OAuthSignatureMethod_PLAINTEXT();
 63    $this->signature_method_hmac_sha1 = new OAuthSignatureMethod_HMAC_SHA1();
 64  }
 65  
 66  public function getGUID()
 67  {
 68     if($this->token) {
 69        return $this->token->yahoo_guid;  
 70     }
 71  }
 72
 73  public function getOpenIDUrl($return_to = false, $lang = 'en', $openIdEndpoint = 'https://open.login.yahooapis.com/openid/op/auth')
 74  {
 75    $openid_request = array(
 76      'openid.ns'                => 'http://specs.openid.net/auth/2.0',
 77      'openid.claimed_id'        => 'http://specs.openid.net/auth/2.0/identifier_select',
 78      'openid.identity'          => 'http://specs.openid.net/auth/2.0/identifier_select',
 79      'openid.realm'             =>  $this->callback_url,
 80      'openid.ui.mode'           => 'popup',
 81      'openid.return_to'         =>  $return_to,
 82      'openid.mode'              => 'checkid_setup',
 83      'openid.assoc_handle'      => session_id(),
 84      'openid.ns.ui'             => 'http://specs.openid.net/extensions/ui/1.0',
 85      'openid.ui.icon'           => 'true',
 86      'openid.ui.language'       =>  $lang,
 87      'openid.ns.ext1'           => 'http://openid.net/srv/ax/1.0',
 88      'openid.ext1.mode'         => 'fetch_request',
 89      'openid.ext1.type.email'   => 'http://axschema.org/contact/email',
 90      'openid.ext1.type.first'   => 'http://axschema.org/namePerson/first',
 91      'openid.ext1.type.last'    => 'http://axschema.org/namePerson/last',
 92      'openid.ext1.type.country' => 'http://axschema.org/contact/country/home',
 93      'openid.ext1.type.lang'    => 'http://axschema.org/pref/language',
 94      'openid.ext1.required'     => 'email,first,last,country,lang',
 95      'openid.ns.oauth'          => 'http://specs.openid.net/extensions/oauth/1.0',
 96      'openid.oauth.consumer'    => $this->consumer_key,
 97      'openid.oauth.scope'       => '',
 98      'xopenid_lang_pref'        => $lang,
 99   );
100
101    return $openIdEndpoint.'?'.http_build_query($openid_request);
102  }
103
104  public function getRequestToken($callback = "oob")
105  {
106    $parameters = array('xoauth_lang_pref' => 'en', 'oauth_callback' => $callback);
107    $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, null, 'GET', YahooOAuthClient::REQUEST_TOKEN_API_URL, $parameters);
108    $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, null);
109    return $this->client->fetch_request_token($oauth_request);
110  }
111
112  public function getAuthorizationUrl($oauth_request_token)
113  {
114    // $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $oauth_request_token, 'GET', YahooOAuthClient::AUTHORIZATION_API_URL);
115    // $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $oauth_request_token);
116    // return $oauth_request->to_url();
117
118    if(isset($oauth_request_token->request_auth_url) && !empty($oauth_request_token->request_auth_url))
119    {
120       $auth_url = $oauth_request_token->request_auth_url;
121    }
122    else
123    {
124       $auth_url = sprintf("%s?oauth_token=%s", YahooOAuthClient::AUTHORIZATION_API_URL, $oauth_request_token->key);
125    }
126
127    return $auth_url;
128  }
129
130  public function getAccessToken($oauth_request_token, $verifier = null)
131  {
132    if ($verifier == null)
133    {
134      $parameters = array();
135    }
136    else
137    {
138      $parameters = array('oauth_verifier' => $verifier);
139    }
140
141    if(isset($oauth_request_token->session_handle) && !empty($oauth_request_token->session_handle))
142    {
143       $parameters["oauth_session_handle"] = $oauth_request_token->session_handle;
144    }
145
146    $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $oauth_request_token, 'GET', YahooOAuthClient::ACCESS_TOKEN_API_URL, $parameters);
147    $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $oauth_request_token);
148    $this->token = $this->client->fetch_access_token($oauth_request);
149
150    return $this->token;
151  }
152
153  public function refreshAccessToken($oauth_access_token)
154  {
155    $parameters = array('oauth_session_handle' => $oauth_access_token->session_handle);
156    $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $oauth_access_token, 'GET', YahooOAuthClient::ACCESS_TOKEN_API_URL, $parameters);
157    $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $oauth_access_token);
158    $this->token = $this->client->fetch_access_token($oauth_request);
159
160    return $this->token;
161  }
162
163  public static function fromYAP($consumer_key, $consumer_secret, $application_id)
164  {
165    $is_canvas = (isset($_POST['yap_appid']) && isset($_POST['yap_view']) && isset($_POST['oauth_signature']));
166    if($is_canvas === false) {
167       throw new YahooOAuthApplicationException('YAP application environment not found in request.');
168    }
169
170    $yap_consumer_key = $_POST['yap_consumer_key'];
171    if($consumer_key != $yap_consumer_key) {
172       throw new YahooOAuthApplicationException(sprintf('Provided consumer key does not match yap_consumer_key: (%s)', $yap_consumer_key));
173    }
174
175    $consumer    = new OAuthConsumer($consumer_key, $consumer_secret);
176    $token       = new YahooOAuthAccessToken($_POST['yap_viewer_access_token'], $_POST['yap_viewer_access_token_secret'], null, null, null, $_POST['yap_viewer_guid']);
177    $application = new YahooOAuthApplication($consumer->key, $consumer->secret, $application_id, null, $token);
178
179    $signature_valid = $application->signature_method_hmac_sha1->check_signature(OAuthRequest::from_request(), $consumer, $token, $_POST['oauth_signature']);
180    if($signature_valid === false) {
181       // temporary fix to allow newer versions of OAuth.php to work with YAP.
182       // return false;
183    }
184
185    return $application;
186  }
187
188  public function getIdentity($yid)
189  {
190    $rsp = $this->yql(sprintf('SELECT * FROM yahoo.identity where yid="%s"', $yid));
191    return isset($rsp->query->results) ? $rsp->query->results : false;
192  }
193
194  public function getProfile($guid = null)
195  {
196    if($guid == null && !is_null($this->token))
197    {
198      $guid = $this->token->yahoo_guid;
199    }
200    
201    $rsp = $this->yql(sprintf('SELECT * FROM social.profile where guid="%s"', $guid));
202
203    return isset($rsp->query->results) ? $rsp->query->results : false;
204  }
205  
206  public function getProfileImages($guid = null, $size = null) 
207  {
208    if($guid == null && !is_null($this->token))
209    {
210      $guid = $this->token->yahoo_guid;
211    }
212    
213    if($size) {
214	  $query = sprintf('SELECT * FROM social.profile.image WHERE guid="%s" and size="%s"', $guid, $size);
215    } else {
216	  $query = sprintf('SELECT * FROM social.profile.image WHERE guid="%s"', $guid);
217    }
218
219    $rsp = $this->yql($query);
220
221    return isset($rsp->query->results) ? $rsp->query->results : false;
222  }
223
224  public function getStatus($guid = null)
225  {
226    if($guid == null && !is_null($this->token))
227    {
228      $guid = $this->token->yahoo_guid;
229    }
230    
231    $rsp = $this->yql(sprintf('SELECT * FROM social.profile.status WHERE guid="%s"', $guid));
232	
233    return isset($rsp->query->results) ? $rsp->query->results : false;
234  }
235
236  public function setStatus($guid = null, $status)
237  {
238    if($guid == null && !is_null($this->token))
239    {
240      $guid = $this->token->yahoo_guid;
241    }
242    
243    $rsp = $this->yql(sprintf('UPDATE social.profile.status SET status="%s" WHERE guid="%s"', $status, $guid), array(), YahooCurl::PUT);
244    
245    return isset($rsp->query->results) ? $rsp->query->results : false;
246  }
247
248  public function getConnections($guid = null, $offset = 0, $limit = 10)
249  {
250    if($guid == null && !is_null($this->token))
251    {
252      $guid = $this->token->yahoo_guid;
253    }
254    
255    $rsp = $this->yql(sprintf('SELECT * FROM social.connections(%s,%s) WHERE owner_guid="%s"', $offset, $limit, $guid));
256    
257    return isset($rsp->query->results) ? $rsp->query->results : false;
258  }
259  
260  public function getRelationships($guid = null, $offset = 0, $limit = 10)
261  {
262    if($guid == null && !is_null($this->token))
263    {
264      $guid = $this->token->yahoo_guid;
265    }
266    
267    $rsp = $this->yql(sprintf('SELECT * FROM social.relationships(%s,%s) WHERE owner_guid="%s"', $offset, $limit, $guid));
268    
269    return isset($rsp->query->results) ? $rsp->query->results : false;
270  }
271
272  public function getContacts($guid = null, $offset = 0, $limit = 10)
273  {
274    if($guid == null && !is_null($this->token))
275    {
276      $guid = $this->token->yahoo_guid;
277    }
278    
279    $rsp = $this->yql(sprintf('SELECT * FROM social.contacts(%s,%s) WHERE guid="%s"', $offset, $limit, $guid));
280    
281    return isset($rsp->query->results) ? $rsp->query->results : false;
282  }
283
284  public function getContact($guid = NULL, $cid)
285  {
286    if($guid == null && !is_null($this->token))
287    {
288      $guid = $this->token->yahoo_guid;
289    }
290
291    $rsp = $this->yql(sprintf('SELECT * from social.contacts WHERE guid="%s" AND contact_id="%s";', $guid, $cid));
292    
293    return isset($rsp->query->results) ? $rsp->query->results : false;
294  }
295
296  public function getContactSync($guid = null, $rev = 0)
297  {
298    if($guid == null && !is_null($this->token))
299    {
300      $guid = $this->token->yahoo_guid;
301    }
302    
303    $rsp = $this->yql(sprintf('SELECT * from social.contacts.sync WHERE guid="%s" AND rev="%s";', $guid, $rev));
304    
305    return isset($rsp->query->results) ? $rsp->query->results : false;
306  }
307
308  public function syncContacts($guid = null, $contactsync)
309  {
310    if($guid == null && !is_null($this->token))
311    {
312      $guid = $this->token->yahoo_guid;
313    }
314
315    $url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/contacts', $guid);
316    $parameters = array('format' => 'json');
317
318    $data = array('contactsync' => $contactsync);
319    $body = json_encode($data);
320
321    $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'PUT', $url, $parameters);
322    $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);
323
324    $http = YahooCurl::fetch($oauth_request->to_url(), array(), array('Content-Type: application/json', 'Accept: *'), $oauth_request->get_normalized_http_method(), $body);
325
326    return $http['response_body'];
327  }
328  
329  public function addSimpleContact($guid = null, $givenName, $familyName, $email, $nickname) 
330  {
331     if($guid == null && !is_null($this->token))
332     {
333       $guid = $this->token->yahoo_guid;
334     }
335     
336     $query = sprintf('INSERT INTO social.contacts (owner_guid, givenName, familyName, email, nickname) VALUES ("%s", "%s", "%s", "%s", "%s")', $guid, $givenName, $familyName, $email, $nickname);
337     $rsp = $this->yql($query, array(), YahooCurl::PUT);
338
339     return isset($rsp->query->results) ? $rsp->query->results : false;
340  }
341
342  public function addContact($guid = null, $contact)
343  {
344    if($guid == null && !is_null($this->token))
345    {
346      $guid = $this->token->yahoo_guid;
347    }
348
349    $url = sprintf(YahooOAuthClient::SOCIAL_API_URL.'/user/%s/contacts', $guid);
350    $parameters = array('format' => 'json');
351
352    $data = array('contact' => $contact);
353    $body = json_encode($data);
354
355    $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, 'POST', $url, $parameters);
356    $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);
357
358    $http = YahooCurl::fetch($oauth_request->to_url(), array(), array('Content-Type: application/json', 'Accept: *'), $oauth_request->get_normalized_http_method(), $body);
359
360    return $http['response_body'];
361  }
362
363  public function getConnectionUpdates($guid = null, $offset = 0, $limit = 10)
364  {
365    if($guid == null && !is_null($this->token))
366    {
367      $guid = $this->token->yahoo_guid;
368    }
369
370    $rsp = $this->yql(sprintf('SELECT * FROM social.connections.updates(%s, %s) WHERE guid="%s"', $offset, $limit, $guid));
371
372    return isset($rsp->query->results) ? $rsp->query->results : false;
373  }
374
375  public function getUpdates($guid = null, $offset = 0, $limit = 10)
376  {
377    if($guid == null && !is_null($this->token))
378    {
379      $guid = $this->token->yahoo_guid;
380    }
381    
382    $rsp = $this->yql(sprintf('SELECT * FROM social.updates(%s, %s) WHERE guid="%s"', $offset, $limit, $guid));
383    
384    return isset($rsp->query->results) ? $rsp->query->results : false;
385  }
386
387  public function insertUpdate($params)
388  {
389     $guid = $this->token->yahoo_guid;
390     
391     $defaults = array(
392        'collectionID' => $guid,
393        'collectionType' => 'guid',
394        'class' => 'app',
395        'suid' => uniqid(mt_rand()),
396        'pubDate' => (string)time(),
397        'source' => 'APP.'.$this->application_id,
398        'type' => 'appActivity3',
399        'link' => ''
400     );
401     
402     $update = array_merge($defaults, $params);
403     $body = array('updates' => array($update));
404     
405     $url = sprintf("http://social.yahooapis.com/v1/user/%s/updates/%s/%s", $update['collectionID'], $update['source'], $update['suid']);
406     
407     $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, YahooCurl::PUT, $url);
408     $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);
409     
410     $parameters = json_encode($body);
411     $headers = array('Content-Type: application/json', 'Accept: application/json', $oauth_request->to_header());
412     
413     $http = YahooCurl::fetch($oauth_request->get_normalized_http_url(), array(), $headers, $oauth_request->get_normalized_http_method(), $parameters);
414     
415     return ($http) ? json_decode($http['response_body']) : false;
416  }
417
418  public function getSocialGraph($guid = null, $offset = 0, $limit = 10)
419  {
420    if($guid == null && !is_null($this->token))
421    {
422      $guid = $this->token->yahoo_guid;
423    }
424    
425    $query = sprintf('SELECT * FROM social.profile where guid in (SELECT guid from social.relationships (%s, %s) WHERE owner_guid="%s");', $offset, $limit, $guid);
426    $rsp = $this->yql($query);
427
428    return isset($rsp->query->results) ? $rsp->query->results : false;
429  }
430
431  public function getProfileLocation($guid = null)
432  {
433    if($guid == null && !is_null($this->token))
434    {
435      $guid = $this->token->yahoo_guid;
436    }
437    
438    $rsp = $this->yql(sprintf('SELECT * FROM geo.places WHERE text IN (SELECT location FROM social.profile WHERE guid="%s");', $guid));
439
440    return isset($rsp->query->results) ? $rsp->query->results : false;
441  }
442
443  public function getGeoPlaces($location)
444  {
445    $rsp = $this->yql(sprintf('SELECT * FROM geo.places where text="%s"', $location));
446    return isset($rsp->query->results) ? $rsp->query->results : false;
447  }
448  
449  public function setSmallView($guid = null, $content) 
450  {
451    if($guid == null && !is_null($this->token))
452    {
453      $guid = $this->token->yahoo_guid;
454    }
455    
456    $rsp = $this->yql(sprintf('UPDATE yap.setsmallview SET content="%s" where guid="%s" and ck="%s" and cks="%s";', 
457		$content, $guid, $this->consumer->key, $this->consumer->secret), array(), YahooCurl::PUT);
458    
459    return isset($rsp->query->results) ? $rsp->query->results : false;
460  }
461
462  public function yql($query, $parameters = array(), $method = YahooCurl::GET)
463  {
464    if(is_array($query))
465    {
466      // handle multi queries
467      $query = sprintf('SELECT * FROM query.multi WHERE queries="%s"', implode(';', str_replace('"', "'", $query)));
468    }
469
470    $parameters = array_merge(array('q' => $query, 'format' => 'json', 'env' => YahooYQLQuery::DATATABLES_URL), $parameters);
471
472    $oauth_request = OAuthRequest::from_consumer_and_token($this->consumer, $this->token, $method, YahooYQLQuery::OAUTH_API_URL, $parameters);
473    $oauth_request->sign_request($this->signature_method_hmac_sha1, $this->consumer, $this->token);
474
475    return json_decode($this->client->access_resource($oauth_request));
476  }
477}
478
479function _yql_insert_quotes($value)
480{
481   return "'$value'";
482}