We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

avatarsettings.php 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?php
/**
 * Laconica, the distributed open-source microblogging tool
 *
 * Upload an avatar
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Settings
 * @package   Laconica
 * @author    Evan Prodromou <evan@controlyourself.ca>
 * @author    Zach Copley <zach@controlyourself.ca>
 * @copyright 2008-2009 Control Yourself, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://laconi.ca/
 */

if (!defined('LACONICA')) {
    exit(1);
}

35
require_once INSTALLDIR.'/lib/accountsettingsaction.php';
36

37 38
define('MAX_ORIGINAL', 480);

39 40 41
/**
 * Upload an avatar
 *
sarven's avatar
sarven committed
42
 * We use jCrop plugin for jQuery to crop the image after upload.
43 44 45 46 47
 *
 * @category Settings
 * @package  Laconica
 * @author   Evan Prodromou <evan@controlyourself.ca>
 * @author   Zach Copley <zach@controlyourself.ca>
sarven's avatar
sarven committed
48
 * @author   Sarven Capadisli <csarven@controlyourself.ca>
49 50 51 52
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://laconi.ca/
 */

53
class AvatarsettingsAction extends AccountSettingsAction
54
{
55 56 57 58
    var $mode = null;
    var $imagefile = null;
    var $filename = null;

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    /**
     * Title of the page
     *
     * @return string Title of the page
     */

    function title()
    {
        return _('Avatar');
    }

    /**
     * Instructions for use
     *
     * @return instructions for use
     */

    function getInstructions()
    {
78
        return _('You can upload your personal avatar. The maximum file size is '.ImageFile::maxFileSize().'.');
79 80 81 82 83 84 85 86 87 88 89
    }

    /**
     * Content area of the page
     *
     * Shows a form for uploading an avatar.
     *
     * @return void
     */

    function showContent()
90 91 92 93 94 95 96 97 98
    {
        if ($this->mode == 'crop') {
            $this->showCropForm();
        } else {
            $this->showUploadForm();
        }
    }

    function showUploadForm()
99 100 101 102 103 104 105 106 107 108 109 110 111 112
    {
        $user = common_current_user();

        $profile = $user->getProfile();

        if (!$profile) {
            common_log_db_error($user, 'SELECT', __FILE__);
            $this->serverError(_('User without matching profile'));
            return;
        }

        $original = $profile->getOriginalAvatar();

        $this->elementStart('form', array('enctype' => 'multipart/form-data',
sarven's avatar
sarven committed
113 114 115
                                          'method' => 'post',
                                          'id' => 'form_settings_avatar',
                                          'class' => 'form_settings',
116 117
                                          'action' =>
                                          common_local_url('avatarsettings')));
sarven's avatar
sarven committed
118 119
        $this->elementStart('fieldset');
        $this->element('legend', null, _('Avatar settings'));
120 121
        $this->hidden('token', common_session_token());

sarven's avatar
sarven committed
122
        $this->elementStart('ul', 'form_data');
123
        if ($original) {
sarven's avatar
sarven committed
124 125
            $this->elementStart('li', array('id' => 'avatar_original',
                                            'class' => 'avatar_view'));
sarven's avatar
sarven committed
126
            $this->element('h2', null, _("Original"));
127 128 129 130 131 132
            $this->elementStart('div', array('id'=>'avatar_original_view'));
            $this->element('img', array('src' => $original->url,
                                        'width' => $original->width,
                                        'height' => $original->height,
                                        'alt' => $user->nickname));
            $this->elementEnd('div');
sarven's avatar
sarven committed
133
            $this->elementEnd('li');
134 135 136 137 138
        }

        $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);

        if ($avatar) {
sarven's avatar
sarven committed
139 140
            $this->elementStart('li', array('id' => 'avatar_preview',
                                            'class' => 'avatar_view'));
sarven's avatar
sarven committed
141
            $this->element('h2', null, _("Preview"));
142
            $this->elementStart('div', array('id'=>'avatar_preview_view'));
sarven's avatar
sarven committed
143
            $this->element('img', array('src' => $original->url,
144 145 146 147
                                        'width' => AVATAR_PROFILE_SIZE,
                                        'height' => AVATAR_PROFILE_SIZE,
                                        'alt' => $user->nickname));
            $this->elementEnd('div');
sarven's avatar
sarven committed
148
            $this->elementEnd('li');
149 150
        }

sarven's avatar
sarven committed
151 152 153 154
        $this->elementStart('li', array ('id' => 'settings_attach'));
        $this->element('input', array('name' => 'avatarfile',
                                      'type' => 'file',
                                      'id' => 'avatarfile'));
155 156 157
        $this->element('input', array('name' => 'MAX_FILE_SIZE',
                                      'type' => 'hidden',
                                      'id' => 'MAX_FILE_SIZE',
158
                                      'value' => ImageFile::maxFileSize(true)));
sarven's avatar
sarven committed
159 160
        $this->elementEnd('li');
        $this->elementEnd('ul');
161

sarven's avatar
sarven committed
162 163
        $this->elementStart('ul', 'form_actions');
        $this->elementStart('li');
164
        $this->submit('upload', _('Upload'));
sarven's avatar
sarven committed
165 166 167 168
        $this->elementEnd('li');
        $this->elementEnd('ul');

        $this->elementEnd('fieldset');
169 170 171 172
        $this->elementEnd('form');

    }

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    function showCropForm()
    {
        $user = common_current_user();

        $profile = $user->getProfile();

        if (!$profile) {
            common_log_db_error($user, 'SELECT', __FILE__);
            $this->serverError(_('User without matching profile'));
            return;
        }

        $original = $profile->getOriginalAvatar();

        $this->elementStart('form', array('method' => 'post',
                                          'id' => 'form_settings_avatar',
                                          'class' => 'form_settings',
                                          'action' =>
                                          common_local_url('avatarsettings')));
        $this->elementStart('fieldset');
        $this->element('legend', null, _('Avatar settings'));
        $this->hidden('token', common_session_token());

        $this->elementStart('ul', 'form_data');

        $this->elementStart('li',
                            array('id' => 'avatar_original',
                                  'class' => 'avatar_view'));
        $this->element('h2', null, _("Original"));
        $this->elementStart('div', array('id'=>'avatar_original_view'));
        $this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
                                    'width' => $this->filedata['width'],
                                    'height' => $this->filedata['height'],
                                    'alt' => $user->nickname));
        $this->elementEnd('div');
        $this->elementEnd('li');

        $this->elementStart('li',
                            array('id' => 'avatar_preview',
                                  'class' => 'avatar_view'));
        $this->element('h2', null, _("Preview"));
        $this->elementStart('div', array('id'=>'avatar_preview_view'));
        $this->element('img', array('src' => common_avatar_url($this->filedata['filename']),
                                    'width' => AVATAR_PROFILE_SIZE,
                                    'height' => AVATAR_PROFILE_SIZE,
                                    'alt' => $user->nickname));
        $this->elementEnd('div');

        foreach (array('avatar_crop_x', 'avatar_crop_y',
                       'avatar_crop_w', 'avatar_crop_h') as $crop_info) {
            $this->element('input', array('name' => $crop_info,
                                          'type' => 'hidden',
                                          'id' => $crop_info));
        }
        $this->submit('crop', _('Crop'));

        $this->elementEnd('li');
        $this->elementEnd('ul');
        $this->elementEnd('fieldset');
        $this->elementEnd('form');

    }

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    /**
     * Handle a post
     *
     * We mux on the button name to figure out what the user actually wanted.
     *
     * @return void
     */

    function handlePost()
    {
        // CSRF protection

        $token = $this->trimmed('token');
        if (!$token || $token != common_session_token()) {
            $this->show_form(_('There was a problem with your session token. '.
                               'Try again, please.'));
            return;
        }

        if ($this->arg('upload')) {
            $this->uploadAvatar();
        } else if ($this->arg('crop')) {
            $this->cropAvatar();
        } else {
            $this->showForm(_('Unexpected form submission.'));
        }
    }

    /**
     * Handle an image upload
     *
     * Does all the magic for handling an image upload, and crops the
     * image by default.
     *
     * @return void
     */

    function uploadAvatar()
    {
275 276 277 278
        try {
            $imagefile = ImageFile::fromUpload('avatarfile');
        } catch (Exception $e) {
            $this->showForm($e->getMessage());
279 280 281
            return;
        }

282
        $cur = common_current_user();
283

284 285 286 287
        $filename = common_avatar_filename($cur->id,
                                           image_type_to_extension($imagefile->type),
                                           null,
                                           'tmp'.common_timestamp());
288

289
        $filepath = common_avatar_path($filename);
290

291
        move_uploaded_file($imagefile->filepath, $filepath);
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

        $filedata = array('filename' => $filename,
                          'filepath' => $filepath,
                          'width' => $imagefile->width,
                          'height' => $imagefile->height,
                          'type' => $imagefile->type);

        $_SESSION['FILEDATA'] = $filedata;

        $this->filedata = $filedata;

        $this->mode = 'crop';

        $this->showForm(_('Pick a square area of the image to be your avatar'),
                        true);
307 308 309 310 311 312 313 314 315 316
    }

    /**
     * Handle the results of jcrop.
     *
     * @return void
     */

    function cropAvatar()
    {
317 318 319 320 321 322
        $filedata = $_SESSION['FILEDATA'];

        if (!$filedata) {
            $this->serverError(_('Lost our file data.'));
            return;
        }
323 324 325 326 327 328 329 330 331
        
        // If image is not being cropped assume pos & dimentions of original
        $dest_x = $this->arg('avatar_crop_x') ? $this->arg('avatar_crop_x'):0;
        $dest_y = $this->arg('avatar_crop_y') ? $this->arg('avatar_crop_y'):0;
        $dest_w = $this->arg('avatar_crop_w') ? $this->arg('avatar_crop_w'):$filedata['width'];
        $dest_h = $this->arg('avatar_crop_h') ? $this->arg('avatar_crop_h'):$filedata['height'];
        $size = min($dest_w, $dest_h);
        $size = ($size > MAX_ORIGINAL) ? MAX_ORIGINAL:$size;
        
332
        $user = common_current_user();
333 334 335 336
        $profile = $user->getProfile();
        
        $imagefile = new ImageFile($user->id, $filedata['filepath']);
        $filename = $imagefile->resize($size, $dest_x, $dest_y, $dest_w, $dest_h);
337

338 339
        if ($profile->setOriginal($filename)) {
            @unlink($filedata['filepath']);
340 341
            unset($_SESSION['FILEDATA']);
            $this->mode = 'upload';
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
            $this->showForm(_('Avatar updated.'), true);
        } else {
            $this->showForm(_('Failed updating avatar.'));
        }
    }

    /**
     * Add the jCrop stylesheet
     *
     * @return void
     */

    function showStylesheets()
    {
        parent::showStylesheets();
        $jcropStyle =
sarven's avatar
sarven committed
358
          common_path('theme/base/css/jquery.Jcrop.css?version='.LACONICA_VERSION);
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

        $this->element('link', array('rel' => 'stylesheet',
                                     'type' => 'text/css',
                                     'href' => $jcropStyle,
                                     'media' => 'screen, projection, tv'));
    }

    /**
     * Add the jCrop scripts
     *
     * @return void
     */

    function showScripts()
    {
        parent::showScripts();

        $jcropPack = common_path('js/jcrop/jquery.Jcrop.pack.js');
        $jcropGo   = common_path('js/jcrop/jquery.Jcrop.go.js');

        $this->element('script', array('type' => 'text/javascript',
                                       'src' => $jcropPack));
        $this->element('script', array('type' => 'text/javascript',
                                       'src' => $jcropGo));
    }
}