BxDolAclQuery.php 13 KB

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