BxInvModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 Invites Invites
  7. * @ingroup TridentModules
  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. public function serviceGetBlockInvite()
  59. {
  60. $iProfileId = $this->getProfileId();
  61. $iAccountId = $this->getAccountId($iProfileId);
  62. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  63. if($mixedAllowed !== true)
  64. return '';
  65. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  66. if(!isAdmin($iAccountId) && $iInvited >= $this->_oConfig->getCountPerUser())
  67. return '';
  68. return array(
  69. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId)
  70. );
  71. }
  72. public function serviceGetBlockFormInvite()
  73. {
  74. $iProfileId = $this->getProfileId();
  75. $iAccountId = $this->getAccountId($iProfileId);
  76. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  77. if($mixedAllowed !== true)
  78. return array(
  79. 'content' => MsgBox($mixedAllowed)
  80. );
  81. $mixedInvites = false;
  82. if(!isAdmin($iAccountId)) {
  83. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  84. $mixedInvites = $this->_oConfig->getCountPerUser() - $iInvited;
  85. if($mixedInvites <= 0)
  86. return array(
  87. 'content' => MsgBox(_t('_bx_invites_err_limit_reached'))
  88. );
  89. }
  90. $oForm = $this->getFormObjectInvite();
  91. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  92. $sResult = '';
  93. $oForm->initChecker();
  94. if($oForm->isSubmittedAndValid()) {
  95. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  96. $sText = bx_process_pass($oForm->getCleanValue('text'));
  97. $mixedResult = $this->invite(BX_INV_TYPE_FROM_MEMBER, $sEmails, $sText, $mixedInvites, $oForm);
  98. if($mixedResult !== false)
  99. $sResult = _t('_bx_invites_msg_invitation_sent', (int)$mixedResult);
  100. else
  101. $sResult = _t('_bx_invites_err_not_available');
  102. $sResult = MsgBox($sResult);
  103. }
  104. return array(
  105. 'content' => $sResult . $oForm->getCode()
  106. );
  107. }
  108. public function serviceGetBlockFormRequest()
  109. {
  110. if(!$this->_oConfig->isRequestInvite())
  111. return array(
  112. 'content' => MsgBox(_t('_bx_invites_err_not_available'))
  113. );
  114. $mixedAllowed = $this->isAllowedRequest(0);
  115. if($mixedAllowed !== true)
  116. return array(
  117. 'content' => MsgBox($mixedAllowed)
  118. );
  119. $sResult = '';
  120. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_request'), $this->_oConfig->getObject('form_display_request_send'));
  121. $oForm->initChecker();
  122. if($oForm->isSubmittedAndValid()) {
  123. $sIp = getVisitorIP();
  124. $iId = (int)$oForm->insert(array(
  125. 'nip' => ip2long($sIp),
  126. 'date' => time()
  127. ));
  128. if($iId !== false) {
  129. $sRequestsEmail = $this->_oConfig->getRequestsEmail();
  130. if(!empty($sRequestsEmail)) {
  131. $sManageUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=invites-requests');
  132. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_request_form_message', array(
  133. 'sender_name' => bx_process_output($oForm->getCleanValue('name')),
  134. 'sender_email' => bx_process_output($oForm->getCleanValue('email')),
  135. 'sender_ip' => $sIp,
  136. 'manage_url' => $sManageUrl
  137. ));
  138. sendMail($sRequestsEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
  139. }
  140. $sResult = MsgBox(_t('_bx_invites_msg_request_sent'));
  141. }
  142. }
  143. return array(
  144. 'content' => $sResult . $oForm->getCode()
  145. );
  146. }
  147. public function serviceGetBlockManageRequests()
  148. {
  149. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_requests'));
  150. if(!$oGrid)
  151. return '';
  152. $this->_oTemplate->addCss(array('main.css'));
  153. return $oGrid->getCode();
  154. }
  155. public function serviceGetMenuAddonRequests()
  156. {
  157. return $this->_oDb->getRequests(array('type' => 'count_all'));
  158. }
  159. /**
  160. * Perform neccessary checking on join form
  161. * @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
  162. */
  163. public function serviceAccountAddFormCheck()
  164. {
  165. $sReturn = '';
  166. if (!$this->_oConfig->isRegistrationByInvitation())
  167. return $sReturn;
  168. $oSession = BxDolSession::getInstance();
  169. $sKeyCode = $this->_oConfig->getKeyCode();
  170. $bKey = bx_get($sKeyCode) !== false;
  171. if($bKey) {
  172. $sKey = bx_process_input(bx_get($sKeyCode));
  173. $oKeys = BxDolKey::getInstance();
  174. if($oKeys && $oKeys->isKeyExists($sKey))
  175. $oSession->setValue($sKeyCode, $sKey);
  176. }
  177. $sKey = $oSession->getValue($sKeyCode);
  178. if($sKey !== false)
  179. return $sReturn;
  180. if($bKey)
  181. $sReturn .= MsgBox(_t('_bx_invites_err_used'));
  182. $sReturn .= $this->_oTemplate->getBlockRequest();
  183. return $sReturn;
  184. }
  185. public function invite($sType, $sEmails, $sText, $mixedLimit = false, $oForm = null)
  186. {
  187. $iProfileId = $this->getProfileId();
  188. $iAccountId = $this->getAccountId($iProfileId);
  189. $oKeys = BxDolKey::getInstance();
  190. if(!$oKeys || !in_array($sType, array(BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM)))
  191. return false;
  192. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  193. $sEmailTemplate = '';
  194. switch($sType) {
  195. case BX_INV_TYPE_FROM_MEMBER:
  196. $sEmailTemplate = 'bx_invites_invite_form_message';
  197. break;
  198. case BX_INV_TYPE_FROM_SYSTEM:
  199. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  200. break;
  201. }
  202. if(empty($oForm))
  203. $oForm = $this->getFormObjectInvite();
  204. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  205. 'text' => $sText
  206. ), $iAccountId, $iProfileId);
  207. $iSent = 0;
  208. $iDate = time();
  209. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  210. if(is_array($aEmails) && !empty($aEmails))
  211. foreach($aEmails as $sEmail) {
  212. if($mixedLimit !== false && (int)$mixedLimit <= 0)
  213. break;
  214. $sEmail = trim($sEmail);
  215. if(empty($sEmail))
  216. continue;
  217. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  218. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => $this->getJoinLink($sKey)), BX_EMAIL_SYSTEM)) {
  219. $oForm->insert(array(
  220. 'account_id' => $iAccountId,
  221. 'profile_id' => $iProfileId,
  222. 'key' => $sKey,
  223. 'email' => $sEmail,
  224. 'date' => $iDate
  225. ));
  226. $this->onInvite($iAccountId, $iProfileId);
  227. $iSent += 1;
  228. if($mixedLimit !== false)
  229. $mixedLimit -= 1;
  230. }
  231. }
  232. return $iSent;
  233. }
  234. public function isAllowedInvite($iProfileId, $bPerform = false)
  235. {
  236. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  237. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  238. }
  239. public function isAllowedRequest($iProfileId, $bPerform = false)
  240. {
  241. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  242. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  243. }
  244. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  245. {
  246. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  247. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  248. }
  249. public function getProfileId()
  250. {
  251. return isLogged() ? bx_get_logged_profile_id() : 0;
  252. }
  253. public function getProfileObject($iProfileId = 0)
  254. {
  255. $oProfile = BxDolProfile::getInstance($iProfileId);
  256. if (!$oProfile)
  257. $oProfile = BxDolProfileUndefined::getInstance();
  258. return $oProfile;
  259. }
  260. public function getAccountId($iProfileId)
  261. {
  262. $oProfile = $this->getProfileObject($iProfileId);
  263. if($oProfile->id() == 0)
  264. return 0;
  265. return $oProfile->getAccountId();
  266. }
  267. protected function onInvite($iAccountId, $iProfileId)
  268. {
  269. $this->isAllowedInvite($iProfileId, true);
  270. //--- Event -> Invite for Alerts Engine ---//
  271. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  272. $oAlert->alert();
  273. //--- Event -> Invite for Alerts Engine ---//
  274. }
  275. protected function onRequest()
  276. {
  277. //--- Event -> Request for Alerts Engine ---//
  278. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'request');
  279. $oAlert->alert();
  280. //--- Event -> Request for Alerts Engine ---//
  281. }
  282. protected function getFormObjectInvite($sDisplay = '')
  283. {
  284. if(empty($sDisplay))
  285. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  286. bx_import('FormCheckerHelper', $this->_aModule);
  287. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  288. }
  289. protected function getJoinLink($sKey)
  290. {
  291. $sKeyCode = $this->_oConfig->getKeyCode();
  292. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  293. return bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey));
  294. }
  295. }
  296. /** @} */