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