BxDolPrivacy.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_FAVES', '6');
  16. //define('BX_DOL_PG_CONTACTS', '7');
  17. define('BX_DOL_PG_DEFAULT', BX_DOL_PG_ALL);
  18. /**
  19. * Privacy settings for any content.
  20. *
  21. * Integration of the content with privacy engine allows site member
  22. * to organize the access to his content.
  23. *
  24. * Related classes:
  25. * BxDolPrivacyQuery - database queries.
  26. *
  27. * Example of usage:
  28. * 1. Register your privacy actions in `sys_privacy_actions` database table.
  29. * 2. Add one privacy field(with INT type) in the table with your items for each action.
  30. * For example, for action 'comment', the field name should be 'allow_comment_to'.
  31. * 3. Add group choosers for necessary actions in the form, which is used to add new items.
  32. *
  33. * $oPrivacy = new BxDolPrivacy();
  34. * $oPrivacy->getGroupChooser($iItemOwnerId, $sModuleUri, $sModuleAction);
  35. *
  36. * 4. Check privacy when any user tries to view an item.
  37. *
  38. * $oPrivacy = new BxDolPrivacy($sTable, $sFieldId, $sFieldOwnerId);
  39. * if($oPrivacy->check($sAction, $iObjectId, $iViewerId)) {
  40. * //show necessary content
  41. * }
  42. *
  43. * @see an example of integration in the default Dolphin's modules(feedback, events, sites, etc)
  44. *
  45. *
  46. * Memberships/ACL:
  47. * Doesn't depend on user's membership.
  48. *
  49. *
  50. * Alerts:
  51. * no alerts available
  52. *
  53. */
  54. class BxDolPrivacy extends BxDol implements iBxDolFactoryObject
  55. {
  56. protected $_oDb;
  57. protected $_sObject;
  58. protected $_aObject;
  59. /**
  60. * Constructor
  61. * @param $aObject array of grid options
  62. */
  63. function __construct($aObject)
  64. {
  65. parent::BxDol();
  66. $this->_aObject = $aObject;
  67. $this->_sObject = $aObject['object'];
  68. $this->_oDb = new BxDolPrivacyQuery($this->_aObject);
  69. }
  70. /**
  71. * Get privacy object instance by object name
  72. * @param $sObject object name
  73. * @return object instance or false on error
  74. */
  75. public static function getObjectInstance($sObject)
  76. {
  77. if(isset($GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject]))
  78. return $GLOBALS['bxDolClasses']['BxDolPrivacy!' . $sObject];
  79. bx_import('BxDolPrivacyQuery');
  80. $aObject = BxDolPrivacyQuery::getPrivacyObject($sObject);
  81. if(!$aObject || !is_array($aObject))
  82. return false;
  83. bx_import('BxTemplPrivacy');
  84. $sClass = 'BxTemplPrivacy';
  85. if(!empty($aObject['override_class_name'])) {
  86. $sClass = $aObject['override_class_name'];
  87. if(!empty($aObject['override_class_file']))
  88. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  89. else
  90. bx_import($sClass);
  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. return array(
  162. 'restriction' => array (
  163. 'privacy_' . $this->_sObject => array(
  164. 'value' => $mixedGroupId,
  165. 'field' => self::getFieldName($this->_aObject['action']),
  166. 'operator' => is_array($mixedGroupId) ? 'in' : '=',
  167. 'table' => $this->_aObject['table'],
  168. ),
  169. ),
  170. );
  171. }
  172. /**
  173. * Get necessary condition array to use privacy in search classes
  174. * @param $iProfileIdOwner owner profile ID
  175. * @return array of conditions, for now with 'restriction' part only is returned
  176. */
  177. public function getContentPublicAsCondition($iProfileIdOwner = 0) {
  178. $mixedPrivacyGroups = BX_DOL_PG_ALL;
  179. if(isLogged()) {
  180. $iProfileIdLogged = bx_get_logged_profile_id();
  181. if($iProfileIdLogged == $iProfileIdOwner)
  182. return array();
  183. if ($iProfileIdOwner && $this->checkConnections($iProfileIdOwner, bx_get_logged_profile_id()))
  184. $mixedPrivacyGroups = array(BX_DOL_PG_ALL, BX_DOL_PG_MEMBERS, BX_DOL_PG_FRIENDS);
  185. else
  186. $mixedPrivacyGroups = array(BX_DOL_PG_ALL, BX_DOL_PG_MEMBERS);
  187. }
  188. return $this->getContentByGroupAsCondition($mixedPrivacyGroups);
  189. }
  190. /**
  191. * Get necessary parts of SQL query to use privacy in other queries
  192. * @param $mixedGroupId group ID or array of group IDs
  193. * @return array of SQL string parts, for now 'where' part only is returned
  194. */
  195. public function getContentByGroupAsSQLPart($mixedGroupId) {
  196. $sField = self::getFieldName($this->_aObject['action']);
  197. return $this->_oDb->getContentByGroupAsSQLPart($sField, $mixedGroupId);
  198. }
  199. /**
  200. * Check whether the viewer can make requested action.
  201. *
  202. * @param integer $iObjectId object ID the action to be performed with.
  203. * @param integer $iViewerId viewer ID.
  204. * @return boolean result of operation.
  205. */
  206. function check($iObjectId, $iViewerId = 0)
  207. {
  208. if(empty($iViewerId))
  209. $iViewerId = bx_get_logged_profile_id();
  210. $aObject = $this->_oDb->getObjectInfo($this->getFieldName($this->_aObject['action']), $iObjectId);
  211. if(empty($aObject) || !is_array($aObject))
  212. return false;
  213. if($aObject['group_id'] == BX_DOL_PG_HIDDEN)
  214. return false;
  215. if(isAdmin() || $iViewerId == $aObject['owner_id'])
  216. return true;
  217. $aGroup = $this->_oDb->getGroupsBy(array('type' => 'id', 'id' => $aObject['group_id']));
  218. if(!empty($aGroup) && is_array($aGroup) && (int)$aGroup['active'] == 1 && !empty($aGroup['check'])) {
  219. $sCheckMethod = $this->getCheckMethod($aGroup['check']);
  220. if(method_exists($this, $sCheckMethod) && $this->$sCheckMethod($aObject['owner_id'], $iViewerId))
  221. return true;
  222. }
  223. return $this->isDynamicGroupMember($aObject['group_id'], $aObject['owner_id'], $iViewerId, $iObjectId);
  224. }
  225. public function checkMeOnly($iOwnerId, $iViewerId)
  226. {
  227. return false;
  228. }
  229. public function checkPublic($iOwnerId, $iViewerId)
  230. {
  231. return true;
  232. }
  233. public function checkMembers($iOwnerId, $iViewerId)
  234. {
  235. return isMember();
  236. }
  237. public function checkConnections($iOwnerId, $iViewerId)
  238. {
  239. bx_import('BxDolConnection');
  240. return BxDolConnection::getObjectInstance('sys_profiles_friends')->isConnected($iOwnerId, $iViewerId, true);
  241. }
  242. protected function getCheckMethod($s)
  243. {
  244. if(substr($s, 0, 1) != '@')
  245. return false;
  246. return str_replace(' ', '', ucwords(str_replace('_', ' ', str_replace('@', 'check_', $s))));
  247. }
  248. /**
  249. * Check whethere viewer is a member of dynamic group.
  250. *
  251. * @param mixed $mixedGroupId dynamic group ID.
  252. * @param integer $iObjectOwnerId object owner ID.
  253. * @param integer $iViewerId viewer ID.
  254. * @return boolean result of operation.
  255. */
  256. protected function isDynamicGroupMember($mixedGroupId, $iObjectOwnerId, $iViewerId, $iObjectId)
  257. {
  258. return false;
  259. }
  260. }