-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointer-domain-search.php
More file actions
313 lines (263 loc) · 10.8 KB
/
pointer-domain-search.php
File metadata and controls
313 lines (263 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* Plugin Name: Pointer Domain Search
* Description: WP block για αναζήτηση domain names μέσω της υπηρεσίας Pointer.gr.
* Version: 0.2.0
* Requires at least: 6.7
* Requires PHP: 7.4
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: pointer-domain-search
*
* @package WpDomainSearch
*/
if (! defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
// Ορισμός σταθερών
define('POINTER_DOMAIN_SEARCH_VERSION', '0.2.0');
define('POINTER_DOMAIN_SEARCH_PATH', plugin_dir_path(__FILE__));
// Συμπερίληψη του Pointer API
require_once plugin_dir_path(__FILE__) . 'includes/pointer-api.php';
require_once plugin_dir_path(__FILE__) . 'includes/admin-settings.php';
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @since 0.1.0
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function create_block_pointer_domain_search_block_init()
{
register_block_type(__DIR__ . '/build/pointer-domain-search');
// Localize script for AJAX URL
wp_localize_script(
'create-block-pointer-domain-search-view-script',
'wpDomainSearch',
array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'styles' => get_option('pointer_domain_search_theme', 'default'),
'nonce' => wp_create_nonce('pointer_domain_search_nonce'),
)
);
}
add_action('init', 'create_block_pointer_domain_search_block_init');
/**
* AJAX Handler για επαλήθευση των διαπιστευτηρίων API
*
* @since 0.1.0
* @return void
*/
function pointer_domain_search_verify_credentials()
{
// Έλεγχος nonce για ασφάλεια
if (! check_ajax_referer('pointer_domain_search_verify_nonce', 'nonce', false)) {
wp_send_json_error(__('Σφάλμα ασφαλείας. Παρακαλούμε ανανεώστε τη σελίδα και δοκιμάστε ξανά.', 'pointer-domain-search'));
return;
}
// Ανάκτηση credentials από τις ρυθμίσεις
$username = get_option('pointer_domain_search_username', '');
$encrypted_password = get_option('pointer_domain_search_password', '');
$password = '';
// Αποκρυπτογράφηση του password αν υπάρχει
if (! empty($encrypted_password)) {
$password = pointer_domain_search_decrypt_password($encrypted_password);
}
// Έλεγχος αν έχουν οριστεί τα credentials
if (empty($username) || empty($password)) {
wp_send_json_error(__('Τα διαπιστευτήρια API δεν έχουν οριστεί. Παρακαλούμε συμπληρώστε τα παραπάνω και αποθηκεύστε τις ρυθμίσεις.', 'pointer-domain-search'));
return;
}
try {
// Δημιουργία νέου αντικειμένου Pointer API
$pointer = new Pointer_API();
// Σύνδεση στο API
$key = $pointer->login($username, $password);
// Αποσύνδεση από το API
$pointer->logout();
if (! empty($key)) {
wp_send_json_success(__('Η σύνδεση με το API της Pointer.gr ήταν επιτυχής!', 'pointer-domain-search'));
} else {
wp_send_json_error(__('Αποτυχία σύνδεσης. Ο server επέστρεψε κενό κλειδί API.', 'pointer-domain-search'));
}
} catch (Exception $e) {
wp_send_json_error(__('Σφάλμα API: ', 'pointer-domain-search') . $e->getMessage());
}
}
add_action('wp_ajax_pointer_domain_search_verify_credentials', 'pointer_domain_search_verify_credentials');
/**
* Handle AJAX domain search request
*
* @since 0.1.0
* @return void
*/
function pointer_domain_search_ajax_handler()
{
// Έλεγχος nonce για ασφάλεια
if (! check_ajax_referer('pointer_domain_search_nonce', 'nonce', false)) {
wp_send_json_error('Σφάλμα ασφαλείας. Παρακαλούμε ανανεώστε τη σελίδα και δοκιμάστε ξανά.');
}
// Έλεγχος rate limiting
if (! pointer_domain_search_check_rate_limit()) {
wp_send_json_error('Πολλές αιτήσεις. Παρακαλούμε δοκιμάστε ξανά σε λίγα λεπτά.');
}
// Έλεγχος αν έχουν σταλεί όλα τα απαραίτητα δεδομένα
if (empty($_POST['domain']) || empty($_POST['tlds'])) {
wp_send_json_error('Λείπουν απαραίτητα δεδομένα.');
}
// Ανάκτηση και καθαρισμός των δεδομένων
$domain = sanitize_text_field(wp_unslash($_POST['domain']));
$tlds_json = sanitize_text_field(wp_unslash($_POST['tlds']));
$tlds = json_decode($tlds_json, true);
if (! is_array($tlds)) {
wp_send_json_error('Μη έγκυρα TLDs.');
}
// Επιπλέον καθαρισμός κάθε στοιχείου του πίνακα
$tlds = array_map('sanitize_text_field', $tlds);
// Ανάκτηση credentials από τις ρυθμίσεις
$username = get_option('pointer_domain_search_username', '');
$encrypted_password = get_option('pointer_domain_search_password', '');
$password = '';
// Αποκρυπτογράφηση του password αν υπάρχει
if (! empty($encrypted_password)) {
$password = pointer_domain_search_decrypt_password($encrypted_password);
}
// Έλεγχος αν έχουν οριστεί τα credentials
if (empty($username) || empty($password)) {
wp_send_json_error('Δεν έχουν οριστεί τα διαπιστευτήρια API. Παρακαλούμε επικοινωνήστε με τον διαχειριστή.');
}
try {
// Δημιουργία νέου αντικειμένου Pointer API
$pointer = new Pointer_API();
// Σύνδεση στο API
$pointer->login($username, $password);
// Θέλουμε να αφαιρρέσουμε το tld αν υπάρχει από το domain
$domain = explode('.', $domain);
$domain = $domain[0];
// Έλεγχος rate limit
if (! pointer_domain_search_check_rate_limit()) {
wp_send_json_error('Έχετε υπερβεί το όριο αιτημάτων. Παρακαλώ προσπαθήστε ξανά αργότερα.');
}
// Αναζήτηση διαθεσιμότητας domain
$results = $pointer->domainCheck($domain, $tlds);
// Αποσύνδεση από το API
$pointer->logout();
// Καταγραφή της επιτυχημένης αίτησης για rate limiting
pointer_domain_search_log_request();
// Επιστροφή των αποτελεσμάτων
wp_send_json_success($results);
} catch (Exception $e) {
// Καταγραφή του σφάλματος για debugging
if (defined('WP_DEBUG') && WP_DEBUG === true) {
// phpcs:disable WordPress.PHP.DevelopmentFunctions
error_log('Pointer API Error: ' . $e->getMessage());
// phpcs:enable
}
wp_send_json_error('Σφάλμα API: ' . $e->getMessage());
}
}
/**
* Έλεγχος για rate limiting
*
* @since 0.1.0
* @return bool Αν το αίτημα επιτρέπεται
*/
function pointer_domain_search_check_rate_limit()
{
// Παίρνουμε τη διεύθυνση IP του χρήστη
$user_ip = pointer_domain_search_get_user_ip();
// Ανάκτηση ρυθμίσεων rate limiting
$max_requests = absint(get_option('pointer_domain_search_rate_limit', 10));
$time_window = 60 * 5; // 5 λεπτά σε δευτερόλεπτα
// Παίρνουμε το ιστορικό αιτημάτων για αυτή την IP
$requests = get_transient('pointer_domain_search_requests_' . md5($user_ip));
if (! $requests) {
$requests = array();
}
// Αφαίρεση παλιών αιτημάτων
$current_time = time();
$requests = array_filter(
$requests,
function ($timestamp) use ($current_time, $time_window) {
return ($current_time - $timestamp) <= $time_window;
}
);
// Έλεγχος αν έχει φτάσει το όριο αιτημάτων
if (count($requests) >= $max_requests) {
return false;
}
return true;
}
/**
* Καταγραφή αιτήματος για rate limiting
*
* @since 0.1.0
* @return void
*/
function pointer_domain_search_log_request()
{
// Παίρνουμε τη διεύθυνση IP του χρήστη
$user_ip = pointer_domain_search_get_user_ip();
// Παίρνουμε το ιστορικό αιτημάτων για αυτή την IP
$requests = get_transient('pointer_domain_search_requests_' . md5($user_ip));
if (! $requests) {
$requests = array();
}
// Προσθήκη τρέχοντος χρόνου στο ιστορικό
$requests[] = time();
// Αποθήκευση του ιστορικού
set_transient('pointer_domain_search_requests_' . md5($user_ip), $requests, 60 * 60); // Αποθήκευση για 1 ώρα
}
/**
* Ανάκτηση IP διεύθυνσης χρήστη
*
* @since 0.1.0
* @return string IP διεύθυνση χρήστη.
*/
function pointer_domain_search_get_user_ip()
{
if (! empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP']));
} elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR']));
} else {
$ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : '';
}
return apply_filters('pointer_domain_search_user_ip', $ip);
}
/**
* Εφαρμογή CSS για το επιλεγμένο θέμα
*
* @since 0.1.0
* @return void
*/
function pointer_domain_search_enqueue_theme_styles()
{
$theme = get_option('pointer_domain_search_theme', 'default');
$valid_themes = array('default', 'dark', 'light', 'colorful');
// Έλεγχος εγκυρότητας θέματος
if (! in_array($theme, $valid_themes, true)) {
$theme = 'default';
}
if ('default' !== $theme) {
// Δημιουργία του URL με βάση την απόλυτη διαδρομή για να αποφύγουμε λάθη
$css_url = plugin_dir_url(__FILE__) . 'assets/css/themes/' . $theme . '.css';
// Προσθήκη μοναδικού αναγνωριστικού για να αποφύγουμε την cache
$cache_buster = filemtime(plugin_dir_path(__FILE__) . 'assets/css/themes/' . $theme . '.css');
if (! $cache_buster) {
$cache_buster = time();
}
wp_enqueue_style(
'pointer-domain-search-theme-' . $theme,
$css_url,
array(),
$cache_buster
);
}
}
add_action('wp_enqueue_scripts', 'pointer_domain_search_enqueue_theme_styles');
// Προσθήκη των AJAX endpoints
add_action('wp_ajax_pointer_domain_search', 'pointer_domain_search_ajax_handler');
add_action('wp_ajax_nopriv_pointer_domain_search', 'pointer_domain_search_ajax_handler');