WikiHowProfilePlugin.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. const PLUGIN_VERSION = '2.0.0';
  51. public function onPluginVersion(array &$versions): bool
  52. {
  53. $versions[] = array('name' => 'WikiHow avatar fetcher',
  54. 'version' => self::PLUGIN_VERSION,
  55. 'author' => 'Brion Vibber',
  56. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Sample',
  57. 'rawdescription' =>
  58. // TRANS: Plugin description.
  59. _m('Fetches avatar and other profile information for WikiHow users when setting up an account via OpenID.'));
  60. return true;
  61. }
  62. /**
  63. * Hook for OpenID user creation; we'll pull the avatar.
  64. *
  65. * @param User $user
  66. * @param string $canonical OpenID provider URL
  67. * @param array $sreg query data from provider
  68. */
  69. function onEndOpenIDCreateNewUser($user, $canonical, $sreg)
  70. {
  71. $this->updateProfile($user, $canonical);
  72. return true;
  73. }
  74. /**
  75. * Hook for OpenID profile updating; we'll pull the avatar.
  76. *
  77. * @param User $user
  78. * @param string $canonical OpenID provider URL (wiki profile page)
  79. * @param array $sreg query data from provider
  80. */
  81. function onEndOpenIDUpdateUser($user, $canonical, $sreg)
  82. {
  83. $this->updateProfile($user, $canonical);
  84. return true;
  85. }
  86. /**
  87. * @param User $user
  88. * @param string $canonical OpenID provider URL (wiki profile page)
  89. */
  90. private function updateProfile($user, $canonical)
  91. {
  92. $prefix = 'http://www.wikihow.com/User:';
  93. if (substr($canonical, 0, strlen($prefix)) == $prefix) {
  94. // Yes, it's a WikiHow user!
  95. $profile = $this->fetchProfile($canonical);
  96. if (!empty($profile['avatar'])) {
  97. $this->saveAvatar($user, $profile['avatar']);
  98. }
  99. }
  100. }
  101. /**
  102. * Given a user's WikiHow profile URL, find their avatar.
  103. *
  104. * @param string $profileUrl user page on the wiki
  105. *
  106. * @return array of data; possible members:
  107. * 'avatar' => full URL to avatar image
  108. *
  109. * @throws Exception on various low-level failures
  110. *
  111. * @todo pull location, web site, and about sections -- they aren't currently marked up cleanly.
  112. */
  113. private function fetchProfile($profileUrl)
  114. {
  115. $client = HTTPClient::start();
  116. $response = $client->get($profileUrl);
  117. if (!$response->isOk()) {
  118. // TRANS: Exception thrown when fetching a WikiHow profile page fails.
  119. throw new Exception(_m('WikiHow profile page fetch failed.'));
  120. // HTTP error response already logged.
  121. return false;
  122. }
  123. // Suppress warnings during HTML parsing; non-well-formed bits will
  124. // spew horrible warning everywhere even though it works fine.
  125. $old = error_reporting();
  126. error_reporting($old & ~E_WARNING);
  127. $dom = new DOMDocument();
  128. $ok = $dom->loadHTML($response->getBody());
  129. error_reporting($old);
  130. if (!$ok) {
  131. // TRANS: Exception thrown when parsing a WikiHow profile page fails.
  132. throw new Exception(_m('HTML parse failure during check for WikiHow avatar.'));
  133. return false;
  134. }
  135. $data = array();
  136. $avatar = $dom->getElementById('avatarULimg');
  137. if ($avatar) {
  138. $src = $avatar->getAttribute('src');
  139. $base = new Net_URL2($profileUrl);
  140. $absolute = $base->resolve($src);
  141. $avatarUrl = strval($absolute);
  142. common_log(LOG_DEBUG, "WikiHow avatar found for $profileUrl - $avatarUrl");
  143. $data['avatar'] = $avatarUrl;
  144. }
  145. return $data;
  146. }
  147. /**
  148. * Actually save the avatar we found locally.
  149. *
  150. * @param User $user
  151. * @param string $url to avatar URL
  152. * @todo merge wrapper funcs for this into common place for 1.0 core
  153. */
  154. private function saveAvatar($user, $url)
  155. {
  156. if (!common_valid_http_url($url)) {
  157. // TRANS: Server exception thrown when an avatar URL is invalid.
  158. // TRANS: %s is the invalid avatar URL.
  159. throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
  160. }
  161. // @todo FIXME: This should be better encapsulated
  162. // ripped from OStatus via oauthstore.php (for old OMB client)
  163. $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
  164. try {
  165. if (!copy($url, $temp_filename)) {
  166. // TRANS: Exception thrown when fetching an avatar from a URL fails.
  167. // TRANS: %s is a URL.
  168. throw new ServerException(sprintf(_m('Unable to fetch avatar from %s.'), $url));
  169. }
  170. $profile = $user->getProfile();
  171. $id = $profile->id;
  172. $imagefile = new ImageFile(null, $temp_filename);
  173. $filename = Avatar::filename($id,
  174. image_type_to_extension($imagefile->type),
  175. null,
  176. common_timestamp());
  177. rename($temp_filename, Avatar::path($filename));
  178. } catch (Exception $e) {
  179. unlink($temp_filename);
  180. throw $e;
  181. }
  182. $profile->setOriginal($filename);
  183. }
  184. }