11<?php
2+
23/**
34 * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
45 * SPDX-License-Identifier: AGPL-3.0-or-later
1516use OCP \IURLGenerator ;
1617use OCP \IUser ;
1718use OCP \IUserSession ;
18- use OCP \IL10N ;
1919use Psr \Log \LoggerInterface ;
2020
2121class Checker {
22- /** @var IRequest */
23- private $ request ;
24- /** @var IUserSession */
25- private $ userSession ;
26- /** @var ISession */
27- private $ session ;
28- /** @var SignatoryMapper */
29- private $ signatoryMapper ;
30- /** @var TermsMapper */
31- private $ termsMapper ;
32- /** @var CountryDetector */
33- private $ countryDetector ;
34- /** @var IConfig */
35- private $ config ;
36- /** @var IL10N */
37- private $ l10n ;
38- /** @var LoggerInterface */
39- private $ logger ;
40- /** @var array */
41- private $ termsCache = [];
42- /** @var IURLGenerator */
43- private $ url ;
22+ private array $ termsCache = [];
4423
4524 public function __construct (
46- IRequest $ request ,
47- IUserSession $ userSession ,
48- ISession $ session ,
49- SignatoryMapper $ signatoryMapper ,
50- TermsMapper $ termsMapper ,
51- CountryDetector $ countryDetector ,
52- IConfig $ config ,
53- IL10N $ l10n ,
54- LoggerInterface $ logger ,
55- IURLGenerator $ url
25+ private IRequest $ request ,
26+ private IUserSession $ userSession ,
27+ private ISession $ session ,
28+ private SignatoryMapper $ signatoryMapper ,
29+ private TermsMapper $ termsMapper ,
30+ private CountryDetector $ countryDetector ,
31+ private IConfig $ config ,
32+ private LoggerInterface $ logger ,
33+ private IURLGenerator $ url ,
5634 ) {
57- $ this ->request = $ request ;
58- $ this ->userSession = $ userSession ;
59- $ this ->session = $ session ;
60- $ this ->signatoryMapper = $ signatoryMapper ;
61- $ this ->termsMapper = $ termsMapper ;
62- $ this ->countryDetector = $ countryDetector ;
63- $ this ->config = $ config ;
64- $ this ->l10n = $ l10n ;
65- $ this ->logger = $ logger ;
66- $ this ->url = $ url ;
6735 }
6836
6937 public function getForbiddenMessage (): string {
@@ -73,8 +41,6 @@ public function getForbiddenMessage(): string {
7341 /**
7442 * Whether the currently logged-in user has signed the terms and conditions
7543 * for the login action
76- *
77- * @return bool
7844 */
7945 public function currentUserHasSigned (): bool {
8046 $ uuid = $ this ->config ->getAppValue (Application::APPNAME , 'term_uuid ' , '' );
@@ -112,13 +78,11 @@ public function currentUserHasSigned(): bool {
11278 }
11379
11480 $ signatories = $ this ->signatoryMapper ->getSignatoriesByUser ($ user );
115- if (!empty ($ signatories )) {
116- foreach ($ signatories as $ signatory ) {
117- foreach ($ this ->termsCache [$ countryCode ] as $ term ) {
118- if ((int )$ term ->getId () === (int )$ signatory ->getTermsId ()) {
119- $ this ->session ->set ('term_uuid ' , $ uuid );
120- return true ;
121- }
81+ foreach ($ signatories as $ signatory ) {
82+ foreach ($ this ->termsCache [$ countryCode ] as $ term ) {
83+ if ((int )$ term ->getId () === (int )$ signatory ->getTermsId ()) {
84+ $ this ->session ->set ('term_uuid ' , $ uuid );
85+ return true ;
12286 }
12387 }
12488 }
@@ -153,11 +117,11 @@ protected function isPathInfoStartingWith(string $allowedPath): bool {
153117 if ($ allowedPath === '' ) {
154118 return false ;
155119 }
156- return strpos ($ this ->request ->getPathInfo (), $ allowedPath ) === 0 ;
120+ return str_starts_with ($ this ->request ->getPathInfo (), $ allowedPath );
157121 }
158122
159123 protected function isAllowedScriptName (): bool {
160- return substr ($ this ->request ->getScriptName (), 0 - strlen ( '/index.php ' )) === ' /index.php ' ;
124+ return str_ends_with ($ this ->request ->getScriptName (), '/index.php ' );
161125 }
162126
163127 protected function isRemoteAddressInRanges (array $ allowedRanges ): bool {
@@ -192,7 +156,7 @@ private function allowedRangeForApp(string $appId, string $configKey): array {
192156 * @copyright (IPv6) MW. https://stackoverflow.com/questions/7951061/matching-ipv6-address-to-a-cidr-subnet via
193157 */
194158 private function matchCidr (string $ ip , string $ range ): bool {
195- list ( $ subnet , $ bits) = array_pad (explode ('/ ' , $ range ), 2 , null );
159+ [ $ subnet , $ bits] = array_pad (explode ('/ ' , $ range ), 2 , null );
196160 if ($ bits === null ) {
197161 $ bits = 32 ;
198162 }
@@ -211,23 +175,23 @@ private function matchCidr(string $ip, string $range): bool {
211175 $ subnet = inet_pton ($ subnet );
212176 $ ip = inet_pton ($ ip );
213177
214- $ binMask = str_repeat (" f " , (int )($ bits / 4 ));
178+ $ binMask = str_repeat (' f ' , (int )($ bits / 4 ));
215179 switch ($ bits % 4 ) {
216180 case 0 :
217181 break ;
218182 case 1 :
219- $ binMask .= " 8 " ;
183+ $ binMask .= ' 8 ' ;
220184 break ;
221185 case 2 :
222- $ binMask .= " c " ;
186+ $ binMask .= ' c ' ;
223187 break ;
224188 case 3 :
225- $ binMask .= " e " ;
189+ $ binMask .= ' e ' ;
226190 break ;
227191 }
228192
229193 $ binMask = str_pad ($ binMask , 32 , '0 ' );
230- $ binMask = pack (" H* " , $ binMask );
194+ $ binMask = pack (' H* ' , $ binMask );
231195
232196 if (($ ip & $ binMask ) === $ subnet ) {
233197 return true ;
@@ -236,11 +200,11 @@ private function matchCidr(string $ip, string $range): bool {
236200 return false ;
237201 }
238202
239- private function isIpv4 ($ ip ) {
203+ private function isIpv4 ($ ip ): mixed {
240204 return filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV4 );
241205 }
242206
243- private function isIpv6 ($ ip ) {
207+ private function isIpv6 ($ ip ): mixed {
244208 return filter_var ($ ip , FILTER_VALIDATE_IP , FILTER_FLAG_IPV6 );
245209 }
246210}
0 commit comments