BxDolStorageQuery.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA 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. if (!$iProfileId) // for guests and storages without owner don't count per-user quota
  77. return array('current_size' => 0, 'current_number' => 0, 'quota_size' => 0, 'quota_number' => 0, 'max_file_size' => 0);
  78. $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);
  79. $a = $this->getRow($sQuery);
  80. if (!is_array($a) || !$a)
  81. $a = array ('current_size' => 0, 'current_number' => 0, 'quota_size' => 0, 'quota_number' => 0, 'max_file_size' => 0);
  82. // get quota_number and quota_size from user's acl/membership
  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. if (!$iProfileId) // for guests and storages without owner don't update per-user quota
  97. return true;
  98. $iTime = time();
  99. $sQuery = $this->prepare("
  100. INSERT INTO `sys_storage_user_quotas`
  101. SET `profile_id` = ?, `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?
  102. ON DUPLICATE KEY UPDATE `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?",
  103. $iProfileId, $iSize, $iNumber, $iTime, $iSize, $iNumber, $iTime
  104. );
  105. if ($this->query($sQuery))
  106. return true;
  107. else
  108. return false;
  109. }
  110. public function addFile($iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $isPrivate)
  111. {
  112. $sQuery = $this->prepare("INSERT INTO " . $this->_sTableFiles . " SET
  113. `profile_id` = ?, `remote_id` = ?, `path` = ?, `file_name` = ?, `mime_type` = ?, `ext` = ?, `size` = ?, `added` = ?, `modified` = ?, `private` = ?",
  114. $iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $iTime, $isPrivate ? 1 : 0
  115. );
  116. return $this->query($sQuery);
  117. }
  118. public function modifyFilePrivate($iFileId, $isPrivate)
  119. {
  120. return $this->modifyCustomField($iFileId, 'private', $isPrivate ? 1 : 0);
  121. }
  122. public function modifyCustomField($iFileId, $sField, $sValue, $isUpdateModifiedField = true)
  123. {
  124. $aBindings = array(
  125. $sField => $sValue,
  126. 'id' => $iFileId
  127. );
  128. $sAdd = '';
  129. if ($isUpdateModifiedField) {
  130. $aBindings['modified'] = time();
  131. $sAdd = ", `modified` = :modified";
  132. }
  133. $sQuery = "UPDATE " . $this->_sTableFiles . " SET `{$sField}` = :{$sField} {$sAdd} WHERE `id` = :id";
  134. return $this->query($sQuery, $aBindings);
  135. }
  136. public function deleteFile($iFileId)
  137. {
  138. // delete queued record for the file
  139. $sQuery = $this->prepare("DELETE FROM `sys_storage_deletions` WHERE `object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  140. $this->query($sQuery);
  141. // delete file record
  142. $sQuery = $this->prepare("DELETE FROM " . $this->_sTableFiles . " WHERE `id` = ?", $iFileId);
  143. if (!$this->query($sQuery))
  144. return false;
  145. // delete any file traces in ghosts table
  146. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  147. $this->query($sQuery);
  148. return true;
  149. }
  150. public function getFileByFileName($sValue)
  151. {
  152. return $this->_getFileBy('`file_name`', $sValue);
  153. }
  154. public function getFileById($sValue)
  155. {
  156. return $this->_getFileBy('`id`', $sValue);
  157. }
  158. public function getFileByRemoteId($sValue)
  159. {
  160. return $this->_getFileBy('`remote_id`', $sValue);
  161. }
  162. protected function _getFileBy($sField, $sValue)
  163. {
  164. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " WHERE " . $sField . " = ?", $sValue);
  165. return $this->getRow($sQuery);
  166. }
  167. public function isTokenValid($iFileId, $sToken)
  168. {
  169. $iTime = time();
  170. $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']);
  171. return $this->getOne($sQuery) ? true : false;
  172. }
  173. public function genToken($iFileId)
  174. {
  175. $iTime = time();
  176. $sToken = md5($iTime . mt_rand() . BX_DOL_SECRET);
  177. $sQuery = $this->prepare("INSERT INTO `sys_storage_tokens` SET `id` = ?, `object` = ?, `hash` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $sToken, $iTime);
  178. if ($this->query($sQuery))
  179. return $sToken;
  180. else
  181. return false;
  182. }
  183. public function insertGhosts($mixedFileIds, $iProfileId, $iContentId = 0)
  184. {
  185. $iTime = time();
  186. if (!is_array($mixedFileIds))
  187. $mixedFileIds = array($mixedFileIds);
  188. $iCount = 0;
  189. foreach ($mixedFileIds as $iFileId) {
  190. $sQuery = $this->prepare("INSERT INTO `sys_storage_ghosts`
  191. SET `id` = ?, `object` = ?, `profile_id` = ?, `content_id` = ?, `created` = ?
  192. ON DUPLICATE KEY UPDATE `profile_id` = ?, `content_id` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $iProfileId, $iContentId, $iTime, $iProfileId, $iContentId, $iTime);
  193. $iCount += $this->query($sQuery);
  194. }
  195. return $iCount;
  196. }
  197. public function updateGhostsContentId($mixedFileIds, $iProfileId, $iContentId, $aProfiles = array())
  198. {
  199. $aBindings = array(
  200. 'content' => $iContentId,
  201. 'profile' => $iProfileId,
  202. 'object' => $this->_aObject['object']
  203. );
  204. $sSetAddon = '';
  205. $sWhere = " AND `profile_id` = :profile ";
  206. if ($aProfiles || isAdmin()) {
  207. $sSetAddon = ', `profile_id` = :profile ';
  208. $sWhere = isAdmin() ? "" : " AND `profile_id` IN(" . $this->implode_escape($aProfiles) . ")";
  209. }
  210. $sQuery = $this->prepare("UPDATE `sys_storage_ghosts` SET `content_id` = :content $sSetAddon WHERE `object` = :object $sWhere AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")");
  211. return $this->res($sQuery, $aBindings);
  212. }
  213. public function deleteGhosts($mixedFileIds, $iProfileId, $iContentId = false)
  214. {
  215. $aBindings = array(
  216. 'profile_id' => $iProfileId,
  217. 'object' => $this->_aObject['object']
  218. );
  219. $sQuery = "DELETE FROM `sys_storage_ghosts` WHERE `profile_id` = :profile_id AND `object` = :object AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")";
  220. if (false !== $iContentId) {
  221. $aBindings['content_id'] = $iContentId;
  222. $sQuery .= " AND `content_id` = :content_id";
  223. }
  224. $iCount = $this->query($sQuery, $aBindings);
  225. if ($iCount)
  226. $this->query("OPTIMIZE TABLE `sys_storage_ghosts`");
  227. return $iCount;
  228. }
  229. public function getGhosts($mixedProfileId, $iContentId = false)
  230. {
  231. return $this->getFiles($mixedProfileId, true, $iContentId);
  232. }
  233. public function getGhost($iFileId)
  234. {
  235. $sQuery = $this->prepare("SELECT * FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  236. return $this->getRow($sQuery);
  237. }
  238. public function getFiles($mixedProfileId, $isGhostsOnly = false, $iContentId = false)
  239. {
  240. $aBindings = array();
  241. $sWhere = '';
  242. $sJoin = '';
  243. if ($isGhostsOnly) {
  244. $aBindings['object'] = $this->_aObject['object'];
  245. $sOnProfile = '';
  246. if (isAdmin() && $iContentId) {
  247. // don't check profile id for admins, so admin can edit any entry
  248. }
  249. elseif (is_array($mixedProfileId) && $mixedProfileId) {
  250. $sOnProfile = " AND `g`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  251. }
  252. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  253. $aBindings['profile_id'] = $mixedProfileId;
  254. $sOnProfile = " AND `g`.`profile_id` = :profile_id ";
  255. }
  256. $sJoin .= " INNER JOIN `sys_storage_ghosts` AS `g` ON (`f`.`id` = `g`.`id` AND `g`.`object` = :object " . $sOnProfile;
  257. if (false !== $iContentId) {
  258. $aBindings['content_id'] = $iContentId;
  259. $sJoin .= " AND `g`.`content_id` = :content_id";
  260. }
  261. $sJoin .= ')';
  262. }
  263. else {
  264. if (is_array($mixedProfileId) && $mixedProfileId) {
  265. $sWhere = " WHERE `f`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  266. }
  267. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  268. $aBindings['profile_id'] = $mixedProfileId;
  269. $sWhere = " WHERE `f`.`profile_id` = :profile_id ";
  270. }
  271. }
  272. $sQuery = "SELECT `f`.* FROM " . $this->_sTableFiles . " AS `f` " . $sJoin . $sWhere;
  273. return $this->getAll($sQuery, $aBindings);
  274. }
  275. public function getFilesAll($iStart, $iPerPage)
  276. {
  277. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " LIMIT ?, ?", (int)$iStart, (int)$iPerPage);
  278. return $this->getAll($sQuery);
  279. }
  280. public function prune()
  281. {
  282. $iTime = time();
  283. $sQuery = $this->prepare("DELETE FROM `sys_storage_tokens` WHERE `object` = ? AND `created` < ?", $this->_aObject['object'], $iTime - $this->_aObject['token_life']);
  284. $iCount = $this->query($sQuery);
  285. if ($iCount)
  286. $this->query("OPTIMIZE TABLE `sys_storage_tokens`");
  287. return $iCount;
  288. }
  289. public function queueFilesForDeletion ($a)
  290. {
  291. $iTime = time();
  292. $iAdded = 0;
  293. foreach ($a as $iFileId) {
  294. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_deletions` SET `object` = ?, `file_id` = ?, `requested` = ?", $this->_aObject['object'], (int)$iFileId, $iTime);
  295. $iAdded += ($this->query($sQuery) ? 1 : 0);
  296. }
  297. return $iAdded;
  298. }
  299. public static function getQueuedFilesForDeletion ($iLimit = 1000)
  300. {
  301. $oDb = BxDolDb::getInstance();
  302. $sQuery = $oDb->prepare("SELECT `object`, `file_id` FROM `sys_storage_deletions` ORDER BY `requested` ASC LIMIT ?", $iLimit);
  303. return $oDb->getAll($sQuery);
  304. }
  305. public static function isQueuedFilesForDeletion ($sPrefix)
  306. {
  307. $oDb = BxDolDb::getInstance();
  308. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `sys_storage_deletions` WHERE `object` LIKE ?", $sPrefix . '%');
  309. return $oDb->getOne($sQuery);
  310. }
  311. }
  312. /** @} */