BxInvModule.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 $this->_echoResultJson(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 $this->_echoResultJson(array('message' => _t('_bx_invites_err_limit_reached')));
  38. }
  39. $oKeys = BxDolKey::getInstance();
  40. if(!$oKeys)
  41. return $this->_echoResultJson(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. $this->_echoResultJson(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 shoulb be processed as usual, non-empty string - if join form need to be replaced with this code
  162. */
  163. public function serviceAccountAddFormCheck()
  164. {
  165. if (!$this->_oConfig->isRegistrationByInvitation())
  166. return '';
  167. $oSession = BxDolSession::getInstance();
  168. $sKeyCode = $this->_oConfig->getKeyCode();
  169. if (bx_get($sKeyCode) !== false) {
  170. $sKey = bx_process_input(bx_get($sKeyCode));
  171. $oKeys = BxDolKey::getInstance();
  172. if($oKeys && $oKeys->isKeyExists($sKey))
  173. $oSession->setValue($sKeyCode, $sKey);
  174. }
  175. $sKey = $oSession->getValue($sKeyCode);
  176. if ($sKey === false)
  177. return $this->_oTemplate->getBlockRequest();
  178. return '';
  179. }
  180. public function invite($sType, $sEmails, $sText, $mixedLimit = false, $oForm = null)
  181. {
  182. $iProfileId = $this->getProfileId();
  183. $iAccountId = $this->getAccountId($iProfileId);
  184. $oKeys = BxDolKey::getInstance();
  185. if(!$oKeys || !in_array($sType, array(BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM)))
  186. return false;
  187. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  188. $sEmailTemplate = '';
  189. switch($sType) {
  190. case BX_INV_TYPE_FROM_MEMBER:
  191. $sEmailTemplate = 'bx_invites_invite_form_message';
  192. break;
  193. case BX_INV_TYPE_FROM_SYSTEM:
  194. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  195. break;
  196. }
  197. if(empty($oForm))
  198. $oForm = $this->getFormObjectInvite();
  199. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  200. 'text' => $sText
  201. ), $iAccountId, $iProfileId);
  202. $iSent = 0;
  203. $iDate = time();
  204. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  205. if(is_array($aEmails) && !empty($aEmails))
  206. foreach($aEmails as $sEmail) {
  207. if($mixedLimit !== false && (int)$mixedLimit <= 0)
  208. break;
  209. $sEmail = trim($sEmail);
  210. if(empty($sEmail))
  211. continue;
  212. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  213. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => $this->getJoinLink($sKey)), BX_EMAIL_SYSTEM)) {
  214. $oForm->insert(array(
  215. 'account_id' => $iAccountId,
  216. 'profile_id' => $iProfileId,
  217. 'key' => $sKey,
  218. 'email' => $sEmail,
  219. 'date' => $iDate
  220. ));
  221. $this->onInvite($iAccountId, $iProfileId);
  222. $iSent += 1;
  223. if($mixedLimit !== false)
  224. $mixedLimit -= 1;
  225. }
  226. }
  227. return $iSent;
  228. }
  229. public function isAllowedInvite($iProfileId, $bPerform = false)
  230. {
  231. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  232. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  233. }
  234. public function isAllowedRequest($iProfileId, $bPerform = false)
  235. {
  236. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  237. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  238. }
  239. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  240. {
  241. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  242. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  243. }
  244. public function getProfileId()
  245. {
  246. return isLogged() ? bx_get_logged_profile_id() : 0;
  247. }
  248. public function getProfileObject($iProfileId = 0)
  249. {
  250. $oProfile = BxDolProfile::getInstance($iProfileId);
  251. if (!$oProfile)
  252. $oProfile = BxDolProfileUndefined::getInstance();
  253. return $oProfile;
  254. }
  255. public function getAccountId($iProfileId)
  256. {
  257. $oProfile = $this->getProfileObject($iProfileId);
  258. if($oProfile->id() == 0)
  259. return 0;
  260. return $oProfile->getAccountId();
  261. }
  262. protected function onInvite($iAccountId, $iProfileId)
  263. {
  264. $this->isAllowedInvite($iProfileId, true);
  265. //--- Event -> Invite for Alerts Engine ---//
  266. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  267. $oAlert->alert();
  268. //--- Event -> Invite for Alerts Engine ---//
  269. }
  270. protected function onRequest()
  271. {
  272. //--- Event -> Request for Alerts Engine ---//
  273. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'request');
  274. $oAlert->alert();
  275. //--- Event -> Request for Alerts Engine ---//
  276. }
  277. protected function getFormObjectInvite($sDisplay = '')
  278. {
  279. if(empty($sDisplay))
  280. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  281. bx_import('FormCheckerHelper', $this->_aModule);
  282. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  283. }
  284. protected function getJoinLink($sKey)
  285. {
  286. $sKeyCode = $this->_oConfig->getKeyCode();
  287. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  288. return bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey));
  289. }
  290. protected function _echoResultJson($a, $isAutoWrapForFormFileSubmit = false)
  291. {
  292. header('Content-type: text/html; charset=utf-8');
  293. $s = json_encode($a);
  294. if ($isAutoWrapForFormFileSubmit && !empty($_FILES))
  295. $s = '<textarea>' . $s . '</textarea>';
  296. echo $s;
  297. }
  298. }
  299. /** @} */