3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * Plugin to pull WikiHow-style user avatars at OpenID setup time.
7 * These are not currently exposed via OpenID.
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.
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.
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/>.
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/
32 if (!defined('STATUSNET')) {
33 // This check helps protect against security problems;
34 // your code file can't be executed directly from the web.
39 * Sample plugin main class
41 * Each plugin requires a main class to interact with the StatusNet system.
44 * @package WikiHowProfilePlugin
45 * @author Brion Vibber <brion@status.net>
46 * @copyright 2010 StatusNet, Inc.
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
48 * @link http://status.net/
50 class WikiHowProfilePlugin extends Plugin
52 function onPluginVersion(array &$versions)
54 $versions[] = array('name' => 'WikiHow avatar fetcher',
55 'version' => GNUSOCIAL_VERSION,
56 'author' => 'Brion Vibber',
57 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Sample',
59 // TRANS: Plugin description.
60 _m('Fetches avatar and other profile information for WikiHow users when setting up an account via OpenID.'));
65 * Hook for OpenID user creation; we'll pull the avatar.
68 * @param string $canonical OpenID provider URL
69 * @param array $sreg query data from provider
71 function onEndOpenIDCreateNewUser($user, $canonical, $sreg)
73 $this->updateProfile($user, $canonical);
78 * Hook for OpenID profile updating; we'll pull the avatar.
81 * @param string $canonical OpenID provider URL (wiki profile page)
82 * @param array $sreg query data from provider
84 function onEndOpenIDUpdateUser($user, $canonical, $sreg)
86 $this->updateProfile($user, $canonical);
92 * @param string $canonical OpenID provider URL (wiki profile page)
94 private function updateProfile($user, $canonical)
96 $prefix = 'http://www.wikihow.com/User:';
98 if (substr($canonical, 0, strlen($prefix)) == $prefix) {
99 // Yes, it's a WikiHow user!
100 $profile = $this->fetchProfile($canonical);
102 if (!empty($profile['avatar'])) {
103 $this->saveAvatar($user, $profile['avatar']);
109 * Given a user's WikiHow profile URL, find their avatar.
111 * @param string $profileUrl user page on the wiki
113 * @return array of data; possible members:
114 * 'avatar' => full URL to avatar image
116 * @throws Exception on various low-level failures
118 * @todo pull location, web site, and about sections -- they aren't currently marked up cleanly.
120 private function fetchProfile($profileUrl)
122 $client = HTTPClient::start();
123 $response = $client->get($profileUrl);
124 if (!$response->isOk()) {
125 // TRANS: Exception thrown when fetching a WikiHow profile page fails.
126 throw new Exception(_m('WikiHow profile page fetch failed.'));
127 // HTTP error response already logged.
131 // Suppress warnings during HTML parsing; non-well-formed bits will
132 // spew horrible warning everywhere even though it works fine.
133 $old = error_reporting();
134 error_reporting($old & ~E_WARNING);
136 $dom = new DOMDocument();
137 $ok = $dom->loadHTML($response->getBody());
139 error_reporting($old);
142 // TRANS: Exception thrown when parsing a WikiHow profile page fails.
143 throw new Exception(_m('HTML parse failure during check for WikiHow avatar.'));
149 $avatar = $dom->getElementById('avatarULimg');
151 $src = $avatar->getAttribute('src');
153 $base = new Net_URL2($profileUrl);
154 $absolute = $base->resolve($src);
155 $avatarUrl = strval($absolute);
157 common_log(LOG_DEBUG, "WikiHow avatar found for $profileUrl - $avatarUrl");
158 $data['avatar'] = $avatarUrl;
165 * Actually save the avatar we found locally.
168 * @param string $url to avatar URL
169 * @todo merge wrapper funcs for this into common place for 1.0 core
171 private function saveAvatar($user, $url)
173 if (!common_valid_http_url($url)) {
174 // TRANS: Server exception thrown when an avatar URL is invalid.
175 // TRANS: %s is the invalid avatar URL.
176 throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
179 // @todo FIXME: This should be better encapsulated
180 // ripped from OStatus via oauthstore.php (for old OMB client)
181 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
183 if (!copy($url, $temp_filename)) {
184 // TRANS: Exception thrown when fetching an avatar from a URL fails.
185 // TRANS: %s is a URL.
186 throw new ServerException(sprintf(_m('Unable to fetch avatar from %s.'), $url));
189 $profile = $user->getProfile();
191 $imagefile = new ImageFile(null, $temp_filename);
192 $filename = Avatar::filename($id,
193 image_type_to_extension($imagefile->type),
196 rename($temp_filename, Avatar::path($filename));
197 } catch (Exception $e) {
198 unlink($temp_filename);
201 $profile->setOriginal($filename);