123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class PhotouploadAction extends Action
- {
- var $user = null;
- function prepare(array $args = array())
- {
- parent::prepare($args);
- $this->user = common_current_user();
- return true;
- }
- function handle()
- {
- parent::handle();
- if($_SERVER['REQUEST_METHOD'] == 'POST') {
- $this->handlePost();
- }
- $this->showPage();
- }
- function title()
- {
- return _m('Upload Photos');
- }
- function showContent()
- {
-
- if(empty($this->user)) {
- $this->element('p', array(), 'You are not logged in.');
- } else {
-
- if(!empty($this->msg)) {
- $class = ($this->success) ? 'success' : 'error';
- $this->element('p', array('class' => $class), $this->msg);
- }
- $this->elementStart('form', array('enctype' => 'multipart/form-data',
- 'method' => 'post',
- 'action' => common_local_url('photoupload')));
- $this->elementStart('ul', 'form_data');
- $this->elementStart('li');
- $this->element('input', array('name' => 'photofile',
- 'type' => 'file',
- 'id' => 'photofile'));
- $this->elementEnd('li');
-
- $this->elementStart('li');
- $this->input('phototitle', _("Title"), null, _("The title of the photo. (Optional)"));
- $this->elementEnd('li');
- $this->elementStart('li');
- $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);
- $this->elementEnd('li');
- $this->elementEnd('ul');
- $this->submit('upload', _('Upload'));
- $this->elementEnd('form');
- $this->element('br');
-
- $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');
-
- $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');
-
- }
- }
- function handlePost()
- {
- common_log(LOG_INFO, 'handlPost()!');
-
-
- 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;
- }
-
- if ($this->arg('upload')) {
- $this->uploadPhoto();
- }
- if ($this->arg('create')) {
- $this->createAlbum();
- }
- if ($this->arg('deletealbum')) {
- $this->deleteAlbum();
- }
- }
- function showForm($msg, $success=false)
- {
- $this->msg = $msg;
- $this->success = $success;
- }
- 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;
- }
- 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;
- }
- $title = $this->trimmed('phototitle');
- $photo_description = $this->trimmed('photo_description');
- common_log(LOG_INFO, 'upload path : ' . $imagefile->filepath);
- $filename = $cur->nickname . '-' . common_timestamp() . sha1_file($imagefile->filepath) . image_type_to_extension($imagefile->type);
- move_uploaded_file($imagefile->filepath, INSTALLDIR . '/file/' . $filename);
- photo_make_thumbnail($filename);
- $uri = 'http://' . common_config('site', 'server') . '/file/' . $filename;
- $thumb_uri = 'http://' . common_config('site', 'server') . '/file/thumb.' . $filename;
- $profile_id = $cur->id;
-
- $album = GNUsocialPhotoAlbum::getKV('album_id', $this->trimmed('album'));
- if ($album->profile_id != $profile_id) {
- $this->showForm(_('Error: This is not your album!'));
- return;
- }
- GNUsocialPhoto::saveNew($profile_id, $album->album_id, $thumb_uri, $uri, 'web', false, $title, $photo_description);
- }
- 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);
- }
- function deleteAlbum()
- {
- $cur = common_current_user();
- if(empty($cur)) return;
-
- $album_id = $this->trimmed('album');
- $album = GNUsocialPhotoAlbum::getKV('album_id', $album_id);
- if (empty($album)) {
- $this->showForm(_('This album does not exist or has been deleted.'));
- return;
- }
-
- $photos = GNUsocialPhoto::getKV('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);
- }
- }
- }
|