BxInvModule.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Invites Invites
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. define('BX_INV_TYPE_FROM_MEMBER', 'from_member');
  13. define('BX_INV_TYPE_FROM_SYSTEM', 'from_system');
  14. class BxInvModule extends BxDolModule
  15. {
  16. /**
  17. * Constructor
  18. */
  19. function __construct($aModule)
  20. {
  21. parent::__construct($aModule);
  22. $this->_oConfig->init($this->_oDb);
  23. }
  24. /**
  25. * ACTION METHODS
  26. */
  27. function actionGetLink()
  28. {
  29. $iProfileId = $this->getProfileId();
  30. $iAccountId = $this->getAccountId($iProfileId);
  31. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  32. if($mixedAllowed !== true)
  33. return echoJson(array('message' => $mixedAllowed));
  34. if(!isAdmin($iAccountId)) {
  35. if($this->_oConfig->getCountPerUser() <= 0)
  36. return echoJson(array('message' => _t('_bx_invites_err_limit_reached')));
  37. }
  38. $oKeys = BxDolKey::getInstance();
  39. if(!$oKeys)
  40. return echoJson(array('message' => _t('_bx_invites_err_not_available')));
  41. $sKey = $oKeys->getNewKey(false, $this->_oConfig->getKeyLifetime());
  42. $sRedirectUrl = '';
  43. $sRedirectCode = $this->_oConfig->getRedirectCode();
  44. if(($oSession = BxDolSession::getInstance()) && $oSession->isValue($sRedirectCode))
  45. $sRedirectUrl = $oSession->getValue($sRedirectCode);
  46. $oForm = $this->getFormObjectInvite();
  47. $oForm->insert(array(
  48. 'account_id' => $iAccountId,
  49. 'profile_id' => $iProfileId,
  50. 'key' => $sKey,
  51. 'redirect' => $sRedirectUrl,
  52. 'email' => '',
  53. 'date' => time()
  54. ));
  55. $this->onInvite($iProfileId);
  56. echoJson(array('popup' => $this->_oTemplate->getLinkPopup(
  57. $this->getJoinLink($sKey)
  58. )));
  59. }
  60. public function actionSetSeenMark($Code)
  61. {
  62. header('Content-Type: image/png');
  63. if (isset($Code) && trim($Code) != "")
  64. $this->_oDb->updateDateSeenForInvite($Code);
  65. }
  66. /**
  67. * SERVICE METHODS
  68. */
  69. public function serviceGetSafeServices()
  70. {
  71. return array (
  72. 'GetBlockInvite' => '',
  73. 'GetBlockFormInvite' => '',
  74. 'GetBlockFormRequest' => '',
  75. );
  76. }
  77. /**
  78. * @page service Service Calls
  79. * @section bx_invites Invitations
  80. * @subsection bx_invites-other Other
  81. * @subsubsection bx_invites-get_include get_include
  82. *
  83. * @code bx_srv('bx_invites', 'get_include', [...]); @endcode
  84. *
  85. * Get all necessary CSS and JS files to include in a page.
  86. *
  87. * @return string with all necessary CSS and JS files.
  88. *
  89. * @see BxInvModule::serviceGetInclude
  90. */
  91. /**
  92. * @ref bx_invites-get_include "get_include"
  93. */
  94. public function serviceGetInclude()
  95. {
  96. return $this->_oTemplate->getInclude();
  97. }
  98. /**
  99. * @page service Service Calls
  100. * @section bx_invites Invitations
  101. * @subsection bx_invites-page_blocks Page Blocks
  102. * @subsubsection bx_invites-get_block_invite get_block_invite
  103. *
  104. * @code bx_srv('bx_invites', 'get_block_invite', [...]); @endcode
  105. *
  106. * Get page block for member's Dashboard which displays invitations related info and action(s).
  107. *
  108. * @return an array describing a block to display on the site or empty string if something is wrong. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  109. *
  110. * @see BxInvModule::serviceGetBlockInvite
  111. */
  112. /**
  113. * @ref bx_invites-get_block_invite "get_block_invite"
  114. */
  115. public function serviceGetBlockInvite($bRedirect = false)
  116. {
  117. $iProfileId = $this->getProfileId();
  118. $iAccountId = $this->getAccountId($iProfileId);
  119. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  120. if($mixedAllowed !== true)
  121. return '';
  122. if(!isAdmin($iAccountId) && $this->_oConfig->getCountPerUser() <= 0)
  123. return '';
  124. return [
  125. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId, (bool)$bRedirect)
  126. ];
  127. }
  128. /**
  129. * @page service Service Calls
  130. * @section bx_invites Invitations
  131. * @subsection bx_invites-page_blocks Page Blocks
  132. * @subsubsection bx_invites-get_block_form_invite get_block_form_invite
  133. *
  134. * @code bx_srv('bx_invites', 'get_block_form_invite', [...]); @endcode
  135. *
  136. * Get page block with invite form.
  137. *
  138. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  139. *
  140. * @see BxInvModule::serviceGetBlockFormInvite
  141. */
  142. /**
  143. * @ref bx_invites-get_block_form_invite "get_block_form_invite"
  144. */
  145. public function serviceGetBlockFormInvite()
  146. {
  147. $oForm = $this->getFormObjectInvite();
  148. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  149. $sResult = '';
  150. $oForm->initChecker();
  151. if($oForm->isSubmittedAndValid())
  152. $sResult = MsgBox($this->processFormObjectInvite($oForm));
  153. return [
  154. 'content' => $sResult . $oForm->getCode()
  155. ];
  156. }
  157. /**
  158. * @page service Service Calls
  159. * @section bx_invites Invitations
  160. * @subsection bx_invites-page_blocks Page Blocks
  161. * @subsubsection bx_invites-get_block_form_request get_block_form_request
  162. *
  163. * @code bx_srv('bx_invites', 'get_block_form_request', [...]); @endcode
  164. *
  165. * Get page block with request invitation form.
  166. *
  167. * @return an array describing a block to display on the site. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  168. *
  169. * @see BxInvModule::serviceGetBlockFormRequest
  170. */
  171. /**
  172. * @ref bx_invites-get_block_form_request "get_block_form_request"
  173. */
  174. public function serviceGetBlockFormRequest()
  175. {
  176. $mixedResult = $this->_oTemplate->getBlockFormRequest();
  177. if(is_array($mixedResult)) {
  178. echoJson($mixedResult);
  179. exit;
  180. }
  181. return array(
  182. 'content' => $mixedResult
  183. );
  184. }
  185. /**
  186. * @page service Service Calls
  187. * @section bx_invites Invitations
  188. * @subsection bx_invites-page_blocks Page Blocks
  189. * @subsubsection bx_invites-get_block_manage_requests get_block_manage_requests
  190. *
  191. * @code bx_srv('bx_invites', 'get_block_manage_requests', [...]); @endcode
  192. *
  193. * Get page block with manage invitation requests table.
  194. *
  195. * @return HTML string with block content to display on the site or empty string if something is wrong. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  196. *
  197. * @see BxInvModule::serviceGetBlockManageRequests
  198. */
  199. /**
  200. * @ref bx_invites-get_block_manage_requests "get_block_manage_requests"
  201. */
  202. public function serviceGetBlockManageRequests()
  203. {
  204. return $this->getBlockManage('requests');
  205. }
  206. /**
  207. * @page service Service Calls
  208. * @section bx_invites Invitations
  209. * @subsection bx_invites-page_blocks Page Blocks
  210. * @subsubsection bx_invites-get_block_manage_invites get_block_manage_requests
  211. *
  212. * @code bx_srv('bx_invites', 'get_block_manage_invites', [...]); @endcode
  213. *
  214. * Get page block with manage invitations table.
  215. *
  216. * @return HTML string with block content to display on the site or empty string if something is wrong. All necessary CSS and JS files are automatically added to the HEAD section of the site HTML.
  217. *
  218. * @see BxInvModule::serviceGetBlockManageInvites
  219. */
  220. /**
  221. * @ref bx_invites-get_block_manage_invites "get_block_manage_invites"
  222. */
  223. public function serviceGetBlockManageInvites()
  224. {
  225. return $this->getBlockManage('invites');
  226. }
  227. /**
  228. * @page service Service Calls
  229. * @section bx_invites Invitations
  230. * @subsection bx_invites-other Other
  231. * @subsubsection bx_invites-get_menu_addon_requests get_menu_addon_requests
  232. *
  233. * @code bx_srv('bx_invites', 'get_menu_addon_requests', [...]); @endcode
  234. *
  235. * Get number or invitation requests.
  236. *
  237. * @return integer value with number of invitation requests.
  238. *
  239. * @see BxInvModule::serviceGetMenuAddonRequests
  240. */
  241. /**
  242. * @ref bx_invites-get_menu_addon_requests "get_menu_addon_requests"
  243. */
  244. public function serviceGetMenuAddonRequests()
  245. {
  246. return array('counter3_value' => $this->_oDb->getRequests(array('type' => 'count_all')));
  247. }
  248. /**
  249. * @page service Service Calls
  250. * @section bx_invites Invitations
  251. * @subsection bx_invites-other Other
  252. * @subsubsection bx_invites-account_add_form_check account_add_form_check
  253. *
  254. * @code bx_srv('bx_invites', 'account_add_form_check', [...]); @endcode
  255. *
  256. * Perform neccessary checking on join form.
  257. *
  258. * @return empty string - if join is allowed and should be processed as usual, non-empty string - if join form need to be replaced with this code.
  259. *
  260. * @see BxInvModule::serviceAccountAddFormCheck
  261. */
  262. /**
  263. * @ref bx_invites-account_add_form_check "account_add_form_check"
  264. */
  265. public function serviceAccountAddFormCheck()
  266. {
  267. $sReturn = '';
  268. $oSession = BxDolSession::getInstance();
  269. $sKeyCode = $this->_oConfig->getKeyCode();
  270. $bKey = bx_get($sKeyCode) !== false;
  271. if($bKey) {
  272. $sKey = bx_process_input(bx_get($sKeyCode));
  273. $oKeys = BxDolKey::getInstance();
  274. if($oKeys && $oKeys->isKeyExists($sKey))
  275. $oSession->setValue($sKeyCode, $sKey);
  276. }
  277. $sKey = $oSession->getValue($sKeyCode);
  278. if($sKey !== false)
  279. return $sReturn;
  280. if($bKey)
  281. $sReturn .= MsgBox(_t('_bx_invites_err_used'));
  282. if ($this->_oConfig->isRegistrationByInvitation())
  283. $sReturn .= $this->_oTemplate->getBlockRequest();
  284. return $sReturn;
  285. }
  286. /**
  287. * Data for Notifications module
  288. */
  289. public function serviceGetNotificationsData()
  290. {
  291. $sModule = $this->_aModule['name'];
  292. $sEventPrivacy = $sModule . '_allow_view_event_to';
  293. if(BxDolPrivacy::getObjectInstance($sEventPrivacy) === false)
  294. $sEventPrivacy = '';
  295. return array(
  296. 'handlers' => array(
  297. array('group' => $sModule . '_object', 'type' => 'insert', 'alert_unit' => $sModule, 'alert_action' => 'request_notify', 'module_name' => $sModule, 'module_method' => 'get_notifications_request', 'module_class' => 'Module', 'module_event_privacy' => $sEventPrivacy),
  298. ),
  299. 'settings' => array(
  300. array('group' => 'request', 'unit' => $sModule, 'action' => 'request_notify', 'types' => array('personal')),
  301. ),
  302. 'alerts' => array(
  303. array('unit' => $sModule, 'action' => 'request_notify'),
  304. )
  305. );
  306. }
  307. /**
  308. * Entry post for Notifications module
  309. */
  310. public function serviceGetNotificationsRequest($aEvent)
  311. {
  312. if (getParam('bx_invites_requests_notifications') != 'on')
  313. return [];
  314. $aRequest = $this->_oDb->getRequests(array('type' => 'by_id', 'value' => $aEvent['object_id']));
  315. if (!$aRequest)
  316. return [];
  317. $CNF = &$this->_oConfig->CNF;
  318. $sEntryUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($CNF['URL_REQUESTS']), '{bx_url_root}');
  319. return [
  320. 'entry_sample' => $aRequest['name'] . '(' . $aRequest['email'] . ')',
  321. 'entry_url' => $sEntryUrl,
  322. 'entry_caption' => $aRequest['text'],
  323. 'entry_author' => '',
  324. 'entry_privacy' => '', //may be empty or not specified. In this case Public privacy will be used.
  325. 'lang_key' => '_bx_invites_alert_action_request', //may be empty or not specified. In this case the default one from Notification module will be used.
  326. ];
  327. }
  328. public function attachAccountIdToInvite($iAccountId, $sKey)
  329. {
  330. $this->_oDb->attachAccountIdToInvite($iAccountId, $sKey);
  331. }
  332. public function invite($sType, $aEmails, $sText, $mixedLimit)
  333. {
  334. if(empty($aEmails) || !is_array($aEmails))
  335. return false;
  336. $oKeys = BxDolKey::getInstance();
  337. if(!$oKeys || !in_array($sType, [BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM]))
  338. return false;
  339. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  340. $sRedirectUrl = '';
  341. $sRedirectCode = $this->_oConfig->getRedirectCode();
  342. if(($oSession = BxDolSession::getInstance()) && $oSession->isValue($sRedirectCode))
  343. $sRedirectUrl = $oSession->getValue($sRedirectCode);
  344. $sEmailTemplate = '';
  345. switch($sType) {
  346. case BX_INV_TYPE_FROM_MEMBER:
  347. $sEmailTemplate = 'bx_invites_invite_form_message';
  348. break;
  349. case BX_INV_TYPE_FROM_SYSTEM:
  350. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  351. break;
  352. }
  353. $iDate = time();
  354. $iProfileId = $this->getProfileId();
  355. $iAccountId = $this->getAccountId($iProfileId);
  356. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  357. 'text' => $sText
  358. ), $iAccountId, $iProfileId);
  359. $aResults = [];
  360. foreach($aEmails as $sEmail) {
  361. if($mixedLimit !== false && (int)$mixedLimit <= 0)
  362. break;
  363. $sEmail = trim($sEmail);
  364. if(empty($sEmail))
  365. continue;
  366. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  367. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => $this->getJoinLink($sKey), 'seen_image_url' => $this->getSeenImageUrl($sKey)), BX_EMAIL_SYSTEM)) {
  368. $iInviteId = (int)$this->_oDb->insertInvite([
  369. 'account_id' => $iAccountId,
  370. 'profile_id' => $iProfileId,
  371. 'key' => $sKey,
  372. 'redirect' => $sRedirectUrl,
  373. 'email' => $sEmail,
  374. 'date' => $iDate
  375. ]);
  376. $this->onInvite($iProfileId);
  377. $aResults[] = $sEmail;
  378. if($mixedLimit !== false)
  379. $mixedLimit -= 1;
  380. }
  381. }
  382. return $aResults;
  383. }
  384. public function processFormObjectInvite(&$oForm)
  385. {
  386. $iProfileId = $this->getProfileId();
  387. $iAccountId = $this->getAccountId($iProfileId);
  388. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  389. if($mixedAllowed !== true)
  390. return $mixedAllowed;
  391. $mixedInvites = false;
  392. if(!isAdmin($iAccountId)) {
  393. if(($mixedInvites = $this->_oConfig->getCountPerUser()) <= 0)
  394. return _t('_bx_invites_err_limit_reached');
  395. }
  396. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  397. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  398. $sText = bx_process_pass($oForm->getCleanValue('text'));
  399. $sResult = _t('_bx_invites_err_not_available');
  400. if(($aEmailsSent = $this->invite(BX_INV_TYPE_FROM_MEMBER, $aEmails, $sText, $mixedInvites)) !== false) {
  401. $sEmailsResult = '';
  402. if(count($aEmails) != count($aEmailsSent))
  403. $sEmailsResult = implode('; ', array_diff($aEmails, $aEmailsSent));
  404. $oForm->aInputs['emails']['value'] = $sEmailsResult;
  405. $sResult = _t('_bx_invites_msg_invitation_sent', count($aEmailsSent));
  406. }
  407. return $sResult;
  408. }
  409. public function isAllowedInvite($iProfileId, $bPerform = false)
  410. {
  411. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  412. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  413. }
  414. public function isAllowedRequest($iProfileId, $bPerform = false)
  415. {
  416. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  417. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  418. }
  419. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  420. {
  421. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  422. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  423. }
  424. public function isAllowedDeleteInvite($iProfileId, $bPerform = false)
  425. {
  426. $aCheckResult = checkActionModule($iProfileId, 'delete invite', $this->getName(), $bPerform);
  427. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  428. }
  429. public function getProfileId()
  430. {
  431. return isLogged() ? bx_get_logged_profile_id() : 0;
  432. }
  433. public function getProfileObject($iProfileId = 0)
  434. {
  435. $oProfile = BxDolProfile::getInstance($iProfileId);
  436. if (!$oProfile)
  437. $oProfile = BxDolProfileUndefined::getInstance();
  438. return $oProfile;
  439. }
  440. public function getAccountId($iProfileId)
  441. {
  442. $oProfile = $this->getProfileObject($iProfileId);
  443. if($oProfile->id() == 0)
  444. return 0;
  445. return $oProfile->getAccountId();
  446. }
  447. public function onRequest($iRequestId)
  448. {
  449. //--- Event -> Request for Alerts Engine ---//
  450. bx_alert($this->_oConfig->getObject('alert'), 'request', $iRequestId);
  451. $aProfiles = BxDolAclQuery::getInstance()->getProfilesByMembership(array(MEMBERSHIP_ID_MODERATOR, MEMBERSHIP_ID_ADMINISTRATOR));
  452. foreach($aProfiles as $aProfile) {
  453. bx_alert($this->_oConfig->getObject('alert'), 'request_notify', $iRequestId, 0, array('parent_author_id' => $aProfile['id']));
  454. }
  455. //--- Event -> Request for Alerts Engine ---//
  456. }
  457. public function getFormObjectInvite($sDisplay = '')
  458. {
  459. if(empty($sDisplay))
  460. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  461. bx_import('FormCheckerHelper', $this->_aModule);
  462. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  463. }
  464. protected function getBlockManage($sType)
  465. {
  466. $this->_oTemplate->addJs('jquery.form.min.js');
  467. $this->_oTemplate->addJs('main.js');
  468. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_' . $sType));
  469. if(!$oGrid)
  470. return '';
  471. $this->_oTemplate->addCss(array('main.css'));
  472. $this->_oTemplate->addJsTranslation(array('_sys_grid_search'));
  473. return array(
  474. 'menu' => $this->_oTemplate->getMenuForManageBlocks($sType),
  475. 'content' => $this->_oTemplate->getJsCode('main', array('grid' => $sType)) . $oGrid->getCode()
  476. );
  477. }
  478. public function onInvite($iProfileId)
  479. {
  480. $this->isAllowedInvite($iProfileId, true);
  481. //--- Event -> Invite for Alerts Engine ---//
  482. bx_alert($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  483. //--- Event -> Invite for Alerts Engine ---//
  484. }
  485. public function getJoinLink($sKey)
  486. {
  487. $sKeyCode = $this->_oConfig->getKeyCode();
  488. $sJoinUrl = bx_absolute_url(BxDolPermalinks::getInstance()->permalink('page.php?i=create-account'));
  489. return bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey));
  490. }
  491. public function getSeenImageUrl($sKey)
  492. {
  493. return BX_DOL_URL_ROOT . $this->_oConfig->getBaseUri() . 'SetSeenMark/' . $sKey . "/";
  494. }
  495. }
  496. /** @} */