BxFilesDb.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Files Files
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. /*
  12. * Module database queries
  13. */
  14. class BxFilesDb extends BxBaseModFilesDb
  15. {
  16. function __construct(&$oConfig)
  17. {
  18. parent::__construct($oConfig);
  19. }
  20. public function updateFileId ($iContentId, $iFileId)
  21. {
  22. return $this->updateEntries(array('file_id' => $iFileId, 'data' => '', 'data_processed' => 0), array('id' => $iContentId));
  23. }
  24. public function updateFileData ($iContentId, $sData, $iDataProcessed = 1)
  25. {
  26. return $this->updateEntries(array('data' => $sData, 'data_processed' => $iDataProcessed), array('id' => $iContentId));
  27. }
  28. public function getNotProcessedFiles ($iLimit)
  29. {
  30. $sQuery = $this->prepare ("SELECT * FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `data_processed` = 0 ORDER BY `added` ASC LIMIT ?", $iLimit);
  31. return $this->getAll($sQuery);
  32. }
  33. public function bookmarkFile($iContentId, $iProfileId) {
  34. if ($this->isFileBookmarked($iContentId, $iProfileId)) {
  35. $this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_BOOKMARKS'] . "` WHERE `object_id` = :id AND `profile_id` = :profile_id", [
  36. 'id' => $iContentId,
  37. 'profile_id' => $iProfileId
  38. ]);
  39. } else {
  40. $this->query("INSERT INTO `" . $this->_oConfig->CNF['TABLE_BOOKMARKS'] . "` (`object_id`, `profile_id`) VALUES (:id, :profile_id)", [
  41. 'id' => $iContentId,
  42. 'profile_id' => $iProfileId
  43. ]);
  44. }
  45. }
  46. public function deleteFileBookmarks($iContentId) {
  47. $this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_BOOKMARKS'] . "` WHERE `object_id` = :id", [
  48. 'id' => $iContentId,
  49. ]);
  50. }
  51. public function deleteProfileBookmarks($iProfileId) {
  52. $this->query("DELETE FROM `" . $this->_oConfig->CNF['TABLE_BOOKMARKS'] . "` WHERE `profile_id` = :profile_id", [
  53. 'profile_id' => $iProfileId,
  54. ]);
  55. }
  56. public function isFileBookmarked($iContentId, $iProfileId) {
  57. return $this->getOne("SELECT COUNT(*) FROM `" . $this->_oConfig->CNF['TABLE_BOOKMARKS'] . "` WHERE `object_id` = :id AND `profile_id` = :profile_id", [
  58. 'id' => $iContentId,
  59. 'profile_id' => $iProfileId
  60. ]);
  61. }
  62. public function updateEntryTitle($iContentId, $sNewTitle) {
  63. $this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` SET `" . $this->_oConfig->CNF['FIELD_TITLE'] . "` = :title WHERE `" . $this->_oConfig->CNF['FIELD_ID'] . "` = :id", [
  64. 'id' => $iContentId,
  65. 'title' => $sNewTitle,
  66. ]);
  67. $iFileId = $this->getOne("SELECT `file_id` FROM `" . $this->_oConfig->CNF['TABLE_ENTRIES'] . "` WHERE `" . $this->_oConfig->CNF['FIELD_ID'] . "` = :id", [
  68. 'id' => $iContentId,
  69. ]);
  70. if ($iFileId)
  71. $this->query("UPDATE `" . $this->_oConfig->CNF['TABLE_FILES'] . "` SET `file_name` = :title WHERE `id` = :id", [
  72. 'id' => $iFileId,
  73. 'title' => $sNewTitle,
  74. ]);
  75. }
  76. public function createFolder($iParentFolder, $iAuthor, $iContext, $sTitle) {
  77. $CNF = &$this->_oConfig->CNF;
  78. $this->query("
  79. INSERT INTO `" . $CNF['TABLE_ENTRIES'] . "`
  80. (`{$CNF['FIELD_AUTHOR']}`, `{$CNF['FIELD_ADDED']}`, `{$CNF['FIELD_CHANGED']}`, `{$CNF['FIELD_TITLE']}`, `data_processed`, `{$CNF['FIELD_ALLOW_VIEW_TO']}`, `type`, `parent_folder_id`)
  81. VALUES (:author, :when, :when, :title, 1, :context, 'folder', :parent_folder_id)", [
  82. 'author' => $iAuthor,
  83. 'when' => time(),
  84. 'title' => $sTitle,
  85. 'context' => $iContext,
  86. 'parent_folder_id' => $iParentFolder,
  87. ]);
  88. }
  89. public function moveFilesToFolder($aFiles, $iFolder) {
  90. $CNF = &$this->_oConfig->CNF;
  91. $this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `parent_folder_id` = {$iFolder} WHERE `" . $CNF['FIELD_ID'] . "` IN (".implode(',', $aFiles).")");
  92. }
  93. public function getParentFolderId($iFolder) {
  94. $CNF = &$this->_oConfig->CNF;
  95. return $this->getOne("SELECT `parent_folder_id` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_ID'] . "` = :folder", [
  96. 'folder' => $iFolder,
  97. ]);
  98. }
  99. public function getFolderFiles($iFolder) {
  100. $CNF = &$this->_oConfig->CNF;
  101. return $this->getColumn("SELECT `{$CNF['FIELD_ID']}` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `parent_folder_id` = :folder", [
  102. 'folder' => $iFolder,
  103. ]);
  104. }
  105. public function getFolderFilesEx($mFile, $sType = 'folder') {
  106. $CNF = &$this->_oConfig->CNF;
  107. $sQuery = "
  108. SELECT `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_ID']}`, `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_FILE_ID']}`, `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_AUTHOR']}`, `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_TITLE']}`, `{$CNF['TABLE_ENTRIES']}`.`type`, `{$CNF['TABLE_FILES']}`.`path`, `{$CNF['TABLE_FILES']}`.`ext`, `{$CNF['TABLE_FILES']}`.`size`
  109. FROM `{$CNF['TABLE_ENTRIES']}`
  110. LEFT JOIN `{$CNF['TABLE_FILES']}` ON `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_FILE_ID']}` = `{$CNF['TABLE_FILES']}`.`{$CNF['FIELD_ID']}`
  111. ";
  112. if ($sType == 'mixed') {
  113. if (!is_array($mFile) || empty($mFile)) return false;
  114. $aIDs = [];
  115. foreach ($mFile as $iFile) $aIDs[] = intval($iFile);
  116. return $this->getAll($sQuery. "WHERE `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_ID']}` IN (".implode(',', $aIDs).")");
  117. } else {
  118. return $this->getAll($sQuery. "
  119. WHERE `{$CNF['TABLE_ENTRIES']}`.`".($sType == 'folder' ? 'parent_folder_id' : $CNF['FIELD_ID'])."` = :file
  120. ", [
  121. 'file' => $mFile,
  122. ]
  123. );
  124. }
  125. }
  126. public function getFolderNestingLevel($iFolder) {
  127. $iLevel = 1;
  128. while($iParentFolder = $this->getParentFolderId($iFolder)) {
  129. $iLevel++;
  130. $iFolder = $iParentFolder;
  131. }
  132. return $iLevel;
  133. }
  134. public function getFolderPath($iFolder) {
  135. $CNF = &$this->_oConfig->CNF;
  136. $aPath = [];
  137. $aFolder = $this->getContentInfoById($iFolder);
  138. if (!$aFolder) return $aPath;
  139. do {
  140. $aPath[] = [
  141. 'folder' => $aFolder[$CNF['FIELD_ID']],
  142. 'name' => $aFolder[$CNF['FIELD_TITLE']],
  143. ];
  144. if (!$aFolder['parent_folder_id']) break;
  145. $aFolder = $this->getContentInfoById($aFolder['parent_folder_id']);
  146. } while (true);
  147. return array_reverse($aPath);
  148. }
  149. public function getFoldersInContext($iContext) {
  150. $CNF = &$this->_oConfig->CNF;
  151. $sIdent = $iContext > 0 ? $CNF['FIELD_AUTHOR'] : $CNF['FIELD_ALLOW_VIEW_TO'];
  152. $aFolders = $this->getAll("SELECT `{$CNF['FIELD_ID']}`, `parent_folder_id`, `{$CNF['FIELD_TITLE']}` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `{$sIdent}` = :context AND `type` = 'folder'", [
  153. 'context' => $iContext,
  154. ]);
  155. if (!$aFolders) return [];
  156. return [
  157. 0 => [
  158. $CNF['FIELD_ID'] => 0,
  159. $CNF['FIELD_TITLE'] => _t('_bx_files_txt_folder_root'),
  160. 'subfolders' => $this->getSubfoldersOf($aFolders, 0),
  161. ]
  162. ];
  163. }
  164. public function getSubfoldersOf(&$aFolders, $iParent) {
  165. $CNF = &$this->_oConfig->CNF;
  166. $aResult = [];
  167. foreach ($aFolders as $aFolder) {
  168. if ($aFolder['parent_folder_id'] == $iParent) {
  169. $aFolder['subfolders'] = $this->getSubfoldersOf($aFolders, $aFolder[$CNF['FIELD_ID']]);
  170. $aResult[] = $aFolder;
  171. }
  172. }
  173. return $aResult;
  174. }
  175. public function createDownloadingJob($aFiles, $sZipFileName, $iOwner) {
  176. $this->query("INSERT INTO `bx_files_downloading_jobs` (`name`, `owner`, `files`, `started`) VALUES (:name, :owner, :files, UNIX_TIMESTAMP())", [
  177. 'files' => serialize($aFiles),
  178. 'owner' => $iOwner,
  179. 'name' => $sZipFileName
  180. ]);
  181. return $this->lastId();
  182. }
  183. public function getDownloadingJob($iJob) {
  184. $aJob = $this->getRow("SELECT * FROM `bx_files_downloading_jobs` WHERE `id` = :id", ['id' => $iJob]);
  185. if ($aJob && $aJob['files']) $aJob['files'] = unserialize($aJob['files']);
  186. return $aJob;
  187. }
  188. public function updateDownloadingJob($iJob, $aFiles, $sZipFilePath) {
  189. $this->query("UPDATE `bx_files_downloading_jobs` SET `result` = :result, `files` = :files WHERE `id` = :id", [
  190. 'result' => $sZipFilePath,
  191. 'files' => serialize($aFiles),
  192. 'id' => $iJob
  193. ]);
  194. }
  195. public function deleteOldDownloadingJobs() {
  196. $aFiles = $this->getColumn("SELECT `result` FROM `bx_files_downloading_jobs` WHERE UNIX_TIMESTAMP() - `started` > 24*3600");
  197. if ($aFiles)
  198. $this->query("DELETE FROM `bx_files_downloading_jobs` WHERE UNIX_TIMESTAMP() - `started` > 24*3600");
  199. return $aFiles;
  200. }
  201. public function setStorageAllowedExtensions($sExtensions) {
  202. $CNF = &$this->_oConfig->CNF;
  203. $aExtensions = explode(',', $sExtensions);
  204. if ($aExtensions) foreach ($aExtensions as $iKey => $sExtension) {
  205. $aExtensions[$iKey] = trim($sExtension, ' .');
  206. }
  207. $sExtensionsSet = implode(',', $aExtensions);
  208. $this->query("UPDATE `sys_objects_storage` SET `ext_mode` = :ext_mode, `ext_allow` = :extensions WHERE `sys_objects_storage`.`object` = :storage_object", [
  209. 'extensions' => $sExtensionsSet,
  210. 'ext_mode' => empty($sExtensionsSet) ? 'deny-allow' : 'allow-deny',
  211. 'storage_object' => $CNF['OBJECT_STORAGE'],
  212. ]);
  213. }
  214. }
  215. /** @} */