BxDolPrivacyQuery.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 $_sCacheGroupsActive;
  20. protected $_sCacheTestedObject;
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. $this->_aObject = array();
  25. $this->_sCachePrivacyObject = 'sys_privacy_object_';
  26. $this->_sCachePrivacyObjectDefault = 'sys_privacy_object_default_';
  27. $this->_sCacheGroup = 'sys_privacy_group_';
  28. $this->_sCacheGroupFriends = 'sys_privacy_group_friends_';
  29. $this->_sCacheGroupsActVis = 'sys_privacy_group_act_vis';
  30. $this->_sCacheTestedObject = 'sys_privacy_tested_object_';
  31. }
  32. static public function getPrivacyObject($sObject)
  33. {
  34. $oDb = BxDolDb::getInstance();
  35. $sQuery = $oDb->prepare("SELECT * FROM `sys_objects_privacy` WHERE `object` = ?", $sObject);
  36. $aObject = $oDb->getRow($sQuery);
  37. if(!$aObject || !is_array($aObject))
  38. return false;
  39. return $aObject;
  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. switch($aParams['type']) {
  61. case 'id':
  62. $sCacheFunction = 'fromCache';
  63. $sCacheName = $this->_sCacheGroup . $aParams['id'];
  64. $sMethod = 'getRow';
  65. $sWhereClause = $this->prepareAsString("`id`=?", (int)$aParams['id']);
  66. break;
  67. case 'active':
  68. $sCacheFunction = 'fromMemory';
  69. $sCacheName = $this->_sCacheGroupsActVis;
  70. $sMethod = 'getAll';
  71. $sWhereClause = "`active`='1' AND `visible`='1'";
  72. break;
  73. }
  74. $sSql = "SELECT
  75. `id`,
  76. `title`,
  77. `check`,
  78. `active`
  79. FROM `sys_privacy_groups`
  80. WHERE " . $sWhereClause;
  81. return !empty($sCacheFunction) && !empty($sCacheName) ? $this->$sCacheFunction($sCacheName, $sMethod, $sSql) : $this->$sMethod($sSql);
  82. }
  83. function getActions($iOwnerId)
  84. {
  85. $sQuery = $this->prepare("SELECT
  86. `tm`.`uri` AS `module_uri`,
  87. `tm`.`title` AS `module_title`,
  88. `ta`.`id` AS `action_id`,
  89. `ta`.`title` AS `action_title`,
  90. `ta`.`default_group` AS `action_default_value`,
  91. `td`.`group_id` AS `default_value`
  92. FROM `sys_objects_privacy` AS `ta`
  93. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  94. INNER JOIN `sys_modules` AS `tm` ON `ta`.`module`=`tm`.`uri`
  95. WHERE 1
  96. ORDER BY `tm`.`title`", $iOwnerId);
  97. return $this->getAll($sQuery);
  98. }
  99. function getTitle($sModule, $sAction)
  100. {
  101. $aAction = $this->_getAction($sModule, $sAction);
  102. return !empty($aAction) && isset($aAction['title']) ? $aAction['title'] : '';
  103. }
  104. function getDefaultGroup($sModule, $sAction)
  105. {
  106. $aAction = $this->_getAction($sModule, $sAction);
  107. return !empty($aAction) && isset($aAction['default_group']) ? $aAction['default_group'] : BX_DOL_PG_DEFAULT;
  108. }
  109. function getDefaultGroupByUser($sModule, $sAction, $iOwnerId)
  110. {
  111. $sQuery = $this->prepare("SELECT
  112. `td`.`group_id`
  113. FROM `sys_objects_privacy` AS `ta`
  114. LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`=?
  115. WHERE
  116. `ta`.`module`=? AND `ta`.`action`=?
  117. LIMIT 1", $iOwnerId, $sModule, $sAction);
  118. $sCacheKey = $this->_sCachePrivacyObjectDefault . $sModule . '_' . $sAction . '_' . $iOwnerId;
  119. return $this->fromMemory($sCacheKey, 'getOne', $sQuery);
  120. }
  121. function replaceDefaulfGroup($iActionId, $iOwnerId, $iGroupId)
  122. {
  123. $sSql = $this->prepare("REPLACE INTO
  124. `sys_privacy_defaults`
  125. SET
  126. `owner_id`=?,
  127. `action_id`=?,
  128. `group_id`=?", $iOwnerId, $iActionId, $iGroupId);
  129. return $this->query($sSql);
  130. }
  131. public function getGroupCustom($aParams)
  132. {
  133. $CNF = &$this->_oConfig->CNF;
  134. $sDiv = ',';
  135. $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
  136. $sSelectClause = "`tg`.*";
  137. $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
  138. switch($aParams['type']) {
  139. case 'id':
  140. $aMethod['name'] = 'getRow';
  141. $aMethod['params'][1] = array(
  142. 'id' => $aParams['id']
  143. );
  144. $sWhereClause = " AND `tg`.`id`=:id";
  145. break;
  146. case 'id_ext':
  147. $aMethod['name'] = 'getRow';
  148. $aMethod['params'][1] = array(
  149. 'id' => $aParams['id']
  150. );
  151. $sSelectClause .= ", GROUP_CONCAT(`tgm`.`member_id` SEPARATOR '" . $sDiv . "') AS `members`";
  152. $sJoinClause = "LEFT JOIN `sys_privacy_groups_custom_members` AS `tgm` ON `tg`.`id`=`tgm`.`group_id`";
  153. $sWhereClause = " AND `tg`.`id`=:id";
  154. $sGroupClause = "`tg`.`id`";
  155. break;
  156. case 'pco':
  157. $aMethod['name'] = 'getRow';
  158. $aMethod['params'][1] = array(
  159. 'profile_id' => $aParams['profile_id'],
  160. 'content_id' => $aParams['content_id'],
  161. 'object' => $aParams['object'],
  162. );
  163. $sWhereClause = " AND `tg`.`profile_id`=:profile_id AND `tg`.`content_id`=:content_id AND `tg`.`object`=:object";
  164. break;
  165. case 'pcog_ext':
  166. $aMethod['name'] = 'getRow';
  167. $aMethod['params'][1] = array(
  168. 'profile_id' => $aParams['profile_id'],
  169. 'content_id' => $aParams['content_id'],
  170. 'object' => $aParams['object'],
  171. 'group_id' => $aParams['group_id'],
  172. );
  173. $sSelectClause .= ", GROUP_CONCAT(`tgm`.`member_id` SEPARATOR '" . $sDiv . "') AS `members`";
  174. $sJoinClause = "LEFT JOIN `sys_privacy_groups_custom_members` AS `tgm` ON `tg`.`id`=`tgm`.`group_id`";
  175. $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";
  176. $sGroupClause = "`tg`.`id`";
  177. break;
  178. case 'profile_id':
  179. $aMethod['params'][1] = array(
  180. 'profile_id' => $aParams['profile_id']
  181. );
  182. $sWhereClause = " AND `tg`.`profile_id`=:profile_id";
  183. break;
  184. }
  185. $sGroupClause = !empty($sGroupClause) ? "GROUP BY " . $sGroupClause : $sGroupClause;
  186. $sOrderClause = !empty($sOrderClause) ? "ORDER BY " . $sOrderClause : $sOrderClause;
  187. $sLimitClause = !empty($sLimitClause) ? "LIMIT " . $sLimitClause : $sLimitClause;
  188. $aMethod['params'][0] = "SELECT
  189. " . $sSelectClause . "
  190. FROM `sys_privacy_groups_custom` AS `tg` " . $sJoinClause . "
  191. WHERE 1" . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
  192. $aResult = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
  193. if(in_array($aParams['type'], array('id_ext', 'pcog_ext')) && !empty($aResult) && is_array($aResult))
  194. $aResult['members'] = explode($sDiv, $aResult['members']);
  195. return $aResult;
  196. }
  197. public function insertGroupCustom($aParamsSet)
  198. {
  199. if(empty($aParamsSet))
  200. return false;
  201. if(!$this->query("INSERT INTO `sys_privacy_groups_custom` SET " . $this->arrayToSQL($aParamsSet)))
  202. return false;
  203. return (int)$this->lastId();
  204. }
  205. public function updateGroupCustom($aParamsSet, $aParamsWhere)
  206. {
  207. if(empty($aParamsSet) || empty($aParamsWhere))
  208. return false;
  209. return $this->query("UPDATE `sys_privacy_groups_custom` SET " . $this->arrayToSQL($aParamsSet) . " WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  210. }
  211. public function deleteGroupCustom($aParamsWhere)
  212. {
  213. if(empty($aParamsWhere))
  214. return false;
  215. $sWhereClause = $this->arrayToSQL($aParamsWhere, " AND ");
  216. $aGroup = $this->getRow("SELECT * FROM `sys_privacy_groups_custom` WHERE " . $sWhereClause . " LIMIT 1");
  217. if(empty($aGroup) || !is_array($aGroup))
  218. return true;
  219. $bResult = $this->query("DELETE FROM `sys_privacy_groups_custom` WHERE " . $sWhereClause . " LIMIT 1") !== false;
  220. if($bResult)
  221. $this->deleteGroupCustomMember(array('group_id' => $aGroup['id']));
  222. return $bResult;
  223. }
  224. public function insertGroupCustomMember($aParamsSet)
  225. {
  226. if(empty($aParamsSet))
  227. return false;
  228. return $this->query("INSERT INTO `sys_privacy_groups_custom_members` SET " . $this->arrayToSQL($aParamsSet));
  229. }
  230. public function deleteGroupCustomMember($aParamsWhere)
  231. {
  232. if(empty($aParamsWhere))
  233. return false;
  234. return $this->query("DELETE FROM `sys_privacy_groups_custom_members` WHERE " . $this->arrayToSQL($aParamsWhere, " AND "));
  235. }
  236. function getContentByGroupAsSQLPart($sField, $mixedGroupId)
  237. {
  238. if(is_array($mixedGroupId))
  239. $sWhere = " AND `" . $this->_sTable . "`.`" . $sField . "` IN (" . $this->implode_escape($mixedGroupId) . ")";
  240. else
  241. $sWhere = $this->prepareAsString(" AND `" . $this->_sTable . "`.`" . $sField . "` = ?", $mixedGroupId);
  242. return array(
  243. 'where' => $sWhere
  244. );
  245. }
  246. protected function _getAction($sModule, $sAction)
  247. {
  248. $sQuery = $this->prepare("SELECT
  249. `id`,
  250. `module`,
  251. `action`,
  252. `title`,
  253. `default_group`
  254. FROM `sys_objects_privacy` AS `ta`
  255. WHERE `module`=? AND `action`=?
  256. LIMIT 1", $sModule, $sAction);
  257. $sCacheKey = $this->_sCachePrivacyObject . $sModule . '_' . $sAction;
  258. return $this->fromCache($sCacheKey, 'getRow', $sQuery);
  259. }
  260. }
  261. /** @} */