BxDolPrivacy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident 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_DEFAULT', BX_DOL_PG_ALL);
  15. /**
  16. * @page objects
  17. * @section privacy Privacy
  18. * @ref BxDolPrivacy
  19. */
  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. * Related classes:
  27. * BxDolPrivacyQuery - database queries.
  28. *
  29. * Example of usage:
  30. * 1. Register your privacy actions in `sys_privacy_actions` database table.
  31. * 2. Add one privacy field(with INT type) in the table with your items for each action.
  32. * For example, for action 'comment', the field name should be 'allow_comment_to'.
  33. * 3. Add group choosers for necessary actions in the form, which is used to add new items.
  34. * @code
  35. * $oPrivacy = new BxDolPrivacy();
  36. * $oPrivacy->getGroupChooser($iItemOwnerId, $sModuleUri, $sModuleAction);
  37. * @endcode
  38. *
  39. * 4. Check privacy when any user tries to view an item.
  40. * @code
  41. * $oPrivacy = new BxDolPrivacy($sTable, $sFieldId, $sFieldOwnerId);
  42. * if($oPrivacy->check($sAction, $iObjectId, $iViewerId)) {
  43. * //show necessary content
  44. * }
  45. * @endcode
  46. *
  47. * @see an example of integration in BoonEx modules, for example: Posts
  48. *
  49. *
  50. * Memberships/ACL:
  51. * Doesn't depend on user's membership.
  52. *
  53. *
  54. * Alerts:
  55. * no alerts available
  56. *
  57. */
  58. class BxDolPrivacy extends BxDolFactory implements iBxDolFactoryObject
  59. {
  60. protected $_oDb;
  61. protected $_sObject;
  62. protected $_aObject;
  63. /**
  64. * Constructor
  65. * @param $aObject array of grid options
  66. */
  67. protected function __construct($aObject)
  68. {
  69. parent::__construct();
  70. $this->_aObject = $aObject;
  71. $this->_sObject = $aObject['object'];
  72. $this->_oDb = new BxDolPrivacyQuery($this->_aObject);
  73. }
  74. /**
  75. * Get privacy object instance by object name
  76. * @param $sObject object name
  77. * @return object instance or false on error
  78. */
  79. public static function getObjectInstance($sObject)
  80. {
  81. if(isset($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject]))
  82. return $GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject];
  83. $aObject = BxDolPrivacyQuery::getPrivacyObject($sObject);
  84. if(!$aObject || !is_array($aObject))
  85. return false;
  86. $sClass = 'BxTemplPrivacy';
  87. if(!empty($aObject['override_class_name'])) {
  88. $sClass = $aObject['override_class_name'];
  89. if(!empty($aObject['override_class_file']))
  90. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  91. }
  92. $o = new $sClass($aObject);
  93. return ($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject] = $o);
  94. }
  95. /**
  96. * Get Select element with available groups.
  97. *
  98. * @param string $sObject privacy object name.
  99. * @param integer $iOwnerId object's owner ID.
  100. * @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).
  101. * @return an array with Select element description.
  102. */
  103. public static function getGroupChooser($sObject, $iOwnerId = 0, $aParams = array())
  104. {
  105. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  106. if(empty($oPrivacy))
  107. return array();
  108. $sModule = $oPrivacy->_aObject['module'];
  109. $sAction = $oPrivacy->_aObject['action'];
  110. if($iOwnerId == 0)
  111. $iOwnerId = bx_get_logged_profile_id();
  112. $sValue = $oPrivacy->_oDb->getDefaultGroupByUser($sModule, $sAction, $iOwnerId);
  113. if(empty($sValue))
  114. $sValue = $oPrivacy->_oDb->getDefaultGroup($sModule, $sAction);
  115. $aValues = array();
  116. $aGroups = $oPrivacy->_oDb->getGroupsBy(array('type' => 'active'));
  117. foreach($aGroups as $aGroup) {
  118. if((int)$aGroup['active'] == 0)
  119. continue;
  120. $aValues[] = array('key' => $aGroup['id'], 'value' => _t($aGroup['title']));
  121. }
  122. if(isset($aParams['dynamic_groups']) && is_array($aParams['dynamic_groups']))
  123. $aValues = array_merge($aValues, $aParams['dynamic_groups']);
  124. $sName = $oPrivacy->convertActionToField($sAction);
  125. $sTitle = isset($aParams['title']) && !empty($aParams['title']) ? $aParams['title'] : '';
  126. if(empty($sTitle)) {
  127. $sTitle = $oPrivacy->_oDb->getTitle($sModule, $sAction);
  128. $sTitle = _t(!empty($sTitle) ? $sTitle : '_' . $sName);
  129. }
  130. return array(
  131. 'type' => 'select',
  132. 'name' => $sName,
  133. 'caption' => $sTitle,
  134. 'value' => $sValue,
  135. 'values' => $aValues,
  136. 'checker' => array(
  137. 'func' => 'avail',
  138. 'error' => _t('_ps_ferr_incorrect_select')
  139. ),
  140. 'db' => array(
  141. 'pass' => 'Int'
  142. )
  143. );
  144. }
  145. /**
  146. * Get database field name for action.
  147. *
  148. * @param string $sObject privacy object name.
  149. * @param string $sAction action name.
  150. * @return string with field name.
  151. */
  152. public static function getFieldName($sObject, $sAction = '')
  153. {
  154. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  155. if(empty($oPrivacy))
  156. return '';
  157. if(empty($sAction))
  158. $sAction = $oPrivacy->_aObject['action'];
  159. return $oPrivacy->convertActionToField($sAction);
  160. }
  161. /**
  162. * Get necessary condition array to use privacy in search classes
  163. * @param $mixedGroupId group ID or array of group IDs
  164. * @return array of conditions, for now with 'restriction' part only is returned
  165. */
  166. public function getContentByGroupAsCondition($mixedGroupId)
  167. {
  168. return array(
  169. 'restriction' => array (
  170. 'privacy_' . $this->_sObject => array(
  171. 'value' => $mixedGroupId,
  172. 'field' => $this->convertActionToField($this->_aObject['action']),
  173. 'operator' => is_array($mixedGroupId) ? 'in' : '=',
  174. 'table' => $this->_aObject['table'],
  175. ),
  176. ),
  177. );
  178. }
  179. /**
  180. * Get necessary condition array to use privacy in search classes
  181. * @param $iProfileIdOwner owner profile ID
  182. * @return array of conditions, for now with 'restriction' part only is returned
  183. */
  184. public function getContentPublicAsCondition($iProfileIdOwner = 0, $aCustomGroups = array())
  185. {
  186. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  187. if($mixedPrivacyGroups === true)
  188. return array();
  189. return $this->getContentByGroupAsCondition($mixedPrivacyGroups);
  190. }
  191. /**
  192. * Get necessary parts of SQL query to use privacy in other queries
  193. * @param $mixedGroupId group ID or array of group IDs
  194. * @return array of SQL string parts, for now 'where' part only is returned
  195. */
  196. public function getContentByGroupAsSQLPart($mixedGroupId)
  197. {
  198. $sField = $this->convertActionToField($this->_aObject['action']);
  199. return $this->_oDb->getContentByGroupAsSQLPart($sField, $mixedGroupId);
  200. }
  201. /**
  202. * Get necessary parts of SQL query to use privacy in other queries
  203. * @param $iProfileIdOwner owner profile ID
  204. * @return array of SQL string parts, for now 'where' part only is returned
  205. */
  206. public function getContentPublicAsSQLPart($iProfileIdOwner = 0, $aCustomGroups = array())
  207. {
  208. $mixedPrivacyGroups = $this->getPrivacyGroupsForContentPublic($iProfileIdOwner, $aCustomGroups);
  209. if($mixedPrivacyGroups === true)
  210. return array();
  211. return $this->getContentByGroupAsSQLPart($mixedPrivacyGroups);
  212. }
  213. /**
  214. * Check whether the viewer can make requested action.
  215. *
  216. * @param integer $iObjectId object ID the action to be performed with.
  217. * @param integer $iViewerId viewer ID.
  218. * @return boolean result of operation.
  219. */
  220. function check($iObjectId, $iViewerId = 0)
  221. {
  222. if(empty($iViewerId))
  223. $iViewerId = (int)bx_get_logged_profile_id();
  224. $aObject = $this->_oDb->getObjectInfo($this->convertActionToField($this->_aObject['action']), $iObjectId);
  225. if(empty($aObject) || !is_array($aObject))
  226. return false;
  227. if($aObject['group_id'] == BX_DOL_PG_HIDDEN)
  228. return false;
  229. if(isAdmin() || $iViewerId == $aObject['owner_id'])
  230. return true;
  231. $aGroup = $this->_oDb->getGroupsBy(array('type' => 'id', 'id' => $aObject['group_id']));
  232. if(!empty($aGroup) && is_array($aGroup) && (int)$aGroup['active'] == 1 && !empty($aGroup['check'])) {
  233. $sCheckMethod = $this->getCheckMethod($aGroup['check']);
  234. if(method_exists($this, $sCheckMethod) && $this->$sCheckMethod($aObject['owner_id'], $iViewerId))
  235. return true;
  236. }
  237. return $this->isDynamicGroupMember($aObject['group_id'], $aObject['owner_id'], $iViewerId, $iObjectId);
  238. }
  239. public function checkMeOnly($iOwnerId, $iViewerId)
  240. {
  241. return false;
  242. }
  243. public function checkPublic($iOwnerId, $iViewerId)
  244. {
  245. return true;
  246. }
  247. public function checkMembers($iOwnerId, $iViewerId)
  248. {
  249. return isMember();
  250. }
  251. public function checkFriends($iOwnerId, $iViewerId)
  252. {
  253. return BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iOwnerId, $iViewerId, true);
  254. }
  255. protected function getPrivacyGroupsForContentPublic($iProfileIdOwner = 0, $aCustomGroups = array())
  256. {
  257. $aGroups = array(BX_DOL_PG_ALL);
  258. if(isLogged()) {
  259. $iProfileIdLogged = bx_get_logged_profile_id();
  260. if($iProfileIdLogged == $iProfileIdOwner)
  261. return true;
  262. $aGroups[] = BX_DOL_PG_MEMBERS;
  263. if($iProfileIdOwner && $this->checkFriends($iProfileIdOwner, $iProfileIdLogged))
  264. $aGroups[] = BX_DOL_PG_FRIENDS;
  265. }
  266. return array_merge($aGroups, $aCustomGroups);
  267. }
  268. protected function getCheckMethod($s)
  269. {
  270. if(substr($s, 0, 1) != '@')
  271. return false;
  272. return bx_gen_method_name(str_replace('@', 'check_', $s));
  273. }
  274. protected function convertActionToField($sAction)
  275. {
  276. return 'allow_' . strtolower(str_replace(' ', '-', $sAction)) . '_to';
  277. }
  278. /**
  279. * Check whethere viewer is a member of dynamic group.
  280. *
  281. * @param mixed $mixedGroupId dynamic group ID.
  282. * @param integer $iObjectOwnerId object owner ID.
  283. * @param integer $iViewerId viewer ID.
  284. * @return boolean result of operation.
  285. */
  286. protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewerId, $iObjectId)
  287. {
  288. return false;
  289. }
  290. }
  291. /** @} */