BxXeroConfig.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Xero Xero
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxXeroConfig extends BxBaseModGeneralConfig
  12. {
  13. protected $_oDb;
  14. protected $_oSession;
  15. protected $_sSessionKey;
  16. protected $_sClientId;
  17. protected $_sClientSecret;
  18. protected $_sRedirectUrl;
  19. protected $_sAuthorizeUrl;
  20. protected $_sWebhookKey;
  21. public function __construct($aModule)
  22. {
  23. parent::__construct($aModule);
  24. $this->_oSession = BxDolSession::getInstance();
  25. $this->_sSessionKey = 'bx_xero_session';
  26. $this->_sAuthorizeUrl = BX_DOL_URL_STUDIO . bx_append_url_params('module.php', ['name' => $this->getName(), 'page' => 'authorize']);
  27. $this->CNF = array (
  28. // database tables
  29. 'TABLE_CONTACTS' => $aModule['db_prefix'] . 'contacts',
  30. // database fields
  31. 'FIELD_ID' => 'id',
  32. 'FIELD_EMAIL' => 'email',
  33. 'FIELD_CONTACT' => 'contact',
  34. // some params
  35. 'PARAM_CLIENT_ID' => 'bx_xero_client_id',
  36. 'PARAM_CLIENT_SECRET' => 'bx_xero_client_secret',
  37. 'PARAM_REDIRECT_URL' => 'bx_xero_redirect_url',
  38. 'PARAM_WEBHOOK_KEY' => 'bx_xero_webhook_key',
  39. 'PARAM_INVOICE_SEND' => 'bx_xero_invoice_send',
  40. );
  41. $this->_aJsClasses = array(
  42. 'main' => 'BxXeroMain',
  43. );
  44. $this->_aJsObjects = array(
  45. 'main' => 'oBxXeroMain',
  46. );
  47. }
  48. public function init(&$oDb)
  49. {
  50. $this->_oDb = &$oDb;
  51. //NOTE: Some settings can be inited here.
  52. }
  53. public function getClientId()
  54. {
  55. if(empty($this->_sClientId))
  56. $this->_sClientId = $this->_oDb->getParam($this->CNF['PARAM_CLIENT_ID']);
  57. return $this->_sClientId;
  58. }
  59. public function getClientSecret()
  60. {
  61. if(empty($this->_sClientSecret))
  62. $this->_sClientSecret = $this->_oDb->getParam($this->CNF['PARAM_CLIENT_SECRET']);
  63. return $this->_sClientSecret;
  64. }
  65. public function getRedirectUrl()
  66. {
  67. if(empty($this->_sRedirectUrl))
  68. $this->_sRedirectUrl = $this->_oDb->getParam($this->CNF['PARAM_REDIRECT_URL']);
  69. return $this->_sRedirectUrl;
  70. }
  71. public function getAuthorizeUrl($iCode = false)
  72. {
  73. return $iCode !== false ? bx_append_url_params($this->_sAuthorizeUrl, ['code' => $iCode]) : $this->_sAuthorizeUrl;
  74. }
  75. public function getWebhookKey()
  76. {
  77. if(empty($this->_sWebhookKey))
  78. $this->_sWebhookKey = $this->_oDb->getParam($this->CNF['PARAM_WEBHOOK_KEY']);
  79. return $this->_sWebhookKey;
  80. }
  81. public function cleanSession()
  82. {
  83. $aData = $this->_oSession->getValue($this->_sSessionKey);
  84. if(empty($aData) || !is_array($aData))
  85. return;
  86. $this->_oSession->unsetValue($this->_sSessionKey);
  87. }
  88. public function setState($sState)
  89. {
  90. $aData = $this->_oSession->getValue($this->_sSessionKey);
  91. if(empty($aData) || !is_array($aData))
  92. $aData = [];
  93. $aData['state'] = $sState;
  94. return $this->_oSession->setValue($this->_sSessionKey, $aData);
  95. }
  96. public function unsetState()
  97. {
  98. $aData = $this->_oSession->getValue($this->_sSessionKey);
  99. if(isset($aData['state']))
  100. unset($aData['state']);
  101. return $this->_oSession->setValue($this->_sSessionKey, $aData);
  102. }
  103. public function getState()
  104. {
  105. $aData = $this->_oSession->getValue($this->_sSessionKey);
  106. return isset($aData['state']) ? $aData['state'] : false;
  107. }
  108. public function setData($sAccessToken, $iAccessTokenExpires, $sTenantId, $sRefreshToken, $sIdToken)
  109. {
  110. $aData = $this->_oSession->getValue($this->_sSessionKey);
  111. if(empty($aData) || !is_array($aData))
  112. $aData = [];
  113. $aData['data'] = [
  114. 'token' => $sAccessToken,
  115. 'expires' => $iAccessTokenExpires,
  116. 'tenant_id' => $sTenantId,
  117. 'refresh_token' => $sRefreshToken,
  118. 'id_token' => $sIdToken
  119. ];
  120. $this->_oDb->setData($aData['data']);
  121. return $this->_oSession->setValue($this->_sSessionKey, $aData);
  122. }
  123. public function getData($bCheckLifetime = false)
  124. {
  125. $aData = $this->_oSession->getValue($this->_sSessionKey);
  126. if(empty($aData['data'])) {
  127. if(!is_array($aData))
  128. $aData = [];
  129. $aData['data'] = $this->_oDb->getData();
  130. if(empty($aData['data']))
  131. return false;
  132. }
  133. if($bCheckLifetime && $aData['data']['expires'] !== null && $aData['data']['expires'] <= time())
  134. return false;
  135. return $aData['data'];
  136. }
  137. public function isDataExpired()
  138. {
  139. $aData = $this->getData();
  140. if(empty($aData))
  141. return true;
  142. if(time() > $aData['expires'])
  143. return true;
  144. return false;
  145. }
  146. public function getTenantId()
  147. {
  148. $aData = $this->getData();
  149. if(empty($aData) || empty($aData['tenant_id']))
  150. return false;
  151. return $aData['tenant_id'];
  152. }
  153. public function getAccessToken()
  154. {
  155. $aData = $this->getData();
  156. if(empty($aData) || empty($aData['token']))
  157. return true;
  158. return (string)$aData['token'];
  159. }
  160. public function getRefreshToken()
  161. {
  162. $aData = $this->getData();
  163. if(empty($aData) || empty($aData['refresh_token']))
  164. return true;
  165. return (string)$aData['refresh_token'];
  166. }
  167. }
  168. /** @} */