BxDolStorageQuery.php 13 KB

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