]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/WikiHowProfile/WikiHowProfilePlugin.php
Merge remote branch 'statusnet/0.9.x' into 1.0.x
[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(&$versions)
53     {
54         $versions[] = array('name' => 'WikiHow avatar fetcher',
55                             'version' => STATUSNET_VERSION,
56                             'author' => 'Brion Vibber',
57                             'homepage' => 'http://status.net/wiki/Plugin:Sample',
58                             'rawdescription' =>
59                             _m('Fetches avatar and other profile information for WikiHow users when setting up an account via OpenID.'));
60         return true;
61     }
62
63     /**
64      * Hook for OpenID user creation; we'll pull the avatar.
65      *
66      * @param User $user
67      * @param string $canonical OpenID provider URL
68      * @param array $sreg query data from provider
69      */
70     function onEndOpenIDCreateNewUser($user, $canonical, $sreg)
71     {
72         $this->updateProfile($user, $canonical);
73         return true;
74     }
75
76     /**
77      * Hook for OpenID profile updating; we'll pull the avatar.
78      *
79      * @param User $user
80      * @param string $canonical OpenID provider URL (wiki profile page)
81      * @param array $sreg query data from provider
82      */
83     function onEndOpenIDUpdateUser($user, $canonical, $sreg)
84     {
85         $this->updateProfile($user, $canonical);
86         return true;
87     }
88
89     /**
90      * @param User $user
91      * @param string $canonical OpenID provider URL (wiki profile page)
92      */
93     private function updateProfile($user, $canonical)
94     {
95         $prefix = 'http://www.wikihow.com/User:';
96
97         if (substr($canonical, 0, strlen($prefix)) == $prefix) {
98             // Yes, it's a WikiHow user!
99             $profile = $this->fetchProfile($canonical);
100
101             if (!empty($profile['avatar'])) {
102                 $this->saveAvatar($user, $profile['avatar']);
103             }
104         }
105     }
106
107     /**
108      * Given a user's WikiHow profile URL, find their avatar.
109      *
110      * @param string $profileUrl user page on the wiki
111      *
112      * @return array of data; possible members:
113      *               'avatar' => full URL to avatar image
114      *
115      * @throws Exception on various low-level failures
116      *
117      * @todo pull location, web site, and about sections -- they aren't currently marked up cleanly.
118      */
119     private function fetchProfile($profileUrl)
120     {
121         $client = HTTPClient::start();
122         $response = $client->get($profileUrl);
123         if (!$response->isOk()) {
124             throw new Exception("WikiHow profile page fetch failed.");
125             // HTTP error response already logged.
126             return false;
127         }
128
129         // Suppress warnings during HTML parsing; non-well-formed bits will
130         // spew horrible warning everywhere even though it works fine.
131         $old = error_reporting();
132         error_reporting($old & ~E_WARNING);
133
134         $dom = new DOMDocument();
135         $ok = $dom->loadHTML($response->getBody());
136
137         error_reporting($old);
138
139         if (!$ok) {
140             throw new Exception("HTML parse failure during check for WikiHow avatar.");
141             return false;
142         }
143
144         $data = array();
145
146         $avatar = $dom->getElementById('avatarULimg');
147         if ($avatar) {
148             $src = $avatar->getAttribute('src');
149
150             $base = new Net_URL2($profileUrl);
151             $absolute = $base->resolve($src);
152             $avatarUrl = strval($absolute);
153
154             common_log(LOG_DEBUG, "WikiHow avatar found for $profileUrl - $avatarUrl");
155             $data['avatar'] = $avatarUrl;
156         }
157
158         return $data;
159     }
160
161     /**
162      * Actually save the avatar we found locally.
163      *
164      * @param User $user
165      * @param string $url to avatar URL
166      * @todo merge wrapper funcs for this into common place for 1.0 core
167      */
168     private function saveAvatar($user, $url)
169     {
170         if (!common_valid_http_url($url)) {
171             throw new ServerException(sprintf(_m("Invalid avatar URL %s."), $url));
172         }
173
174         // @fixme this should be better encapsulated
175         // ripped from OStatus via oauthstore.php (for old OMB client)
176         $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
177         try {
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         } catch (Exception $e) {
193             unlink($temp_filename);
194             throw $e;
195         }
196         $profile->setOriginal($filename);
197     }
198 }