config
1 month ago
Amazon.php
4 years ago
Helper.php
1 month ago
InstalledPlugin.php
3 years ago
InstalledTheme.php
4 years ago
Login.php
1 month ago
Product.php
4 years ago
ProductActions.php
4 years ago
ProductState.php
1 year ago
WpAjaxUpgraderSkin.php
4 years ago
Login.php
409 lines
| 1 | <?php |
| 2 | namespace Tenweb_Authorization; |
| 3 | |
| 4 | use Tenweb_Authorization\Helper; |
| 5 | |
| 6 | class Login { |
| 7 | |
| 8 | protected static $instance = null; |
| 9 | |
| 10 | private $domain_id; |
| 11 | private $access_token = false; |
| 12 | private $refresh_token = false; |
| 13 | private $connected_from; |
| 14 | private $error_logs = array(); |
| 15 | |
| 16 | protected function __construct() |
| 17 | { |
| 18 | $access_token = get_site_option(TENWEB_PREFIX . '_access_token'); |
| 19 | $refresh_token = get_site_option(TENWEB_PREFIX . '_refresh_token'); |
| 20 | $connected_from = get_site_option(TENWEB_PREFIX . '_connected_from'); |
| 21 | $this->access_token = !empty($access_token) ? $access_token : false; |
| 22 | $this->refresh_token = !empty($refresh_token) ? $refresh_token : false; |
| 23 | $this->connected_from = !empty($connected_from) ? $connected_from : TENWEB_CONNECTED_MANAGER; |
| 24 | add_action('init', array($this, 'fire_actions')); |
| 25 | } |
| 26 | |
| 27 | public function login($email = "", $pwd = "", $site_type = "", $args = array()) |
| 28 | { |
| 29 | |
| 30 | /*if ($this->access_token !== false) { |
| 31 | $this->error_logs["error"] = 1; |
| 32 | $this->error_logs["message"] = 'User already logged in.'; |
| 33 | |
| 34 | return false; |
| 35 | }*/ |
| 36 | |
| 37 | |
| 38 | |
| 39 | $blog_id = null; |
| 40 | if (is_multisite()) { |
| 41 | //CHECK get_site_info function |
| 42 | $blog_id = 'multisite'; |
| 43 | } |
| 44 | $body = Helper::get_site_info($blog_id); |
| 45 | |
| 46 | if (is_multisite()) { |
| 47 | $body["domains"] = Helper::get_blogs_info(); |
| 48 | } |
| 49 | |
| 50 | $body['is_logged_in'] = false; |
| 51 | if($this->check_logged_in()){ |
| 52 | $body['is_logged_in'] = true; |
| 53 | } |
| 54 | |
| 55 | if (empty($email)) { |
| 56 | $body['email'] = $_POST['email']; |
| 57 | $body['password'] = $_POST['password']; |
| 58 | if (isset($_POST['site_type'])) { |
| 59 | $body['site_type'] = $_POST['site_type']; |
| 60 | } |
| 61 | } else { |
| 62 | $body['email'] = $email; |
| 63 | $body['password'] = $pwd; |
| 64 | $body['site_type'] = $site_type; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | $confirm_token = md5(uniqid(mt_rand(), true)); |
| 69 | $this->setConfirmToken($confirm_token); |
| 70 | |
| 71 | $body['confirm_token'] = $confirm_token; |
| 72 | |
| 73 | $body = $body + $args; |
| 74 | |
| 75 | $url = TENWEB_API_URL . '/login'; |
| 76 | $result = wp_remote_post($url, array( |
| 77 | 'method' => 'POST', |
| 78 | 'body' => $body, |
| 79 | 'timeout' => 1500, |
| 80 | 'headers' => array( |
| 81 | "Accept" => "application/x.10webmanager.v1+json" |
| 82 | ) |
| 83 | )); |
| 84 | if (is_wp_error($result)) { |
| 85 | Helper::set_error_log('login_wp_error', $result->get_error_message()); |
| 86 | $this->error_logs["error"] = 1; |
| 87 | $this->error_logs["message"] = $result->get_error_message(); |
| 88 | |
| 89 | return false; |
| 90 | } else { |
| 91 | $res_obj = json_decode($result['body']); |
| 92 | |
| 93 | if (isset($res_obj->error)) { |
| 94 | Helper::set_error_log('login_error_from_api', json_encode($res_obj->error)); |
| 95 | $this->access_token = false; |
| 96 | if (isset($res_obj->error->message)) { |
| 97 | $this->error_logs["error"] = 1; |
| 98 | $this->error_logs["message"] = $res_obj->error->message; |
| 99 | } |
| 100 | |
| 101 | return false; |
| 102 | |
| 103 | } else if (isset($res_obj->status) && $res_obj->status == 'ok') { |
| 104 | |
| 105 | if(!empty($args['connected_from']) && $args['connected_from'] == 'ai_assistant') { |
| 106 | $this->set_access_token($res_obj->token, true); |
| 107 | $this->set_refresh_token($res_obj->refresh_token, true); |
| 108 | update_site_option(TENWEB_PREFIX . '_ai_assistant_domain_id', $res_obj->domain_id); |
| 109 | } else { |
| 110 | $this->set_access_token($res_obj->token); |
| 111 | $this->set_refresh_token($res_obj->refresh_token); |
| 112 | update_site_option(TENWEB_PREFIX . '_domain_id', $res_obj->domain_id); |
| 113 | } |
| 114 | |
| 115 | update_site_option(TENWEB_PREFIX . '_workspace_id', $res_obj->workspace_id); |
| 116 | update_site_option(TENWEB_PREFIX . '_is_available', $res_obj->is_available . ''); |
| 117 | update_site_option(TENWEB_PREFIX . '_user_timezone_offset', $res_obj->timezone_offset); |
| 118 | |
| 119 | $user_info = array( |
| 120 | 'client_info' => array( |
| 121 | 'name' => $res_obj->client_name, |
| 122 | 'timezone_offset' => $res_obj->timezone_offset |
| 123 | ), |
| 124 | 'agreement_info' => $res_obj->agreement_info |
| 125 | ); |
| 126 | |
| 127 | set_site_transient(TENWEB_PREFIX . '_user_info_transient', '1', 43200); //12 hours |
| 128 | update_site_option(TENWEB_PREFIX . '_user_info', $user_info); |
| 129 | |
| 130 | if(property_exists($res_obj, 'connected_from')) { |
| 131 | update_site_option(TENWEB_PREFIX . '_connected_from', $res_obj->connected_from); |
| 132 | } |
| 133 | |
| 134 | if (property_exists($res_obj, 'two_flow_data')) { |
| 135 | if(property_exists($res_obj->two_flow_data, 'flow_id')) { |
| 136 | update_site_option(TENWEB_PREFIX . '_flow_id', $res_obj->two_flow_data->flow_id); |
| 137 | } |
| 138 | |
| 139 | if(property_exists($res_obj->two_flow_data, 'notification_id')) { |
| 140 | update_site_option(TENWEB_PREFIX . '_notification_id', $res_obj->two_flow_data->notification_id); |
| 141 | } |
| 142 | |
| 143 | if(property_exists($res_obj->two_flow_data, 'referral_hash')) { |
| 144 | update_site_option(TENWEB_PREFIX . '_client_referral_hash', $res_obj->two_flow_data->referral_hash); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (is_multisite() && !empty($res_obj->domains)) { |
| 149 | foreach ($res_obj->domains as $blog_id => $domain) { |
| 150 | if (!empty($res_obj->connected_from) && $res_obj->connected_from == 'ai_assistant') { |
| 151 | update_blog_option($blog_id, TENWEB_PREFIX . '_ai_assistant_domain_id', $domain->domain_id); |
| 152 | } else { |
| 153 | update_blog_option($blog_id, TENWEB_PREFIX . '_domain_id', $domain->domain_id); |
| 154 | } |
| 155 | update_blog_option($blog_id, TENWEB_PREFIX . '_is_available', $domain->is_available); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | $this->domain_id = $res_obj->domain_id; |
| 160 | /* create 10web user */ |
| 161 | |
| 162 | do_action('tenweb_force_updates_check'); |
| 163 | // $user = User::get_instance($res_obj->password); |
| 164 | Helper::clear_cache(); |
| 165 | Helper::check_site_state(true); |
| 166 | |
| 167 | do_action('tenweb_logged_in'); |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | public function get_access_token() |
| 177 | { |
| 178 | return $this->access_token; |
| 179 | } |
| 180 | |
| 181 | public function set_access_token($token, $ai_assistant = false) |
| 182 | { |
| 183 | $this->access_token = $token; |
| 184 | $this->save_access_token($token, $ai_assistant); |
| 185 | } |
| 186 | |
| 187 | public function get_refresh_token() |
| 188 | { |
| 189 | return $this->refresh_token; |
| 190 | } |
| 191 | |
| 192 | public function set_refresh_token($token, $ai_assistant = false) |
| 193 | { |
| 194 | $this->refresh_token = $token; |
| 195 | $this->save_refresh_token($token, $ai_assistant); |
| 196 | } |
| 197 | |
| 198 | private function save_access_token($new_token, $ai_assistant = false) |
| 199 | { |
| 200 | if ($ai_assistant) { |
| 201 | update_site_option(TENWEB_PREFIX . '_ai_assistant_access_token', $new_token); |
| 202 | } else { |
| 203 | update_site_option(TENWEB_PREFIX . '_access_token', $new_token); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | private function save_refresh_token($new_token, $ai_assistant=false) |
| 208 | { |
| 209 | |
| 210 | if ($ai_assistant) { |
| 211 | update_site_option(TENWEB_PREFIX . '_ai_assistant_refresh_token', $new_token); |
| 212 | } else { |
| 213 | update_site_option(TENWEB_PREFIX . '_refresh_token', $new_token); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public function logout($redirect = true, $disconnected_service = 'manager') |
| 218 | { |
| 219 | // $workspace_id = \TenwebServices::get_workspace_id(); |
| 220 | $args = array(); |
| 221 | $domain_id = get_site_option('tenweb_domain_id'); |
| 222 | if ($disconnected_service == 'ai_assistant') { |
| 223 | $domain_id = get_site_option('tenweb_ai_assistant_domain_id'); |
| 224 | $this->access_token = get_site_option(TENWEB_PREFIX . '_ai_assistant_access_token'); |
| 225 | $this->refresh_token = get_site_option(TENWEB_PREFIX . '_ai_assistant_refresh_token'); |
| 226 | $args['disconnected_service'] = $disconnected_service; |
| 227 | } |
| 228 | $url = TENWEB_API_URL . '/domains/' . $domain_id . '/logout'; |
| 229 | |
| 230 | $result = wp_remote_request($url, array( |
| 231 | 'timeout' => 1500, |
| 232 | 'method' => 'POST', |
| 233 | 'headers' => array( |
| 234 | "Authorization" => "Bearer " . $this->access_token, |
| 235 | "Accept" => "application/x.10webmanager.v1+json" |
| 236 | ), |
| 237 | 'body' => $args |
| 238 | )); |
| 239 | |
| 240 | if (is_wp_error($result)) { |
| 241 | Helper::set_error_log('logout_wp_error', $result->get_error_message()); |
| 242 | } else { |
| 243 | $res_arr = json_decode($result['body'], true); |
| 244 | if (isset($res_arr['error'])) { |
| 245 | Helper::set_error_log('logout_api_error', json_encode($res_arr['error'])); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
| 250 | $this->force_logout($disconnected_service); |
| 251 | // $user = User::get_instance(); |
| 252 | |
| 253 | do_action('tenweb_logged_out'); |
| 254 | |
| 255 | if ($redirect) { |
| 256 | wp_safe_redirect('admin.php?page=tenweb_menu'); |
| 257 | exit(); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | } |
| 262 | |
| 263 | public function check_logged_in() |
| 264 | { |
| 265 | return !empty($this->access_token); |
| 266 | } |
| 267 | |
| 268 | public function get_connection_type() |
| 269 | { |
| 270 | return $this->connected_from; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | public function force_logout($disconnected_service='manager') |
| 275 | { |
| 276 | if ($disconnected_service == 'ai_assistant'){ |
| 277 | delete_site_option(TENWEB_PREFIX . '_ai_assistant_access_token'); |
| 278 | delete_site_option(TENWEB_PREFIX . '_ai_assistant_refresh_token'); |
| 279 | } else { |
| 280 | delete_site_option(TENWEB_PREFIX . '_access_token'); |
| 281 | delete_site_option(TENWEB_PREFIX . '_refresh_token'); |
| 282 | } |
| 283 | $this->access_token = false; |
| 284 | $this->refresh_token = false; |
| 285 | Helper::clear_cache(); |
| 286 | } |
| 287 | |
| 288 | public function fire_actions() |
| 289 | { |
| 290 | $action = $this->find_request_var('action', ''); |
| 291 | |
| 292 | |
| 293 | if (!empty($action)) { |
| 294 | switch ($action) { |
| 295 | case 'logout': |
| 296 | //delete_site_option(TENWEB_PREFIX . '_access_token'); |
| 297 | if (isset($_POST[TENWEB_PREFIX . '_nonce']) && wp_verify_nonce($_POST[TENWEB_PREFIX . '_nonce'], TENWEB_PREFIX . '_nonce')) { |
| 298 | $this->logout(); |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | |
| 306 | private function find_request_var($key, $default_value = null) |
| 307 | { |
| 308 | if (isset($_POST[$key])) { |
| 309 | return $_POST[$key]; |
| 310 | } else if (isset($_GET[$key])) { |
| 311 | return $_GET[$key]; |
| 312 | } else if (isset($default_value)) { |
| 313 | return $default_value; |
| 314 | } |
| 315 | |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | public static function get_instance() |
| 320 | { |
| 321 | if (null == self::$instance) { |
| 322 | self::$instance = new self; |
| 323 | } |
| 324 | |
| 325 | return self::$instance; |
| 326 | } |
| 327 | |
| 328 | public function get_errors() |
| 329 | { |
| 330 | return $this->error_logs; |
| 331 | } |
| 332 | |
| 333 | public function check_request($request) |
| 334 | { |
| 335 | if (!$this->check_logged_in()) { |
| 336 | $data_for_response = array( |
| 337 | "code" => "unauthorized", |
| 338 | "message" => "manager unauthorized, please login", |
| 339 | "data" => array( |
| 340 | "status" => 401 |
| 341 | ) |
| 342 | ); |
| 343 | |
| 344 | return new \WP_REST_Response($data_for_response, 401); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | public function authorize($request, $check_for_network = false) |
| 349 | { |
| 350 | |
| 351 | $response = array( |
| 352 | "code" => "unauthorized", |
| 353 | "message" => "unauthorized: incorrect token", |
| 354 | "data" => array( |
| 355 | "status" => 401 |
| 356 | ) |
| 357 | ); |
| 358 | |
| 359 | $token = $request->get_header('tenweb-authorization'); |
| 360 | if (empty($token)) { |
| 361 | $response['message'] = 'unauthorized: no token'; |
| 362 | |
| 363 | return $response; |
| 364 | } |
| 365 | |
| 366 | //check user pwd |
| 367 | if ($this->check_password($token) === true) { |
| 368 | return true; |
| 369 | } |
| 370 | //check token |
| 371 | $helper = Helper::get_instance(); |
| 372 | if ($helper->check_single_token($token, $check_for_network) === true) { |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | return $response; |
| 377 | } |
| 378 | |
| 379 | public function check_password($pwd) |
| 380 | { |
| 381 | |
| 382 | $failed_login_attempts = intval(get_site_transient(TENWEB_PREFIX . 'failed_login_attempts')); |
| 383 | /* do not allow more than three login attempts with wrong pwd*/ |
| 384 | if ($failed_login_attempts >= 12) { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | set_site_transient(TENWEB_PREFIX . 'failed_login_attempts', $failed_login_attempts + 1, 12 * 60 * 60); |
| 389 | |
| 390 | return false; |
| 391 | |
| 392 | } |
| 393 | |
| 394 | public function setConfirmToken($confirm_token){ |
| 395 | update_site_option(TENWEB_PREFIX . '_confirm_token', $confirm_token); // 5 min |
| 396 | } |
| 397 | public function getConfirmToken(){ |
| 398 | // if both option and transient exist in db |
| 399 | return array(get_site_option(TENWEB_PREFIX . '_confirm_token'), get_site_transient(TENWEB_PREFIX . '_confirm_token')); |
| 400 | } |
| 401 | |
| 402 | public function checkConfirmToken($confirm_token) { |
| 403 | $saved_token =$this->getConfirmToken(); |
| 404 | return $confirm_token === $saved_token[0] || $confirm_token === $saved_token[1]; |
| 405 | } |
| 406 | |
| 407 | |
| 408 | } |
| 409 |