BxDolPrivacy.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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_CUSTOM', '9');
  18. define('BX_DOL_PG_DEFAULT', BX_DOL_PG_ALL);
  19. /**
  20. * Privacy settings for any content.
  21. *
  22. * Integration of the content with privacy engine allows site member
  23. * to organize the access to his content.
  24. *
  25. * In addition to regular privacy groups (Public, Friends), spaces are supported.
  26. * When some space (usually some another profile) is specified as privacy,
  27. * then another profile visibility is used to check the privacy.
  28. *
  29. * Related classes:
  30. * BxDolPrivacyQuery - database queries.
  31. *
  32. * Example of usage:
  33. * 1. Register your privacy actions in `sys_privacy_actions` database table.
  34. * 2. Add one privacy field(with INT type) in the table with your items for each action.
  35. * For example, for action 'comment', the field name should be 'allow_comment_to'.
  36. * 3. Add group choosers for necessary actions in the form, which is used to add new items.
  37. * @code
  38. * $oPrivacy = new BxDolPrivacy();
  39. * $oPrivacy->getGroupChooser($iItemOwnerId, $sModuleUri, $sModuleAction);
  40. * @endcode
  41. *
  42. * 4. Check privacy when any user tries to view an item.
  43. * @code
  44. * $oPrivacy = new BxDolPrivacy($sTable, $sFieldId, $sFieldOwnerId);
  45. * if($oPrivacy->check($sAction, $iObjectId, $iViewerId)) {
  46. * //show necessary content
  47. * }
  48. * @endcode
  49. *
  50. * @see an example of integration in BoonEx modules, for example: Posts
  51. *
  52. *
  53. * Memberships/ACL:
  54. * Doesn't depend on user's membership.
  55. *
  56. *
  57. * Alerts:
  58. * no alerts available
  59. *
  60. */
  61. class BxDolPrivacy extends BxDolFactory implements iBxDolFactoryObject
  62. {
  63. protected $_oDb;
  64. protected $_sObject;
  65. protected $_aObject;
  66. protected $_aGroupsSettings;
  67. protected $_aGroupsExclude;
  68. protected $_sFormUsers;
  69. protected $_sFormDisplayUsersSelect;
  70. /**
  71. * Constructor
  72. * @param $aObject array of grid options
  73. */
  74. protected function __construct($aObject)
  75. {
  76. parent::__construct();
  77. $this->_aObject = $aObject;
  78. $this->_sObject = $aObject['object'];
  79. $this->_oDb = new BxDolPrivacyQuery();
  80. $this->_oDb->init($this->_aObject);
  81. $this->_aGroupsSettings = array(
  82. BX_DOL_PG_FRIENDS_SELECTED => array('connection' => 'sys_profiles_friends'),
  83. BX_DOL_PG_RELATIONS_SELECTED => array('connection' => 'sys_profiles_relations')
  84. );
  85. $this->_aGroupsExclude = array();
  86. $this->_sFormUsers = 'sys_privacy_group_custom';
  87. $this->_sFormDisplayUsersSelect = 'sys_privacy_group_custom_manage';
  88. }
  89. /**
  90. * Get privacy object instance by object name
  91. * @param $sObject object name
  92. * @return object instance or false on error
  93. */
  94. public static function getObjectInstance($sObject)
  95. {
  96. if(isset($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject]))
  97. return $GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject];
  98. $aObject = BxDolPrivacyQuery::getPrivacyObject($sObject);
  99. if(!$aObject || !is_array($aObject))
  100. return false;
  101. $sClass = 'BxTemplPrivacy';
  102. if(!empty($aObject['override_class_name'])) {
  103. $sClass = $aObject['override_class_name'];
  104. if(!empty($aObject['override_class_file']))
  105. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  106. }
  107. $o = new $sClass($aObject);
  108. return ($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject] = $o);
  109. }
  110. /**
  111. * Get Select element with available groups.
  112. *
  113. * @param string $sObject privacy object name.
  114. * @param integer $iOwnerId object's owner ID.
  115. * @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).
  116. * @return an array with Select element description.
  117. */
  118. public static function getGroupChooser($sObject, $iOwnerId = 0, $aParams = array())
  119. {
  120. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  121. if(empty($oPrivacy))
  122. return array();
  123. $sModule = $oPrivacy->_aObject['module'];
  124. $sAction = $oPrivacy->_aObject['action'];
  125. if($iOwnerId == 0)
  126. $iOwnerId = bx_get_logged_profile_id();
  127. $sValue = $oPrivacy->_oDb->getDefaultGroupByUser($sModule, $sAction, $iOwnerId);
  128. if(empty($sValue))
  129. $sValue = $oPrivacy->_oDb->getDefaultGroup($sModule, $sAction);
  130. $aValues = $oPrivacy->getGroups();
  131. $aValues = $oPrivacy->addDynamicGroups($aValues, $iOwnerId, $aParams);
  132. $aValues = $oPrivacy->addAclGroups($aValues, $iOwnerId, $aParams);
  133. $aValues = $oPrivacy->addSpaces($aValues, $iOwnerId, $aParams);
  134. $sName = $oPrivacy->convertActionToField($sAction);
  135. $sTitle = isset($aParams['title']) && !empty($aParams['title']) ? $aParams['title'] : '';
  136. if(empty($sTitle)) {
  137. $sTitle = $oPrivacy->_oDb->getTitle($sModule, $sAction);
  138. $sTitle = _t(!empty($sTitle) ? $sTitle : '_' . $sName);
  139. }
  140. $bDynamicMode = (isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true) || bx_is_dynamic_request();
  141. return array(
  142. 'type' => 'select',
  143. 'name' => $sName,
  144. 'caption' => $sTitle,
  145. 'value' => $sValue,
  146. 'values' => $aValues,
  147. 'attrs' => array(
  148. 'class' => 'sys-privacy-group',
  149. 'onchange' => 'javascript: ' . $oPrivacy->getJsObjectName() . '.selectGroup(this);'
  150. ),
  151. 'checker' => array(
  152. 'func' => 'avail',
  153. 'error' => _t('_sys_ps_ferr_incorrect_select')
  154. ),
  155. 'db' => array(
  156. 'pass' => 'Int'
  157. ),
  158. 'content' => $oPrivacy->addCssJs($bDynamicMode)
  159. );
  160. }
  161. public static function initGroupChooser($sObject, $iOwnerId = 0, $aParams = array())
  162. {
  163. $sResult = '';
  164. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  165. if(empty($oPrivacy))
  166. return $sResult;
  167. $iOwnerId = !empty($iOwnerId) ? (int)$iOwnerId : bx_get_logged_profile_id();
  168. $iContentId = !empty($aParams['content_id']) ? (int)$aParams['content_id'] : 0;
  169. $iGroupId = !empty($aParams['group_id']) ? (int)$aParams['group_id'] : 0;
  170. $bDynamicMode = (isset($aParams['dynamic_mode']) && (bool)$aParams['dynamic_mode'] === true) || bx_is_dynamic_request();
  171. $sJsCodeAdd = '';
  172. if($oPrivacy->isGroupsCustom())
  173. $sJsCodeAdd = $oPrivacy->getLoadGroupCustom($iOwnerId, $iContentId, $iGroupId, isset($aParams['html_ids']) ? $aParams['html_ids'] : array());
  174. return $oPrivacy->getJsScript($sJsCodeAdd, $bDynamicMode);
  175. }
  176. public static function getIcon($iVisibility)
  177. {
  178. $aIcons =array(
  179. BX_DOL_PG_MEONLY => 'lock',
  180. BX_DOL_PG_ALL => 'globe',
  181. BX_DOL_PG_FRIENDS => 'user-friends',
  182. BX_DOL_PG_FRIENDS => 'user-friends'
  183. );
  184. return isset($aIcons[$iVisibility]) ? $aIcons[$iVisibility] : 'eye';
  185. }
  186. public function actionLoadGroupCustom()
  187. {
  188. $iProfileId = (int)bx_get('profile_id');
  189. $iContentId = (int)bx_get('content_id');
  190. $iGroupId = (int)bx_get('group_id');
  191. $oForm = BxDolForm::getObjectInstance($this->_sFormUsers, $this->_sFormDisplayUsersSelect);
  192. return echoJson(array('eval' => $this->getJsObjectName() . '.onSelectGroup(oData);', 'content' => $oForm->getElementGroupCustom(array(
  193. 'profile_id' => $iProfileId,
  194. 'content_id' => $iContentId,
  195. 'object' => $this->_sObject,
  196. 'group_id' => $iGroupId
  197. ))));
  198. }
  199. public function actionSelectGroup()
  200. {
  201. $aValues = array(
  202. 'profile_id' => (int)bx_get('profile_id'),
  203. 'content_id' => (int)bx_get('content_id'),
  204. 'object' => $this->_sObject,
  205. 'group_id' => (int)bx_get('group_id')
  206. );
  207. $aParams = array(
  208. 'popup_only' => (bool)bx_get('popup_only')
  209. );
  210. if(!isset($this->_aGroupsSettings[$aValues['group_id']]))
  211. return echoJson(array());
  212. if($aValues['profile_id'] != bx_get_logged_profile_id())
  213. return echoJson(array('msg' => _t('_sys_ps_ferr_incorrect_gc_owner')));
  214. if(($mixedResult = $this->isSelectGroupCustom($aValues)) !== true)
  215. return echoJson(array('msg' => $mixedResult));
  216. return echoJson($this->getSelectGroup($aValues, $aParams));
  217. }
  218. public function actionUsersList()
  219. {
  220. $iGroup = (int)bx_get('group');
  221. if(!isset($this->_aGroupsSettings[$iGroup]))
  222. return echoJson(array());
  223. $oConnection = BxDolConnection::getObjectInstance($this->_aGroupsSettings[$iGroup]['connection']);
  224. if(!$oConnection)
  225. return echoJson(array());
  226. $iProfileId = bx_get_logged_profile_id();
  227. $aConnectedIds = $oConnection->getConnectedContent($iProfileId, true);
  228. if(empty($aConnectedIds) || !is_array($aConnectedIds))
  229. return echoJson(array());
  230. $sTerm = bx_get('term');
  231. $aProfiles = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  232. if(empty($aProfiles))
  233. return echoJson(array());
  234. $aResult = array();
  235. foreach($aProfiles as $aProfile)
  236. if(in_array($aProfile['value'], $aConnectedIds))
  237. $aResult[] = $aProfile;
  238. echoJson($aResult);
  239. }
  240. public function getGroupSettings($iGroup)
  241. {
  242. if(empty($this->_aGroupsSettings[$iGroup]) || !is_array($this->_aGroupsSettings[$iGroup]))
  243. return false;
  244. return $this->_aGroupsSettings[$iGroup];
  245. }
  246. public function isGroupsCustom()
  247. {
  248. $aGroups = $this->_oDb->getGroupsBy(array('type' => 'active_list'));
  249. return in_array(BX_DOL_PG_FRIENDS_SELECTED, $aGroups) || in_array(BX_DOL_PG_RELATIONS_SELECTED, $aGroups);
  250. }
  251. public function getGroupsBy($aParams)
  252. {
  253. return $this->_oDb->getGroupsBy($aParams);
  254. }
  255. public function getGroupCustom($aParams)
  256. {
  257. return $this->_oDb->getGroupCustom($aParams);
  258. }
  259. public function updateGroupCustom($aParamsSet, $aParamsWhere)
  260. {
  261. return $this->_oDb->updateGroupCustom($aParamsSet, $aParamsWhere);
  262. }
  263. public function deleteGroupCustom($aParamsWhere)
  264. {
  265. return $this->_oDb->deleteGroupCustom($aParamsWhere);
  266. }
  267. public function associateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId)
  268. {
  269. return $this->updateGroupCustom(array('content_id' => $iContentId), array(
  270. 'profile_id' => $iProfileId,
  271. 'content_id' => 0,
  272. 'object' => $this->_sObject,
  273. 'group_id' => $iGroupId
  274. ));
  275. }
  276. public function reassociateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId)
  277. {
  278. $aGroupCustom = $this->getGroupCustom(array(
  279. 'type' => 'pco',
  280. 'profile_id' => $iProfileId,
  281. 'content_id' => $iContentId,
  282. 'object' => $this->_sObject
  283. ));
  284. if(!empty($aGroupCustom) && is_array($aGroupCustom) && $aGroupCustom['group_id'] != $iGroupId) {
  285. $this->deleteGroupCustom(array('id' => $aGroupCustom['id']));
  286. $this->associateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId);
  287. }
  288. }
  289. public function deleteGroupCustomByContentId($iContentId)
  290. {
  291. return $this->_oDb->deleteGroupCustom(array('content_id' => $iContentId, 'object' => $this->_sObject));
  292. }
  293. public function deleteGroupCustomByProfileId($iProfileId)
  294. {
  295. $aGroups = $this->_oDb->getGroupCustom(array('type' => 'profile_id', 'profile_id' => $iProfileId));
  296. if(empty($aGroups) || !is_array($aGroups))
  297. return true;
  298. foreach($aGroups as $aGroup)
  299. $this->_oDb->deleteGroupCustom(array('id' => $aGroup['id']));
  300. return true;
  301. }
  302. public function addDynamicGroups($aValues, $iOwnerId, $aParams)
  303. {
  304. if (isset($aParams['dynamic_groups']) && is_array($aParams['dynamic_groups']))
  305. $aValues = array_merge($aValues, $aParams['dynamic_groups']);
  306. return $aValues;
  307. }
  308. public function addAclGroups($aValues, $iOwnerId, $aParams)
  309. {
  310. $aCheck = checkActionModule($iOwnerId, 'show membership levels in privacy groups', 'system', false);
  311. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  312. return $aValues;
  313. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true, true, true);
  314. if(empty($aLevels) || !is_array($aLevels))
  315. return $aValues;
  316. $aValues[] = array('type' => 'group_header', 'value' => mb_strtoupper(_t('_sys_privacy_groups_acl')));
  317. foreach($aLevels as $iId => $sTitle)
  318. $aValues[] = array('key' => 'ml' . $iId, 'value' => $sTitle);
  319. $aValues[] = array('type' => 'group_end');
  320. return $aValues;
  321. }
  322. public function addSpaces($aValues, $iOwnerId, $aParams)
  323. {
  324. if (!$this->_aObject['spaces'])
  325. return $aValues;
  326. if (!($oProfile = BxDolProfile::getInstance($iOwnerId)))
  327. return $aValues;
  328. if (!($aModules = BxDolModuleQuery::getInstance()->getModules()))
  329. return $aValues;
  330. $aExcludeModules = explode(',', getParam('sys_hide_post_to_context_for_privacy'));
  331. foreach ($aModules as $aModule) {
  332. if (!$aModule['enabled'])
  333. continue;
  334. if ('all' != $this->_aObject['spaces'] && false === stripos($this->_aObject['spaces'], $aModule['name']))
  335. continue;
  336. if (!BxDolRequest::serviceExists($aModule['name'], 'act_as_profile'))
  337. continue;
  338. if (in_array($aModule['name'], $aExcludeModules))
  339. continue;
  340. $oModule = BxDolModule::getInstance($aModule['name']);
  341. $a = BxDolService::call($aModule['name'], 'get_participating_profiles', array($oProfile->id()));
  342. $aSpaces = array();
  343. foreach ($a as $iProfileId) {
  344. if (!($o = BxDolProfile::getInstance($iProfileId)))
  345. continue;
  346. $aSpaces[-$iProfileId] = array('key' => -$iProfileId, 'value' => $o->getDisplayName());
  347. }
  348. if ($aSpaces) {
  349. $aItemStart = array(array('type' => 'group_header', 'value' => mb_strtoupper(BxDolService::call($aModule['name'], 'get_space_title'))));
  350. $aItemEnd = array(array('type' => 'group_end'));
  351. $aValues = array_merge($aValues, $aItemStart, array_values($aSpaces), $aItemEnd);
  352. }
  353. }
  354. return $aValues;
  355. }
  356. /**
  357. * Get database field name for action.
  358. *
  359. * @param string $sObject privacy object name.
  360. * @param string $sAction action name.
  361. * @return string with field name.
  362. */
  363. public static function getFieldName($sObject, $sAction = '')
  364. {
  365. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  366. if(empty($oPrivacy))
  367. return '';
  368. if(empty($sAction))
  369. $sAction = $oPrivacy->_aObject['action'];
  370. return $oPrivacy->convertActionToField($sAction);
  371. }
  372. /**
  373. * Get necessary condition array to use privacy in search classes
  374. * @param $mixedGroupId group ID or array of group IDs
  375. * @return array of conditions, for now with 'restriction' part only is returned
  376. */
  377. public function getContentByGroupAsCondition($mixedGroupId)
  378. {
  379. $aResult = array(
  380. 'restriction' => array (
  381. 'privacy_' . $this->_sObject => array(
  382. 'value' => $mixedGroupId,
  383. 'field' => $this->convertActionToField($this->_aObject['action']),
  384. 'operator' => is_array($mixedGroupId) ? 'in' : '=',
  385. 'table' => $this->_aObject['table'],
  386. ),
  387. ),
  388. );
  389. bx_alert('system', 'privacy_condition', 0, false, array(
  390. 'group_id' => $mixedGroupId,
  391. 'field' => $this->convertActionToField($this->_aObject['action']),
  392. 'object' => $this->_aObject,
  393. 'privacy_object' => $this,
  394. 'result' => &$aResult
  395. )
  396. );
  397. return $aResult;
  398. }
  399. /**
  400. * Get necessary condition array to use privacy in search classes
  401. * @param $iProfileIdOwner owner profile ID
  402. * @return array of conditions, for now with 'restriction' part only is returned
  403. */
  404. public function getContentPublicAsCondition($iProfileIdOwner = 0, $aCustomGroups = array())
  405. {
  406. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  407. if($mixedPrivacyGroups === true)
  408. return array();
  409. return $this->getContentByGroupAsCondition($mixedPrivacyGroups);
  410. }
  411. /**
  412. * Get necessary parts of SQL query to use privacy in other queries
  413. * @param $mixedGroupId group ID or array of group IDs
  414. * @return array of SQL string parts, for now 'where' part only is returned
  415. */
  416. public function getContentByGroupAsSQLPart($mixedGroupId)
  417. {
  418. $sField = $this->convertActionToField($this->_aObject['action']);
  419. return $this->_oDb->getContentByGroupAsSQLPart($sField, $mixedGroupId);
  420. }
  421. /**
  422. * Get necessary parts of SQL query to use privacy in other queries
  423. * @param $iProfileIdOwner owner profile ID
  424. * @return array of SQL string parts, for now 'where' part only is returned
  425. */
  426. public function getContentPublicAsSQLPart($iProfileIdOwner = 0, $aCustomGroups = array())
  427. {
  428. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  429. if($mixedPrivacyGroups === true)
  430. return array();
  431. return $this->getContentByGroupAsSQLPart($mixedPrivacyGroups);
  432. }
  433. /**
  434. * Check whether the viewer can make requested action.
  435. *
  436. * @param integer $iObjectId object ID the action to be performed with.
  437. * @param integer $iViewerId viewer ID.
  438. * @return boolean result of operation.
  439. */
  440. public function check($iObjectId, $iViewerId = 0)
  441. {
  442. $aObject = $this->getObjectInfo($this->convertActionToField($this->_aObject['action']), $iObjectId);
  443. $bRv = $this->_check($iObjectId, $iViewerId, $aObject);
  444. bx_alert('system', 'check_privacy', 0, 0, array(
  445. 'object_id' => $iObjectId,
  446. 'viewer_id' => $iViewerId,
  447. 'object' => $aObject,
  448. 'object_privacy' => $this->_aObject,
  449. 'result' => &$bRv
  450. ));
  451. return $bRv;
  452. }
  453. public function checkSpace($aObject, $iViewerId)
  454. {
  455. $oProfile = BxDolProfile::getInstance(-$aObject['group_id']);
  456. if (!$oProfile)
  457. return false;
  458. return CHECK_ACTION_RESULT_ALLOWED === BxDolService::call($oProfile->getModule(), 'check_space_privacy', array($oProfile->getContentId()));
  459. }
  460. public function checkMeOnly($iOwnerId, $iViewerId)
  461. {
  462. return false;
  463. }
  464. public function checkPublic($iOwnerId, $iViewerId)
  465. {
  466. return true;
  467. }
  468. public function checkMembers($iOwnerId, $iViewerId)
  469. {
  470. return isMember();
  471. }
  472. public function checkFriends($iOwnerId, $iViewerId)
  473. {
  474. return BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iOwnerId, $iViewerId, true);
  475. }
  476. public function checkFriendsSelectedByObject($aObject, $iViewerId)
  477. {
  478. if(!$this->checkFriends($aObject['owner_id'], $iViewerId))
  479. return false;
  480. $aGroupCustom = $this->getGroupCustom(array(
  481. 'type' => 'pcog_ext',
  482. 'profile_id' => $aObject['owner_id'],
  483. 'content_id' => $aObject['id'],
  484. 'object' => $this->_sObject,
  485. 'group_id' => $aObject['group_id']
  486. ));
  487. return !empty($aGroupCustom['members']) && is_array($aGroupCustom['members']) && in_array($iViewerId, $aGroupCustom['members']);
  488. }
  489. public function checkRelations($iOwnerId, $iViewerId)
  490. {
  491. return BxDolConnection::getObjectInstance('sys_profiles_relations')->isConnected($iOwnerId, $iViewerId, true);
  492. }
  493. public function checkRelationsSelectedByObject($aObject, $iViewerId)
  494. {
  495. if(!$this->checkRelations($aObject['owner_id'], $iViewerId))
  496. return false;
  497. $aGroupCustom = $this->getGroupCustom(array(
  498. 'type' => 'pcog_ext',
  499. 'profile_id' => $aObject['owner_id'],
  500. 'content_id' => $aObject['id'],
  501. 'object' => $this->_sObject,
  502. 'group_id' => $aObject['group_id']
  503. ));
  504. return !empty($aGroupCustom['members']) && is_array($aGroupCustom['members']) && in_array($iViewerId, $aGroupCustom['members']);
  505. }
  506. public function checkCustomByObject($aObject, $iViewerId)
  507. {
  508. return '';
  509. }
  510. public function setTableFieldAuthor($sValue)
  511. {
  512. $this->_aObject['table_field_author'] = $sValue;
  513. $this->_oDb->init($this->_aObject);
  514. }
  515. protected function getObjectInfo($sAction, $iObjectId)
  516. {
  517. return $this->_oDb->getObjectInfo($sAction, $iObjectId);
  518. }
  519. protected function getPrivacyGroupsForContentPublic($iProfileIdOwner = 0, $aCustomGroups = array())
  520. {
  521. $aGroups = array(BX_DOL_PG_ALL);
  522. if(isLogged()) {
  523. $iProfileIdLogged = bx_get_logged_profile_id();
  524. if($iProfileIdLogged == $iProfileIdOwner)
  525. return true;
  526. $aGroups[] = BX_DOL_PG_MEMBERS;
  527. if($iProfileIdOwner && $this->checkFriends($iProfileIdOwner, $iProfileIdLogged))
  528. $aGroups[] = BX_DOL_PG_FRIENDS;
  529. }
  530. return array_merge($aGroups, $aCustomGroups);
  531. }
  532. protected function getCheckMethod($s)
  533. {
  534. if(substr($s, 0, 1) != '@')
  535. return false;
  536. return bx_gen_method_name(str_replace('@', 'check_', $s));
  537. }
  538. protected function convertActionToField($sAction)
  539. {
  540. return 'allow_' . strtolower(str_replace(' ', '-', $sAction)) . '_to';
  541. }
  542. /**
  543. * Check whethere viewer is a member of dynamic group.
  544. *
  545. * @param mixed $mixedGroupId dynamic group ID.
  546. * @param integer $iObjectOwnerId object owner ID.
  547. * @param integer $iViewerId viewer ID.
  548. * @return boolean result of operation.
  549. */
  550. protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewerId, $iObjectId)
  551. {
  552. return false;
  553. }
  554. /**
  555. * get privacy groups for getGroupChooser
  556. */
  557. protected function getGroups()
  558. {
  559. $aValues = array();
  560. $aGroups = $this->_oDb->getGroupsBy(array('type' => 'active'));
  561. foreach($aGroups as $aGroup) {
  562. if((int)$aGroup['active'] == 0 || in_array($aGroup['id'], $this->_aGroupsExclude))
  563. continue;
  564. $aValues[] = array('key' => $aGroup['id'], 'value' => _t($aGroup['title']));
  565. }
  566. return $aValues;
  567. }
  568. /**
  569. * Check whethere custom group is allowed in current circumstances.
  570. * NOTE. Can be overwritten if it's needed.
  571. *
  572. * @param type $aParams an array of parameters.
  573. * @return boolean result of operation.
  574. */
  575. protected function isSelectGroupCustom($aParams)
  576. {
  577. return true;
  578. }
  579. /**
  580. * Check whether the viewer can make requested action.
  581. *
  582. * @param integer $iObjectId object ID the action to be performed with.
  583. * @param integer $iViewerId viewer ID.
  584. * @return boolean result of operation.
  585. */
  586. protected function _check($iObjectId, $iViewerId, $aObject)
  587. {
  588. if(empty($iViewerId))
  589. $iViewerId = (int)bx_get_logged_profile_id();
  590. if(empty($aObject) || !is_array($aObject))
  591. return false;
  592. if($aObject['group_id'] == BX_DOL_PG_HIDDEN)
  593. return false;
  594. $aViewer = BxDolProfileQuery::getInstance()->getInfoById($iViewerId);
  595. if(($aViewer && isAdmin($aViewer['account_id'])) || $iViewerId == $aObject['owner_id'])
  596. return true;
  597. if(strncmp($aObject['group_id'], 'ml', 2) === 0) {
  598. $iLevel = (int)substr($aObject['group_id'], 2);
  599. return (bool)BxDolAcl::getInstance()->isMemberLevelInSet(array($iLevel), $iViewerId);
  600. }
  601. if($aObject['group_id'] < 0)
  602. return $this->checkSpace($aObject, $iViewerId);
  603. $aGroup = $this->_oDb->getGroupsBy(array('type' => 'id', 'id' => $aObject['group_id']));
  604. if(!empty($aGroup) && is_array($aGroup) && (int)$aGroup['active'] == 1 && !empty($aGroup['check'])) {
  605. $sCheckMethod = $this->getCheckMethod($aGroup['check']);
  606. if(method_exists($this, $sCheckMethod) && $this->$sCheckMethod((substr($sCheckMethod, -8) == 'ByObject' ? $aObject : $aObject['owner_id']), $iViewerId))
  607. return true;
  608. }
  609. return $this->isDynamicGroupMember($aObject['group_id'], $aObject['owner_id'], $iViewerId, $iObjectId);
  610. }
  611. }
  612. /** @} */