PluginProbe ʕ •ᴥ•ʔ
Hostinger Reach – AI-Powered Email Marketing for WordPress / 1.5.9
Hostinger Reach – AI-Powered Email Marketing for WordPress v1.5.9
1.6.0 1.5.9 1.5.8 1.5.7 1.5.6 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 trunk 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6
hostinger-reach / src / Api / ApiKeyManager.php
hostinger-reach / src / Api Last commit date
Handlers 2 weeks ago Routes 2 weeks ago Webhooks 8 months ago ApiKeyManager.php 3 months ago ResourceIdManager.php 5 months ago
ApiKeyManager.php
95 lines
1 <?php
2
3 namespace Hostinger\Reach\Api;
4
5 use Hostinger\Reach\Admin\Notices\ConnectionNotice;
6 use Hostinger\Reach\Setup\Encrypt;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 class ApiKeyManager {
13 public const CSRF_TRANSIENT = 'hostinger_reach_csrf_token';
14 public const CSRF_TRANSIENT_EXPIRATION = 500;
15 public const API_KEY_NAME = 'hostinger_reach_api_key';
16 public const API_CONNECTION_TIME_NAME = 'hostinger_reach_api_connection_time';
17
18 public function store_token( string $token ): bool {
19 add_option( self::API_CONNECTION_TIME_NAME, time() );
20
21 return update_option( self::API_KEY_NAME, $this->encrypt_token( $token ) );
22 }
23
24 public function get_token(): string {
25 return $this->decrypt_token( get_option( self::API_KEY_NAME, '' ) );
26 }
27
28 public function encrypt_token( string $token ): string {
29 return Encrypt::encrypt( $token );
30 }
31
32 public function decrypt_token( string $token ): string {
33 return Encrypt::decrypt( $token );
34 }
35
36 public function validate_csrf( string $csrf ): bool {
37 $transient_csrf = $this->get_csrf();
38 $order_id = $this->get_order_id();
39 if ( $transient_csrf && $transient_csrf === $csrf ) {
40 return true;
41 }
42
43 if ( $order_id && $order_id === $csrf ) {
44 return true;
45 }
46
47 return false;
48 }
49
50 public function generate_csrf(): void {
51 set_transient( self::CSRF_TRANSIENT, wp_generate_password( 12, false ), self::CSRF_TRANSIENT_EXPIRATION );
52 }
53
54 public function get_csrf(): string {
55 return get_transient( self::CSRF_TRANSIENT );
56 }
57
58 public function clear_csrf(): void {
59 delete_transient( self::CSRF_TRANSIENT );
60 }
61
62 public function clear_token(): void {
63 delete_option( self::API_KEY_NAME );
64 delete_transient( ConnectionNotice::NOTICE_DISMISS_TRANSIENT );
65 }
66
67 public function is_connected(): bool {
68 return ! empty( $this->get_token() );
69 }
70
71 protected function get_order_id(): string {
72 $api_token_path = $this->get_api_token_path();
73 if ( file_exists( $api_token_path ) ) {
74 $order_id = trim( file_get_contents( $api_token_path ) );
75 }
76
77 if ( ! empty( $order_id ) ) {
78 return $order_id;
79 }
80
81 return '';
82 }
83
84 private function get_api_token_path(): string {
85 $hostinger_parts = explode( '/', __DIR__ );
86 if ( count( $hostinger_parts ) >= 3 ) {
87 $hostinger_root = '/' . $hostinger_parts[1] . '/' . $hostinger_parts[2];
88
89 return $hostinger_root . '/.api_token';
90 }
91
92 return '';
93 }
94 }
95