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