BxDolAclQuery.php 23 KB

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