4 * Copyright (C) 2010, Free Software Foundation, Inc.
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Ian Denhardt <ian@zenhack.net>
25 * @author Sean Corbett <sean@gnu.org>
26 * @copyright 2010 Free Software Foundation, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
30 if (!defined('STATUSNET')) {
34 class PhotouploadAction extends Action
38 function prepare($args)
40 parent::prepare($args);
41 $this->user = common_current_user();
45 function handle($args)
47 parent::handle($args);
48 if($_SERVER['REQUEST_METHOD'] == 'POST') {
56 return _m('Upload Photos');
59 function showContent()
61 if(empty($this->user)) {
62 $this->element('p', array(), 'You are not logged in.');
64 $this->elementStart('form', array('enctype' => 'mutlipart/form-data',
66 'action' => common_local_url('photoupload')));
67 $this->element('input', array('name' => 'photofile',
69 'id' => 'photofile'));
70 $this->submit('upload', _('Upload'));
71 $this->elementEnd('form');
78 common_log(LOG_INFO, 'handlPost()!');
79 // Workaround for PHP returning empty $_POST and $_FILES when POST
80 // length > post_max_size in php.ini
84 && ($_SERVER['CONTENT_LENGTH'] > 0)
86 $msg = _('The server was unable to handle that much POST ' .
87 'data (%s bytes) due to its current configuration.');
89 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
95 /* $token = $this->trimmed('token');
96 if (!$token || $token != common_session_token()) {
97 $this->showForm(_('There was a problem with your session token. '.
98 'Try again, please.'));
102 if($this->arg('upload')) {
103 $this->uploadPhoto();
107 function showForm($msg, $success=false)
110 $this->success = $success;
112 // $this->showPage();
115 function uploadPhoto()
117 $cur = common_current_user();
122 $imagefile = ImageFile::fromUpload('photofile');
123 } catch (Exception $e) {
124 $this->showForm($e->getMessage());
127 if ($imagefile === null) {
128 $this->showForm(_('No file uploaded.'));
132 common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
134 $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . image_type_to_extension($imagefile->type);
135 move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
136 photo_make_thumbnail($filename);
137 $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
138 $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
139 $profile_id = $cur->id;
141 // TODO: proper multiple album support
142 $album = GNUsocialPhotoAlbum::staticGet('profile_id', $profile_id);
144 $album = GNUsocialPhotoAlbum::newAlbum($profile_id, 'Default');
145 GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web');
147 GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web');