BxDolPrivacyQuery.php 14 KB

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