1
0

BxBaseModProfileModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 serviceBrowseActiveProfiles ($bDisplayEmptyMsg = false)
  129. {
  130. return $this->_serviceBrowse ('active', false, BX_DB_PADDING_DEF, $bDisplayEmptyMsg);
  131. }
  132. public function serviceBrowseConnections ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iDesignBox = BX_DB_PADDING_DEF, $iProfileId2 = 0)
  133. {
  134. return $this->_serviceBrowse (
  135. 'connections',
  136. array(
  137. 'object' => $sObjectConnections,
  138. 'type' => $sConnectionsType,
  139. 'mutual' => $iMutual,
  140. 'profile' => (int)$iProfileId,
  141. 'profile2' => (int)$iProfileId2),
  142. $iDesignBox
  143. );
  144. }
  145. public function serviceBrowseConnectionsQuick ($iProfileId, $sObjectConnections = 'sys_profiles_friends', $sConnectionsType = 'content', $iMutual = false, $iProfileId2 = 0)
  146. {
  147. // get connections object
  148. $oConnection = BxDolConnection::getObjectInstance($sObjectConnections);
  149. if (!$oConnection)
  150. return '';
  151. // set some vars
  152. $iLimit = empty($this->_oConfig->CNF['PARAM_NUM_CONNECTIONS_QUICK']) ? 4 : getParam($this->_oConfig->CNF['PARAM_NUM_CONNECTIONS_QUICK']);
  153. if (!$iLimit)
  154. $iLimit = 4;
  155. $iStart = (int)bx_get('start');
  156. // get connections array
  157. bx_import('BxDolConnection');
  158. $a = $oConnection->getConnectionsAsArray ($sConnectionsType, $iProfileId, $iProfileId2, $iMutual, (int)bx_get('start'), $iLimit + 1, BX_CONNECTIONS_ORDER_ADDED_DESC);
  159. if (!$a)
  160. return '';
  161. // get paginate object
  162. $oPaginate = new BxTemplPaginate(array(
  163. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}');",
  164. 'num' => count($a),
  165. 'per_page' => $iLimit,
  166. 'start' => $iStart,
  167. ));
  168. // remove last item from connection array, because we've got one more item for pagination calculations only
  169. if (count($a) > $iLimit)
  170. array_pop($a);
  171. // get profiles HTML
  172. $s = '';
  173. foreach ($a as $iProfileId) {
  174. if (!($o = BxDolProfile::getInstance($iProfileId))) {
  175. continue;
  176. }
  177. $s .= $o->getUnit();
  178. }
  179. // return profiles + paginate
  180. return $s . (!$iStart && $oPaginate->getNum() <= $iLimit ? '' : $oPaginate->getSimplePaginate());
  181. }
  182. public function serviceEntityEditCover ($iContentId = 0)
  183. {
  184. return $this->_serviceEntityForm ('editDataForm', $iContentId, $this->_oConfig->CNF['OBJECT_FORM_ENTRY_DISPLAY_EDIT_COVER']);
  185. }
  186. public function serviceProfileCover ($iContentId = 0)
  187. {
  188. if (!$iContentId) {
  189. if (bx_get('id')) {
  190. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  191. } elseif (bx_get('profile_id')) {
  192. $oProfile = BxDolProfile::getInstance(bx_get('profile_id'));
  193. if ($oProfile && ($a = $oProfile->getInfo()) && $this->getName() == $a['type'])
  194. $iContentId = $oProfile->getContentId();
  195. }
  196. }
  197. if (!$iContentId)
  198. return false;
  199. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  200. if (!$aContentInfo)
  201. return false;
  202. return $this->_oTemplate->cover($aContentInfo);
  203. }
  204. public function serviceProfileFriends ($iContentId = 0)
  205. {
  206. if (!$iContentId)
  207. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  208. if (!$iContentId)
  209. return false;
  210. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  211. if (!$aContentInfo)
  212. return false;
  213. bx_import('BxDolConnection');
  214. $s = $this->serviceBrowseConnectionsQuick ($aContentInfo['profile_id'], 'sys_profiles_friends', BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  215. if (!$s)
  216. return MsgBox(_t('_sys_txt_empty'));
  217. return $s;
  218. }
  219. /**
  220. * For internal usage only.
  221. */
  222. public function serviceDeleteEntityService ($iContentId, $bDeleteWithContent = false)
  223. {
  224. return parent::serviceDeleteEntity ($iContentId, 'deleteDataService');
  225. }
  226. // ====== PERMISSION METHODS
  227. /**
  228. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  229. */
  230. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  231. {
  232. // moderator always has access
  233. if ($this->_isModerator($isPerformAction))
  234. return CHECK_ACTION_RESULT_ALLOWED;
  235. // owner (checked by account! not as profile as ususal) always have access
  236. $oProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->_aModule['name']);
  237. if (!$oProfile)
  238. return _t('_sys_txt_error_occured');
  239. if ($oProfile->getAccountId() == $this->_iAccountId)
  240. return CHECK_ACTION_RESULT_ALLOWED;
  241. return _t('_sys_txt_access_denied');
  242. }
  243. /**
  244. * Check if user can change cover image
  245. */
  246. public function checkAllowedChangeCover ($aDataEntry, $isPerformAction = false)
  247. {
  248. // moderator always has access
  249. if ($this->_isModerator($isPerformAction))
  250. return CHECK_ACTION_RESULT_ALLOWED;
  251. // owner (checked by account! not as profile as ususal) always have access
  252. $oProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->_aModule['name']);
  253. if (!$oProfile)
  254. return _t('_sys_txt_error_occured');
  255. if ($oProfile->getAccountId() == $this->_iAccountId)
  256. return CHECK_ACTION_RESULT_ALLOWED;
  257. return _t('_sys_txt_access_denied');
  258. }
  259. /**
  260. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  261. */
  262. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  263. {
  264. // moderator always has access
  265. if ($this->_isModerator($isPerformAction))
  266. return CHECK_ACTION_RESULT_ALLOWED;
  267. // check ACL and owner (checked by account! not as profile as ususal)
  268. $aCheck = checkActionModule($this->_iProfileId, 'delete entry', $this->getName(), $isPerformAction);
  269. $oProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->_aModule['name']);
  270. if (!$oProfile)
  271. return _t('_sys_txt_error_occured');
  272. if ($oProfile->getAccountId() == $this->_iAccountId && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  273. return CHECK_ACTION_RESULT_ALLOWED;
  274. return _t('_sys_txt_access_denied');
  275. }
  276. /**
  277. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  278. */
  279. public function checkAllowedViewMoreMenu (&$aDataEntry, $isPerformAction = false)
  280. {
  281. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']);
  282. if (!$oMenu || !$oMenu->getCode())
  283. return _t('_sys_txt_access_denied');
  284. return CHECK_ACTION_RESULT_ALLOWED;
  285. }
  286. /**
  287. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  288. */
  289. public function checkAllowedFriendAdd (&$aDataEntry, $isPerformAction = false)
  290. {
  291. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  292. }
  293. /**
  294. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  295. */
  296. public function checkAllowedFriendRemove (&$aDataEntry, $isPerformAction = false)
  297. {
  298. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, true))
  299. return CHECK_ACTION_RESULT_ALLOWED;
  300. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, 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 checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  306. {
  307. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  308. }
  309. /**
  310. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  311. */
  312. public function checkAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false)
  313. {
  314. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  315. }
  316. public function checkMyself($iContentId)
  317. {
  318. $iLogged = (int)bx_get_logged_profile_id();
  319. if(empty($iLogged))
  320. return false;
  321. $oProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->_oConfig->getName());
  322. if(!$oProfile)
  323. return false;
  324. return $oProfile->id() == $iLogged;
  325. }
  326. // ====== PROTECTED METHODS
  327. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  328. {
  329. if (!$this->_iProfileId)
  330. return _t('_sys_txt_access_denied');
  331. $CNF = &$this->_oConfig->CNF;
  332. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[$CNF['FIELD_AUTHOR']]);
  333. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[$CNF['FIELD_ID']], $this->_aModule['name'], $oProfileAuthor->getAccountId()) : false;
  334. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  335. return _t('_sys_txt_access_denied');
  336. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  337. if ($isSwap)
  338. $isConnected = $oConn->isConnected($oProfile->id(), $this->_iProfileId, $isMutual);
  339. else
  340. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  341. if ($isInvertResult)
  342. $isConnected = !$isConnected;
  343. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  344. }
  345. protected function _buildRssParams($sMode, $aArgs)
  346. {
  347. $aParams = array ();
  348. $sMode = bx_process_input($sMode);
  349. switch ($sMode) {
  350. case 'connections':
  351. $aParams = array(
  352. 'object' => isset($aArgs[0]) ? $aArgs[0] : '',
  353. 'type' => isset($aArgs[1]) ? $aArgs[1] : '',
  354. 'profile' => isset($aArgs[2]) ? (int)$aArgs[2] : 0,
  355. 'mutual' => isset($aArgs[3]) ? (int)$aArgs[3] : 0,
  356. 'profile2' => isset($aArgs[4]) ? (int)$aArgs[4] : 0,
  357. );
  358. break;
  359. }
  360. return $aParams;
  361. }
  362. }
  363. /** @} */