BxDolAclQuery.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * Database queries for ACL
  11. * @see BxDolAcl
  12. */
  13. class BxDolAclQuery extends BxDolDb implements iBxDolSingleton
  14. {
  15. protected 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_active_not_automatic_pair':
  69. $aMethod['name'] = "getPairs";
  70. $aMethod['params'][1] = 'id';
  71. $aMethod['params'][2] = 'name';
  72. $sWhereClause .= "AND `tal`.`Active`='yes' AND `tal`.`ID` NOT IN (" . $this->implode_escape(array(
  73. MEMBERSHIP_ID_NON_MEMBER,
  74. MEMBERSHIP_ID_ACCOUNT,
  75. MEMBERSHIP_ID_UNCONFIRMED,
  76. MEMBERSHIP_ID_PENDING,
  77. MEMBERSHIP_ID_SUSPENDED
  78. )) . ")";
  79. if(($sAvailableTo = $aParams['available_to'] ?? false))
  80. $sWhereClause .= " AND `tal`.`UnavailableTo` NOT LIKE " . $this->escape('%|' . $aParams['available_to'] . '|%');
  81. break;
  82. case 'all_pair':
  83. $aMethod['name'] = "getPairs";
  84. $aMethod['params'][1] = 'id';
  85. $aMethod['params'][2] = 'name';
  86. break;
  87. case 'all_order_id':
  88. $sOrderClause = "ORDER BY `tal`.`ID` ASC";
  89. break;
  90. case 'password_can_expired':
  91. $sWhereClause .= "AND `tal`.`PasswordExpired` <> 0";
  92. break;
  93. case 'password_expired_notify':
  94. $sWhereClause .= "AND `tal`.`PasswordExpired` <> 0 AND `tal`.`PasswordExpiredNotify` <> 0";
  95. break;
  96. case 'all':
  97. break;
  98. }
  99. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  100. `tal`.`ID` AS `id`,
  101. `tal`.`Name` AS `name`,
  102. `tal`.`Icon` AS `icon`,
  103. `tal`.`Description` AS `description`,
  104. `tal`.`Active` AS `active`,
  105. `tal`.`Purchasable` AS `purchasable`,
  106. `tal`.`Removable` AS `removable`,
  107. `tal`.`UnavailableTo` AS `unavailable_to`,
  108. `tal`.`QuotaSize` AS `quota_size`,
  109. `tal`.`QuotaNumber` AS `quota_number`,
  110. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  111. `tal`.`PasswordExpired` AS `password_expired`,
  112. `tal`.`PasswordExpiredNotify` AS `password_expired_notify`,
  113. `tal`.`Order` AS `order`" . $sSelectClause . "
  114. FROM `sys_acl_levels` AS `tal` " . $sJoinClause . "
  115. WHERE 1 " . $sWhereClause . " " . $sOrderClause . " " . $sLimitClause;
  116. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  117. if(!$bReturnCount)
  118. return !empty($aItems);
  119. return (int)$this->getOne("SELECT FOUND_ROWS()");
  120. }
  121. function getActions($aParams, &$aItems, $bReturnCount = true)
  122. {
  123. $sMemoryKey = '';
  124. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  125. $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  126. if(!isset($aParams['order']) || empty($aParams['order']))
  127. $sOrderClause = "`taa`.`Title` ASC";
  128. switch($aParams['type']) {
  129. case 'by_names_and_module':
  130. $aMethod['params'][1] = array(
  131. 'module' => $aParams['module']
  132. );
  133. $sWhereClause .= " AND `taa`.`Name` IN(" . $this->implode_escape($aParams['value']) . ") AND `taa`.`Module` = :module ";
  134. $sMemoryKey = 'BxDolAclQuery::getActions' . $aParams['type'] . $aParams['module'] . $sWhereClause;
  135. break;
  136. case 'by_names':
  137. $sWhereClause .= " AND `taa`.`Name` IN(" . $this->implode_escape($aParams['value']) . ")";
  138. break;
  139. case 'by_level_id':
  140. $aMethod['params'][1] = [
  141. 'level_id' => $aParams['value'],
  142. 'level_code' => pow(2, ($aParams['value'] - 1))
  143. ];
  144. $sSelectClause .= ", `tam`.`AllowedCount` AS `allowed_count`, `tam`.`AllowedPeriodLen` AS `allowed_period_len`, `tam`.`AllowedPeriodStart` AS `allowed_period_start`, `tam`.`AllowedPeriodEnd` AS `allowed_period_end`, `tam`.`AdditionalParamValue` AS `additional_param_value` ";
  145. $sJoinClause .= "LEFT JOIN `sys_acl_matrix` AS `tam` ON `taa`.`ID`=`tam`.`IDAction` ";
  146. $sWhereClause .= "AND `tam`.`IDLevel`=:level_id AND (`taa`.`DisabledForLevels`='0' OR `taa`.`DisabledForLevels`&:level_code=0)";
  147. if(isset($aParams['start'], $aParams['limit']))
  148. $sLimitClause = $this->prepareAsString("?, ?", $aParams['start'], $aParams['limit']);
  149. break;
  150. case 'by_level_id_key_id':
  151. $aMethod['name'] = 'getAllWithKey';
  152. $aMethod['params'][1] = 'id';
  153. $aMethod['params'][2] = array(
  154. 'level_id' => $aParams['value']
  155. );
  156. $sSelectClause .= ", `tam`.`AllowedCount` AS `allowed_count`, `tam`.`AllowedPeriodLen` AS `allowed_period_len` ";
  157. $sJoinClause .= "LEFT JOIN `sys_acl_matrix` AS `tam` ON `taa`.`ID`=`tam`.`IDAction` ";
  158. $sWhereClause .= "AND `tam`.`IDLevel`=:level_id";
  159. break;
  160. case 'counter_by_modules':
  161. $aMethod['name'] = 'getPairs';
  162. $aMethod['params'][1] = 'module';
  163. $aMethod['params'][2] = 'counter';
  164. $sSelectClause = ", COUNT(*) AS `counter`";
  165. $sGroupClause = "GROUP BY `taa`.`Module`";
  166. break;
  167. case 'counter_by_levels':
  168. $aMethod['name'] = 'getPairs';
  169. $aMethod['params'][1] = 'level_id';
  170. $aMethod['params'][2] = 'counter';
  171. $sSelectClause = ", `tam`.`IDLevel` AS `level_id`, COUNT(`tam`.`IDAction`) AS `counter`";
  172. $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) ";
  173. $sGroupClause = "GROUP BY `tam`.`IDLevel`";
  174. break;
  175. }
  176. if($sOrderClause)
  177. $sOrderClause = "ORDER BY " . $sOrderClause;
  178. if($sLimitClause)
  179. $sLimitClause = "LIMIT " . $sLimitClause;
  180. $aMethod['params'][0] = "SELECT " . ($bReturnCount ? "SQL_CALC_FOUND_ROWS" : "") . "
  181. `taa`.`ID` AS `id`,
  182. `taa`.`Module` AS `module`,
  183. `taa`.`Name` AS `name`,
  184. `taa`.`Title` AS `title`,
  185. `taa`.`Countable` AS `countable`,
  186. `taa`.`DisabledForLevels` AS `disabled_for_levels`" . $sSelectClause . "
  187. FROM `sys_acl_actions` AS `taa` " . $sJoinClause . "
  188. WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  189. if ($sMemoryKey) {
  190. array_unshift($aMethod['params'], $sMemoryKey, $aMethod['name']);
  191. $aItems = call_user_func_array(array($this, 'fromMemory'), $aMethod['params']);
  192. return $bReturnCount ? count($aItems) : !empty($aItems);
  193. }
  194. $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  195. if(!$bReturnCount)
  196. return !empty($aItems);
  197. return (int)$this->getOne("SELECT FOUND_ROWS()");
  198. }
  199. /**
  200. * Fetch the last purchased/assigned membership that is still active for the given profile.
  201. *
  202. * NOTE. Don't use cache here, because it's causing an error, if a number of memberrship levels are purchased at the same time.
  203. * fromMemory returns the same DateExpires because setMembership (old buyMembership) function is called in cycle in the same session.
  204. */
  205. function getLevelCurrent($iProfileId, $iTime = 0)
  206. {
  207. $iTime = $iTime == 0 ? time() : (int)$iTime;
  208. $sSql = $this->prepare("
  209. SELECT `sys_acl_levels_members`.`IDLevel` as `id`,
  210. `sys_acl_levels`.`Name` AS `name`,
  211. `sys_acl_levels`.`QuotaSize` AS `quota_size`,
  212. `sys_acl_levels`.`QuotaNumber` AS `quota_number`,
  213. `sys_acl_levels`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  214. `sys_acl_levels`.`PasswordExpired` AS `password_expired`,
  215. UNIX_TIMESTAMP(`sys_acl_levels_members`.`DateStarts`) as `date_starts`,
  216. UNIX_TIMESTAMP(`sys_acl_levels_members`.`DateExpires`) as `date_expires`,
  217. `sys_acl_levels_members`.`State` AS `state`,
  218. `sys_acl_levels_members`.`TransactionID` AS `transaction_id`,
  219. `sys_profiles`.`status`
  220. FROM `sys_acl_levels_members`
  221. RIGHT JOIN `sys_profiles` ON `sys_acl_levels_members`.IDMember = `sys_profiles`.`id`
  222. AND (`sys_acl_levels_members`.DateStarts IS NULL OR `sys_acl_levels_members`.DateStarts <= FROM_UNIXTIME(?))
  223. AND (`sys_acl_levels_members`.DateExpires IS NULL OR `sys_acl_levels_members`.DateExpires > FROM_UNIXTIME(?))
  224. LEFT JOIN `sys_acl_levels` ON `sys_acl_levels_members`.IDLevel = `sys_acl_levels`.ID
  225. WHERE `sys_profiles`.`id` = ?
  226. ORDER BY `sys_acl_levels_members`.DateStarts DESC
  227. LIMIT 1", $iTime, $iTime, $iProfileId);
  228. return $this->fromMemory('BxDolAclQuery::getLevelCurrent' . $iProfileId . $iTime, 'getRow', $sSql);
  229. }
  230. function getLevelByIdCached($iLevel)
  231. {
  232. $sQuery = $this->prepare("SELECT
  233. `tal`.`ID` AS `id`,
  234. `tal`.`Name` AS `name`,
  235. `tal`.`QuotaSize` AS `quota_size`,
  236. `tal`.`QuotaNumber` AS `quota_number`,
  237. `tal`.`QuotaMaxFileSize` AS `quota_max_file_size`,
  238. `tal`.`PasswordExpired` AS `password_expired`
  239. FROM `sys_acl_levels` AS `tal`
  240. WHERE `tal`.`ID`=?
  241. LIMIT 1", $iLevel);
  242. return $this->fromCache('sys_acl_levels' . $iLevel, 'getRow', $sQuery);
  243. }
  244. function getAction($iMembershipId, $iActionId)
  245. {
  246. $sQuery = $this->prepare("SELECT
  247. `tam`.`IDAction` AS `id`,
  248. `taa`.`Name` AS `name`,
  249. `taa`.`Title` AS `title`,
  250. `tam`.`AllowedCount` AS `allowed_count`,
  251. `tam`.`AllowedPeriodLen` AS `allowed_period_len`,
  252. UNIX_TIMESTAMP(`tam`.`AllowedPeriodStart`) as `allowed_period_start`,
  253. UNIX_TIMESTAMP(`tam`.`AllowedPeriodEnd`) as `allowed_period_end`,
  254. `tam`.`AdditionalParamValue` AS `additional_param_value`
  255. FROM `sys_acl_actions` AS `taa`
  256. LEFT JOIN `sys_acl_matrix` AS `tam` ON `tam`.`IDAction` = `taa`.`ID` AND `tam`.`IDLevel` = ?
  257. WHERE `taa`.`ID` = ?", $iMembershipId, $iActionId);
  258. return $this->fromMemory('BxDolAclQuery::getAction' . $iMembershipId . $iActionId, 'getRow', $sQuery);
  259. }
  260. function getActionTrack($iActionId, $iProfileId)
  261. {
  262. $sQuery = $this->prepare("SELECT
  263. `taat`.`ActionsLeft` AS `actions_left`,
  264. UNIX_TIMESTAMP(`taat`.`ValidSince`) as `valid_since`
  265. FROM `sys_acl_actions_track` AS `taat`
  266. WHERE `taat`.`IDAction`=? AND `taat`.`IDMember`=?", $iActionId, $iProfileId);
  267. return $this->getRow($sQuery);
  268. }
  269. function insertActionTarck($iActionId, $iProfileId, $iActionsLeft, $iValidSince)
  270. {
  271. $sQuery = $this->prepare("INSERT INTO `sys_acl_actions_track`(`IDAction`, `IDMember`, `ActionsLeft`, `ValidSince`) VALUES (?, ?, ?, FROM_UNIXTIME(?))", $iActionId, $iProfileId, $iActionsLeft, $iValidSince);
  272. return (int)$this->query($sQuery) > 0;
  273. }
  274. function updateActionTrack($iActionId, $iProfileId, $iActionsLeft, $iValidSince = 0)
  275. {
  276. $aBindings = array(
  277. 'actions_left' => $iActionsLeft,
  278. 'action_id' => $iActionId,
  279. 'member_id' => $iProfileId
  280. );
  281. $sUpdateAddon = "";
  282. if($iValidSince != 0) {
  283. $aBindings['valid_since'] = $iValidSince;
  284. $sUpdateAddon = ", ValidSince=FROM_UNIXTIME(:valid_since)";
  285. }
  286. $sQuery = "UPDATE `sys_acl_actions_track` SET `ActionsLeft`=:actions_left" . $sUpdateAddon . " WHERE `IDAction`=:action_id AND `IDMember`=:member_id";
  287. return (int)$this->query($sQuery, $aBindings) > 0;
  288. }
  289. function insertLevelByProfileId($iProfileId, $iMembershipId, $iDateStarts, $aPeriod, $sTransactionId)
  290. {
  291. $aBindings = array(
  292. 'member_id' => $iProfileId,
  293. 'level_id' => $iMembershipId,
  294. 'transaction_id' => $sTransactionId,
  295. 'date_starts' => $iDateStarts
  296. );
  297. $sSetClause = '';
  298. if((int)$aPeriod['period'] != 0) {
  299. $aBindings['period'] = (int)$aPeriod['period'];
  300. switch($aPeriod['period_unit']) {
  301. case MEMBERSHIP_PERIOD_UNIT_DAY:
  302. case MEMBERSHIP_PERIOD_UNIT_WEEK:
  303. if($aPeriod['period_unit'] == MEMBERSHIP_PERIOD_UNIT_WEEK)
  304. $aBindings['period'] *= 7;
  305. $sSetClause = "DATE_ADD(FROM_UNIXTIME(:date_starts), INTERVAL :period DAY)";
  306. break;
  307. case MEMBERSHIP_PERIOD_UNIT_MONTH:
  308. $sSetClause = "DATE_ADD(FROM_UNIXTIME(:date_starts), INTERVAL :period MONTH)";
  309. break;
  310. case MEMBERSHIP_PERIOD_UNIT_YEAR:
  311. $sSetClause = "DATE_ADD(FROM_UNIXTIME(:date_starts), INTERVAL :period YEAR)";
  312. break;
  313. }
  314. if(!empty($sSetClause) && !empty($aPeriod['period_reserve'])) {
  315. $aBindings['reserve'] = (int)$aPeriod['period_reserve'];
  316. $sSetClause = "DATE_ADD(" . $sSetClause . ", INTERVAL :reserve DAY)";
  317. }
  318. if(!empty($sSetClause))
  319. $sSetClause = ", `DateExpires`=" . $sSetClause;
  320. if(isset($aPeriod['period_trial']) && $aPeriod['period_trial'] === true) {
  321. $aBindings['state'] = 'trial';
  322. $sSetClause .= ", `State`=:state";
  323. }
  324. }
  325. $sQuery = $this->prepare("INSERT `sys_acl_levels_members` SET `IDMember`=:member_id, `IDLevel`=:level_id, `DateStarts`=FROM_UNIXTIME(:date_starts), `TransactionID`=:transaction_id" . $sSetClause);
  326. return (int)$this->query($sQuery, $aBindings) > 0;
  327. }
  328. function deleteLevelByProfileId($iProfileId, $bAll = false)
  329. {
  330. $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `IDMember` = ? " . ($bAll ? "" : " AND (`DateExpires` IS NULL OR `DateExpires` > NOW())"), $iProfileId);
  331. return (int)$this->query($sQuery) > 0;
  332. }
  333. function deleteLevelBy($aWhere)
  334. {
  335. if(empty($aWhere))
  336. return false;
  337. $sQuery = "DELETE FROM `sys_acl_levels_members` WHERE " . $this->arrayToSQL($aWhere, ' AND ');
  338. return (int)$this->query($sQuery) > 0;
  339. }
  340. function maintenance($iDaysToCleanMemLevels = 0)
  341. {
  342. $sQuery = $this->prepare("DELETE FROM `sys_acl_levels_members` WHERE `DateExpires` < NOW() - INTERVAL ? DAY", $iDaysToCleanMemLevels);
  343. if ($iDeleteMemLevels = $this->query($sQuery))
  344. $this->query("OPTIMIZE TABLE `sys_acl_levels_members`");
  345. return $iDeleteMemLevels;
  346. }
  347. function clearActionsTracksForMember($iMemberId)
  348. {
  349. $sQuery = $this->prepare("DELETE FROM `sys_acl_actions_track` WHERE `IDMember` = ?", (int)$iMemberId);
  350. return $this->query($sQuery);
  351. }
  352. function getContentByLevelAsSQLPart($sContentTable, $sContentField, $mixedLevelId)
  353. {
  354. $sJoin = $sWhere = "";
  355. $iLevelId = !is_array($mixedLevelId) ? $mixedLevelId : 0;
  356. if (!$iLevelId && is_array($mixedLevelId) && 1 == count($mixedLevelId)) {
  357. $a = array_values($mixedLevelId);
  358. $iLevelId = array_shift($a);
  359. }
  360. // unconfirmed
  361. if (MEMBERSHIP_ID_UNCONFIRMED == $iLevelId) {
  362. return array(
  363. 'where' => " AND `cblasp_a`.`email_confirmed` = 0 ",
  364. 'join' => " INNER JOIN `sys_profiles` AS `cblasp_p` ON (`" . $sContentTable . "`.`" . $sContentField . "`=`cblasp_p`.`id`) INNER JOIN `sys_accounts` AS `cblasp_a` ON (`cblasp_a`.`id`=`cblasp_p`.`account_id`) ",
  365. );
  366. }
  367. // standard
  368. elseif (MEMBERSHIP_ID_STANDARD == $iLevelId) {
  369. $sWhere .= " AND (`tlm`.`DateStarts` IS NULL OR `tlm`.`DateStarts` <= NOW()) AND (`tlm`.`DateExpires` IS NULL OR `tlm`.`DateExpires` > NOW()) AND `tlm`.`IDMember` IS NULL AND `cblasp_a`.`email_confirmed` != 0 ";
  370. $sJoin .= " LEFT JOIN `sys_acl_levels_members` AS `tlm` ON `" . $sContentTable . "`.`" . $sContentField . "`=`tlm`.`IDMember` INNER JOIN `sys_profiles` AS `cblasp_p` ON (`" . $sContentTable . "`.`" . $sContentField . "`=`cblasp_p`.`id`) INNER JOIN `sys_accounts` AS `cblasp_a` ON (`cblasp_a`.`id`=`cblasp_p`.`account_id`) ";
  371. return array(
  372. 'where' => $sWhere,
  373. 'join' => $sJoin
  374. );
  375. }
  376. // other levels
  377. else {
  378. if(is_array($mixedLevelId))
  379. $sWhere .= " AND `tlm`.`IDLevel` IN (" . $this->implode_escape($mixedLevelId) . ")";
  380. else
  381. $sWhere .= $this->prepareAsString(" AND `tlm`.`IDLevel` = ?", (int)$mixedLevelId);
  382. $sWhere .= " AND (`tlm`.`DateStarts` IS NULL OR `tlm`.`DateStarts` <= NOW()) AND (`tlm`.`DateExpires` IS NULL OR `tlm`.`DateExpires` > NOW()) AND `cblasp_a`.`email_confirmed` != 0 ";
  383. $sJoin .= " INNER JOIN `sys_acl_levels_members` AS `tlm` ON `" . $sContentTable . "`.`" . $sContentField . "`=`tlm`.`IDMember` INNER JOIN `sys_profiles` AS `cblasp_p` ON (`" . $sContentTable . "`.`" . $sContentField . "`=`cblasp_p`.`id`) INNER JOIN `sys_accounts` AS `cblasp_a` ON (`cblasp_a`.`id`=`cblasp_p`.`account_id`) ";
  384. return array(
  385. 'where' => $sWhere,
  386. 'join' => $sJoin
  387. );
  388. }
  389. }
  390. function getContentByActionAsSQLPart($sContentTable, $sContentField, $mixedActionName, $aParams = [])
  391. {
  392. $sWhere = " AND (`tlm`.`DateStarts` IS NULL OR `tlm`.`DateStarts` <= NOW()) AND (`tlm`.`DateExpires` IS NULL OR `tlm`.`DateExpires` > NOW()) ";
  393. $sJoinWhere = "";
  394. if(is_array($mixedActionName))
  395. $sJoinWhere .= " AND `ta`.`Name` IN (" . $this->implode_escape($mixedActionName) . ")";
  396. else
  397. $sJoinWhere .= $this->prepareAsString(" AND `ta`.`Name` = ?", $mixedActionName);
  398. if(!empty($aParams['module']))
  399. $sJoinWhere .= $this->prepareAsString(" AND `ta`.`Module` = ?", $aParams['module']);
  400. $sJoin = " INNER JOIN `sys_acl_levels_members` AS `tlm` ON `" . $sContentTable . "`.`" . $sContentField . "`=`tlm`.`IDMember` INNER JOIN `sys_acl_matrix` AS `tm` ON `tlm`.`IDLevel`=`tm`.`IDLevel` INNER JOIN `sys_acl_actions` AS `ta` ON (`tm`.`IDAction`=`ta`.`ID` " . $sJoinWhere . ") ";
  401. return array(
  402. 'where' => $sWhere,
  403. 'join' => $sJoin
  404. );
  405. }
  406. function getProfilesByMembership($mixedLevelId, $aParams = [])
  407. {
  408. $sSelectClause = $sJoinClause = $sWhereClause = '';
  409. $aSqlParts = $this->getContentByLevelAsSQLPart('sys_profiles', 'id', $mixedLevelId);
  410. if(isset($aSqlParts['join'], $aSqlParts['where'])) {
  411. $sJoinClause = $aSqlParts['join'];
  412. $sWhereClause = $aSqlParts['where'];
  413. }
  414. if(isset($aParams['password_expired'], $aParams['password_expired_notify'])) {
  415. $iExpire = (int)$aParams['password_expired'] * 86400;
  416. $iNotify = ($iExpire - (int)$aParams['password_expired_notify']) * 86400;
  417. $sSelectClause .= ", `ta`.`email` AS `email`, IF(`ta`.`password_changed` <> 0, `ta`.`password_changed`, `ta`.`added`) + " . $iExpire . " AS `password_expired`";
  418. $sJoinClause .= " LEFT JOIN `sys_accounts` AS `ta` ON `sys_profiles`.`account_id`=`ta`.`id`";
  419. $sWhereClause .= " AND IF(`ta`.`password_changed` <> 0, `ta`.`password_changed`, `ta`.`added`) + " . $iNotify . " < UNIX_TIMESTAMP() AND IF(`ta`.`password_changed` <> 0, `ta`.`password_changed`, `ta`.`added`) + " . $iExpire . " >= UNIX_TIMESTAMP()";
  420. }
  421. return $this->getAll("SELECT `sys_profiles`.*" . $sSelectClause . " FROM `sys_profiles`" . $sJoinClause . " WHERE 1" . $sWhereClause);
  422. }
  423. function getProfilesByAction($mixedActionName, $aParams = [])
  424. {
  425. $sMethod = "getAll";
  426. $sSqlSelect = "`sys_profiles`.*";
  427. if(isset($aParams['ids_only']) && $aParams['ids_only'] === true) {
  428. $sMethod = "getColumn";
  429. $sSqlSelect = "`sys_profiles`.`id`";
  430. }
  431. $aSqlParts = $this->getContentByActionAsSQLPart('sys_profiles', 'id', $mixedActionName, $aParams);
  432. return $this->$sMethod("SELECT DISTINCT " . $sSqlSelect . " FROM `sys_profiles`" . $aSqlParts['join'] . " WHERE 1" . $aSqlParts['where']);
  433. }
  434. }
  435. /** @} */