BxDolPrivacyQuery.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. class BxDolPrivacyQuery extends BxDolDb
  10. {
  11. protected $_aObject;
  12. protected $_sTable;
  13. protected $_sFieldId;
  14. protected $_sFieldOwnerId;
  15. protected $_sCachePrivacyObject;
  16. protected $_sCachePrivacyObjectDefault;
  17. protected $_sCacheGroup;
  18. protected $_sCacheGroupFriends;
  19. protected $_sCacheGroupsActVis;
  20. protected $_sCacheGroupsActVisList;
  21. protected $_sCacheTestedObject;
  22. public function __construct()
  23. {
  24. parent::__construct();
  25. $this->_aObject = array();
  26. $this->_sCachePrivacyObject = 'sys_privacy_object_';
  27. $this->_sCachePrivacyObjectDefault = 'sys_privacy_object_default_';
  28. $this->_sCacheGroup = 'sys_privacy_group_';
  29. $this->_sCacheGroupFriends = 'sys_privacy_group_friends_';
  30. $this->_sCacheGroupsActVis = 'sys_privacy_group_act_vis';
  31. $this->_sCacheGroupsActVisList = 'sys_privacy_group_act_vis_list';
  32. $this->_sCacheTestedObject = 'sys_privacy_tested_object_';
  33. }
  34. static public function getPrivacyObject($sObject)
  35. {
  36. $a = BxDolDb::getInstance()->fromCache('sys_objects_privacy', 'getAllWithKey', "SELECT * FROM `sys_objects_privacy`", 'object');
  37. if ($a && isset($a[$sObject]))
  38. return $a[$sObject];
  39. return false;
  40. }
  41. public function init($aObject = array())
  42. {
  43. if(empty($aObject) || !is_array($aObject))
  44. return;
  45. $this->_aObject = $aObject;
  46. $this->_sTable = $this->_aObject['table'];
  47. $this->_sFieldId = $this->_aObject['table_field_id'];
  48. $this->_sFieldOwnerId = $this->_aObject['table_field_author'];
  49. }
  50. function getObjectInfo($sAction, $iObjectId)
  51. {
  52. if(empty($this->_sTable) || empty($this->_sFieldId) || empty($this->_sFieldOwnerId))
  53. return array();
  54. $sQuery = $this->prepare("SELECT `" . $this->_sFieldId . "` AS `id`, `" . $this->_sFieldOwnerId . "` AS `owner_id`, `" . $sAction . "` AS `group_id` FROM `" . $this->_sTable . "` WHERE `" . $this->_sFieldId . "`=? LIMIT 1", $iObjectId);
  55. $sCacheKey = $this->_sCacheTestedObject . $this->_sTable . '_' . $this->_aObject['action'] . '_' . $iObjectId;
  56. return $this->fromMemory($sCacheKey, 'getRow', $sQuery);
  57. }
  58. function getGroupsBy($aParams)
  59. {
  60. $sSelectClause = "`id`, `title`, `check`, `active`";
  61. $sWhereClause = $sOrderClause = "";
  62. switch($aParams['type']) {
  63. case 'id':
  64. $sCacheFunction = 'fromCache';
  65. $sCacheName = $this->_sCacheGroup . $aParams['id'];
  66. $sMethod = 'getRow';
  67. $sWhereClause = $this->prepareAsString("`id`=?", (int)$aParams['id']);
  68. break;
  69. case 'active':
  70. $sCacheFunction = 'fromMemory';
  71. $sCacheName = $this->_sCacheGroupsActVis;
  72. $sMethod = 'getAll';
  73. $sWhereClause = "`active`='1' AND `visible`='1'";
  74. $sOrderClause = "`order` ASC";
  75. break;
  76. case 'active_list':
  77. $sCacheFunction = 'fromMemory';
  78. $sCacheName = $this->_sCacheGroupsActVisList;
  79. $sMethod = 'getColumn';
  80. $sSelectClause = "`id`";
  81. $sWhereClause = "`active`='1' AND `visible`='1'";
  82. break;
  83. }
  84. if($sOrderClause)
  85. $sOrderClause = "ORDER BY " . $sOrderClause;
  86. $sSql = "SELECT
  87. " . $sSelectClause . "
  88. FROM `sys_privacy_groups`
  89. WHERE " . $sWhereClause . " " . $sOrderClause;
  90. return !empty($sCacheFunction) && !empty($sCacheName) ? $this->$sCacheFunction($sCacheName, $sMethod, $sSql) : $this->$sMethod($sSql);
  91. }
  92. function getActions($iOwnerId)
  93. {
  94. $sQuery = $this->prepare("SELECT
  95. `tm`.`uri` AS `module_uri`,
  96. `tm`.`title` AS `module_title`,
  97. `ta`.`id` AS `action_id`,
  98. `ta`.`title` AS `action_title`,
  99. `ta`.`default_group` AS `action_default_value`,
  100. `td`.`group_id` AS `default_value`
  101. FROM `sys_objects_privacy` AS `ta`
  102. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  103. INNER JOIN `sys_modules` AS `tm` ON `ta`.`module`=`tm`.`uri`
  104. WHERE 1
  105. ORDER BY `tm`.`title`", $iOwnerId);
  106. return $this->getAll($sQuery);
  107. }
  108. function getTitle($sModule, $sAction)
  109. {
  110. $aAction = $this->_getAction($sModule, $sAction);
  111. return !empty($aAction) && isset($aAction['title']) ? $aAction['title'] : '';
  112. }
  113. function getDefaultGroup($sModule, $sAction)
  114. {
  115. $aAction = $this->_getAction($sModule, $sAction);
  116. return !empty($aAction) && isset($aAction['default_group']) ? $aAction['default_group'] : BX_DOL_PG_DEFAULT;
  117. }
  118. function getDefaultGroupByUser($sModule, $sAction, $iOwnerId)
  119. {
  120. $sQuery = $this->prepare("SELECT
  121. `td`.`group_id`
  122. FROM `sys_objects_privacy` AS `ta`
  123. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  124. WHERE
  125. `ta`.`module`=? AND `ta`.`action`=?
  126. LIMIT 1", $iOwnerId, $sModule, $sAction);
  127. $sCacheKey = $this->_sCachePrivacyObjectDefault . $sModule . '_' . $sAction . '_' . $iOwnerId;
  128. return $this->fromMemory($sCacheKey, 'getOne', $sQuery);
  129. }
  130. function replaceDefaulfGroup($iActionId, $iOwnerId, $iGroupId)
  131. {
  132. $sSql = $this->prepare("REPLACE INTO
  133. `sys_privacy_defaults`
  134. SET
  135. `owner_id`=?,
  136. `action_id`=?,
  137. `group_id`=?", $iOwnerId, $iActionId, $iGroupId);
  138. return $this->query($sSql);
  139. }
  140. public function getGroupCustom($aParams)
  141. {
  142. $sDiv = ',';
  143. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  144. $sSelectClause = "`tg`.*";
  145. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  146. switch($aParams['type']) {
  147. case 'id':
  148. $aMethod['name'] = 'getRow';
  149. $aMethod['params'][1] = array(
  150. 'id' => $aParams['id']
  151. );
  152. $sWhereClause = " AND `tg`.`id`=:id";
  153. break;
  154. case 'id_ext':
  155. $aMethod['name'] = 'getRow';
  156. $aMethod['params'][1] = array(
  157. 'id' => $aParams['id']
  158. );
  159. $sSelectClause .= ", GROUP_CONCAT(`tgm`.`" . $aParams['group_items_field'] . "` SEPARATOR '" . $sDiv . "') AS `items`";
  160. $sJoinClause = "LEFT JOIN `" . $aParams['group_items_table'] . "` AS `tgm` ON `tg`.`id`=`tgm`.`group_id`";
  161. $sWhereClause = " AND `tg`.`id`=:id";
  162. $sGroupClause = "`tg`.`id`";
  163. break;
  164. case 'pco':
  165. $aMethod['name'] = 'getRow';
  166. $aMethod['params'][1] = array(
  167. 'profile_id' => $aParams['profile_id'],
  168. 'content_id' => $aParams['content_id'],
  169. 'object' => $aParams['object'],
  170. );
  171. $sWhereClause = " AND `tg`.`profile_id`=:profile_id AND `tg`.`content_id`=:content_id AND `tg`.`object`=:object";
  172. break;
  173. case 'pcog_ext':
  174. $aMethod['name'] = 'getRow';
  175. $aMethod['params'][1] = array(
  176. 'profile_id' => $aParams['profile_id'],
  177. 'content_id' => $aParams['content_id'],
  178. 'object' => $aParams['object'],
  179. 'group_id' => $aParams['group_id'],
  180. );
  181. $sSelectClause .= ", GROUP_CONCAT(`tgi`.`" . $aParams['group_items_field'] . "` SEPARATOR '" . $sDiv . "') AS `items`";
  182. $sJoinClause = "LEFT JOIN `" . $aParams['group_items_table'] . "` AS `tgi` ON `tg`.`id`=`tgi`.`group_id`";
  183. $sWhereClause = " AND `tg`.`profile_id`=:profile_id AND (`tg`.`content_id`=:content_id" . (!empty($aParams['content_id']) ? " OR `tg`.`content_id`='0'" : "") . ") AND `tg`.`object`=:object AND `tg`.`group_id`=:group_id";
  184. $sGroupClause = "`tg`.`id`";
  185. break;
  186. case 'profile_id':
  187. $aMethod['params'][1] = [
  188. 'profile_id' => $aParams['profile_id']
  189. ];
  190. $sWhereClause = " AND `tg`.`profile_id`=:profile_id";
  191. if(isset($aParams['content_id'])) {
  192. $aMethod['params'][1]['content_id'] = $aParams['content_id'];
  193. $sWhereClause .= " AND `tg`.`content_id`=:content_id";
  194. }
  195. break;
  196. }
  197. $sGroupClause = !empty($sGroupClause) ? "GROUP BY " . $sGroupClause : $sGroupClause;
  198. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  199. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  200. $aMethod['params'][0] = "SELECT
  201. " . $sSelectClause . "
  202. FROM `sys_privacy_groups_custom` AS `tg` " . $sJoinClause . "
  203. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  204. $aResult = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  205. if(in_array($aParams['type'], ['id_ext', 'pcog_ext']) && !empty($aResult) && is_array($aResult))
  206. $aResult['items'] = !empty($aResult['items']) ? explode($sDiv, $aResult['items']) : [];
  207. return $aResult;
  208. }
  209. public function insertGroupCustom($aParamsSet)
  210. {
  211. if(empty($aParamsSet))
  212. return false;
  213. if(!$this->query("INSERT INTO `sys_privacy_groups_custom` SET " . $this->arrayToSQL($aParamsSet)))
  214. return false;
  215. return (int)$this->lastId();
  216. }
  217. public function updateGroupCustom($aParamsSet, $aParamsWhere)
  218. {
  219. if(empty($aParamsSet) || empty($aParamsWhere))
  220. return false;
  221. return $this->query("UPDATE `sys_privacy_groups_custom` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  222. }
  223. public function deleteGroupCustom($aParamsWhere)
  224. {
  225. if(empty($aParamsWhere))
  226. return false;
  227. $sWhereClause = $this->arrayToSQL($aParamsWhere, " AND ");
  228. $aGroup = $this->getRow("SELECT * FROM `sys_privacy_groups_custom` WHERE " . $sWhereClause . " LIMIT 1");
  229. if(empty($aGroup) || !is_array($aGroup))
  230. return true;
  231. $bResult = $this->query("DELETE FROM `sys_privacy_groups_custom` WHERE " . $sWhereClause . " LIMIT 1") !== false;
  232. if($bResult) {
  233. $this->deleteGroupCustomMember(['group_id' => $aGroup['id']]);
  234. $this->deleteGroupCustomMembership(['group_id' => $aGroup['id']]);
  235. }
  236. return $bResult;
  237. }
  238. public function insertGroupCustomMember($aParamsSet)
  239. {
  240. return $this->_insertGroupCustomItem('sys_privacy_groups_custom_members', $aParamsSet);
  241. }
  242. public function deleteGroupCustomMember($aParamsWhere)
  243. {
  244. return $this->_deleteGroupCustomItem('sys_privacy_groups_custom_members', $aParamsWhere);
  245. }
  246. public function insertGroupCustomMembership($aParamsSet)
  247. {
  248. return $this->_insertGroupCustomItem('sys_privacy_groups_custom_memberships', $aParamsSet);
  249. }
  250. public function deleteGroupCustomMembership($aParamsWhere)
  251. {
  252. return $this->_deleteGroupCustomItem('sys_privacy_groups_custom_memberships', $aParamsWhere);
  253. }
  254. public function getContentByGroupAsSQLPart($sField, $mixedGroupId)
  255. {
  256. if(is_array($mixedGroupId))
  257. $sWhere = " AND `" . $this->_sTable . "`.`" . $sField . "` IN (" . $this->implode_escape($mixedGroupId) . ")";
  258. else
  259. $sWhere = $this->prepareAsString(" AND `" . $this->_sTable . "`.`" . $sField . "` = ?", $mixedGroupId);
  260. return array(
  261. 'where' => $sWhere
  262. );
  263. }
  264. public function getContentByContextAsSQLPart($sField, $mixedContextId)
  265. {
  266. if(!empty($mixedContextId))
  267. return $this->getContentByGroupAsSQLPart($sField, $mixedContextId);
  268. return [
  269. 'where' => " AND `" . $this->_sTable . "`.`" . $sField . "` < 0"
  270. ];
  271. }
  272. public function getContentByGroupAndContextAsSQLPart($sField, $mixedGroupId, $mixedContextId)
  273. {
  274. $aResultGroup = $this->getContentByGroupAsSQLPart($sField, $mixedGroupId);
  275. $aResultContext = $this->getContentByContextAsSQLPart($sField, $mixedContextId);
  276. $sPattern = "/^\s*AND\s*/i";
  277. return [
  278. 'where' => " AND (" . preg_replace($sPattern, '', $aResultGroup['where']) . " OR " . preg_replace($sPattern, '', $aResultContext['where']) . ")"
  279. ];
  280. }
  281. protected function _getAction($sModule, $sAction)
  282. {
  283. $sQuery = $this->prepare("SELECT
  284. `id`,
  285. `module`,
  286. `action`,
  287. `title`,
  288. `default_group`
  289. FROM `sys_objects_privacy` AS `ta`
  290. WHERE `module`=? AND `action`=?
  291. LIMIT 1", $sModule, $sAction);
  292. $sCacheKey = $this->_sCachePrivacyObject . $sModule . '_' . $sAction;
  293. return $this->fromCache($sCacheKey, 'getRow', $sQuery);
  294. }
  295. protected function _insertGroupCustomItem($sTable, $aParamsSet)
  296. {
  297. if(empty($aParamsSet))
  298. return false;
  299. return $this->query("INSERT IGNORE INTO `" . $sTable . "` SET " . $this->arrayToSQL($aParamsSet));
  300. }
  301. protected function _deleteGroupCustomItem($sTable, $aParamsWhere)
  302. {
  303. if(empty($aParamsWhere))
  304. return false;
  305. return $this->query("DELETE FROM `" . $sTable . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  306. }
  307. }
  308. /** @} */