BxDolStorageQuery.php 14 KB

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