]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Gravatar/GravatarPlugin.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[quix0rs-gnu-social.git] / plugins / Gravatar / GravatarPlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2009, StatusNet, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 /**
21  * @package GravatarPlugin
22  * @maintainer Eric Helgeson <erichelgeson@gmail.com>
23  */
24
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.
28     exit(1);
29 }
30
31 class GravatarPlugin extends Plugin
32 {
33     function onInitializePlugin()
34     {
35         return true;
36     }
37
38     function onStartAvatarFormData($action)
39     {
40         $user = common_current_user();
41         $hasGravatar = $this->hasGravatar($user->id);
42
43         if($hasGravatar) {
44             return false;
45         }
46     }
47
48     function onEndAvatarFormData($action)
49     {
50         $user = common_current_user();
51         $hasGravatar = $this->hasGravatar($user->id);
52
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',
57                                                 'action' =>
58                                                 common_local_url('avatarsettings')));
59             $action->elementStart('fieldset', array('id' => 'settings_gravatar_add'));
60             // TRANS: Fieldset legend. Gravatar is an avatar service.
61             $action->element('legend', null, _m('Set Gravatar'));
62             $action->hidden('token', common_session_token());
63             $action->element('p', 'form_guide',
64                              // TRANS: Form guide. Gravatar is an avatar service.
65                              _m('If you want to use your Gravatar image, click "Add".'));
66             $action->element('input', array('type' => 'submit',
67                                             'id' => 'settings_gravatar_add_action-submit',
68                                             'name' => 'add',
69                                             'class' => 'submit',
70                                             // TRANS: Button text to add a Gravatar. Gravatar is an avatar service.
71                                             'value' => _m('BUTTON','Add')));
72             $action->elementEnd('fieldset');
73             $action->elementEnd('form');
74         } elseif($hasGravatar) {
75             $action->elementStart('form', array('method' => 'post',
76                                                 'id' => 'form_settings_gravatar_remove',
77                                                 'class' => 'form_settings',
78                                                 'action' =>
79                                                 common_local_url('avatarsettings')));
80             $action->elementStart('fieldset', array('id' => 'settings_gravatar_remove'));
81             // TRANS: Fieldset legend. Gravatar is an avatar service.
82             $action->element('legend', null, _m('Remove Gravatar'));
83             $action->hidden('token', common_session_token());
84             $action->element('p', 'form_guide',
85                              // TRANS: Form guide. Gravatar is an avatar service.
86                              _m('If you want to remove your Gravatar image, click "Remove".'));
87             $action->element('input', array('type' => 'submit',
88                                             'id' => 'settings_gravatar_remove_action-submit',
89                                             'name' => 'remove',
90                                             'class' => 'submit',
91                                             // TRANS: Button text to remove a Gravatar. Gravatar is an avatar service.
92                                             'value' => _m('Remove')));
93             $action->elementEnd('fieldset');
94             $action->elementEnd('form');
95         } else {
96             $action->element('p', 'form_guide',
97                              // TRANS: Form guide. Gravatar is an avatar service.
98                              _m('To use a Gravatar first enter in an email address.'));
99         }
100     }
101
102     function onStartAvatarSaveForm($action)
103     {
104         if ($action->arg('add')) {
105             $result = $this->gravatar_save();
106
107             if($result['success']===true) {
108                 common_broadcast_profile(common_current_user()->getProfile());
109             }
110
111             $action->showForm($result['message'], $result['success']);
112
113             return false;
114         } else if ($action->arg('remove')) {
115             $result = $this->gravatar_remove();
116
117             if($result['success']===true) {
118                 common_broadcast_profile(common_current_user()->getProfile());
119             }
120
121             $action->showForm($result['message'], $result['success']);
122
123             return false;
124         } else {
125             return true;
126         }
127     }
128
129     function hasGravatar($id) {
130         $avatar = new Avatar();
131         $avatar->profile_id = $id;
132         if ($avatar->find()) {
133             while ($avatar->fetch()) {
134                 if($avatar->filename == null) {
135                     return true;
136                 }
137             }
138         }
139         return false;
140      }
141
142     function gravatar_save()
143     {
144         $cur = common_current_user();
145
146         if(empty($cur->email)) {
147             // TRANS: Message displayed when no e-mail address was set when saving Gravatar setting. Gravatar is an avatar service.
148             return array('message' => _m('You do not have an email address set in your profile.'),
149                          'success' => false);
150         }
151         //Get rid of previous Avatar
152         $this->gravatar_remove();
153
154         foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
155             $gravatar = new Avatar();
156             $gravatar->profile_id = $cur->id;
157             $gravatar->width = $size;
158             $gravatar->height = $size;
159             $gravatar->original = false; //No file, so no original
160             $gravatar->mediatype = 'img';//XXX: Unsure what to put here
161             //$gravatar->filename = null;//No filename. Remote
162             $gravatar->url = $this->gravatar_url($cur->email, $size);
163             $gravatar->created = DB_DataObject_Cast::dateTime(); # current time
164
165             if (!$gravatar->insert()) {
166                 // TRANS: Message displayed when saving Gravatar setting fails. Gravatar is an avatar service.
167                 return array('message' => _m('Failed to save Gravatar to the database.'),
168                              'success' => false);
169             }
170         }
171         // TRANS: Message displayed when Gravatar was added. Gravatar is an avatar service.
172         return array('message' => _m('Gravatar added.'),
173                      'success' => true);
174      }
175
176     function gravatar_remove()
177     {
178         $user = common_current_user();
179         $profile = $user->getProfile();
180
181         $avatar = $profile->getOriginalAvatar();
182         if($avatar) $avatar->delete();
183         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
184         if($avatar) $avatar->delete();
185         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
186         if($avatar) $avatar->delete();
187         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
188         if($avatar) $avatar->delete();
189
190         // TRANS: Message displayed when Gravatar was removed. Gravatar is an avatar service.
191         return array('message' => _m('Gravatar removed.'),
192                      'success' => true);
193     }
194
195     function gravatar_url($email, $size)
196     {
197         $url = "https://secure.gravatar.com/avatar.php?gravatar_id=".
198                 md5(strtolower($email)).
199                 "&default=".urlencode(Avatar::defaultImage($size)).
200                 "&size=".$size;
201             return $url;
202     }
203
204     function onPluginVersion(&$versions)
205     {
206         $versions[] = array('name' => 'Gravatar',
207                             'version' => STATUSNET_VERSION,
208                             'author' => 'Eric Helgeson',
209                             'homepage' => 'http://status.net/wiki/Plugin:Gravatar',
210                             'rawdescription' =>
211                             // TRANS: Plugin decsription.
212                             _m('The Gravatar plugin allows users to use their <a href="http://www.gravatar.com/">Gravatar</a> with StatusNet.'));
213
214         return true;
215     }
216 }