/php-LightweightPicasaAPI-3.3/Picasa/Author.php

# · PHP · 250 lines · 101 code · 30 blank · 119 comment · 21 complexity · b9b0ac45284575ef528e0ec0e0f494f8 MD5 · raw file

  1. <?php
  2. require_once('Picasa/Logger.php');
  3. require_once('Picasa/Cache.php');
  4. /**
  5. * Holds a Picasa user.
  6. * Not all fields are guaranteed to be filled. Name and Uri will likely always contain a valid value.
  7. * This class should certainly be called "User" instead, but it's way too late to change it now.
  8. *
  9. * @author Cameron Hinkle
  10. * @version Version 3.0
  11. * @license http://www.gnu.org/licenses/ GNU Public License Version 3
  12. * @copyright Copyright (c) 2007 Cameron Hinkle
  13. * @package Picasa
  14. * @since Version 1.0
  15. */
  16. class Picasa_Author {
  17. /**
  18. * The registered name of the user.
  19. *
  20. * @access private
  21. * @var string
  22. */
  23. private $name;
  24. /**
  25. * The address of their main page on Picasa.
  26. *
  27. * @access private
  28. * @var string
  29. */
  30. private $uri;
  31. /**
  32. * The username of the user.
  33. *
  34. * @access private
  35. * @var string
  36. */
  37. private $user;
  38. /**
  39. * The user's Picasa nickname.
  40. *
  41. * @access private
  42. * @var string
  43. */
  44. private $nickname;
  45. /**
  46. * The address of an image selected by the user as their thumbnail.
  47. *
  48. * @access private
  49. * @var string
  50. */
  51. private $thumbnail;
  52. /**
  53. * @return string
  54. * @access public
  55. */
  56. public function getName () {
  57. return $this->name;
  58. }
  59. /**
  60. * @return string
  61. * @access public
  62. */
  63. public function getUri () {
  64. return $this->uri;
  65. }
  66. /**
  67. * @return string
  68. * @access public
  69. */
  70. public function getUser () {
  71. return $this->user;
  72. }
  73. /**
  74. * @return string
  75. * @access public
  76. */
  77. public function getNickname () {
  78. return $this->nickname;
  79. }
  80. /**
  81. * @return string
  82. * @access public
  83. */
  84. public function getThumbnail () {
  85. return $this->thumbnail;
  86. }
  87. /**
  88. * @param string
  89. * @return void
  90. * @access public
  91. */
  92. public function setName ($name) {
  93. $this->name = $name;
  94. }
  95. /**
  96. * @param string
  97. * @return void
  98. * @access public
  99. */
  100. public function setUri ($uri) {
  101. $this->uri = $uri;
  102. }
  103. /**
  104. * @param string
  105. * @return void
  106. * @access public
  107. */
  108. public function setUser ($user) {
  109. $this->user = $user;
  110. }
  111. /**
  112. * @param string
  113. * @return void
  114. * @access public
  115. */
  116. public function setNickname ($nickname) {
  117. $this->nickname = $nickname;
  118. }
  119. /**
  120. * @param string
  121. * @return void
  122. * @access public
  123. */
  124. public function setThumbnail ($thumbnail) {
  125. $this->thumbnail = $thumbnail;
  126. }
  127. /**
  128. * Constructs a Picasa_Author object from XML. It's important to remember that several of the fields in any Picasa_Author
  129. * object will be null or blank. It depends on what type of feed you're requesting. It's best to do trial and error before
  130. * relying on a field to be populated.
  131. *
  132. * @param string $author XML representing the Picasa user, most likely retrieved from an Album or Image object.
  133. */
  134. public function __construct (SimpleXMLElement $author=null) {
  135. if ($author != null) {
  136. $namespaces = $author->getNamespaces(true);
  137. if (array_key_exists("gphoto", $namespaces)) {
  138. $gphoto_ns = $author->children($namespaces["gphoto"]);
  139. $this->nickname = $gphoto_ns->nickname;
  140. $this->thumbnail = $gphoto_ns->thumbnail;
  141. $this->user = $gphoto_ns->user;
  142. }
  143. // If this XML has an interior author entry, go into it
  144. if ($author->author != null && $author->author->name != null && strcmp($author->author->name, "") != 0) {
  145. $author = $author->author;
  146. }
  147. $this->name = $author->name;
  148. $this->uri = $author->uri;
  149. }
  150. }
  151. /**
  152. * Constructs a textual representation of the current instantiation.
  153. *
  154. * @return string
  155. */
  156. public function __toString() {
  157. $retString="
  158. [ TYPE: Picasa_Author
  159. NAME: ".$this->name."
  160. USER: ".$this->user."
  161. NICKNAME: ".$this->nickname."
  162. THUMBNAIL: ".$this->thumbnail."
  163. ]";
  164. return $retString;
  165. }
  166. /**
  167. * Constructs an array of {@link Picasa_Author} objects based on the XML taken from either the $xml parameter or from the contents of $url.
  168. *
  169. * @param string $url A URL pointing to a Picasa Atom feed that has zero or more "entry" nodes represeing
  170. * a Picasa author. Optional, the default is null. If this parameter is null, the method will
  171. * try to get the XML content from the $xml parameter directly.
  172. * @param SimpleXMLElement $xml XML from a Picasa Atom feed that has zero or more "entry" nodes represeing a Picasa author.
  173. * Optional, the default is null. If the $url parameter is null and the $xml parameter is null,
  174. * a {@Picasa_Exception} is thrown.
  175. * @param array $contextArray An array that can be passed to stream_context_create() to generate
  176. * a PHP context. See
  177. * {@link http://us2.php.net/manual/en/function.stream-context-create.php}
  178. * @param boolean $useCache You can decide not to cache a specific request by passing false here. You may
  179. * want to do this, for instance, if you're requesting a private feed.
  180. * @throws {@link Picasa_Exception} If the XML passed (through either parameter) could not be used to construct a {@link SimpleXMLElement}.
  181. * @return array An array of {@link Picasa_Author} objects representing all authors in the requested feed.
  182. * @see http://php.net/simplexml
  183. */
  184. public static function getAuthorArray($url=null, SimpleXMLElement $xml=null, $contextArray=null, $useCache=true) {
  185. if ($url != null) {
  186. $context = null;
  187. $authorXml = false;
  188. if ($contextArray != null) {
  189. $context = stream_context_create($contextArray);
  190. }
  191. if ($useCache === true) {
  192. $authorXml = Picasa_Cache::getCache()->getIfCached($url);
  193. }
  194. if ($authorXml === false) {
  195. Picasa_Logger::getLogger()->logIfEnabled("Not using cached entry for ".$url);
  196. $authorXml = @file_get_contents($url, null, $context);
  197. if ($useCache === true && $authorXml !== false) {
  198. Picasa_Logger::getLogger()->logIfEnabled("Refreshing cache entry for key ".$url);
  199. Picasa_Cache::getCache()->setInCache($url, $authorXml);
  200. }
  201. }
  202. if ($authorXml == false) {
  203. throw Picasa::getExceptionFromInvalidQuery($url);
  204. }
  205. }
  206. try {
  207. // Load the XML file into a SimpleXMLElement
  208. $xml = new SimpleXMLElement($authorXml);
  209. } catch (Exception $e) {
  210. throw new Picasa_Exception($e->getMessage(), null, $url);
  211. }
  212. $authorArray = array();
  213. $i = 0;
  214. foreach($xml->entry as $author) {
  215. $authorArray[$i] = new Picasa_Author($author);
  216. $i++;
  217. }
  218. return $authorArray;
  219. }
  220. }