BxDolPrivacy.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 BxDol implements iBxDolFactoryObject
  59. {
  60. protected $_oDb;
  61. protected $_sObject;
  62. protected $_aObject;
  63. /**
  64. * Constructor
  65. * @param $aObject array of grid options
  66. */
  67. 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 = self::getFieldName($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 $sAction action name.
  149. * @return string with field name.
  150. */
  151. public static function getFieldName($sAction)
  152. {
  153. return 'allow_' . strtolower(str_replace(' ', '-', $sAction)) . '_to';
  154. }
  155. /**
  156. * Get necessary condition array to use privacy in search classes
  157. * @param $mixedGroupId group ID or array of group IDs
  158. * @return array of conditions, for now with 'restriction' part only is returned
  159. */
  160. public function getContentByGroupAsCondition($mixedGroupId)
  161. {
  162. return array(
  163. 'restriction' => array (
  164. 'privacy_' . $this->_sObject => array(
  165. 'value' => $mixedGroupId,
  166. 'field' => self::getFieldName($this->_aObject['action']),
  167. 'operator' => is_array($mixedGroupId) ? 'in' : '=',
  168. 'table' => $this->_aObject['table'],
  169. ),
  170. ),
  171. );
  172. }
  173. /**
  174. * Get necessary condition array to use privacy in search classes
  175. * @param $iProfileIdOwner owner profile ID
  176. * @return array of conditions, for now with 'restriction' part only is returned
  177. */
  178. public function getContentPublicAsCondition($iProfileIdOwner = 0)
  179. {
  180. $mixedPrivacyGroups = BX_DOL_PG_ALL;
  181. if(isLogged()) {
  182. $iProfileIdLogged = bx_get_logged_profile_id();
  183. if($iProfileIdLogged == $iProfileIdOwner)
  184. return array();
  185. if ($iProfileIdOwner && $this->checkConnections($iProfileIdOwner, bx_get_logged_profile_id()))
  186. $mixedPrivacyGroups = array(BX_DOL_PG_ALL, BX_DOL_PG_MEMBERS, BX_DOL_PG_FRIENDS);
  187. else
  188. $mixedPrivacyGroups = array(BX_DOL_PG_ALL, BX_DOL_PG_MEMBERS);
  189. }
  190. return $this->getContentByGroupAsCondition($mixedPrivacyGroups);
  191. }
  192. /**
  193. * Get necessary parts of SQL query to use privacy in other queries
  194. * @param $mixedGroupId group ID or array of group IDs
  195. * @return array of SQL string parts, for now 'where' part only is returned
  196. */
  197. public function getContentByGroupAsSQLPart($mixedGroupId)
  198. {
  199. $sField = self::getFieldName($this->_aObject['action']);
  200. return $this->_oDb->getContentByGroupAsSQLPart($sField, $mixedGroupId);
  201. }
  202. /**
  203. * Check whether the viewer can make requested action.
  204. *
  205. * @param integer $iObjectId object ID the action to be performed with.
  206. * @param integer $iViewerId viewer ID.
  207. * @return boolean result of operation.
  208. */
  209. function check($iObjectId, $iViewerId = 0)
  210. {
  211. if(empty($iViewerId))
  212. $iViewerId = bx_get_logged_profile_id();
  213. $aObject = $this->_oDb->getObjectInfo($this->getFieldName($this->_aObject['action']), $iObjectId);
  214. if(empty($aObject) || !is_array($aObject))
  215. return false;
  216. if($aObject['group_id'] == BX_DOL_PG_HIDDEN)
  217. return false;
  218. if(isAdmin() || $iViewerId == $aObject['owner_id'])
  219. return true;
  220. $aGroup = $this->_oDb->getGroupsBy(array('type' => 'id', 'id' => $aObject['group_id']));
  221. if(!empty($aGroup) && is_array($aGroup) && (int)$aGroup['active'] == 1 && !empty($aGroup['check'])) {
  222. $sCheckMethod = $this->getCheckMethod($aGroup['check']);
  223. if(method_exists($this, $sCheckMethod) && $this->$sCheckMethod($aObject['owner_id'], $iViewerId))
  224. return true;
  225. }
  226. return $this->isDynamicGroupMember($aObject['group_id'], $aObject['owner_id'], $iViewerId, $iObjectId);
  227. }
  228. public function checkMeOnly($iOwnerId, $iViewerId)
  229. {
  230. return false;
  231. }
  232. public function checkPublic($iOwnerId, $iViewerId)
  233. {
  234. return true;
  235. }
  236. public function checkMembers($iOwnerId, $iViewerId)
  237. {
  238. return isMember();
  239. }
  240. public function checkConnections($iOwnerId, $iViewerId)
  241. {
  242. return BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iOwnerId, $iViewerId, true);
  243. }
  244. protected function getCheckMethod($s)
  245. {
  246. if(substr($s, 0, 1) != '@')
  247. return false;
  248. return bx_gen_method_name(str_replace('@', 'check_', $s));
  249. }
  250. /**
  251. * Check whethere viewer is a member of dynamic group.
  252. *
  253. * @param mixed $mixedGroupId dynamic group ID.
  254. * @param integer $iObjectOwnerId object owner ID.
  255. * @param integer $iViewerId viewer ID.
  256. * @return boolean result of operation.
  257. */
  258. protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewerId, $iObjectId)
  259. {
  260. return false;
  261. }
  262. }
  263. /** @} */