BxBaseModProfileModule.php 16 KB

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