BxDolAclQuery.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. $sWhereClause .= $this->prepare("AND `tal`.`ID`=?", $aParams['value']);
  48. $sLimitClause .= "LIMIT 1";
  49. break;
  50. case 'all_active':
  51. $sWhereClause .= "AND `tal`.`Active`='yes'";
  52. break;
  53. case 'all_active_purchasble_pair':
  54. $aMethod['name'] = "getPairs";
  55. $aMethod['params'][1] = 'id';
  56. $aMethod['params'][2] = 'name';
  57. $sWhereClause .= "AND `tal`.`Active`='yes' AND `tal`.`Purchasable`='yes'";
  58. break;
  59. case 'all_active_pair':
  60. $aMethod['name'] = "getPairs";
  61. $aMethod['params'][1] = 'id';
  62. $aMethod['params'][2] = 'name';
  63. $sWhereClause .= "AND `tal`.`Active`='yes'";
  64. break;
  65. case 'all_pair':
  66. $aMethod['name'] = "getPairs";
  67. $aMethod['params'][1] = 'id';
  68. $aMethod['params'][2] = 'name';
  69. break;
  70. case 'all_order_id':
  71. $sOrderClause = "ORDER BY `tal`.`ID` ASC";
  72. break;
  73. case 'all':
  74. break;
  75. }
  76. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  77. `tal`.`ID` AS `id`,
  78. `tal`.`Name` AS `name`,
  79. `tal`.`Icon` AS `icon`,
  80. `tal`.`Description` AS `description`,
  81. `tal`.`Active` AS `active`,
  82. `tal`.`Purchasable` AS `purchasable`,
  83. `tal`.`Removable` AS `removable`,
  84. `tal`.`QuotaSize` AS `quota_size`,
  85. `tal`.`QuotaNumber` AS `quota_number`,
  86. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  87. `tal`.`Order` AS `order`" . $sSelectClause . "
  88. FROM `sys_acl_levels` AS `tal` " . $sJoinClause . "
  89. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  90. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  91. if(!$bReturnCount)
  92. return !empty($aItems);
  93. return (int)$this->getOne("SELECT FOUND_ROWS()");
  94. }
  95. function getActions($aParams, &$aItems, $bReturnCount = true)
  96. {
  97. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  98. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  99. if(!isset($aParams['order']) || empty($aParams['order']))
  100. $sOrderClause = "ORDER BY `taa`.`Title` ASC";
  101. switch($aParams['type']) {
  102. case 'by_names_and_module':
  103. $sWhereClause .= " AND `taa`.`Name` IN(" . $this->implode_escape($aParams['value']) . ") " . $this->prepare(" AND `taa`.`Module` = ? ", $aParams['module']);
  104. break;
  105. case 'by_names':
  106. $sWhereClause .= " AND `taa`.`Name` IN(" . $this->implode_escape($aParams['value']) . ")";
  107. break;
  108. case 'by_level_id':
  109. $sSelectClause .= ", `tam`.`AllowedCount` AS `allowed_count`, `tam`.`AllowedPeriodLen` AS `allowed_period_len` ";
  110. $sJoinClause .= "LEFT JOIN `sys_acl_matrix` AS `tam` ON `taa`.`ID`=`tam`.`IDAction` ";
  111. $sWhereClause .= $this->prepare("AND `tam`.`IDLevel`=? AND (`taa`.`DisabledForLevels`='0' OR `taa`.`DisabledForLevels`&?=0)", $aParams['value'], pow(2, ($aParams['value'] - 1)));
  112. break;
  113. case 'by_level_id_key_id':
  114. $aMethod['name'] = 'getAllWithKey';
  115. $aMethod['params'][1] = 'id';
  116. $sSelectClause .= ", `tam`.`AllowedCount` AS `allowed_count`, `tam`.`AllowedPeriodLen` AS `allowed_period_len` ";
  117. $sJoinClause .= "LEFT JOIN `sys_acl_matrix` AS `tam` ON `taa`.`ID`=`tam`.`IDAction` ";
  118. $sWhereClause .= $this->prepare("AND `tam`.`IDLevel`=?", $aParams['value']);
  119. break;
  120. case 'counter_by_modules':
  121. $aMethod['name'] = 'getPairs';
  122. $aMethod['params'][1] = 'module';
  123. $aMethod['params'][2] = 'counter';
  124. $sSelectClause = ", COUNT(*) AS `counter`";
  125. $sGroupClause = "GROUP BY `taa`.`Module`";
  126. break;
  127. case 'counter_by_levels':
  128. $aMethod['name'] = 'getPairs';
  129. $aMethod['params'][1] = 'level_id';
  130. $aMethod['params'][2] = 'counter';
  131. $sSelectClause = ", `tam`.`IDLevel` AS `level_id`, COUNT(`tam`.`IDAction`) AS `counter`";
  132. $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) ";
  133. $sGroupClause = "GROUP BY `tam`.`IDLevel`";
  134. break;
  135. }
  136. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  137. `taa`.`ID` AS `id`,
  138. `taa`.`Module` AS `module`,
  139. `taa`.`Name` AS `name`,
  140. `taa`.`Title` AS `title`,
  141. `taa`.`Countable` AS `countable`,
  142. `taa`.`DisabledForLevels` AS `disabled_for_levels`" . $sSelectClause . "
  143. FROM `sys_acl_actions` AS `taa` " . $sJoinClause . "
  144. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  145. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  146. if(!$bReturnCount)
  147. return !empty($aItems);
  148. return (int)$this->getOne("SELECT FOUND_ROWS()");
  149. }
  150. /**
  151. * Fetch the last purchased/assigned membership that is still active for the given profile.
  152. *
  153. * NOTE. Don't use cache here, because it's causing an error, if a number of memberrship levels are purchased at the same time.
  154. *
  155. * fromMemory returns the same DateExpires because buyMembership function is called in cycle in the same session.
  156. */
  157. function getLevelCurrent($iProfileId, $iTime = 0)
  158. {
  159. $iTime = $iTime == 0 ? time() : (int)$iTime;
  160. $sSql = $this->prepare("
  161. SELECT `sys_acl_levels_members`.`IDLevel` as `id`,
  162. `sys_acl_levels`.`Name` AS `name`,
  163. `sys_acl_levels`.`QuotaSize` AS `quota_size`,
  164. `sys_acl_levels`.`QuotaNumber` AS `quota_number`,
  165. `sys_acl_levels`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  166. UNIX_TIMESTAMP(`sys_acl_levels_members`.`DateStarts`) as `date_starts`,
  167. UNIX_TIMESTAMP(`sys_acl_levels_members`.`DateExpires`) as `date_expires`,
  168. `sys_acl_levels_members`.`TransactionID` AS `transaction_id`,
  169. `sys_profiles`.`status`
  170. FROM `sys_acl_levels_members`
  171. RIGHT JOIN `sys_profiles` ON `sys_acl_levels_members`.IDMember = `sys_profiles`.`id`
  172. AND (`sys_acl_levels_members`.DateStarts IS NULL OR `sys_acl_levels_members`.DateStarts <= FROM_UNIXTIME(?))
  173. AND (`sys_acl_levels_members`.DateExpires IS NULL OR `sys_acl_levels_members`.DateExpires > FROM_UNIXTIME(?))
  174. LEFT JOIN `sys_acl_levels` ON `sys_acl_levels_members`.IDLevel = `sys_acl_levels`.ID
  175. WHERE `sys_profiles`.`id` = ?
  176. ORDER BY `sys_acl_levels_members`.DateStarts DESC
  177. LIMIT 1", $iTime, $iTime, $iProfileId);
  178. return $this->getRow($sSql);
  179. }
  180. function getLevelByIdCached($iLevel)
  181. {
  182. $sQuery = $this->prepare("SELECT
  183. `tal`.`ID` AS `id`,
  184. `tal`.`Name` AS `name`,
  185. `tal`.`QuotaSize` AS `quota_size`,
  186. `tal`.`QuotaNumber` AS `quota_number`,
  187. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`
  188. FROM `sys_acl_levels` AS `tal`
  189. WHERE `tal`.`ID`=?
  190. LIMIT 1", $iLevel);
  191. return $this->fromCache('sys_acl_levels' . $iLevel, 'getRow', $sQuery);
  192. }
  193. function getAction($iMembershipId, $iActionId)
  194. {
  195. $sQuery = $this->prepare("SELECT
  196. `tam`.`IDAction` AS `id`,
  197. `taa`.`Name` AS `name`,
  198. `taa`.`Title` AS `title`,
  199. `tam`.`AllowedCount` AS `allowed_count`,
  200. `tam`.`AllowedPeriodLen` AS `allowed_period_len`,
  201. UNIX_TIMESTAMP(`tam`.`AllowedPeriodStart`) as `allowed_period_start`,
  202. UNIX_TIMESTAMP(`tam`.`AllowedPeriodEnd`) as `allowed_period_end`,
  203. `tam`.`AdditionalParamValue` AS `additional_param_value`
  204. FROM `sys_acl_actions` AS `taa`
  205. LEFT JOIN `sys_acl_matrix` AS `tam` ON `tam`.`IDAction` = `taa`.`ID` AND `tam`.`IDLevel` = ?
  206. WHERE `taa`.`ID` = ?", $iMembershipId, $iActionId);
  207. return $this->getRow($sQuery);
  208. }
  209. function getActionTrack($iActionId, $iProfileId)
  210. {
  211. $sQuery = $this->prepare("SELECT
  212. `taat`.`ActionsLeft` AS `actions_left`,
  213. UNIX_TIMESTAMP(`taat`.`ValidSince`) as `valid_since`
  214. FROM `sys_acl_actions_track` AS `taat`
  215. WHERE `taat`.`IDAction`=? AND `taat`.`IDMember`=?", $iActionId, $iProfileId);
  216. return $this->getRow($sQuery);
  217. }
  218. function insertActionTarck($iActionId, $iProfileId, $iActionsLeft, $iValidSince)
  219. {
  220. $sQuery = $this->prepare("INSERT INTO `sys_acl_actions_track`(`IDAction`, `IDMember`, `ActionsLeft`, `ValidSince`) VALUES (?, ?, ?, FROM_UNIXTIME(?))", $iActionId, $iProfileId, $iActionsLeft, $iValidSince);
  221. return (int)$this->query($sQuery) > 0;
  222. }
  223. function updateActionTrack($iActionId, $iProfileId, $iActionsLeft, $iValidSince = 0)
  224. {
  225. $sUpdateAddon = "";
  226. if($iValidSince != 0)
  227. $sUpdateAddon = $this->prepare(", ValidSince=FROM_UNIXTIME(?)", $iValidSince);
  228. $sQuery = $this->prepare("UPDATE `sys_acl_actions_track` SET `ActionsLeft`=?" . $sUpdateAddon . " WHERE `IDAction`=? AND `IDMember`=?", $iActionsLeft, $iActionId, $iProfileId);
  229. return (int)$this->query($sQuery) > 0;
  230. }
  231. function insertLevelByProfileId($iProfileId, $iMembershipId, $iDateStarts, $iDateExpires, $sTransactionId)
  232. {
  233. $sQuery = $this->prepare("INSERT `sys_acl_levels_members` (`IDMember`, `IDLevel`, `DateStarts`, `DateExpires`, `TransactionID`) VALUES (?, ?, FROM_UNIXTIME(?), FROM_UNIXTIME(?), ?)", $iProfileId, $iMembershipId, $iDateStarts, $iDateExpires, $sTransactionId);
  234. return (int)$this->query($sQuery) > 0;
  235. }
  236. function deleteLevelByProfileId($iProfileId, $bAll = false)
  237. {
  238. $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `IDMember` = ? " . ($bAll ? "" : " AND (`DateExpires` IS NULL OR `DateExpires` > NOW())"), $iProfileId);
  239. return (int)$this->query($sQuery) > 0;
  240. }
  241. function maintenance($iDaysToCleanMemLevels)
  242. {
  243. $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `DateExpires` < NOW() - INTERVAL ? DAY", $iDaysToCleanMemLevels);
  244. if ($iDeleteMemLevels = $this->query($sQuery))
  245. $this->query("OPTIMIZE TABLE `sys_acl_levels_members`");
  246. return $iDeleteMemLevels;
  247. }
  248. function clearActionsTracksForMember($iMemberId)
  249. {
  250. $sQuery = $this->prepare("DELETE FROM `sys_acl_actions_track` WHERE `IDMember` = ?", (int)$iMemberId);
  251. return $this->query($sQuery);
  252. }
  253. }
  254. /** @} */