BxDolStorageQuery.php 15 KB

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