]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/avatar.php
debug logging in __process
[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 get_instructions() {
27                 return _('Upload a new "avatar" (user image) here. You can\'t edit the picture after you upload it, so make sure it\'s more or less square. It must be under the site license, also. Use a picture that belongs to you and that you want to share.');
28         }
29
30         function show_form($msg=NULL, $success=false) {
31
32                 $this->form_header(_('Avatar'), $msg, $success);
33
34                 $user = common_current_user();
35                 $profile = $user->getProfile();
36                 $original = $profile->getOriginalAvatar();
37
38                 if ($original) {
39                         common_element('img', array('src' => $original->url,
40                                                                                 'class' => 'avatar original',
41                                                                                 'width' => $original->width,
42                                                                                 'height' => $original->height,
43                                                                                 'alt' => $user->nickname));
44                 }
45
46                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
47
48                 if ($avatar) {
49                         common_element('img', array('src' => $avatar->url,
50                                                                                 'class' => 'avatar profile',
51                                                                                 'width' => AVATAR_PROFILE_SIZE,
52                                                                                 'height' => AVATAR_PROFILE_SIZE,
53                                                                                 'alt' => $user->nickname));
54                 }
55
56                 common_element_start('form', array('enctype' => 'multipart/form-data',
57                                                                                    'method' => 'POST',
58                                                                                    'id' => 'avatar',
59                                                                                    'action' =>
60                                                                                    common_local_url('avatar')));
61                 common_hidden('token', common_session_token());
62                 common_element('input', array('name' => 'MAX_FILE_SIZE',
63                                                                           'type' => 'hidden',
64                                                                           'id' => 'MAX_FILE_SIZE',
65                                                                           'value' => MAX_AVATAR_SIZE));
66                 common_element('input', array('name' => 'avatarfile',
67                                                                           'type' => 'file',
68                                                                           'id' => 'avatarfile'));
69                 common_submit('submit', _('Upload'));
70                 common_element_end('form');
71                 common_show_footer();
72         }
73
74         function handle_post() {
75
76                 # CSRF protection
77
78                 $token = $this->trimmed('token');
79                 if (!$token || $token != common_session_token()) {
80                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
81                         return;
82                 }
83
84                 switch ($_FILES['avatarfile']['error']) {
85                  case UPLOAD_ERR_OK: # success, jump out
86                         break;
87                  case UPLOAD_ERR_INI_SIZE:
88                  case UPLOAD_ERR_FORM_SIZE:
89                         $this->show_form(_('That file is too big.'));
90                         return;
91                  case UPLOAD_ERR_PARTIAL:
92                         @unlink($_FILES['avatarfile']['tmp_name']);
93                         $this->show_form(_('Partial upload.'));
94                         return;
95                  default:
96                         $this->show_form(_('System error uploading file.'));
97                         return;
98                 }
99
100                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
101
102                 if (!$info) {
103                         @unlink($_FILES['avatarfile']['tmp_name']);
104                         $this->show_form(_('Not an image or corrupt file.'));
105                         return;
106                 }
107
108                 switch ($info[2]) {
109                  case IMAGETYPE_GIF:
110                  case IMAGETYPE_JPEG:
111                  case IMAGETYPE_PNG:
112                         break;
113                  default:
114                         $this->show_form(_('Unsupported image file format.'));
115                         return;
116                 }
117
118                 $user = common_current_user();
119                 $profile = $user->getProfile();
120
121                 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
122                         $this->show_form(_('Avatar updated.'), true);
123                 } else {
124                         $this->show_form(_('Failed updating avatar.'));
125                 }
126
127                 @unlink($_FILES['avatarfile']['tmp_name']);
128         }
129 }
130