2010-08-04 22:26:55 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* GNU Social
|
|
|
|
* Copyright (C) 2010, Free Software Foundation, Inc.
|
|
|
|
*
|
|
|
|
* 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 Widget
|
|
|
|
* @package GNU Social
|
|
|
|
* @author Ian Denhardt <ian@zenhack.net>
|
2010-09-05 18:20:27 +01:00
|
|
|
* @author Sean Corbett <sean@gnu.org>
|
2010-12-27 13:42:00 +00:00
|
|
|
* @author Max Shinn <trombonechamp@gmail.com>
|
2010-08-04 22:26:55 +01:00
|
|
|
* @copyright 2010 Free Software Foundation, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!defined('STATUSNET')) {
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
class PhotouploadAction extends Action
|
|
|
|
{
|
|
|
|
var $user = null;
|
|
|
|
|
|
|
|
function prepare($args)
|
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
$this->user = common_current_user();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handle($args)
|
|
|
|
{
|
|
|
|
parent::handle($args);
|
2010-08-05 18:11:34 +01:00
|
|
|
if($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
|
|
$this->handlePost();
|
|
|
|
}
|
2010-08-04 22:26:55 +01:00
|
|
|
$this->showPage();
|
|
|
|
}
|
|
|
|
|
|
|
|
function title()
|
|
|
|
{
|
|
|
|
return _m('Upload Photos');
|
|
|
|
}
|
|
|
|
|
|
|
|
function showContent()
|
|
|
|
{
|
2010-12-28 13:33:06 +00:00
|
|
|
//Upload a photo
|
2010-08-04 22:26:55 +01:00
|
|
|
if(empty($this->user)) {
|
|
|
|
$this->element('p', array(), 'You are not logged in.');
|
|
|
|
} else {
|
2010-12-30 14:02:59 +00:00
|
|
|
//showForm() data
|
|
|
|
if(!empty($this->msg)) {
|
|
|
|
$class = ($this->success) ? 'success' : 'error';
|
|
|
|
$this->element('p', array('class' => $class), $this->msg);
|
|
|
|
}
|
|
|
|
|
2010-12-20 21:52:25 +00:00
|
|
|
$this->elementStart('form', array('enctype' => 'multipart/form-data',
|
2010-08-04 22:26:55 +01:00
|
|
|
'method' => 'post',
|
|
|
|
'action' => common_local_url('photoupload')));
|
2010-12-27 02:51:17 +00:00
|
|
|
$this->elementStart('ul', 'form_data');
|
|
|
|
$this->elementStart('li');
|
2010-08-04 22:26:55 +01:00
|
|
|
$this->element('input', array('name' => 'photofile',
|
|
|
|
'type' => 'file',
|
|
|
|
'id' => 'photofile'));
|
2010-12-27 02:51:17 +00:00
|
|
|
$this->elementEnd('li');
|
|
|
|
//$this->element('br');
|
|
|
|
$this->elementStart('li');
|
2010-12-28 13:33:06 +00:00
|
|
|
$this->input('phototitle', _("Title"), null, _("The title of the photo. (Optional)"));
|
2010-12-27 02:51:17 +00:00
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementStart('li');
|
2010-12-28 13:33:06 +00:00
|
|
|
$this->textarea('photo_description', _("Description"), null, _("A description of the photo. (Optional)"));
|
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementStart('li');
|
|
|
|
$this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
|
2010-12-27 02:51:17 +00:00
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementEnd('ul');
|
2010-08-04 22:26:55 +01:00
|
|
|
$this->submit('upload', _('Upload'));
|
|
|
|
$this->elementEnd('form');
|
2010-12-28 13:33:06 +00:00
|
|
|
$this->element('br');
|
|
|
|
|
|
|
|
//Create a new album
|
|
|
|
$this->element('h3', array(), _("Create a new album"));
|
|
|
|
$this->elementStart('form', array('method' => 'post',
|
|
|
|
'action' =>common_local_url('photoupload')));
|
|
|
|
$this->elementStart('ul', 'form_data');
|
|
|
|
$this->elementStart('li');
|
|
|
|
$this->input('album_name', _("Title"), null, _("The title of the album."));
|
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementStart('li');
|
|
|
|
$this->textarea('album_description', _("Description"), null, _("A description of the album. (Optional)"));
|
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementEnd('ul');
|
|
|
|
$this->submit('create', _('Create'));
|
|
|
|
$this->elementEnd('form');
|
2010-12-30 14:02:59 +00:00
|
|
|
|
|
|
|
//Delete an album
|
|
|
|
$this->element('h3', array(), _("Delete an album"));
|
|
|
|
$this->elementStart('form', array('method' => 'post',
|
|
|
|
'action' =>common_local_url('photoupload')));
|
|
|
|
$this->elementStart('ul', 'form_data');
|
|
|
|
$this->elementStart('li');
|
|
|
|
$this->dropdown('album', _("Album"), $this->albumList(), _("The album in which to place this photo"), false);
|
|
|
|
$this->elementEnd('li');
|
|
|
|
$this->elementEnd('ul');
|
|
|
|
$this->submit('deletealbum', _('Delete'));
|
|
|
|
$this->elementEnd('form');
|
2010-12-28 13:33:06 +00:00
|
|
|
|
2010-08-04 22:26:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handlePost()
|
|
|
|
{
|
|
|
|
|
|
|
|
common_log(LOG_INFO, 'handlPost()!');
|
|
|
|
// Workaround for PHP returning empty $_POST and $_FILES when POST
|
|
|
|
// length > post_max_size in php.ini
|
|
|
|
|
|
|
|
if (empty($_FILES)
|
|
|
|
&& empty($_POST)
|
|
|
|
&& ($_SERVER['CONTENT_LENGTH'] > 0)
|
|
|
|
) {
|
|
|
|
$msg = _('The server was unable to handle that much POST ' .
|
|
|
|
'data (%s bytes) due to its current configuration.');
|
|
|
|
|
|
|
|
$this->showForm(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSRF protection
|
|
|
|
|
2010-08-05 18:11:34 +01:00
|
|
|
/* $token = $this->trimmed('token');
|
2010-08-04 22:26:55 +01:00
|
|
|
if (!$token || $token != common_session_token()) {
|
|
|
|
$this->showForm(_('There was a problem with your session token. '.
|
|
|
|
'Try again, please.'));
|
|
|
|
return;
|
2010-08-05 18:11:34 +01:00
|
|
|
} */
|
2010-08-04 22:26:55 +01:00
|
|
|
|
2010-12-28 13:33:06 +00:00
|
|
|
if ($this->arg('upload')) {
|
2010-08-04 22:26:55 +01:00
|
|
|
$this->uploadPhoto();
|
|
|
|
}
|
2010-12-28 13:33:06 +00:00
|
|
|
if ($this->arg('create')) {
|
|
|
|
$this->createAlbum();
|
|
|
|
}
|
2010-12-30 14:02:59 +00:00
|
|
|
if ($this->arg('deletealbum')) {
|
|
|
|
$this->deleteAlbum();
|
|
|
|
}
|
2010-08-04 22:26:55 +01:00
|
|
|
}
|
|
|
|
|
2010-08-11 15:54:06 +01:00
|
|
|
function showForm($msg, $success=false)
|
2010-12-28 13:33:06 +00:00
|
|
|
{
|
2010-08-11 15:54:06 +01:00
|
|
|
$this->msg = $msg;
|
|
|
|
$this->success = $success;
|
|
|
|
|
|
|
|
// $this->showPage();
|
|
|
|
}
|
|
|
|
|
2010-12-28 13:33:06 +00:00
|
|
|
function albumList()
|
|
|
|
{
|
|
|
|
$cur = common_current_user();
|
|
|
|
$album = new GNUsocialPhotoAlbum();
|
|
|
|
$album->user_id = $cur->id;
|
|
|
|
|
|
|
|
$albumlist = array();
|
|
|
|
if (!$album->find()) {
|
|
|
|
GNUsocialPhotoAlbum::newAlbum($cur->id, 'Default');
|
|
|
|
}
|
|
|
|
while ($album->fetch()) {
|
|
|
|
$albumlist[$album->album_id] = $album->album_name;
|
|
|
|
}
|
|
|
|
return $albumlist;
|
|
|
|
}
|
|
|
|
|
2010-08-04 22:26:55 +01:00
|
|
|
function uploadPhoto()
|
|
|
|
{
|
|
|
|
$cur = common_current_user();
|
|
|
|
if(empty($cur)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$imagefile = ImageFile::fromUpload('photofile');
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->showForm($e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($imagefile === null) {
|
|
|
|
$this->showForm(_('No file uploaded.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-12-27 02:51:17 +00:00
|
|
|
$title = $this->trimmed('phototitle');
|
|
|
|
$photo_description = $this->trimmed('photo_description');
|
2010-12-25 14:02:06 +00:00
|
|
|
|
2010-08-04 22:26:55 +01:00
|
|
|
common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
|
|
|
|
|
2010-08-11 15:54:06 +01:00
|
|
|
$filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . image_type_to_extension($imagefile->type);
|
2010-08-04 22:26:55 +01:00
|
|
|
move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
|
|
|
|
photo_make_thumbnail($filename);
|
2010-08-16 21:21:27 +01:00
|
|
|
$uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
|
|
|
|
$thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
|
2010-08-11 15:54:06 +01:00
|
|
|
$profile_id = $cur->id;
|
2010-09-05 18:19:15 +01:00
|
|
|
|
2010-12-28 13:33:06 +00:00
|
|
|
$album = GNUsocialPhotoAlbum::staticGet('album_id', $this->trimmed('album'));
|
|
|
|
if ($album->profile_id != $profile_id) {
|
|
|
|
$this->showForm(_('Error: This is not your album!'));
|
|
|
|
return;
|
|
|
|
}
|
2010-12-25 14:02:06 +00:00
|
|
|
GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
|
2010-08-04 22:26:55 +01:00
|
|
|
}
|
|
|
|
|
2010-12-28 13:33:06 +00:00
|
|
|
function createAlbum()
|
|
|
|
{
|
|
|
|
$cur = common_current_user();
|
|
|
|
if(empty($cur)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$album_name = $this->trimmed('album_name');
|
|
|
|
$album_description = $this->trimmed('album_description');
|
|
|
|
|
|
|
|
GNUsocialPhotoAlbum::newAlbum($cur->id, $album_name, $album_description);
|
|
|
|
}
|
2010-12-30 14:02:59 +00:00
|
|
|
|
|
|
|
function deleteAlbum()
|
|
|
|
{
|
|
|
|
$cur = common_current_user();
|
|
|
|
if(empty($cur)) return;
|
|
|
|
|
|
|
|
$album_id = $this->trimmed('album');
|
|
|
|
$album = GNUsocialPhotoAlbum::staticGet('album_id', $album_id);
|
|
|
|
if (empty($album)) {
|
|
|
|
$this->showForm(_('This album does not exist or has been deleted.'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Check if the album has any photos in it before deleting
|
|
|
|
$photos = GNUsocialPhoto::staticGet('album_id', $album_id);
|
|
|
|
if(empty($photos)) {
|
|
|
|
$album->delete();
|
|
|
|
$this->showForm(_('Album deleted'), true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$this->showForm(_('Cannot delete album when there are photos in it!'), false);
|
|
|
|
}
|
|
|
|
}
|
2010-08-04 22:26:55 +01:00
|
|
|
}
|