BxDolPrivacy.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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. define('BX_DOL_PG_HIDDEN', '1');
  10. define('BX_DOL_PG_MEONLY', '2');
  11. define('BX_DOL_PG_ALL', '3');
  12. define('BX_DOL_PG_MEMBERS', '4');
  13. define('BX_DOL_PG_FRIENDS', '5');
  14. define('BX_DOL_PG_FRIENDS_SELECTED', '6');
  15. define('BX_DOL_PG_RELATIONS', '7');
  16. define('BX_DOL_PG_RELATIONS_SELECTED', '8');
  17. define('BX_DOL_PG_MEMBERSHIPS_SELECTED', '9');
  18. define('BX_DOL_PG_CUSTOM', '99');
  19. define('BX_DOL_PG_DEFAULT', BX_DOL_PG_ALL);
  20. /**
  21. * Privacy settings for any content.
  22. *
  23. * Integration of the content with privacy engine allows site member
  24. * to organize the access to his content.
  25. *
  26. * In addition to regular privacy groups (Public, Friends), spaces are supported.
  27. * When some space (usually some another profile) is specified as privacy,
  28. * then another profile visibility is used to check the privacy.
  29. *
  30. * Related classes:
  31. * BxDolPrivacyQuery - database queries.
  32. *
  33. * Example of usage:
  34. * 1. Register your privacy actions in `sys_privacy_actions` database table.
  35. * 2. Add one privacy field(with INT type) in the table with your items for each action.
  36. * For example, for action 'comment', the field name should be 'allow_comment_to'.
  37. * 3. Add group choosers for necessary actions in the form, which is used to add new items.
  38. * @code
  39. * $oPrivacy = new BxDolPrivacy();
  40. * $oPrivacy->getGroupChooser($iItemOwnerId, $sModuleUri, $sModuleAction);
  41. * @endcode
  42. *
  43. * 4. Check privacy when any user tries to view an item.
  44. * @code
  45. * $oPrivacy = new BxDolPrivacy($sTable, $sFieldId, $sFieldOwnerId);
  46. * if($oPrivacy->check($sAction, $iObjectId, $iViewerId)) {
  47. * //show necessary content
  48. * }
  49. * @endcode
  50. *
  51. * @see an example of integration in BoonEx modules, for example: Posts
  52. *
  53. *
  54. * Memberships/ACL:
  55. * Doesn't depend on user's membership.
  56. *
  57. *
  58. * Alerts:
  59. * no alerts available
  60. *
  61. */
  62. class BxDolPrivacy extends BxDolFactory implements iBxDolFactoryObject
  63. {
  64. protected $_oDb;
  65. protected $_sObject;
  66. protected $_aObject;
  67. protected $_aGroupsSettings;
  68. protected $_aGroupsExclude;
  69. protected $_sFormGroupCustom;
  70. protected $_sFormDisplayGcMembers;
  71. protected $_sFormDisplayGcMemberships;
  72. /**
  73. * Constructor
  74. * @param $aObject array of grid options
  75. */
  76. protected function __construct($aObject)
  77. {
  78. parent::__construct();
  79. $this->_aObject = $aObject;
  80. $this->_sObject = $aObject['object'];
  81. $this->_oDb = new BxDolPrivacyQuery();
  82. $this->_oDb->init($this->_aObject);
  83. $this->_aGroupsSettings = [
  84. BX_DOL_PG_FRIENDS_SELECTED => [
  85. 'name' => 'friends_selected',
  86. 'is_allowed' => '',
  87. 'connection' => 'sys_profiles_friends',
  88. 'js_method_create_group' => 'selectMembers',
  89. 'db_table_items' => 'sys_privacy_groups_custom_members',
  90. 'db_field_item' => 'member_id',
  91. 'uri_get_items' => 'users_list',
  92. ],
  93. BX_DOL_PG_RELATIONS => [
  94. 'name' => 'relations',
  95. 'is_allowed' => 'isAllowedRelations',
  96. 'connection' => 'sys_profiles_relations',
  97. ],
  98. BX_DOL_PG_RELATIONS_SELECTED => [
  99. 'name' => 'relations_selected',
  100. 'is_allowed' => 'isAllowedRelations',
  101. 'connection' => 'sys_profiles_relations',
  102. 'js_method_create_group' => 'selectMembers',
  103. 'db_table_items' => 'sys_privacy_groups_custom_members',
  104. 'db_field_item' => 'member_id',
  105. 'uri_get_items' => 'users_list',
  106. ],
  107. BX_DOL_PG_MEMBERSHIPS_SELECTED => [
  108. 'name' => 'memberships_selected',
  109. 'is_allowed' => 'isAllowedMemberships',
  110. 'connection' => '',
  111. 'js_method_create_group' => 'selectMemberships',
  112. 'db_table_items' => 'sys_privacy_groups_custom_memberships',
  113. 'db_field_item' => 'membership_id',
  114. 'uri_get_items' => 'memberships_list',
  115. ]
  116. ];
  117. $this->_aGroupsExclude = array();
  118. $this->_sFormGroupCustom = 'sys_privacy_group_custom';
  119. $this->_sFormDisplayGcMembers = 'sys_privacy_group_custom_members';
  120. $this->_sFormDisplayGcMemberships = 'sys_privacy_group_custom_memberships';
  121. }
  122. /**
  123. * Get privacy object instance by object name
  124. * @param $sObject object name
  125. * @return object instance or false on error
  126. */
  127. public static function getObjectInstance($sObject)
  128. {
  129. if(isset($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject]))
  130. return $GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject];
  131. $aObject = BxDolPrivacyQuery::getPrivacyObject($sObject);
  132. if(!$aObject || !is_array($aObject))
  133. return false;
  134. $sClass = 'BxTemplPrivacy';
  135. if(!empty($aObject['override_class_name'])) {
  136. $sClass = $aObject['override_class_name'];
  137. if(!empty($aObject['override_class_file']))
  138. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  139. }
  140. $o = new $sClass($aObject);
  141. return ($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject] = $o);
  142. }
  143. /**
  144. * Get Select element with available groups.
  145. *
  146. * @param string $sObject privacy object name.
  147. * @param integer $iOwnerId object's owner ID.
  148. * @param array $aParams an array of custom selector's params (dynamic_groups - an array of arrays('key' => group_id, 'value' => group_title), title - the title to be used for generated field).
  149. * @return an array with Select element description.
  150. */
  151. public static function getGroupChooser($sObject, $iOwnerId = 0, $aParams = array())
  152. {
  153. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  154. if(empty($oPrivacy))
  155. return array();
  156. $sModule = $oPrivacy->_aObject['module'];
  157. $sAction = $oPrivacy->_aObject['action'];
  158. if($iOwnerId == 0)
  159. $iOwnerId = bx_get_logged_profile_id();
  160. $sValue = $oPrivacy->_oDb->getDefaultGroupByUser($sModule, $sAction, $iOwnerId);
  161. if(empty($sValue))
  162. $sValue = $oPrivacy->_oDb->getDefaultGroup($sModule, $sAction);
  163. $aValues = $oPrivacy->getGroups($iOwnerId, $aParams);
  164. $aValues = $oPrivacy->addDynamicGroups($aValues, $iOwnerId, $aParams);
  165. if(getParam('sys_enable_post_to_context_for_privacy') == 'on')
  166. $aValues = $oPrivacy->addSpaces($aValues, $iOwnerId, $aParams);
  167. $sName = $oPrivacy->convertActionToField($sAction);
  168. $sTitle = isset($aParams['title']) && !empty($aParams['title']) ? $aParams['title'] : '';
  169. if(empty($sTitle)) {
  170. $sTitle = $oPrivacy->_oDb->getTitle($sModule, $sAction);
  171. $sTitle = _t(!empty($sTitle) ? $sTitle : '_' . $sName);
  172. }
  173. $bDynamicMode = (isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true) || bx_is_dynamic_request();
  174. return array(
  175. 'type' => 'select',
  176. 'name' => $sName,
  177. 'caption' => $sTitle,
  178. 'value' => $sValue,
  179. 'values' => $aValues,
  180. 'attrs' => array(
  181. 'class' => 'sys-privacy-group',
  182. 'onchange' => 'javascript: ' . $oPrivacy->getJsObjectName() . '.selectGroup(this);'
  183. ),
  184. 'checker' => array(
  185. 'func' => 'avail',
  186. 'error' => _t('_sys_ps_ferr_incorrect_select')
  187. ),
  188. 'db' => array(
  189. 'pass' => 'Int'
  190. ),
  191. 'content' => $oPrivacy->addCssJs($bDynamicMode)
  192. );
  193. }
  194. public static function initGroupChooser($sObject, $iOwnerId = 0, $aParams = array())
  195. {
  196. $sResult = '';
  197. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  198. if(empty($oPrivacy))
  199. return $sResult;
  200. $iOwnerId = !empty($iOwnerId) ? (int)$iOwnerId : bx_get_logged_profile_id();
  201. $iContentId = !empty($aParams['content_id']) ? (int)$aParams['content_id'] : 0;
  202. $iGroupId = !empty($aParams['group_id']) ? (int)$aParams['group_id'] : 0;
  203. $bDynamicMode = (isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true) || bx_is_dynamic_request();
  204. $sJsCodeAdd = '';
  205. if($oPrivacy->isGroupsCustom())
  206. $sJsCodeAdd = $oPrivacy->getLoadGroupCustom($iOwnerId, $iContentId, $iGroupId, isset($aParams['html_ids']) ? $aParams['html_ids'] : array());
  207. return $oPrivacy->getJsScript($sJsCodeAdd, $bDynamicMode);
  208. }
  209. public static function initGroupChooserAPI($sObject, $iOwnerId = 0, $aParams = [])
  210. {
  211. $sResult = '';
  212. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  213. if(empty($oPrivacy))
  214. return $sResult;
  215. $iOwnerId = !empty($iOwnerId) ? (int)$iOwnerId : bx_get_logged_profile_id();
  216. $iContentId = !empty($aParams['content_id']) ? (int)$aParams['content_id'] : 0;
  217. $iGroupId = !empty($aParams['group_id']) ? (int)$aParams['group_id'] : 0;
  218. if($oPrivacy->isGroupsCustom() && ($aGroupsSettings = $oPrivacy->getGroupSettings($iGroupId)) !== false) {
  219. $aGroupCustom = $oPrivacy->getGroupCustom([
  220. 'type' => 'pcog_ext',
  221. 'profile_id' => $iOwnerId,
  222. 'content_id' => $iContentId,
  223. 'object' => $sObject,
  224. 'group_id' => $iGroupId,
  225. 'group_items_table' => $aGroupsSettings['db_table_items'],
  226. 'group_items_field' => $aGroupsSettings['db_field_item']
  227. ]);
  228. if(is_array($aGroupCustom) && !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']))
  229. $sResult = implode(',', $aGroupCustom['items']);
  230. }
  231. return $sResult;
  232. }
  233. public static function getIcon($iVisibility)
  234. {
  235. $aIcons =array(
  236. BX_DOL_PG_MEONLY => 'lock',
  237. BX_DOL_PG_ALL => 'globe',
  238. BX_DOL_PG_FRIENDS => 'user-friends',
  239. BX_DOL_PG_FRIENDS => 'user-friends'
  240. );
  241. return isset($aIcons[$iVisibility]) ? $aIcons[$iVisibility] : 'eye';
  242. }
  243. public function actionLoadGroupCustom()
  244. {
  245. $iProfileId = (int)bx_get('profile_id');
  246. $iContentId = (int)bx_get('content_id');
  247. $iGroupId = (int)bx_get('group_id');
  248. $oForm = BxDolForm::getObjectInstance($this->_sFormGroupCustom, $this->_sFormDisplayGcMembers);
  249. return echoJson(array('eval' => $this->getJsObjectName() . '.onSelectGroup(oData);', 'content' => $oForm->getElementGroupCustom(array(
  250. 'profile_id' => $iProfileId,
  251. 'content_id' => $iContentId,
  252. 'object' => $this->_sObject,
  253. 'group_id' => $iGroupId
  254. ))));
  255. }
  256. public function actionSelectMembers()
  257. {
  258. $aValues = array(
  259. 'profile_id' => (int)bx_get('profile_id'),
  260. 'content_id' => (int)bx_get('content_id'),
  261. 'object' => $this->_sObject,
  262. 'group_id' => (int)bx_get('group_id')
  263. );
  264. $aParams = array(
  265. 'popup_only' => (bool)bx_get('popup_only')
  266. );
  267. if(!isset($this->_aGroupsSettings[$aValues['group_id']]))
  268. return echoJson(array());
  269. if($aValues['profile_id'] != bx_get_logged_profile_id())
  270. return echoJson(array('msg' => _t('_sys_ps_ferr_incorrect_gc_owner')));
  271. if(($mixedResult = $this->isSelectGroupCustomUsers($aValues)) !== true)
  272. return echoJson(array('msg' => $mixedResult));
  273. return echoJson($this->getSelectGroup($aValues, $aParams));
  274. }
  275. public function actionSelectMemberships()
  276. {
  277. $aValues = array(
  278. 'profile_id' => (int)bx_get('profile_id'),
  279. 'content_id' => (int)bx_get('content_id'),
  280. 'object' => $this->_sObject,
  281. 'group_id' => (int)bx_get('group_id')
  282. );
  283. $aParams = array(
  284. 'popup_only' => (bool)bx_get('popup_only')
  285. );
  286. if(!isset($this->_aGroupsSettings[$aValues['group_id']]))
  287. return echoJson(array());
  288. if($aValues['profile_id'] != bx_get_logged_profile_id())
  289. return echoJson(array('msg' => _t('_sys_ps_ferr_incorrect_gc_owner')));
  290. if(($mixedResult = $this->isSelectGroupCustomMemberships($aValues)) !== true)
  291. return echoJson(array('msg' => $mixedResult));
  292. return echoJson($this->getSelectMemberships($aValues, $aParams));
  293. }
  294. public function actionUsersList()
  295. {
  296. $iGroup = (int)bx_get('group');
  297. if(!isset($this->_aGroupsSettings[$iGroup]))
  298. return echoJson(array());
  299. $oConnection = BxDolConnection::getObjectInstance($this->_aGroupsSettings[$iGroup]['connection']);
  300. if(!$oConnection)
  301. return echoJson(array());
  302. $iProfileId = bx_get_logged_profile_id();
  303. $aConnectedIds = $oConnection->getConnectedContent($iProfileId, true);
  304. if(empty($aConnectedIds) || !is_array($aConnectedIds))
  305. return echoJson(array());
  306. $sTerm = bx_get('term');
  307. $aProfiles = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  308. if(empty($aProfiles))
  309. return echoJson(array());
  310. $aResult = array();
  311. foreach($aProfiles as $aProfile)
  312. if(in_array($aProfile['value'], $aConnectedIds))
  313. $aResult[] = $aProfile;
  314. echoJson($aResult);
  315. }
  316. public function getGroupSettings($iGroup)
  317. {
  318. if(empty($this->_aGroupsSettings[$iGroup]) || !is_array($this->_aGroupsSettings[$iGroup]))
  319. return false;
  320. return $this->_aGroupsSettings[$iGroup];
  321. }
  322. public function isGroupsCustom()
  323. {
  324. $aGroups = $this->_oDb->getGroupsBy(array('type' => 'active_list'));
  325. return in_array(BX_DOL_PG_FRIENDS_SELECTED, $aGroups) || in_array(BX_DOL_PG_RELATIONS_SELECTED, $aGroups) || in_array(BX_DOL_PG_MEMBERSHIPS_SELECTED, $aGroups);
  326. }
  327. public function getGroupsBy($aParams)
  328. {
  329. return $this->_oDb->getGroupsBy($aParams);
  330. }
  331. public function getGroupCustom($aParams)
  332. {
  333. return $this->_oDb->getGroupCustom($aParams);
  334. }
  335. /**
  336. * Note. Is used when privacy group is processing after the main form submit.
  337. */
  338. public function addGroupCustom($aGroup, $aItems = [])
  339. {
  340. $this->deleteGroupCustom([
  341. 'profile_id' => $aGroup['profile_id'],
  342. 'content_id' => $aGroup['content_id'],
  343. 'object' => $aGroup['object']
  344. ]);
  345. if(empty($aItems) || !is_array($aItems))
  346. return 0;
  347. $iGroupId = (int)$aGroup['group_id'];
  348. $iGroupCustomId = $this->_oDb->insertGroupCustom($aGroup);
  349. if($iGroupCustomId !== false && !empty($aItems) && is_array($aItems)) {
  350. $bMembers = $iGroupId == BX_DOL_PG_FRIENDS_SELECTED || $iGroupId == BX_DOL_PG_RELATIONS_SELECTED;
  351. $bMemberships = $iGroupId == BX_DOL_PG_MEMBERSHIPS_SELECTED;
  352. if(($bMembers || $bMemberships) && ($sType = $bMembers ? 'member' : ($bMemberships ? 'membership' : '')) !== '')
  353. foreach($aItems as $iItem)
  354. $this->_oDb->{'insertGroupCustom' . ucfirst($sType)}(['group_id' => $iGroupCustomId, $sType . '_id' => $iItem]);
  355. }
  356. return $iGroupCustomId;
  357. }
  358. /**
  359. * Note. Is used when privacy group is processing after the main form submit.
  360. */
  361. public function editGroupCustom($aGroup, $aItems = [])
  362. {
  363. $aGroupCustom = $this->getGroupCustom([
  364. 'type' => 'pco',
  365. 'profile_id' => $aGroup['profile_id'],
  366. 'content_id' => $aGroup['content_id'],
  367. 'object' => $this->_sObject
  368. ]);
  369. if(empty($aGroupCustom) || !is_array($aGroupCustom))
  370. return $this->addGroupCustom($aGroup, $aItems);
  371. $iGroupId = (int)$aGroupCustom['group_id'];
  372. $iGroupCustomId = (int)$aGroupCustom['id'];
  373. if($iGroupId != (int)$aGroup['group_id'])
  374. return $this->addGroupCustom($aGroup, $aItems);
  375. $bMembers = $iGroupId == BX_DOL_PG_FRIENDS_SELECTED || $iGroupId == BX_DOL_PG_RELATIONS_SELECTED;
  376. $bMemberships = $iGroupId == BX_DOL_PG_MEMBERSHIPS_SELECTED;
  377. if(($bMembers || $bMemberships) && ($sType = $bMembers ? 'member' : ($bMemberships ? 'membership' : '')) !== '') {
  378. $sTypeUc = ucfirst($sType);
  379. $this->_oDb->{'deleteGroupCustom' . $sTypeUc}(['group_id' => $iGroupCustomId]);
  380. if(!empty($aItems) && is_array($aItems))
  381. foreach($aItems as $iItemId)
  382. $this->_oDb->{'insertGroupCustom' . $sTypeUc}(['group_id' => $iGroupCustomId, $sType . '_id' => $iItemId]);
  383. }
  384. return $iGroupCustomId;
  385. }
  386. public function updateGroupCustom($aParamsSet, $aParamsWhere)
  387. {
  388. return $this->_oDb->updateGroupCustom($aParamsSet, $aParamsWhere);
  389. }
  390. public function deleteGroupCustom($aParamsWhere)
  391. {
  392. return $this->_oDb->deleteGroupCustom($aParamsWhere);
  393. }
  394. public function associateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId)
  395. {
  396. return $this->updateGroupCustom(array('content_id' => $iContentId), array(
  397. 'profile_id' => $iProfileId,
  398. 'content_id' => 0,
  399. 'object' => $this->_sObject,
  400. 'group_id' => $iGroupId
  401. ));
  402. }
  403. public function reassociateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId)
  404. {
  405. $aGroupCustom = $this->getGroupCustom(array(
  406. 'type' => 'pco',
  407. 'profile_id' => $iProfileId,
  408. 'content_id' => $iContentId,
  409. 'object' => $this->_sObject
  410. ));
  411. if(!empty($aGroupCustom) && is_array($aGroupCustom) && $aGroupCustom['group_id'] != $iGroupId) {
  412. $this->deleteGroupCustom(array('id' => $aGroupCustom['id']));
  413. $this->associateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId);
  414. }
  415. }
  416. public function deleteGroupCustomByContentId($iContentId)
  417. {
  418. return $this->_oDb->deleteGroupCustom(array('content_id' => $iContentId, 'object' => $this->_sObject));
  419. }
  420. public function deleteGroupCustomByProfileId($iProfileId, $mixedContentId = false)
  421. {
  422. $aParams = ['type' => 'profile_id', 'profile_id' => $iProfileId];
  423. if($mixedContentId !== false)
  424. $aParams['content_id'] = (int)$mixedContentId;
  425. $aGroups = $this->_oDb->getGroupCustom($aParams);
  426. if(empty($aGroups) || !is_array($aGroups))
  427. return true;
  428. foreach($aGroups as $aGroup)
  429. $this->_oDb->deleteGroupCustom(array('id' => $aGroup['id']));
  430. return true;
  431. }
  432. public function deleteGroupCustomByProfileIdUnused($iProfileId)
  433. {
  434. return $this->deleteGroupCustomByProfileId($iProfileId, 0);
  435. }
  436. public function addDynamicGroups($aValues, $iOwnerId, $aParams)
  437. {
  438. if (isset($aParams['dynamic_groups']) && is_array($aParams['dynamic_groups']))
  439. $aValues = array_merge($aValues, $aParams['dynamic_groups']);
  440. return $aValues;
  441. }
  442. public function addSpaces($aValues, $iOwnerId, $aParams)
  443. {
  444. if (!$this->_aObject['spaces'])
  445. return $aValues;
  446. if (!($oProfile = BxDolProfile::getInstance($iOwnerId)))
  447. return $aValues;
  448. if (!($aModules = BxDolModuleQuery::getInstance()->getModules()))
  449. return $aValues;
  450. $aExcludeModules = explode(',', getParam('sys_hide_post_to_context_for_privacy'));
  451. foreach ($aModules as $aModule) {
  452. if (!$aModule['enabled'])
  453. continue;
  454. if ('all' != $this->_aObject['spaces'] && false === stripos($this->_aObject['spaces'], $aModule['name']))
  455. continue;
  456. if (!BxDolRequest::serviceExists($aModule['name'], 'act_as_profile'))
  457. continue;
  458. if (in_array($aModule['name'], $aExcludeModules))
  459. continue;
  460. $oModule = BxDolModule::getInstance($aModule['name']);
  461. if (bx_srv($aModule['name'], 'act_as_profile') && (!isset($oModule->_oConfig->CNF['ALLOW_AS_CONTEXT']) || $oModule->_oConfig->CNF['ALLOW_AS_CONTEXT'] == false))
  462. continue;
  463. $a = BxDolService::call($aModule['name'], 'get_participating_profiles', array($oProfile->id()));
  464. // for an organization we should treat the organization profile itself as a participant of itself
  465. // to be able to post into an organization's context while being logged as that organization
  466. if ($aModule['name'] == 'bx_organizations' && $oProfile->getModule() == 'bx_organizations') $a = array_merge($a, [$oProfile->id()]);
  467. $aSpaces = array();
  468. foreach ($a as $iProfileId) {
  469. if (!($o = BxDolProfile::getInstance($iProfileId)))
  470. continue;
  471. // check whether a profile is allowed to post this type of content to a context
  472. $oConnectedProfile = BxDolProfile::getInstance($iProfileId);
  473. if (bx_srv($aModule['name'], 'check_allowed_post_in_profile', array($oConnectedProfile->getContentId(), $this->_aObject['module'])) !== CHECK_ACTION_RESULT_ALLOWED)
  474. continue;
  475. $aSpaces[-$iProfileId] = array('key' => -$iProfileId, 'value' => $o->getDisplayName());
  476. }
  477. if ($aSpaces) {
  478. $aItemStart = array(array('type' => 'group_header', 'value' => mb_strtoupper(BxDolService::call($aModule['name'], 'get_space_title'))));
  479. $aItemEnd = array(array('type' => 'group_end'));
  480. $aValues = array_merge($aValues, $aItemStart, array_values($aSpaces), $aItemEnd);
  481. }
  482. }
  483. return $aValues;
  484. }
  485. /**
  486. * Get database field name for action.
  487. *
  488. * @param string $sObject privacy object name.
  489. * @param string $sAction action name.
  490. * @return string with field name.
  491. */
  492. public static function getFieldName($sObject, $sAction = '')
  493. {
  494. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  495. if(empty($oPrivacy))
  496. return '';
  497. if(empty($sAction))
  498. $sAction = $oPrivacy->_aObject['action'];
  499. return $oPrivacy->convertActionToField($sAction);
  500. }
  501. /**
  502. * Get necessary condition array to use privacy in search classes
  503. * @param $mixedGroupId group ID or array of group IDs
  504. * @return array of conditions, for now with 'restriction' part only is returned
  505. */
  506. public function getContentByGroupAsCondition($mixedGroupId)
  507. {
  508. $aResult = array(
  509. 'restriction' => array (
  510. 'privacy_' . $this->_sObject => array(
  511. 'value' => $mixedGroupId,
  512. 'field' => $this->convertActionToField($this->_aObject['action']),
  513. 'operator' => is_array($mixedGroupId) ? 'in' : '=',
  514. 'table' => $this->_aObject['table'],
  515. ),
  516. ),
  517. );
  518. /**
  519. * @hooks
  520. * @hookdef hook-system-privacy_condition 'system', 'privacy_condition' - hook on get necessary condition array to use privacy in search classes
  521. * - $unit_name - equals `profile`
  522. * - $action - equals `privacy_condition`
  523. * - $object_id - not used
  524. * - $sender_id - not used
  525. * - $extra_params - array of additional params with the following array keys:
  526. * - `group_id` - [int] group ID or array of group IDs
  527. * - `field` - [string] field name
  528. * - `object` - [array] array with object privacy
  529. * - `privacy_object` - [array] object privacy
  530. * - `result` - [bool] by ref, on success return true otherwise false, can be overridden in hook processing
  531. * @hook @ref hook-system-privacy_condition
  532. */
  533. bx_alert('system', 'privacy_condition', 0, false, array(
  534. 'group_id' => $mixedGroupId,
  535. 'field' => $this->convertActionToField($this->_aObject['action']),
  536. 'object' => $this->_aObject,
  537. 'privacy_object' => $this,
  538. 'result' => &$aResult
  539. )
  540. );
  541. return $aResult;
  542. }
  543. /**
  544. * Get necessary condition array to use privacy in search classes
  545. * @param $iProfileIdOwner owner profile ID
  546. * @return array of conditions, for now with 'restriction' part only is returned
  547. */
  548. public function getContentPublicAsCondition($iProfileIdOwner = 0, $aCustomGroups = array())
  549. {
  550. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  551. if($mixedPrivacyGroups === true)
  552. return array();
  553. return $this->getContentByGroupAsCondition($mixedPrivacyGroups);
  554. }
  555. /**
  556. * Get necessary parts of SQL query to use privacy in other queries
  557. * @param $mixedGroupId group ID or array of group IDs
  558. * @return array of SQL string parts, for now 'where' part only is returned
  559. */
  560. public function getContentByGroupAsSQLPart($mixedGroupId)
  561. {
  562. $sField = $this->convertActionToField($this->_aObject['action']);
  563. return $this->_oDb->getContentByGroupAsSQLPart($sField, $mixedGroupId);
  564. }
  565. /**
  566. * Get necessary parts of SQL query to use privacy in other queries
  567. * @param $iProfileIdOwner owner profile ID
  568. * @return array of SQL string parts, for now 'where' part only is returned
  569. */
  570. public function getContentPublicAsSQLPart($iProfileIdOwner = 0, $aCustomGroups = array())
  571. {
  572. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  573. if($mixedPrivacyGroups === true)
  574. return array();
  575. return $this->getContentByGroupAsSQLPart($mixedPrivacyGroups);
  576. }
  577. /**
  578. * Get necessary parts of SQL query to use privacy in other queries
  579. * @param $iProfileIdOwner owner profile ID
  580. * @return array of SQL string parts, for now 'where' part only is returned
  581. */
  582. public function getContentPublicAndInContextAsSQLPart($iProfileIdOwner = 0, $aCustomGroups = [], $aCustomContexts = [])
  583. {
  584. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  585. if($mixedPrivacyGroups === true)
  586. return [];
  587. $sField = $this->convertActionToField($this->_aObject['action']);
  588. return $this->_oDb->getContentByGroupAndContextAsSQLPart($sField, $mixedPrivacyGroups, $aCustomContexts);
  589. }
  590. /**
  591. * Check whether the viewer can make requested action.
  592. *
  593. * @param integer $iObjectId object ID the action to be performed with.
  594. * @param integer $iViewerId viewer ID.
  595. * @return boolean result of operation.
  596. */
  597. public function check($iObjectId, $iViewerId = 0)
  598. {
  599. $aObject = $this->getObjectInfo($this->convertActionToField($this->_aObject['action']), $iObjectId);
  600. $bRv = $this->_check($iObjectId, $iViewerId, $aObject);
  601. /**
  602. * @hooks
  603. * @hookdef hook-system-check_privacy 'system', 'check_privacy' - check privacy for object
  604. * - $unit_name - equals `profile`
  605. * - $action - equals `check_privacy`
  606. * - $object_id - not used
  607. * - $sender_id - not used
  608. * - $extra_params - array of additional params with the following array keys:
  609. * - `object_id` - [int] object id
  610. * - `viewer_id` - [int] profile_id for viewer
  611. * - `object` - [array] object privacy
  612. * - `object_privacy` - [array] arry with object privacy
  613. * - `result` - [bool] by ref, on success return true otherwise false, can be overridden in hook processing
  614. * @hook @ref hook-system-check_privacy
  615. */
  616. bx_alert('system', 'check_privacy', 0, 0, array(
  617. 'object_id' => $iObjectId,
  618. 'viewer_id' => $iViewerId,
  619. 'object' => $aObject,
  620. 'object_privacy' => $this->_aObject,
  621. 'result' => &$bRv
  622. ));
  623. return $bRv;
  624. }
  625. public function checkSpace($aObject, $iViewerId)
  626. {
  627. $oProfile = BxDolProfile::getInstance(-$aObject['group_id']);
  628. if (!$oProfile)
  629. return false;
  630. return CHECK_ACTION_RESULT_ALLOWED === BxDolService::call($oProfile->getModule(), 'check_space_privacy', array($oProfile->getContentId()));
  631. }
  632. public function checkMeOnly($iOwnerId, $iViewerId)
  633. {
  634. return false;
  635. }
  636. public function checkPublic($iOwnerId, $iViewerId)
  637. {
  638. return true;
  639. }
  640. public function checkMembers($iOwnerId, $iViewerId)
  641. {
  642. return isMember();
  643. }
  644. public function checkFriends($iOwnerId, $iViewerId)
  645. {
  646. return BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iOwnerId, $iViewerId, true);
  647. }
  648. public function checkFriendsSelectedByObject($aObject, $iViewerId)
  649. {
  650. if(!$this->checkFriends($aObject['owner_id'], $iViewerId))
  651. return false;
  652. $aGroupCustom = $this->getGroupCustom(array(
  653. 'type' => 'pcog_ext',
  654. 'profile_id' => $aObject['owner_id'],
  655. 'content_id' => $aObject['id'],
  656. 'object' => $this->_sObject,
  657. 'group_id' => $aObject['group_id'],
  658. 'group_items_table' => $this->_aGroupsSettings[$aObject['group_id']]['db_table_items'],
  659. 'group_items_field' => $this->_aGroupsSettings[$aObject['group_id']]['db_field_item']
  660. ));
  661. return !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']) && in_array($iViewerId, $aGroupCustom['items']);
  662. }
  663. public function checkRelations($iOwnerId, $iViewerId)
  664. {
  665. return BxDolConnection::getObjectInstance('sys_profiles_relations')->isConnected($iOwnerId, $iViewerId, true);
  666. }
  667. public function checkRelationsSelectedByObject($aObject, $iViewerId)
  668. {
  669. if(!$this->checkRelations($aObject['owner_id'], $iViewerId))
  670. return false;
  671. $aGroupCustom = $this->getGroupCustom(array(
  672. 'type' => 'pcog_ext',
  673. 'profile_id' => $aObject['owner_id'],
  674. 'content_id' => $aObject['id'],
  675. 'object' => $this->_sObject,
  676. 'group_id' => $aObject['group_id'],
  677. 'group_items_table' => $this->_aGroupsSettings[$aObject['group_id']]['db_table_items'],
  678. 'group_items_field' => $this->_aGroupsSettings[$aObject['group_id']]['db_field_item']
  679. ));
  680. return !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']) && in_array($iViewerId, $aGroupCustom['items']);
  681. }
  682. public function checkMembershipsSelectedByObject($aObject, $iViewerId)
  683. {
  684. $aGroupCustom = $this->getGroupCustom(array(
  685. 'type' => 'pcog_ext',
  686. 'profile_id' => $aObject['owner_id'],
  687. 'content_id' => $aObject['id'],
  688. 'object' => $this->_sObject,
  689. 'group_id' => $aObject['group_id'],
  690. 'group_items_table' => $this->_aGroupsSettings[$aObject['group_id']]['db_table_items'],
  691. 'group_items_field' => $this->_aGroupsSettings[$aObject['group_id']]['db_field_item']
  692. ));
  693. return !empty($aGroupCustom['items']) && is_array($aGroupCustom['items']) && BxDolAcl::getInstance()->isMemberLevelInSet($aGroupCustom['items'], $iViewerId);
  694. }
  695. public function checkCustomByObject($aObject, $iViewerId)
  696. {
  697. return '';
  698. }
  699. public function setTableFieldAuthor($sValue)
  700. {
  701. $this->_aObject['table_field_author'] = $sValue;
  702. $this->_oDb->init($this->_aObject);
  703. }
  704. protected function getObjectInfo($sAction, $iObjectId)
  705. {
  706. return $this->_oDb->getObjectInfo($sAction, $iObjectId);
  707. }
  708. protected function getPrivacyGroupsForContentPublic($iProfileIdOwner = 0, $aCustomGroups = array())
  709. {
  710. $aGroups = array(BX_DOL_PG_ALL);
  711. if(isLogged()) {
  712. $iProfileIdLogged = bx_get_logged_profile_id();
  713. if($iProfileIdLogged == $iProfileIdOwner)
  714. return true;
  715. $aGroups[] = BX_DOL_PG_MEMBERS;
  716. if($iProfileIdOwner && $this->checkFriends($iProfileIdOwner, $iProfileIdLogged))
  717. $aGroups[] = BX_DOL_PG_FRIENDS;
  718. }
  719. return array_merge($aGroups, $aCustomGroups);
  720. }
  721. protected function getCheckMethod($s)
  722. {
  723. if(substr($s, 0, 1) != '@')
  724. return false;
  725. return bx_gen_method_name(str_replace('@', 'check_', $s));
  726. }
  727. protected function convertActionToField($sAction)
  728. {
  729. return 'allow_' . strtolower(str_replace(' ', '-', $sAction)) . '_to';
  730. }
  731. /**
  732. * Check whethere viewer is a member of dynamic group.
  733. *
  734. * @param mixed $mixedGroupId dynamic group ID.
  735. * @param integer $iObjectOwnerId object owner ID.
  736. * @param integer $iViewerId viewer ID.
  737. * @return boolean result of operation.
  738. */
  739. protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewerId, $iObjectId)
  740. {
  741. return false;
  742. }
  743. /**
  744. * get privacy groups for getGroupChooser
  745. */
  746. protected function getGroups($iOwnerId = 0, $aParams = [])
  747. {
  748. $aValues = array();
  749. $aGroups = $this->_oDb->getGroupsBy(array('type' => 'active'));
  750. foreach($aGroups as $aGroup) {
  751. $iGroupId = (int)$aGroup['id'];
  752. if((int)$aGroup['active'] == 0 || in_array($iGroupId, $this->_aGroupsExclude))
  753. continue;
  754. if(isset($this->_aGroupsSettings[$iGroupId]) && !empty($this->_aGroupsSettings[$iGroupId]['is_allowed'])) {
  755. $sMethodIsAllowed = $this->_aGroupsSettings[$iGroupId]['is_allowed'];
  756. if(method_exists($this, $sMethodIsAllowed) && $this->$sMethodIsAllowed($iOwnerId) !== true)
  757. continue;
  758. }
  759. $aValues[] = array('key' => $aGroup['id'], 'value' => _t($aGroup['title']));
  760. }
  761. return $aValues;
  762. }
  763. /**
  764. * Check whethere a custom group (based on users list) is allowed in current circumstances.
  765. * NOTE. Can be overwritten if it's needed.
  766. *
  767. * @param type $aParams an array of parameters.
  768. * @return boolean result of operation.
  769. */
  770. protected function isSelectGroupCustomUsers($aParams)
  771. {
  772. return true;
  773. }
  774. /**
  775. * Check whethere a custom group (based on memberships list) is allowed in current circumstances.
  776. * NOTE. Can be overwritten if it's needed.
  777. *
  778. * @param type $aParams an array of parameters.
  779. * @return boolean result of operation.
  780. */
  781. protected function isSelectGroupCustomMemberships($aParams)
  782. {
  783. return true;
  784. }
  785. public function isAllowedRelations($iUserId)
  786. {
  787. return BxDolRelation::isEnabled();
  788. }
  789. public function isAllowedMemberships($iUserId)
  790. {
  791. $aCheck = checkActionModule($iUserId, 'show membership levels in privacy groups', 'system', false);
  792. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  793. }
  794. /**
  795. * Check whether the viewer can make requested action.
  796. *
  797. * @param integer $iObjectId object ID the action to be performed with.
  798. * @param integer $iViewerId viewer ID.
  799. * @return boolean result of operation.
  800. */
  801. protected function _check($iObjectId, $iViewerId, $aObject)
  802. {
  803. if(empty($iViewerId))
  804. $iViewerId = (int)bx_get_logged_profile_id();
  805. if(empty($aObject) || !is_array($aObject))
  806. return false;
  807. if($aObject['group_id'] == BX_DOL_PG_HIDDEN)
  808. return false;
  809. $aViewer = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
  810. if(($aViewer && isAdmin($aViewer['account_id'])) || $iViewerId == $aObject['owner_id'])
  811. return true;
  812. if(strncmp($aObject['group_id'], 'ml', 2) === 0) {
  813. $iLevel = (int)substr($aObject['group_id'], 2);
  814. return (bool)BxDolAcl::getInstance()->isMemberLevelInSet(array($iLevel), $iViewerId);
  815. }
  816. if($aObject['group_id'] < 0)
  817. return $this->checkSpace($aObject, $iViewerId);
  818. $aGroup = $this->_oDb->getGroupsBy(array('type' => 'id', 'id' => $aObject['group_id']));
  819. if(!empty($aGroup) && is_array($aGroup) && (int)$aGroup['active'] == 1 && !empty($aGroup['check'])) {
  820. $sCheckMethod = $this->getCheckMethod($aGroup['check']);
  821. if(method_exists($this, $sCheckMethod) && $this->$sCheckMethod((substr($sCheckMethod, -8) == 'ByObject' ? $aObject : $aObject['owner_id']), $iViewerId))
  822. return true;
  823. }
  824. return $this->isDynamicGroupMember($aObject['group_id'], $aObject['owner_id'], $iViewerId, $iObjectId);
  825. }
  826. }
  827. /** @} */