-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathproxy.php
506 lines (434 loc) · 11.9 KB
/
proxy.php
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
/**
* This file sanitizes and sends search requests to the Elasticsearch server.
*
* phpcs:disable WordPress.PHP.DiscouragedPHPFunctions
* phpcs:disable WordPress.Security.NonceVerification
* phpcs:disable WordPress.WP.AlternativeFunctions
* phpcs:disable WordPress.PHP.IniSet
*
* @package ElasticPress_Custom_Proxy
*/
if ( isset( $_SERVER['HTTP_HOST'] ) && false !== strpos( $_SERVER['HTTP_HOST'], '.test' ) ) {
error_reporting( E_ALL ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions
ini_set( 'display_errors', 1 );
}
/**
* Class to hold all the proxy functionality.
*/
class EP_PHP_Proxy {
/**
* The query to be sent to Elasticsearch.
*
* @var string|array
*/
protected $query;
/**
* The additional filters the request may need.
*
* @var array
*/
protected $filters = [];
/**
* The relation between filters.
*
* @var array
*/
protected $filter_relations = [];
/**
* Global relation between filters
*
* @var string
*/
protected $relation = '';
/**
* The request object.
*
* @var object
*/
protected $request;
/**
* The request response.
*
* @var string
*/
protected $response;
/**
* The URL of the posts index.
*
* @var string
*/
protected $post_index_url = '';
/**
* Entry point of the class.
*/
public function proxy() {
/**
* This file is built by the plugin when a reindex is done or the weighting dashboard is saved.
*
* It contains the template query, credentials, and the endpoint URL.
*/
require '../../uploads/ep-custom-proxy-credentials.php';
$this->query = $query_template;
$this->post_index_url = $post_index_url;
$this->build_query();
$this->make_request();
$this->return_response();
}
/**
* Build the query to be sent, i.e., get the template and make all necessary replaces/changes.
*/
protected function build_query() {
// For the next replacements, we'll need to work with an object
$this->query = json_decode( $this->query, true );
$this->set_search_term();
$this->set_pagination();
$this->set_order();
$this->set_highlighting();
$this->relation = ( ! empty( $_REQUEST['relation'] ) ) ? $this->sanitize_string( $_REQUEST['relation'] ) : 'or';
$this->relation = ( 'or' === $this->relation ) ? $this->relation : 'and';
$this->handle_post_type_filter();
$this->handle_taxonomies_filters();
$this->handle_price_filter();
$this->apply_filters();
$this->query = json_encode( $this->query );
}
/**
* Set the search term in the query.
*/
protected function set_search_term() {
$search_term = $this->sanitize_string( $_REQUEST['search'] );
// Stringify the JSON object again just to make the str_replace easier.
if ( ! empty( $search_term ) ) {
$query_string = json_encode( $this->query );
$query_string = str_replace( '{{ep_placeholder}}', $search_term, $query_string );
$this->query = json_decode( $query_string, true );
return;
}
// If there is no search term, get everything.
$this->query['query'] = [ 'match_all' => [ 'boost' => 1 ] ];
}
/**
* Set the pagination.
*/
protected function set_pagination() {
// Pagination
$per_page = $this->sanitize_number( $_REQUEST['per_page'] );
$offset = $this->sanitize_number( $_REQUEST['offset'] );
if ( $per_page && $per_page > 1 ) {
$this->query['size'] = $per_page;
}
if ( $offset && $offset > 1 ) {
$this->query['from'] = $offset;
}
}
/**
* Set the order.
*/
protected function set_order() {
$orderby = $this->sanitize_string( $_REQUEST['orderby'] );
$order = $this->sanitize_string( $_REQUEST['order'] );
$order = ( 'desc' === $order ) ? $order : 'asc';
$sort_clause = [];
switch ( $orderby ) {
case 'date':
$sort_clause['post_date'] = [ 'order' => $order ];
break;
case 'price':
$sort_clause['meta._price.double'] = [
'order' => $order,
'mode' => ( 'asc' === $order ) ? 'min' : 'max',
];
break;
case 'rating':
$sort_clause['meta._wc_average_rating.double'] = [ 'order' => $order ];
break;
}
if ( ! empty( $sort_clause ) ) {
$this->query['sort'] = [ $sort_clause ];
}
}
/**
* Set the highlighting clause.
*/
protected function set_highlighting() {
$this->query['highlight'] = [
'type' => 'plain',
'encoder' => 'html',
'pre_tags' => [ '' ],
'post_tags' => [ '' ],
'fields' => [
'post_title' => [
'number_of_fragments' => 0,
'no_match_size' => 9999,
],
'post_content_plain' => [
'number_of_fragments' => 2,
'fragment_size' => 200,
'no_match_size' => 200,
],
],
];
$tag = $this->sanitize_string( $_REQUEST['highlight'] );
if ( $tag ) {
$this->query['highlight']['pre_tags'] = [ "<${tag}>" ];
$this->query['highlight']['post_tags'] = [ "</${tag}>" ];
}
}
/**
* Add post types to the filters.
*/
protected function handle_post_type_filter() {
$post_types = ( ! empty( $_REQUEST['post_type'] ) ) ? explode( ',', $_REQUEST['post_type'] ) : [];
$post_types = array_filter( array_map( [ $this, 'sanitize_string' ], $post_types ) );
if ( empty( $post_types ) ) {
return;
}
if ( 'or' === $this->relation ) {
$this->filters['post_type'] = [
'terms' => [
'post_type.raw' => $post_types,
],
];
return;
}
$terms = [];
foreach ( $post_types as $post_type ) {
$terms[] = [
'term' => [
'post_type.raw' => $post_type,
],
];
}
$this->filters['post_type'] = [
'bool' => [
'must' => $terms,
],
];
}
/**
* Add taxonomies to the filters.
*/
protected function handle_taxonomies_filters() {
$taxonomies = [];
$tax_relations = ( ! empty( $_REQUEST['term_relations'] ) ) ? (array) $_REQUEST['term_relations'] : [];
foreach ( (array) $_REQUEST as $key => $value ) {
if ( ! preg_match( '/^tax-(\S+)$/', $key, $matches ) ) {
continue;
}
if ( empty( $value ) ) {
continue;
}
$taxonomy = $matches[1];
$relation = ( ! empty( $tax_relations[ $taxonomy ] ) ) ?
$this->sanitize_string( $tax_relations[ $taxonomy ] ) :
$this->relation;
$taxonomies[ $matches[1] ] = [
'relation' => $relation,
'terms' => array_map( [ $this, 'sanitize_number' ], explode( ',', $value ) ),
];
}
if ( empty( $taxonomies ) ) {
return;
}
foreach ( $taxonomies as $taxonomy_slug => $taxonomy ) {
if ( 'or' === $this->relation ) {
$this->filters[ $taxonomy_slug ] = [
'terms' => [
"terms.{$taxonomy_slug}.term_id" => $taxonomy['terms'],
],
];
return;
}
$terms = [];
foreach ( $taxonomy['terms'] as $term ) {
$terms[] = [
'term' => [
"terms.{$taxonomy_slug}.term_id" => $term,
],
];
}
$this->filters[ $taxonomy_slug ] = [
'bool' => [
'must' => $terms,
],
];
}
}
/**
* Add price ranges to the filters.
*/
protected function handle_price_filter() {
$min_price = ( ! empty( $_REQUEST['min_price'] ) ) ? $this->sanitize_string( $_REQUEST['min_price'] ) : '';
$max_price = ( ! empty( $_REQUEST['max_price'] ) ) ? $this->sanitize_string( $_REQUEST['max_price'] ) : '';
if ( $min_price ) {
$this->filters['min_price'] = [
'range' => [
'meta._price.double' => [
'gte' => $min_price,
],
],
];
}
if ( $max_price ) {
$this->filters['max_price'] = [
'range' => [
'meta._price.double' => [
'lte' => $max_price,
],
],
];
}
}
/**
* Add filters to the query.
*/
protected function apply_filters() {
$occurrence = ( 'and' === $this->relation ) ? 'must' : 'should';
$existing_filter = ( ! empty( $this->query['post_filter'] ) ) ? $this->query['post_filter'] : [ 'match_all' => [ 'boost' => 1 ] ];
if ( ! empty( $this->filters ) ) {
$this->query['post_filter'] = [
'bool' => [
'must' => [
$existing_filter,
[
'bool' => [
$occurrence => array_values( $this->filters ),
],
],
],
],
];
}
/**
* If there's no aggregations in the template or if the relation isn't 'and', we are done.
*/
if ( empty( $this->query['aggs'] ) || 'and' !== $this->relation ) {
return;
}
/**
* Apply filters to aggregations.
*
* Note the usage of `&agg` (passing by reference.)
*/
foreach ( $this->query['aggs'] as $agg_name => &$agg ) {
$new_filters = [];
/**
* Only filter an aggregation if there's sub-aggregations.
*/
if ( empty( $agg['aggs'] ) ) {
continue;
}
/**
* Get any existing filter, or a placeholder.
*/
$existing_filter = $agg['filter'] ?? [ 'match_all' => [ 'boost' => 1 ] ];
/**
* Get new filters for this aggregation.
*
* Don't apply a filter to a matching aggregation if the relation is 'or'.
*/
foreach ( $this->filters as $filter_name => $filter ) {
// @todo: this relation should not be the global one but the relation between aggs.
if ( $filter_name === $agg_name && 'or' === $this->relation ) {
continue;
}
$new_filters[] = $filter;
}
/**
* Add filters to the aggregation.
*/
if ( ! empty( $new_filters ) ) {
$agg['filter'] = [
'bool' => [
'must' => [
$existing_filter,
[
'bool' => [
$occurrence => $new_filters,
],
],
],
],
];
}
}
}
/**
* Make the cURL request.
*/
protected function make_request() {
$http_headers = [ 'Content-Type: application/json' ];
$endpoint = $this->post_index_url . '/_search';
// Create the cURL request.
$this->request = curl_init( $endpoint );
curl_setopt( $this->request, CURLOPT_POSTFIELDS, $this->query );
curl_setopt_array(
$this->request,
[
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLINFO_HEADER_OUT => true,
CURLOPT_HTTPHEADER => $http_headers,
]
);
$this->response = curl_exec( $this->request );
}
/**
* Format and output the response from Elasticsearch.
*/
protected function return_response() {
// Fetch all info from the request.
$header_size = curl_getinfo( $this->request, CURLINFO_HEADER_SIZE );
$response_header = substr( $this->response, 0, $header_size );
$response_body = substr( $this->response, $header_size );
$response_info = curl_getinfo( $this->request );
$response_code = $response_info['http_code'] ?? 500;
$response_headers = preg_split( '/[\r\n]+/', $response_info['request_header'] ?? '' );
if ( 0 === $response_code ) {
$response_code = 404;
}
curl_close( $this->request );
// Respond with the same headers, content and status code.
// Split header text into an array.
$response_headers = preg_split( '/[\r\n]+/', $response_header );
// Pass headers to output
foreach ( $response_headers as $header ) {
// Pass following headers to response
if ( preg_match( '/^(?:Content-Type|Content-Language|Content-Security|X)/i', $header ) ) {
header( $header );
} elseif ( strpos( $header, 'Set-Cookie' ) !== false ) {
// Replace cookie domain and path
$header = preg_replace( '/((?>domain)\s*=\s*)[^;\s]+/', '\1.' . $_SERVER['HTTP_HOST'], $header );
$header = preg_replace( '/\s*;?\s*path\s*=\s*[^;\s]+/', '', $header );
header( $header, false );
} elseif ( 'Content-Encoding: gzip' === $header ) {
// Decode response body if gzip encoding is used
$response_body = gzdecode( $response_body );
}
}
http_response_code( $response_code );
exit( $response_body ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Utilitary function to sanitize string.
*
* @param string $string String to be sanitized
* @return string
*/
protected function sanitize_string( $string ) {
return htmlspecialchars( $string );
}
/**
* Utilitary function to sanitize numbers.
*
* @param string $string Number to be sanitized
* @return string
*/
protected function sanitize_number( $string ) {
return filter_var( $string, FILTER_SANITIZE_NUMBER_INT );
}
}
$ep_php_proxy = new EP_PHP_Proxy();
$ep_php_proxy->proxy();