| 199 |
lars |
1 |
<?php namespace Clockwork\Request;
|
|
|
2 |
|
|
|
3 |
// Filter incoming requests before collecting data
|
|
|
4 |
class ShouldCollect
|
|
|
5 |
{
|
|
|
6 |
// Enable on-demand mode, boolean or the secret value
|
|
|
7 |
protected $onDemand = false;
|
|
|
8 |
// Enable sampling, chance to be sampled (eg. 100 to collect 1 in 100 requests)
|
|
|
9 |
protected $sample = false;
|
|
|
10 |
|
|
|
11 |
// List of URIs that should not be collected, can contain regexes
|
|
|
12 |
protected $except = [];
|
|
|
13 |
// List of URIs that should only be collected, can contain regexes (only used if non-empty)
|
|
|
14 |
protected $only = [];
|
|
|
15 |
|
|
|
16 |
// Disable collection of OPTIONS method requests (most commonly used for CORS pre-flight requests)
|
|
|
17 |
protected $exceptPreflight = false;
|
|
|
18 |
|
|
|
19 |
// Custom filter callback
|
|
|
20 |
protected $callback;
|
|
|
21 |
|
|
|
22 |
// Append one or more except URIs
|
|
|
23 |
public function except($uris)
|
|
|
24 |
{
|
|
|
25 |
$this->except = array_merge($this->except, is_array($uris) ? $uris : [ $uris ]);
|
|
|
26 |
|
|
|
27 |
return $this;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
// Append one or more only URIs
|
|
|
31 |
public function only($uris)
|
|
|
32 |
{
|
|
|
33 |
$this->only = array_merge($this->only, is_array($uris) ? $uris : [ $uris ]);
|
|
|
34 |
|
|
|
35 |
return $this;
|
|
|
36 |
}
|
|
|
37 |
|
|
|
38 |
// Merge multiple settings from array
|
|
|
39 |
public function merge(array $data = [])
|
|
|
40 |
{
|
|
|
41 |
foreach ($data as $key => $val) $this->$key = $val;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
// Apply the filter to an incoming request
|
|
|
45 |
public function filter(IncomingRequest $request)
|
|
|
46 |
{
|
|
|
47 |
return $this->passOnDemand($request)
|
|
|
48 |
&& $this->passSampling()
|
|
|
49 |
&& $this->passExcept($request)
|
|
|
50 |
&& $this->passOnly($request)
|
|
|
51 |
&& $this->passExceptPreflight($request)
|
|
|
52 |
&& $this->passCallback($request);
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
protected function passOnDemand(IncomingRequest $request)
|
|
|
56 |
{
|
|
|
57 |
if (! $this->onDemand) return true;
|
|
|
58 |
|
|
|
59 |
if ($this->onDemand !== true) {
|
|
|
60 |
$input = isset($request->input['clockwork-profile']) ? $request->input['clockwork-profile'] : '';
|
|
|
61 |
$cookie = isset($request->cookies['clockwork-profile']) ? $request->cookies['clockwork-profile'] : '';
|
|
|
62 |
|
|
|
63 |
return hash_equals($this->onDemand, $input) || hash_equals($this->onDemand, $cookie);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
return isset($request->input['clockwork-profile']) || isset($request->cookies['clockwork-profile']);
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
protected function passSampling()
|
|
|
70 |
{
|
|
|
71 |
if (! $this->sample) return true;
|
|
|
72 |
|
|
|
73 |
return mt_rand(0, $this->sample) == $this->sample;
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
protected function passExcept(IncomingRequest $request)
|
|
|
77 |
{
|
|
|
78 |
if (! count($this->except)) return true;
|
|
|
79 |
|
|
|
80 |
foreach ($this->except as $pattern) {
|
|
|
81 |
if (preg_match('#' . str_replace('#', '\#', $pattern) . '#', $request->uri)) return false;
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
return true;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
protected function passOnly(IncomingRequest $request)
|
|
|
88 |
{
|
|
|
89 |
if (! count($this->only)) return true;
|
|
|
90 |
|
|
|
91 |
foreach ($this->only as $pattern) {
|
|
|
92 |
if (preg_match('#' . str_replace('#', '\#', $pattern) . '#', $request->uri)) return true;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return false;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
protected function passExceptPreflight(IncomingRequest $request)
|
|
|
99 |
{
|
|
|
100 |
if (! $this->exceptPreflight) return true;
|
|
|
101 |
|
|
|
102 |
return strtoupper($request->method) != 'OPTIONS';
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
protected function passCallback(IncomingRequest $request)
|
|
|
106 |
{
|
|
|
107 |
if (! $this->callback) return true;
|
|
|
108 |
|
|
|
109 |
return call_user_func($this->callback, $request);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public function __call($method, $parameters)
|
|
|
113 |
{
|
|
|
114 |
if (! count($parameters)) return $this->$method;
|
|
|
115 |
|
|
|
116 |
$this->$method = count($parameters) ? $parameters[0] : true;
|
|
|
117 |
|
|
|
118 |
return $this;
|
|
|
119 |
}
|
|
|
120 |
}
|