BxInvModule.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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('BxDolModule');
  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. * SERVICE METHODS
  26. */
  27. public function serviceGetBlockInvite()
  28. {
  29. $iProfileId = $this->getProfileId();
  30. $iAccountId = $this->getAccountId($iProfileId);
  31. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  32. if($mixedAllowed !== true)
  33. return '';
  34. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  35. if($iInvited >= $this->_oConfig->getCountPerUser())
  36. return '';
  37. return array(
  38. 'content' => $this->_oTemplate->getBlockInvite($iAccountId, $iProfileId)
  39. );
  40. }
  41. public function serviceGetBlockFormInvite()
  42. {
  43. $iProfileId = $this->getProfileId();
  44. $iAccountId = $this->getAccountId($iProfileId);
  45. $mixedAllowed = $this->isAllowedInvite($iProfileId);
  46. if($mixedAllowed !== true)
  47. return array(
  48. 'content' => MsgBox($mixedAllowed)
  49. );
  50. $iInvited = (int)$this->_oDb->getInvites(array('type' => 'count_by_account', 'value' => $iAccountId));
  51. $iInvites = $this->_oConfig->getCountPerUser() - $iInvited;
  52. if($iInvites <= 0)
  53. return array(
  54. 'content' => MsgBox(_t('_bx_invites_err_limit_reached'))
  55. );
  56. $oForm = $this->getFormObjectInvite();
  57. $oForm->aInputs['text']['value'] = _t('_bx_invites_msg_invitation');
  58. $sResult = '';
  59. $oForm->initChecker();
  60. if($oForm->isSubmittedAndValid()) {
  61. $sEmails = bx_process_input($oForm->getCleanValue('emails'));
  62. $sText = bx_process_pass($oForm->getCleanValue('text'));
  63. $mixedResult = $this->invite(BX_INV_TYPE_FROM_MEMBER, $sEmails, $sText, $iInvites, $oForm);
  64. if($mixedResult !== false)
  65. $sResult = _t('_bx_invites_msg_invitation_sent', (int)$mixedResult);
  66. else
  67. $sResult = _t('_bx_invites_err_not_available');
  68. $sResult = MsgBox($sResult);
  69. }
  70. return array(
  71. 'content' => $sResult . $oForm->getCode()
  72. );
  73. }
  74. public function serviceGetBlockFormRequest()
  75. {
  76. if(!$this->_oConfig->isRequestInvite())
  77. return array(
  78. 'content' => MsgBox(_t('_bx_invites_err_not_available'))
  79. );
  80. $mixedAllowed = $this->isAllowedRequest(0);
  81. if($mixedAllowed !== true)
  82. return array(
  83. 'content' => MsgBox($mixedAllowed)
  84. );
  85. $sResult = '';
  86. bx_import('BxDolForm');
  87. $oForm = BxDolForm::getObjectInstance($this->_oConfig->getObject('form_request'), $this->_oConfig->getObject('form_display_request_send'));
  88. $oForm->initChecker();
  89. if($oForm->isSubmittedAndValid()) {
  90. $sIp = getVisitorIP();
  91. $iId = (int)$oForm->insert(array(
  92. 'nip' => ip2long($sIp),
  93. 'date' => time()
  94. ));
  95. if($iId !== false) {
  96. $sRequestsEmail = $this->_oConfig->getRequestsEmail();
  97. if(!empty($sRequestsEmail)) {
  98. bx_import('BxDolPermalinks');
  99. $sManageUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=invites-requests');
  100. bx_import('BxDolEmailTemplates');
  101. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate('bx_invites_request_form_message', array(
  102. 'sender_name' => bx_process_output($oForm->getCleanValue('name')),
  103. 'sender_email' => bx_process_output($oForm->getCleanValue('email')),
  104. 'sender_ip' => $sIp,
  105. 'manage_url' => $sManageUrl
  106. ));
  107. sendMail($sRequestsEmail, $aMessage['Subject'], $aMessage['Body'], 0, array(), BX_EMAIL_SYSTEM);
  108. }
  109. $sResult = MsgBox(_t('_bx_invites_msg_request_sent'));
  110. }
  111. }
  112. return array(
  113. 'content' => $sResult . $oForm->getCode()
  114. );
  115. }
  116. public function serviceGetBlockManageRequests()
  117. {
  118. bx_import('BxDolGrid');
  119. $oGrid = BxDolGrid::getObjectInstance($this->_oConfig->getObject('grid_requests'));
  120. if(!$oGrid)
  121. return '';
  122. return $oGrid->getCode();
  123. }
  124. public function serviceGetMenuAddonRequests()
  125. {
  126. return $this->_oDb->getRequests(array('type' => 'count_all'));
  127. }
  128. public function invite($sType, $sEmails, $sText, $iLimit = false, $oForm = null)
  129. {
  130. $iProfileId = $this->getProfileId();
  131. $iAccountId = $this->getAccountId($iProfileId);
  132. bx_import('BxDolKey');
  133. $oKeys = BxDolKey::getInstance();
  134. if(!$oKeys || !in_array($sType, array(BX_INV_TYPE_FROM_MEMBER, BX_INV_TYPE_FROM_SYSTEM)))
  135. return false;
  136. $sKeyCode = $this->_oConfig->getKeyCode();
  137. $iKeyLifetime = $this->_oConfig->getKeyLifetime();
  138. $sEmailTemplate = '';
  139. switch($sType) {
  140. case BX_INV_TYPE_FROM_MEMBER:
  141. $sEmailTemplate = 'bx_invites_invite_form_message';
  142. break;
  143. case BX_INV_TYPE_FROM_SYSTEM:
  144. $sEmailTemplate = 'bx_invites_invite_by_request_message';
  145. break;
  146. }
  147. if(empty($oForm))
  148. $oForm = $this->getFormObjectInvite();
  149. bx_import('BxDolPermalinks');
  150. $sJoinUrl = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  151. bx_import('BxDolEmailTemplates');
  152. $aMessage = BxDolEmailTemplates::getInstance()->parseTemplate($sEmailTemplate, array(
  153. 'text' => $sText
  154. ), $iAccountId, $iProfileId);
  155. $iSent = 0;
  156. $iDate = time();
  157. $aEmails = preg_split("/[\s\n,;]+/", $sEmails);
  158. if(is_array($aEmails) && !empty($aEmails))
  159. foreach($aEmails as $sEmail) {
  160. if($iLimit !== false && (int)$iLimit <= 0)
  161. break;
  162. $sEmail = trim($sEmail);
  163. if(empty($sEmail))
  164. continue;
  165. $sKey = $oKeys->getNewKey(false, $iKeyLifetime);
  166. if(sendMail($sEmail, $aMessage['Subject'], $aMessage['Body'], 0, array('join_url' => bx_append_url_params($sJoinUrl, array($sKeyCode => $sKey))), BX_EMAIL_SYSTEM)) {
  167. $oForm->insert(array(
  168. 'account_id' => $iAccountId,
  169. 'profile_id' => $iProfileId,
  170. 'email' => $sEmail,
  171. 'date' => $iDate
  172. ));
  173. $this->onInvite($iAccountId, $iProfileId);
  174. $iSent += 1;
  175. if($iLimit !== false)
  176. $iLimit -= 1;
  177. }
  178. }
  179. return $iSent;
  180. }
  181. public function isAllowedInvite($iProfileId, $bPerform = false)
  182. {
  183. $aCheckResult = checkActionModule($iProfileId, 'invite', $this->getName(), $bPerform);
  184. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  185. }
  186. public function isAllowedRequest($iProfileId, $bPerform = false)
  187. {
  188. $aCheckResult = checkActionModule($iProfileId, 'request', $this->getName(), $bPerform);
  189. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  190. }
  191. public function isAllowedDeleteRequest($iProfileId, $bPerform = false)
  192. {
  193. $aCheckResult = checkActionModule($iProfileId, 'delete request', $this->getName(), $bPerform);
  194. return $aCheckResult[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED ? $aCheckResult[CHECK_ACTION_MESSAGE] : true;
  195. }
  196. public function getProfileId()
  197. {
  198. return isLogged() ? bx_get_logged_profile_id() : 0;
  199. }
  200. public function getProfileObject($iProfileId = 0)
  201. {
  202. bx_import('BxDolProfile');
  203. $oProfile = BxDolProfile::getInstance($iProfileId);
  204. if (!$oProfile) {
  205. bx_import('BxDolProfileUndefined');
  206. $oProfile = BxDolProfileUndefined::getInstance();
  207. }
  208. return $oProfile;
  209. }
  210. public function getAccountId($iProfileId)
  211. {
  212. $oProfile = $this->getProfileObject($iProfileId);
  213. if($oProfile->id() == 0)
  214. return 0;
  215. return $oProfile->getAccountId();
  216. }
  217. protected function onInvite($iAccountId, $iProfileId)
  218. {
  219. $this->isAllowedInvite($iProfileId, true);
  220. //--- Event -> Invite for Alerts Engine ---//
  221. bx_import('BxDolAlerts');
  222. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'invite', 0, $iProfileId);
  223. $oAlert->alert();
  224. //--- Event -> Invite for Alerts Engine ---//
  225. }
  226. protected function onRequest()
  227. {
  228. //--- Event -> Request for Alerts Engine ---//
  229. bx_import('BxDolAlerts');
  230. $oAlert = new BxDolAlerts($this->_oConfig->getObject('alert'), 'request');
  231. $oAlert->alert();
  232. //--- Event -> Request for Alerts Engine ---//
  233. }
  234. protected function getFormObjectInvite($sDisplay = '')
  235. {
  236. if(empty($sDisplay))
  237. $sDisplay = $this->_oConfig->getObject('form_display_invite_send');
  238. bx_import('BxDolForm');
  239. bx_import('FormCheckerHelper', $this->_aModule);
  240. return BxDolForm::getObjectInstance($this->_oConfig->getObject('form_invite'), $sDisplay);
  241. }
  242. }
  243. /** @} */