BxBaseModProfileModule.php 17 KB

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