BxSitesModule.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. public function serviceResponse($oAlert)
  128. {
  129. if($oAlert->sUnit != 'account' || !in_array($oAlert->sAction, array('login')))
  130. return;
  131. bx_import('BxDolSession');
  132. $sDomain = BxDolSession::getInstance()->getUnsetValue('bx_sites_domain');
  133. if($sDomain === false)
  134. return;
  135. $iAccountId = $this->_oDb->insertAccount(array(
  136. 'owner_id' => $oAlert->iObject,
  137. 'domain' => $sDomain,
  138. 'created' => time(),
  139. 'status' => BX_SITES_ACCOUNT_STATUS_UNCONFIRMED
  140. ));
  141. if(!$iAccountId)
  142. return;
  143. $oAccount = $this->getObject('Account');
  144. $oAccount->onAccountCreated($iAccountId);
  145. $sUrl = $this->startSubscription($iAccountId);
  146. header('Location: ' . $sUrl);
  147. exit;
  148. }
  149. // ====== ACTION METHODS
  150. public function actionIpn()
  151. {
  152. $aData = &$_POST;
  153. foreach($aData as $sKey => $sValue)
  154. $aData[$sKey] = bx_process_input(trim($sValue));
  155. if(!isset($aData['txn_type']))
  156. return;
  157. bx_import('Paypal', $this->_aModule);
  158. $oPaypal = new BxSitesPaypal($this);
  159. $oPaypal->process($aData);
  160. bx_import('Account', $this->_aModule);
  161. $oAccount = new BxSitesAccount($this);
  162. if($oPaypal->bProfileCreated) {
  163. $oAccount->onProfileConfirmed($aData);
  164. }
  165. else if($oPaypal->bProfileCanceled) {
  166. $oAccount->onProfileCanceled($aData);
  167. }
  168. else if($oPaypal->bPaymentDone) {
  169. $oAccount->onPaymentReceived($aData, $oPaypal->fPaymentAmout);
  170. }
  171. else if($oPaypal->bPaymentRefund) {
  172. $oAccount->onPaymentRefunded($aData);
  173. }
  174. }
  175. /*
  176. * Can be removed if it's not used.
  177. *
  178. public function actionReactivate($iId)
  179. {
  180. $aResult = array('code' => '1', 'message' => _t('_bx_sites_txt_err_cannot_perform'));
  181. $sUrl = $this->startSubscription($iId);
  182. if(!empty($sUrl))
  183. $aResult = array('code' => '0', 'message' => '', 'redirect' => $sUrl);
  184. header('Content-Type:text/javascript');
  185. $oJson = new Services_JSON();
  186. echo $oJson->encode($aResult);
  187. }
  188. */
  189. // ====== PERMISSION METHODS
  190. public function isAllowedAdd ($isPerformAction = false)
  191. {
  192. $aCheck = checkActionModule($this->_iProfileId, 'create site', $this->getName(), $isPerformAction);
  193. if ($aCheck[CHECK_ACTION_RESULT] !== CHECK_ACTION_RESULT_ALLOWED)
  194. return $aCheck[CHECK_ACTION_MESSAGE];
  195. return CHECK_ACTION_RESULT_ALLOWED;
  196. }
  197. public function isAllowedView ($aDataEntry, $isPerformAction = false)
  198. {
  199. // moderator and owner always have access
  200. if ($aDataEntry[BxSitesConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->isModeratorAccess($isPerformAction))
  201. return CHECK_ACTION_RESULT_ALLOWED;
  202. return _t('_sys_txt_access_denied');
  203. }
  204. public function isAllowedEdit ($aDataEntry, $isPerformAction = false)
  205. {
  206. // moderator and owner always have access
  207. if ($aDataEntry[BxSitesConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->isModeratorAccess($isPerformAction))
  208. return CHECK_ACTION_RESULT_ALLOWED;
  209. return _t('_sys_txt_access_denied');
  210. }
  211. public function isAllowedDelete (&$aDataEntry, $isPerformAction = false)
  212. {
  213. // moderator and owner always have access
  214. if ($aDataEntry[BxSitesConfig::$FIELD_AUTHOR] == $this->_iProfileId || $this->isModeratorAccess($isPerformAction))
  215. return CHECK_ACTION_RESULT_ALLOWED;
  216. return _t('_sys_txt_access_denied');
  217. }
  218. public function isModeratorAccess ($isPerformAction = false)
  219. {
  220. $aCheck = checkActionModule($this->_iProfileId, 'manage sites', $this->getName(), $isPerformAction);
  221. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  222. }
  223. // ====== COMMON METHODS
  224. public function getSelectParam()
  225. {
  226. $sType = 'id';
  227. $mixedId = bx_get('id');
  228. if($mixedId === false) {
  229. $mixedId = bx_get('sid');
  230. if($mixedId === false)
  231. return false;
  232. $sType = 'profile_sid';
  233. $mixedId = bx_process_input($mixedId);
  234. }
  235. else
  236. $mixedId = bx_process_input($mixedId, BX_DATA_INT);
  237. return array('type' => $sType, 'value' => $mixedId);
  238. }
  239. public function getDomain($sDomain, $bProtocol = false, $bWww = false)
  240. {
  241. $sDomain = sprintf($this->_oConfig->getDomainMask(), $sDomain);
  242. if($bWww)
  243. $sDomain = 'www.' . $sDomain;
  244. if($bProtocol)
  245. $sDomain = 'http://' . $sDomain;
  246. return $sDomain;
  247. }
  248. public function getObject($sClass)
  249. {
  250. return bx_instance('BxSites' . $sClass, array($this), $this->_aModule);
  251. }
  252. public function startSubscription($iId)
  253. {
  254. $oPaypal = $this->getObject('Paypal');
  255. return $oPaypal->start($iId);
  256. }
  257. public function cancelSubscription($sPpProfileId)
  258. {
  259. $oPaypal = $this->getObject('Paypal');
  260. return $oPaypal->performAction($sPpProfileId);
  261. }
  262. }
  263. /** @} */