BxDolPrivacy.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolPrivacyQuery');
  10. define('BX_DOL_PG_HIDDEN', '1');
  11. define('BX_DOL_PG_MEONLY', '2');
  12. define('BX_DOL_PG_ALL', '3');
  13. define('BX_DOL_PG_MEMBERS', '4');
  14. define('BX_DOL_PG_FRIENDS', '5');
  15. define('BX_DOL_PG_DEFAULT', BX_DOL_PG_ALL);
  16. /**
  17. * @page objects
  18. * @section privacy Privacy
  19. * @ref BxDolPrivacy
  20. */
  21. /**
  22. * Privacy settings for any content.
  23. *
  24. * Integration of the content with privacy engine allows site member
  25. * to organize the access to his content.
  26. *
  27. * Related classes:
  28. * BxDolPrivacyQuery - database queries.
  29. *
  30. * Example of usage:
  31. * 1. Register your privacy actions in `sys_privacy_actions` database table.
  32. * 2. Add one privacy field(with INT type) in the table with your items for each action.
  33. * For example, for action 'comment', the field name should be 'allow_comment_to'.
  34. * 3. Add group choosers for necessary actions in the form, which is used to add new items.
  35. *
  36. * $oPrivacy = new BxDolPrivacy();
  37. * $oPrivacy->getGroupChooser($iItemOwnerId, $sModuleUri, $sModuleAction);
  38. *
  39. * 4. Check privacy when any user tries to view an item.
  40. *
  41. * $oPrivacy = new BxDolPrivacy($sTable, $sFieldId, $sFieldOwnerId);
  42. * if($oPrivacy->check($sAction, $iObjectId, $iViewerId)) {
  43. * //show necessary content
  44. * }
  45. *
  46. * @see an example of integration in the default Dolphin's modules(feedback, events, sites, etc)
  47. *
  48. *
  49. * Memberships/ACL:
  50. * Doesn't depend on user's membership.
  51. *
  52. *
  53. * Alerts:
  54. * no alerts available
  55. *
  56. */
  57. class BxDolPrivacy extends BxDol implements iBxDolFactoryObject
  58. {
  59. protected $_oDb;
  60. protected $_sObject;
  61. protected $_aObject;
  62. /**
  63. * Constructor
  64. * @param $aObject array of grid options
  65. */
  66. function __construct($aObject)
  67. {
  68. parent::__construct();
  69. $this->_aObject = $aObject;
  70. $this->_sObject = $aObject['object'];
  71. $this->_oDb = new BxDolPrivacyQuery($this->_aObject);
  72. }
  73. /**
  74. * Get privacy object instance by object name
  75. * @param $sObject object name
  76. * @return object instance or false on error
  77. */
  78. public static function getObjectInstance($sObject)
  79. {
  80. if(isset($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject]))
  81. return $GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject];
  82. bx_import('BxDolPrivacyQuery');
  83. $aObject = BxDolPrivacyQuery::getPrivacyObject($sObject);
  84. if(!$aObject || !is_array($aObject))
  85. return false;
  86. bx_import('BxTemplPrivacy');
  87. $sClass = 'BxTemplPrivacy';
  88. if(!empty($aObject['override_class_name'])) {
  89. $sClass = $aObject['override_class_name'];
  90. if(!empty($aObject['override_class_file']))
  91. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  92. else
  93. bx_import($sClass);
  94. }
  95. $o = new $sClass($aObject);
  96. return ($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject] = $o);
  97. }
  98. /**
  99. * Get Select element with available groups.
  100. *
  101. * @param string $sObject privacy object name.
  102. * @param integer $iOwnerId object's owner ID.
  103. * @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).
  104. * @return an array with Select element description.
  105. */
  106. public static function getGroupChooser($sObject, $iOwnerId = 0, $aParams = array())
  107. {
  108. $oPrivacy = BxDolPrivacy::getObjectInstance($sObject);
  109. if(empty($oPrivacy))
  110. return array();
  111. $sModule = $oPrivacy->_aObject['module'];
  112. $sAction = $oPrivacy->_aObject['action'];
  113. if($iOwnerId == 0)
  114. $iOwnerId = bx_get_logged_profile_id();
  115. $sValue = $oPrivacy->_oDb->getDefaultGroupByUser($sModule, $sAction, $iOwnerId);
  116. if(empty($sValue))
  117. $sValue = $oPrivacy->_oDb->getDefaultGroup($sModule, $sAction);
  118. $aValues = array();
  119. $aGroups = $oPrivacy->_oDb->getGroupsBy(array('type' => 'active'));
  120. foreach($aGroups as $aGroup) {
  121. if((int)$aGroup['active'] == 0)
  122. continue;
  123. $aValues[] = array('key' => $aGroup['id'], 'value' => _t($aGroup['title']));
  124. }
  125. if(isset($aParams['dynamic_groups']) && is_array($aParams['dynamic_groups']))
  126. $aValues = array_merge($aValues, $aParams['dynamic_groups']);
  127. $sName = self::getFieldName($sAction);
  128. $sTitle = isset($aParams['title']) && !empty($aParams['title']) ? $aParams['title'] : '';
  129. if(empty($sTitle)) {
  130. $sTitle = $oPrivacy->_oDb->getTitle($sModule, $sAction);
  131. $sTitle = _t(!empty($sTitle) ? $sTitle : '_' . $sName);
  132. }
  133. return array(
  134. 'type' => 'select',
  135. 'name' => $sName,
  136. 'caption' => $sTitle,
  137. 'value' => $sValue,
  138. 'values' => $aValues,
  139. 'checker' => array(
  140. 'func' => 'avail',
  141. 'error' => _t('_ps_ferr_incorrect_select')
  142. ),
  143. 'db' => array(
  144. 'pass' => 'Int'
  145. )
  146. );
  147. }
  148. /**
  149. * Get database field name for action.
  150. *
  151. * @param string $sAction action name.
  152. * @return string with field name.
  153. */
  154. public static function getFieldName($sAction)
  155. {
  156. return 'allow_' . strtolower(str_replace(' ', '-', $sAction)) . '_to';
  157. }
  158. /**
  159. * Get necessary condition array to use privacy in search classes
  160. * @param $mixedGroupId group ID or array of group IDs
  161. * @return array of conditions, for now with 'restriction' part only is returned
  162. */
  163. public function getContentByGroupAsCondition($mixedGroupId) {
  164. return array(
  165. 'restriction' => array (
  166. 'privacy_' . $this->_sObject => array(
  167. 'value' => $mixedGroupId,
  168. 'field' => self::getFieldName($this->_aObject['action']),
  169. 'operator' => is_array($mixedGroupId) ? 'in' : '=',
  170. 'table' => $this->_aObject['table'],
  171. ),
  172. ),
  173. );
  174. }
  175. /**
  176. * Get necessary condition array to use privacy in search classes
  177. * @param $iProfileIdOwner owner profile ID
  178. * @return array of conditions, for now with 'restriction' part only is returned
  179. */
  180. public function getContentPublicAsCondition($iProfileIdOwner = 0) {
  181. $mixedPrivacyGroups = BX_DOL_PG_ALL;
  182. if(isLogged()) {
  183. $iProfileIdLogged = bx_get_logged_profile_id();
  184. if($iProfileIdLogged == $iProfileIdOwner)
  185. return array();
  186. if ($iProfileIdOwner && $this->checkConnections($iProfileIdOwner, bx_get_logged_profile_id()))
  187. $mixedPrivacyGroups = array(BX_DOL_PG_ALL, BX_DOL_PG_MEMBERS, BX_DOL_PG_FRIENDS);
  188. else
  189. $mixedPrivacyGroups = array(BX_DOL_PG_ALL, BX_DOL_PG_MEMBERS);
  190. }
  191. return $this->getContentByGroupAsCondition($mixedPrivacyGroups);
  192. }
  193. /**
  194. * Get necessary parts of SQL query to use privacy in other queries
  195. * @param $mixedGroupId group ID or array of group IDs
  196. * @return array of SQL string parts, for now 'where' part only is returned
  197. */
  198. public function getContentByGroupAsSQLPart($mixedGroupId) {
  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. bx_import('BxDolConnection');
  243. return BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iOwnerId, $iViewerId, true);
  244. }
  245. protected function getCheckMethod($s)
  246. {
  247. if(substr($s, 0, 1) != '@')
  248. return false;
  249. return bx_gen_method_name(str_replace('@', 'check_', $s));
  250. }
  251. /**
  252. * Check whethere viewer is a member of dynamic group.
  253. *
  254. * @param mixed $mixedGroupId dynamic group ID.
  255. * @param integer $iObjectOwnerId object owner ID.
  256. * @param integer $iViewerId viewer ID.
  257. * @return boolean result of operation.
  258. */
  259. protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewerId, $iObjectId)
  260. {
  261. return false;
  262. }
  263. }
  264. /** @} */