BxDolStorageQuery.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. $sQuery = $this->prepare("DELETE FROM " . $this->_sTableFiles . " WHERE `id` = ?", $iFileId);
  116. if (!$this->query($sQuery))
  117. return false;
  118. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  119. $this->query($sQuery);
  120. return true;
  121. }
  122. public function getFileByFileName($sValue) {
  123. return $this->_getFileBy('`file_name`', $sValue);
  124. }
  125. public function getFileById($sValue) {
  126. return $this->_getFileBy('`id`', $sValue);
  127. }
  128. public function getFileByRemoteId($sValue) {
  129. return $this->_getFileBy('`remote_id`', $sValue);
  130. }
  131. protected function _getFileBy($sField, $sValue) {
  132. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " WHERE " . $sField . " = ?", $sValue);
  133. return $this->getRow($sQuery);
  134. }
  135. public function isTokenValid($iFileId, $sToken) {
  136. $iTime = time();
  137. $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']);
  138. return $this->getOne($sQuery) ? true : false;
  139. }
  140. public function genToken($iFileId) {
  141. $iTime = time();
  142. $sToken = md5($iTime . mt_rand() . BX_DOL_SECRET);
  143. $sQuery = $this->prepare("INSERT INTO `sys_storage_tokens` SET `id` = ?, `object` = ?, `hash` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $sToken, $iTime);
  144. if ($this->query($sQuery))
  145. return $sToken;
  146. else
  147. return false;
  148. }
  149. public function insertGhosts($mixedFileIds, $iProfileId, $iContentId = 0) {
  150. $iTime = time();
  151. if (!is_array($mixedFileIds))
  152. $mixedFileIds = array($mixedFileIds);
  153. $iCount = 0;
  154. foreach ($mixedFileIds as $iFileId) {
  155. $sQuery = $this->prepare("INSERT INTO `sys_storage_ghosts`
  156. SET `id` = ?, `object` = ?, `profile_id` = ?, `content_id` = ?, `created` = ?
  157. ON DUPLICATE KEY UPDATE `profile_id` = ?, `content_id` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $iProfileId, $iContentId, $iTime, $iProfileId, $iContentId, $iTime);
  158. $iCount += $this->query($sQuery);
  159. }
  160. return $iCount;
  161. }
  162. public function updateGhostsContentId($mixedFileIds, $iProfileId, $iContentId) {
  163. $sQuery = $this->prepare("UPDATE `sys_storage_ghosts` SET `content_id` = ? WHERE `profile_id` = ? AND `object` = ?", $iContentId, $iProfileId, $this->_aObject['object']);
  164. $sQuery .= " AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")";
  165. return $this->res($sQuery);
  166. }
  167. public function deleteGhosts($mixedFileIds, $iProfileId, $iContentId = false) {
  168. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `profile_id` = ? AND `object` = ? AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")", $iProfileId, $this->_aObject['object']);
  169. if (false !== $iContentId)
  170. $sQuery .= $this->prepare(" AND `content_id` = ?", $iContentId);
  171. $iCount = $this->query($sQuery);
  172. if ($iCount)
  173. $this->query("OPTIMIZE TABLE `sys_storage_ghosts`");
  174. return $iCount;
  175. }
  176. public function getGhosts($iProfileId, $iContentId = false) {
  177. return $this->getFiles($iProfileId, true, $iContentId);
  178. }
  179. public function getFiles($iProfileId, $isGhostsOnly = false, $iContentId = false) {
  180. $sJoin = '';
  181. if ($isGhostsOnly) {
  182. $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']);
  183. if (false !== $iContentId)
  184. $sJoin .= $this->prepare(" AND `g`.`content_id` = ?", $iContentId);
  185. $sJoin .= ')';
  186. }
  187. $sQuery = "SELECT `f`.* FROM " . $this->_sTableFiles . " AS `f` " . $sJoin . $this->prepare("WHERE `f`.`profile_id` = ?", $iProfileId);
  188. return $this->getAll($sQuery);
  189. }
  190. public function prune() {
  191. $iTime = time();
  192. $sQuery = $this->prepare("DELETE FROM `sys_storage_tokens` WHERE `object` = ? AND `created` < ?", $this->_aObject['object'], $iTime - $this->_aObject['token_life']);
  193. $iCount = $this->query($sQuery);
  194. if ($iCount)
  195. $this->query("OPTIMIZE TABLE `sys_storage_tokens`");
  196. return $iCount;
  197. }
  198. }