123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class WikiHowProfilePlugin extends Plugin
- {
- const PLUGIN_VERSION = '2.0.0';
- function onPluginVersion(array &$versions)
- {
- $versions[] = array('name' => 'WikiHow avatar fetcher',
- 'version' => self::PLUGIN_VERSION,
- 'author' => 'Brion Vibber',
- 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Sample',
- 'rawdescription' =>
-
- _m('Fetches avatar and other profile information for WikiHow users when setting up an account via OpenID.'));
- return true;
- }
-
- function onEndOpenIDCreateNewUser($user, $canonical, $sreg)
- {
- $this->updateProfile($user, $canonical);
- return true;
- }
-
- function onEndOpenIDUpdateUser($user, $canonical, $sreg)
- {
- $this->updateProfile($user, $canonical);
- return true;
- }
-
- private function updateProfile($user, $canonical)
- {
- $prefix = 'http://www.wikihow.com/User:';
- if (substr($canonical, 0, strlen($prefix)) == $prefix) {
-
- $profile = $this->fetchProfile($canonical);
- if (!empty($profile['avatar'])) {
- $this->saveAvatar($user, $profile['avatar']);
- }
- }
- }
-
- private function fetchProfile($profileUrl)
- {
- $client = HTTPClient::start();
- $response = $client->get($profileUrl);
- if (!$response->isOk()) {
-
- throw new Exception(_m('WikiHow profile page fetch failed.'));
-
- return false;
- }
-
-
- $old = error_reporting();
- error_reporting($old & ~E_WARNING);
- $dom = new DOMDocument();
- $ok = $dom->loadHTML($response->getBody());
- error_reporting($old);
- if (!$ok) {
-
- throw new Exception(_m('HTML parse failure during check for WikiHow avatar.'));
- return false;
- }
- $data = array();
- $avatar = $dom->getElementById('avatarULimg');
- if ($avatar) {
- $src = $avatar->getAttribute('src');
- $base = new Net_URL2($profileUrl);
- $absolute = $base->resolve($src);
- $avatarUrl = strval($absolute);
- common_log(LOG_DEBUG, "WikiHow avatar found for $profileUrl - $avatarUrl");
- $data['avatar'] = $avatarUrl;
- }
- return $data;
- }
-
- private function saveAvatar($user, $url)
- {
- if (!common_valid_http_url($url)) {
-
-
- throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
- }
-
-
- $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
- try {
- if (!copy($url, $temp_filename)) {
-
-
- throw new ServerException(sprintf(_m('Unable to fetch avatar from %s.'), $url));
- }
- $profile = $user->getProfile();
- $id = $profile->id;
- $imagefile = new ImageFile(null, $temp_filename);
- $filename = Avatar::filename($id,
- image_type_to_extension($imagefile->type),
- null,
- common_timestamp());
- rename($temp_filename, Avatar::path($filename));
- } catch (Exception $e) {
- unlink($temp_filename);
- throw $e;
- }
- $profile->setOriginal($filename);
- }
- }
|