BxBaseModProfileModule.php 17 KB

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