BxDolStorageQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Database queries for File storage class.
  11. * @see BxDolStorage
  12. */
  13. class BxDolStorageQuery extends BxDolDb
  14. {
  15. protected $_aObject;
  16. protected $_sTableFiles;
  17. public function __construct($aObject)
  18. {
  19. parent::__construct();
  20. $this->_aObject = $aObject;
  21. $this->_sTableFiles = '`' . $aObject['table_files'] . '`';
  22. }
  23. static public function getStorageObject ($sObject)
  24. {
  25. $oDb = BxDolDb::getInstance();
  26. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_storage` WHERE `object` = ?", $sObject);
  27. return $oDb->getRow($sQuery);
  28. }
  29. static public function getStorageObjects ()
  30. {
  31. $oDb = BxDolDb::getInstance();
  32. $sQuery = "SELECT * FROM `sys_objects_storage`";
  33. return $oDb->getAll($sQuery);
  34. }
  35. public function changeStorageEngine ($sEngine)
  36. {
  37. $sQuery = $this->prepare("UPDATE `sys_objects_storage` SET `engine` = ? WHERE `object` = ?", $sEngine, $this->_aObject['object']);
  38. return $this->query($sQuery);
  39. }
  40. public function getMimeTypeByExt($sExt)
  41. {
  42. $sQuery = $this->prepare("SELECT `mime_type` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  43. return $this->getOne($sQuery);
  44. }
  45. public function getIconByExt($sExt)
  46. {
  47. $sQuery = $this->prepare("SELECT `icon` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  48. return $this->getOne($sQuery);
  49. }
  50. public function getIconFontByExt($sExt)
  51. {
  52. $sQuery = $this->prepare("SELECT `icon_font` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  53. return $this->getOne($sQuery);
  54. }
  55. public function clearAllMimeTypes()
  56. {
  57. return $this->query("TRUNCATE TABLE `sys_storage_mime_types`");
  58. }
  59. public function addMimeType($sMimeType, $sExt, $sIcon = '', $sIconFont = '')
  60. {
  61. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_mime_types` SET `ext` = ?, `mime_type` = ?, `icon` = ?, `icon_font` = ?", $sExt, $sMimeType, $sIcon, $sIconFont);
  62. return $this->query($sQuery);
  63. }
  64. public function getStorageObjectQuota()
  65. {
  66. return $this->_aObject;
  67. }
  68. public function updateStorageObjectQuota($iSize, $iNumber = 1)
  69. {
  70. $iTime = time();
  71. $sQuery = $this->prepare("
  72. UPDATE `sys_objects_storage`
  73. SET `current_size` = `current_size` + ?, `current_number` = `current_number` + (?), `ts` = ?
  74. WHERE `object` = ?",
  75. $iSize, $iNumber, $iTime, $this->_aObject['object']
  76. );
  77. if ($this->query($sQuery)) {
  78. $this->_aObject = $this->getStorageObject($this->_aObject['object']);
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }
  84. public function getUserQuota($iProfileId)
  85. {
  86. return BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  87. }
  88. public function updateUserQuota($iProfileId, $iSize, $iNumber = 1)
  89. {
  90. return BxDolProfileQuery::getInstance()->updateProfileQuota($iProfileId, $iSize, $iNumber);
  91. }
  92. public function addFile($iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $isPrivate)
  93. {
  94. $sQuery = $this->prepare("INSERT INTO " . $this->_sTableFiles . " SET
  95. `profile_id` = ?, `remote_id` = ?, `path` = ?, `file_name` = ?, `mime_type` = ?, `ext` = ?, `size` = ?, `added` = ?, `modified` = ?, `private` = ?",
  96. $iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $iTime, $isPrivate ? 1 : 0
  97. );
  98. return $this->query($sQuery);
  99. }
  100. public function modifyFilePrivate($iFileId, $isPrivate)
  101. {
  102. return $this->modifyCustomField($iFileId, 'private', $isPrivate ? 1 : 0);
  103. }
  104. public function modifyCustomField($iFileId, $sField, $sValue, $isUpdateModifiedField = true)
  105. {
  106. $aBindings = array(
  107. $sField => $sValue,
  108. 'id' => $iFileId
  109. );
  110. $sAdd = '';
  111. if ($isUpdateModifiedField) {
  112. $aBindings['modified'] = time();
  113. $sAdd = ", `modified` = :modified";
  114. }
  115. $sQuery = "UPDATE " . $this->_sTableFiles . " SET `{$sField}` = :{$sField} {$sAdd} WHERE `id` = :id";
  116. return $this->query($sQuery, $aBindings);
  117. }
  118. public function deleteFile($iFileId)
  119. {
  120. // delete queued record for the file
  121. $sQuery = $this->prepare("DELETE FROM `sys_storage_deletions` WHERE `object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  122. $this->query($sQuery);
  123. // delete file record
  124. $sQuery = $this->prepare("DELETE FROM " . $this->_sTableFiles . " WHERE `id` = ?", $iFileId);
  125. if (!$this->query($sQuery))
  126. return false;
  127. // delete any file traces in ghosts table
  128. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  129. $this->query($sQuery);
  130. return true;
  131. }
  132. public function getFileByFileName($sValue)
  133. {
  134. return $this->_getFileBy('`file_name`', $sValue);
  135. }
  136. public function getFileById($sValue)
  137. {
  138. return $this->_getFileBy('`id`', $sValue);
  139. }
  140. public function getFileByRemoteId($sValue)
  141. {
  142. return $this->_getFileBy('`remote_id`', $sValue);
  143. }
  144. protected function _getFileBy($sField, $sValue)
  145. {
  146. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " WHERE " . $sField . " = ?", $sValue);
  147. return $this->getRow($sQuery);
  148. }
  149. public function isTokenValid($iFileId, $sToken)
  150. {
  151. $iTime = time();
  152. $sQuery = $this->prepare("SELECT `created` FROM `sys_storage_tokens` WHERE `id` = ? AND `object` = ? AND `hash` = ? AND `created` > ?", $iFileId, $this->_aObject['object'], $sToken, $iTime - $this->_aObject['token_life']);
  153. return $this->getOne($sQuery) ? true : false;
  154. }
  155. public function genToken($iFileId)
  156. {
  157. $iTime = time();
  158. $sToken = md5($iTime . mt_rand() . BX_DOL_SECRET);
  159. $sQuery = $this->prepare("INSERT INTO `sys_storage_tokens` SET `id` = ?, `object` = ?, `hash` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $sToken, $iTime);
  160. if ($this->query($sQuery))
  161. return $sToken;
  162. else
  163. return false;
  164. }
  165. public function insertGhosts($mixedFileIds, $iProfileId, $iContentId = 0)
  166. {
  167. $iTime = time();
  168. if (!is_array($mixedFileIds))
  169. $mixedFileIds = array($mixedFileIds);
  170. $iCount = 0;
  171. foreach ($mixedFileIds as $iFileId) {
  172. $sQuery = $this->prepare("INSERT INTO `sys_storage_ghosts`
  173. SET `id` = ?, `object` = ?, `profile_id` = ?, `content_id` = ?, `created` = ?
  174. ON DUPLICATE KEY UPDATE `profile_id` = ?, `content_id` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $iProfileId, $iContentId, $iTime, $iProfileId, $iContentId, $iTime);
  175. $iCount += $this->query($sQuery);
  176. }
  177. return $iCount;
  178. }
  179. public function updateGhostsContentId($mixedFileIds, $iProfileId, $iContentId, $aProfiles = array(), $isAdmin = false)
  180. {
  181. $aBindings = array(
  182. 'content' => $iContentId,
  183. 'profile' => $iProfileId,
  184. 'object' => $this->_aObject['object']
  185. );
  186. $sSetAddon = '';
  187. $sWhere = " AND `profile_id` = :profile ";
  188. if ($aProfiles || $isAdmin) {
  189. $sSetAddon = ', `profile_id` = :profile ';
  190. $sWhere = $isAdmin ? "" : " AND `profile_id` IN(" . $this->implode_escape($aProfiles) . ")";
  191. }
  192. $sQuery = $this->prepare("UPDATE `sys_storage_ghosts` SET `content_id` = :content $sSetAddon WHERE `object` = :object $sWhere AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")");
  193. return $this->res($sQuery, $aBindings);
  194. }
  195. public function deleteGhosts($mixedFileIds, $iProfileId, $iContentId = false)
  196. {
  197. $aBindings = array(
  198. 'profile_id' => $iProfileId,
  199. 'object' => $this->_aObject['object']
  200. );
  201. $sQuery = "DELETE FROM `sys_storage_ghosts` WHERE `profile_id` = :profile_id AND `object` = :object AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")";
  202. if (false !== $iContentId) {
  203. $aBindings['content_id'] = $iContentId;
  204. $sQuery .= " AND `content_id` = :content_id";
  205. }
  206. $iCount = $this->query($sQuery, $aBindings);
  207. if ($iCount)
  208. $this->query("OPTIMIZE TABLE `sys_storage_ghosts`");
  209. return $iCount;
  210. }
  211. public function getGhosts($mixedProfileId, $iContentId = false, $isAdmin = false)
  212. {
  213. return $this->getFiles($mixedProfileId, true, $iContentId, $isAdmin);
  214. }
  215. public function getGhost($iFileId)
  216. {
  217. $sQuery = $this->prepare("SELECT * FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  218. return $this->getRow($sQuery);
  219. }
  220. public function getFiles($mixedProfileId, $isGhostsOnly = false, $iContentId = false, $isAdmin = false)
  221. {
  222. $aBindings = array();
  223. $sWhere = '';
  224. $sJoin = '';
  225. if ($isGhostsOnly) {
  226. $aBindings['object'] = $this->_aObject['object'];
  227. $sOnProfile = '';
  228. if ($isAdmin && $iContentId) {
  229. // don't check profile id for admins, so admin can edit any entry
  230. }
  231. elseif (is_array($mixedProfileId) && $mixedProfileId) {
  232. $sOnProfile = " AND `g`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  233. }
  234. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  235. $aBindings['profile_id'] = $mixedProfileId;
  236. $sOnProfile = " AND `g`.`profile_id` = :profile_id ";
  237. }
  238. $sJoin .= " INNER JOIN `sys_storage_ghosts` AS `g` ON (`f`.`id` = `g`.`id` AND `g`.`object` = :object " . $sOnProfile;
  239. if (false !== $iContentId) {
  240. $aBindings['content_id'] = $iContentId;
  241. $sJoin .= " AND `g`.`content_id` = :content_id";
  242. }
  243. $sJoin .= ')';
  244. }
  245. else {
  246. if (is_array($mixedProfileId) && $mixedProfileId) {
  247. $sWhere = " WHERE `f`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  248. }
  249. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  250. $aBindings['profile_id'] = $mixedProfileId;
  251. $sWhere = " WHERE `f`.`profile_id` = :profile_id ";
  252. }
  253. }
  254. $sQuery = "SELECT `f`.* FROM " . $this->_sTableFiles . " AS `f` " . $sJoin . $sWhere;
  255. return $this->getAll($sQuery, $aBindings);
  256. }
  257. public function getFilesAll($iStart, $iPerPage)
  258. {
  259. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " LIMIT ?, ?", (int)$iStart, (int)$iPerPage);
  260. return $this->getAll($sQuery);
  261. }
  262. public function prune()
  263. {
  264. $iTime = time();
  265. $sQuery = $this->prepare("DELETE FROM `sys_storage_tokens` WHERE `object` = ? AND `created` < ?", $this->_aObject['object'], $iTime - $this->_aObject['token_life']);
  266. $iCount = $this->query($sQuery);
  267. if ($iCount)
  268. $this->query("OPTIMIZE TABLE `sys_storage_tokens`");
  269. return $iCount;
  270. }
  271. public function queueFilesForDeletion ($a)
  272. {
  273. $iTime = time();
  274. $iAdded = 0;
  275. foreach ($a as $iFileId) {
  276. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_deletions` SET `object` = ?, `file_id` = ?, `requested` = ?", $this->_aObject['object'], (int)$iFileId, $iTime);
  277. $iAdded += ($this->query($sQuery) ? 1 : 0);
  278. }
  279. return $iAdded;
  280. }
  281. public static function getQueuedFilesForDeletion ($iLimit = 1000)
  282. {
  283. $oDb = BxDolDb::getInstance();
  284. $sQuery = $oDb->prepare("SELECT `object`, `file_id` FROM `sys_storage_deletions` ORDER BY `requested` ASC LIMIT ?", $iLimit);
  285. return $oDb->getAll($sQuery);
  286. }
  287. public static function isQueuedFilesForDeletion ($sPrefix)
  288. {
  289. $oDb = BxDolDb::getInstance();
  290. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `sys_storage_deletions` WHERE `object` LIKE ?", $sPrefix . '%');
  291. return $oDb->getOne($sQuery);
  292. }
  293. }
  294. /** @} */