]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WikiHowProfile/WikiHowProfilePlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / WikiHowProfile / WikiHowProfilePlugin.php
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
32 if (!defined('STATUSNET')) {
33     // This check helps protect against security problems;
34     // your code file can't be executed directly from the web.
35     exit(1);
36 }
37
38 /**
39  * Sample plugin main class
40  *
41  * Each plugin requires a main class to interact with the StatusNet system.
42  *
43  * @category  Plugins
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/
49  */
50 class WikiHowProfilePlugin extends Plugin
51 {
52     function onPluginVersion(array &$versions)
53     {
54         $versions[] = array('name' => 'WikiHow avatar fetcher',
55                             'version' => GNUSOCIAL_VERSION,
56                             'author' => 'Brion Vibber',
57                             'homepage' => 'http://status.net/wiki/Plugin:Sample',
58                             'rawdescription' =>
59                             // TRANS: Plugin description.
60                             _m('Fetches avatar and other profile information for WikiHow users when setting up an account via OpenID.'));
61         return true;
62     }
63
64     /**
65      * Hook for OpenID user creation; we'll pull the avatar.
66      *
67      * @param User $user
68      * @param string $canonical OpenID provider URL
69      * @param array $sreg query data from provider
70      */
71     function onEndOpenIDCreateNewUser($user, $canonical, $sreg)
72     {
73         $this->updateProfile($user, $canonical);
74         return true;
75     }
76
77     /**
78      * Hook for OpenID profile updating; we'll pull the avatar.
79      *
80      * @param User $user
81      * @param string $canonical OpenID provider URL (wiki profile page)
82      * @param array $sreg query data from provider
83      */
84     function onEndOpenIDUpdateUser($user, $canonical, $sreg)
85     {
86         $this->updateProfile($user, $canonical);
87         return true;
88     }
89
90     /**
91      * @param User $user
92      * @param string $canonical OpenID provider URL (wiki profile page)
93      */
94     private function updateProfile($user, $canonical)
95     {
96         $prefix = 'http://www.wikihow.com/User:';
97
98         if (substr($canonical, 0, strlen($prefix)) == $prefix) {
99             // Yes, it's a WikiHow user!
100             $profile = $this->fetchProfile($canonical);
101
102             if (!empty($profile['avatar'])) {
103                 $this->saveAvatar($user, $profile['avatar']);
104             }
105         }
106     }
107
108     /**
109      * Given a user's WikiHow profile URL, find their avatar.
110      *
111      * @param string $profileUrl user page on the wiki
112      *
113      * @return array of data; possible members:
114      *               'avatar' => full URL to avatar image
115      *
116      * @throws Exception on various low-level failures
117      *
118      * @todo pull location, web site, and about sections -- they aren't currently marked up cleanly.
119      */
120     private function fetchProfile($profileUrl)
121     {
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.
128             return false;
129         }
130
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);
135
136         $dom = new DOMDocument();
137         $ok = $dom->loadHTML($response->getBody());
138
139         error_reporting($old);
140
141         if (!$ok) {
142             // TRANS: Exception thrown when parsing a WikiHow profile page fails.
143             throw new Exception(_m('HTML parse failure during check for WikiHow avatar.'));
144             return false;
145         }
146
147         $data = array();
148
149         $avatar = $dom->getElementById('avatarULimg');
150         if ($avatar) {
151             $src = $avatar->getAttribute('src');
152
153             $base = new Net_URL2($profileUrl);
154             $absolute = $base->resolve($src);
155             $avatarUrl = strval($absolute);
156
157             common_debug("WikiHow avatar found for $profileUrl - $avatarUrl");
158             $data['avatar'] = $avatarUrl;
159         }
160
161         return $data;
162     }
163
164     /**
165      * Actually save the avatar we found locally.
166      *
167      * @param User $user
168      * @param string $url to avatar URL
169      * @todo merge wrapper funcs for this into common place for 1.0 core
170      */
171     private function saveAvatar($user, $url)
172     {
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));
177         }
178
179         // @todo FIXME: This should be better encapsulated
180         // ripped from OStatus via oauthstore.php (for old OMB client)
181         $temp_filename = tempnam(common_get_temp_dir(), 'listener_avatar');
182         try {
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));
187             }
188
189             $profile = $user->getProfile();
190             $id = $profile->id;
191             $imagefile = new ImageFile(null, $temp_filename);
192             $filename = Avatar::filename($id,
193                                          image_type_to_extension($imagefile->type),
194                                          null,
195                                          common_timestamp());
196             rename($temp_filename, Avatar::path($filename));
197         } catch (Exception $e) {
198             unlink($temp_filename);
199             throw $e;
200         }
201         $profile->setOriginal($filename);
202     }
203 }