BxDolStorageQuery.php 11 KB

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