PageRenderTime 21ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/pacore/web/includes/classes/Flickrclient.php

https://github.com/rerooting/PeopleAggregator
PHP | 172 lines | 107 code | 30 blank | 35 comment | 3 complexity | fa50d0209f620e152188531cda8284f8 MD5 | raw file
  1. <?php
  2. /** !
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * [filename] is a part of PeopleAggregator.
  5. * [description including history]
  6. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  7. * @author [creator, or "Original Author"]
  8. * @license http://bit.ly/aVWqRV PayAsYouGo License
  9. * @copyright Copyright (c) 2010 Broadband Mechanics
  10. * @package PeopleAggregator
  11. */
  12. ?>
  13. <?php
  14. /**
  15. * This class is use to fetch the photo of an user from www.flickr.com
  16. * and then return the array contains the title and url of that photo.
  17. *
  18. * @package Flickrclient
  19. * @author Tekriti Software (http://www.tekritisoftware.com)
  20. */
  21. //api
  22. require_once "api/PAException/PAException.php";
  23. require_once "api/Logger/Logger.php";
  24. //ext
  25. require_once "Flickr/API.php";
  26. class Flickrclient {
  27. public function __construct() {
  28. Logger::log("Enter: Flickrclient::__construct");
  29. // individual users can override the global API key and secret in their local_config.php files if they wish
  30. global $flickr_api_key, $flickr_api_secret, $flickr_auth_type;
  31. $this->api_key = trim($flickr_api_key);
  32. $this->api_secret = trim($flickr_api_secret);
  33. $this->auth_type = trim($flickr_auth_type);
  34. // set up api wrapper
  35. $this->api = new Flickr_API(array(
  36. 'api_key' => $this->api_key,
  37. 'api_secret' => $this->api_secret,
  38. ));
  39. Logger::log("Exit: Flickrclient::__construct");
  40. }
  41. // wrapper for Flickr_API::callMethod that converts errors into PAExceptions
  42. private function _api_callmethod($methodName, $args) {
  43. $r = $this->api->callMethod($methodName, $args);
  44. if (!$r) {
  45. throw new PAException(REMOTE_ERROR, "Flickr error ".$this->api->_err_code.": ".$this->api->_err_msg);
  46. }
  47. return $r;
  48. }
  49. // generate an authentication frob (see flickr_in.php for usage)
  50. public function auth_getFrob() {
  51. list($dom, $xp, $xml) = $this->_api_callmethod("flickr.auth.getFrob", array());
  52. $frob = $xp->query("/rsp/frob")->item(0)->textContent;
  53. return $frob;
  54. }
  55. // build an authentication url, given a frob and a permission string (see flickr_in.php for usage)
  56. public function make_auth_url($frob, $perms) {
  57. $munge = $this->api_secret."api_key".$this->api_key."frob$frob"."perms$perms";
  58. return "http://flickr.com/services/auth/?api_key=$this->api_key&perms=$perms&frob=$frob&api_sig=".md5($munge);
  59. }
  60. // turn a validated frob into an authentication token (see flickr_in.php for usage)
  61. public function auth_getToken($frob) {
  62. list($dom, $xp, $xml) = $this->_api_callmethod("flickr.auth.getToken", array("frob" => $frob));
  63. $this->user_token = $xp->query("/rsp/auth/token")->item(0)->textContent;
  64. return array(
  65. "token" => $this->user_token,
  66. "perms" => $xp->query("/rsp/auth/perms")->item(0)->textContent,
  67. "nsid" => $xp->query("/rsp/auth/user/@nsid")->item(0)->value,
  68. "username" => $xp->query("/rsp/auth/user/@username")->item(0)->value,
  69. "fullname" => $xp->query("/rsp/auth/user/@fullname")->item(0)->value,
  70. );
  71. }
  72. // given an user's nsid get their (public) infp
  73. public function people_getInfo($nsid) {
  74. $r =
  75. $this->_api_callmethod("flickr.people.getInfo",
  76. array("user_id" => $nsid));
  77. return $r;
  78. }
  79. // get a user's contact list (requires read authentication)
  80. function contacts_getList() {
  81. if (!$this->user_token) throw new PAException(OPERATION_NOT_PERMITTED, "You must authenticate against flickr before calling contacts_getList()");
  82. list($dom, $xp, $xml) = $this->_api_callmethod("flickr.contacts.getList", array('auth_token' => $this->user_token));
  83. $contacts = array();
  84. foreach ($xp->query("/rsp/contacts/contact") as $node) {
  85. $contacts[] = array(
  86. "nsid" => $xp->query("@nsid", $node)->item(0)->value,
  87. "username" => $xp->query("@username", $node)->item(0)->value,
  88. "iconserver" => $xp->query("@iconserver", $node)->item(0)->value,
  89. "realname" => $xp->query("@realname", $node)->item(0)->value,
  90. "friend" => $xp->query("@friend", $node)->item(0)->value,
  91. "family" => $xp->query("@family", $node)->item(0)->value,
  92. "ignored" => $xp->query("@ignored", $node)->item(0)->value,
  93. );
  94. }
  95. return $contacts;
  96. }
  97. // given an e-mail address, get the user's nsid
  98. public function people_findByEmail($email) {
  99. list($dom, $xp, $xml) = $this->_api_callmethod("flickr.people.findByEmail", array("find_email" => $email));
  100. $nsid = $xp->query("/rsp/user/@nsid")->item(0)->value;
  101. Logger::log("Resolved Flickr e-mail '$email' to NSID '$nsid'");
  102. return $nsid;
  103. }
  104. // given a username, get the user's nsid
  105. public function people_findByUsername($username) {
  106. list($dom, $xp, $xml) = $this->_api_callmethod("flickr.people.findByUsername", array("username" => $username));
  107. $nsid = $xp->query("/rsp/user/@nsid")->item(0)->value;
  108. Logger::log("Resolved Flickr username '$username' to NSID '$nsid'");
  109. return $nsid;
  110. }
  111. // gets info on a user's public photos
  112. // returns array(
  113. // array("server" => "...", "id" => "...", "secret" => "...", "thumbnail_url" => "..."),
  114. // ...
  115. // );
  116. public function people_getPublicPhotos($nsid, $per_page, $page) {
  117. list($dom, $xp, $response) = $this->_api_callmethod('flickr.people.getPublicPhotos', array('user_id'=> $nsid, 'per_page' => $per_page, 'page' => $page));
  118. $photos = array();
  119. foreach ($xp->query("/rsp/photos/photo") as $node) {
  120. $server = $xp->query("@server", $node)->item(0)->value;
  121. $id = $xp->query("@id", $node)->item(0)->value;
  122. $secret = $xp->query("@secret", $node)->item(0)->value;
  123. $base = "http://static.flickr.com/$server/${id}_${secret}";
  124. $photos[] = array(
  125. "server" => $server,
  126. "id" => $id,
  127. "secret" => $secret,
  128. "url" => "http://flickr.com/photos/$nsid/$id/",
  129. "med_url" => "$base.jpg",
  130. "75x75_url" => "${base}_s.jpg",
  131. );
  132. }
  133. Logger::log("Retrieved ".count($photos)." photos from Flickr for NSID $nsid");
  134. return $photos;
  135. }
  136. // get a url to a user's photos, given the user's nsid
  137. function urls_getUserPhotos($user_id) {
  138. list($dom, $xp, $xml) = $this->_api_callmethod("flickr.urls.getUserPhotos", array("user_id" => $user_id));
  139. return $xp->query("/rsp/user/@url")->item(0)->value;
  140. }
  141. }
  142. ?>