BxFilesDb.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. }
  68. public function createFolder($iParentFolder, $iAuthor, $iContext, $sTitle) {
  69. $CNF = &$this->_oConfig->CNF;
  70. $this->query("
  71. INSERT INTO `" . $CNF['TABLE_ENTRIES'] . "`
  72. (`{$CNF['FIELD_AUTHOR']}`, `{$CNF['FIELD_ADDED']}`, `{$CNF['FIELD_CHANGED']}`, `{$CNF['FIELD_TITLE']}`, `data_processed`, `{$CNF['FIELD_ALLOW_VIEW_TO']}`, `type`, `parent_folder_id`)
  73. VALUES (:author, :when, :when, :title, 1, :context, 'folder', :parent_folder_id)", [
  74. 'author' => $iAuthor,
  75. 'when' => time(),
  76. 'title' => $sTitle,
  77. 'context' => $iContext,
  78. 'parent_folder_id' => $iParentFolder,
  79. ]);
  80. }
  81. public function moveFilesToFolder($aFiles, $iFolder) {
  82. $CNF = &$this->_oConfig->CNF;
  83. $this->query("UPDATE `" . $CNF['TABLE_ENTRIES'] . "` SET `parent_folder_id` = {$iFolder} WHERE `" . $CNF['FIELD_ID'] . "` IN (".implode(',', $aFiles).")");
  84. }
  85. public function getParentFolderId($iFolder) {
  86. $CNF = &$this->_oConfig->CNF;
  87. return $this->getOne("SELECT `parent_folder_id` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `" . $CNF['FIELD_ID'] . "` = :folder", [
  88. 'folder' => $iFolder,
  89. ]);
  90. }
  91. public function getFolderFiles($iFolder) {
  92. $CNF = &$this->_oConfig->CNF;
  93. return $this->getColumn("SELECT `{$CNF['FIELD_ID']}` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `parent_folder_id` = :folder", [
  94. 'folder' => $iFolder,
  95. ]);
  96. }
  97. public function getFolderFilesEx($mFile, $sType = 'folder') {
  98. $CNF = &$this->_oConfig->CNF;
  99. $sQuery = "
  100. 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`
  101. FROM `{$CNF['TABLE_ENTRIES']}`
  102. LEFT JOIN `{$CNF['TABLE_FILES']}` ON `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_FILE_ID']}` = `{$CNF['TABLE_FILES']}`.`{$CNF['FIELD_ID']}`
  103. ";
  104. if ($sType == 'mixed') {
  105. if (!is_array($mFile) || empty($mFile)) return false;
  106. $aIDs = [];
  107. foreach ($mFile as $iFile) $aIDs[] = intval($iFile);
  108. return $this->getAll($sQuery. "WHERE `{$CNF['TABLE_ENTRIES']}`.`{$CNF['FIELD_ID']}` IN (".implode(',', $aIDs).")");
  109. } else {
  110. return $this->getAll($sQuery. "
  111. WHERE `{$CNF['TABLE_ENTRIES']}`.`".($sType == 'folder' ? 'parent_folder_id' : $CNF['FIELD_ID'])."` = :file
  112. ", [
  113. 'file' => $mFile,
  114. ]
  115. );
  116. }
  117. }
  118. public function getFolderNestingLevel($iFolder) {
  119. $iLevel = 1;
  120. while($iParentFolder = $this->getParentFolderId($iFolder)) {
  121. $iLevel++;
  122. $iFolder = $iParentFolder;
  123. }
  124. return $iLevel;
  125. }
  126. public function getFolderPath($iFolder) {
  127. $CNF = &$this->_oConfig->CNF;
  128. $aPath = [];
  129. $aFolder = $this->getContentInfoById($iFolder);
  130. if (!$aFolder) return $aPath;
  131. do {
  132. $aPath[] = [
  133. 'folder' => $aFolder[$CNF['FIELD_ID']],
  134. 'name' => $aFolder[$CNF['FIELD_TITLE']],
  135. ];
  136. if (!$aFolder['parent_folder_id']) break;
  137. $aFolder = $this->getContentInfoById($aFolder['parent_folder_id']);
  138. } while (true);
  139. return array_reverse($aPath);
  140. }
  141. public function getFoldersInContext($iContext) {
  142. $CNF = &$this->_oConfig->CNF;
  143. $sIdent = $iContext > 0 ? $CNF['FIELD_AUTHOR'] : $CNF['FIELD_ALLOW_VIEW_TO'];
  144. $aFolders = $this->getAll("SELECT `{$CNF['FIELD_ID']}`, `parent_folder_id`, `{$CNF['FIELD_TITLE']}` FROM `" . $CNF['TABLE_ENTRIES'] . "` WHERE `{$sIdent}` = :context AND `type` = 'folder'", [
  145. 'context' => $iContext,
  146. ]);
  147. if (!$aFolders) return [];
  148. return [
  149. 0 => [
  150. $CNF['FIELD_ID'] => 0,
  151. $CNF['FIELD_TITLE'] => _t('_bx_files_txt_folder_root'),
  152. 'subfolders' => $this->getSubfoldersOf($aFolders, 0),
  153. ]
  154. ];
  155. }
  156. public function getSubfoldersOf(&$aFolders, $iParent) {
  157. $CNF = &$this->_oConfig->CNF;
  158. $aResult = [];
  159. foreach ($aFolders as $aFolder) {
  160. if ($aFolder['parent_folder_id'] == $iParent) {
  161. $aFolder['subfolders'] = $this->getSubfoldersOf($aFolders, $aFolder[$CNF['FIELD_ID']]);
  162. $aResult[] = $aFolder;
  163. }
  164. }
  165. return $aResult;
  166. }
  167. public function createDownloadingJob($aFiles, $sZipFileName, $iOwner) {
  168. $this->query("INSERT INTO `bx_files_downloading_jobs` (`name`, `owner`, `files`, `started`) VALUES (:name, :owner, :files, UNIX_TIMESTAMP())", [
  169. 'files' => serialize($aFiles),
  170. 'owner' => $iOwner,
  171. 'name' => $sZipFileName
  172. ]);
  173. return $this->lastId();
  174. }
  175. public function getDownloadingJob($iJob) {
  176. $aJob = $this->getRow("SELECT * FROM `bx_files_downloading_jobs` WHERE `id` = :id", ['id' => $iJob]);
  177. if ($aJob && $aJob['files']) $aJob['files'] = unserialize($aJob['files']);
  178. return $aJob;
  179. }
  180. public function updateDownloadingJob($iJob, $aFiles, $sZipFilePath) {
  181. $this->query("UPDATE `bx_files_downloading_jobs` SET `result` = :result, `files` = :files WHERE `id` = :id", [
  182. 'result' => $sZipFilePath,
  183. 'files' => serialize($aFiles),
  184. 'id' => $iJob
  185. ]);
  186. }
  187. public function deleteOldDownloadingJobs() {
  188. $aFiles = $this->getColumn("SELECT `result` FROM `bx_files_downloading_jobs` WHERE UNIX_TIMESTAMP() - `started` > 24*3600");
  189. if ($aFiles)
  190. $this->query("DELETE FROM `bx_files_downloading_jobs` WHERE UNIX_TIMESTAMP() - `started` > 24*3600");
  191. return $aFiles;
  192. }
  193. }
  194. /** @} */