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