BxInvModule.php 19 KB

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