BxBaseModProfileModule.php 16 KB

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