diff --git a/config/application.config.php b/config/application.config.php index ae76bfc5..533e44f6 100644 --- a/config/application.config.php +++ b/config/application.config.php @@ -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( diff --git a/config/autoload/acl.global.php b/config/autoload/acl.global.php index 77487860..88388f0f 100644 --- a/config/autoload/acl.global.php +++ b/config/autoload/acl.global.php @@ -82,6 +82,9 @@ 'AuthDoctrine\Controller\Admin' => array( 'all' => 'admin', ), + 'CsnFileManager\Controller\Index' => array( + 'all' => 'member', + ), // for CMS articles 'Public Resource' => array( 'view' => 'guest', diff --git a/data/.gitignore b/data/.gitignore new file mode 100644 index 00000000..95a48fbc --- /dev/null +++ b/data/.gitignore @@ -0,0 +1 @@ +uploads \ No newline at end of file diff --git a/module/CsnFileManager/Module.php b/module/CsnFileManager/Module.php new file mode 100644 index 00000000..25301b8a --- /dev/null +++ b/module/CsnFileManager/Module.php @@ -0,0 +1,22 @@ + array( + 'namespaces' => array( + __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, + ), + ), + ); + } +} \ No newline at end of file diff --git a/module/CsnFileManager/config/module.config.php b/module/CsnFileManager/config/module.config.php new file mode 100644 index 00000000..c62ccb7e --- /dev/null +++ b/module/CsnFileManager/config/module.config.php @@ -0,0 +1,64 @@ + 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', + ) + ) + ) + ), +); \ No newline at end of file diff --git a/module/CsnFileManager/src/CsnFileManager/Controller/IndexController.php b/module/CsnFileManager/src/CsnFileManager/Controller/IndexController.php new file mode 100644 index 00000000..3e5579fb --- /dev/null +++ b/module/CsnFileManager/src/CsnFileManager/Controller/IndexController.php @@ -0,0 +1,156 @@ +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']); + } + } +} \ No newline at end of file diff --git a/module/CsnFileManager/src/CsnFileManager/Form/UploadForm.php b/module/CsnFileManager/src/CsnFileManager/Form/UploadForm.php new file mode 100644 index 00000000..ba52968e --- /dev/null +++ b/module/CsnFileManager/src/CsnFileManager/Form/UploadForm.php @@ -0,0 +1,75 @@ +_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); + } +} \ No newline at end of file diff --git a/module/CsnFileManager/view/csn-file-manager/index/delete.phtml b/module/CsnFileManager/view/csn-file-manager/index/delete.phtml new file mode 100644 index 00000000..3ac50625 --- /dev/null +++ b/module/CsnFileManager/view/csn-file-manager/index/delete.phtml @@ -0,0 +1 @@ +

delete.phtml

\ No newline at end of file diff --git a/module/CsnFileManager/view/csn-file-manager/index/download.phtml b/module/CsnFileManager/view/csn-file-manager/index/download.phtml new file mode 100644 index 00000000..77b51867 --- /dev/null +++ b/module/CsnFileManager/view/csn-file-manager/index/download.phtml @@ -0,0 +1 @@ +

download.phtml

\ No newline at end of file diff --git a/module/CsnFileManager/view/csn-file-manager/index/get-image.phtml b/module/CsnFileManager/view/csn-file-manager/index/get-image.phtml new file mode 100644 index 00000000..58fa35b3 --- /dev/null +++ b/module/CsnFileManager/view/csn-file-manager/index/get-image.phtml @@ -0,0 +1 @@ +

get-image.phtml

\ No newline at end of file diff --git a/module/CsnFileManager/view/csn-file-manager/index/index.phtml b/module/CsnFileManager/view/csn-file-manager/index/index.phtml new file mode 100644 index 00000000..b49baede --- /dev/null +++ b/module/CsnFileManager/view/csn-file-manager/index/index.phtml @@ -0,0 +1,32 @@ +





+headTitle($title); +?> +

escapeHtml($title); ?>

+

+ Upload +

+ + + + + + + + + + + + + +
Name  
+ Get + View + Download + Delete +
\ No newline at end of file diff --git a/module/CsnFileManager/view/csn-file-manager/index/upload.phtml b/module/CsnFileManager/view/csn-file-manager/index/upload.phtml new file mode 100644 index 00000000..6a8ce956 --- /dev/null +++ b/module/CsnFileManager/view/csn-file-manager/index/upload.phtml @@ -0,0 +1,17 @@ +





+headTitle($title); +?> +

escapeHtml($title); ?>

+

tempFile; ?>

+form; +$form->setAttribute('action', $this->url('csn-file-manager/default', array('controller' => 'index', 'action' => 'upload'))); +$form->prepare(); + +echo $this->form()->openTag($form); +echo $this->formRow($form->get('image-file')); +echo $this->formSubmit($form->get('submit')); +echo $this->form()->closeTag(); +?> \ No newline at end of file diff --git a/module/CsnFileManager/view/csn-file-manager/index/view.phtml b/module/CsnFileManager/view/csn-file-manager/index/view.phtml new file mode 100644 index 00000000..5d504c9a --- /dev/null +++ b/module/CsnFileManager/view/csn-file-manager/index/view.phtml @@ -0,0 +1,2 @@ +"; \ No newline at end of file diff --git a/public/loader.php b/public/loader.php new file mode 100644 index 00000000..08385fc6 --- /dev/null +++ b/public/loader.php @@ -0,0 +1,52 @@ + + * example with file manager module. Only members can access + * + */ +// 1) set the folder with the assets +$path = realpath(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . '54' ; +// get the file (e.g. http://localhost:10122/loader.php?file=css\style.css) or css/style.css +$file = $_GET['file']; +// 3) create the path +$filename = $path . DIRECTORY_SEPARATOR . $file; +// 4) determen the MIME file type +$extension = array_pop(explode('.', $file)); +switch ($extension) { + case 'css' : + $mime = 'text/css'; + break; + case 'js' : + $mime = 'text/javascript'; + break; + case 'jpg' : + case 'jpeg': + case 'JPG' : + case 'JPEG': + $mime = 'image/jpeg'; + break; + case 'png' : + $mime = 'image/png'; + break; + case 'gif' : + $mime = 'image/gif'; + break; + default: + $mime = 'text/text'; + break; +} + +if (file_exists($filename)) { + header('Content-Type: ' . $mime); + ob_clean(); + flush(); + readfile($filename); + exit; +} + +header("HTTP/1.0 404 Not Found"); \ No newline at end of file