1 | <?php |
||
24 | trait RequestData |
||
25 | { |
||
26 | /** |
||
27 | * @var array Array of IP address classed as 'private' by RFC1918. |
||
28 | */ |
||
29 | protected static $rfc1918ips = array( |
||
30 | "10.0.0.0" => "10.255.255.255", |
||
31 | "172.16.0.0" => "172.31.255.255", |
||
32 | "192.168.0.0" => "192.168.255.255", |
||
33 | "169.254.0.0" => "169.254.255.255", |
||
34 | "127.0.0.0" => "127.255.255.255", |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * Gets a request object |
||
39 | * |
||
40 | * @param PdoDatabase $database The database connection |
||
41 | * @param int $requestId The ID of the request to retrieve |
||
42 | * |
||
43 | * @return Request |
||
44 | * @throws ApplicationLogicException |
||
45 | */ |
||
46 | protected function getRequest(PdoDatabase $database, $requestId) |
||
59 | |||
60 | /** |
||
61 | * Returns a value stating whether the user is allowed to see private data or not |
||
62 | * |
||
63 | * @param Request $request |
||
64 | * @param User $currentUser |
||
65 | * |
||
66 | * @return bool |
||
67 | * @category Security-Critical |
||
68 | */ |
||
69 | 6 | protected function isAllowedPrivateData(Request $request, User $currentUser) |
|
70 | { |
||
71 | // Test the main security barrier for private data access using SecurityManager |
||
72 | 6 | if ($this->barrierTest('alwaysSeePrivateData', $currentUser, 'RequestData')) { |
|
73 | // Tool admins/check-users can always see private data |
||
74 | 1 | return true; |
|
75 | } |
||
76 | |||
77 | // reserving user is allowed to see the data |
||
78 | 5 | if ($currentUser->getId() === $request->getReserved() |
|
79 | 5 | && $request->getReserved() !== null |
|
80 | 5 | && $this->barrierTest('seePrivateDataWhenReserved', $currentUser, 'RequestData') |
|
81 | 4 | ) { |
|
82 | return true; |
||
83 | } |
||
84 | |||
85 | // user has the reveal hash |
||
86 | 4 | if (WebRequest::getString('hash') === $request->getRevealHash() |
|
|
|||
87 | 4 | && $this->barrierTest('seePrivateDataWithHash', $currentUser, 'RequestData') |
|
88 | 3 | ) { |
|
89 | return true; |
||
90 | } |
||
91 | |||
92 | // nope. Not allowed. |
||
93 | 3 | return false; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Tests the security barrier for a specified action. |
||
98 | * |
||
99 | * Don't use within templates |
||
100 | * |
||
101 | * @param string $action |
||
102 | * |
||
103 | * @param User $user |
||
104 | * @param null|string $pageName |
||
105 | * |
||
106 | * @return bool |
||
107 | * @category Security-Critical |
||
108 | */ |
||
109 | abstract protected function barrierTest($action, User $user, $pageName = null); |
||
110 | |||
111 | /** |
||
112 | * Gets the name of the route that has been passed from the request router. |
||
113 | * @return string |
||
114 | */ |
||
115 | abstract protected function getRouteName(); |
||
116 | |||
117 | /** @return SecurityManager */ |
||
118 | abstract protected function getSecurityManager(); |
||
119 | |||
120 | /** |
||
121 | * Sets the name of the template this page should display. |
||
122 | * |
||
123 | * @param string $name |
||
124 | */ |
||
125 | abstract protected function setTemplate($name); |
||
126 | |||
127 | /** @return IXffTrustProvider */ |
||
128 | abstract protected function getXffTrustProvider(); |
||
129 | |||
130 | /** @return ILocationProvider */ |
||
131 | abstract protected function getLocationProvider(); |
||
132 | |||
133 | /** @return IRDnsProvider */ |
||
134 | abstract protected function getRdnsProvider(); |
||
135 | |||
136 | /** |
||
137 | * Assigns a Smarty variable |
||
138 | * |
||
139 | * @param array|string $name the template variable name(s) |
||
140 | * @param mixed $value the value to assign |
||
141 | */ |
||
142 | abstract protected function assign($name, $value); |
||
143 | |||
144 | /** |
||
145 | * @param int $requestReservationId |
||
146 | * @param PdoDatabase $database |
||
147 | * @param User $currentUser |
||
148 | */ |
||
149 | protected function setupReservationDetails($requestReservationId, PdoDatabase $database, User $currentUser) |
||
150 | { |
||
151 | $requestIsReserved = $requestReservationId !== null; |
||
152 | $this->assign('requestIsReserved', $requestIsReserved); |
||
153 | $this->assign('requestIsReservedByMe', false); |
||
154 | |||
155 | if ($requestIsReserved) { |
||
156 | $this->assign('requestReservedByName', User::getById($requestReservationId, $database)->getUsername()); |
||
157 | $this->assign('requestReservedById', $requestReservationId); |
||
158 | |||
159 | if ($requestReservationId === $currentUser->getId()) { |
||
160 | $this->assign('requestIsReservedByMe', true); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | $this->assign('canBreakReservation', $this->barrierTest('force', $currentUser, PageBreakReservation::class)); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Adds private request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
||
169 | * |
||
170 | * @param Request $request |
||
171 | * @param User $currentUser |
||
172 | * @param SiteConfiguration $configuration |
||
173 | * |
||
174 | * @param PdoDatabase $database |
||
175 | */ |
||
176 | protected function setupPrivateData( |
||
177 | $request, |
||
178 | User $currentUser, |
||
179 | SiteConfiguration $configuration, |
||
180 | PdoDatabase $database |
||
181 | ) { |
||
182 | $xffProvider = $this->getXffTrustProvider(); |
||
183 | |||
184 | $relatedEmailRequests = RequestSearchHelper::get($database) |
||
185 | ->byEmailAddress($request->getEmail()) |
||
186 | ->withConfirmedEmail() |
||
187 | ->excludingPurgedData($configuration) |
||
188 | ->excludingRequest($request->getId()) |
||
189 | ->fetch(); |
||
190 | |||
191 | $this->assign('requestEmail', $request->getEmail()); |
||
192 | $emailDomain = explode("@", $request->getEmail())[1]; |
||
193 | $this->assign("emailurl", $emailDomain); |
||
194 | $this->assign('requestRelatedEmailRequestsCount', count($relatedEmailRequests)); |
||
195 | $this->assign('requestRelatedEmailRequests', $relatedEmailRequests); |
||
196 | |||
197 | $trustedIp = $xffProvider->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
||
198 | $this->assign('requestTrustedIp', $trustedIp); |
||
199 | $this->assign('requestRealIp', $request->getIp()); |
||
200 | $this->assign('requestForwardedIp', $request->getForwardedIp()); |
||
201 | |||
202 | $trustedIpLocation = $this->getLocationProvider()->getIpLocation($trustedIp); |
||
203 | $this->assign('requestTrustedIpLocation', $trustedIpLocation); |
||
204 | |||
205 | $this->assign('requestHasForwardedIp', $request->getForwardedIp() !== null); |
||
206 | |||
207 | $relatedIpRequests = RequestSearchHelper::get($database) |
||
208 | ->byIp($trustedIp) |
||
209 | ->withConfirmedEmail() |
||
210 | ->excludingPurgedData($configuration) |
||
211 | ->excludingRequest($request->getId()) |
||
212 | ->fetch(); |
||
213 | |||
214 | $this->assign('requestRelatedIpRequestsCount', count($relatedIpRequests)); |
||
215 | $this->assign('requestRelatedIpRequests', $relatedIpRequests); |
||
216 | |||
217 | $this->assign('showRevealLink', false); |
||
218 | if ($request->getReserved() === $currentUser->getId() || |
||
219 | $this->barrierTest('alwaysSeeHash', $currentUser, 'RequestData') |
||
220 | ) { |
||
221 | $this->assign('showRevealLink', true); |
||
222 | $this->assign('revealHash', $request->getRevealHash()); |
||
223 | } |
||
224 | |||
225 | $this->setupForwardedIpData($request); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Adds checkuser request data to Smarty. DO NOT USE WITHOUT FIRST CHECKING THAT THE USER IS AUTHORISED! |
||
230 | * |
||
231 | * @param Request $request |
||
232 | */ |
||
233 | protected function setupCheckUserData(Request $request) |
||
237 | |||
238 | /** |
||
239 | * Sets up the basic data for this request, and adds it to Smarty |
||
240 | * |
||
241 | * @param Request $request |
||
242 | * @param SiteConfiguration $config |
||
243 | */ |
||
244 | protected function setupBasicData(Request $request, SiteConfiguration $config) |
||
254 | |||
255 | /** |
||
256 | * Sets up the forwarded IP data for this request and adds it to Smarty |
||
257 | * |
||
258 | * @param Request $request |
||
259 | */ |
||
260 | 6 | protected function setupForwardedIpData(Request $request) |
|
344 | } |
||
345 |