1
0

BxBaseModProfileModule.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  190. if (!$iContentId)
  191. return false;
  192. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  193. if (!$aContentInfo)
  194. return false;
  195. return $this->_oTemplate->cover($aContentInfo);
  196. }
  197. public function serviceProfileFriends ($iContentId = 0)
  198. {
  199. if (!$iContentId)
  200. $iContentId = bx_process_input(bx_get('id'), BX_DATA_INT);
  201. if (!$iContentId)
  202. return false;
  203. $aContentInfo = $this->_oDb->getContentInfoById($iContentId);
  204. if (!$aContentInfo)
  205. return false;
  206. bx_import('BxDolConnection');
  207. $s = $this->serviceBrowseConnectionsQuick ($aContentInfo['profile_id'], 'sys_profiles_friends', BX_CONNECTIONS_CONTENT_TYPE_CONTENT, true);
  208. if (!$s)
  209. return MsgBox(_t('_sys_txt_empty'));
  210. return $s;
  211. }
  212. /**
  213. * For internal usage only.
  214. */
  215. public function serviceDeleteEntityService ($iContentId, $bDeleteWithContent = false)
  216. {
  217. return parent::serviceDeleteEntity ($iContentId, 'deleteDataService');
  218. }
  219. // ====== PERMISSION METHODS
  220. /**
  221. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  222. */
  223. public function checkAllowedEdit ($aDataEntry, $isPerformAction = false)
  224. {
  225. // moderator always has access
  226. if ($this->_isModerator($isPerformAction))
  227. return CHECK_ACTION_RESULT_ALLOWED;
  228. // owner (checked by account! not as profile as ususal) always have access
  229. $oProfile = BxDolProfile::getInstanceByContentAndType($aDataEntry[$this->_oConfig->CNF['FIELD_ID']], $this->_aModule['name']);
  230. if (!$oProfile)
  231. return _t('_sys_txt_error_occured');
  232. if ($oProfile->getAccountId() == $this->_iAccountId)
  233. return CHECK_ACTION_RESULT_ALLOWED;
  234. return _t('_sys_txt_access_denied');
  235. }
  236. /**
  237. * Check if user can change cover image
  238. */
  239. public function checkAllowedChangeCover ($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. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  254. */
  255. public function checkAllowedDelete (&$aDataEntry, $isPerformAction = false)
  256. {
  257. // moderator always has access
  258. if ($this->_isModerator($isPerformAction))
  259. return CHECK_ACTION_RESULT_ALLOWED;
  260. // check ACL and owner (checked by account! not as profile as ususal)
  261. $aCheck = checkActionModule($this->_iProfileId, 'delete entry', $this->getName(), $isPerformAction);
  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 && $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED)
  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 checkAllowedViewMoreMenu (&$aDataEntry, $isPerformAction = false)
  273. {
  274. $oMenu = BxTemplMenu::getObjectInstance($this->_oConfig->CNF['OBJECT_MENU_ACTIONS_VIEW_ENTRY_MORE']);
  275. if (!$oMenu || !$oMenu->getCode())
  276. return _t('_sys_txt_access_denied');
  277. return CHECK_ACTION_RESULT_ALLOWED;
  278. }
  279. /**
  280. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  281. */
  282. public function checkAllowedFriendAdd (&$aDataEntry, $isPerformAction = false)
  283. {
  284. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', true, false);
  285. }
  286. /**
  287. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  288. */
  289. public function checkAllowedFriendRemove (&$aDataEntry, $isPerformAction = false)
  290. {
  291. if (CHECK_ACTION_RESULT_ALLOWED === $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, true))
  292. return CHECK_ACTION_RESULT_ALLOWED;
  293. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_friends', false, true, false);
  294. }
  295. /**
  296. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  297. */
  298. public function checkAllowedSubscribeAdd (&$aDataEntry, $isPerformAction = false)
  299. {
  300. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, false);
  301. }
  302. /**
  303. * @return CHECK_ACTION_RESULT_ALLOWED if access is granted or error message if access is forbidden.
  304. */
  305. public function checkAllowedSubscribeRemove (&$aDataEntry, $isPerformAction = false)
  306. {
  307. return $this->_checkAllowedConnect ($aDataEntry, $isPerformAction, 'sys_profiles_subscriptions', false, true);
  308. }
  309. public function checkMyself($iContentId)
  310. {
  311. $iLogged = (int)bx_get_logged_profile_id();
  312. if(empty($iLogged))
  313. return false;
  314. $oProfile = BxDolProfile::getInstanceByContentAndType((int)$iContentId, $this->_oConfig->getName());
  315. if(!$oProfile)
  316. return false;
  317. return $oProfile->id() == $iLogged;
  318. }
  319. // ====== PROTECTED METHODS
  320. protected function _checkAllowedConnect (&$aDataEntry, $isPerformAction, $sObjConnection, $isMutual, $isInvertResult, $isSwap = false)
  321. {
  322. if (!$this->_iProfileId)
  323. return _t('_sys_txt_access_denied');
  324. $CNF = &$this->_oConfig->CNF;
  325. $oProfileAuthor = BxDolProfile::getInstance($aDataEntry[$CNF['FIELD_AUTHOR']]);
  326. $oProfile = $oProfileAuthor ? BxDolProfile::getInstanceByContentTypeAccount($aDataEntry[$CNF['FIELD_ID']], $this->_aModule['name'], $oProfileAuthor->getAccountId()) : false;
  327. if (!$oProfile || $oProfile->id() == $this->_iProfileId)
  328. return _t('_sys_txt_access_denied');
  329. $oConn = BxDolConnection::getObjectInstance($sObjConnection);
  330. if ($isSwap)
  331. $isConnected = $oConn->isConnected($oProfile->id(), $this->_iProfileId, $isMutual);
  332. else
  333. $isConnected = $oConn->isConnected($this->_iProfileId, $oProfile->id(), $isMutual);
  334. if ($isInvertResult)
  335. $isConnected = !$isConnected;
  336. return $isConnected ? _t('_sys_txt_access_denied') : CHECK_ACTION_RESULT_ALLOWED;
  337. }
  338. protected function _buildRssParams($sMode, $aArgs)
  339. {
  340. $aParams = array ();
  341. $sMode = bx_process_input($sMode);
  342. switch ($sMode) {
  343. case 'connections':
  344. $aParams = array(
  345. 'object' => isset($aArgs[0]) ? $aArgs[0] : '',
  346. 'type' => isset($aArgs[1]) ? $aArgs[1] : '',
  347. 'profile' => isset($aArgs[2]) ? (int)$aArgs[2] : 0,
  348. 'mutual' => isset($aArgs[3]) ? (int)$aArgs[3] : 0,
  349. 'profile2' => isset($aArgs[4]) ? (int)$aArgs[4] : 0,
  350. );
  351. break;
  352. }
  353. return $aParams;
  354. }
  355. }
  356. /** @} */