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/
51 class WikiHowProfilePlugin extends Plugin
53 function onPluginVersion(&$versions)
55 $versions[] = array('name' => 'WikiHow avatar fetcher',
56 'version' => STATUSNET_VERSION,
57 'author' => 'Brion Vibber',
58 'homepage' => 'http://status.net/wiki/Plugin:Sample',
60 _m('Fetches avatar and other profile info 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 throw new Exception("WikiHow profile page fetch failed.");
126 // HTTP error response already logged.
130 // Suppress warnings during HTML parsing; non-well-formed bits will
131 // spew horrible warning everywhere even though it works fine.
132 $old = error_reporting();
133 error_reporting($old & ~E_WARNING);
135 $dom = new DOMDocument();
136 $ok = $dom->loadHTML($response->getBody());
138 error_reporting($old);
141 throw new Exception("HTML parse failure during check for WikiHow avatar.");
147 $avatar = $dom->getElementById('avatarULimg');
149 $src = $avatar->getAttribute('src');
151 $base = new Net_URL2($profileUrl);
152 $absolute = $base->resolve($src);
153 $avatarUrl = strval($absolute);
155 common_log(LOG_DEBUG, "WikiHow avatar found for $profileUrl - $avatarUrl");
156 $data['avatar'] = $avatarUrl;
163 * Actually save the avatar we found locally.
166 * @param string $url to avatar URL
167 * @todo merge wrapper funcs for this into common place for 1.0 core
169 private function saveAvatar($user, $url)
171 if (!common_valid_http_url($url)) {
172 throw new ServerException(sprintf(_m("Invalid avatar URL %s"), $url));
175 // @fixme this should be better encapsulated
176 // ripped from OStatus via oauthstore.php (for old OMB client)
177 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
178 if (!copy($url, $temp_filename)) {
179 throw new ServerException(sprintf(_m("Unable to fetch avatar from %s"), $url));
182 $profile = $user->getProfile();
184 // @fixme should we be using different ids?
186 $imagefile = new ImageFile($id, $temp_filename);
187 $filename = Avatar::filename($id,
188 image_type_to_extension($imagefile->type),
191 rename($temp_filename, Avatar::path($filename));
192 $profile->setOriginal($filename);