1
0

BxDolStorageQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. $sAdd = '';
  121. if ($isUpdateModifiedField) {
  122. $iTime = time();
  123. $sAdd = $this->prepare(", `modified` = ?", $iTime);
  124. }
  125. $sQuery = $this->prepare("UPDATE " . $this->_sTableFiles . " SET `{$sField}` = ? {$sAdd} WHERE `id` = ?", $sValue, $iFileId);
  126. return $this->query($sQuery);
  127. }
  128. public function deleteFile($iFileId)
  129. {
  130. // delete queued record for the file
  131. $sQuery = $this->prepare("DELETE FROM `sys_storage_deletions` WHERE `object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  132. $this->query($sQuery);
  133. // delete file record
  134. $sQuery = $this->prepare("DELETE FROM " . $this->_sTableFiles . " WHERE `id` = ?", $iFileId);
  135. if (!$this->query($sQuery))
  136. return false;
  137. // delete any file traces in ghosts table
  138. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  139. $this->query($sQuery);
  140. return true;
  141. }
  142. public function getFileByFileName($sValue)
  143. {
  144. return $this->_getFileBy('`file_name`', $sValue);
  145. }
  146. public function getFileById($sValue)
  147. {
  148. return $this->_getFileBy('`id`', $sValue);
  149. }
  150. public function getFileByRemoteId($sValue)
  151. {
  152. return $this->_getFileBy('`remote_id`', $sValue);
  153. }
  154. protected function _getFileBy($sField, $sValue)
  155. {
  156. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " WHERE " . $sField . " = ?", $sValue);
  157. return $this->getRow($sQuery);
  158. }
  159. public function isTokenValid($iFileId, $sToken)
  160. {
  161. $iTime = time();
  162. $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']);
  163. return $this->getOne($sQuery) ? true : false;
  164. }
  165. public function genToken($iFileId)
  166. {
  167. $iTime = time();
  168. $sToken = md5($iTime . mt_rand() . BX_DOL_SECRET);
  169. $sQuery = $this->prepare("INSERT INTO `sys_storage_tokens` SET `id` = ?, `object` = ?, `hash` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $sToken, $iTime);
  170. if ($this->query($sQuery))
  171. return $sToken;
  172. else
  173. return false;
  174. }
  175. public function insertGhosts($mixedFileIds, $iProfileId, $iContentId = 0)
  176. {
  177. $iTime = time();
  178. if (!is_array($mixedFileIds))
  179. $mixedFileIds = array($mixedFileIds);
  180. $iCount = 0;
  181. foreach ($mixedFileIds as $iFileId) {
  182. $sQuery = $this->prepare("INSERT INTO `sys_storage_ghosts`
  183. SET `id` = ?, `object` = ?, `profile_id` = ?, `content_id` = ?, `created` = ?
  184. ON DUPLICATE KEY UPDATE `profile_id` = ?, `content_id` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $iProfileId, $iContentId, $iTime, $iProfileId, $iContentId, $iTime);
  185. $iCount += $this->query($sQuery);
  186. }
  187. return $iCount;
  188. }
  189. public function updateGhostsContentId($mixedFileIds, $iProfileId, $iContentId)
  190. {
  191. $sQuery = $this->prepare("UPDATE `sys_storage_ghosts` SET `content_id` = ? WHERE `profile_id` = ? AND `object` = ?", $iContentId, $iProfileId, $this->_aObject['object']);
  192. $sQuery .= " AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")";
  193. return $this->res($sQuery);
  194. }
  195. public function deleteGhosts($mixedFileIds, $iProfileId, $iContentId = false)
  196. {
  197. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `profile_id` = ? AND `object` = ? AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")", $iProfileId, $this->_aObject['object']);
  198. if (false !== $iContentId)
  199. $sQuery .= $this->prepare(" AND `content_id` = ?", $iContentId);
  200. $iCount = $this->query($sQuery);
  201. if ($iCount)
  202. $this->query("OPTIMIZE TABLE `sys_storage_ghosts`");
  203. return $iCount;
  204. }
  205. public function getGhosts($iProfileId, $iContentId = false)
  206. {
  207. return $this->getFiles($iProfileId, true, $iContentId);
  208. }
  209. public function getFiles($iProfileId, $isGhostsOnly = false, $iContentId = false)
  210. {
  211. $sJoin = '';
  212. if ($isGhostsOnly) {
  213. $sJoin .= $this->prepare(" INNER JOIN `sys_storage_ghosts` AS `g` ON (`f`.`id` = `g`.`id` AND `g`.`profile_id` = ? AND `g`.`object` = ? ", $iProfileId, $this->_aObject['object']);
  214. if (false !== $iContentId)
  215. $sJoin .= $this->prepare(" AND `g`.`content_id` = ?", $iContentId);
  216. $sJoin .= ')';
  217. }
  218. $sQuery = "SELECT `f`.* FROM " . $this->_sTableFiles . " AS `f` " . $sJoin . (false === $iProfileId ? '' : $this->prepare("WHERE `f`.`profile_id` = ?", $iProfileId));
  219. return $this->getAll($sQuery);
  220. }
  221. public function getFilesAll($iStart, $iPerPage)
  222. {
  223. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " LIMIT ?, ?", (int)$iStart, (int)$iPerPage);
  224. return $this->getAll($sQuery);
  225. }
  226. public function prune()
  227. {
  228. $iTime = time();
  229. $sQuery = $this->prepare("DELETE FROM `sys_storage_tokens` WHERE `object` = ? AND `created` < ?", $this->_aObject['object'], $iTime - $this->_aObject['token_life']);
  230. $iCount = $this->query($sQuery);
  231. if ($iCount)
  232. $this->query("OPTIMIZE TABLE `sys_storage_tokens`");
  233. return $iCount;
  234. }
  235. public function queueFilesForDeletion ($a)
  236. {
  237. $iTime = time();
  238. $iAdded = 0;
  239. foreach ($a as $iFileId) {
  240. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_deletions` SET `object` = ?, `file_id` = ?, `requested` = ?", $this->_aObject['object'], (int)$iFileId, $iTime);
  241. $iAdded += ($this->query($sQuery) ? 1 : 0);
  242. }
  243. return $iAdded;
  244. }
  245. public static function getQueuedFilesForDeletion ($iLimit = 1000)
  246. {
  247. $oDb = BxDolDb::getInstance();
  248. $sQuery = $oDb->prepare("SELECT `object`, `file_id` FROM `sys_storage_deletions` ORDER BY `requested` ASC LIMIT ?", $iLimit);
  249. return $oDb->getAll($sQuery);
  250. }
  251. public static function isQueuedFilesForDeletion ($sPrefix)
  252. {
  253. $oDb = BxDolDb::getInstance();
  254. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `sys_storage_deletions` WHERE `object` LIKE ?", $sPrefix . '%');
  255. return $oDb->getOne($sQuery);
  256. }
  257. }
  258. /** @} */