BxDolStorageQuery.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolDb');
  10. /**
  11. * Database queries for File storage class.
  12. * @see BxDolStorage
  13. */
  14. class BxDolStorageQuery extends BxDolDb {
  15. protected $_aObject;
  16. protected $_sTableFiles;
  17. public function __construct($aObject) {
  18. parent::__construct();
  19. $this->_aObject = $aObject;
  20. $this->_sTableFiles = '`' . $aObject['table_files'] . '`';
  21. }
  22. static public function getStorageObject ($sObject) {
  23. $oDb = BxDolDb::getInstance();
  24. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_storage` WHERE `object` = ?", $sObject);
  25. return $oDb->getRow($sQuery);
  26. }
  27. static public function getStorageObjects () {
  28. $oDb = BxDolDb::getInstance();
  29. $sQuery = "SELECT * FROM `sys_objects_storage`";
  30. return $oDb->getAll($sQuery);
  31. }
  32. public function getMimeTypeByExt($sExt) {
  33. $sQuery = $this->prepare("SELECT `mime_type` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  34. return $this->getOne($sQuery);
  35. }
  36. public function getIconByExt($sExt) {
  37. $sQuery = $this->prepare("SELECT `icon` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  38. return $this->getOne($sQuery);
  39. }
  40. public function clearAllMimeTypes() {
  41. return $this->query("TRUNCATE TABLE `sys_storage_mime_types`");
  42. }
  43. public function addMimeType($sMimeType, $sExt, $sIcon = '') {
  44. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_mime_types` SET `ext` = ?, `mime_type` = ?, `icon` = ?", $sExt, $sMimeType, $sIcon);
  45. return $this->query($sQuery);
  46. }
  47. public function getStorageObjectQuota() {
  48. return $this->_aObject;
  49. }
  50. public function updateStorageObjectQuota($iSize, $iNumber = 1) {
  51. $iTime = time();
  52. $sQuery = $this->prepare("
  53. UPDATE `sys_objects_storage`
  54. SET `current_size` = `current_size` + ?, `current_number` = `current_number` + (?), `ts` = ?
  55. WHERE `object` = ?",
  56. $iSize, $iNumber, $iTime, $this->_aObject['object']
  57. );
  58. if ($this->query($sQuery)) {
  59. $this->_aObject = $this->getStorageObject($this->_aObject['object']);
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. public function getUserQuota($iProfileId) {
  66. $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);
  67. $a = $this->getRow($sQuery);
  68. if (!is_array($a) || !$a)
  69. $a = array ('current_size' => 0, 'current_number' => 0, 'quota_size' => 0, 'quota_number' => 0, 'max_file_size' => 0);
  70. // get quota_number and quota_size from user's acl/membership
  71. bx_import('BxDolAcl');
  72. $aMembershipInfo = BxDolAcl::getInstance()->getMemberMembershipInfo($iProfileId);
  73. if ($aMembershipInfo) {
  74. if (isset($aMembershipInfo['quota_size']))
  75. $a['quota_size'] = $aMembershipInfo['quota_size'];
  76. if (isset($aMembershipInfo['quota_number']))
  77. $a['quota_number'] = $aMembershipInfo['quota_number'];
  78. if (isset($aMembershipInfo['quota_max_file_size']))
  79. $a['max_file_size'] = $aMembershipInfo['quota_max_file_size'];
  80. }
  81. return $a;
  82. }
  83. public function updateUserQuota($iProfileId, $iSize, $iNumber = 1) {
  84. $iTime = time();
  85. $sQuery = $this->prepare("
  86. INSERT INTO `sys_storage_user_quotas`
  87. SET `profile_id` = ?, `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?
  88. ON DUPLICATE KEY UPDATE `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?",
  89. $iProfileId, $iSize, $iNumber, $iTime, $iSize, $iNumber, $iTime
  90. );
  91. if ($this->query($sQuery))
  92. return true;
  93. else
  94. return false;
  95. }
  96. public function addFile($iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $isPrivate) {
  97. $sQuery = $this->prepare("INSERT INTO " . $this->_sTableFiles . " SET
  98. `profile_id` = ?, `remote_id` = ?, `path` = ?, `file_name` = ?, `mime_type` = ?, `ext` = ?, `size` = ?, `added` = ?, `modified` = ?, `private` = ?",
  99. $iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $iTime, $isPrivate ? 1 : 0
  100. );
  101. return $this->query($sQuery);
  102. }
  103. public function modifyFilePrivate($iFileId, $isPrivate) {
  104. return $this->modifyCustomField($iFileId, 'private', $isPrivate ? 1 : 0);
  105. }
  106. public function modifyCustomField($iFileId, $sField, $sValue, $isUpdateModifiedField = true) {
  107. $sAdd = '';
  108. if ($isUpdateModifiedField) {
  109. $iTime = time();
  110. $sAdd = $this->prepare(", `modified` = ?", $iTime);
  111. }
  112. $sQuery = $this->prepare("UPDATE " . $this->_sTableFiles . " SET `{$sField}` = ? {$sAdd} WHERE `id` = ?", $sValue, $iFileId);
  113. return $this->query($sQuery);
  114. }
  115. public function deleteFile($iFileId) {
  116. // delete queued record for the file
  117. $sQuery = $this->prepare("DELETE FROM `sys_storage_deletions` WHERE `object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  118. $this->query($sQuery);
  119. // delete file record
  120. $sQuery = $this->prepare("DELETE FROM " . $this->_sTableFiles . " WHERE `id` = ?", $iFileId);
  121. if (!$this->query($sQuery))
  122. return false;
  123. // delete any file traces in ghosts table
  124. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  125. $this->query($sQuery);
  126. return true;
  127. }
  128. public function getFileByFileName($sValue) {
  129. return $this->_getFileBy('`file_name`', $sValue);
  130. }
  131. public function getFileById($sValue) {
  132. return $this->_getFileBy('`id`', $sValue);
  133. }
  134. public function getFileByRemoteId($sValue) {
  135. return $this->_getFileBy('`remote_id`', $sValue);
  136. }
  137. protected function _getFileBy($sField, $sValue) {
  138. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " WHERE " . $sField . " = ?", $sValue);
  139. return $this->getRow($sQuery);
  140. }
  141. public function isTokenValid($iFileId, $sToken) {
  142. $iTime = time();
  143. $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']);
  144. return $this->getOne($sQuery) ? true : false;
  145. }
  146. public function genToken($iFileId) {
  147. $iTime = time();
  148. $sToken = md5($iTime . mt_rand() . BX_DOL_SECRET);
  149. $sQuery = $this->prepare("INSERT INTO `sys_storage_tokens` SET `id` = ?, `object` = ?, `hash` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $sToken, $iTime);
  150. if ($this->query($sQuery))
  151. return $sToken;
  152. else
  153. return false;
  154. }
  155. public function insertGhosts($mixedFileIds, $iProfileId, $iContentId = 0) {
  156. $iTime = time();
  157. if (!is_array($mixedFileIds))
  158. $mixedFileIds = array($mixedFileIds);
  159. $iCount = 0;
  160. foreach ($mixedFileIds as $iFileId) {
  161. $sQuery = $this->prepare("INSERT INTO `sys_storage_ghosts`
  162. SET `id` = ?, `object` = ?, `profile_id` = ?, `content_id` = ?, `created` = ?
  163. ON DUPLICATE KEY UPDATE `profile_id` = ?, `content_id` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $iProfileId, $iContentId, $iTime, $iProfileId, $iContentId, $iTime);
  164. $iCount += $this->query($sQuery);
  165. }
  166. return $iCount;
  167. }
  168. public function updateGhostsContentId($mixedFileIds, $iProfileId, $iContentId) {
  169. $sQuery = $this->prepare("UPDATE `sys_storage_ghosts` SET `content_id` = ? WHERE `profile_id` = ? AND `object` = ?", $iContentId, $iProfileId, $this->_aObject['object']);
  170. $sQuery .= " AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")";
  171. return $this->res($sQuery);
  172. }
  173. public function deleteGhosts($mixedFileIds, $iProfileId, $iContentId = false) {
  174. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `profile_id` = ? AND `object` = ? AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")", $iProfileId, $this->_aObject['object']);
  175. if (false !== $iContentId)
  176. $sQuery .= $this->prepare(" AND `content_id` = ?", $iContentId);
  177. $iCount = $this->query($sQuery);
  178. if ($iCount)
  179. $this->query("OPTIMIZE TABLE `sys_storage_ghosts`");
  180. return $iCount;
  181. }
  182. public function getGhosts($iProfileId, $iContentId = false) {
  183. return $this->getFiles($iProfileId, true, $iContentId);
  184. }
  185. public function getFiles($iProfileId, $isGhostsOnly = false, $iContentId = false) {
  186. $sJoin = '';
  187. if ($isGhostsOnly) {
  188. $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']);
  189. if (false !== $iContentId)
  190. $sJoin .= $this->prepare(" AND `g`.`content_id` = ?", $iContentId);
  191. $sJoin .= ')';
  192. }
  193. $sQuery = "SELECT `f`.* FROM " . $this->_sTableFiles . " AS `f` " . $sJoin . (false === $iProfileId ? '' : $this->prepare("WHERE `f`.`profile_id` = ?", $iProfileId));
  194. return $this->getAll($sQuery);
  195. }
  196. public function prune() {
  197. $iTime = time();
  198. $sQuery = $this->prepare("DELETE FROM `sys_storage_tokens` WHERE `object` = ? AND `created` < ?", $this->_aObject['object'], $iTime - $this->_aObject['token_life']);
  199. $iCount = $this->query($sQuery);
  200. if ($iCount)
  201. $this->query("OPTIMIZE TABLE `sys_storage_tokens`");
  202. return $iCount;
  203. }
  204. public function queueFilesForDeletion ($a) {
  205. $iTime = time();
  206. $iAdded = 0;
  207. foreach ($a as $iFileId) {
  208. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_deletions` SET `object` = ?, `file_id` = ?, `requested` = ?", $this->_aObject['object'], (int)$iFileId, $iTime);
  209. $iAdded += ($this->query($sQuery) ? 1 : 0);
  210. }
  211. return $iAdded;
  212. }
  213. public static function getQueuedFilesForDeletion ($iLimit = 1000) {
  214. $oDb = BxDolDb::getInstance();
  215. $sQuery = $oDb->prepare("SELECT `object`, `file_id` FROM `sys_storage_deletions` ORDER BY `requested` ASC LIMIT ?", $iLimit);
  216. return $oDb->getAll($sQuery);
  217. }
  218. public static function isQueuedFilesForDeletion ($sPrefix) {
  219. $oDb = BxDolDb::getInstance();
  220. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `sys_storage_deletions` WHERE `object` LIKE ?", $sPrefix . '%');
  221. return $oDb->getOne($sQuery);
  222. }
  223. }
  224. /** @} */