]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatar.php
call validate before saving objects
[quix0rs-gnu-social.git] / actions / avatar.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23
24 class AvatarAction extends SettingsAction {
25
26         function show_form($msg=NULL, $success=false) {
27                 common_show_header(_t('Avatar'));
28                 $this->settings_menu();
29                 $this->message($msg, $success);
30
31                 $user = common_current_user();
32                 $profile = $user->getProfile();
33                 $original = $profile->getOriginal();
34
35                 if ($original) {
36                         common_element('img', array('src' => $original->url,
37                                                                                 'class' => 'avatar original',
38                                                                                 'width' => $original->width,
39                                                                                 'height' => $original->height,
40                                                                                 'alt' => $user->nickname));
41                 }
42
43                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
44
45                 if ($avatar) {
46                         common_element('img', array('src' => $avatar->url,
47                                                                                 'class' => 'avatar profile',
48                                                                                 'width' => AVATAR_PROFILE_SIZE,
49                                                                                 'height' => AVATAR_PROFILE_SIZE,
50                                                                                 'alt' => $user->nickname));
51                 }
52
53                 common_element_start('form', array('enctype' => 'multipart/form-data',
54                                                                                    'method' => 'POST',
55                                                                                    'id' => 'avatar',
56                                                                                    'action' =>
57                                                                                    common_local_url('avatar')));
58                 common_element('input', array('name' => 'MAX_FILE_SIZE',
59                                                                           'type' => 'hidden',
60                                                                           'id' => 'MAX_FILE_SIZE',
61                                                                           'value' => MAX_AVATAR_SIZE));
62                 common_element('input', array('name' => 'avatarfile',
63                                                                           'type' => 'file',
64                                                                           'id' => 'avatarfile'));
65                 common_element('input', array('name' => 'submit',
66                                                                           'type' => 'submit',
67                                                                           'id' => 'submit',
68                                                                           'value' =>_t('Upload')));
69                 common_element_end('form');
70                 common_show_footer();
71         }
72
73         function handle_post() {
74
75                 switch ($_FILES['avatarfile']['error']) {
76                  case UPLOAD_ERR_OK: # success, jump out
77                         break;
78                  case UPLOAD_ERR_INI_SIZE:
79                  case UPLOAD_ERR_FORM_SIZE:
80                         $this->show_form(_t('That file is too big.'));
81                         return;
82                  case UPLOAD_ERR_PARTIAL:
83                         @unlink($_FILES['avatarfile']['tmp_name']);
84                         $this->show_form(_t('Partial upload.'));
85                         return;
86                  default:
87                         $this->show_form(_t('System error uploading file.'));
88                         return;
89                 }
90
91                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
92
93                 if (!$info) {
94                         @unlink($_FILES['avatarfile']['tmp_name']);
95                         $this->show_form(_t('Not an image or corrupt file.'));
96                         return;
97                 }
98
99                 switch ($info[2]) {
100                  case IMAGETYPE_GIF:
101                  case IMAGETYPE_JPEG:
102                  case IMAGETYPE_PNG:
103                         break;
104                  default:
105                         $this->show_form(_t('Unsupported image file format.'));
106                         return;
107                 }
108
109                 $user = common_current_user();
110
111                 $filename = common_avatar_filename($user, image_type_to_extension($info[2]));
112                 $filepath = common_avatar_path($filename);
113
114                 if (!move_uploaded_file($_FILES['avatarfile']['tmp_name'], $filepath)) {
115                         @unlink($_FILES['avatarfile']['tmp_name']);
116                         $this->show_form(_t('System error uploading file.'));
117                         return;
118                 }
119
120                 $avatar = DB_DataObject::factory('avatar');
121
122                 $avatar->profile_id = $user->id;
123                 $avatar->width = $info[0];
124                 $avatar->height = $info[1];
125                 $avatar->mediatype = image_type_to_mime_type($info[2]);
126                 $avatar->filename = $filename;
127                 $avatar->original = true;
128                 $avatar->url = common_avatar_url($filename);
129                 $avatar->created = DB_DataObject_Cast::dateTime(); # current time
130
131                 $val = $avatar->validate();
132                 
133                 if ($val !== TRUE) {
134                         $err = '';
135                         foreach ($val as $k=>$v) {
136                                 $err .= _t('Something wrong with ') . $k;
137                                 $this->show_form($err);
138                                 return;
139                         }
140                 }
141                 
142                 foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
143                         $scaled[] = $this->scale_avatar($user, $avatar, $size);
144                 }
145
146                 # XXX: start a transaction here
147
148                 if (!$this->delete_old_avatars($user)) {
149                         @unlink($filepath);
150                         common_server_error(_t('Error deleting old avatars.'));
151                         return;
152                 }
153                 if (!$avatar->insert()) {
154                         @unlink($filepath);
155                         common_server_error(_t('Error inserting avatar.'));
156                         return;
157                 }
158
159                 foreach ($scaled as $s) {
160                         if (!$s->insert()) {
161                                 common_server_error(_t('Error inserting scaled avatar.'));
162                                 return;
163                         }
164                 }
165
166                 # XXX: end transaction here
167
168                 $this->show_form(_t('Avatar updated.'), true);
169         }
170
171         function scale_avatar($user, $avatar, $size) {
172                 $image_s = imagecreatetruecolor($size, $size);
173                 $image_a = $this->avatar_to_image($avatar);
174
175                 $square = min($avatar->width, $avatar->height);
176
177                 imagecopyresampled($image_s, $image_a, 0, 0, 0, 0,
178                                                    $size, $size, $square, $square);
179
180                 $ext = ($avatar->mediattype == 'image/jpeg') ? ".jpg" : ".png";
181
182                 $filename = common_avatar_filename($user, $ext, $size);
183
184                 if ($avatar->mediatype == 'image/jpeg') {
185                         imagejpeg($image_s, common_avatar_path($filename));
186                 } else {
187                         imagepng($image_s, common_avatar_path($filename));
188                 }
189
190                 $scaled = DB_DataObject::factory('avatar');
191                 $scaled->profile_id = $avatar->profile_id;
192                 $scaled->width = $size;
193                 $scaled->height = $size;
194                 $scaled->original = false;
195                 $scaled->mediatype = ($avatar->mediattype == 'image/jpeg') ? 'image/jpeg' : 'image/png';
196                 $scaled->filename = $filename;
197                 $scaled->url = common_avatar_url($filename);
198                 $scaled->created = DB_DataObject_Cast::dateTime(); # current time
199
200                 return $scaled;
201         }
202
203         function avatar_to_image($avatar) {
204                 $filepath = common_avatar_path($avatar->filename);
205                 if ($avatar->mediatype == 'image/gif') {
206                         return imagecreatefromgif($filepath);
207                 } else if ($avatar->mediatype == 'image/jpeg') {
208                         return imagecreatefromjpeg($filepath);
209                 } else if ($avatar->mediatype == 'image/png') {
210                         return imagecreatefrompng($filepath);
211                 } else {
212                         common_server_error(_t('Unsupported image type:') . $avatar->mediatype);
213                         return NULL;
214                 }
215         }
216
217         function delete_old_avatars($user) {
218                 $avatar = DB_DataObject::factory('avatar');
219                 $avatar->profile_id = $user->id;
220                 $avatar->find();
221                 while ($avatar->fetch()) {
222                         unlink(common_avatar_path($avatar->filename));
223                         $avatar->delete();
224                 }
225                 return true;
226         }
227 }
228