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