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 * @author Max Shinn <trombonechamp@gmail.com>
27 * @copyright 2010 Free Software Foundation, Inc.
28 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
31 if (!defined('STATUSNET')) {
35 class PhotouploadAction extends Action
39 function prepare($args)
41 parent::prepare($args);
42 $this->user = common_current_user();
46 function handle($args)
48 parent::handle($args);
49 if($_SERVER['REQUEST_METHOD'] == 'POST') {
57 return _m('Upload Photos');
60 function showContent()
62 if(empty($this->user)) {
63 $this->element('p', array(), 'You are not logged in.');
65 $this->elementStart('form', array('enctype' => 'multipart/form-data',
67 'action' => common_local_url('photoupload')));
68 $this->elementStart('ul', 'form_data');
69 $this->elementStart('li');
70 $this->element('input', array('name' => 'photofile',
72 'id' => 'photofile'));
73 $this->elementEnd('li');
74 //$this->element('br');
75 $this->elementStart('li');
76 $this->input('phototitle', "Title", $this->trimmed('phototitle'), "The title of the photo. (Optional)");
77 $this->elementEnd('li');
78 $this->elementStart('li');
79 $this->textarea('photo_description', "Description", $this->trimmed('photo_description'), "A description of the photo. (Optional)");
80 $this->elementEnd('li');
81 $this->elementEnd('ul');
82 $this->submit('upload', _('Upload'));
83 $this->elementEnd('form');
90 common_log(LOG_INFO, 'handlPost()!');
91 // Workaround for PHP returning empty $_POST and $_FILES when POST
92 // length > post_max_size in php.ini
96 && ($_SERVER['CONTENT_LENGTH'] > 0)
98 $msg = _('The server was unable to handle that much POST ' .
99 'data (%s bytes) due to its current configuration.');
101 $this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
107 /* $token = $this->trimmed('token');
108 if (!$token || $token != common_session_token()) {
109 $this->showForm(_('There was a problem with your session token. '.
110 'Try again, please.'));
114 if($this->arg('upload')) {
115 $this->uploadPhoto();
119 function showForm($msg, $success=false)
122 $this->success = $success;
124 // $this->showPage();
127 function uploadPhoto()
129 $cur = common_current_user();
134 $imagefile = ImageFile::fromUpload('photofile');
135 } catch (Exception $e) {
136 $this->showForm($e->getMessage());
139 if ($imagefile === null) {
140 $this->showForm(_('No file uploaded.'));
144 $title = $this->trimmed('phototitle');
145 $photo_description = $this->trimmed('photo_description');
147 common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
149 $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . image_type_to_extension($imagefile->type);
150 move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
151 photo_make_thumbnail($filename);
152 $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
153 $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
154 $profile_id = $cur->id;
156 // TODO: proper multiple album support
157 $album = GNUsocialPhotoAlbum::staticGet('profile_id', $profile_id);
159 $album = GNUsocialPhotoAlbum::newAlbum($profile_id, 'Default');
160 GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);