PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/ecwid-shopping-cart/includes/class-ecwid-oauth.php

https://gitlab.com/oresticouci/ablSportsConsulting
PHP | 351 lines | 269 code | 80 blank | 2 comment | 50 complexity | 749b234d936c475749a8749b59fde11f MD5 | raw file
  1. <?php
  2. include ECWID_PLUGIN_DIR . "lib/phpseclib/AES.php";
  3. require_once ECWID_PLUGIN_DIR . 'lib/ecwid_api_v3.php';
  4. class Ecwid_OAuth {
  5. const MODE_CONNECT = 'connect';
  6. const MODE_RECONNECT = 'reconnect';
  7. protected $state;
  8. protected $api;
  9. public function __construct()
  10. {
  11. add_action('admin_post_ecwid_oauth', array($this, 'process_authorization'));
  12. add_action('admin_post_ecwid_oauth_reconnect', array($this, 'process_authorization'));
  13. add_action('admin_post_ecwid_disconnect', array($this, 'disconnect_store'));
  14. add_action('admin_post_ecwid_show_reconnect', array($this, 'show_reconnect'));
  15. $this->_load_state();
  16. $this->api = new Ecwid_Api_V3();
  17. }
  18. public function show_reconnect()
  19. {
  20. $ecwid_oauth = $this;
  21. require_once(ECWID_PLUGIN_DIR . 'templates/reconnect.php');
  22. }
  23. public function test_post()
  24. {
  25. $return = EcwidPlatform::http_post_request($this->get_test_post_url());
  26. return is_array($return);
  27. }
  28. public function get_test_post_url()
  29. {
  30. return 'https://my.ecwid.com/api/oauth/token';
  31. }
  32. public function get_auth_dialog_url( )
  33. {
  34. $action = 'ecwid_oauth';
  35. if ( $this->_is_reconnect() ) {
  36. $action = 'ecwid_oauth_reconnect';
  37. }
  38. $redirect_uri = 'admin-post.php?action=' . $action;
  39. return $this->api->get_oauth_dialog_url(
  40. admin_url( $redirect_uri ),
  41. implode(' ', $this->_get_scope() )
  42. );
  43. }
  44. public function get_sso_reconnect_dialog_url()
  45. {
  46. $redirect_uri = 'admin-post.php?action=ecwid_oauth_reconnect';
  47. $scope = $this->_get_scope();
  48. if (!in_array('create_customers', $scope)) {
  49. $scope[] = 'create_customers';
  50. }
  51. return $this->api->get_oauth_dialog_url(
  52. admin_url( $redirect_uri ),
  53. implode(' ', $scope )
  54. );
  55. }
  56. public function process_authorization()
  57. {
  58. $reconnect = $_REQUEST['action'] == 'ecwid_oauth_reconnect';
  59. if ( isset( $_REQUEST['error'] ) || !isset( $_REQUEST['code'] ) ) {
  60. if ($reconnect) {
  61. $this->update_state(array('mode' => self::MODE_RECONNECT, 'error' => 'cancelled'));
  62. } else {
  63. $this->update_state(array('mode' => self::MODE_CONNECT, 'error' => 'cancelled'));
  64. }
  65. wp_redirect('admin.php?page=ecwid&connection_error' . ($reconnect ? '&reconnect' : ''));
  66. exit;
  67. }
  68. $base_admin_url = 'admin-post.php?action=ecwid_oauth' . ($reconnect ? '_reconnect' : '');
  69. $params['code'] = $_REQUEST['code'];
  70. $params['client_id'] = Ecwid_Api_V3::CLIENT_ID;
  71. $params['client_secret'] = Ecwid_Api_V3::CLIENT_SECRET;
  72. $params['redirect_uri'] = admin_url( $base_admin_url );
  73. $params['grant_type'] = 'authorization_code';
  74. $request = Ecwid_HTTP::create_post( 'oauth_authorize', 'https://my.ecwid.com/api/oauth/token', array(
  75. Ecwid_HTTP::POLICY_RETURN_VERBOSE
  76. ));
  77. $return = $request->do_request(array('body' => $params));
  78. if (is_array($return) && isset($return['data'])) {
  79. $result = json_decode($return['data']);
  80. }
  81. if (
  82. !is_array($return)
  83. || !isset( $result->store_id )
  84. || !isset( $result->scope )
  85. || !isset( $result->access_token )
  86. || ( $result->token_type != 'Bearer' )
  87. ) {
  88. ecwid_log_error(var_export($return, true));
  89. return $this->trigger_auth_error($reconnect ? 'reconnect' : 'default');
  90. }
  91. Ecwid_Kissmetrics::record( $reconnect ? 'accountReconnected' : 'accountConnected' );
  92. update_option( 'ecwid_store_id', $result->store_id );
  93. update_option( 'ecwid_oauth_scope', $result->scope );
  94. update_option( 'ecwid_api_check_time', 0 );
  95. EcwidPlatform::cache_reset( 'all_categories' );
  96. $this->api->save_token($result->access_token);
  97. // Reset "Create store cookie" set previously to display the landing page
  98. //in "Connect" mode rather than "Create" mode
  99. setcookie('ecwid_create_store_clicked', null, strtotime('-1 day'), ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
  100. if ( isset( $this->state->return_url ) && !empty( $this->state->return_url ) ) {
  101. wp_redirect( admin_url( $this->state->return_url ) );
  102. } else {
  103. $url = '';
  104. if ($reconnect) {
  105. $url = 'admin.php?page=ecwid&setting-updated=true';
  106. } else {
  107. $url = 'admin.php?page=ecwid';
  108. }
  109. wp_redirect( $url );
  110. }
  111. exit;
  112. }
  113. public function disconnect_store()
  114. {
  115. update_option( 'ecwid_store_id', ECWID_DEMO_STORE_ID );
  116. $this->api->save_token( '' );
  117. update_option( 'ecwid_is_api_enabled', 'off' );
  118. update_option( 'ecwid_api_check_time', 0 );
  119. wp_redirect('admin.php?page=ecwid');
  120. exit;
  121. }
  122. public function get_safe_scopes_array($scopes)
  123. {
  124. if (!isset($scopes) || empty($scopes)) {
  125. return $this->_get_default_scopes_array();
  126. }
  127. if (!empty($scopes)) {
  128. $scopes_array = explode(' ', $scopes);
  129. foreach ($scopes_array as $key => $scope) {
  130. if (!preg_match('/^[a-z_]+$/', $scope)) {
  131. unset($scopes_array[$key]);
  132. }
  133. }
  134. }
  135. return $scopes_array;
  136. }
  137. public function has_scope( $scope ) {
  138. $stored_scope = get_option( 'ecwid_oauth_scope' );
  139. if (empty($stored_scope)) {
  140. $stored_scope = 'read_store_profile read_catalog';
  141. }
  142. return in_array( $scope, explode(' ', $stored_scope) );
  143. }
  144. protected function _get_default_scopes_array() {
  145. return array( 'read_store_profile', 'read_catalog', 'allow_sso', 'create_customers' );
  146. }
  147. protected function trigger_auth_error($mode = 'default')
  148. {
  149. update_option('ecwid_last_oauth_fail_time', time());
  150. $logs = get_option('ecwid_error_log');
  151. if ($logs) {
  152. $logs = json_decode($logs);
  153. }
  154. if (is_array($logs) && count($logs) > 0) {
  155. $entry = $logs[count($logs) - 1];
  156. if (isset($entry->message)) {
  157. $last_error = $entry->message;
  158. }
  159. }
  160. if ( $mode == self::MODE_RECONNECT ) {
  161. $this->update_state(array(
  162. 'mode' => 'reconnect',
  163. 'error' => 'other'
  164. ));
  165. }
  166. if (isset($last_error)) {
  167. EcwidPlatform::report_error($last_error);
  168. }
  169. wp_redirect('admin.php?page=ecwid&connection_error' . ($mode == self::MODE_RECONNECT ? '&reconnect' : ''));
  170. exit;
  171. }
  172. protected function _get_scope() {
  173. $default = $this->_get_default_scopes_array();
  174. $scopes = array();
  175. if ( $this->_is_reconnect() ) {
  176. $scopes = isset($this->state->reconnect_scopes) && is_array($this->state->reconnect_scopes)
  177. ? $this->state->reconnect_scopes
  178. : array();
  179. }
  180. $scopes = array_merge($scopes, $default);
  181. return $scopes;
  182. }
  183. public function get_sso_admin_link() {
  184. $url = 'https://my.ecwid.com/api/v3/%s/sso?token=%s&timestamp=%s&signature=%s&inline=true';
  185. $store_id = get_ecwid_store_id();
  186. $token = $this->api->get_token();
  187. $timestamp = time();
  188. $signature = hash('sha256', $store_id . $token . $timestamp . Ecwid_Api_V3::CLIENT_SECRET);
  189. $url = sprintf(
  190. $url,
  191. $store_id,
  192. $token,
  193. $timestamp,
  194. $signature
  195. );
  196. return $url;
  197. }
  198. protected function _load_state() {
  199. if (isset($_COOKIE['ecwid_oauth_state'])) {
  200. $this->state = @json_decode( $_COOKIE['ecwid_oauth_state'] );
  201. }
  202. if (!is_object($this->state)) {
  203. $this->state = new stdClass();
  204. $this->state->reconnect_scopes = array();
  205. $this->state->reconnect_error = '';
  206. $this->state->return_url = '';
  207. $this->state->reason = '';
  208. $this->state->mode = self::MODE_CONNECT;
  209. }
  210. if (isset($_COOKIE['ecwid_create_store_clicked'])) {
  211. $this->state->create_store_clicked = $_COOKIE['ecwid_create_store_clicked'];
  212. }
  213. }
  214. public function get_state() {
  215. return $this->state;
  216. }
  217. public function was_create_store_clicked() {
  218. return $this->state->create_store_clicked;
  219. }
  220. protected function _save_state() {
  221. setcookie('ecwid_oauth_state', json_encode($this->state), strtotime('+1 day'), ADMIN_COOKIE_PATH, COOKIE_DOMAIN);
  222. }
  223. public function get_reconnect_error() {
  224. return $this->state->reconnect_error;
  225. }
  226. public function update_state($params) {
  227. if (isset($params['mode'])) {
  228. $this->state->mode = $params['mode'] == self::MODE_RECONNECT ? self::MODE_RECONNECT : self::MODE_CONNECT;
  229. }
  230. if ( $this->_is_reconnect() ) {
  231. if ( isset( $params['scope'] ) ) {
  232. $this->state->reconnect_scopes = $this->get_safe_scopes_array( @$params['scope'] );
  233. }
  234. if ( isset( $params['return_url'] ) ) {
  235. $this->state->return_url = $params['return_url'];
  236. }
  237. if ( isset( $params['error'] ) ) {
  238. $this->state->reconnect_error = $params['error'];
  239. }
  240. if ( isset( $params['reason'] ) ) {
  241. $this->state->reason = $params['reason'];
  242. }
  243. }
  244. $this->_save_state();
  245. }
  246. public function get_error() {
  247. if ($this->_is_reconnect()) {
  248. return $this->state->reconnect_error;
  249. } else {
  250. return $this->state->error;
  251. }
  252. }
  253. public function get_reconnect_message() {
  254. $reconnect_message = '';
  255. if (isset($this->state->reason)) {
  256. switch ( $this->state->reason ) {
  257. case '1':
  258. $reconnect_message = "Message 1";
  259. break;
  260. case '2':
  261. $reconnect_message = "Message 2";
  262. break;
  263. }
  264. }
  265. return $reconnect_message;
  266. }
  267. protected function _is_reconnect() {
  268. return @$this->state->mode == self::MODE_RECONNECT;
  269. }
  270. }
  271. $ecwid_oauth = new Ecwid_OAuth();