BxPersonsModule.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 Persons Persons
  7. * @ingroup DolphinModules
  8. *
  9. * @{
  10. */
  11. bx_import ('BxDolModule');
  12. bx_import ('BxDolAcl');
  13. /**
  14. * Persons module
  15. *
  16. * Basic person profiles.
  17. */
  18. class BxPersonsModule extends BxDolModule implements iBxDolProfileService {
  19. protected $_iProfileId;
  20. protected $_iAccountId;
  21. function __construct(&$aModule) {
  22. parent::__construct($aModule);
  23. $this->_iProfileId = bx_get_logged_profile_id();
  24. $this->_iAccountId = getLoggedId();
  25. }
  26. // ====== SERVICE METHODS
  27. public function serviceProfileUnit ($iContentId) {
  28. if (!$iContentId)
  29. return false;
  30. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  31. if (!$aContentInfo)
  32. return false;
  33. return $this->_oTemplate->unit($aContentInfo);
  34. }
  35. public function serviceProfileThumb ($iContentId) {
  36. if (!$iContentId)
  37. return false;
  38. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  39. if (!$aContentInfo)
  40. return false;
  41. return $this->_oTemplate->thumb($aContentInfo);
  42. }
  43. public function serviceProfileIcon ($iContentId) {
  44. if (!$iContentId)
  45. return false;
  46. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  47. if (!$aContentInfo)
  48. return false;
  49. return $this->_oTemplate->icon($aContentInfo);
  50. }
  51. public function serviceProfileName ($iContentId) {
  52. if (!$iContentId)
  53. return false;
  54. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  55. if (!$aContentInfo)
  56. return false;
  57. return $aContentInfo[BxPersonsConfig::$FIELD_NAME];
  58. }
  59. public function serviceProfileUrl ($iContentId) {
  60. if (!$iContentId)
  61. return false;
  62. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  63. if (!$aContentInfo)
  64. return false;
  65. bx_import('BxDolPermalinks');
  66. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=view-persons-profile&id=' . $aContentInfo['id']);
  67. }
  68. public function serviceBrowseRecentPersons () {
  69. return $this->_serviceBrowse ('recent');
  70. }
  71. public function serviceBrowseConnections ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iDesignBox = BX_DB_PADDING_DEF) {
  72. return $this->_serviceBrowse (
  73. 'connections',
  74. array(
  75. 'object' => $sObjectConnections,
  76. 'type' => $sConnectionsType,
  77. 'mutual' => $iMutual,
  78. 'profile' => (int)$iProfileId),
  79. $iDesignBox
  80. );
  81. }
  82. public function _serviceBrowse ($sMode, $aParams = false, $iDesignBox = BX_DB_PADDING_DEF) {
  83. bx_import('SearchResult', $this->_aModule);
  84. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  85. $o = new $sClass($sMode, $aParams);
  86. $o->setDesignBoxTemplateId($iDesignBox);
  87. if ($o->isError)
  88. return false;
  89. if ($s = $o->processing())
  90. return $s;
  91. else
  92. return false;
  93. }
  94. public function serviceCreateProfile () {
  95. bx_import('ProfileForms', $this->_aModule);
  96. $oProfileForms = new BxPersonsProfileForms($this);
  97. return $oProfileForms->addDataForm();
  98. }
  99. public function serviceEditProfile ($iContentId = 0) {
  100. if (!$iContentId)
  101. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  102. if (!$iContentId)
  103. return false;
  104. bx_import('ProfileForms', $this->_aModule);
  105. $oProfileForms = new BxPersonsProfileForms($this);
  106. return $oProfileForms->editDataForm((int)$iContentId);
  107. }
  108. public function serviceEditCover ($iContentId = 0) {
  109. if (!$iContentId)
  110. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  111. if (!$iContentId)
  112. return false;
  113. bx_import('ProfileForms', $this->_aModule);
  114. $oProfileForms = new BxPersonsProfileForms($this);
  115. return $oProfileForms->editDataForm((int)$iContentId, 'bx_person_edit_cover');
  116. }
  117. public function serviceDeleteProfile ($iContentId = 0) {
  118. if (!$iContentId)
  119. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  120. if (!$iContentId)
  121. return false;
  122. bx_import('ProfileForms', $this->_aModule);
  123. $oProfileForms = new BxPersonsProfileForms($this);
  124. return $oProfileForms->deleteDataForm((int)$iContentId);
  125. }
  126. public function serviceProfileInfo ($iContentId = 0) {
  127. if (!$iContentId)
  128. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  129. if (!$iContentId)
  130. return false;
  131. bx_import('ProfileForms', $this->_aModule);
  132. $oProfileForms = new BxPersonsProfileForms($this);
  133. return $oProfileForms->viewDataForm((int)$iContentId);
  134. }
  135. public function serviceProfileCover ($iContentId = 0) {
  136. if (!$iContentId)
  137. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  138. if (!$iContentId)
  139. return false;
  140. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  141. if (!$aContentInfo)
  142. return false;
  143. return $this->_oTemplate->cover($aContentInfo);
  144. }
  145. public function serviceProfileActions ($iContentId = 0) {
  146. if (!$iContentId)
  147. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  148. if (!$iContentId)
  149. return false;
  150. bx_import('BxTemplMenu');
  151. $oMenu = BxTemplMenu::getObjectInstance('bx_persons_view');
  152. return $oMenu ? $oMenu->getCode() : false;
  153. }
  154. public function serviceProfileFriends ($iContentId = 0) {
  155. if (!$iContentId)
  156. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  157. if (!$iContentId)
  158. return false;
  159. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  160. if (!$aContentInfo)
  161. return false;
  162. return 'TODO: friends here';
  163. }
  164. // ====== ACTION METHODS
  165. function actionBrowse ($sMode = '') {
  166. $sMode = bx_process_input($sMode);
  167. /*
  168. if ('user' == $sMode || 'my' == $sMode) {
  169. $aProfile = getProfileInfo ($this->_iProfileId);
  170. if (0 == strcasecmp($sValue, $aProfile['NickName']) || 'my' == $sMode) {
  171. $this->_browseMy ($aProfile);
  172. return;
  173. }
  174. }
  175. if ('tag' == $sMode || 'category' == $sMode)
  176. $sValue = uri2title($sValue);
  177. */
  178. if (CHECK_ACTION_RESULT_ALLOWED !== $this->isAllowedBrowse()) {
  179. $this->_oTemplate->displayAccessDenied ();
  180. return;
  181. }
  182. bx_import ('SearchResult', $this->_aModule);
  183. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  184. $o = new $sClass($sMode);
  185. if ($o->isError) {
  186. $this->_oTemplate->displayPageNotFound ();
  187. return;
  188. }
  189. if (bx_get('rss')) {
  190. echo $o->rss();
  191. exit;
  192. }
  193. if (!($s = $o->processing())) {
  194. $this->_oTemplate->displayNoData ();
  195. return;
  196. }
  197. // TODO: remale to use "pages"
  198. $this->_oTemplate->addCss ('main.css');
  199. $oTemplate = BxDolTemplate::getInstance();
  200. $oTemplate->setPageHeader ($o->aCurrent['title']);
  201. $oTemplate->setPageNameIndex (BX_PAGE_DEFAULT);
  202. $oTemplate->setPageContent ('page_main_code', $s);
  203. $oTemplate->getPageCode();
  204. }
  205. // ====== PERMISSION METHODS
  206. function _checkModeratorAccess ($isPerformAction = false) {
  207. // check moderator ACL
  208. $aCheck = checkActionModule($this->_iProfileId, 'edit any person profile', $this->getName(), $isPerformAction);
  209. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  210. }
  211. /**
  212. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  213. */
  214. function isAllowedView ($aDataEntry, $isPerformAction = false) {
  215. // moderator and owner always have access
  216. if ($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->_checkModeratorAccess($isPerformAction))
  217. return CHECK_ACTION_RESULT_ALLOWED;
  218. // check ACL
  219. $aCheck = checkActionModule($this->_iProfileId, 'view person profile', $this->getName(), $isPerformAction);
  220. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  221. return $aCheck[CHECK_ACTION_MESSAGE];
  222. // TODO: check privacy
  223. return CHECK_ACTION_RESULT_ALLOWED;
  224. }
  225. /**
  226. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden. So make sure to make "true === " checking.
  227. */
  228. function isAllowedBrowse () {
  229. return CHECK_ACTION_RESULT_ALLOWED;
  230. }
  231. /**
  232. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  233. */
  234. function isAllowedAdd ($isPerformAction = false) {
  235. // check ACL
  236. $aCheck = checkActionModule($this->_iProfileId, 'create person profile', $this->getName(), $isPerformAction);
  237. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  238. return $aCheck[CHECK_ACTION_MESSAGE];
  239. return CHECK_ACTION_RESULT_ALLOWED;
  240. }
  241. /**
  242. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  243. */
  244. function isAllowedEdit ($aDataEntry, $isPerformAction = false) {
  245. // moderator always has access
  246. if ($this->_checkModeratorAccess($isPerformAction))
  247. return CHECK_ACTION_RESULT_ALLOWED;
  248. // owner (checked by account! not as profile as ususal) always have access
  249. bx_import('BxDolProfile');
  250. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  251. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  252. return CHECK_ACTION_RESULT_ALLOWED;
  253. return _t('_sys_txt_access_denied');
  254. }
  255. /**
  256. * Check if user can change cover image
  257. */
  258. function isAllowedChangeCover ($aDataEntry, $isPerformAction = false) {
  259. // moderator always has access
  260. if ($this->_checkModeratorAccess($isPerformAction))
  261. return CHECK_ACTION_RESULT_ALLOWED;
  262. // owner (checked by account! not as profile as ususal) always have access
  263. bx_import('BxDolProfile');
  264. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  265. if ($oProfileAurhor->getAccountId() == $this->_iAccountId)
  266. return CHECK_ACTION_RESULT_ALLOWED;
  267. return _t('_sys_txt_access_denied');
  268. }
  269. /**
  270. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  271. */
  272. function isAllowedDelete (&$aDataEntry, $isPerformAction = false) {
  273. // moderator always has access
  274. if ($this->_checkModeratorAccess($isPerformAction))
  275. return CHECK_ACTION_RESULT_ALLOWED;
  276. // check ACL and owner (checked by account! not as profile as ususal)
  277. bx_import('BxDolProfile');
  278. $oProfileAurhor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  279. $aCheck = checkActionModule($this->_iProfileId, 'delete person profile', $this->getName(), $isPerformAction);
  280. if ($oProfileAurhor->getAccountId() == $this->_iAccountId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  281. return CHECK_ACTION_RESULT_ALLOWED;
  282. return _t('_sys_txt_access_denied');
  283. }
  284. /**
  285. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  286. */
  287. function isAllowedFriendAdd (&$aDataEntry, $isPerformAction = false) {
  288. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  289. }
  290. /**
  291. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  292. */
  293. function isAllowedFriendRemove (&$aDataEntry, $isPerformAction = false) {
  294. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, true);
  295. }
  296. /**
  297. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  298. */
  299. function isAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false) {
  300. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  301. }
  302. /**
  303. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  304. */
  305. function isAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false) {
  306. return $this->_isAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  307. }
  308. function _isAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult) {
  309. if (!$this->_iProfileId)
  310. return _t('_sys_txt_access_denied');
  311. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[BxPersonsConfig::$FIELD_AUTHOR]);
  312. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[BxPersonsConfig::$FIELD_ID], 'bx_persons', $oProfileAuthor->getAccountId()) : false;
  313. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  314. return _t('_sys_txt_access_denied');
  315. bx_import('BxDolConnection');
  316. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  317. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  318. if ($isInvertResult)
  319. $isConnected = !$isConnected;
  320. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  321. }
  322. }
  323. /** @} */