]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Gravatar/GravatarPlugin.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline
[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         return true;
35     }
36     
37     function onStartAvatarFormData($action) {
38         $user = common_current_user();
39         $hasGravatar = $this->hasGravatar($user->id);
40         
41         if($hasGravatar) {
42             return false;
43         }
44     }
45     
46     function onEndAvatarFormData(&$action) {
47         $user = common_current_user();
48         $hasGravatar = $this->hasGravatar($user->id);
49
50         if(!empty($user->email) && !$hasGravatar) { //and not gravatar already set
51             $action->elementStart('form', array('method' => 'post',
52                                                 'id' => 'form_settings_gravatar_add',
53                                                 'class' => 'form_settings',
54                                                 'action' =>
55                                                 common_local_url('avatarsettings')));
56             $action->elementStart('fieldset', array('id' => 'settings_gravatar_add'));
57             $action->element('legend', null, _m('Set Gravatar'));
58             $action->hidden('token', common_session_token());
59             $action->element('p', 'form_guide',
60                              _m('If you want to use your Gravatar image, click "Add".'));
61             $action->element('input', array('type' => 'submit',
62                                             'id' => 'settings_gravatar_add_action-submit',
63                                             'name' => 'add',
64                                             'class' => 'submit',
65                                             'value' => _m('Add')));
66             $action->elementEnd('fieldset');
67             $action->elementEnd('form');
68         } elseif($hasGravatar) {
69             $action->elementStart('form', array('method' => 'post',
70                                                 'id' => 'form_settings_gravatar_remove',
71                                                 'class' => 'form_settings',
72                                                 'action' =>
73                                                 common_local_url('avatarsettings')));
74             $action->elementStart('fieldset', array('id' => 'settings_gravatar_remove'));
75             $action->element('legend', null, _m('Remove Gravatar'));
76             $action->hidden('token', common_session_token());
77             $action->element('p', 'form_guide',
78                              _m('If you want to remove your Gravatar image, click "Remove".'));
79             $action->element('input', array('type' => 'submit',
80                                             'id' => 'settings_gravatar_remove_action-submit',
81                                             'name' => 'remove',
82                                             'class' => 'submit',
83                                             'value' => _m('Remove')));
84             $action->elementEnd('fieldset');
85             $action->elementEnd('form');
86         } else {
87             $action->element('p', 'form_guide',
88                              _m('To use a Gravatar first enter in an email address.'));
89         }
90     }
91     
92     function onStartAvatarSaveForm($action) {
93         if ($action->arg('add')) {
94             $result = $this->gravatar_save();
95
96             if($result['success']===true) {
97                 common_broadcast_profile(common_current_user()->getProfile());
98             }
99
100             $action->showForm($result['message'], $result['success']);
101
102             return false;
103         } else if ($action->arg('remove')) {
104             $result = $this->gravatar_remove();
105
106             if($result['success']===true) {
107                 common_broadcast_profile(common_current_user()->getProfile());
108             }
109
110             $action->showForm($result['message'], $result['success']);
111
112             return false;
113         } else {
114             return true;
115         }
116     }
117
118     function hasGravatar($id) {
119         $avatar = new Avatar();
120         $avatar->profile_id = $id;
121         if ($avatar->find()) {
122             while ($avatar->fetch()) {
123                 if($avatar->filename == null) {
124                     return true;
125                 }
126             }
127         }
128         return false;
129      }
130  
131
132     function gravatar_save()
133     {
134         $cur = common_current_user();
135         
136         if(empty($cur->email)) {
137             return array('message' => _m('You do not have a email set in your profile.'),
138                          'success' => false);
139         }
140         //Get rid of previous Avatar
141         $this->gravatar_remove();
142         
143         foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
144             $gravatar = new Avatar();
145             $gravatar->profile_id = $cur->id;
146             $gravatar->width = $size;
147             $gravatar->height = $size;
148             $gravatar->original = false; //No file, so no original
149             $gravatar->mediatype = 'img';//XXX: Unsure what to put here
150             //$gravatar->filename = null;//No filename. Remote
151             $gravatar->url = $this->gravatar_url($cur->email, $size);
152             $gravatar->created = DB_DataObject_Cast::dateTime(); # current time
153
154             if (!$gravatar->insert()) {
155                 return array('message' => _m('Failed to save Gravatar to the DB.'),
156                              'success' => false);
157             }
158         }
159         return array('message' => _m('Gravatar added.'),
160                      'success' => true);
161      }
162
163     function gravatar_remove()
164     {
165         $user = common_current_user();
166         $profile = $user->getProfile();
167
168         $avatar = $profile->getOriginalAvatar();
169         if($avatar) $avatar->delete();
170         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
171         if($avatar) $avatar->delete();
172         $avatar = $profile->getAvatar(AVATAR_STREAM_SIZE);
173         if($avatar) $avatar->delete();
174         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
175         if($avatar) $avatar->delete();
176
177         return array('message' => _m('Gravatar removed.'),
178                      'success' => true);
179     }
180  
181     function gravatar_url($email, $size) {
182         $url = "http://www.gravatar.com/avatar.php?gravatar_id=".
183                 md5(strtolower($email)).
184                 "&default=".urlencode(Avatar::defaultImage($size)).
185                 "&size=".$size;
186             return $url;
187     }
188 }