1
0

BxBaseModProfileModule.php 15 KB

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