forked from wingman007/fmi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d7bdc66
commit 6061a7e
Showing
14 changed files
with
428 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
uploads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__, | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
) | ||
) | ||
) | ||
), | ||
); |
156 changes: 156 additions & 0 deletions
156
module/CsnFileManager/src/CsnFileManager/Controller/IndexController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
75
module/CsnFileManager/src/CsnFileManager/Form/UploadForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>delete.phtml</h1> |
1 change: 1 addition & 0 deletions
1
module/CsnFileManager/view/csn-file-manager/index/download.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>download.phtml</h1> |
1 change: 1 addition & 0 deletions
1
module/CsnFileManager/view/csn-file-manager/index/get-image.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<h1>get-image.phtml</h1> |
32 changes: 32 additions & 0 deletions
32
module/CsnFileManager/view/csn-file-manager/index/index.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> </th> | ||
<th> </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> |
Oops, something went wrong.