/gadgets/Directory/Model/Admin/Files.php

https://github.com/jaws-project/jaws · PHP · 282 lines · 197 code · 27 blank · 58 comment · 43 complexity · 58c5855d96a2b91baf838c4678b3888c MD5 · raw file

  1. <?php
  2. /**
  3. * Directory Gadget
  4. *
  5. * @category GadgetModel
  6. * @package Directory
  7. */
  8. class Directory_Model_Admin_Files extends Jaws_Gadget_Model
  9. {
  10. /**
  11. * Inserts a new file/directory
  12. *
  13. * @access public
  14. * @param array $data File data
  15. * @return mixed Query result
  16. */
  17. function InsertFile($data)
  18. {
  19. $data['public'] = (bool)$data['public'];
  20. $data['parent'] = (int)$data['parent'];
  21. if (!$data['public']) {
  22. $data['key'] = mt_rand(1000, 9999999999);
  23. }
  24. $data['create_time'] = $data['update_time'] = time();
  25. return Jaws_ORM::getInstance()->table('directory')->insert($data)->exec();
  26. }
  27. /**
  28. * Update a file/directory
  29. *
  30. * @access public
  31. * @param int $id File id
  32. * @param array $data File data
  33. * @return mixed Query result
  34. */
  35. function UpdateFile($id, $data)
  36. {
  37. unset($data['id']);
  38. $data['public'] = (bool)$data['public'];
  39. $data['parent'] = (int)$data['parent'];
  40. $data['update_time'] = time();
  41. return Jaws_ORM::getInstance()->table('directory')->update($data)->where('id', $id)->exec();
  42. }
  43. /**
  44. * Creates a new file
  45. *
  46. * @access public
  47. * @return array Response array
  48. */
  49. function SaveFile($data)
  50. {
  51. try {
  52. $loggedUser = (int)$this->app->session->user->id;
  53. // Validate parent
  54. if ($data['parent'] != 0) {
  55. $parent = $this->gadget->model->load('Files')->GetFile($data['parent']);
  56. if (Jaws_Error::IsError($parent)) {
  57. throw new Exception($this::t('ERROR_FILE_UPLOAD'));
  58. }
  59. }
  60. // extract tags
  61. $tags = $data['tags'];
  62. unset($data['tags']);
  63. $data['is_dir'] = false;
  64. $data['public'] = (bool)$data['public'];
  65. $data['title'] = Jaws_XSS::defilter($data['title']);
  66. $data['description'] = Jaws_XSS::defilter($data['description']);
  67. if (!$this->gadget->GetPermission('PublishFiles')) {
  68. $data['published'] = !$data['public'];
  69. } else {
  70. $data['published'] = is_null($data['published']) ? true : (bool)$data['published'];
  71. }
  72. $dirPath = ROOT_DATA_PATH . 'directory';
  73. if (!is_dir($dirPath)) {
  74. if (!Jaws_FileManagement_File::mkdir($dirPath)) {
  75. throw new Exception('DIRECTORY_ERROR_FILE_UPLOAD');
  76. }
  77. }
  78. if (!empty($data['id'])) {
  79. $dbFileInfo = $this->gadget->model->load('Files')->GetFile($data['id']);
  80. if (Jaws_Error::IsError($dbFileInfo) || empty($dbFileInfo)) {
  81. return Jaws_HTTPError::Get(404);
  82. }
  83. // check permission
  84. if ($dbFileInfo['public']) {
  85. if (!$this->gadget->GetPermission('PublishFiles')) {
  86. return Jaws_HTTPError::Get(403);
  87. }
  88. } elseif ($dbFileInfo['user'] != $loggedUser) {
  89. return Jaws_HTTPError::Get(403);
  90. }
  91. }
  92. $files = Jaws_FileManagement_File::uploadFiles($_FILES, $dirPath, '', null);
  93. if (Jaws_Error::IsError($files)) {
  94. throw new Exception($files->getMessage());
  95. }
  96. if (isset($files['file'])) {
  97. $data['host_filename'] = $files['file'][0]['host_filename'];
  98. $data['user_filename'] = $files['file'][0]['user_filename'];
  99. $data['mime_type'] = $files['file'][0]['host_mimetype'];
  100. $data['file_size'] = $files['file'][0]['host_filesize'];
  101. } elseif (isset($data['host_filename'])) {
  102. if ($data['host_filename'] == ':nochange:') {
  103. if (isset($dbFileInfo)) {
  104. $data['host_filename'] = $dbFileInfo['host_filename'];
  105. } else {
  106. unset($data['host_filename']);
  107. unset($data['user_filename']);
  108. unset($data['mime_type']);
  109. unset($data['file_size']);
  110. }
  111. }
  112. // do nothing
  113. } elseif (isset($dbFileInfo)) {
  114. $data['host_filename'] = $dbFileInfo['host_filename'];
  115. $data['user_filename'] = $dbFileInfo['user_filename'];
  116. $data['mime_type'] = $dbFileInfo['mime_type'];
  117. $data['file_size'] = $dbFileInfo['file_size'];
  118. } else {
  119. // File is mandatory
  120. throw new Exception($this::t('ERROR_FILE_UPLOAD'));
  121. }
  122. if (isset($files['thumbnail']) || isset($data['thumbnail'])) {
  123. $thumbfile = isset($data['thumbnail'])? $data['thumbnail'] : $files['thumbnail'][0]['host_filename'];
  124. // Save resize thumbnail file
  125. $thumbSize = $this->gadget->registry->fetch('thumbnail_size');
  126. $thumbSize = empty($thumbSize) ? '128x128' : $thumbSize;
  127. $thumbSize = explode('x', $thumbSize);
  128. $objImage = Jaws_Image::factory();
  129. if (Jaws_Error::IsError($objImage)) {
  130. throw new Exception($objImage->getMessage());
  131. }
  132. $res = $objImage->load($dirPath. '/'. $thumbfile);
  133. if (Jaws_Error::IsError($result)) {
  134. throw new Exception($res->getMessage());
  135. }
  136. $objImage->resize($thumbSize[0], $thumbSize[1]);
  137. $res = $objImage->save($dirPath. '/'. basename($data['host_filename']). '.thumbnail.png', 'png');
  138. $objImage->free();
  139. if (Jaws_Error::IsError($res)) {
  140. throw new Exception($res->getMessage());
  141. }
  142. Jaws_FileManagement_File::delete($dirPath. '/'. $thumbfile);
  143. }
  144. unset($data['thumbnail']);
  145. if(isset($data['user_filename'])) {
  146. $data['file_type'] = $this->gadget->model->load('Files')->getFileType($data['user_filename']);
  147. }
  148. if (!empty($data['id'])) {
  149. // update old file info
  150. $result = $this->UpdateFile($data['id'], $data);
  151. if (Jaws_Error::IsError($result)) {
  152. // TODO: delete uploaded file
  153. throw new Exception($this::t('ERROR_FILE_CREATE'));
  154. }
  155. // Update Tags
  156. if (Jaws_Gadget::IsGadgetInstalled('Tags') && !empty($tags)) {
  157. $tModel = Jaws_Gadget::getInstance('Tags')->model->loadAdmin('Tags');
  158. $tModel->UpdateReferenceTags(
  159. 'Directory',
  160. 'file',
  161. $data['id'],
  162. $data['published'],
  163. time(),
  164. $tags
  165. );
  166. }
  167. return $this::t('NOTICE_FILE_UPDATED');
  168. } else {
  169. //insert new file
  170. unset($data['id']);
  171. $data['user'] = $loggedUser;
  172. $id = $this->InsertFile($data);
  173. if (Jaws_Error::IsError($id)) {
  174. // TODO: delete uploaded file
  175. throw new Exception($this::t('ERROR_FILE_CREATE'));
  176. }
  177. // Insert Tags
  178. if (Jaws_Gadget::IsGadgetInstalled('Tags') && !empty($tags)) {
  179. $tModel = Jaws_Gadget::getInstance('Tags')->model->loadAdmin('Tags');
  180. $tModel->InsertReferenceTags(
  181. 'Directory',
  182. 'file',
  183. $id,
  184. $data['published'],
  185. time(),
  186. $tags
  187. );
  188. }
  189. // shout Activities event
  190. $this->gadget->event->shout('Activities', array('action'=>'File'));
  191. return $this::t('NOTICE_FILE_CREATED');
  192. }
  193. } catch (Exception $e) {
  194. return Jaws_Error::raiseError($e->getMessage(), __FUNCTION__);
  195. }
  196. }
  197. /**
  198. * Updates parent of the file/directory
  199. *
  200. * @access public
  201. * @param int $id File ID
  202. * @param int $parent New file parent
  203. * @return mixed Query result
  204. */
  205. function Move($id, $parent)
  206. {
  207. $table = Jaws_ORM::getInstance()->table('directory');
  208. $table->update(array('parent' => $parent));
  209. return $table->where('id', $id)->exec();
  210. }
  211. /**
  212. * Deletes file/directory
  213. *
  214. * @access public
  215. * @param array $data File data
  216. * @return mixed Query result
  217. */
  218. function DeleteFile($data)
  219. {
  220. if ($data['is_dir']) {
  221. $files = $this->gadget->model->load('Files')->GetFiles(array('parent' => $data['id']));
  222. if (Jaws_Error::IsError($files)) {
  223. return false;
  224. }
  225. foreach ($files as $file) {
  226. $this->DeleteFile($file);
  227. }
  228. }
  229. // Delete file/folder and related shortcuts
  230. $table = Jaws_ORM::getInstance()->table('directory');
  231. $table->delete()->where('id', $data['id']);
  232. $res = $table->exec();
  233. if (Jaws_Error::IsError($res)) {
  234. return false;
  235. }
  236. // Delete from disk
  237. if (!$data['is_dir']) {
  238. $filename = ROOT_DATA_PATH . 'directory/' . $data['host_filename'];
  239. if (Jaws_FileManagement_File::file_exists($filename)) {
  240. if (!Jaws_FileManagement_File::delete($filename)) {
  241. return false;
  242. }
  243. }
  244. // delete thumbnail file
  245. $fileInfo = Jaws_FileManagement_File::pathinfo($filename);
  246. $thumbnailPath = ROOT_DATA_PATH . 'directory/' . $fileInfo['filename'] . '.thumbnail.png';
  247. if (Jaws_FileManagement_File::file_exists($thumbnailPath)) {
  248. if (!Jaws_FileManagement_File::delete($thumbnailPath)) {
  249. return false;
  250. }
  251. }
  252. }
  253. return true;
  254. }
  255. }