BxDolStorageQuery.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 changeStorageEngine ($sEngine)
  36. {
  37. $sQuery = $this->prepare("UPDATE `sys_objects_storage` SET `engine` = ? WHERE `object` = ?", $sEngine, $this->_aObject['object']);
  38. return $this->query($sQuery);
  39. }
  40. public function getMimeTypeByExt($sExt)
  41. {
  42. $sQuery = $this->prepare("SELECT `mime_type` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  43. return $this->getOne($sQuery);
  44. }
  45. public function getIconByExt($sExt)
  46. {
  47. $sQuery = $this->prepare("SELECT `icon` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  48. return $this->getOne($sQuery);
  49. }
  50. public function getIconFontByExt($sExt)
  51. {
  52. $sQuery = $this->prepare("SELECT `icon_font` FROM `sys_storage_mime_types` WHERE `ext` = ?", $sExt);
  53. return $this->getOne($sQuery);
  54. }
  55. public function clearAllMimeTypes()
  56. {
  57. return $this->query("TRUNCATE TABLE `sys_storage_mime_types`");
  58. }
  59. public function addMimeType($sMimeType, $sExt, $sIcon = '', $sIconFont = '')
  60. {
  61. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_mime_types` SET `ext` = ?, `mime_type` = ?, `icon` = ?, `icon_font` = ?", $sExt, $sMimeType, $sIcon, $sIconFont);
  62. return $this->query($sQuery);
  63. }
  64. public function getStorageObjectQuota()
  65. {
  66. return $this->_aObject;
  67. }
  68. public function updateStorageObjectQuota($iSize, $iNumber = 1)
  69. {
  70. $iTime = time();
  71. $sQuery = $this->prepare("
  72. UPDATE `sys_objects_storage`
  73. SET `current_size` = `current_size` + ?, `current_number` = `current_number` + (?), `ts` = ?
  74. WHERE `object` = ?",
  75. $iSize, $iNumber, $iTime, $this->_aObject['object']
  76. );
  77. if ($this->query($sQuery)) {
  78. $this->_aObject = $this->getStorageObject($this->_aObject['object']);
  79. return true;
  80. } else {
  81. return false;
  82. }
  83. }
  84. public function getUserQuota($iProfileId)
  85. {
  86. if (!$iProfileId) // for guests and storages without owner don't count per-user quota
  87. return array('current_size' => 0, 'current_number' => 0, 'quota_size' => 0, 'quota_number' => 0, 'max_file_size' => 0);
  88. $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);
  89. $a = $this->getRow($sQuery);
  90. if (!is_array($a) || !$a)
  91. $a = array ('current_size' => 0, 'current_number' => 0, 'quota_size' => 0, 'quota_number' => 0, 'max_file_size' => 0);
  92. // get quota_number and quota_size from user's acl/membership
  93. $aMembershipInfo = BxDolAcl::getInstance()->getMemberMembershipInfo($iProfileId);
  94. if ($aMembershipInfo) {
  95. if (isset($aMembershipInfo['quota_size']))
  96. $a['quota_size'] = $aMembershipInfo['quota_size'];
  97. if (isset($aMembershipInfo['quota_number']))
  98. $a['quota_number'] = $aMembershipInfo['quota_number'];
  99. if (isset($aMembershipInfo['quota_max_file_size']))
  100. $a['max_file_size'] = $aMembershipInfo['quota_max_file_size'];
  101. }
  102. return $a;
  103. }
  104. public function updateUserQuota($iProfileId, $iSize, $iNumber = 1)
  105. {
  106. if (!$iProfileId) // for guests and storages without owner don't update per-user quota
  107. return true;
  108. $iTime = time();
  109. $sQuery = $this->prepare("
  110. INSERT INTO `sys_storage_user_quotas`
  111. SET `profile_id` = ?, `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?
  112. ON DUPLICATE KEY UPDATE `current_size` = `current_size` + ?, `current_number` = `current_number` + ?, `ts` = ?",
  113. $iProfileId, $iSize, $iNumber, $iTime, $iSize, $iNumber, $iTime
  114. );
  115. if ($this->query($sQuery))
  116. return true;
  117. else
  118. return false;
  119. }
  120. public function addFile($iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $isPrivate)
  121. {
  122. $sQuery = $this->prepare("INSERT INTO " . $this->_sTableFiles . " SET
  123. `profile_id` = ?, `remote_id` = ?, `path` = ?, `file_name` = ?, `mime_type` = ?, `ext` = ?, `size` = ?, `added` = ?, `modified` = ?, `private` = ?",
  124. $iProfileId, $sLocalId, $sPath, $aFileName, $sMimeType, $sExt, $iSize, $iTime, $iTime, $isPrivate ? 1 : 0
  125. );
  126. return $this->query($sQuery);
  127. }
  128. public function modifyFilePrivate($iFileId, $isPrivate)
  129. {
  130. return $this->modifyCustomField($iFileId, 'private', $isPrivate ? 1 : 0);
  131. }
  132. public function modifyCustomField($iFileId, $sField, $sValue, $isUpdateModifiedField = true)
  133. {
  134. $aBindings = array(
  135. $sField => $sValue,
  136. 'id' => $iFileId
  137. );
  138. $sAdd = '';
  139. if ($isUpdateModifiedField) {
  140. $aBindings['modified'] = time();
  141. $sAdd = ", `modified` = :modified";
  142. }
  143. $sQuery = "UPDATE " . $this->_sTableFiles . " SET `{$sField}` = :{$sField} {$sAdd} WHERE `id` = :id";
  144. return $this->query($sQuery, $aBindings);
  145. }
  146. public function deleteFile($iFileId)
  147. {
  148. // delete queued record for the file
  149. $sQuery = $this->prepare("DELETE FROM `sys_storage_deletions` WHERE `object` = ? AND `file_id` = ?", $this->_aObject['object'], $iFileId);
  150. $this->query($sQuery);
  151. // delete file record
  152. $sQuery = $this->prepare("DELETE FROM " . $this->_sTableFiles . " WHERE `id` = ?", $iFileId);
  153. if (!$this->query($sQuery))
  154. return false;
  155. // delete any file traces in ghosts table
  156. $sQuery = $this->prepare("DELETE FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  157. $this->query($sQuery);
  158. return true;
  159. }
  160. public function getFileByFileName($sValue)
  161. {
  162. return $this->_getFileBy('`file_name`', $sValue);
  163. }
  164. public function getFileById($sValue)
  165. {
  166. return $this->_getFileBy('`id`', $sValue);
  167. }
  168. public function getFileByRemoteId($sValue)
  169. {
  170. return $this->_getFileBy('`remote_id`', $sValue);
  171. }
  172. protected function _getFileBy($sField, $sValue)
  173. {
  174. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " WHERE " . $sField . " = ?", $sValue);
  175. return $this->getRow($sQuery);
  176. }
  177. public function isTokenValid($iFileId, $sToken)
  178. {
  179. $iTime = time();
  180. $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']);
  181. return $this->getOne($sQuery) ? true : false;
  182. }
  183. public function genToken($iFileId)
  184. {
  185. $iTime = time();
  186. $sToken = md5($iTime . mt_rand() . BX_DOL_SECRET);
  187. $sQuery = $this->prepare("INSERT INTO `sys_storage_tokens` SET `id` = ?, `object` = ?, `hash` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $sToken, $iTime);
  188. if ($this->query($sQuery))
  189. return $sToken;
  190. else
  191. return false;
  192. }
  193. public function insertGhosts($mixedFileIds, $iProfileId, $iContentId = 0)
  194. {
  195. $iTime = time();
  196. if (!is_array($mixedFileIds))
  197. $mixedFileIds = array($mixedFileIds);
  198. $iCount = 0;
  199. foreach ($mixedFileIds as $iFileId) {
  200. $sQuery = $this->prepare("INSERT INTO `sys_storage_ghosts`
  201. SET `id` = ?, `object` = ?, `profile_id` = ?, `content_id` = ?, `created` = ?
  202. ON DUPLICATE KEY UPDATE `profile_id` = ?, `content_id` = ?, `created` = ?", $iFileId, $this->_aObject['object'], $iProfileId, $iContentId, $iTime, $iProfileId, $iContentId, $iTime);
  203. $iCount += $this->query($sQuery);
  204. }
  205. return $iCount;
  206. }
  207. public function updateGhostsContentId($mixedFileIds, $iProfileId, $iContentId, $aProfiles = array(), $isAdmin = false)
  208. {
  209. $aBindings = array(
  210. 'content' => $iContentId,
  211. 'profile' => $iProfileId,
  212. 'object' => $this->_aObject['object']
  213. );
  214. $sSetAddon = '';
  215. $sWhere = " AND `profile_id` = :profile ";
  216. if ($aProfiles || $isAdmin) {
  217. $sSetAddon = ', `profile_id` = :profile ';
  218. $sWhere = $isAdmin ? "" : " AND `profile_id` IN(" . $this->implode_escape($aProfiles) . ")";
  219. }
  220. $sQuery = $this->prepare("UPDATE `sys_storage_ghosts` SET `content_id` = :content $sSetAddon WHERE `object` = :object $sWhere AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")");
  221. return $this->res($sQuery, $aBindings);
  222. }
  223. public function deleteGhosts($mixedFileIds, $iProfileId, $iContentId = false)
  224. {
  225. $aBindings = array(
  226. 'profile_id' => $iProfileId,
  227. 'object' => $this->_aObject['object']
  228. );
  229. $sQuery = "DELETE FROM `sys_storage_ghosts` WHERE `profile_id` = :profile_id AND `object` = :object AND `id` IN (" . $this->implode_escape($mixedFileIds) . ")";
  230. if (false !== $iContentId) {
  231. $aBindings['content_id'] = $iContentId;
  232. $sQuery .= " AND `content_id` = :content_id";
  233. }
  234. $iCount = $this->query($sQuery, $aBindings);
  235. if ($iCount)
  236. $this->query("OPTIMIZE TABLE `sys_storage_ghosts`");
  237. return $iCount;
  238. }
  239. public function getGhosts($mixedProfileId, $iContentId = false, $isAdmin = false)
  240. {
  241. return $this->getFiles($mixedProfileId, true, $iContentId, $isAdmin);
  242. }
  243. public function getGhost($iFileId)
  244. {
  245. $sQuery = $this->prepare("SELECT * FROM `sys_storage_ghosts` WHERE `object` = ? AND `id` = ?", $this->_aObject['object'], $iFileId);
  246. return $this->getRow($sQuery);
  247. }
  248. public function getFiles($mixedProfileId, $isGhostsOnly = false, $iContentId = false, $isAdmin = false)
  249. {
  250. $aBindings = array();
  251. $sWhere = '';
  252. $sJoin = '';
  253. if ($isGhostsOnly) {
  254. $aBindings['object'] = $this->_aObject['object'];
  255. $sOnProfile = '';
  256. if ($isAdmin && $iContentId) {
  257. // don't check profile id for admins, so admin can edit any entry
  258. }
  259. elseif (is_array($mixedProfileId) && $mixedProfileId) {
  260. $sOnProfile = " AND `g`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  261. }
  262. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  263. $aBindings['profile_id'] = $mixedProfileId;
  264. $sOnProfile = " AND `g`.`profile_id` = :profile_id ";
  265. }
  266. $sJoin .= " INNER JOIN `sys_storage_ghosts` AS `g` ON (`f`.`id` = `g`.`id` AND `g`.`object` = :object " . $sOnProfile;
  267. if (false !== $iContentId) {
  268. $aBindings['content_id'] = $iContentId;
  269. $sJoin .= " AND `g`.`content_id` = :content_id";
  270. }
  271. $sJoin .= ')';
  272. }
  273. else {
  274. if (is_array($mixedProfileId) && $mixedProfileId) {
  275. $sWhere = " WHERE `f`.`profile_id` IN(" . $this->implode_escape($mixedProfileId) . ") ";
  276. }
  277. elseif (!is_array($mixedProfileId) && false !== $mixedProfileId) {
  278. $aBindings['profile_id'] = $mixedProfileId;
  279. $sWhere = " WHERE `f`.`profile_id` = :profile_id ";
  280. }
  281. }
  282. $sQuery = "SELECT `f`.* FROM " . $this->_sTableFiles . " AS `f` " . $sJoin . $sWhere;
  283. return $this->getAll($sQuery, $aBindings);
  284. }
  285. public function getFilesAll($iStart, $iPerPage)
  286. {
  287. $sQuery = $this->prepare("SELECT * FROM " . $this->_sTableFiles . " LIMIT ?, ?", (int)$iStart, (int)$iPerPage);
  288. return $this->getAll($sQuery);
  289. }
  290. public function prune()
  291. {
  292. $iTime = time();
  293. $sQuery = $this->prepare("DELETE FROM `sys_storage_tokens` WHERE `object` = ? AND `created` < ?", $this->_aObject['object'], $iTime - $this->_aObject['token_life']);
  294. $iCount = $this->query($sQuery);
  295. if ($iCount)
  296. $this->query("OPTIMIZE TABLE `sys_storage_tokens`");
  297. return $iCount;
  298. }
  299. public function queueFilesForDeletion ($a)
  300. {
  301. $iTime = time();
  302. $iAdded = 0;
  303. foreach ($a as $iFileId) {
  304. $sQuery = $this->prepare("INSERT IGNORE INTO `sys_storage_deletions` SET `object` = ?, `file_id` = ?, `requested` = ?", $this->_aObject['object'], (int)$iFileId, $iTime);
  305. $iAdded += ($this->query($sQuery) ? 1 : 0);
  306. }
  307. return $iAdded;
  308. }
  309. public static function getQueuedFilesForDeletion ($iLimit = 1000)
  310. {
  311. $oDb = BxDolDb::getInstance();
  312. $sQuery = $oDb->prepare("SELECT `object`, `file_id` FROM `sys_storage_deletions` ORDER BY `requested` ASC LIMIT ?", $iLimit);
  313. return $oDb->getAll($sQuery);
  314. }
  315. public static function isQueuedFilesForDeletion ($sPrefix)
  316. {
  317. $oDb = BxDolDb::getInstance();
  318. $sQuery = $oDb->prepare("SELECT COUNT(*) FROM `sys_storage_deletions` WHERE `object` LIKE ?", $sPrefix . '%');
  319. return $oDb->getOne($sQuery);
  320. }
  321. }
  322. /** @} */