BxBaseModProfileModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 BaseProfile Base classes for profile modules
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. bx_import ('BxBaseModGeneralModule');
  12. bx_import ('BxDolAcl');
  13. /**
  14. * Base class for profile modules.
  15. */
  16. class BxBaseModProfileModule extends BxBaseModGeneralModule implements iBxDolProfileService
  17. {
  18. protected $_iAccountId;
  19. function __construct(&$aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_iAccountId = getLoggedId();
  23. }
  24. public function actionDeleteProfileImg($iFileId, $iContentId, $sFieldPicture)
  25. {
  26. $aResult = array();
  27. $CNF = &$this->_oConfig->CNF;
  28. bx_import('BxDolStorage');
  29. $oSrorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE']);
  30. if (!($aFile = $oSrorage->getFile((int)$iFileId)) || !($aContentInfo = $this->_oDb->getContentInfoById($iContentId)) || $aContentInfo[$sFieldPicture] != (int)$iFileId)
  31. $aResult = array('error' => 1, 'msg' => _t('_sys_storage_err_file_not_found'));
  32. if ((!$aResult && !isLogged()) || (!$aResult && $aFile['profile_id'] != bx_get_logged_profile_id() && !$this->_isModerator()))
  33. $aResult = array('error' => 2, 'msg' => _t('_Access denied'));
  34. bx_import('BxDolForm');
  35. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ENTRY'], $CNF['OBJECT_FORM_ENTRY_DISPLAY_ADD'], $this->_oTemplate);
  36. if (!$aResult && !$oForm->_deleteFile($iContentId, $sFieldPicture, (int)$iFileId, true))
  37. $aResult = array('error' => 3, 'msg' => _t('_Failed'));
  38. elseif (!$aResult)
  39. $aResult = array('error' => 0, 'msg' => '');
  40. header('Content-Type: application/json; charset=utf-8');
  41. echo json_encode($aResult);
  42. }
  43. // ====== SERVICE METHODS
  44. public function serviceGetMenuAddonManageTools()
  45. {
  46. bx_import('SearchResult', $this->_aModule);
  47. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  48. $o = new $sClass();
  49. $o->fillFilters(array(
  50. 'perofileStatus' => BX_PROFILE_STATUS_PENDING
  51. ));
  52. $o->unsetPaginate();
  53. return $o->getNum();
  54. }
  55. public function serviceGetMenuAddonManageToolsProfileStats()
  56. {
  57. bx_import('SearchResult', $this->_aModule);
  58. $sClass = $this->_aModule['class_prefix'] . 'SearchResult';
  59. $o = new $sClass();
  60. $o->fillFilters(array(
  61. 'account_id' => getLoggedId(),
  62. 'perofileStatus' => ''
  63. ));
  64. $o->unsetPaginate();
  65. return $o->getNum();
  66. }
  67. public function serviceGetMenuSetNameForMenuTrigger ($sMenuTriggerName)
  68. {
  69. if ('trigger_profile_view_submenu' == $sMenuTriggerName)
  70. return $this->_oConfig->CNF['OBJECT_MENU_SUBMENU_VIEW_ENTRY'];
  71. elseif ('trigger_profile_view_actions' == $sMenuTriggerName)
  72. return $this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY'];
  73. return '';
  74. }
  75. public function serviceGetPageObjectForPageTrigger ($sPageTriggerName)
  76. {
  77. if(isset($this->_oConfig->CNF['TRIGGER_PAGE_VIEW_ENTRY']) && $this->_oConfig->CNF['TRIGGER_PAGE_VIEW_ENTRY'] == $sPageTriggerName)
  78. return $this->_oConfig->CNF['OBJECT_PAGE_VIEW_ENTRY'];
  79. return '';
  80. }
  81. public function serviceProfilesSearch ($sTerm, $iLimit)
  82. {
  83. $aRet = array();
  84. $a = $this->_oDb->searchByTerm($sTerm, $iLimit);
  85. foreach ($a as $r)
  86. $aRet[] = array ('label' => $this->serviceProfileName($r['content_id']), 'value' => $r['profile_id']);
  87. return $aRet;
  88. }
  89. public function serviceProfileUnit ($iContentId)
  90. {
  91. return $this->_serviceTemplateFunc('unit', $iContentId);
  92. }
  93. public function serviceProfileAvatar ($iContentId)
  94. {
  95. return $this->_serviceTemplateFunc('urlAvatar', $iContentId);
  96. }
  97. public function serviceProfileEditUrl ($iContentId)
  98. {
  99. bx_import('BxDolPermalinks');
  100. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $this->_oConfig->CNF['URI_EDIT_ENTRY'] . '&id=' . $iContentId);
  101. }
  102. public function serviceProfileThumb ($iContentId)
  103. {
  104. return $this->_serviceTemplateFunc('thumb', $iContentId);
  105. }
  106. public function serviceProfileIcon ($iContentId)
  107. {
  108. return $this->_serviceTemplateFunc('icon', $iContentId);
  109. }
  110. public function serviceProfileName ($iContentId)
  111. {
  112. if (!$iContentId)
  113. return false;
  114. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  115. if (!$aContentInfo)
  116. return false;
  117. return $aContentInfo[$this->_oConfig->CNF['FIELD_NAME']];
  118. }
  119. public function serviceProfileUrl ($iContentId)
  120. {
  121. if (!$iContentId)
  122. return false;
  123. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  124. if (!$aContentInfo)
  125. return false;
  126. $CNF = $this->_oConfig->CNF;
  127. bx_import('BxDolPermalinks');
  128. return BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=' . $CNF['URI_VIEW_ENTRY'] . '&id=' . $aContentInfo[$CNF['FIELD_ID']]);
  129. }
  130. public function serviceBrowseRecentProfiles ($bDisplayEmptyMsg = false)
  131. {
  132. return $this->_serviceBrowse ('recent', false, BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  133. }
  134. public function serviceBrowseConnections ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iDesignBox = BX_DB_PADDING_DEF, $iProfileId2 = 0)
  135. {
  136. return $this->_serviceBrowse (
  137. 'connections',
  138. array(
  139. 'object' => $sObjectConnections,
  140. 'type' => $sConnectionsType,
  141. 'mutual' => $iMutual,
  142. 'profile' => (int)$iProfileId,
  143. 'profile2' => (int)$iProfileId2),
  144. $iDesignBox
  145. );
  146. }
  147. public function serviceBrowseConnectionsQuick ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iProfileId2 = 0)
  148. {
  149. // get connections object
  150. bx_import('BxDolConnection');
  151. $oConnection = BxDolConnection::getObjectInstance($sObjectConnections);
  152. if (!$oConnection)
  153. return '';
  154. // set some vars
  155. $iLimit = empty($this->_oConfig->CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? 4 : getParam($this->_oConfig->CNF['PARAM_NUM_CONNECTIONS_QUICK']);
  156. if (!$iLimit)
  157. $iLimit = 4;
  158. $iStart = (int)bx_get('start');
  159. // get connections array
  160. $a = $oConnection->getConnectionsAsArray ($sConnectionsType, $iProfileId, $iProfileId2, $iMutual, (int)bx_get('start'), $iLimit + 1, BX_CONNECTIONS_ORDER_ADDED_DESC);
  161. if (!$a)
  162. return '';
  163. // get paginate object
  164. bx_import('BxTemplPaginate');
  165. $oPaginate = new BxTemplPaginate(array(
  166. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}');",
  167. 'num' => count($a),
  168. 'per_page' => $iLimit,
  169. 'start' => $iStart,
  170. ));
  171. // remove last item from connection array, because we've got one more item for pagination calculations only
  172. if (count($a) > $iLimit)
  173. array_pop($a);
  174. // get profiles HTML
  175. bx_import('BxDolProfile');
  176. $s = '';
  177. foreach ($a as $iProfileId) {
  178. if (!($o = BxDolProfile::getInstance($iProfileId))) {
  179. continue;
  180. }
  181. $s .= $o->getUnit();
  182. }
  183. // return profiles + paginate
  184. return $s . (!$iStart && $oPaginate->getNum() <= $iLimit ? '' : $oPaginate->getSimplePaginate());
  185. }
  186. public function serviceEntityEditCover ($iContentId = 0)
  187. {
  188. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_COVER']);
  189. }
  190. public function serviceProfileCover ($iContentId = 0)
  191. {
  192. if (!$iContentId)
  193. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  194. if (!$iContentId)
  195. return false;
  196. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  197. if (!$aContentInfo)
  198. return false;
  199. return $this->_oTemplate->cover($aContentInfo);
  200. }
  201. public function serviceProfileFriends ($iContentId = 0)
  202. {
  203. if (!$iContentId)
  204. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  205. if (!$iContentId)
  206. return false;
  207. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  208. if (!$aContentInfo)
  209. return false;
  210. bx_import('BxDolConnection');
  211. $s = $this->serviceBrowseConnectionsQuick ($aContentInfo['profile_id'], 'sys_profiles_friends', BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  212. if (!$s)
  213. return MsgBox(_t('_sys_txt_empty'));
  214. return $s;
  215. }
  216. /**
  217. * For internal usage only.
  218. */
  219. public function serviceDeleteEntityService ($iContentId, $bDeleteWithContent = false)
  220. {
  221. return parent::serviceDeleteEntity ($iContentId, 'deleteDataService');
  222. }
  223. // ====== PERMISSION METHODS
  224. /**
  225. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  226. */
  227. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  228. {
  229. // moderator always has access
  230. if ($this->_isModerator($isPerformAction))
  231. return CHECK_ACTION_RESULT_ALLOWED;
  232. // owner (checked by account! not as profile as ususal) always have access
  233. bx_import('BxDolProfile');
  234. $oProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->_aModule['name']);
  235. if (!$oProfile)
  236. return _t('_sys_txt_error_occured');
  237. if ($oProfile->getAccountId() == $this->_iAccountId)
  238. return CHECK_ACTION_RESULT_ALLOWED;
  239. return _t('_sys_txt_access_denied');
  240. }
  241. /**
  242. * Check if user can change cover image
  243. */
  244. public function checkAllowedChangeCover ($aDataEntry, $isPerformAction = false)
  245. {
  246. // moderator always has access
  247. if ($this->_isModerator($isPerformAction))
  248. return CHECK_ACTION_RESULT_ALLOWED;
  249. // owner (checked by account! not as profile as ususal) always have access
  250. bx_import('BxDolProfile');
  251. $oProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->_aModule['name']);
  252. if (!$oProfile)
  253. return _t('_sys_txt_error_occured');
  254. if ($oProfile->getAccountId() == $this->_iAccountId)
  255. return CHECK_ACTION_RESULT_ALLOWED;
  256. return _t('_sys_txt_access_denied');
  257. }
  258. /**
  259. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  260. */
  261. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  262. {
  263. // moderator always has access
  264. if ($this->_isModerator($isPerformAction))
  265. return CHECK_ACTION_RESULT_ALLOWED;
  266. // check ACL and owner (checked by account! not as profile as ususal)
  267. $aCheck = checkActionModule($this->_iProfileId, 'delete entry', $this->getName(), $isPerformAction);
  268. bx_import('BxDolProfile');
  269. $oProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->_aModule['name']);
  270. if (!$oProfile)
  271. return _t('_sys_txt_error_occured');
  272. if ($oProfile->getAccountId() == $this->_iAccountId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  273. return CHECK_ACTION_RESULT_ALLOWED;
  274. return _t('_sys_txt_access_denied');
  275. }
  276. /**
  277. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  278. */
  279. public function checkAllowedViewMoreMenu (&$aDataEntry, $isPerformAction = false)
  280. {
  281. bx_import('BxTemplMenu');
  282. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']);
  283. if (!$oMenu || !$oMenu->getCode())
  284. return _t('_sys_txt_access_denied');
  285. return CHECK_ACTION_RESULT_ALLOWED;
  286. }
  287. /**
  288. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  289. */
  290. public function checkAllowedFriendAdd (&$aDataEntry, $isPerformAction = false)
  291. {
  292. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  293. }
  294. /**
  295. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  296. */
  297. public function checkAllowedFriendRemove (&$aDataEntry, $isPerformAction = false)
  298. {
  299. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, true))
  300. return CHECK_ACTION_RESULT_ALLOWED;
  301. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, false);
  302. }
  303. /**
  304. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  305. */
  306. public function checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  307. {
  308. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  309. }
  310. /**
  311. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  312. */
  313. public function checkAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false)
  314. {
  315. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  316. }
  317. public function checkMyself($iContentId)
  318. {
  319. $iLogged = (int)bx_get_logged_profile_id();
  320. if(empty($iLogged))
  321. return false;
  322. bx_import('BxDolProfile');
  323. $oProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->_oConfig->getName());
  324. if(!$oProfile)
  325. return false;
  326. return $oProfile->id() == $iLogged;
  327. }
  328. // ====== PROTECTED METHODS
  329. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  330. {
  331. if (!$this->_iProfileId)
  332. return _t('_sys_txt_access_denied');
  333. $CNF = &$this->_oConfig->CNF;
  334. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[$CNF['FIELD_AUTHOR']]);
  335. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[$CNF['FIELD_ID']], $this->_aModule['name'], $oProfileAuthor->getAccountId()) : false;
  336. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  337. return _t('_sys_txt_access_denied');
  338. bx_import('BxDolConnection');
  339. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  340. if ($isSwap)
  341. $isConnected = $oConn->isConnected($oProfile->id(), $this->_iProfileId, $isMutual);
  342. else
  343. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  344. if ($isInvertResult)
  345. $isConnected = !$isConnected;
  346. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  347. }
  348. protected function _buildRssParams($sMode, $aArgs)
  349. {
  350. $aParams = array ();
  351. $sMode = bx_process_input($sMode);
  352. switch ($sMode) {
  353. case 'connections':
  354. $aParams = array(
  355. 'object' => isset($aArgs[0]) ? $aArgs[0] : '',
  356. 'type' => isset($aArgs[1]) ? $aArgs[1] : '',
  357. 'profile' => isset($aArgs[2]) ? (int)$aArgs[2] : 0,
  358. 'mutual' => isset($aArgs[3]) ? (int)$aArgs[3] : 0,
  359. 'profile2' => isset($aArgs[4]) ? (int)$aArgs[4] : 0,
  360. );
  361. break;
  362. }
  363. return $aParams;
  364. }
  365. }
  366. /** @} */