WikiHowProfilePlugin.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * Plugin to pull WikiHow-style user avatars at OpenID setup time.
  7. * These are not currently exposed via OpenID.
  8. *
  9. * PHP version 5
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * @category Plugins
  25. * @package StatusNet
  26. * @author Brion Vibber <brion@status.net>
  27. * @copyright 2010 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. // This check helps protect against security problems;
  33. // your code file can't be executed directly from the web.
  34. exit(1);
  35. }
  36. /**
  37. * Sample plugin main class
  38. *
  39. * Each plugin requires a main class to interact with the StatusNet system.
  40. *
  41. * @category Plugins
  42. * @package WikiHowProfilePlugin
  43. * @author Brion Vibber <brion@status.net>
  44. * @copyright 2010 StatusNet, Inc.
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  46. * @link http://status.net/
  47. */
  48. class WikiHowProfilePlugin extends Plugin
  49. {
  50. function onPluginVersion(array &$versions)
  51. {
  52. $versions[] = array('name' => 'WikiHow avatar fetcher',
  53. 'version' => GNUSOCIAL_VERSION,
  54. 'author' => 'Brion Vibber',
  55. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Sample',
  56. 'rawdescription' =>
  57. // TRANS: Plugin description.
  58. _m('Fetches avatar and other profile information for WikiHow users when setting up an account via OpenID.'));
  59. return true;
  60. }
  61. /**
  62. * Hook for OpenID user creation; we'll pull the avatar.
  63. *
  64. * @param User $user
  65. * @param string $canonical OpenID provider URL
  66. * @param array $sreg query data from provider
  67. */
  68. function onEndOpenIDCreateNewUser($user, $canonical, $sreg)
  69. {
  70. $this->updateProfile($user, $canonical);
  71. return true;
  72. }
  73. /**
  74. * Hook for OpenID profile updating; we'll pull the avatar.
  75. *
  76. * @param User $user
  77. * @param string $canonical OpenID provider URL (wiki profile page)
  78. * @param array $sreg query data from provider
  79. */
  80. function onEndOpenIDUpdateUser($user, $canonical, $sreg)
  81. {
  82. $this->updateProfile($user, $canonical);
  83. return true;
  84. }
  85. /**
  86. * @param User $user
  87. * @param string $canonical OpenID provider URL (wiki profile page)
  88. */
  89. private function updateProfile($user, $canonical)
  90. {
  91. $prefix = 'http://www.wikihow.com/User:';
  92. if (substr($canonical, 0, strlen($prefix)) == $prefix) {
  93. // Yes, it's a WikiHow user!
  94. $profile = $this->fetchProfile($canonical);
  95. if (!empty($profile['avatar'])) {
  96. $this->saveAvatar($user, $profile['avatar']);
  97. }
  98. }
  99. }
  100. /**
  101. * Given a user's WikiHow profile URL, find their avatar.
  102. *
  103. * @param string $profileUrl user page on the wiki
  104. *
  105. * @return array of data; possible members:
  106. * 'avatar' => full URL to avatar image
  107. *
  108. * @throws Exception on various low-level failures
  109. *
  110. * @todo pull location, web site, and about sections -- they aren't currently marked up cleanly.
  111. */
  112. private function fetchProfile($profileUrl)
  113. {
  114. $client = HTTPClient::start();
  115. $response = $client->get($profileUrl);
  116. if (!$response->isOk()) {
  117. // TRANS: Exception thrown when fetching a WikiHow profile page fails.
  118. throw new Exception(_m('WikiHow profile page fetch failed.'));
  119. // HTTP error response already logged.
  120. return false;
  121. }
  122. // Suppress warnings during HTML parsing; non-well-formed bits will
  123. // spew horrible warning everywhere even though it works fine.
  124. $old = error_reporting();
  125. error_reporting($old & ~E_WARNING);
  126. $dom = new DOMDocument();
  127. $ok = $dom->loadHTML($response->getBody());
  128. error_reporting($old);
  129. if (!$ok) {
  130. // TRANS: Exception thrown when parsing a WikiHow profile page fails.
  131. throw new Exception(_m('HTML parse failure during check for WikiHow avatar.'));
  132. return false;
  133. }
  134. $data = array();
  135. $avatar = $dom->getElementById('avatarULimg');
  136. if ($avatar) {
  137. $src = $avatar->getAttribute('src');
  138. $base = new Net_URL2($profileUrl);
  139. $absolute = $base->resolve($src);
  140. $avatarUrl = strval($absolute);
  141. common_log(LOG_DEBUG, "WikiHow avatar found for $profileUrl - $avatarUrl");
  142. $data['avatar'] = $avatarUrl;
  143. }
  144. return $data;
  145. }
  146. /**
  147. * Actually save the avatar we found locally.
  148. *
  149. * @param User $user
  150. * @param string $url to avatar URL
  151. * @todo merge wrapper funcs for this into common place for 1.0 core
  152. */
  153. private function saveAvatar($user, $url)
  154. {
  155. if (!common_valid_http_url($url)) {
  156. // TRANS: Server exception thrown when an avatar URL is invalid.
  157. // TRANS: %s is the invalid avatar URL.
  158. throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
  159. }
  160. // @todo FIXME: This should be better encapsulated
  161. // ripped from OStatus via oauthstore.php (for old OMB client)
  162. $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
  163. try {
  164. if (!copy($url, $temp_filename)) {
  165. // TRANS: Exception thrown when fetching an avatar from a URL fails.
  166. // TRANS: %s is a URL.
  167. throw new ServerException(sprintf(_m('Unable to fetch avatar from %s.'), $url));
  168. }
  169. $profile = $user->getProfile();
  170. $id = $profile->id;
  171. $imagefile = new ImageFile(null, $temp_filename);
  172. $filename = Avatar::filename($id,
  173. image_type_to_extension($imagefile->type),
  174. null,
  175. common_timestamp());
  176. rename($temp_filename, Avatar::path($filename));
  177. } catch (Exception $e) {
  178. unlink($temp_filename);
  179. throw $e;
  180. }
  181. $profile->setOriginal($filename);
  182. }
  183. }