BxSitesModule.php 8.6 KB

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