PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/Field/src/Controller/FileHandlerController.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 140 lines | 77 code | 16 blank | 47 comment | 7 complexity | 46cccd07b5be992978ef9dd2c4db5fc1 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * Licensed under The GPL-3.0 License
  4. * For full copyright and license information, please see the LICENSE.txt
  5. * Redistributions of files must retain the above copyright notice.
  6. *
  7. * @since 2.0.0
  8. * @author Christopher Castro <chris@quickapps.es>
  9. * @link http://www.quickappscms.org
  10. * @license http://opensource.org/licenses/gpl-3.0.html GPL-3.0 License
  11. */
  12. namespace Field\Controller;
  13. use Cake\Filesystem\File;
  14. use Cake\Network\Exception\NotFoundException;
  15. use Cake\ORM\TableRegistry;
  16. use Cake\Routing\Router;
  17. use CMS\Core\Plugin;
  18. use Field\Utility\FileToolbox;
  19. /**
  20. * Handles file uploading by "File Field Handler".
  21. *
  22. * @property \Field\Model\Table\FieldInstancesTable $FieldInstances
  23. */
  24. class FileHandlerController extends AppController
  25. {
  26. /**
  27. * Uploads a new file for the given FileField instance.
  28. *
  29. * @param string $name EAV attribute name
  30. * @throws \Cake\Network\Exception\NotFoundException When invalid slug is given,
  31. * or when upload process could not be completed
  32. */
  33. public function upload($name)
  34. {
  35. $instance = $this->_getInstance($name);
  36. require_once Plugin::classPath('Field') . 'Lib/class.upload.php';
  37. $uploader = new \upload($this->request->data['Filedata']);
  38. if (!empty($instance->settings['extensions'])) {
  39. $exts = explode(',', $instance->settings['extensions']);
  40. $exts = array_map('trim', $exts);
  41. $exts = array_map('strtolower', $exts);
  42. if (!in_array(strtolower($uploader->file_src_name_ext), $exts)) {
  43. $this->_error(__d('field', 'Invalid file extension.'), 501);
  44. }
  45. }
  46. $response = '';
  47. $uploader->file_overwrite = false;
  48. $folder = normalizePath(WWW_ROOT . "/files/{$instance->settings['upload_folder']}/");
  49. $url = normalizePath("/files/{$instance->settings['upload_folder']}/", '/');
  50. $uploader->process($folder);
  51. if ($uploader->processed) {
  52. $response = json_encode([
  53. 'file_url' => Router::url($url . $uploader->file_dst_name, true),
  54. 'file_size' => FileToolbox::bytesToSize($uploader->file_src_size),
  55. 'file_name' => $uploader->file_dst_name,
  56. 'mime_icon' => FileToolbox::fileIcon($uploader->file_src_mime),
  57. ]);
  58. } else {
  59. $this->_error(__d('field', 'File upload error, details: {0}', $uploader->error), 502);
  60. }
  61. $this->viewBuilder()->layout('ajax');
  62. $this->title(__d('field', 'Upload File'));
  63. $this->set(compact('response'));
  64. }
  65. /**
  66. * Deletes a file for the given FileField instance.
  67. *
  68. * File name must be passes as `file` GET parameter.
  69. *
  70. * @param string $name EAV attribute name
  71. * @return void
  72. * @throws \Cake\Network\Exception\NotFoundException When invalid attribute name
  73. * is given
  74. */
  75. public function delete($name)
  76. {
  77. $this->loadModel('Field.FieldInstances');
  78. $instance = $this->_getInstance($name);
  79. if ($instance && !empty($this->request->query['file'])) {
  80. $file = normalizePath(WWW_ROOT . "/files/{$instance->settings['upload_folder']}/{$this->request->query['file']}", DS);
  81. $file = new File($file);
  82. $file->delete();
  83. }
  84. $response = '';
  85. $this->viewBuilder()->layout('ajax');
  86. $this->title(__d('field', 'Delete File'));
  87. $this->set(compact('response'));
  88. }
  89. /**
  90. * Get field instance information.
  91. *
  92. * @param string $name EAV attribute name
  93. * @return \Field\Model\Entity\FieldInstance
  94. * @throws \Cake\Network\Exception\NotFoundException When invalid attribute name
  95. * is given
  96. */
  97. protected function _getInstance($name)
  98. {
  99. $this->loadModel('Field.FieldInstances');
  100. $instance = $this->FieldInstances
  101. ->find()
  102. ->contain(['EavAttribute'])
  103. ->where(['EavAttribute.name' => $name])
  104. ->first();
  105. if (!$instance) {
  106. $this->_error(__d('field', 'Invalid field instance.'), 504);
  107. }
  108. return $instance;
  109. }
  110. /**
  111. * Sends a JSON message error.
  112. *
  113. * @param string $message The message
  114. * @param int $code A unique code identifier for this message
  115. * @return void Stops scripts execution
  116. */
  117. protected function _error($message, $code)
  118. {
  119. header("HTTP/1.0 {$code} {$message}");
  120. echo $message;
  121. TableRegistry::get('Field.FieldInstances')->connection()->disconnect();
  122. exit(0);
  123. }
  124. }