BxDolAclQuery.php 12 KB

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