1
0

BxBaseModProfileModule.php 16 KB

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