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