BxDolStorageQuery.php 11 KB

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