Cheetah
ChWsbPrivacyQuery.php
Go to the documentation of this file.
1 <?php
2 
8 ch_import('ChWsbDb');
9 
11 {
12  var $_sTable;
15 
22 
26  function __construct($sTable = '', $sFieldId = '', $sFieldOwnerId = '')
27  {
28  parent::__construct();
29 
30  $this->_sTable = $sTable;
31  $this->_sFieldId = $sFieldId;
32  $this->_sFieldOwnerId = $sFieldOwnerId;
33 
34  $this->_sGroupCache = 'sys_ps_group_';
35  $this->_sGroupsByOwnersCache = 'sys_ps_groups_owners_';
36  $this->_sGroupMembersCache = 'sys_ps_group_members_';
37  $this->_sObjectCache = 'sys_ps_object_';
38  $this->_sActionCache = 'sys_ps_action_';
39  $this->_sActionDefaultCache = 'sys_ps_action_default_';
40  }
41  function getObjectInfo($sAction, $iObjectId)
42  {
43  if(empty($this->_sTable) || empty($this->_sFieldId) || empty($this->_sFieldOwnerId))
44  return array();
45 
46  return $this->fromMemory(
47  $this->_sObjectCache . $this->_sTable . '_' . $sAction . '_' . $iObjectId,
48  "getRow",
49  "SELECT `" . $this->_sFieldOwnerId . "` AS `owner_id`, `" . $sAction . "` AS `group_id` FROM `" . $this->_sTable . "` WHERE `" . $this->_sFieldId . "`= ? LIMIT 1",
50  [$iObjectId]
51  );
52  }
53  function isGroupMember($mixedObjectGroupId, $iObjectOwnerId, $iViewerId)
54  {
55  $iGroupId = (int)$mixedObjectGroupId;
56 
57  $aGroup = $this->getGroupsBy(array('type' => 'id', 'id' => $iGroupId));
58  if(empty($aGroup) || !is_array($aGroup))
59  return false;
60 
61  //--- Check in group's direct members ---//
62  if((int)$aGroup['owner_id'] != 0) {
63  $aGroupMembers = $this->fromMemory(
64  $this->_sGroupMembersCache . $iGroupId,
65  "getAllWithKey",
66  "SELECT `id`, `member_id` FROM `sys_privacy_members` WHERE `group_id`='" . $iGroupId . "'",
67  "member_id"
68  );
69 
70  if(array_key_exists($iViewerId, $aGroupMembers))
71  return true;
72  }
73  //--- Check in system groups('All', 'Friends', etc) ---//
74  if($this->getParam('sys_ps_enabled_group_' . $aGroup['id']) == 'on' && (int)$aGroup['owner_id'] == 0 && !empty($aGroup['get_content'])) {
75  $oFunction = function($arg0, $arg1, $arg2) use ($aGroup) {
76  return eval($aGroup['get_content']);
77  };
78 
79  if($oFunction($this, $iObjectOwnerId, $iViewerId))
80  return true;
81  }
82  //--- Check in 'Default' group ---//
83  if((int)$aGroup['owner_id'] == 0 && !empty($aGroup['get_parent'])) {
84  $oFunction = function($arg0, $arg1, $arg2) use ($aGroup) {
85  return eval($aGroup['get_parent']);
86  };
87 
88  $iId = $oFunction($this, $iObjectOwnerId, $iViewerId);
89  if($this->isGroupMember($iId, $iObjectOwnerId, $iViewerId))
90  return true;
91  }
92  //--- Check in extended groups ---//
93  if((int)$aGroup['parent_id'] != 0 && $this->isGroupMember($aGroup['parent_id'], $iObjectOwnerId, $iViewerId))
94  return true;
95 
96  return false;
97  }
98  function getGroupsBy($aParams)
99  {
100  switch($aParams['type']) {
101  case 'id':
102  $sCacheFunction = 'fromCache';
103  $sCacheName = $this->_sGroupCache . $aParams['id'];
104  $sMethod = 'getRow';
105  $sWhereClause = "`id`='" . (int)$aParams['id'] . "'";
106  break;
107 
108  case 'owner':
109  $aIds = array($aParams['owner_id']);
110  if(isset($aParams['full']))
111  $aIds[] = '0';
112 
113  $sCacheFunction = 'fromMemory';
114  $sCacheName = $this->_sGroupsByOwnersCache . implode("_", $aIds);
115  $sMethod = 'getAll';
116  $sWhereClause = "`owner_id` IN ('" . implode("','", $aIds) . "')";
117  break;
118 
119  case 'extendable':
120  $$sCacheFunction = '';
121  $sCacheName = '';
122  $sMethod = 'getAll';
123  $sWhereClause = "(`owner_id`='0' AND `get_content`<>'') OR `owner_id`='" . (int)$aParams['owner_id'] . "'";
124  break;
125  }
126  $sSql = "SELECT
127  `id`,
128  `owner_id`,
129  `parent_id`,
130  `title`,
131  `home_url`,
132  `get_parent`,
133  `get_content`,
134  `members_count`
135  FROM `sys_privacy_groups`
136  WHERE " . $sWhereClause;
137 
138  return !empty($sCacheFunction) && !empty($sCacheName) ? $this->$sCacheFunction($sCacheName, $sMethod, $sSql) : $this->$sMethod($sSql);
139  }
140  function deleteGroupsById($mixedValues)
141  {
142  if(is_array($mixedValues)) {
143  foreach ($mixedValues as $k => $v)
144  $mixedValues[$k] = (int)$v;
145  $sGroupIds = implode("','", $mixedValues);
146  } else if(is_string($mixedValues)) {
147  $sGroupIds = process_db_input($mixedValues, CH_TAGS_STRIP);
148  }
149 
150  $this->query("DELETE FROM `sys_privacy_members` WHERE `group_id` IN ('" . $sGroupIds . "')");
151  $this->query("DELETE FROM `sys_privacy_groups` WHERE `id` IN ('" . $sGroupIds . "')");
152  }
153  function getMembersIds($iGroupId)
154  {
155  $sSql = "SELECT `member_id` AS `id` FROM `sys_privacy_members` WHERE `group_id`= ?";
156  return $this->getAll($sSql, [$iGroupId]);
157  }
158  function addToGroup($iGroupId, $aMemberIds)
159  {
160  $iCount = 0;
161  foreach($aMemberIds as $iMemberId)
162  $iCount += $this->query("INSERT IGNORE INTO `sys_privacy_members` SET `group_id`='" . $iGroupId . "', `member_id`='" . (int)$iMemberId . "'");
163  $this->cleanCache($this->_sGroupMembersCache . $iGroupId);
164 
165  $this->query("UPDATE `sys_privacy_groups` SET `members_count`=`members_count`+'" . $iCount . "' WHERE `id`='" . $iGroupId . "' LIMIT 1");
166  $this->cleanCache($this->_sGroupCache . $iGroupId);
167  }
168  function deleteFromGroup($iGroupId, $aMemberIds)
169  {
170  foreach ($aMemberIds as $k => $v)
171  $aMemberIds[$k] = (int)$v;
172  $iCount = $this->query("DELETE FROM `sys_privacy_members` WHERE `group_id`='" . $iGroupId . "' AND `member_id` IN ('" . implode("','", $aMemberIds) . "')");
173  $this->cleanCache($this->_sGroupMembersCache . $iGroupId);
174 
175  $this->query("UPDATE `sys_privacy_groups` SET `members_count`=`members_count`-'" . $iCount . "' WHERE `id`='" . $iGroupId . "' LIMIT 1");
176  $this->cleanCache($this->_sGroupCache . $iGroupId);
177  }
178 
179  function getDefaultGroup($iOwnerId)
180  {
181  $sSql = "SELECT `PrivacyDefaultGroup` FROM `Profiles` WHERE `ID`='" . $iOwnerId . "' LIMIT 1";
182  return $this->getOne($sSql);
183  }
184  function setDefaultGroup($iOwnerId, $iGroupId)
185  {
186  $sSql = "UPDATE `Profiles` SET `PrivacyDefaultGroup`='" . $iGroupId . "' WHERE `ID`='" . $iOwnerId . "'";
187  return (int)$this->query($sSql);
188  }
189  function getActions($iOwnerId)
190  {
191  $sSql = "SELECT
192  `tm`.`uri` AS `module_uri`,
193  `tm`.`title` AS `module_title`,
194  `ta`.`id` AS `action_id`,
195  `ta`.`title` AS `action_title`,
196  `ta`.`default_group` AS `action_default_value`,
197  `td`.`group_id` AS `default_value`
198  FROM `sys_privacy_actions` AS `ta`
199  LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`= ?
200  INNER JOIN `sys_modules` AS `tm` ON `ta`.`module_uri`=`tm`.`uri`
201  WHERE 1
202  ORDER BY `tm`.`title`";
203  return $this->getAll($sSql, [$iOwnerId]);
204  }
205  function getDefaultValue($iOwnerId, $sModuleUri, $sActionName)
206  {
207  $sSql = "SELECT
208  `td`.`group_id`
209  FROM `sys_privacy_actions` AS `ta`
210  LEFT JOIN `sys_privacy_defaults` AS `td` ON `ta`.`id`=`td`.`action_id` AND `td`.`owner_id`='" . $iOwnerId . "'
211  WHERE `ta`.`module_uri`='" . $sModuleUri . "' AND `ta`.`name`='" . $sActionName . "'
212  LIMIT 1";
213  return $this->fromMemory($this->_sActionDefaultCache . $sModuleUri . '_' . $sActionName . '_' . $iOwnerId, 'getOne', $sSql);
214  }
215  function getDefaultValueModule($sModuleUri, $sActionName)
216  {
217  $aAction = $this->_getAction($sModuleUri, $sActionName);
218  return !empty($aAction) && isset($aAction['default_group']) ? $aAction['default_group'] : CH_WSB_PG_ALL;
219  }
220  function replaceDefaulfValue($iOwnerId, $iActionId, $iGroupId)
221  {
222  $sSql = "REPLACE INTO `sys_privacy_defaults` SET `owner_id`='" . $iOwnerId . "', `action_id`='" . $iActionId . "', `group_id`='" . $iGroupId . "'";
223  return $this->query($sSql);
224  }
225  function getFieldActionTitle($sModuleUri, $sActionName)
226  {
227  $aAction = $this->_getAction($sModuleUri, $sActionName);
228  return !empty($aAction) && isset($aAction['title']) ? $aAction['title'] : '';
229  }
230 
234  function _getAction($sModuleUri, $sActionName)
235  {
236  $sSql = "SELECT
237  `id`,
238  `module_uri`,
239  `name`,
240  `title`,
241  `default_group`
242  FROM `sys_privacy_actions` AS `ta`
243  WHERE `module_uri`= ? AND `name`= ?
244  LIMIT 1";
245  return $this->fromCache($this->_sActionCache . $sModuleUri . '_' . $sActionName, 'getRow', $sSql, [$sModuleUri, $sActionName]);
246  }
247 }
process_db_input
process_db_input($sText, $iStripTags=0)
Definition: utils.inc.php:256
ChWsbDb\getParam
getParam($sName, $bCache=true)
Definition: ChWsbDb.php:454
ChWsbPrivacyQuery\_getAction
_getAction($sModuleUri, $sActionName)
Definition: ChWsbPrivacyQuery.php:234
ChWsbPrivacyQuery\$_sGroupMembersCache
$_sGroupMembersCache
Definition: ChWsbPrivacyQuery.php:18
$iMemberId
$iMemberId
Definition: profile.php:91
ChWsbPrivacyQuery\getDefaultValue
getDefaultValue($iOwnerId, $sModuleUri, $sActionName)
Definition: ChWsbPrivacyQuery.php:205
ChWsbPrivacyQuery\$_sObjectCache
$_sObjectCache
Definition: ChWsbPrivacyQuery.php:19
ChWsbPrivacyQuery\$_sGroupsByOwnersCache
$_sGroupsByOwnersCache
Definition: ChWsbPrivacyQuery.php:17
ChWsbPrivacyQuery\addToGroup
addToGroup($iGroupId, $aMemberIds)
Definition: ChWsbPrivacyQuery.php:158
ChWsbPrivacyQuery\getMembersIds
getMembersIds($iGroupId)
Definition: ChWsbPrivacyQuery.php:153
ch_import
ch_import($sClassName, $aModule=array())
Definition: utils.inc.php:1218
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
ChWsbPrivacyQuery
Definition: ChWsbPrivacyQuery.php:11
php
ChWsbPrivacyQuery\$_sActionCache
$_sActionCache
Definition: ChWsbPrivacyQuery.php:20
$iId
$iId
Definition: license.php:15
ChWsbDb\getAll
getAll($sQuery, $aBindings=[], $iFetchType=PDO::FETCH_ASSOC)
Definition: ChWsbDb.php:206
ChWsbPrivacyQuery\$_sFieldOwnerId
$_sFieldOwnerId
Definition: ChWsbPrivacyQuery.php:14
ChWsbPrivacyQuery\getObjectInfo
getObjectInfo($sAction, $iObjectId)
Definition: ChWsbPrivacyQuery.php:41
ChWsbPrivacyQuery\getDefaultValueModule
getDefaultValueModule($sModuleUri, $sActionName)
Definition: ChWsbPrivacyQuery.php:215
ChWsbPrivacyQuery\deleteGroupsById
deleteGroupsById($mixedValues)
Definition: ChWsbPrivacyQuery.php:140
ChWsbDb\fromCache
fromCache($sName, $sFunc)
Definition: ChWsbDb.php:534
ChWsbPrivacyQuery\deleteFromGroup
deleteFromGroup($iGroupId, $aMemberIds)
Definition: ChWsbPrivacyQuery.php:168
ChWsbPrivacyQuery\getGroupsBy
getGroupsBy($aParams)
Definition: ChWsbPrivacyQuery.php:98
ChWsbPrivacyQuery\$_sActionDefaultCache
$_sActionDefaultCache
Definition: ChWsbPrivacyQuery.php:21
ChWsbPrivacyQuery\$_sGroupCache
$_sGroupCache
Definition: ChWsbPrivacyQuery.php:16
ChWsbPrivacyQuery\setDefaultGroup
setDefaultGroup($iOwnerId, $iGroupId)
Definition: ChWsbPrivacyQuery.php:184
ChWsbDb\query
query($sQuery, $aBindings=[])
Definition: ChWsbDb.php:386
ChWsbPrivacyQuery\getDefaultGroup
getDefaultGroup($iOwnerId)
Definition: ChWsbPrivacyQuery.php:179
CH_WSB_PG_ALL
const CH_WSB_PG_ALL
Definition: ChWsbPrivacy.php:12
ChWsbPrivacyQuery\replaceDefaulfValue
replaceDefaulfValue($iOwnerId, $iActionId, $iGroupId)
Definition: ChWsbPrivacyQuery.php:220
ChWsbDb\getOne
getOne($sQuery, $aBindings=[], $iIndex=0)
Definition: ChWsbDb.php:263
ChWsbPrivacyQuery\isGroupMember
isGroupMember($mixedObjectGroupId, $iObjectOwnerId, $iViewerId)
Definition: ChWsbPrivacyQuery.php:53
CH_TAGS_STRIP
const CH_TAGS_STRIP
Definition: utils.inc.php:22
ChWsbDb\fromMemory
& fromMemory($sName, $sFunc)
Definition: ChWsbDb.php:574
ChWsbPrivacyQuery\$_sFieldId
$_sFieldId
Definition: ChWsbPrivacyQuery.php:13
ChWsbPrivacyQuery\getFieldActionTitle
getFieldActionTitle($sModuleUri, $sActionName)
Definition: ChWsbPrivacyQuery.php:225
ChWsbPrivacyQuery\__construct
__construct($sTable='', $sFieldId='', $sFieldOwnerId='')
Definition: ChWsbPrivacyQuery.php:26
ChWsbDb\cleanCache
cleanCache($sName)
Definition: ChWsbDb.php:565
$sAction
$sAction
Definition: categories.php:274
empty
Attr AllowedRel this is empty
Definition: Attr.AllowedRel.txt:7
ChWsbDb
Definition: ChWsbDb.php:13
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
ChWsbPrivacyQuery\getActions
getActions($iOwnerId)
Definition: ChWsbPrivacyQuery.php:189
ChWsbPrivacyQuery\$_sTable
$_sTable
Definition: ChWsbPrivacyQuery.php:12