3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2009, StatusNet, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * @package GravatarPlugin
22 * @maintainer Eric Helgeson <erichelgeson@gmail.com>
25 if (!defined('STATUSNET') && !defined('LACONICA')) {
26 // This check helps protect against security problems;
27 // your code file can't be executed directly from the web.
31 class GravatarPlugin extends Plugin
33 function onInitializePlugin()
38 function onStartAvatarFormData($action)
40 $user = common_current_user();
41 $hasGravatar = $this->hasGravatar($user->id);
48 function onEndAvatarFormData($action)
50 $user = common_current_user();
51 $hasGravatar = $this->hasGravatar($user->id);
53 if(!empty($user->email) && !$hasGravatar) { //and not gravatar already set
54 $action->elementStart('form', array('method' => 'post',
55 'id' => 'form_settings_gravatar_add',
56 'class' => 'form_settings',
58 common_local_url('avatarsettings')));
59 $action->elementStart('fieldset', array('id' => 'settings_gravatar_add'));
60 $action->element('legend', null, _m('Set Gravatar'));
61 $action->hidden('token', common_session_token());
62 $action->element('p', 'form_guide',
63 _m('If you want to use your Gravatar image, click "Add".'));
64 $action->element('input', array('type' => 'submit',
65 'id' => 'settings_gravatar_add_action-submit',
68 'value' => _m('Add')));
69 $action->elementEnd('fieldset');
70 $action->elementEnd('form');
71 } elseif($hasGravatar) {
72 $action->elementStart('form', array('method' => 'post',
73 'id' => 'form_settings_gravatar_remove',
74 'class' => 'form_settings',
76 common_local_url('avatarsettings')));
77 $action->elementStart('fieldset', array('id' => 'settings_gravatar_remove'));
78 $action->element('legend', null, _m('Remove Gravatar'));
79 $action->hidden('token', common_session_token());
80 $action->element('p', 'form_guide',
81 _m('If you want to remove your Gravatar image, click "Remove".'));
82 $action->element('input', array('type' => 'submit',
83 'id' => 'settings_gravatar_remove_action-submit',
86 'value' => _m('Remove')));
87 $action->elementEnd('fieldset');
88 $action->elementEnd('form');
90 $action->element('p', 'form_guide',
91 _m('To use a Gravatar first enter in an email address.'));
95 function onStartAvatarSaveForm($action)
97 if ($action->arg('add')) {
98 $result = $this->gravatar_save();
100 if($result['success']===true) {
101 common_broadcast_profile(common_current_user()->getProfile());
104 $action->showForm($result['message'], $result['success']);
107 } else if ($action->arg('remove')) {
108 $result = $this->gravatar_remove();
110 if($result['success']===true) {
111 common_broadcast_profile(common_current_user()->getProfile());
114 $action->showForm($result['message'], $result['success']);
122 function hasGravatar($id) {
123 $avatar = new Avatar();
124 $avatar->profile_id = $id;
125 if ($avatar->find()) {
126 while ($avatar->fetch()) {
127 if($avatar->filename == null) {
135 function gravatar_save()
137 $cur = common_current_user();
139 if(empty($cur->email)) {
140 return array('message' => _m('You do not have an email address set in your profile.'),
143 //Get rid of previous Avatar
144 $this->gravatar_remove();
146 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
147 $gravatar = new Avatar();
148 $gravatar->profile_id = $cur->id;
149 $gravatar->width = $size;
150 $gravatar->height = $size;
151 $gravatar->original = false; //No file, so no original
152 $gravatar->mediatype = 'img';//XXX: Unsure what to put here
153 //$gravatar->filename = null;//No filename. Remote
154 $gravatar->url = $this->gravatar_url($cur->email, $size);
155 $gravatar->created = DB_DataObject_Cast::dateTime(); # current time
157 if (!$gravatar->insert()) {
158 return array('message' => _m('Failed to save Gravatar to the database.'),
162 return array('message' => _m('Gravatar added.'),
166 function gravatar_remove()
168 $user = common_current_user();
169 $profile = $user->getProfile();
171 $avatar = $profile->getOriginalAvatar();
172 if($avatar) $avatar->delete();
173 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
174 if($avatar) $avatar->delete();
175 $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
176 if($avatar) $avatar->delete();
177 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
178 if($avatar) $avatar->delete();
180 return array('message' => _m('Gravatar removed.'),
184 function gravatar_url($email, $size)
186 $url = "http://www.gravatar.com/avatar.php?gravatar_id=".
187 md5(strtolower($email)).
188 "&default=".urlencode(Avatar::defaultImage($size)).
193 function onPluginVersion(&$versions)
195 $versions[] = array('name' => 'Gravatar',
196 'version' => STATUSNET_VERSION,
197 'author' => 'Eric Helgeson',
198 'homepage' => 'http://status.net/wiki/Plugin:Gravatar',
200 _m('The Gravatar plugin allows users to use their <a href="http://www.gravatar.com/">Gravatar</a> with StatusNet.'));