WikiHowProfilePlugin.php 6.5 KB

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