BxInvModule.php 19 KB

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