From 16ad70e16f1c4a97e8c5fd4566e87760f67578ec Mon Sep 17 00:00:00 2001 From: Ian Denhardt Date: Wed, 4 Aug 2010 17:26:55 -0400 Subject: [PATCH] Started working on proper photo upload. not fully functional yet. --- .../GNUsocialPhotos/GNUsocialPhotosPlugin.php | 14 ++ .../GNUsocialPhotos/actions/photoupload.php | 132 ++++++++++++++++++ .../classes/gnusocialphoto.php | 57 ++++++++ 3 files changed, 203 insertions(+) create mode 100644 plugins/GNUsocialPhotos/actions/photoupload.php create mode 100644 plugins/GNUsocialPhotos/classes/gnusocialphoto.php diff --git a/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php b/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php index 91861818a1..d93bb8cca8 100644 --- a/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php +++ b/plugins/GNUsocialPhotos/GNUsocialPhotosPlugin.php @@ -44,6 +44,9 @@ class GNUsocialPhotosPlugin extends Plugin case 'PhotosAction': include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; return false; + case 'PhotouploadAction': + include_once $dir . '/actions/' . strtolower(mb_substr($cls, 0, -6)) . '.php'; + return false; default: return true; } @@ -51,9 +54,20 @@ class GNUsocialPhotosPlugin extends Plugin return true; } + function onCheckSchema() + { + $schema = Schema::get(); + $schema->ensureTable('GNUsocialPhotos', + array(new ColumnDef('object_id', 'integer', null, false, 'PRI', true, null, null, true), + new ColumnDef('path', 'varchar(150)', null, false), + new ColumnDef('thumb_path', 'varchar(156)', null, false), // 156 = 150 + strlen('thumb.') + new ColumnDef('owner_id', 'int(11)', null, false))); + } + function onRouterInitialized($m) { $m->connect(':nickname/photos', array('action' => 'photos')); + $m->connect('main/uploadphoto', array('action' => 'photoupload')); common_log(LOG_INFO, "init'd!"); return true; } diff --git a/plugins/GNUsocialPhotos/actions/photoupload.php b/plugins/GNUsocialPhotos/actions/photoupload.php new file mode 100644 index 0000000000..286dd4386d --- /dev/null +++ b/plugins/GNUsocialPhotos/actions/photoupload.php @@ -0,0 +1,132 @@ +. + * + * @category Widget + * @package GNU Social + * @author Ian Denhardt + * @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); + $this->showPage(); + } + + function title() + { + return _m('Upload Photos'); + } + + function showContent() + { + if(empty($this->user)) { + $this->element('p', array(), 'You are not logged in.'); + } else { + $this->elementStart('form', array('enctype' => 'mutlipart/form-data', + 'method' => 'post', + 'action' => common_local_url('photoupload'))); + $this->element('input', array('name' => 'photofile', + 'type' => 'file', + 'id' => 'photofile')); + $this->submit('upload', _('Upload')); + $this->elementEnd('form'); + } + } + + 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 + + $token = $this->trimmed('token'); + if (!$token || $token != common_session_token()) { + $this->showForm(_('There was a problem with your session token. '. + 'Try again, please.')); + return; + } + + if($this->arg('upload')) { + $this->uploadPhoto(); + } + } + + function uploadPhoto() + { + common_log(LOG_INFO, 'Is this function even getting called?'); + $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; + } + + 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); + $photo = new GNUsocialPhoto(); + $photo->path = '/file/' . $filename; + $photo->thumb_path = '/file/thumb.' . $filename; + $photo->insert(); + } + +} diff --git a/plugins/GNUsocialPhotos/classes/gnusocialphoto.php b/plugins/GNUsocialPhotos/classes/gnusocialphoto.php new file mode 100644 index 0000000000..ecb90616e1 --- /dev/null +++ b/plugins/GNUsocialPhotos/classes/gnusocialphoto.php @@ -0,0 +1,57 @@ +. + * + * @category Widget + * @package GNU Social + * @author Ian Denhardt + * @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); +} + +require_once INSTALLDIR . '/classes/Memcached_DataObject.php'; + +class GNUsocialPhoto extends Memcahced_DataObject +{ + public $object_id; // integer + public $path; // varchar(150) + public $thumb_path; // varchar(156) + public $owner_id; // int(11) (user who posted the photo) + + function staticGet($k,$v=NULL) + { + return Memcached_DataObject::staticGet('GNUsocialPhoto',$k,$v); + } + + function delete() + { + if(!unlink(INSTALLDIR . $this->thumb_path)) { + return false; + } + if(!unlink(INSTALLDIR . $this->path)) { + return false; + } + return parent::delete(); + } +}