BxSitesModule.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 Sites Sites
  7. * @ingroup TridentModules
  8. *
  9. * @{
  10. */
  11. bx_import('BxDolAcl');
  12. define('BX_SITES_ACCOUNT_STATUS_UNCONFIRMED', 'unconfirmed');
  13. define('BX_SITES_ACCOUNT_STATUS_PENDING', 'pending');
  14. define('BX_SITES_ACCOUNT_STATUS_TRIAL', 'trial');
  15. define('BX_SITES_ACCOUNT_STATUS_ACTIVE', 'active');
  16. define('BX_SITES_ACCOUNT_STATUS_CANCELED', 'canceled');
  17. define('BX_SITES_ACCOUNT_STATUS_SUSPENDED', 'suspended');
  18. define('BX_SITES_TRANSACTION_INIT', 'init');
  19. define('BX_SITES_TRANSACTION_TRIAL', 'trial');
  20. define('BX_SITES_TRANSACTION_REGULAR', 'regular');
  21. /**
  22. * PayPal integration.
  23. */
  24. define('BX_SITES_PP_DIRECTORY_PATH_API', BX_DIRECTORY_PATH_MODULES . 'boonex/sites/api/');
  25. define('BX_SITES_PP_ACTIVATION_INIT_AMOUNT', 'init_amount');
  26. define('BX_SITES_PP_ACTIVATION_TRIAL_PERIOD', 'trial_period');
  27. define('BX_SITES_PP_PERIOD_TRIAL', 'Trial');
  28. define('BX_SITES_PP_PERIOD_REGULAR', 'Regular');
  29. define('BX_SITES_PP_RPA_CANCEL', 'Cancel');
  30. define('BX_SITES_PP_RPA_SUSPEND', 'Suspend');
  31. define('BX_SITES_PP_RPA_REACTIVATE', 'Reactivate');
  32. /**
  33. * Sites module
  34. */
  35. class BxSitesModule extends BxDolModule
  36. {
  37. protected $_iProfileId;
  38. function __construct(&$aModule)
  39. {
  40. parent::__construct($aModule);
  41. $this->_oConfig->init($this->_oDb);
  42. $this->_iProfileId = bx_get_logged_profile_id();
  43. }
  44. // ====== SERVICE METHODS
  45. public function serviceIsUsed($sDomain)
  46. {
  47. return $this->_oDb->isAccount(array('domain' => $sDomain));
  48. }
  49. public function serviceBrowse()
  50. {
  51. $oGrid = BxDolGrid::getObjectInstance('bx_sites_browse');
  52. if(!$oGrid)
  53. return '';
  54. return $oGrid->getCode();
  55. }
  56. public function serviceSiteCreate()
  57. {
  58. bx_import('Forms', $this->_aModule);
  59. $oForms = new BxSitesForms($this);
  60. return $oForms->addDataForm();
  61. }
  62. public function serviceSiteSubscribe()
  63. {
  64. $sToken = bx_process_input(bx_get('token'));
  65. if($sToken === false)
  66. return MsgBox(_t('_bx_sites_paypal_err_token_not_found'));
  67. bx_import('Paypal', $this->_aModule);
  68. $oPaypal = new BxSitesPaypal($this);
  69. $sResultMessage = $oPaypal->subscribe($sToken);
  70. return MsgBox(_t($sResultMessage));
  71. }
  72. public function serviceSiteView()
  73. {
  74. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  75. if($iId === false)
  76. return MsgBox(_t('_bx_sites_txt_err_site_is_not_defined'));
  77. $aAccount = $this->_oDb->getAccount(array('type' => 'id', 'value' => $iId));
  78. if(empty($aAccount) || !is_array($aAccount))
  79. return MsgBox(_t('_bx_sites_txt_err_site_is_not_defined'));
  80. $sMsg = $this->isAllowedView($aAccount);
  81. if($sMsg !== CHECK_ACTION_RESULT_ALLOWED)
  82. return MsgBox($sMsg);
  83. $oGrid = BxDolGrid::getObjectInstance('bx_sites_overview');
  84. if(!$oGrid)
  85. return '';
  86. $oGrid->setAccount($aAccount);
  87. return array(
  88. 'title' => $this->getDomain($aAccount['domain']),
  89. 'content' => $this->_oTemplate->getJs(true) . $oGrid->getCode()
  90. );
  91. }
  92. public function serviceSiteEdit()
  93. {
  94. $aParams = $this->getSelectParam();
  95. if($aParams === false)
  96. return MsgBox(_t('_bx_sites_txt_err_site_is_not_defined'));
  97. $aAccount = $this->_oDb->getAccount($aParams);
  98. if(empty($aAccount) || !is_array($aAccount))
  99. return MsgBox(_t('_bx_sites_txt_err_site_is_not_defined'));
  100. bx_import('Forms', $this->_aModule);
  101. $oForms = new BxSitesForms($this);
  102. return array(
  103. 'title' => _t('_bx_sites_page_block_title_site_manage') . ' ' . $this->getDomain($aAccount['domain']),
  104. 'content' => $oForms->editDataForm($aAccount)
  105. );
  106. }
  107. public function serviceSiteDelete($iId = 0)
  108. {
  109. if(!$iId)
  110. $iId = bx_process_input(bx_get('id'), BX_DATA_INT);
  111. if(!$iId)
  112. return MsgBox(_t('_bx_sites_txt_err_site_is_not_defined'));
  113. $aAccount = $this->_oDb->getAccount(array('type' => 'id', 'value' => $iId));
  114. if(empty($aAccount) || !is_array($aAccount))
  115. return MsgBox(_t('_bx_sites_txt_err_site_is_not_defined'));
  116. bx_import('Forms', $this->_aModule);
  117. $oProfileForms = new BxSitesForms($this);
  118. return array(
  119. 'title' => _t('_bx_sites_page_block_title_site_delete') . ' ' . $this->getDomain($aAccount['domain']),
  120. 'content' => $oProfileForms->deleteDataForm($aAccount)
  121. );
  122. }
  123. // ====== ACTION METHODS
  124. public function actionIpn()
  125. {
  126. $aData = &$_POST;
  127. foreach($aData as $sKey => $sValue)
  128. $aData[$sKey] = bx_process_input(trim($sValue));
  129. if(!isset($aData['txn_type']))
  130. return;
  131. bx_import('Paypal', $this->_aModule);
  132. $oPaypal = new BxSitesPaypal($this);
  133. $oPaypal->process($aData);
  134. bx_import('Account', $this->_aModule);
  135. $oAccount = new BxSitesAccount($this);
  136. if($oPaypal->bProfileCreated) {
  137. $oAccount->onProfileConfirmed($aData);
  138. } else if($oPaypal->bProfileCanceled) {
  139. $oAccount->onProfileCanceled($aData);
  140. } else if($oPaypal->bPaymentDone) {
  141. $oAccount->onPaymentReceived($aData, $oPaypal->fPaymentAmout);
  142. } else if($oPaypal->bPaymentRefund) {
  143. $oAccount->onPaymentRefunded($aData);
  144. }
  145. }
  146. /*
  147. * Can be removed if it's not used.
  148. *
  149. public function actionReactivate($iId)
  150. {
  151. $aResult = array('code' => '1', 'message' => _t('_bx_sites_txt_err_cannot_perform'));
  152. $sUrl = $this->startSubscription($iId);
  153. if(!empty($sUrl))
  154. $aResult = array('code' => '0', 'message' => '', 'redirect' => $sUrl);
  155. header('Content-Type:text/javascript');
  156. echo json_encode($aResult);
  157. }
  158. */
  159. // ====== PERMISSION METHODS
  160. public function isAllowedAdd ($isPerformAction = false)
  161. {
  162. $aCheck = checkActionModule($this->_iProfileId, 'create site', $this->getName(), $isPerformAction);
  163. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  164. return $aCheck[CHECK_ACTION_MESSAGE];
  165. return CHECK_ACTION_RESULT_ALLOWED;
  166. }
  167. public function isAllowedView ($aDataEntry, $isPerformAction = false)
  168. {
  169. // moderator and owner always have access
  170. if ($aDataEntry[BxSitesConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->isModeratorAccess($isPerformAction))
  171. return CHECK_ACTION_RESULT_ALLOWED;
  172. return _t('_sys_txt_access_denied');
  173. }
  174. public function isAllowedEdit ($aDataEntry, $isPerformAction = false)
  175. {
  176. // moderator and owner always have access
  177. if ($aDataEntry[BxSitesConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->isModeratorAccess($isPerformAction))
  178. return CHECK_ACTION_RESULT_ALLOWED;
  179. return _t('_sys_txt_access_denied');
  180. }
  181. public function isAllowedDelete (&$aDataEntry, $isPerformAction = false)
  182. {
  183. // moderator and owner always have access
  184. if ($aDataEntry[BxSitesConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->isModeratorAccess($isPerformAction))
  185. return CHECK_ACTION_RESULT_ALLOWED;
  186. return _t('_sys_txt_access_denied');
  187. }
  188. public function isModeratorAccess ($isPerformAction = false)
  189. {
  190. $aCheck = checkActionModule($this->_iProfileId, 'manage sites', $this->getName(), $isPerformAction);
  191. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  192. }
  193. // ====== COMMON METHODS
  194. public function getSelectParam()
  195. {
  196. $sType = 'id';
  197. $mixedId = bx_get('id');
  198. if($mixedId === false) {
  199. $mixedId = bx_get('sid');
  200. if($mixedId === false)
  201. return false;
  202. $sType = 'profile_sid';
  203. $mixedId = bx_process_input($mixedId);
  204. } else
  205. $mixedId = bx_process_input($mixedId, BX_DATA_INT);
  206. return array('type' => $sType, 'value' => $mixedId);
  207. }
  208. public function getDomain($sDomain, $bProtocol = false, $bWww = false)
  209. {
  210. $sDomain = sprintf($this->_oConfig->getDomainMask(), $sDomain);
  211. if($bWww)
  212. $sDomain = 'www.' . $sDomain;
  213. if($bProtocol)
  214. $sDomain = 'http://' . $sDomain;
  215. return $sDomain;
  216. }
  217. public function getObject($sClass)
  218. {
  219. return bx_instance('BxSites' . $sClass, array($this), $this->_aModule);
  220. }
  221. public function startSubscription($iId)
  222. {
  223. $oPaypal = $this->getObject('Paypal');
  224. return $oPaypal->start($iId);
  225. }
  226. public function cancelSubscription($sPpProfileId)
  227. {
  228. $oPaypal = $this->getObject('Paypal');
  229. return $oPaypal->performAction($sPpProfileId);
  230. }
  231. }
  232. /** @} */