1
0

BxDolStorageQuery.php 13 KB

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