BxDolPrivacy.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  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 function actionLoadGroupCustom()
  177. {
  178. $iProfileId = (int)bx_get('profile_id');
  179. $iContentId = (int)bx_get('content_id');
  180. $iGroupId = (int)bx_get('group_id');
  181. $oForm = BxDolForm::getObjectInstance($this->_sFormUsers, $this->_sFormDisplayUsersSelect);
  182. return echoJson(array('eval' => $this->getJsObjectName() . '.onSelectGroup(oData);', 'content' => $oForm->getElementGroupCustom(array(
  183. 'profile_id' => $iProfileId,
  184. 'content_id' => $iContentId,
  185. 'object' => $this->_sObject,
  186. 'group_id' => $iGroupId
  187. ))));
  188. }
  189. public function actionSelectGroup()
  190. {
  191. $aValues = array(
  192. 'profile_id' => (int)bx_get('profile_id'),
  193. 'content_id' => (int)bx_get('content_id'),
  194. 'object' => $this->_sObject,
  195. 'group_id' => (int)bx_get('group_id')
  196. );
  197. $aParams = array(
  198. 'popup_only' => (bool)bx_get('popup_only')
  199. );
  200. if(!isset($this->_aGroupsSettings[$aValues['group_id']]))
  201. return echoJson(array());
  202. if($aValues['profile_id'] != bx_get_logged_profile_id())
  203. return echoJson(array('msg' => _t('_sys_ps_ferr_incorrect_gc_owner')));
  204. if(($mixedResult = $this->isSelectGroupCustom($aValues)) !== true)
  205. return echoJson(array('msg' => $mixedResult));
  206. return echoJson($this->getSelectGroup($aValues, $aParams));
  207. }
  208. public function actionUsersList()
  209. {
  210. $iGroup = (int)bx_get('group');
  211. if(!isset($this->_aGroupsSettings[$iGroup]))
  212. return echoJson(array());
  213. $oConnection = BxDolConnection::getObjectInstance($this->_aGroupsSettings[$iGroup]['connection']);
  214. if(!$oConnection)
  215. return echoJson(array());
  216. $iProfileId = bx_get_logged_profile_id();
  217. $aConnectedIds = $oConnection->getConnectedContent($iProfileId, true);
  218. if(empty($aConnectedIds) || !is_array($aConnectedIds))
  219. return echoJson(array());
  220. $sTerm = bx_get('term');
  221. $aProfiles = BxDolService::call('system', 'profiles_search', array($sTerm), 'TemplServiceProfiles');
  222. if(empty($aProfiles))
  223. return echoJson(array());
  224. $aResult = array();
  225. foreach($aProfiles as $aProfile)
  226. if(in_array($aProfile['value'], $aConnectedIds))
  227. $aResult[] = $aProfile;
  228. echoJson($aResult);
  229. }
  230. public function getGroupSettings($iGroup)
  231. {
  232. if(empty($this->_aGroupsSettings[$iGroup]) || !is_array($this->_aGroupsSettings[$iGroup]))
  233. return false;
  234. return $this->_aGroupsSettings[$iGroup];
  235. }
  236. public function isGroupsCustom()
  237. {
  238. $aGroups = $this->_oDb->getGroupsBy(array('type' => 'active_list'));
  239. return in_array(BX_DOL_PG_FRIENDS_SELECTED, $aGroups) || in_array(BX_DOL_PG_RELATIONS_SELECTED, $aGroups);
  240. }
  241. public function getGroupCustom($aParams)
  242. {
  243. return $this->_oDb->getGroupCustom($aParams);
  244. }
  245. public function updateGroupCustom($aParamsSet, $aParamsWhere)
  246. {
  247. return $this->_oDb->updateGroupCustom($aParamsSet, $aParamsWhere);
  248. }
  249. public function deleteGroupCustom($aParamsWhere)
  250. {
  251. return $this->_oDb->deleteGroupCustom($aParamsWhere);
  252. }
  253. public function associateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId)
  254. {
  255. return $this->updateGroupCustom(array('content_id' => $iContentId), array(
  256. 'profile_id' => $iProfileId,
  257. 'content_id' => 0,
  258. 'object' => $this->_sObject,
  259. 'group_id' => $iGroupId
  260. ));
  261. }
  262. public function reassociateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId)
  263. {
  264. $aGroupCustom = $this->getGroupCustom(array(
  265. 'type' => 'pco',
  266. 'profile_id' => $iProfileId,
  267. 'content_id' => $iContentId,
  268. 'object' => $this->_sObject
  269. ));
  270. if(!empty($aGroupCustom) && is_array($aGroupCustom) && $aGroupCustom['group_id'] != $iGroupId) {
  271. $this->deleteGroupCustom(array('id' => $aGroupCustom['id']));
  272. $this->associateGroupCustomWithContent($iProfileId, $iContentId, $iGroupId);
  273. }
  274. }
  275. public function deleteGroupCustomByContentId($iContentId)
  276. {
  277. return $this->_oDb->deleteGroupCustom(array('content_id' => $iContentId, 'object' => $this->_sObject));
  278. }
  279. public function deleteGroupCustomByProfileId($iProfileId)
  280. {
  281. $aGroups = $this->_oDb->getGroupCustom(array('type' => 'profile_id', 'profile_id' => $iProfileId));
  282. if(empty($aGroups) || !is_array($aGroups))
  283. return true;
  284. foreach($aGroups as $aGroup)
  285. $this->_oDb->deleteGroupCustom(array('id' => $aGroup['id']));
  286. return true;
  287. }
  288. public function addDynamicGroups($aValues, $iOwnerId, $aParams)
  289. {
  290. if (isset($aParams['dynamic_groups']) && is_array($aParams['dynamic_groups']))
  291. $aValues = array_merge($aValues, $aParams['dynamic_groups']);
  292. return $aValues;
  293. }
  294. public function addAclGroups($aValues, $iOwnerId, $aParams)
  295. {
  296. $aCheck = checkActionModule($iOwnerId, 'show membership levels in privacy groups', 'system', false);
  297. if($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  298. return $aValues;
  299. $aLevels = BxDolAcl::getInstance()->getMemberships(false, true, true, true);
  300. if(empty($aLevels) || !is_array($aLevels))
  301. return $aValues;
  302. $aValues[] = array('type' => 'group_header', 'value' => mb_strtoupper(_t('_sys_privacy_groups_acl')));
  303. foreach($aLevels as $iId => $sTitle)
  304. $aValues[] = array('key' => 'ml' . $iId, 'value' => $sTitle);
  305. $aValues[] = array('type' => 'group_end');
  306. return $aValues;
  307. }
  308. public function addSpaces($aValues, $iOwnerId, $aParams)
  309. {
  310. if (!$this->_aObject['spaces'])
  311. return $aValues;
  312. if (!($oProfile = BxDolProfile::getInstance($iOwnerId)))
  313. return $aValues;
  314. if (!($aModules = BxDolModuleQuery::getInstance()->getModules()))
  315. return $aValues;
  316. $bProfileProcessed = true; // don't allow to post to profiles for now
  317. $aExcludeModules = explode(',', getParam('sys_hide_post_to_context_for_privacy'));
  318. foreach ($aModules as $aModule) {
  319. if (!$aModule['enabled'])
  320. continue;
  321. if ('all' != $this->_aObject['spaces'] && false === stripos($this->_aObject['spaces'], $aModule['name']))
  322. continue;
  323. if (!BxDolRequest::serviceExists($aModule['name'], 'act_as_profile'))
  324. continue;
  325. if (in_array($aModule['name'], $aExcludeModules))
  326. continue;
  327. $bActAsProfile = BxDolService::call($aModule['name'], 'act_as_profile');
  328. if ($bActAsProfile && $bProfileProcessed)
  329. continue;
  330. if ($bActAsProfile)
  331. $bProfileProcessed = true;
  332. $a = BxDolService::call($aModule['name'], 'get_participating_profiles', array($oProfile->id()));
  333. $aSpaces = array();
  334. foreach ($a as $iProfileId) {
  335. if (!($o = BxDolProfile::getInstance($iProfileId)))
  336. continue;
  337. $aSpaces[-$iProfileId] = array('key' => -$iProfileId, 'value' => $o->getDisplayName());
  338. }
  339. if ($aSpaces) {
  340. $aItemStart = array(array('type' => 'group_header', 'value' => mb_strtoupper(BxDolService::call($aModule['name'], 'get_space_title'))));
  341. $aItemEnd = array(array('type' => 'group_end'));
  342. $aValues = array_merge($aValues, $aItemStart, array_values($aSpaces), $aItemEnd);
  343. }
  344. }
  345. return $aValues;
  346. }
  347. /**
  348. * Get database field name for action.
  349. *
  350. * @param string $sObject privacy object name.
  351. * @param string $sAction action name.
  352. * @return string with field name.
  353. */
  354. public static function getFieldName($sObject, $sAction = '')
  355. {
  356. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  357. if(empty($oPrivacy))
  358. return '';
  359. if(empty($sAction))
  360. $sAction = $oPrivacy->_aObject['action'];
  361. return $oPrivacy->convertActionToField($sAction);
  362. }
  363. /**
  364. * Get necessary condition array to use privacy in search classes
  365. * @param $mixedGroupId group ID or array of group IDs
  366. * @return array of conditions, for now with 'restriction' part only is returned
  367. */
  368. public function getContentByGroupAsCondition($mixedGroupId)
  369. {
  370. $aResult = array(
  371. 'restriction' => array (
  372. 'privacy_' . $this->_sObject => array(
  373. 'value' => $mixedGroupId,
  374. 'field' => $this->convertActionToField($this->_aObject['action']),
  375. 'operator' => is_array($mixedGroupId) ? 'in' : '=',
  376. 'table' => $this->_aObject['table'],
  377. ),
  378. ),
  379. );
  380. bx_alert('system', 'privacy_condition', 0, false, array(
  381. 'group_id' => $mixedGroupId,
  382. 'field' => $this->convertActionToField($this->_aObject['action']),
  383. 'object' => $this->_aObject,
  384. 'privacy_object' => $this,
  385. 'result' => &$aResult
  386. )
  387. );
  388. return $aResult;
  389. }
  390. /**
  391. * Get necessary condition array to use privacy in search classes
  392. * @param $iProfileIdOwner owner profile ID
  393. * @return array of conditions, for now with 'restriction' part only is returned
  394. */
  395. public function getContentPublicAsCondition($iProfileIdOwner = 0, $aCustomGroups = array())
  396. {
  397. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  398. if($mixedPrivacyGroups === true)
  399. return array();
  400. return $this->getContentByGroupAsCondition($mixedPrivacyGroups);
  401. }
  402. /**
  403. * Get necessary parts of SQL query to use privacy in other queries
  404. * @param $mixedGroupId group ID or array of group IDs
  405. * @return array of SQL string parts, for now 'where' part only is returned
  406. */
  407. public function getContentByGroupAsSQLPart($mixedGroupId)
  408. {
  409. $sField = $this->convertActionToField($this->_aObject['action']);
  410. return $this->_oDb->getContentByGroupAsSQLPart($sField, $mixedGroupId);
  411. }
  412. /**
  413. * Get necessary parts of SQL query to use privacy in other queries
  414. * @param $iProfileIdOwner owner profile ID
  415. * @return array of SQL string parts, for now 'where' part only is returned
  416. */
  417. public function getContentPublicAsSQLPart($iProfileIdOwner = 0, $aCustomGroups = array())
  418. {
  419. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  420. if($mixedPrivacyGroups === true)
  421. return array();
  422. return $this->getContentByGroupAsSQLPart($mixedPrivacyGroups);
  423. }
  424. /**
  425. * Check whether the viewer can make requested action.
  426. *
  427. * @param integer $iObjectId object ID the action to be performed with.
  428. * @param integer $iViewerId viewer ID.
  429. * @return boolean result of operation.
  430. */
  431. public function check($iObjectId, $iViewerId = 0)
  432. {
  433. $aObject = $this->getObjectInfo($this->convertActionToField($this->_aObject['action']), $iObjectId);
  434. $bRv = $this->_check($iObjectId, $iViewerId, $aObject);
  435. bx_alert('system', 'check_privacy', 0, 0, array(
  436. 'object_id' => $iObjectId,
  437. 'viewer_id' => $iViewerId,
  438. 'object' => $aObject,
  439. 'result' => &$bRv
  440. ));
  441. return $bRv;
  442. }
  443. public function checkSpace($aObject, $iViewerId)
  444. {
  445. $oProfile = BxDolProfile::getInstance(-$aObject['group_id']);
  446. if (!$oProfile)
  447. return false;
  448. return CHECK_ACTION_RESULT_ALLOWED === BxDolService::call($oProfile->getModule(), 'check_space_privacy', array($oProfile->getContentId()));
  449. }
  450. public function checkMeOnly($iOwnerId, $iViewerId)
  451. {
  452. return false;
  453. }
  454. public function checkPublic($iOwnerId, $iViewerId)
  455. {
  456. return true;
  457. }
  458. public function checkMembers($iOwnerId, $iViewerId)
  459. {
  460. return isMember();
  461. }
  462. public function checkFriends($iOwnerId, $iViewerId)
  463. {
  464. return BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iOwnerId, $iViewerId, true);
  465. }
  466. public function checkFriendsSelectedByObject($aObject, $iViewerId)
  467. {
  468. if(!$this->checkFriends($aObject['owner_id'], $iViewerId))
  469. return false;
  470. $aGroupCustom = $this->getGroupCustom(array(
  471. 'type' => 'pcog_ext',
  472. 'profile_id' => $aObject['owner_id'],
  473. 'content_id' => $aObject['id'],
  474. 'object' => $this->_sObject,
  475. 'group_id' => $aObject['group_id']
  476. ));
  477. return !empty($aGroupCustom['members']) && is_array($aGroupCustom['members']) && in_array($iViewerId, $aGroupCustom['members']);
  478. }
  479. public function checkRelations($iOwnerId, $iViewerId)
  480. {
  481. return BxDolConnection::getObjectInstance('sys_profiles_relations')->isConnected($iOwnerId, $iViewerId, true);
  482. }
  483. public function checkRelationsSelectedByObject($aObject, $iViewerId)
  484. {
  485. if(!$this->checkRelations($aObject['owner_id'], $iViewerId))
  486. return false;
  487. $aGroupCustom = $this->getGroupCustom(array(
  488. 'type' => 'pcog_ext',
  489. 'profile_id' => $aObject['owner_id'],
  490. 'content_id' => $aObject['id'],
  491. 'object' => $this->_sObject,
  492. 'group_id' => $aObject['group_id']
  493. ));
  494. return !empty($aGroupCustom['members']) && is_array($aGroupCustom['members']) && in_array($iViewerId, $aGroupCustom['members']);
  495. }
  496. public function checkCustomByObject($aObject, $iViewerId)
  497. {
  498. return '';
  499. }
  500. public function setTableFieldAuthor($sValue)
  501. {
  502. $this->_aObject['table_field_author'] = $sValue;
  503. $this->_oDb->init($this->_aObject);
  504. }
  505. protected function getObjectInfo($sAction, $iObjectId)
  506. {
  507. return $this->_oDb->getObjectInfo($sAction, $iObjectId);
  508. }
  509. protected function getPrivacyGroupsForContentPublic($iProfileIdOwner = 0, $aCustomGroups = array())
  510. {
  511. $aGroups = array(BX_DOL_PG_ALL);
  512. if(isLogged()) {
  513. $iProfileIdLogged = bx_get_logged_profile_id();
  514. if($iProfileIdLogged == $iProfileIdOwner)
  515. return true;
  516. $aGroups[] = BX_DOL_PG_MEMBERS;
  517. if($iProfileIdOwner && $this->checkFriends($iProfileIdOwner, $iProfileIdLogged))
  518. $aGroups[] = BX_DOL_PG_FRIENDS;
  519. }
  520. return array_merge($aGroups, $aCustomGroups);
  521. }
  522. protected function getCheckMethod($s)
  523. {
  524. if(substr($s, 0, 1) != '@')
  525. return false;
  526. return bx_gen_method_name(str_replace('@', 'check_', $s));
  527. }
  528. protected function convertActionToField($sAction)
  529. {
  530. return 'allow_' . strtolower(str_replace(' ', '-', $sAction)) . '_to';
  531. }
  532. /**
  533. * Check whethere viewer is a member of dynamic group.
  534. *
  535. * @param mixed $mixedGroupId dynamic group ID.
  536. * @param integer $iObjectOwnerId object owner ID.
  537. * @param integer $iViewerId viewer ID.
  538. * @return boolean result of operation.
  539. */
  540. protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewerId, $iObjectId)
  541. {
  542. return false;
  543. }
  544. /**
  545. * get privacy groups for getGroupChooser
  546. */
  547. protected function getGroups()
  548. {
  549. $aValues = array();
  550. $aGroups = $this->_oDb->getGroupsBy(array('type' => 'active'));
  551. foreach($aGroups as $aGroup) {
  552. if((int)$aGroup['active'] == 0 || in_array($aGroup['id'], $this->_aGroupsExclude))
  553. continue;
  554. $aValues[] = array('key' => $aGroup['id'], 'value' => _t($aGroup['title']));
  555. }
  556. return $aValues;
  557. }
  558. /**
  559. * Check whethere custom group is allowed in current circumstances.
  560. * NOTE. Can be overwritten if it's needed.
  561. *
  562. * @param type $aParams an array of parameters.
  563. * @return boolean result of operation.
  564. */
  565. protected function isSelectGroupCustom($aParams)
  566. {
  567. return true;
  568. }
  569. /**
  570. * Check whether the viewer can make requested action.
  571. *
  572. * @param integer $iObjectId object ID the action to be performed with.
  573. * @param integer $iViewerId viewer ID.
  574. * @return boolean result of operation.
  575. */
  576. protected function _check($iObjectId, $iViewerId, $aObject)
  577. {
  578. if(empty($iViewerId))
  579. $iViewerId = (int)bx_get_logged_profile_id();
  580. if(empty($aObject) || !is_array($aObject))
  581. return false;
  582. if($aObject['group_id'] == BX_DOL_PG_HIDDEN)
  583. return false;
  584. if(isAdmin() || $iViewerId == $aObject['owner_id'])
  585. return true;
  586. if(strncmp($aObject['group_id'], 'ml', 2) === 0) {
  587. $iLevel = (int)substr($aObject['group_id'], 2);
  588. return (bool)BxDolAcl::getInstance()->isMemberLevelInSet(array($iLevel), $iViewerId);
  589. }
  590. if($aObject['group_id'] < 0)
  591. return $this->checkSpace($aObject, $iViewerId);
  592. $aGroup = $this->_oDb->getGroupsBy(array('type' => 'id', 'id' => $aObject['group_id']));
  593. if(!empty($aGroup) && is_array($aGroup) && (int)$aGroup['active'] == 1 && !empty($aGroup['check'])) {
  594. $sCheckMethod = $this->getCheckMethod($aGroup['check']);
  595. if(method_exists($this, $sCheckMethod) && $this->$sCheckMethod((substr($sCheckMethod, -8) == 'ByObject' ? $aObject : $aObject['owner_id']), $iViewerId))
  596. return true;
  597. }
  598. return $this->isDynamicGroupMember($aObject['group_id'], $aObject['owner_id'], $iViewerId, $iObjectId);
  599. }
  600. }
  601. /** @} */