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