BxDolAclQuery.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * Database queries for ACL
  11. * @see BxDolAcl
  12. */
  13. class BxDolAclQuery extends BxDolDb implements iBxDolSingleton
  14. {
  15. function __construct()
  16. {
  17. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  18. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  19. parent::__construct();
  20. }
  21. /**
  22. * Prevent cloning the instance
  23. */
  24. public function __clone()
  25. {
  26. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  27. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  28. }
  29. /**
  30. * Get singleton instance of the class
  31. */
  32. static function getInstance()
  33. {
  34. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  35. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolAclQuery();
  36. return $GLOBALS['bxDolClasses'][__CLASS__];
  37. }
  38. function getLevels($aParams, &$aItems, $bReturnCount = true)
  39. {
  40. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  41. $sSelectClause = $sJoinClause = $sWhereClause = $sOrderClause = $sLimitClause = "";
  42. if(!isset($aParams['order']) || empty($aParams['order']))
  43. $sOrderClause = "ORDER BY `tal`.`Order` ASC";
  44. switch($aParams['type']) {
  45. case 'by_id':
  46. $aMethod['name'] = 'getRow';
  47. $aMethod['params'][1] = array(
  48. 'id' => $aParams['value']
  49. );
  50. $sWhereClause .= "AND `tal`.`ID`=:id";
  51. $sLimitClause .= "LIMIT 1";
  52. break;
  53. case 'all_active':
  54. $sWhereClause .= "AND `tal`.`Active`='yes'";
  55. break;
  56. case 'all_active_purchasble_pair':
  57. $aMethod['name'] = "getPairs";
  58. $aMethod['params'][1] = 'id';
  59. $aMethod['params'][2] = 'name';
  60. $sWhereClause .= "AND `tal`.`Active`='yes' AND `tal`.`Purchasable`='yes'";
  61. break;
  62. case 'all_active_pair':
  63. $aMethod['name'] = "getPairs";
  64. $aMethod['params'][1] = 'id';
  65. $aMethod['params'][2] = 'name';
  66. $sWhereClause .= "AND `tal`.`Active`='yes'";
  67. break;
  68. case 'all_pair':
  69. $aMethod['name'] = "getPairs";
  70. $aMethod['params'][1] = 'id';
  71. $aMethod['params'][2] = 'name';
  72. break;
  73. case 'all_order_id':
  74. $sOrderClause = "ORDER BY `tal`.`ID` ASC";
  75. break;
  76. case 'all':
  77. break;
  78. }
  79. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  80. `tal`.`ID` AS `id`,
  81. `tal`.`Name` AS `name`,
  82. `tal`.`Icon` AS `icon`,
  83. `tal`.`Description` AS `description`,
  84. `tal`.`Active` AS `active`,
  85. `tal`.`Purchasable` AS `purchasable`,
  86. `tal`.`Removable` AS `removable`,
  87. `tal`.`QuotaSize` AS `quota_size`,
  88. `tal`.`QuotaNumber` AS `quota_number`,
  89. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  90. `tal`.`Order` AS `order`" . $sSelectClause . "
  91. FROM `sys_acl_levels` AS `tal` " . $sJoinClause . "
  92. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  93. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  94. if(!$bReturnCount)
  95. return !empty($aItems);
  96. return (int)$this->getOne("SELECT FOUND_ROWS()");
  97. }
  98. function getActions($aParams, &$aItems, $bReturnCount = true)
  99. {
  100. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  101. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  102. if(!isset($aParams['order']) || empty($aParams['order']))
  103. $sOrderClause = "ORDER BY `taa`.`Title` ASC";
  104. switch($aParams['type']) {
  105. case 'by_names_and_module':
  106. $aMethod['params'][1] = array(
  107. 'module' => $aParams['module']
  108. );
  109. $sWhereClause .= " AND `taa`.`Name` IN(" . $this->implode_escape($aParams['value']) . ") AND `taa`.`Module` = :module ";
  110. break;
  111. case 'by_names':
  112. $sWhereClause .= " AND `taa`.`Name` IN(" . $this->implode_escape($aParams['value']) . ")";
  113. break;
  114. case 'by_level_id':
  115. $aMethod['params'][1] = array(
  116. 'level_id' => $aParams['value'],
  117. 'level_code' => pow(2, ($aParams['value'] - 1))
  118. );
  119. $sSelectClause .= ", `tam`.`AllowedCount` AS `allowed_count`, `tam`.`AllowedPeriodLen` AS `allowed_period_len` ";
  120. $sJoinClause .= "LEFT JOIN `sys_acl_matrix` AS `tam` ON `taa`.`ID`=`tam`.`IDAction` ";
  121. $sWhereClause .= "AND `tam`.`IDLevel`=:level_id AND (`taa`.`DisabledForLevels`='0' OR `taa`.`DisabledForLevels`&:level_code=0)";
  122. break;
  123. case 'by_level_id_key_id':
  124. $aMethod['name'] = 'getAllWithKey';
  125. $aMethod['params'][1] = 'id';
  126. $aMethod['params'][2] = array(
  127. 'level_id' => $aParams['value']
  128. );
  129. $sSelectClause .= ", `tam`.`AllowedCount` AS `allowed_count`, `tam`.`AllowedPeriodLen` AS `allowed_period_len` ";
  130. $sJoinClause .= "LEFT JOIN `sys_acl_matrix` AS `tam` ON `taa`.`ID`=`tam`.`IDAction` ";
  131. $sWhereClause .= "AND `tam`.`IDLevel`=:level_id";
  132. break;
  133. case 'counter_by_modules':
  134. $aMethod['name'] = 'getPairs';
  135. $aMethod['params'][1] = 'module';
  136. $aMethod['params'][2] = 'counter';
  137. $sSelectClause = ", COUNT(*) AS `counter`";
  138. $sGroupClause = "GROUP BY `taa`.`Module`";
  139. break;
  140. case 'counter_by_levels':
  141. $aMethod['name'] = 'getPairs';
  142. $aMethod['params'][1] = 'level_id';
  143. $aMethod['params'][2] = 'counter';
  144. $sSelectClause = ", `tam`.`IDLevel` AS `level_id`, COUNT(`tam`.`IDAction`) AS `counter`";
  145. $sJoinClause = "LEFT JOIN `sys_acl_matrix` AS `tam` ON `taa`.`ID`=`tam`.`IDAction` AND (`taa`.`DisabledForLevels`='0' OR `taa`.`DisabledForLevels`&POW(2, `tam`.`IDLevel`-1)=0) ";
  146. $sGroupClause = "GROUP BY `tam`.`IDLevel`";
  147. break;
  148. }
  149. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  150. `taa`.`ID` AS `id`,
  151. `taa`.`Module` AS `module`,
  152. `taa`.`Name` AS `name`,
  153. `taa`.`Title` AS `title`,
  154. `taa`.`Countable` AS `countable`,
  155. `taa`.`DisabledForLevels` AS `disabled_for_levels`" . $sSelectClause . "
  156. FROM `sys_acl_actions` AS `taa` " . $sJoinClause . "
  157. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  158. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  159. if(!$bReturnCount)
  160. return !empty($aItems);
  161. return (int)$this->getOne("SELECT FOUND_ROWS()");
  162. }
  163. /**
  164. * Fetch the last purchased/assigned membership that is still active for the given profile.
  165. *
  166. * NOTE. Don't use cache here, because it's causing an error, if a number of memberrship levels are purchased at the same time.
  167. * fromMemory returns the same DateExpires because setMembership (old buyMembership) function is called in cycle in the same session.
  168. */
  169. function getLevelCurrent($iProfileId, $iTime = 0)
  170. {
  171. $iTime = $iTime == 0 ? time() : (int)$iTime;
  172. $sSql = $this->prepare("
  173. SELECT `sys_acl_levels_members`.`IDLevel` as `id`,
  174. `sys_acl_levels`.`Name` AS `name`,
  175. `sys_acl_levels`.`QuotaSize` AS `quota_size`,
  176. `sys_acl_levels`.`QuotaNumber` AS `quota_number`,
  177. `sys_acl_levels`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  178. UNIX_TIMESTAMP(`sys_acl_levels_members`.`DateStarts`) as `date_starts`,
  179. UNIX_TIMESTAMP(`sys_acl_levels_members`.`DateExpires`) as `date_expires`,
  180. `sys_acl_levels_members`.`TransactionID` AS `transaction_id`,
  181. `sys_profiles`.`status`
  182. FROM `sys_acl_levels_members`
  183. RIGHT JOIN `sys_profiles` ON `sys_acl_levels_members`.IDMember = `sys_profiles`.`id`
  184. AND (`sys_acl_levels_members`.DateStarts IS NULL OR `sys_acl_levels_members`.DateStarts <= FROM_UNIXTIME(?))
  185. AND (`sys_acl_levels_members`.DateExpires IS NULL OR `sys_acl_levels_members`.DateExpires > FROM_UNIXTIME(?))
  186. LEFT JOIN `sys_acl_levels` ON `sys_acl_levels_members`.IDLevel = `sys_acl_levels`.ID
  187. WHERE `sys_profiles`.`id` = ?
  188. ORDER BY `sys_acl_levels_members`.DateStarts DESC
  189. LIMIT 1", $iTime, $iTime, $iProfileId);
  190. return $this->getRow($sSql);
  191. }
  192. function getLevelByIdCached($iLevel)
  193. {
  194. $sQuery = $this->prepare("SELECT
  195. `tal`.`ID` AS `id`,
  196. `tal`.`Name` AS `name`,
  197. `tal`.`QuotaSize` AS `quota_size`,
  198. `tal`.`QuotaNumber` AS `quota_number`,
  199. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`
  200. FROM `sys_acl_levels` AS `tal`
  201. WHERE `tal`.`ID`=?
  202. LIMIT 1", $iLevel);
  203. return $this->fromCache('sys_acl_levels' . $iLevel, 'getRow', $sQuery);
  204. }
  205. function getAction($iMembershipId, $iActionId)
  206. {
  207. $sQuery = $this->prepare("SELECT
  208. `tam`.`IDAction` AS `id`,
  209. `taa`.`Name` AS `name`,
  210. `taa`.`Title` AS `title`,
  211. `tam`.`AllowedCount` AS `allowed_count`,
  212. `tam`.`AllowedPeriodLen` AS `allowed_period_len`,
  213. UNIX_TIMESTAMP(`tam`.`AllowedPeriodStart`) as `allowed_period_start`,
  214. UNIX_TIMESTAMP(`tam`.`AllowedPeriodEnd`) as `allowed_period_end`,
  215. `tam`.`AdditionalParamValue` AS `additional_param_value`
  216. FROM `sys_acl_actions` AS `taa`
  217. LEFT JOIN `sys_acl_matrix` AS `tam` ON `tam`.`IDAction` = `taa`.`ID` AND `tam`.`IDLevel` = ?
  218. WHERE `taa`.`ID` = ?", $iMembershipId, $iActionId);
  219. return $this->getRow($sQuery);
  220. }
  221. function getActionTrack($iActionId, $iProfileId)
  222. {
  223. $sQuery = $this->prepare("SELECT
  224. `taat`.`ActionsLeft` AS `actions_left`,
  225. UNIX_TIMESTAMP(`taat`.`ValidSince`) as `valid_since`
  226. FROM `sys_acl_actions_track` AS `taat`
  227. WHERE `taat`.`IDAction`=? AND `taat`.`IDMember`=?", $iActionId, $iProfileId);
  228. return $this->getRow($sQuery);
  229. }
  230. function insertActionTarck($iActionId, $iProfileId, $iActionsLeft, $iValidSince)
  231. {
  232. $sQuery = $this->prepare("INSERT INTO `sys_acl_actions_track`(`IDAction`, `IDMember`, `ActionsLeft`, `ValidSince`) VALUES (?, ?, ?, FROM_UNIXTIME(?))", $iActionId, $iProfileId, $iActionsLeft, $iValidSince);
  233. return (int)$this->query($sQuery) > 0;
  234. }
  235. function updateActionTrack($iActionId, $iProfileId, $iActionsLeft, $iValidSince = 0)
  236. {
  237. $aBindings = array(
  238. 'actions_left' => $iActionsLeft,
  239. 'action_id' => $iActionId,
  240. 'member_id' => $iProfileId
  241. );
  242. $sUpdateAddon = "";
  243. if($iValidSince != 0) {
  244. $aBindings['valid_since'] = $iValidSince;
  245. $sUpdateAddon = ", ValidSince=FROM_UNIXTIME(:valid_since)";
  246. }
  247. $sQuery = "UPDATE `sys_acl_actions_track` SET `ActionsLeft`=:actions_left" . $sUpdateAddon . " WHERE `IDAction`=:action_id AND `IDMember`=:member_id";
  248. return (int)$this->query($sQuery, $aBindings) > 0;
  249. }
  250. function insertLevelByProfileId($iProfileId, $iMembershipId, $iDateStarts, $iDateExpires, $sTransactionId)
  251. {
  252. $sQuery = $this->prepare("INSERT `sys_acl_levels_members` (`IDMember`, `IDLevel`, `DateStarts`, `DateExpires`, `TransactionID`) VALUES (?, ?, FROM_UNIXTIME(?), FROM_UNIXTIME(?), ?)", $iProfileId, $iMembershipId, $iDateStarts, $iDateExpires, $sTransactionId);
  253. return (int)$this->query($sQuery) > 0;
  254. }
  255. function deleteLevelByProfileId($iProfileId, $bAll = false)
  256. {
  257. $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `IDMember` = ? " . ($bAll ? "" : " AND (`DateExpires` IS NULL OR `DateExpires` > NOW())"), $iProfileId);
  258. return (int)$this->query($sQuery) > 0;
  259. }
  260. function deleteLevelBy($aWhere)
  261. {
  262. if(empty($aWhere))
  263. return false;
  264. $sQuery = "DELETE FROM `sys_acl_levels_members` WHERE " . $this->arrayToSQL($aWhere, ' AND ');
  265. return (int)$this->query($sQuery) > 0;
  266. }
  267. function maintenance($iDaysToCleanMemLevels)
  268. {
  269. $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `DateExpires` < NOW() - INTERVAL ? DAY", $iDaysToCleanMemLevels);
  270. if ($iDeleteMemLevels = $this->query($sQuery))
  271. $this->query("OPTIMIZE TABLE `sys_acl_levels_members`");
  272. return $iDeleteMemLevels;
  273. }
  274. function clearActionsTracksForMember($iMemberId)
  275. {
  276. $sQuery = $this->prepare("DELETE FROM `sys_acl_actions_track` WHERE `IDMember` = ?", (int)$iMemberId);
  277. return $this->query($sQuery);
  278. }
  279. }
  280. /** @} */