BxDolPrivacyQuery.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 = "";
  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. break;
  77. case 'active_list':
  78. $sCacheFunction = 'fromMemory';
  79. $sCacheName = $this->_sCacheGroupsActVisList;
  80. $sMethod = 'getColumn';
  81. $sSelectClause = "`id`";
  82. $sWhereClause = "`active`='1' AND `visible`='1'";
  83. break;
  84. }
  85. $sSql = "SELECT
  86. " . $sSelectClause . "
  87. FROM `sys_privacy_groups`
  88. WHERE " . $sWhereClause;
  89. return !empty($sCacheFunction) && !empty($sCacheName) ? $this->$sCacheFunction($sCacheName, $sMethod, $sSql) : $this->$sMethod($sSql);
  90. }
  91. function getActions($iOwnerId)
  92. {
  93. $sQuery = $this->prepare("SELECT
  94. `tm`.`uri` AS `module_uri`,
  95. `tm`.`title` AS `module_title`,
  96. `ta`.`id` AS `action_id`,
  97. `ta`.`title` AS `action_title`,
  98. `ta`.`default_group` AS `action_default_value`,
  99. `td`.`group_id` AS `default_value`
  100. FROM `sys_objects_privacy` AS `ta`
  101. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  102. INNER JOIN `sys_modules` AS `tm` ON `ta`.`module`=`tm`.`uri`
  103. WHERE 1
  104. ORDER BY `tm`.`title`", $iOwnerId);
  105. return $this->getAll($sQuery);
  106. }
  107. function getTitle($sModule, $sAction)
  108. {
  109. $aAction = $this->_getAction($sModule, $sAction);
  110. return !empty($aAction) && isset($aAction['title']) ? $aAction['title'] : '';
  111. }
  112. function getDefaultGroup($sModule, $sAction)
  113. {
  114. $aAction = $this->_getAction($sModule, $sAction);
  115. return !empty($aAction) && isset($aAction['default_group']) ? $aAction['default_group'] : BX_DOL_PG_DEFAULT;
  116. }
  117. function getDefaultGroupByUser($sModule, $sAction, $iOwnerId)
  118. {
  119. $sQuery = $this->prepare("SELECT
  120. `td`.`group_id`
  121. FROM `sys_objects_privacy` AS `ta`
  122. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  123. WHERE
  124. `ta`.`module`=? AND `ta`.`action`=?
  125. LIMIT 1", $iOwnerId, $sModule, $sAction);
  126. $sCacheKey = $this->_sCachePrivacyObjectDefault . $sModule . '_' . $sAction . '_' . $iOwnerId;
  127. return $this->fromMemory($sCacheKey, 'getOne', $sQuery);
  128. }
  129. function replaceDefaulfGroup($iActionId, $iOwnerId, $iGroupId)
  130. {
  131. $sSql = $this->prepare("REPLACE INTO
  132. `sys_privacy_defaults`
  133. SET
  134. `owner_id`=?,
  135. `action_id`=?,
  136. `group_id`=?", $iOwnerId, $iActionId, $iGroupId);
  137. return $this->query($sSql);
  138. }
  139. public function getGroupCustom($aParams)
  140. {
  141. $sDiv = ',';
  142. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  143. $sSelectClause = "`tg`.*";
  144. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  145. switch($aParams['type']) {
  146. case 'id':
  147. $aMethod['name'] = 'getRow';
  148. $aMethod['params'][1] = array(
  149. 'id' => $aParams['id']
  150. );
  151. $sWhereClause = " AND `tg`.`id`=:id";
  152. break;
  153. case 'id_ext':
  154. $aMethod['name'] = 'getRow';
  155. $aMethod['params'][1] = array(
  156. 'id' => $aParams['id']
  157. );
  158. $sSelectClause .= ", GROUP_CONCAT(`tgm`.`" . $aParams['group_items_field'] . "` SEPARATOR '" . $sDiv . "') AS `items`";
  159. $sJoinClause = "LEFT JOIN `" . $aParams['group_items_table'] . "` AS `tgm` ON `tg`.`id`=`tgm`.`group_id`";
  160. $sWhereClause = " AND `tg`.`id`=:id";
  161. $sGroupClause = "`tg`.`id`";
  162. break;
  163. case 'pco':
  164. $aMethod['name'] = 'getRow';
  165. $aMethod['params'][1] = array(
  166. 'profile_id' => $aParams['profile_id'],
  167. 'content_id' => $aParams['content_id'],
  168. 'object' => $aParams['object'],
  169. );
  170. $sWhereClause = " AND `tg`.`profile_id`=:profile_id AND `tg`.`content_id`=:content_id AND `tg`.`object`=:object";
  171. break;
  172. case 'pcog_ext':
  173. $aMethod['name'] = 'getRow';
  174. $aMethod['params'][1] = array(
  175. 'profile_id' => $aParams['profile_id'],
  176. 'content_id' => $aParams['content_id'],
  177. 'object' => $aParams['object'],
  178. 'group_id' => $aParams['group_id'],
  179. );
  180. $sSelectClause .= ", GROUP_CONCAT(`tgi`.`" . $aParams['group_items_field'] . "` SEPARATOR '" . $sDiv . "') AS `items`";
  181. $sJoinClause = "LEFT JOIN `" . $aParams['group_items_table'] . "` AS `tgi` ON `tg`.`id`=`tgi`.`group_id`";
  182. $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";
  183. $sGroupClause = "`tg`.`id`";
  184. break;
  185. case 'profile_id':
  186. $aMethod['params'][1] = array(
  187. 'profile_id' => $aParams['profile_id']
  188. );
  189. $sWhereClause = " AND `tg`.`profile_id`=:profile_id";
  190. break;
  191. }
  192. $sGroupClause = !empty($sGroupClause) ? "GROUP BY " . $sGroupClause : $sGroupClause;
  193. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  194. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  195. $aMethod['params'][0] = "SELECT
  196. " . $sSelectClause . "
  197. FROM `sys_privacy_groups_custom` AS `tg` " . $sJoinClause . "
  198. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  199. $aResult = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  200. if(in_array($aParams['type'], array('id_ext', 'pcog_ext')) && !empty($aResult) && is_array($aResult))
  201. $aResult['items'] = explode($sDiv, $aResult['items']);
  202. return $aResult;
  203. }
  204. public function insertGroupCustom($aParamsSet)
  205. {
  206. if(empty($aParamsSet))
  207. return false;
  208. if(!$this->query("INSERT INTO `sys_privacy_groups_custom` SET " . $this->arrayToSQL($aParamsSet)))
  209. return false;
  210. return (int)$this->lastId();
  211. }
  212. public function updateGroupCustom($aParamsSet, $aParamsWhere)
  213. {
  214. if(empty($aParamsSet) || empty($aParamsWhere))
  215. return false;
  216. return $this->query("UPDATE `sys_privacy_groups_custom` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  217. }
  218. public function deleteGroupCustom($aParamsWhere)
  219. {
  220. if(empty($aParamsWhere))
  221. return false;
  222. $sWhereClause = $this->arrayToSQL($aParamsWhere, " AND ");
  223. $aGroup = $this->getRow("SELECT * FROM `sys_privacy_groups_custom` WHERE " . $sWhereClause . " LIMIT 1");
  224. if(empty($aGroup) || !is_array($aGroup))
  225. return true;
  226. $bResult = $this->query("DELETE FROM `sys_privacy_groups_custom` WHERE " . $sWhereClause . " LIMIT 1") !== false;
  227. if($bResult)
  228. $this->deleteGroupCustomMember(array('group_id' => $aGroup['id']));
  229. return $bResult;
  230. }
  231. public function insertGroupCustomMember($aParamsSet)
  232. {
  233. return $this->_insertGroupCustomItem('sys_privacy_groups_custom_members', $aParamsSet);
  234. }
  235. public function deleteGroupCustomMember($aParamsWhere)
  236. {
  237. return $this->_deleteGroupCustomItem('sys_privacy_groups_custom_members', $aParamsWhere);
  238. }
  239. public function insertGroupCustomMembership($aParamsSet)
  240. {
  241. return $this->_insertGroupCustomItem('sys_privacy_groups_custom_memberships', $aParamsSet);
  242. }
  243. public function deleteGroupCustomMembership($aParamsWhere)
  244. {
  245. return $this->_deleteGroupCustomItem('sys_privacy_groups_custom_memberships', $aParamsWhere);
  246. }
  247. public function getContentByGroupAsSQLPart($sField, $mixedGroupId)
  248. {
  249. if(is_array($mixedGroupId))
  250. $sWhere = " AND `" . $this->_sTable . "`.`" . $sField . "` IN (" . $this->implode_escape($mixedGroupId) . ")";
  251. else
  252. $sWhere = $this->prepareAsString(" AND `" . $this->_sTable . "`.`" . $sField . "` = ?", $mixedGroupId);
  253. return array(
  254. 'where' => $sWhere
  255. );
  256. }
  257. protected function _getAction($sModule, $sAction)
  258. {
  259. $sQuery = $this->prepare("SELECT
  260. `id`,
  261. `module`,
  262. `action`,
  263. `title`,
  264. `default_group`
  265. FROM `sys_objects_privacy` AS `ta`
  266. WHERE `module`=? AND `action`=?
  267. LIMIT 1", $sModule, $sAction);
  268. $sCacheKey = $this->_sCachePrivacyObject . $sModule . '_' . $sAction;
  269. return $this->fromCache($sCacheKey, 'getRow', $sQuery);
  270. }
  271. protected function _insertGroupCustomItem($sTable, $aParamsSet)
  272. {
  273. if(empty($aParamsSet))
  274. return false;
  275. return $this->query("INSERT IGNORE INTO `" . $sTable . "` SET " . $this->arrayToSQL($aParamsSet));
  276. }
  277. protected function _deleteGroupCustomItem($sTable, $aParamsWhere)
  278. {
  279. if(empty($aParamsWhere))
  280. return false;
  281. return $this->query("DELETE FROM `" . $sTable . "` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  282. }
  283. }
  284. /** @} */