BxInvModule.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. /**
  56. * SERVICE METHODS
  57. */
  58. /**
  59. * @page service Service Calls
  60. * @section bx_invites Invitations
  61. * @subsection bx_invites-page_blocks Page Blocks
  62. * @subsubsection bx_invites-get_block_invite get_block_invite
  63. *
  64. * @code bx_srv('bx_invites', 'get_block_invite', [...]); @endcode
  65. *
  66. * Get page block for member's Dashboard which displays invitations related info and action(s).
  67. *
  68. * @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.
  69. *
  70. * @see BxInvModule::serviceGetBlockInvite
  71. */
  72. /**
  73. * @ref bx_invites-get_block_invite "get_block_invite"
  74. */
  75. public function serviceGetBlockInvite()
  76. {
  77. $iProfileId = $this->getProfileId();
  78. $iAccountId = $this->getAccountId($iProfileId);
  79. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  80. if($mixedAllowed !== true)
  81. return '';
  82. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  83. if(!isAdmin($iAccountId) && $iInvited >= $this->_oConfig->getCountPerUser())
  84. return '';
  85. return array(
  86. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId)
  87. );
  88. }
  89. /**
  90. * @page service Service Calls
  91. * @section bx_invites Invitations
  92. * @subsection bx_invites-page_blocks Page Blocks
  93. * @subsubsection bx_invites-get_block_form_invite get_block_form_invite
  94. *
  95. * @code bx_srv('bx_invites', 'get_block_form_invite', [...]); @endcode
  96. *
  97. * Get page block with invite form.
  98. *
  99. * @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.
  100. *
  101. * @see BxInvModule::serviceGetBlockFormInvite
  102. */
  103. /**
  104. * @ref bx_invites-get_block_form_invite "get_block_form_invite"
  105. */
  106. public function serviceGetBlockFormInvite()
  107. {
  108. $iProfileId = $this->getProfileId();
  109. $iAccountId = $this->getAccountId($iProfileId);
  110. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  111. if($mixedAllowed !== true)
  112. return array(
  113. 'content' => MsgBox($mixedAllowed)
  114. );
  115. $mixedInvites = false;
  116. if(!isAdmin($iAccountId)) {
  117. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  118. $mixedInvites = $this->_oConfig->getCountPerUser() - $iInvited;
  119. if($mixedInvites <= 0)
  120. return array(
  121. 'content' => MsgBox(_t('_bx_invites_err_limit_reached'))
  122. );
  123. }
  124. $oForm = $this->getFormObjectInvite();
  125. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  126. $sResult = '';
  127. $oForm->initChecker();
  128. if($oForm->isSubmittedAndValid()) {
  129. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  130. $sText = bx_process_pass($oForm->getCleanValue('text'));
  131. $mixedResult = $this->invite(BX_INV_TYPE_FROM_MEMBER, $sEmails, $sText, $mixedInvites, $oForm);
  132. if($mixedResult !== false)
  133. $sResult = _t('_bx_invites_msg_invitation_sent', (int)$mixedResult);
  134. else
  135. $sResult = _t('_bx_invites_err_not_available');
  136. $sResult = MsgBox($sResult);
  137. }
  138. return array(
  139. 'content' => $sResult . $oForm->getCode()
  140. );
  141. }
  142. /**
  143. * @page service Service Calls
  144. * @section bx_invites Invitations
  145. * @subsection bx_invites-page_blocks Page Blocks
  146. * @subsubsection bx_invites-get_block_form_request get_block_form_request
  147. *
  148. * @code bx_srv('bx_invites', 'get_block_form_request', [...]); @endcode
  149. *
  150. * Get page block with request invitation form.
  151. *
  152. * @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.
  153. *
  154. * @see BxInvModule::serviceGetBlockFormRequest
  155. */
  156. /**
  157. * @ref bx_invites-get_block_form_request "get_block_form_request"
  158. */
  159. public function serviceGetBlockFormRequest()
  160. {
  161. if(!$this->_oConfig->isRequestInvite())
  162. return array(
  163. 'content' => MsgBox(_t('_bx_invites_err_not_available'))
  164. );
  165. $mixedAllowed = $this->isAllowedRequest(0);
  166. if($mixedAllowed !== true)
  167. return array(
  168. 'content' => MsgBox($mixedAllowed)
  169. );
  170. $sResult = '';
  171. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_request'), $this->_oConfig->getObject('form_display_request_send'));
  172. $oForm->initChecker();
  173. if($oForm->isSubmittedAndValid()) {
  174. $sIp = getVisitorIP();
  175. $iId = (int)$oForm->insert(array(
  176. 'nip' => ip2long($sIp),
  177. 'date' => time()
  178. ));
  179. if($iId !== false) {
  180. $sRequestsEmail = $this->_oConfig->getRequestsEmail();
  181. if(!empty($sRequestsEmail)) {
  182. $sManageUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=invites-requests');
  183. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_request_form_message', array(
  184. 'sender_name' => bx_process_output($oForm->getCleanValue('name')),
  185. 'sender_email' => bx_process_output($oForm->getCleanValue('email')),
  186. 'sender_ip' => $sIp,
  187. 'manage_url' => $sManageUrl
  188. ));
  189. sendMail($sRequestsEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
  190. }
  191. $sResult = MsgBox(_t('_bx_invites_msg_request_sent'));
  192. }
  193. }
  194. return array(
  195. 'content' => $sResult . $oForm->getCode()
  196. );
  197. }
  198. /**
  199. * @page service Service Calls
  200. * @section bx_invites Invitations
  201. * @subsection bx_invites-page_blocks Page Blocks
  202. * @subsubsection bx_invites-get_block_manage_requests get_block_manage_requests
  203. *
  204. * @code bx_srv('bx_invites', 'get_block_manage_requests', [...]); @endcode
  205. *
  206. * Get page block with manage invitation requests table.
  207. *
  208. * @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.
  209. *
  210. * @see BxInvModule::serviceGetBlockManageRequests
  211. */
  212. /**
  213. * @ref bx_invites-get_block_manage_requests "get_block_manage_requests"
  214. */
  215. public function serviceGetBlockManageRequests()
  216. {
  217. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_requests'));
  218. if(!$oGrid)
  219. return '';
  220. $this->_oTemplate->addCss(array('main.css'));
  221. return $oGrid->getCode();
  222. }
  223. /**
  224. * @page service Service Calls
  225. * @section bx_invites Invitations
  226. * @subsection bx_invites-other Other
  227. * @subsubsection bx_invites-get_menu_addon_requests get_menu_addon_requests
  228. *
  229. * @code bx_srv('bx_invites', 'get_menu_addon_requests', [...]); @endcode
  230. *
  231. * Get number or invitation requests.
  232. *
  233. * @return integer value with number of invitation requests.
  234. *
  235. * @see BxInvModule::serviceGetMenuAddonRequests
  236. */
  237. /**
  238. * @ref bx_invites-get_menu_addon_requests "get_menu_addon_requests"
  239. */
  240. public function serviceGetMenuAddonRequests()
  241. {
  242. return $this->_oDb->getRequests(array('type' => 'count_all'));
  243. }
  244. /**
  245. * @page service Service Calls
  246. * @section bx_invites Invitations
  247. * @subsection bx_invites-other Other
  248. * @subsubsection bx_invites-account_add_form_check account_add_form_check
  249. *
  250. * @code bx_srv('bx_invites', 'account_add_form_check', [...]); @endcode
  251. *
  252. * Perform neccessary checking on join form.
  253. *
  254. * @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.
  255. *
  256. * @see BxInvModule::serviceAccountAddFormCheck
  257. */
  258. /**
  259. * @ref bx_invites-account_add_form_check "account_add_form_check"
  260. */
  261. public function serviceAccountAddFormCheck()
  262. {
  263. $sReturn = '';
  264. if (!$this->_oConfig->isRegistrationByInvitation())
  265. return $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. $sReturn .= $this->_oTemplate->getBlockRequest();
  281. return $sReturn;
  282. }
  283. public function invite($sType, $sEmails, $sText, $mixedLimit = false, $oForm = null)
  284. {
  285. $iProfileId = $this->getProfileId();
  286. $iAccountId = $this->getAccountId($iProfileId);
  287. $oKeys = BxDolKey::getInstance();
  288. if(!$oKeys || !in_array($sType, array(BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM)))
  289. return false;
  290. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  291. $sEmailTemplate = '';
  292. switch($sType) {
  293. case BX_INV_TYPE_FROM_MEMBER:
  294. $sEmailTemplate = 'bx_invites_invite_form_message';
  295. break;
  296. case BX_INV_TYPE_FROM_SYSTEM:
  297. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  298. break;
  299. }
  300. if(empty($oForm))
  301. $oForm = $this->getFormObjectInvite();
  302. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  303. 'text' => $sText
  304. ), $iAccountId, $iProfileId);
  305. $iSent = 0;
  306. $iDate = time();
  307. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  308. if(is_array($aEmails) && !empty($aEmails))
  309. foreach($aEmails as $sEmail) {
  310. if($mixedLimit !== false && (int)$mixedLimit <= 0)
  311. break;
  312. $sEmail = trim($sEmail);
  313. if(empty($sEmail))
  314. continue;
  315. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  316. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => $this->getJoinLink($sKey)), BX_EMAIL_SYSTEM)) {
  317. $oForm->insert(array(
  318. 'account_id' => $iAccountId,
  319. 'profile_id' => $iProfileId,
  320. 'key' => $sKey,
  321. 'email' => $sEmail,
  322. 'date' => $iDate
  323. ));
  324. $this->onInvite($iAccountId, $iProfileId);
  325. $iSent += 1;
  326. if($mixedLimit !== false)
  327. $mixedLimit -= 1;
  328. }
  329. }
  330. return $iSent;
  331. }
  332. public function isAllowedInvite($iProfileId, $bPerform = false)
  333. {
  334. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  335. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  336. }
  337. public function isAllowedRequest($iProfileId, $bPerform = false)
  338. {
  339. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  340. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  341. }
  342. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  343. {
  344. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  345. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  346. }
  347. public function getProfileId()
  348. {
  349. return isLogged() ? bx_get_logged_profile_id() : 0;
  350. }
  351. public function getProfileObject($iProfileId = 0)
  352. {
  353. $oProfile = BxDolProfile::getInstance($iProfileId);
  354. if (!$oProfile)
  355. $oProfile = BxDolProfileUndefined::getInstance();
  356. return $oProfile;
  357. }
  358. public function getAccountId($iProfileId)
  359. {
  360. $oProfile = $this->getProfileObject($iProfileId);
  361. if($oProfile->id() == 0)
  362. return 0;
  363. return $oProfile->getAccountId();
  364. }
  365. protected function onInvite($iAccountId, $iProfileId)
  366. {
  367. $this->isAllowedInvite($iProfileId, true);
  368. //--- Event -> Invite for Alerts Engine ---//
  369. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  370. $oAlert->alert();
  371. //--- Event -> Invite for Alerts Engine ---//
  372. }
  373. protected function onRequest()
  374. {
  375. //--- Event -> Request for Alerts Engine ---//
  376. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'request');
  377. $oAlert->alert();
  378. //--- Event -> Request for Alerts Engine ---//
  379. }
  380. protected function getFormObjectInvite($sDisplay = '')
  381. {
  382. if(empty($sDisplay))
  383. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  384. bx_import('FormCheckerHelper', $this->_aModule);
  385. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  386. }
  387. protected function getJoinLink($sKey)
  388. {
  389. $sKeyCode = $this->_oConfig->getKeyCode();
  390. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  391. return bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey));
  392. }
  393. }
  394. /** @} */