BxDolStorageQuery.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. $aTmp = $oDb->getCache('sys_objects_storage_' . $sObject, 'getRow', $sQuery);
  28. if ($aTmp)
  29. return $aTmp;
  30. $aRow = $oDb->getRow($sQuery);
  31. if (isset($aRow['params'])){
  32. $mParams = unserialize($aRow['params']);
  33. if ($oDb->isFieldExists($aRow['table_files'], 'dimensions')){
  34. $aDim = ['fields' => ['dimensions' => 'getFileDimensions']];
  35. if (is_array($mParams)){
  36. $mParams = array_merge($mParams, $aDim);
  37. }
  38. else{
  39. $mParams = $aDim;
  40. }
  41. }
  42. $aRow['params'] = serialize($mParams);
  43. }
  44. $oDb->setCache('sys_objects_storage_' . $sObject, $aRow);
  45. return $aRow;
  46. }
  47. static public function getStorageObjects ()
  48. {
  49. $oDb = BxDolDb::getInstance();
  50. $sQuery = "SELECT * FROM `sys_objects_storage`";
  51. return $oDb->getAll($sQuery);
  52. }
  53. static public function getAllGhosts ($aParams)
  54. {
  55. $oDb = BxDolDb::getInstance();
  56. $sQuery = "SELECT * FROM `sys_storage_ghosts` WHERE 2>1 ";
  57. if (isset($aParams['profile_id']))
  58. $sQuery .= " AND profile_id = :profile_id";
  59. if (isset($aParams['object']))
  60. $sQuery .= " AND object = :object";
  61. if (isset($aParams['content_id']))
  62. $sQuery .= " AND content_id = :content_id";
  63. if (isset($aParams['sort_field'])){
  64. $sQuery .= " ORDER BY `" . $aParams['sort_field'] . "` " . (isset($aParams['sort_direction']) ? $aParams['sort_direction'] : '');
  65. unset($aParams['sort_field']);
  66. unset($aParams['sort_direction']);
  67. }
  68. return $oDb->getAll($sQuery, $aParams);
  69. }
  70. public function changeStorageEngine ($sEngine)
  71. {
  72. $sQuery = $this->prepare("UPDATE `sys_objects_storage` SET `engine` = ? WHERE `object` = ?", $sEngine, $this->_aObject['object']);
  73. return $this->query($sQuery);
  74. }
  75. public function getMimeTypeByExt($sExt)
  76. {
  77. $sQuery = $this->prepare("SELECT `mime_type` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  78. return $this->getOne($sQuery);
  79. }
  80. public function getIconByExt($sExt)
  81. {
  82. $sQuery = $this->prepare("SELECT `icon` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  83. return $this->getOne($sQuery);
  84. }
  85. public function getIconFontByExt($sExt)
  86. {
  87. $sQuery = $this->prepare("SELECT `icon_font` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  88. return $this->getOne($sQuery);
  89. }
  90. public function clearAllMimeTypes()
  91. {
  92. return $this->query("TRUNCATE TABLE `sys_storage_mime_types`");
  93. }
  94. public function addMimeType($sMimeType, $sExt, $sIcon = '', $sIconFont = '')
  95. {
  96. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_mime_types` SET `ext` = ?, `mime_type` = ?, `icon` = ?, `icon_font` = ?", $sExt, $sMimeType, $sIcon, $sIconFont);
  97. return $this->query($sQuery);
  98. }
  99. public function getStorageObjectQuota()
  100. {
  101. return $this->_aObject;
  102. }
  103. public function updateStorageObjectQuota($iSize, $iNumber = 1)
  104. {
  105. $iTime = time();
  106. $sQuery = $this->prepare("
  107. UPDATE `sys_objects_storage`
  108. SET `current_size` = `current_size` + ?, `current_number` = `current_number` + (?), `ts` = ?
  109. WHERE `object` = ?",
  110. $iSize, $iNumber, $iTime, $this->_aObject['object']
  111. );
  112. if ($this->query($sQuery)) {
  113. $this->_aObject = $this->getStorageObject($this->_aObject['object']);
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. public function getUserQuota($iProfileId)
  120. {
  121. return BxDolProfileQuery::getInstance()->getProfileQuota($iProfileId);
  122. }
  123. public function updateUserQuota($iProfileId, $iSize, $iNumber = 1)
  124. {
  125. return BxDolProfileQuery::getInstance()->updateProfileQuota($iProfileId, $iSize, $iNumber);
  126. }
  127. public function addFile($iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $isPrivate, $aAdditionalFields = array())
  128. {
  129. $aBind = array('profile_id' => $iProfileId, 'local_id' => $sLocalId, 'path' => $sPath, 'file_name' => $aFileName, 'mime_type' => $sMimeType, 'ext' => $sExt, 'size' => $iSize, 'time' => $iTime, 'private' => $isPrivate ? 1 : 0);
  130. $sQuery = "INSERT INTO " . $this->_sTableFiles . " SET
  131. `profile_id` = :profile_id, `remote_id` = :local_id, `path` = :path, `file_name` = :file_name, `mime_type` = :mime_type, `ext` = :ext, `size` = :size, `added` = :time, `modified` = :time, `private` = :private";
  132. if ($aAdditionalFields && is_array($aAdditionalFields)) {
  133. foreach ($aAdditionalFields as $sField => $sValue) {
  134. $aBind[$sField] = $sValue;
  135. $sQuery .= ", `$sField` = :$sField";
  136. }
  137. }
  138. return $this->query($sQuery, $aBind);
  139. }
  140. public function modifyFilePrivate($iFileId, $isPrivate)
  141. {
  142. return $this->modifyCustomField($iFileId, 'private', $isPrivate ? 1 : 0);
  143. }
  144. public function modifyCustomField($iFileId, $sField, $sValue, $isUpdateModifiedField = true)
  145. {
  146. $aBindings = array(
  147. $sField => $sValue,
  148. 'id' => $iFileId
  149. );
  150. $sAdd = '';
  151. if ($isUpdateModifiedField) {
  152. $aBindings['modified'] = time();
  153. $sAdd = ", `modified` = :modified";
  154. }
  155. $sQuery = "UPDATE " . $this->_sTableFiles . " SET `{$sField}` = :{$sField} {$sAdd} WHERE `id` = :id";
  156. return $this->query($sQuery, $aBindings);
  157. }
  158. public function deleteFile($iFileId)
  159. {
  160. // delete queued record for the file
  161. $sQuery = $this->prepare("DELETE FROM `sys_storage_deletions` WHERE `object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  162. $this->query($sQuery);
  163. // delete file record
  164. $sQuery = $this->prepare("DELETE FROM " . $this->_sTableFiles . " WHERE `id` = ?", $iFileId);
  165. if (!$this->query($sQuery))
  166. return false;
  167. // delete any file traces in ghosts table
  168. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  169. $this->query($sQuery);
  170. return true;
  171. }
  172. public function getFileByFileName($sValue)
  173. {
  174. return $this->_getFileBy('`file_name`', $sValue);
  175. }
  176. public function getFileById($sValue)
  177. {
  178. return $this->_getFileBy('`id`', $sValue);
  179. }
  180. public function getFileByRemoteId($sValue)
  181. {
  182. return $this->_getFileBy('`remote_id`', $sValue);
  183. }
  184. protected function _getFileBy($sField, $sValue)
  185. {
  186. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " WHERE " . $sField . " = ?", $sValue);
  187. return $this->getRow($sQuery);
  188. }
  189. public function isTokenValid($iFileId, $sToken)
  190. {
  191. $iTime = time();
  192. $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']);
  193. return $this->getOne($sQuery) ? true : false;
  194. }
  195. public function genToken($iFileId)
  196. {
  197. $iTime = time();
  198. $sToken = md5($iTime . mt_rand() . BX_DOL_SECRET);
  199. $sQuery = $this->prepare("INSERT INTO `sys_storage_tokens` SET `id` = ?, `object` = ?, `hash` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $sToken, $iTime);
  200. if ($this->query($sQuery))
  201. return $sToken;
  202. else
  203. return false;
  204. }
  205. public function insertGhosts($mixedFileIds, $iProfileId, $iContentId = 0)
  206. {
  207. $iTime = time();
  208. if (!is_array($mixedFileIds))
  209. $mixedFileIds = array($mixedFileIds);
  210. $iCount = 0;
  211. foreach ($mixedFileIds as $iFileId) {
  212. $sQuery = $this->prepare("INSERT INTO `sys_storage_ghosts`
  213. SET `id` = ?, `object` = ?, `profile_id` = ?, `content_id` = ?, `created` = ?
  214. ON DUPLICATE KEY UPDATE `profile_id` = ?, `content_id` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $iProfileId, $iContentId, $iTime, $iProfileId, $iContentId, $iTime);
  215. $iCount += $this->query($sQuery);
  216. }
  217. return $iCount;
  218. }
  219. public function updateGhostOrder($iProfileId, $iContentId, $iFileId, $iOrder)
  220. {
  221. $aBindings = array(
  222. 'object' => $this->_aObject['object'],
  223. 'content_id' => $iContentId,
  224. 'id' => $iFileId,
  225. 'order' => $iOrder,
  226. );
  227. $sWhereClause = " AND `object`=:object AND `content_id`=:content_id AND `id`=:id";
  228. if(!empty($iProfileId)) {
  229. $aBindings['profile_id'] = $iProfileId;
  230. $sWhereClause .= " AND `profile_id`=:profile_id";
  231. }
  232. return $this->query("UPDATE `sys_storage_ghosts` SET `order`=:order WHERE 1" . $sWhereClause, $aBindings) !== false;
  233. }
  234. public function updateGhostsContentId($mixedFileIds, $iProfileId, $iContentId, $aProfiles = array(), $isAdmin = false)
  235. {
  236. $aBindings = array(
  237. 'content' => $iContentId,
  238. 'profile' => $iProfileId,
  239. 'object' => $this->_aObject['object']
  240. );
  241. $sSetAddon = '';
  242. $sWhere = " AND `profile_id` = :profile ";
  243. if ($aProfiles || $isAdmin) {
  244. $sSetAddon = ', `profile_id` = :profile ';
  245. $sWhere = $isAdmin ? "" : " AND `profile_id` IN(" . $this->implode_escape($aProfiles) . ")";
  246. }
  247. $sQuery = $this->prepare("UPDATE `sys_storage_ghosts` SET `content_id` = :content $sSetAddon WHERE `object` = :object $sWhere AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")");
  248. return $this->res($sQuery, $aBindings);
  249. }
  250. public function updateGhostsUploaderId($mixedFileIds, $iUploaderId)
  251. {
  252. return $this->query("UPDATE `sys_storage_ghosts` SET `uploader_id` = :uploader_id WHERE `object` = :object AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")", [
  253. 'object' => $this->_aObject['object'],
  254. 'uploader_id' => $iUploaderId
  255. ]);
  256. }
  257. public function deleteGhosts($mixedFileIds, $iProfileId, $iContentId = false)
  258. {
  259. $aBindings = array(
  260. 'profile_id' => $iProfileId,
  261. 'object' => $this->_aObject['object']
  262. );
  263. $sQuery = "DELETE FROM `sys_storage_ghosts` WHERE `profile_id` = :profile_id AND `object` = :object AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")";
  264. if (false !== $iContentId) {
  265. $aBindings['content_id'] = $iContentId;
  266. $sQuery .= " AND `content_id` = :content_id";
  267. }
  268. $iCount = $this->query($sQuery, $aBindings);
  269. if ($iCount)
  270. $this->query("OPTIMIZE TABLE `sys_storage_ghosts`");
  271. return $iCount;
  272. }
  273. public function getGhosts($mixedProfileId, $mixedContent = false, $isAdmin = false)
  274. {
  275. return $this->getFiles($mixedProfileId, true, $mixedContent, $isAdmin);
  276. }
  277. public function getGhost($iFileId)
  278. {
  279. $sQuery = $this->prepare("SELECT * FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  280. return $this->getRow($sQuery);
  281. }
  282. public function getFiles($mixedProfileId, $isGhostsOnly = false, $mixedContent = false, $isAdmin = false)
  283. {
  284. $aBindings = [];
  285. $sSelect = '`f`.*';
  286. $sWhere = $sJoin = $sOrder = '';
  287. if ($isGhostsOnly) {
  288. $aBindings['object'] = $this->_aObject['object'];
  289. $sOnProfile = '';
  290. if ($isAdmin && $mixedContent) {
  291. // don't check profile id for admins, so admin can edit any entry
  292. }
  293. elseif (is_array($mixedProfileId) && $mixedProfileId) {
  294. $sOnProfile = " AND `g`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  295. }
  296. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  297. $aBindings['profile_id'] = $mixedProfileId;
  298. $sOnProfile = " AND `g`.`profile_id` = :profile_id ";
  299. }
  300. $sSelect .= ", `g`.`uploader_id`, `g`.`created`";
  301. $sJoin .= " INNER JOIN `sys_storage_ghosts` AS `g` ON (`f`.`id` = `g`.`id` AND `g`.`object` = :object " . $sOnProfile;
  302. if ($mixedContent !== false) {
  303. if(!is_array($mixedContent)) {
  304. $aBindings['content_id'] = $mixedContent;
  305. $sJoin .= " AND `g`.`content_id` = :content_id";
  306. }
  307. else {
  308. list($aBindings['content_id'], $aBindings['uploader_id']) = $mixedContent;
  309. $sJoin .= " AND `g`.`content_id` = :content_id AND `g`.`uploader_id` = :uploader_id";
  310. }
  311. }
  312. $sJoin .= ')';
  313. $sOrder = ' ORDER BY `g`.`order` ASC, `g`.`created` DESC';
  314. }
  315. else {
  316. if (is_array($mixedProfileId) && $mixedProfileId) {
  317. $sWhere = " WHERE `f`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  318. }
  319. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  320. $aBindings['profile_id'] = $mixedProfileId;
  321. $sWhere = " WHERE `f`.`profile_id` = :profile_id ";
  322. }
  323. }
  324. $sQuery = "SELECT " . $sSelect . " FROM " . $this->_sTableFiles . " AS `f` " . $sJoin . $sWhere . $sOrder;
  325. return $this->getAll($sQuery, $aBindings);
  326. }
  327. public function getFilesAll($iStart, $iPerPage)
  328. {
  329. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " LIMIT ?, ?", (int)$iStart, (int)$iPerPage);
  330. return $this->getAll($sQuery);
  331. }
  332. public function prune()
  333. {
  334. $iTime = time();
  335. $sQuery = $this->prepare("DELETE FROM `sys_storage_tokens` WHERE `object` = ? AND `created` < ?", $this->_aObject['object'], $iTime - $this->_aObject['token_life']);
  336. $iCount = $this->query($sQuery);
  337. if ($iCount)
  338. $this->query("OPTIMIZE TABLE `sys_storage_tokens`");
  339. return $iCount;
  340. }
  341. public function queueFilesForDeletion ($a)
  342. {
  343. $iTime = time();
  344. $iAdded = 0;
  345. foreach ($a as $iFileId) {
  346. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_deletions` SET `object` = ?, `file_id` = ?, `requested` = ?", $this->_aObject['object'], (int)$iFileId, $iTime);
  347. $iAdded += ($this->query($sQuery) ? 1 : 0);
  348. }
  349. return $iAdded;
  350. }
  351. public static function getQueuedFilesForDeletion ($iLimit = 1000)
  352. {
  353. $oDb = BxDolDb::getInstance();
  354. $sQuery = $oDb->prepare("SELECT `object`, `file_id` FROM `sys_storage_deletions` ORDER BY `requested` ASC LIMIT ?", $iLimit);
  355. return $oDb->getAll($sQuery);
  356. }
  357. public static function isQueuedFilesForDeletion ($sPrefix)
  358. {
  359. $oDb = BxDolDb::getInstance();
  360. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `sys_storage_deletions` WHERE `object` LIKE ?", $sPrefix . '%');
  361. return $oDb->getOne($sQuery);
  362. }
  363. public static function getOutdatedUnusedGhosts($iLifetime)
  364. {
  365. return BxDolDb::getInstance()->getAll("SELECT * FROM `sys_storage_ghosts` WHERE `content_id` = '0' AND `created` < (UNIX_TIMESTAMP() - :lifetime)", [
  366. 'lifetime' => $iLifetime
  367. ]);
  368. }
  369. }
  370. /** @} */