BxDolAclQuery.php 12 KB

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