Skip to content

Commit

Permalink
CsnFileManager created
Browse files Browse the repository at this point in the history
  • Loading branch information
wingman007 committed Sep 11, 2013
1 parent d7bdc66 commit 6061a7e
Show file tree
Hide file tree
Showing 14 changed files with 428 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
'AuthDoctrine', // if you want to enable AuthDoctrine disable the module Auth. They register the same services
'CsnNavigation', // this module simply adds ACL to navigation
'CsnCms',
'CsnFileManager',
),
'module_listener_options' => array(
'config_glob_paths' => array(
Expand Down
3 changes: 3 additions & 0 deletions config/autoload/acl.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
'AuthDoctrine\Controller\Admin' => array(
'all' => 'admin',
),
'CsnFileManager\Controller\Index' => array(
'all' => 'member',
),
// for CMS articles
'Public Resource' => array(
'view' => 'guest',
Expand Down
1 change: 1 addition & 0 deletions data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uploads
22 changes: 22 additions & 0 deletions module/CsnFileManager/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace CsnFileManager;

class Module
{
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}

public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
64 changes: 64 additions & 0 deletions module/CsnFileManager/config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace CsnFileManager;

return array(
'controllers' => array(
'invokables' => array(
'CsnFileManager\Controller\Index' => 'CsnFileManager\Controller\IndexController',
),
),
'router' => array(
'routes' => array(
'csn-file-manager' => array(
'type' => 'Literal',
'options' => array(
'route' => '/csn-file-manager',
'defaults' => array(
'__NAMESPACE__' => 'CsnFileManager\Controller',
'controller' => 'Index',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'default' => array(
'type' => 'Segment',
'options' => array(
'route' => '/[:controller[/:action[/:id]]]', // there is no constraints for id!
'constraints' => array(
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
),
),
),
),
),
),
),
'view_manager' => array(
'template_path_stack' => array(
'csn-cms' => __DIR__ . '/../view'
),

'display_exceptions' => true,
),
'doctrine' => array(
'driver' => array(
__NAMESPACE__ . '_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array(
__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity',
),
),
'orm_default' => array(
'drivers' => array(
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver',
)
)
)
),
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

namespace CsnFileManager\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use CsnFileManager\Form\UploadForm;

class IndexController extends AbstractActionController
{
protected $_dir = null;

public function init()
{
$config = $this->getServiceLocator()->get('Config');
$fileManagerDir = $config['file_manager']['dir'];
if ($user = $this->identity()) {

} else {
return $this->redirect()->toRoute('home');
}

$this->_dir = realpath($fileManagerDir) .
DIRECTORY_SEPARATOR .
$user->getUsrId();
}

public function indexAction()
{
$this->init();

$files = array();
if (is_dir($this->_dir)) {
$handle = opendir($this->_dir);
if ($handle) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$files[] = $entry;
}
}
closedir($handle);
}
}

return new ViewModel(array('files' => $files));
}

public function uploadAction()
{
$this->init();
if (!is_dir($this->_dir)) {
mkdir($this->_dir, 0777);
}
$form = new UploadForm($this->_dir, 'upload-form');
$request = $this->getRequest();
if ($request->isPost()) {
// Make certain to merge the files info!
$post = array_merge_recursive(
$request->getPost()->toArray(),
$request->getFiles()->toArray()
);

$form->setData($post);
if ($form->isValid()) {
$data = $form->getData();
// Form is valid, save the form!
$this->setFileNames($data);
// The data can be saved in the DataBase
return $this->redirect()->toRoute('csn-file-manager');
}
}


return new ViewModel(array('form' => $form));
}

public function downloadAction()
{
$this->init();
$file = urldecode($this->params()->fromRoute('id'));
$filename = $this->_dir . DIRECTORY_SEPARATOR . $file;

if (file_exists($filename)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename)); // $file));
ob_clean();
flush();
// readfile($file);
readfile($filename);
exit;
}

return new ViewModel(array());
}

public function deleteAction()
{
$this->init();
$file = urldecode($this->params()->fromRoute('id'));
$filename = $this->_dir . DIRECTORY_SEPARATOR . $file;
unlink ($filename);
return $this->redirect()->toRoute('csn-file-manager');
return new ViewModel(array());
}

public function viewAction()
{
$this->init();
$file = urldecode($this->params()->fromRoute('id'));
$filename = $this->_dir . DIRECTORY_SEPARATOR . $file;
$contents = null;
if (file_exists($filename)) {
$handle = fopen($filename, "r"); // "r" - not r but b for Windows "b" - keeps giving me errors no file
$contents = fread($handle, filesize($filename));
fclose($handle);
}
return new ViewModel(array('contents' => $contents));
}

public function getImageAction()
{
$this->init();
$file = urldecode($this->params()->fromRoute('id'));
$filename = $this->_dir . DIRECTORY_SEPARATOR . $file;

if (file_exists($filename)) {
header('Content-Type: image/jpeg');
ob_clean();
flush();
readfile($filename);
exit;
}
return new ViewModel(array());
}

/**
* Change the names of the uploaded files to their original names. Since we don't keep anything in the DB
*
* @param array $data array of arrays
* @return void
*/
protected function setFileNames($data)
{
unset($data['submit']);
foreach ($data['image-file'] as $key => $file) {
rename($file['tmp_name'], $this->_dir . DIRECTORY_SEPARATOR . $file['name']);
}
}
}
75 changes: 75 additions & 0 deletions module/CsnFileManager/src/CsnFileManager/Form/UploadForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
// File: UploadForm.php // File Post-Redirect-Get Plugin
namespace CsnFileManager\Form;

use Zend\InputFilter;
use Zend\Form\Element;
use Zend\Form\Form;

class UploadForm extends Form
{

protected $_dir;

public function __construct($dir, $name = null, $options = array())
{
parent::__construct($name, $options);
$this->_dir = $dir;
$this->addElements();
$this->addInputFilter();
}

public function addElements()
{
// File Input
$file = new Element\File('image-file');
$file->setLabel('Avatar Image Upload')
->setAttribute('id', 'image-file')
->setAttribute('multiple', true); // That's it
$this->add($file);

$this->add(array(
'name' => 'submit',
'attributes' => array(
'type' => 'submit',
'value' => 'Upload',
'id' => 'submitbutton',
),
));
}

/**
* Adding a RenameUpload filter to our form’s file input, with details on where the valid files should be stored
*/
public function addInputFilter()
{
$inputFilter = new InputFilter\InputFilter();

// File Input
$fileInput = new InputFilter\FileInput('image-file');
$fileInput->setRequired(true);

// You only need to define validators and filters
// as if only one file was being uploaded. All files
// will be run through the same validators and filters
// automatically.
$fileInput->getValidatorChain()
->attachByName('filesize', array('max' => 204800))
->attachByName('filemimetype', array('mimeType' => 'image/png,image/x-png, image/jpeg'));
// ->attachByName('fileimagesize', array('maxWidth' => 100, 'maxHeight' => 100));

// All files will be renamed, i.e.:
// ./data/tmpuploads/avatar_4b3403665fea6.png,
// ./data/tmpuploads/avatar_5c45147660fb7.png
$fileInput->getFilterChain()->attachByName(
'filerenameupload',
array(
'target' => $this->_dir, // './data/tmpuploads/avatar.png',
'randomize' => true,
)
);
$inputFilter->add($fileInput);

$this->setInputFilter($inputFilter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>delete.phtml</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>download.phtml</h1>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<h1>get-image.phtml</h1>
32 changes: 32 additions & 0 deletions module/CsnFileManager/view/csn-file-manager/index/index.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<br /><br /><br /><br /><br /><br />
<?php
$title = 'My files';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<p>
<a href="<?php echo $this->url('csn-file-manager/default', array('controller' => 'index', 'action'=>'upload'));?>">Upload</a>
</p>

<table class="table">
<tr>
<th>Name</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
<?php foreach ($files as $file) : ?>
<tr>
<td><?php echo $file;?></td>
<td>
<a href="<?php echo $this->url('csn-file-manager/default', // SUPER IMPORTANT use csn-file-manager/<segment> NOT ONLY grace-drops
array('controller' => 'index', 'action'=>'get-image', 'id' => urlencode($file)));?>">Get</a>
<a href="<?php echo $this->url('csn-file-manager/default', // SUPER IMPORTANT use csn-file-manager/<segment> NOT ONLY grace-drops
array('controller' => 'index', 'action'=>'view', 'id' => urlencode($file)));?>">View</a>
<a href="<?php echo $this->url('csn-file-manager/default', // SUPER IMPORTANT use csn-file-manager/<segment> NOT ONLY grace-drops
array('controller' => 'index', 'action'=>'download', 'id' => urlencode($file)));?>">Download</a>
<a href="<?php echo $this->url('csn-file-manager/default', // SUPER IMPORTANT use csn-file-manager/<segment>
array('controller' => 'index', 'action'=>'delete', 'id' => urlencode($file)));?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>
Loading

0 comments on commit 6061a7e

Please sign in to comment.