PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/kohana/gravatar/xmlrpc.php

https://github.com/mintbridge/kohana-gravatar
PHP | 309 lines | 126 code | 34 blank | 149 comment | 9 complexity | 01fecff6313ce73fef9346b86fa19108 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * [ref-gravatar] Gravatars are universal avatars available to all web sites and services.
  4. * Users must register their email addresses with Gravatar before their avatars will be
  5. * usable with this module. Users with gravatars can have a default image of your selection.
  6. *
  7. * @see http://en.gravatar.com/
  8. *
  9. * @package Kohana
  10. * @category Gravatar
  11. * @version 3.1.0
  12. * @author Kohana Team
  13. * @copyright (c) 2009-2010 Kohana Team
  14. * @license http://kohanaphp.com/license
  15. */
  16. class Kohana_Gravatar_Xmlrpc {
  17. /**
  18. * Gravatar Ratings constants
  19. */
  20. const G = 0;
  21. const PG = 1;
  22. const R = 2;
  23. const X = 3;
  24. /**
  25. * Create an instance of the Gravatar XMLRPC API client
  26. *
  27. * @param array $config
  28. * @return Gravatar_Xmlrpc
  29. * @access public
  30. */
  31. public static function factory($config = array())
  32. {
  33. return new Gravatar_Xmlrpc($config);
  34. }
  35. /**
  36. * @var array
  37. */
  38. protected $_config;
  39. /**
  40. * Constructor
  41. *
  42. * @param array $config
  43. * @throws Kohana_Gravatar_Xmlrpc_Exception
  44. */
  45. public function __construct($config)
  46. {
  47. // Check for soap
  48. if ( ! extension_loaded('XMLRPC'))
  49. {
  50. throw new Kohana_Gravatar_Xmlrpc_Exception('XML-RPC extension must be loaded to use this class!');
  51. }
  52. // Configure this library
  53. $config += Kohana::config('gravatar.xmlrpc');
  54. $this->_config = $config;
  55. }
  56. /**
  57. * Set or get the password
  58. *
  59. * @param string $password
  60. * @return mixed
  61. */
  62. public function password($password = NULL)
  63. {
  64. // If there is no API key supplied
  65. if ($password === NULL)
  66. {
  67. return $this->_config['password'];
  68. }
  69. // Else set the API key
  70. $this->_config['password'] = (string) $password;
  71. // Return this
  72. return $this;
  73. }
  74. /**
  75. * Set or get the email address in question
  76. *
  77. * @param string $email [Optional]
  78. * @return mixed
  79. */
  80. public function email($email = NULL)
  81. {
  82. // If no argument, return the email address
  83. if ($email === NULL)
  84. {
  85. return $this->_config['email'];
  86. }
  87. // Set the email address
  88. $this->_config['email'];
  89. // Return this
  90. return $this;
  91. }
  92. /**
  93. * Checks that a hash exists
  94. *
  95. * @param array $hashes
  96. * @return array
  97. */
  98. public function exists(array $hashes)
  99. {
  100. return $this->_xmlrpc_request('grav.exists', array('hashes' => $hashes));
  101. }
  102. /**
  103. * Returns an array of email addresses
  104. * registered to the account API key
  105. *
  106. * @return array
  107. */
  108. public function addresses()
  109. {
  110. return $this->_xmlrpc_request('grav.addresses');
  111. }
  112. /**
  113. * Returns an array containing images
  114. * registered to this user - and their
  115. * respective rating
  116. *
  117. * @return array
  118. */
  119. public function user_images()
  120. {
  121. return $this->_xmlrpc_request('grav.userimages');
  122. }
  123. /**
  124. * Save an image to the registered account.
  125. * Images must be transferred in raw base64
  126. * encoded format.
  127. *
  128. * @param string $image
  129. * @param int $rating [Optional]
  130. * @return string|boolean
  131. * @throws Kohana_Gravatar_Xmlrpc_Exception
  132. */
  133. public function save_data($image, $rating = NULL)
  134. {
  135. // Load the image
  136. if ( ! $resource = file_get_contents($image))
  137. {
  138. throw new Kohana_Gravatar_Xmlrpc_Exception(__METHOD__.' unable to open image : :file', array(':file' => $image));
  139. }
  140. // If no rating has been applied, use general
  141. if ($rating === NULL)
  142. {
  143. $rating = Gravatar_Xmlrpc::G;
  144. }
  145. // Encode the image resource
  146. $encoded_image = base64_encode($resource);
  147. // Save the image to Gravatar
  148. return $this->_xmlrpc_request('grav.saveData', array('data' => $encoded_image, 'rating' => $rating));
  149. }
  150. /**
  151. * Save a image URL to the registered account.
  152. *
  153. * @param string $url
  154. * @param int $rating [Optional]
  155. * @return string|boolean
  156. * @throws Kohana_Gravatar_Xmlrpc_Exception
  157. */
  158. public function save_url($url, $rating = NULL)
  159. {
  160. // If the URL supplied is not valid
  161. if ( ! Validate::url($url))
  162. {
  163. // Throw an exception
  164. throw new Kohana_Gravatar_Xmlrpc_Exception(__METHOD__.' invalid URL supplied : :url', array(':url' => $url));
  165. }
  166. // If no rating has been applied, use general
  167. if ($rating === NULL)
  168. {
  169. $rating = Gravatar_Xmlrpc::G;
  170. }
  171. // Save the image to Gravatar
  172. return $this->_xmlrpc_request('grav.saveData', array('url' => $url, 'rating' => $rating));
  173. }
  174. /**
  175. * Assign a user image on Gravatar to the addresses
  176. * supplied.
  177. *
  178. * @param string user_image
  179. * @param array addresses
  180. * @return array
  181. */
  182. public function use_user_image($user_image, array $addresses)
  183. {
  184. // Set the userimage to all supplied addresses
  185. return $this->_xmlrpc_request('grav.useUserimage', array('userImage' => $user_image, 'addresses' => $addresses));
  186. }
  187. /**
  188. * Remove the userimage associated with one or more email addresses
  189. *
  190. * @param array addresses
  191. * @return boolean
  192. */
  193. public function remove_image(array $addresses)
  194. {
  195. // Remove the current image from the supplied addresses
  196. return $this->_xmlrpc_request('grav.removeImage', array('addresses' => $addresses));
  197. }
  198. /**
  199. * Remove a user image from the account and any email addresses with which it is associated
  200. *
  201. * @param string user_image
  202. * @return boolean
  203. */
  204. public function delete_user_image($user_image)
  205. {
  206. // Delete an image from the account
  207. return $this->_xmlrpc_request('grav.deleteUserimage', array('userimage' => $user_image));
  208. }
  209. /**
  210. * Processes an XML-RPC response based on the method and parameters passed
  211. * to it. This method uses PHP streams over cURL, which makes it available
  212. * to the vast majority of systems.
  213. *
  214. * @param string method
  215. * @param array parameters
  216. * @return mixed
  217. * @throws Kohana_Gravatar_Xmlrpc_Exception
  218. */
  219. protected function _xmlrpc_request($method, array $parameters = array())
  220. {
  221. // Create the endpoint
  222. $endpoint = $this->_prepare_service_endpoint();
  223. // Apply the Gravatar user password to the parameters
  224. $parameters['password'] = $this->_config['password'];
  225. // Create the XML-RPC request
  226. $xml_payload = xmlrpc_encode_request($method, $parameters);
  227. // Create a context stream, enforcing a POST request with correct header
  228. $context = stream_context_create(array(
  229. 'http' => array(
  230. 'method' => 'POST',
  231. 'header' => 'Content-Type: text/xml',
  232. 'content' => $xml_payload
  233. )
  234. ));
  235. try
  236. {
  237. // Process the XML-RPC request and decode the response
  238. $xmlrpc_response = xmlrpc_decode(file_get_contents($endpoint, FALSE, $context));
  239. // If there was an error
  240. if ($xmlrpc_response and xmlrpc_is_fault($xmlrpc_response))
  241. {
  242. // Throw an exception
  243. throw new Kohana_Gravatar_Xmlrpc_Exception($xmlrpc_response['faultString'], NULL, $xmlrpc_response['faultCode']);
  244. }
  245. else
  246. {
  247. // return the response
  248. return $xmlrpc_response;
  249. }
  250. // Shouldn't get this far
  251. throw new Kohana_Gravatar_Xmlrpc_Exception(__METHOD__.' something went wrong, xmlrpc_response was empty');
  252. }
  253. // Catch all unexpected exceptions
  254. catch (Exception $e)
  255. {
  256. throw new Kohana_Gravatar_Xmlrpc_Exception($e->getMessage(), NULL, $e->getCode());
  257. }
  258. }
  259. /**
  260. * Execute the Xmlrpc request and return
  261. * the result
  262. *
  263. * @return mixed
  264. * @throws Kohana_Gravatar_Xmlrpc_Exception
  265. */
  266. protected function _prepare_service_endpoint()
  267. {
  268. if ($this->_config['email'] === NULL)
  269. {
  270. throw new Kohana_Gravatar_Xmlrpc_Exception('Username must be supplied to perform Gravatar API requests!');
  271. }
  272. // Generate full uri with user id
  273. return $this->_config['service'].'?user='.md5($this->_config['email']);
  274. }
  275. }