]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WikiHowProfile/WikiHowProfilePlugin.php
Fix for ticket 2756 - Calls to OAuth endpoints are redirected to the
[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
51 class WikiHowProfilePlugin extends Plugin
52 {
53     function onPluginVersion(&$versions)
54     {
55         $versions[] = array('name' => 'WikiHow avatar fetcher',
56                             'version' => STATUSNET_VERSION,
57                             'author' => 'Brion Vibber',
58                             'homepage' => 'http://status.net/wiki/Plugin:Sample',
59                             'rawdescription' =>
60                             _m('Fetches avatar and other profile info 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             throw new Exception("WikiHow profile page fetch failed.");
126             // HTTP error response already logged.
127             return false;
128         }
129
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);
134
135         $dom = new DOMDocument();
136         $ok = $dom->loadHTML($response->getBody());
137
138         error_reporting($old);
139
140         if (!$ok) {
141             throw new Exception("HTML parse failure during check for WikiHow avatar.");
142             return false;
143         }
144
145         $data = array();
146
147         $avatar = $dom->getElementById('avatarULimg');
148         if ($avatar) {
149             $src = $avatar->getAttribute('src');
150
151             $base = new Net_URL2($profileUrl);
152             $absolute = $base->resolve($src);
153             $avatarUrl = strval($absolute);
154
155             common_log(LOG_DEBUG, "WikiHow avatar found for $profileUrl - $avatarUrl");
156             $data['avatar'] = $avatarUrl;
157         }
158
159         return $data;
160     }
161
162     /**
163      * Actually save the avatar we found locally.
164      *
165      * @param User $user
166      * @param string $url to avatar URL
167      * @todo merge wrapper funcs for this into common place for 1.0 core
168      */
169     private function saveAvatar($user, $url)
170     {
171         if (!common_valid_http_url($url)) {
172             throw new ServerException(sprintf(_m("Invalid avatar URL %s"), $url));
173         }
174
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));
180         }
181
182         $profile = $user->getProfile();
183         $id = $profile->id;
184         // @fixme should we be using different ids?
185
186         $imagefile = new ImageFile($id, $temp_filename);
187         $filename = Avatar::filename($id,
188                                      image_type_to_extension($imagefile->type),
189                                      null,
190                                      common_timestamp());
191         rename($temp_filename, Avatar::path($filename));
192         $profile->setOriginal($filename);
193     }
194
195 }
196