1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cloudflare\Zone; |
4
|
|
|
|
5
|
|
|
use Cloudflare\Api; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* CloudFlare API wrapper |
9
|
|
|
* |
10
|
|
|
* Page rules for a Zone |
11
|
|
|
* A rule describing target patterns for requests and actions to perform on matching requests |
12
|
|
|
* |
13
|
|
|
* @author James Bell <[email protected]> |
14
|
|
|
* |
15
|
|
|
* @version 1 |
16
|
|
|
*/ |
17
|
|
|
class Pagerules extends Api |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Create a page rule [BETA] (permission needed: #zone:edit) |
21
|
|
|
* |
22
|
|
|
* @param string $zone_identifier API item identifier tag |
23
|
|
|
* @param array $targets Targets to evaluate on a request |
24
|
|
|
* @param array $actions The set of actions to perform if the targets of this rule match the request. |
25
|
|
|
* Actions can redirect the url to another url or override settings (but not both) |
26
|
|
|
* @param int|null $priority A number that indicates the preference for a page rule over another. In the case where |
27
|
|
|
* you may have a catch-all page rule (e.g., #1: '/images/') but want a rule that is more |
28
|
|
|
* specific to take precedence (e.g., #2: '/images/special/'), you'll want to specify a |
29
|
|
|
* higher priority on the latter (#2) so it will override the first. |
30
|
|
|
* @param string|null $status Status of the page rule |
31
|
|
|
*/ |
32
|
|
|
public function create($zone_identifier, $targets, $actions, $priority = null, $status = 'active') |
33
|
|
|
{ |
34
|
|
|
$data = [ |
35
|
|
|
'targets' => $targets, |
36
|
|
|
'actions' => $actions, |
37
|
|
|
'priority' => $priority, |
38
|
|
|
'status' => $status, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
return $this->post('zones/'.$zone_identifier.'/pagerules', $data); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* List page rules [BETA] (permission needed: #zone:read) |
46
|
|
|
* |
47
|
|
|
* @param string $zone_identifier API item identifier tag |
48
|
|
|
* @param string|null $status Status of the page rule |
49
|
|
|
* @param string|null $order Field to order page rules by (status, priority) |
50
|
|
|
* @param string|null $direction Direction to order page rules (asc, desc) |
51
|
|
|
* @param string|null $match Whether to match all search requirements or at least one (any) (any, all) |
52
|
|
|
*/ |
53
|
|
|
public function list_pagerules($zone_identifier, $status = null, $order = null, $direction = null, $match = null) |
54
|
|
|
{ |
55
|
|
|
$data = [ |
56
|
|
|
'status' => $status, |
57
|
|
|
'order' => $order, |
58
|
|
|
'direction' => $direction, |
59
|
|
|
'match' => $match, |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
return $this->get('zones/'.$zone_identifier.'/pagerules', $data); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Page rule details [BETA] (permission needed: #zone:read) |
67
|
|
|
* |
68
|
|
|
* @param string $zone_identifier API item identifier tag |
69
|
|
|
* @param string $identifier |
70
|
|
|
*/ |
71
|
|
|
public function details($zone_identifier, $identifier) |
72
|
|
|
{ |
73
|
|
|
return $this->get('zones/'.$zone_identifier.'/pagerules/'.$identifier); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Change a page rule [BETA] (permission needed: #zone:edit) |
78
|
|
|
* |
79
|
|
|
* @param string $zone_identifier API item identifier tag |
80
|
|
|
* @param string $identifier |
81
|
|
|
* @param array|null $targets Targets to evaluate on a request |
82
|
|
|
* @param array|null $actions The set of actions to perform if the targets of this rule match the request. |
83
|
|
|
* Actions can redirect the url to another url or override settings (but not both) |
84
|
|
|
* @param int|null $priority A number that indicates the preference for a page rule over another. In the case where |
85
|
|
|
* you may have a catch-all page rule (e.g., #1: '/images/') but want a rule that is more |
86
|
|
|
* specific to take precedence (e.g., #2: '/images/special/'), you'll want to specify a |
87
|
|
|
* higher priority on the latter (#2) so it will override the first. |
88
|
|
|
* @param string|null $status Status of the page rule |
89
|
|
|
*/ |
90
|
|
|
public function change($zone_identifier, $identifier, $targets = null, $actions = null, $priority = null, $status = null) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$data = [ |
93
|
|
|
'targets' => $targets, |
94
|
|
|
'actions' => $actions, |
95
|
|
|
'priority' => $priority, |
96
|
|
|
'status' => $status, |
97
|
|
|
]; |
98
|
|
|
|
99
|
|
|
return $this->patch('zones/'.$zone_identifier.'/pagerules/'.$identifier, $data); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Update a page rule [BETA] (permission needed: #zone:edit) |
104
|
|
|
* Replace a page rule. The final rule will exactly match the data passed with this request. |
105
|
|
|
* |
106
|
|
|
* @param string $zone_identifier API item identifier tag |
107
|
|
|
* @param string $identifier |
108
|
|
|
* @param array $targets Targets to evaluate on a request |
109
|
|
|
* @param array $actions The set of actions to perform if the targets of this rule match the request. |
110
|
|
|
* Actions can redirect the url to another url or override settings (but not both) |
111
|
|
|
* @param int|null $priority A number that indicates the preference for a page rule over another. In the case where |
112
|
|
|
* you may have a catch-all page rule (e.g., #1: '/images/') but want a rule that is more |
113
|
|
|
* specific to take precedence (e.g., #2: '/images/special/'), you'll want to specify a |
114
|
|
|
* higher priority on the latter (#2) so it will override the first. |
115
|
|
|
* @param string|null $status Status of the page rule |
116
|
|
|
*/ |
117
|
|
|
public function update($zone_identifier, $identifier, $targets, $actions, $priority = null, $status = null) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
$data = [ |
120
|
|
|
'targets' => $targets, |
121
|
|
|
'actions' => $actions, |
122
|
|
|
'priority' => $priority, |
123
|
|
|
'status' => $status, |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
return $this->put('zones/'.$zone_identifier.'/pagerules/'.$identifier, $data); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Delete a page rule [BETA] (permission needed: #zone:edit) |
131
|
|
|
* |
132
|
|
|
* @param string $zone_identifier API item identifier tag |
133
|
|
|
* @param string $identifier |
134
|
|
|
*/ |
135
|
|
|
public function delete_pagerule($zone_identifier, $identifier) |
136
|
|
|
{ |
137
|
|
|
return $this->delete('zones/'.$zone_identifier.'/pagerules/'.$identifier); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.