Completed
Push — master ( 034246...d4e9a8 )
by
unknown
19:42 queued 13s
created
apps/files_trashbin/composer/composer/autoload_real.php 1 patch
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -9,7 +9,7 @@  discard block
 block discarded – undo
9 9
     public static function loadClassLoader($class)
10 10
     {
11 11
         if ('Composer\Autoload\ClassLoader' === $class) {
12
-            require __DIR__ . '/ClassLoader.php';
12
+            require __DIR__.'/ClassLoader.php';
13 13
         }
14 14
     }
15 15
 
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
         self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
27 27
         spl_autoload_unregister(array('ComposerAutoloaderInitFiles_Trashbin', 'loadClassLoader'));
28 28
 
29
-        require __DIR__ . '/autoload_static.php';
29
+        require __DIR__.'/autoload_static.php';
30 30
         call_user_func(\Composer\Autoload\ComposerStaticInitFiles_Trashbin::getInitializer($loader));
31 31
 
32 32
         $loader->setClassMapAuthoritative(true);
Please login to merge, or discard this patch.
apps/theming/lib/Themes/DyslexiaFont.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -53,7 +53,7 @@
 block discarded – undo
53 53
 		$originalFontFace = $variables['--font-face'];
54 54
 
55 55
 		$variables = [
56
-			'--font-face' => 'OpenDyslexic, ' . $originalFontFace
56
+			'--font-face' => 'OpenDyslexic, '.$originalFontFace
57 57
 		];
58 58
 
59 59
 		return $variables;
Please login to merge, or discard this patch.
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -11,42 +11,42 @@  discard block
 block discarded – undo
11 11
 
12 12
 class DyslexiaFont extends DefaultTheme implements ITheme {
13 13
 
14
-	public function getId(): string {
15
-		return 'opendyslexic';
16
-	}
14
+    public function getId(): string {
15
+        return 'opendyslexic';
16
+    }
17 17
 
18
-	public function getType(): int {
19
-		return ITheme::TYPE_FONT;
20
-	}
18
+    public function getType(): int {
19
+        return ITheme::TYPE_FONT;
20
+    }
21 21
 
22
-	public function getTitle(): string {
23
-		return $this->l->t('Dyslexia font');
24
-	}
22
+    public function getTitle(): string {
23
+        return $this->l->t('Dyslexia font');
24
+    }
25 25
 
26
-	public function getEnableLabel(): string {
27
-		return $this->l->t('Enable dyslexia font');
28
-	}
26
+    public function getEnableLabel(): string {
27
+        return $this->l->t('Enable dyslexia font');
28
+    }
29 29
 
30
-	public function getDescription(): string {
31
-		return $this->l->t('OpenDyslexic is a free typeface/font designed to mitigate some of the common reading errors caused by dyslexia.');
32
-	}
30
+    public function getDescription(): string {
31
+        return $this->l->t('OpenDyslexic is a free typeface/font designed to mitigate some of the common reading errors caused by dyslexia.');
32
+    }
33 33
 
34
-	public function getCSSVariables(): array {
35
-		$variables = parent::getCSSVariables();
36
-		$originalFontFace = $variables['--font-face'];
34
+    public function getCSSVariables(): array {
35
+        $variables = parent::getCSSVariables();
36
+        $originalFontFace = $variables['--font-face'];
37 37
 
38
-		$variables = [
39
-			'--font-face' => 'OpenDyslexic, ' . $originalFontFace
40
-		];
38
+        $variables = [
39
+            '--font-face' => 'OpenDyslexic, ' . $originalFontFace
40
+        ];
41 41
 
42
-		return $variables;
43
-	}
42
+        return $variables;
43
+    }
44 44
 
45
-	public function getCustomCss(): string {
46
-		$fontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Regular.otf');
47
-		$boldFontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Bold.otf');
45
+    public function getCustomCss(): string {
46
+        $fontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Regular.otf');
47
+        $boldFontPathOtf = $this->urlGenerator->linkTo('theming', 'fonts/OpenDyslexic-Bold.otf');
48 48
 
49
-		return "
49
+        return "
50 50
 			@font-face {
51 51
 				font-family: 'OpenDyslexic';
52 52
 				font-style: normal;
@@ -61,5 +61,5 @@  discard block
 block discarded – undo
61 61
 				src: url('$boldFontPathOtf') format('opentype');
62 62
 			}
63 63
 		";
64
-	}
64
+    }
65 65
 }
Please login to merge, or discard this patch.
core/Migrations/Version24000Date20220425072957.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -32,22 +32,22 @@
 block discarded – undo
32 32
 use OCP\Migration\SimpleMigrationStep;
33 33
 
34 34
 class Version24000Date20220425072957 extends SimpleMigrationStep {
35
-	/**
36
-	 * @param IOutput $output
37
-	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
38
-	 * @param array $options
39
-	 * @return null|ISchemaWrapper
40
-	 */
41
-	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
42
-		/** @var ISchemaWrapper $schema */
43
-		$schema = $schemaClosure();
35
+    /**
36
+     * @param IOutput $output
37
+     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
38
+     * @param array $options
39
+     * @return null|ISchemaWrapper
40
+     */
41
+    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
42
+        /** @var ISchemaWrapper $schema */
43
+        $schema = $schemaClosure();
44 44
 
45
-		$table = $schema->getTable('mounts');
46
-		if (!$table->hasIndex('mount_user_storage')) {
47
-			$table->addIndex(['storage_id', 'user_id'], 'mount_user_storage');
48
-			return $schema;
49
-		}
45
+        $table = $schema->getTable('mounts');
46
+        if (!$table->hasIndex('mount_user_storage')) {
47
+            $table->addIndex(['storage_id', 'user_id'], 'mount_user_storage');
48
+            return $schema;
49
+        }
50 50
 
51
-		return null;
52
-	}
51
+        return null;
52
+    }
53 53
 }
Please login to merge, or discard this patch.
lib/public/IEmojiHelper.php 1 patch
Indentation   +8 added lines, -8 removed lines patch added patch discarded remove patch
@@ -27,13 +27,13 @@
 block discarded – undo
27 27
  * @since 24.0.0
28 28
  */
29 29
 interface IEmojiHelper {
30
-	/**
31
-	 * @since 24.0.0
32
-	 */
33
-	public function doesPlatformSupportEmoji(): bool;
30
+    /**
31
+     * @since 24.0.0
32
+     */
33
+    public function doesPlatformSupportEmoji(): bool;
34 34
 
35
-	/**
36
-	 * @since 24.0.0
37
-	 */
38
-	public function isValidSingleEmoji(string $emoji): bool;
35
+    /**
36
+     * @since 24.0.0
37
+     */
38
+    public function isValidSingleEmoji(string $emoji): bool;
39 39
 }
Please login to merge, or discard this patch.
lib/private/DateTimeZone.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -65,7 +65,7 @@  discard block
 block discarded – undo
65 65
 		try {
66 66
 			return new \DateTimeZone($timeZone);
67 67
 		} catch (\Exception $e) {
68
-			\OC::$server->get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timeZone . '"', ['app' => 'datetimezone']);
68
+			\OC::$server->get(LoggerInterface::class)->debug('Failed to created DateTimeZone "'.$timeZone.'"', ['app' => 'datetimezone']);
69 69
 			return new \DateTimeZone($this->getDefaultTimeZone());
70 70
 		}
71 71
 	}
@@ -86,9 +86,9 @@  discard block
 block discarded – undo
86 86
 			// so a positive offset means negative timeZone
87 87
 			// and the other way around.
88 88
 			if ($offset > 0) {
89
-				$timeZone = 'Etc/GMT-' . $offset;
89
+				$timeZone = 'Etc/GMT-'.$offset;
90 90
 			} else {
91
-				$timeZone = 'Etc/GMT+' . abs($offset);
91
+				$timeZone = 'Etc/GMT+'.abs($offset);
92 92
 			}
93 93
 
94 94
 			return new \DateTimeZone($timeZone);
@@ -110,7 +110,7 @@  discard block
 block discarded – undo
110 110
 			}
111 111
 
112 112
 			// No timezone found, fallback to UTC
113
-			\OC::$server->get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']);
113
+			\OC::$server->get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "'.$offset.'"', ['app' => 'datetimezone']);
114 114
 			return new \DateTimeZone($this->getDefaultTimeZone());
115 115
 		}
116 116
 	}
Please login to merge, or discard this patch.
Indentation   +86 added lines, -86 removed lines patch added patch discarded remove patch
@@ -13,100 +13,100 @@
 block discarded – undo
13 13
 use Psr\Log\LoggerInterface;
14 14
 
15 15
 class DateTimeZone implements IDateTimeZone {
16
-	/** @var IConfig */
17
-	protected $config;
16
+    /** @var IConfig */
17
+    protected $config;
18 18
 
19
-	/** @var ISession */
20
-	protected $session;
19
+    /** @var ISession */
20
+    protected $session;
21 21
 
22
-	/**
23
-	 * Constructor
24
-	 *
25
-	 * @param IConfig $config
26
-	 * @param ISession $session
27
-	 */
28
-	public function __construct(IConfig $config, ISession $session) {
29
-		$this->config = $config;
30
-		$this->session = $session;
31
-	}
22
+    /**
23
+     * Constructor
24
+     *
25
+     * @param IConfig $config
26
+     * @param ISession $session
27
+     */
28
+    public function __construct(IConfig $config, ISession $session) {
29
+        $this->config = $config;
30
+        $this->session = $session;
31
+    }
32 32
 
33
-	/**
34
-	 * Get the timezone of the current user, based on their session information and config data
35
-	 *
36
-	 * @param bool|int $timestamp
37
-	 * @return \DateTimeZone
38
-	 */
39
-	public function getTimeZone($timestamp = false) {
40
-		$timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null);
41
-		if ($timeZone === null) {
42
-			if ($this->session->exists('timezone')) {
43
-				return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp);
44
-			}
45
-			$timeZone = $this->getDefaultTimeZone();
46
-		}
33
+    /**
34
+     * Get the timezone of the current user, based on their session information and config data
35
+     *
36
+     * @param bool|int $timestamp
37
+     * @return \DateTimeZone
38
+     */
39
+    public function getTimeZone($timestamp = false) {
40
+        $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null);
41
+        if ($timeZone === null) {
42
+            if ($this->session->exists('timezone')) {
43
+                return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp);
44
+            }
45
+            $timeZone = $this->getDefaultTimeZone();
46
+        }
47 47
 
48
-		try {
49
-			return new \DateTimeZone($timeZone);
50
-		} catch (\Exception $e) {
51
-			\OC::$server->get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timeZone . '"', ['app' => 'datetimezone']);
52
-			return new \DateTimeZone($this->getDefaultTimeZone());
53
-		}
54
-	}
48
+        try {
49
+            return new \DateTimeZone($timeZone);
50
+        } catch (\Exception $e) {
51
+            \OC::$server->get(LoggerInterface::class)->debug('Failed to created DateTimeZone "' . $timeZone . '"', ['app' => 'datetimezone']);
52
+            return new \DateTimeZone($this->getDefaultTimeZone());
53
+        }
54
+    }
55 55
 
56
-	/**
57
-	 * Guess the DateTimeZone for a given offset
58
-	 *
59
-	 * We first try to find a Etc/GMT* timezone, if that does not exist,
60
-	 * we try to find it manually, before falling back to UTC.
61
-	 *
62
-	 * @param mixed $offset
63
-	 * @param bool|int $timestamp
64
-	 * @return \DateTimeZone
65
-	 */
66
-	protected function guessTimeZoneFromOffset($offset, $timestamp) {
67
-		try {
68
-			// Note: the timeZone name is the inverse to the offset,
69
-			// so a positive offset means negative timeZone
70
-			// and the other way around.
71
-			if ($offset > 0) {
72
-				$timeZone = 'Etc/GMT-' . $offset;
73
-			} else {
74
-				$timeZone = 'Etc/GMT+' . abs($offset);
75
-			}
56
+    /**
57
+     * Guess the DateTimeZone for a given offset
58
+     *
59
+     * We first try to find a Etc/GMT* timezone, if that does not exist,
60
+     * we try to find it manually, before falling back to UTC.
61
+     *
62
+     * @param mixed $offset
63
+     * @param bool|int $timestamp
64
+     * @return \DateTimeZone
65
+     */
66
+    protected function guessTimeZoneFromOffset($offset, $timestamp) {
67
+        try {
68
+            // Note: the timeZone name is the inverse to the offset,
69
+            // so a positive offset means negative timeZone
70
+            // and the other way around.
71
+            if ($offset > 0) {
72
+                $timeZone = 'Etc/GMT-' . $offset;
73
+            } else {
74
+                $timeZone = 'Etc/GMT+' . abs($offset);
75
+            }
76 76
 
77
-			return new \DateTimeZone($timeZone);
78
-		} catch (\Exception $e) {
79
-			// If the offset has no Etc/GMT* timezone,
80
-			// we try to guess one timezone that has the same offset
81
-			foreach (\DateTimeZone::listIdentifiers() as $timeZone) {
82
-				$dtz = new \DateTimeZone($timeZone);
83
-				$dateTime = new \DateTime();
77
+            return new \DateTimeZone($timeZone);
78
+        } catch (\Exception $e) {
79
+            // If the offset has no Etc/GMT* timezone,
80
+            // we try to guess one timezone that has the same offset
81
+            foreach (\DateTimeZone::listIdentifiers() as $timeZone) {
82
+                $dtz = new \DateTimeZone($timeZone);
83
+                $dateTime = new \DateTime();
84 84
 
85
-				if ($timestamp !== false) {
86
-					$dateTime->setTimestamp($timestamp);
87
-				}
85
+                if ($timestamp !== false) {
86
+                    $dateTime->setTimestamp($timestamp);
87
+                }
88 88
 
89
-				$dtOffset = $dtz->getOffset($dateTime);
90
-				if ($dtOffset == 3600 * $offset) {
91
-					return $dtz;
92
-				}
93
-			}
89
+                $dtOffset = $dtz->getOffset($dateTime);
90
+                if ($dtOffset == 3600 * $offset) {
91
+                    return $dtz;
92
+                }
93
+            }
94 94
 
95
-			// No timezone found, fallback to UTC
96
-			\OC::$server->get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']);
97
-			return new \DateTimeZone($this->getDefaultTimeZone());
98
-		}
99
-	}
95
+            // No timezone found, fallback to UTC
96
+            \OC::$server->get(LoggerInterface::class)->debug('Failed to find DateTimeZone for offset "' . $offset . '"', ['app' => 'datetimezone']);
97
+            return new \DateTimeZone($this->getDefaultTimeZone());
98
+        }
99
+    }
100 100
 
101
-	/**
102
-	 * Get the default timezone of the server
103
-	 *
104
-	 * Falls back to UTC if it is not yet set.
105
-	 *
106
-	 * @return string
107
-	 */
108
-	protected function getDefaultTimeZone() {
109
-		$serverTimeZone = date_default_timezone_get();
110
-		return $serverTimeZone ?: 'UTC';
111
-	}
101
+    /**
102
+     * Get the default timezone of the server
103
+     *
104
+     * Falls back to UTC if it is not yet set.
105
+     *
106
+     * @return string
107
+     */
108
+    protected function getDefaultTimeZone() {
109
+        $serverTimeZone = date_default_timezone_get();
110
+        return $serverTimeZone ?: 'UTC';
111
+    }
112 112
 }
Please login to merge, or discard this patch.
lib/public/Lock/ILockingProvider.php 1 patch
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -36,47 +36,47 @@
 block discarded – undo
36 36
  * @since 8.1.0
37 37
  */
38 38
 interface ILockingProvider {
39
-	/**
40
-	 * @since 8.1.0
41
-	 */
42
-	public const LOCK_SHARED = 1;
43
-	/**
44
-	 * @since 8.1.0
45
-	 */
46
-	public const LOCK_EXCLUSIVE = 2;
39
+    /**
40
+     * @since 8.1.0
41
+     */
42
+    public const LOCK_SHARED = 1;
43
+    /**
44
+     * @since 8.1.0
45
+     */
46
+    public const LOCK_EXCLUSIVE = 2;
47 47
 
48
-	/**
49
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
50
-	 * @since 8.1.0
51
-	 */
52
-	public function isLocked(string $path, int $type): bool;
48
+    /**
49
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
50
+     * @since 8.1.0
51
+     */
52
+    public function isLocked(string $path, int $type): bool;
53 53
 
54
-	/**
55
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
56
-	 * @param ?string $readablePath A human-readable path to use in error messages, since 20.0.0
57
-	 * @throws LockedException
58
-	 * @since 8.1.0
59
-	 */
60
-	public function acquireLock(string $path, int $type, ?string $readablePath = null): void;
54
+    /**
55
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
56
+     * @param ?string $readablePath A human-readable path to use in error messages, since 20.0.0
57
+     * @throws LockedException
58
+     * @since 8.1.0
59
+     */
60
+    public function acquireLock(string $path, int $type, ?string $readablePath = null): void;
61 61
 
62
-	/**
63
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
64
-	 * @since 8.1.0
65
-	 */
66
-	public function releaseLock(string $path, int $type): void;
62
+    /**
63
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $type
64
+     * @since 8.1.0
65
+     */
66
+    public function releaseLock(string $path, int $type): void;
67 67
 
68
-	/**
69
-	 * Change the target type of an existing lock
70
-	 *
71
-	 * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $targetType
72
-	 * @throws LockedException
73
-	 * @since 8.1.0
74
-	 */
75
-	public function changeLock(string $path, int $targetType): void;
68
+    /**
69
+     * Change the target type of an existing lock
70
+     *
71
+     * @psalm-param self::LOCK_SHARED|self::LOCK_EXCLUSIVE $targetType
72
+     * @throws LockedException
73
+     * @since 8.1.0
74
+     */
75
+    public function changeLock(string $path, int $targetType): void;
76 76
 
77
-	/**
78
-	 * Release all lock acquired by this instance
79
-	 * @since 8.1.0
80
-	 */
81
-	public function releaseAll(): void;
77
+    /**
78
+     * Release all lock acquired by this instance
79
+     * @since 8.1.0
80
+     */
81
+    public function releaseAll(): void;
82 82
 }
Please login to merge, or discard this patch.
lib/private/Lock/DBLockingProvider.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
 			->from('file_locks')
114 114
 			->where($query->expr()->eq('key', $query->createNamedParameter($path)));
115 115
 		$result = $query->executeQuery();
116
-		$lockValue = (int)$result->fetchOne();
116
+		$lockValue = (int) $result->fetchOne();
117 117
 		if ($type === self::LOCK_SHARED) {
118 118
 			if ($this->isLocallyLocked($path)) {
119 119
 				// if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
 		}
248 248
 		// since we keep shared locks we need to manually clean those
249 249
 		$lockedPaths = array_keys($this->sharedLocks);
250
-		$lockedPaths = array_filter($lockedPaths, function ($path) {
250
+		$lockedPaths = array_filter($lockedPaths, function($path) {
251 251
 			return $this->sharedLocks[$path];
252 252
 		});
253 253
 
Please login to merge, or discard this patch.
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -18,223 +18,223 @@
 block discarded – undo
18 18
  * Locking provider that stores the locks in the database
19 19
  */
20 20
 class DBLockingProvider extends AbstractLockingProvider {
21
-	private array $sharedLocks = [];
22
-
23
-	public function __construct(
24
-		private IDBConnection $connection,
25
-		private ITimeFactory $timeFactory,
26
-		int $ttl = 3600,
27
-		private bool $cacheSharedLocks = true,
28
-	) {
29
-		parent::__construct($ttl);
30
-	}
31
-
32
-	/**
33
-	 * Check if we have an open shared lock for a path
34
-	 */
35
-	protected function isLocallyLocked(string $path): bool {
36
-		return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path];
37
-	}
38
-
39
-	/** @inheritDoc */
40
-	protected function markAcquire(string $path, int $targetType): void {
41
-		parent::markAcquire($path, $targetType);
42
-		if ($this->cacheSharedLocks) {
43
-			if ($targetType === self::LOCK_SHARED) {
44
-				$this->sharedLocks[$path] = true;
45
-			}
46
-		}
47
-	}
48
-
49
-	/**
50
-	 * Change the type of an existing tracked lock
51
-	 */
52
-	protected function markChange(string $path, int $targetType): void {
53
-		parent::markChange($path, $targetType);
54
-		if ($this->cacheSharedLocks) {
55
-			if ($targetType === self::LOCK_SHARED) {
56
-				$this->sharedLocks[$path] = true;
57
-			} elseif ($targetType === self::LOCK_EXCLUSIVE) {
58
-				$this->sharedLocks[$path] = false;
59
-			}
60
-		}
61
-	}
62
-
63
-	/**
64
-	 * Insert a file locking row if it does not exists.
65
-	 */
66
-	protected function initLockField(string $path, int $lock = 0): int {
67
-		$expire = $this->getExpireTime();
68
-		return $this->connection->insertIgnoreConflict('file_locks', [
69
-			'key' => $path,
70
-			'lock' => $lock,
71
-			'ttl' => $expire
72
-		]);
73
-	}
74
-
75
-	protected function getExpireTime(): int {
76
-		return $this->timeFactory->getTime() + $this->ttl;
77
-	}
78
-
79
-	/** @inheritDoc */
80
-	public function isLocked(string $path, int $type): bool {
81
-		if ($this->hasAcquiredLock($path, $type)) {
82
-			return true;
83
-		}
84
-		$query = $this->connection->getQueryBuilder();
85
-		$query->select('lock')
86
-			->from('file_locks')
87
-			->where($query->expr()->eq('key', $query->createNamedParameter($path)));
88
-		$result = $query->executeQuery();
89
-		$lockValue = (int)$result->fetchOne();
90
-		if ($type === self::LOCK_SHARED) {
91
-			if ($this->isLocallyLocked($path)) {
92
-				// if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db
93
-				return $lockValue > 1;
94
-			} else {
95
-				return $lockValue > 0;
96
-			}
97
-		} elseif ($type === self::LOCK_EXCLUSIVE) {
98
-			return $lockValue === -1;
99
-		} else {
100
-			return false;
101
-		}
102
-	}
103
-
104
-	/** @inheritDoc */
105
-	public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
106
-		$expire = $this->getExpireTime();
107
-		if ($type === self::LOCK_SHARED) {
108
-			if (!$this->isLocallyLocked($path)) {
109
-				$result = $this->initLockField($path, 1);
110
-				if ($result <= 0) {
111
-					$query = $this->connection->getQueryBuilder();
112
-					$query->update('file_locks')
113
-						->set('lock', $query->func()->add('lock', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
114
-						->set('ttl', $query->createNamedParameter($expire))
115
-						->where($query->expr()->eq('key', $query->createNamedParameter($path)))
116
-						->andWhere($query->expr()->gte('lock', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
117
-					$result = $query->executeStatement();
118
-				}
119
-			} else {
120
-				$result = 1;
121
-			}
122
-		} else {
123
-			$existing = 0;
124
-			if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) {
125
-				$existing = 1;
126
-			}
127
-			$result = $this->initLockField($path, -1);
128
-			if ($result <= 0) {
129
-				$query = $this->connection->getQueryBuilder();
130
-				$query->update('file_locks')
131
-					->set('lock', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
132
-					->set('ttl', $query->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
133
-					->where($query->expr()->eq('key', $query->createNamedParameter($path)))
134
-					->andWhere($query->expr()->eq('lock', $query->createNamedParameter($existing)));
135
-				$result = $query->executeStatement();
136
-			}
137
-		}
138
-		if ($result !== 1) {
139
-			throw new LockedException($path, null, null, $readablePath);
140
-		}
141
-		$this->markAcquire($path, $type);
142
-	}
143
-
144
-	/** @inheritDoc */
145
-	public function releaseLock(string $path, int $type): void {
146
-		$this->markRelease($path, $type);
147
-
148
-		// we keep shared locks till the end of the request so we can re-use them
149
-		if ($type === self::LOCK_EXCLUSIVE) {
150
-			$qb = $this->connection->getQueryBuilder();
151
-			$qb->update('file_locks')
152
-				->set('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
153
-				->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
154
-				->andWhere($qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)));
155
-			$qb->executeStatement();
156
-		} elseif (!$this->cacheSharedLocks) {
157
-			$qb = $this->connection->getQueryBuilder();
158
-			$qb->update('file_locks')
159
-				->set('lock', $qb->func()->subtract('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
160
-				->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
161
-				->andWhere($qb->expr()->gt('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
162
-			$qb->executeStatement();
163
-		}
164
-	}
165
-
166
-	/** @inheritDoc */
167
-	public function changeLock(string $path, int $targetType): void {
168
-		$expire = $this->getExpireTime();
169
-		if ($targetType === self::LOCK_SHARED) {
170
-			$qb = $this->connection->getQueryBuilder();
171
-			$result = $qb->update('file_locks')
172
-				->set('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
173
-				->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
174
-				->where($qb->expr()->andX(
175
-					$qb->expr()->eq('key', $qb->createNamedParameter($path)),
176
-					$qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
177
-				))->executeStatement();
178
-		} else {
179
-			// since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually
180
-			if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) {
181
-				throw new LockedException($path);
182
-			}
183
-			$qb = $this->connection->getQueryBuilder();
184
-			$result = $qb->update('file_locks')
185
-				->set('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
186
-				->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
187
-				->where($qb->expr()->andX(
188
-					$qb->expr()->eq('key', $qb->createNamedParameter($path)),
189
-					$qb->expr()->eq('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
190
-				))->executeStatement();
191
-		}
192
-		if ($result !== 1) {
193
-			throw new LockedException($path);
194
-		}
195
-		$this->markChange($path, $targetType);
196
-	}
197
-
198
-	/** @inheritDoc */
199
-	public function cleanExpiredLocks(): void {
200
-		$expire = $this->timeFactory->getTime();
201
-		try {
202
-			$qb = $this->connection->getQueryBuilder();
203
-			$qb->delete('file_locks')
204
-				->where($qb->expr()->lt('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)))
205
-				->executeStatement();
206
-		} catch (\Exception $e) {
207
-			// If the table is missing, the clean up was successful
208
-			if ($this->connection->tableExists('file_locks')) {
209
-				throw $e;
210
-			}
211
-		}
212
-	}
213
-
214
-	/** @inheritDoc */
215
-	public function releaseAll(): void {
216
-		parent::releaseAll();
217
-
218
-		if (!$this->cacheSharedLocks) {
219
-			return;
220
-		}
221
-		// since we keep shared locks we need to manually clean those
222
-		$lockedPaths = array_keys($this->sharedLocks);
223
-		$lockedPaths = array_filter($lockedPaths, function ($path) {
224
-			return $this->sharedLocks[$path];
225
-		});
226
-
227
-		$chunkedPaths = array_chunk($lockedPaths, 100);
228
-
229
-		$qb = $this->connection->getQueryBuilder();
230
-		$qb->update('file_locks')
231
-			->set('lock', $qb->func()->subtract('lock', $qb->expr()->literal(1)))
232
-			->where($qb->expr()->in('key', $qb->createParameter('chunk')))
233
-			->andWhere($qb->expr()->gt('lock', new Literal(0)));
234
-
235
-		foreach ($chunkedPaths as $chunk) {
236
-			$qb->setParameter('chunk', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
237
-			$qb->executeStatement();
238
-		}
239
-	}
21
+    private array $sharedLocks = [];
22
+
23
+    public function __construct(
24
+        private IDBConnection $connection,
25
+        private ITimeFactory $timeFactory,
26
+        int $ttl = 3600,
27
+        private bool $cacheSharedLocks = true,
28
+    ) {
29
+        parent::__construct($ttl);
30
+    }
31
+
32
+    /**
33
+     * Check if we have an open shared lock for a path
34
+     */
35
+    protected function isLocallyLocked(string $path): bool {
36
+        return isset($this->sharedLocks[$path]) && $this->sharedLocks[$path];
37
+    }
38
+
39
+    /** @inheritDoc */
40
+    protected function markAcquire(string $path, int $targetType): void {
41
+        parent::markAcquire($path, $targetType);
42
+        if ($this->cacheSharedLocks) {
43
+            if ($targetType === self::LOCK_SHARED) {
44
+                $this->sharedLocks[$path] = true;
45
+            }
46
+        }
47
+    }
48
+
49
+    /**
50
+     * Change the type of an existing tracked lock
51
+     */
52
+    protected function markChange(string $path, int $targetType): void {
53
+        parent::markChange($path, $targetType);
54
+        if ($this->cacheSharedLocks) {
55
+            if ($targetType === self::LOCK_SHARED) {
56
+                $this->sharedLocks[$path] = true;
57
+            } elseif ($targetType === self::LOCK_EXCLUSIVE) {
58
+                $this->sharedLocks[$path] = false;
59
+            }
60
+        }
61
+    }
62
+
63
+    /**
64
+     * Insert a file locking row if it does not exists.
65
+     */
66
+    protected function initLockField(string $path, int $lock = 0): int {
67
+        $expire = $this->getExpireTime();
68
+        return $this->connection->insertIgnoreConflict('file_locks', [
69
+            'key' => $path,
70
+            'lock' => $lock,
71
+            'ttl' => $expire
72
+        ]);
73
+    }
74
+
75
+    protected function getExpireTime(): int {
76
+        return $this->timeFactory->getTime() + $this->ttl;
77
+    }
78
+
79
+    /** @inheritDoc */
80
+    public function isLocked(string $path, int $type): bool {
81
+        if ($this->hasAcquiredLock($path, $type)) {
82
+            return true;
83
+        }
84
+        $query = $this->connection->getQueryBuilder();
85
+        $query->select('lock')
86
+            ->from('file_locks')
87
+            ->where($query->expr()->eq('key', $query->createNamedParameter($path)));
88
+        $result = $query->executeQuery();
89
+        $lockValue = (int)$result->fetchOne();
90
+        if ($type === self::LOCK_SHARED) {
91
+            if ($this->isLocallyLocked($path)) {
92
+                // if we have a shared lock we kept open locally but it's released we always have at least 1 shared lock in the db
93
+                return $lockValue > 1;
94
+            } else {
95
+                return $lockValue > 0;
96
+            }
97
+        } elseif ($type === self::LOCK_EXCLUSIVE) {
98
+            return $lockValue === -1;
99
+        } else {
100
+            return false;
101
+        }
102
+    }
103
+
104
+    /** @inheritDoc */
105
+    public function acquireLock(string $path, int $type, ?string $readablePath = null): void {
106
+        $expire = $this->getExpireTime();
107
+        if ($type === self::LOCK_SHARED) {
108
+            if (!$this->isLocallyLocked($path)) {
109
+                $result = $this->initLockField($path, 1);
110
+                if ($result <= 0) {
111
+                    $query = $this->connection->getQueryBuilder();
112
+                    $query->update('file_locks')
113
+                        ->set('lock', $query->func()->add('lock', $query->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
114
+                        ->set('ttl', $query->createNamedParameter($expire))
115
+                        ->where($query->expr()->eq('key', $query->createNamedParameter($path)))
116
+                        ->andWhere($query->expr()->gte('lock', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
117
+                    $result = $query->executeStatement();
118
+                }
119
+            } else {
120
+                $result = 1;
121
+            }
122
+        } else {
123
+            $existing = 0;
124
+            if ($this->hasAcquiredLock($path, ILockingProvider::LOCK_SHARED) === false && $this->isLocallyLocked($path)) {
125
+                $existing = 1;
126
+            }
127
+            $result = $this->initLockField($path, -1);
128
+            if ($result <= 0) {
129
+                $query = $this->connection->getQueryBuilder();
130
+                $query->update('file_locks')
131
+                    ->set('lock', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
132
+                    ->set('ttl', $query->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
133
+                    ->where($query->expr()->eq('key', $query->createNamedParameter($path)))
134
+                    ->andWhere($query->expr()->eq('lock', $query->createNamedParameter($existing)));
135
+                $result = $query->executeStatement();
136
+            }
137
+        }
138
+        if ($result !== 1) {
139
+            throw new LockedException($path, null, null, $readablePath);
140
+        }
141
+        $this->markAcquire($path, $type);
142
+    }
143
+
144
+    /** @inheritDoc */
145
+    public function releaseLock(string $path, int $type): void {
146
+        $this->markRelease($path, $type);
147
+
148
+        // we keep shared locks till the end of the request so we can re-use them
149
+        if ($type === self::LOCK_EXCLUSIVE) {
150
+            $qb = $this->connection->getQueryBuilder();
151
+            $qb->update('file_locks')
152
+                ->set('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT))
153
+                ->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
154
+                ->andWhere($qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT)));
155
+            $qb->executeStatement();
156
+        } elseif (!$this->cacheSharedLocks) {
157
+            $qb = $this->connection->getQueryBuilder();
158
+            $qb->update('file_locks')
159
+                ->set('lock', $qb->func()->subtract('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
160
+                ->where($qb->expr()->eq('key', $qb->createNamedParameter($path)))
161
+                ->andWhere($qb->expr()->gt('lock', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
162
+            $qb->executeStatement();
163
+        }
164
+    }
165
+
166
+    /** @inheritDoc */
167
+    public function changeLock(string $path, int $targetType): void {
168
+        $expire = $this->getExpireTime();
169
+        if ($targetType === self::LOCK_SHARED) {
170
+            $qb = $this->connection->getQueryBuilder();
171
+            $result = $qb->update('file_locks')
172
+                ->set('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
173
+                ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
174
+                ->where($qb->expr()->andX(
175
+                    $qb->expr()->eq('key', $qb->createNamedParameter($path)),
176
+                    $qb->expr()->eq('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
177
+                ))->executeStatement();
178
+        } else {
179
+            // since we only keep one shared lock in the db we need to check if we have more then one shared lock locally manually
180
+            if (isset($this->acquiredLocks['shared'][$path]) && $this->acquiredLocks['shared'][$path] > 1) {
181
+                throw new LockedException($path);
182
+            }
183
+            $qb = $this->connection->getQueryBuilder();
184
+            $result = $qb->update('file_locks')
185
+                ->set('lock', $qb->createNamedParameter(-1, IQueryBuilder::PARAM_INT))
186
+                ->set('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT))
187
+                ->where($qb->expr()->andX(
188
+                    $qb->expr()->eq('key', $qb->createNamedParameter($path)),
189
+                    $qb->expr()->eq('lock', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT))
190
+                ))->executeStatement();
191
+        }
192
+        if ($result !== 1) {
193
+            throw new LockedException($path);
194
+        }
195
+        $this->markChange($path, $targetType);
196
+    }
197
+
198
+    /** @inheritDoc */
199
+    public function cleanExpiredLocks(): void {
200
+        $expire = $this->timeFactory->getTime();
201
+        try {
202
+            $qb = $this->connection->getQueryBuilder();
203
+            $qb->delete('file_locks')
204
+                ->where($qb->expr()->lt('ttl', $qb->createNamedParameter($expire, IQueryBuilder::PARAM_INT)))
205
+                ->executeStatement();
206
+        } catch (\Exception $e) {
207
+            // If the table is missing, the clean up was successful
208
+            if ($this->connection->tableExists('file_locks')) {
209
+                throw $e;
210
+            }
211
+        }
212
+    }
213
+
214
+    /** @inheritDoc */
215
+    public function releaseAll(): void {
216
+        parent::releaseAll();
217
+
218
+        if (!$this->cacheSharedLocks) {
219
+            return;
220
+        }
221
+        // since we keep shared locks we need to manually clean those
222
+        $lockedPaths = array_keys($this->sharedLocks);
223
+        $lockedPaths = array_filter($lockedPaths, function ($path) {
224
+            return $this->sharedLocks[$path];
225
+        });
226
+
227
+        $chunkedPaths = array_chunk($lockedPaths, 100);
228
+
229
+        $qb = $this->connection->getQueryBuilder();
230
+        $qb->update('file_locks')
231
+            ->set('lock', $qb->func()->subtract('lock', $qb->expr()->literal(1)))
232
+            ->where($qb->expr()->in('key', $qb->createParameter('chunk')))
233
+            ->andWhere($qb->expr()->gt('lock', new Literal(0)));
234
+
235
+        foreach ($chunkedPaths as $chunk) {
236
+            $qb->setParameter('chunk', $chunk, IQueryBuilder::PARAM_STR_ARRAY);
237
+            $qb->executeStatement();
238
+        }
239
+    }
240 240
 }
Please login to merge, or discard this patch.
lib/private/Diagnostics/QueryLogger.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -69,7 +69,7 @@
 block discarded – undo
69 69
 	public function stopQuery() {
70 70
 		if ($this->activated && $this->activeQuery) {
71 71
 			$this->activeQuery->end(microtime(true));
72
-			$this->queries[(string)$this->index] = $this->activeQuery;
72
+			$this->queries[(string) $this->index] = $this->activeQuery;
73 73
 			$this->index++;
74 74
 			$this->activeQuery = null;
75 75
 		}
Please login to merge, or discard this patch.
Indentation   +52 added lines, -52 removed lines patch added patch discarded remove patch
@@ -11,64 +11,64 @@
 block discarded – undo
11 11
 use OCP\Diagnostics\IQueryLogger;
12 12
 
13 13
 class QueryLogger implements IQueryLogger {
14
-	protected int $index = 0;
15
-	protected ?Query $activeQuery = null;
16
-	/** @var CappedMemoryCache<Query> */
17
-	protected CappedMemoryCache $queries;
14
+    protected int $index = 0;
15
+    protected ?Query $activeQuery = null;
16
+    /** @var CappedMemoryCache<Query> */
17
+    protected CappedMemoryCache $queries;
18 18
 
19
-	/**
20
-	 * QueryLogger constructor.
21
-	 */
22
-	public function __construct() {
23
-		$this->queries = new CappedMemoryCache(1024);
24
-	}
19
+    /**
20
+     * QueryLogger constructor.
21
+     */
22
+    public function __construct() {
23
+        $this->queries = new CappedMemoryCache(1024);
24
+    }
25 25
 
26 26
 
27
-	/**
28
-	 * @var bool - Module needs to be activated by some app
29
-	 */
30
-	private $activated = false;
27
+    /**
28
+     * @var bool - Module needs to be activated by some app
29
+     */
30
+    private $activated = false;
31 31
 
32
-	/**
33
-	 * @inheritdoc
34
-	 */
35
-	public function startQuery($sql, ?array $params = null, ?array $types = null) {
36
-		if ($this->activated) {
37
-			$this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack());
38
-		}
39
-	}
32
+    /**
33
+     * @inheritdoc
34
+     */
35
+    public function startQuery($sql, ?array $params = null, ?array $types = null) {
36
+        if ($this->activated) {
37
+            $this->activeQuery = new Query($sql, $params, microtime(true), $this->getStack());
38
+        }
39
+    }
40 40
 
41
-	private function getStack() {
42
-		$stack = debug_backtrace();
43
-		array_shift($stack);
44
-		array_shift($stack);
45
-		array_shift($stack);
46
-		return $stack;
47
-	}
41
+    private function getStack() {
42
+        $stack = debug_backtrace();
43
+        array_shift($stack);
44
+        array_shift($stack);
45
+        array_shift($stack);
46
+        return $stack;
47
+    }
48 48
 
49
-	/**
50
-	 * @inheritdoc
51
-	 */
52
-	public function stopQuery() {
53
-		if ($this->activated && $this->activeQuery) {
54
-			$this->activeQuery->end(microtime(true));
55
-			$this->queries[(string)$this->index] = $this->activeQuery;
56
-			$this->index++;
57
-			$this->activeQuery = null;
58
-		}
59
-	}
49
+    /**
50
+     * @inheritdoc
51
+     */
52
+    public function stopQuery() {
53
+        if ($this->activated && $this->activeQuery) {
54
+            $this->activeQuery->end(microtime(true));
55
+            $this->queries[(string)$this->index] = $this->activeQuery;
56
+            $this->index++;
57
+            $this->activeQuery = null;
58
+        }
59
+    }
60 60
 
61
-	/**
62
-	 * @inheritdoc
63
-	 */
64
-	public function getQueries() {
65
-		return $this->queries->getData();
66
-	}
61
+    /**
62
+     * @inheritdoc
63
+     */
64
+    public function getQueries() {
65
+        return $this->queries->getData();
66
+    }
67 67
 
68
-	/**
69
-	 * @inheritdoc
70
-	 */
71
-	public function activate() {
72
-		$this->activated = true;
73
-	}
68
+    /**
69
+     * @inheritdoc
70
+     */
71
+    public function activate() {
72
+        $this->activated = true;
73
+    }
74 74
 }
Please login to merge, or discard this patch.
apps/federation/lib/DbHandler.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
 			return $query->getLastInsertId();
81 81
 		}
82 82
 
83
-		$message = 'Internal failure, Could not add trusted server: ' . $url;
83
+		$message = 'Internal failure, Could not add trusted server: '.$url;
84 84
 		$message_t = $this->IL10N->t('Could not add server');
85 85
 		throw new HintException($message, $message_t);
86 86
 		return -1;
@@ -114,7 +114,7 @@  discard block
 block discarded – undo
114 114
 		$qResult->closeCursor();
115 115
 
116 116
 		if (empty($result)) {
117
-			throw new \Exception('No Server found with ID: ' . $id);
117
+			throw new \Exception('No Server found with ID: '.$id);
118 118
 		}
119 119
 
120 120
 		return $result[0];
@@ -183,7 +183,7 @@  discard block
 block discarded – undo
183 183
 		$statement->closeCursor();
184 184
 
185 185
 		if (!isset($result['token'])) {
186
-			throw new \Exception('No token found for: ' . $url);
186
+			throw new \Exception('No token found for: '.$url);
187 187
 		}
188 188
 
189 189
 		return $result['token'];
@@ -216,7 +216,7 @@  discard block
 block discarded – undo
216 216
 		$statement = $query->executeQuery();
217 217
 		$result = $statement->fetch();
218 218
 		$statement->closeCursor();
219
-		return (string)$result['shared_secret'];
219
+		return (string) $result['shared_secret'];
220 220
 	}
221 221
 
222 222
 	/**
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
 		$statement = $query->executeQuery();
248 248
 		$result = $statement->fetch();
249 249
 		$statement->closeCursor();
250
-		return (int)$result['status'];
250
+		return (int) $result['status'];
251 251
 	}
252 252
 
253 253
 	/**
Please login to merge, or discard this patch.
Indentation   +240 added lines, -240 removed lines patch added patch discarded remove patch
@@ -25,244 +25,244 @@
 block discarded – undo
25 25
  * @package OCA\Federation
26 26
  */
27 27
 class DbHandler {
28
-	private string $dbTable = 'trusted_servers';
29
-
30
-	public function __construct(
31
-		private IDBConnection $connection,
32
-		private IL10N $IL10N,
33
-	) {
34
-	}
35
-
36
-	/**
37
-	 * Add server to the list of trusted servers
38
-	 *
39
-	 * @throws HintException
40
-	 */
41
-	public function addServer(string $url): int {
42
-		$hash = $this->hash($url);
43
-		$url = rtrim($url, '/');
44
-		$query = $this->connection->getQueryBuilder();
45
-		$query->insert($this->dbTable)
46
-			->values([
47
-				'url' => $query->createParameter('url'),
48
-				'url_hash' => $query->createParameter('url_hash'),
49
-			])
50
-			->setParameter('url', $url)
51
-			->setParameter('url_hash', $hash);
52
-
53
-		$result = $query->executeStatement();
54
-
55
-		if ($result) {
56
-			return $query->getLastInsertId();
57
-		}
58
-
59
-		$message = 'Internal failure, Could not add trusted server: ' . $url;
60
-		$message_t = $this->IL10N->t('Could not add server');
61
-		throw new HintException($message, $message_t);
62
-		return -1;
63
-	}
64
-
65
-	/**
66
-	 * Remove server from the list of trusted servers
67
-	 */
68
-	public function removeServer(int $id): void {
69
-		$query = $this->connection->getQueryBuilder();
70
-		$query->delete($this->dbTable)
71
-			->where($query->expr()->eq('id', $query->createParameter('id')))
72
-			->setParameter('id', $id);
73
-		$query->executeStatement();
74
-	}
75
-
76
-	/**
77
-	 * Get trusted server with given ID
78
-	 *
79
-	 * @return array{id: int, url: string, url_hash: string, token: ?string, shared_secret: ?string, status: int, sync_token: ?string}
80
-	 * @throws \Exception
81
-	 */
82
-	public function getServerById(int $id): array {
83
-		$query = $this->connection->getQueryBuilder();
84
-		$query->select('*')->from($this->dbTable)
85
-			->where($query->expr()->eq('id', $query->createParameter('id')))
86
-			->setParameter('id', $id, IQueryBuilder::PARAM_INT);
87
-
88
-		$qResult = $query->executeQuery();
89
-		$result = $qResult->fetchAll();
90
-		$qResult->closeCursor();
91
-
92
-		if (empty($result)) {
93
-			throw new \Exception('No Server found with ID: ' . $id);
94
-		}
95
-
96
-		return $result[0];
97
-	}
98
-
99
-	/**
100
-	 * Get all trusted servers
101
-	 *
102
-	 * @return list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}>
103
-	 * @throws DBException
104
-	 */
105
-	public function getAllServer(): array {
106
-		$query = $this->connection->getQueryBuilder();
107
-		$query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
108
-			->from($this->dbTable);
109
-		$statement = $query->executeQuery();
110
-		$result = $statement->fetchAll();
111
-		$statement->closeCursor();
112
-		return $result;
113
-	}
114
-
115
-	/**
116
-	 * Check if server already exists in the database table
117
-	 */
118
-	public function serverExists(string $url): bool {
119
-		$hash = $this->hash($url);
120
-		$query = $this->connection->getQueryBuilder();
121
-		$query->select('url')
122
-			->from($this->dbTable)
123
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
124
-			->setParameter('url_hash', $hash);
125
-		$statement = $query->executeQuery();
126
-		$result = $statement->fetchAll();
127
-		$statement->closeCursor();
128
-
129
-		return !empty($result);
130
-	}
131
-
132
-	/**
133
-	 * Write token to database. Token is used to exchange the secret
134
-	 */
135
-	public function addToken(string $url, string $token): void {
136
-		$hash = $this->hash($url);
137
-		$query = $this->connection->getQueryBuilder();
138
-		$query->update($this->dbTable)
139
-			->set('token', $query->createParameter('token'))
140
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
141
-			->setParameter('url_hash', $hash)
142
-			->setParameter('token', $token);
143
-		$query->executeStatement();
144
-	}
145
-
146
-	/**
147
-	 * Get token stored in database
148
-	 * @throws \Exception
149
-	 */
150
-	public function getToken(string $url): string {
151
-		$hash = $this->hash($url);
152
-		$query = $this->connection->getQueryBuilder();
153
-		$query->select('token')->from($this->dbTable)
154
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
155
-			->setParameter('url_hash', $hash);
156
-
157
-		$statement = $query->executeQuery();
158
-		$result = $statement->fetch();
159
-		$statement->closeCursor();
160
-
161
-		if (!isset($result['token'])) {
162
-			throw new \Exception('No token found for: ' . $url);
163
-		}
164
-
165
-		return $result['token'];
166
-	}
167
-
168
-	/**
169
-	 * Add shared Secret to database
170
-	 */
171
-	public function addSharedSecret(string $url, string $sharedSecret): void {
172
-		$hash = $this->hash($url);
173
-		$query = $this->connection->getQueryBuilder();
174
-		$query->update($this->dbTable)
175
-			->set('shared_secret', $query->createParameter('sharedSecret'))
176
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
177
-			->setParameter('url_hash', $hash)
178
-			->setParameter('sharedSecret', $sharedSecret);
179
-		$query->executeStatement();
180
-	}
181
-
182
-	/**
183
-	 * Get shared secret from database
184
-	 */
185
-	public function getSharedSecret(string $url): string {
186
-		$hash = $this->hash($url);
187
-		$query = $this->connection->getQueryBuilder();
188
-		$query->select('shared_secret')->from($this->dbTable)
189
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
190
-			->setParameter('url_hash', $hash);
191
-
192
-		$statement = $query->executeQuery();
193
-		$result = $statement->fetch();
194
-		$statement->closeCursor();
195
-		return (string)$result['shared_secret'];
196
-	}
197
-
198
-	/**
199
-	 * Set server status
200
-	 */
201
-	public function setServerStatus(string $url, int $status, ?string $token = null): void {
202
-		$hash = $this->hash($url);
203
-		$query = $this->connection->getQueryBuilder();
204
-		$query->update($this->dbTable)
205
-			->set('status', $query->createNamedParameter($status))
206
-			->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
207
-		if (!is_null($token)) {
208
-			$query->set('sync_token', $query->createNamedParameter($token));
209
-		}
210
-		$query->executeStatement();
211
-	}
212
-
213
-	/**
214
-	 * Get server status
215
-	 */
216
-	public function getServerStatus(string $url): int {
217
-		$hash = $this->hash($url);
218
-		$query = $this->connection->getQueryBuilder();
219
-		$query->select('status')->from($this->dbTable)
220
-			->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
221
-			->setParameter('url_hash', $hash);
222
-
223
-		$statement = $query->executeQuery();
224
-		$result = $statement->fetch();
225
-		$statement->closeCursor();
226
-		return (int)$result['status'];
227
-	}
228
-
229
-	/**
230
-	 * Create hash from URL
231
-	 */
232
-	protected function hash(string $url): string {
233
-		$normalized = $this->normalizeUrl($url);
234
-		return sha1($normalized);
235
-	}
236
-
237
-	/**
238
-	 * Normalize URL, used to create the sha1 hash
239
-	 */
240
-	protected function normalizeUrl(string $url): string {
241
-		$normalized = $url;
242
-
243
-		if (strpos($url, 'https://') === 0) {
244
-			$normalized = substr($url, strlen('https://'));
245
-		} elseif (strpos($url, 'http://') === 0) {
246
-			$normalized = substr($url, strlen('http://'));
247
-		}
248
-
249
-		$normalized = Filesystem::normalizePath($normalized);
250
-		$normalized = trim($normalized, '/');
251
-
252
-		return $normalized;
253
-	}
254
-
255
-	public function auth(string $username, string $password): bool {
256
-		if ($username !== 'system') {
257
-			return false;
258
-		}
259
-		$query = $this->connection->getQueryBuilder();
260
-		$query->select('url')->from($this->dbTable)
261
-			->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
262
-
263
-		$statement = $query->executeQuery();
264
-		$result = $statement->fetch();
265
-		$statement->closeCursor();
266
-		return !empty($result);
267
-	}
28
+    private string $dbTable = 'trusted_servers';
29
+
30
+    public function __construct(
31
+        private IDBConnection $connection,
32
+        private IL10N $IL10N,
33
+    ) {
34
+    }
35
+
36
+    /**
37
+     * Add server to the list of trusted servers
38
+     *
39
+     * @throws HintException
40
+     */
41
+    public function addServer(string $url): int {
42
+        $hash = $this->hash($url);
43
+        $url = rtrim($url, '/');
44
+        $query = $this->connection->getQueryBuilder();
45
+        $query->insert($this->dbTable)
46
+            ->values([
47
+                'url' => $query->createParameter('url'),
48
+                'url_hash' => $query->createParameter('url_hash'),
49
+            ])
50
+            ->setParameter('url', $url)
51
+            ->setParameter('url_hash', $hash);
52
+
53
+        $result = $query->executeStatement();
54
+
55
+        if ($result) {
56
+            return $query->getLastInsertId();
57
+        }
58
+
59
+        $message = 'Internal failure, Could not add trusted server: ' . $url;
60
+        $message_t = $this->IL10N->t('Could not add server');
61
+        throw new HintException($message, $message_t);
62
+        return -1;
63
+    }
64
+
65
+    /**
66
+     * Remove server from the list of trusted servers
67
+     */
68
+    public function removeServer(int $id): void {
69
+        $query = $this->connection->getQueryBuilder();
70
+        $query->delete($this->dbTable)
71
+            ->where($query->expr()->eq('id', $query->createParameter('id')))
72
+            ->setParameter('id', $id);
73
+        $query->executeStatement();
74
+    }
75
+
76
+    /**
77
+     * Get trusted server with given ID
78
+     *
79
+     * @return array{id: int, url: string, url_hash: string, token: ?string, shared_secret: ?string, status: int, sync_token: ?string}
80
+     * @throws \Exception
81
+     */
82
+    public function getServerById(int $id): array {
83
+        $query = $this->connection->getQueryBuilder();
84
+        $query->select('*')->from($this->dbTable)
85
+            ->where($query->expr()->eq('id', $query->createParameter('id')))
86
+            ->setParameter('id', $id, IQueryBuilder::PARAM_INT);
87
+
88
+        $qResult = $query->executeQuery();
89
+        $result = $qResult->fetchAll();
90
+        $qResult->closeCursor();
91
+
92
+        if (empty($result)) {
93
+            throw new \Exception('No Server found with ID: ' . $id);
94
+        }
95
+
96
+        return $result[0];
97
+    }
98
+
99
+    /**
100
+     * Get all trusted servers
101
+     *
102
+     * @return list<array{id: int, url: string, url_hash: string, shared_secret: ?string, status: int, sync_token: ?string}>
103
+     * @throws DBException
104
+     */
105
+    public function getAllServer(): array {
106
+        $query = $this->connection->getQueryBuilder();
107
+        $query->select(['url', 'url_hash', 'id', 'status', 'shared_secret', 'sync_token'])
108
+            ->from($this->dbTable);
109
+        $statement = $query->executeQuery();
110
+        $result = $statement->fetchAll();
111
+        $statement->closeCursor();
112
+        return $result;
113
+    }
114
+
115
+    /**
116
+     * Check if server already exists in the database table
117
+     */
118
+    public function serverExists(string $url): bool {
119
+        $hash = $this->hash($url);
120
+        $query = $this->connection->getQueryBuilder();
121
+        $query->select('url')
122
+            ->from($this->dbTable)
123
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
124
+            ->setParameter('url_hash', $hash);
125
+        $statement = $query->executeQuery();
126
+        $result = $statement->fetchAll();
127
+        $statement->closeCursor();
128
+
129
+        return !empty($result);
130
+    }
131
+
132
+    /**
133
+     * Write token to database. Token is used to exchange the secret
134
+     */
135
+    public function addToken(string $url, string $token): void {
136
+        $hash = $this->hash($url);
137
+        $query = $this->connection->getQueryBuilder();
138
+        $query->update($this->dbTable)
139
+            ->set('token', $query->createParameter('token'))
140
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
141
+            ->setParameter('url_hash', $hash)
142
+            ->setParameter('token', $token);
143
+        $query->executeStatement();
144
+    }
145
+
146
+    /**
147
+     * Get token stored in database
148
+     * @throws \Exception
149
+     */
150
+    public function getToken(string $url): string {
151
+        $hash = $this->hash($url);
152
+        $query = $this->connection->getQueryBuilder();
153
+        $query->select('token')->from($this->dbTable)
154
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
155
+            ->setParameter('url_hash', $hash);
156
+
157
+        $statement = $query->executeQuery();
158
+        $result = $statement->fetch();
159
+        $statement->closeCursor();
160
+
161
+        if (!isset($result['token'])) {
162
+            throw new \Exception('No token found for: ' . $url);
163
+        }
164
+
165
+        return $result['token'];
166
+    }
167
+
168
+    /**
169
+     * Add shared Secret to database
170
+     */
171
+    public function addSharedSecret(string $url, string $sharedSecret): void {
172
+        $hash = $this->hash($url);
173
+        $query = $this->connection->getQueryBuilder();
174
+        $query->update($this->dbTable)
175
+            ->set('shared_secret', $query->createParameter('sharedSecret'))
176
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
177
+            ->setParameter('url_hash', $hash)
178
+            ->setParameter('sharedSecret', $sharedSecret);
179
+        $query->executeStatement();
180
+    }
181
+
182
+    /**
183
+     * Get shared secret from database
184
+     */
185
+    public function getSharedSecret(string $url): string {
186
+        $hash = $this->hash($url);
187
+        $query = $this->connection->getQueryBuilder();
188
+        $query->select('shared_secret')->from($this->dbTable)
189
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
190
+            ->setParameter('url_hash', $hash);
191
+
192
+        $statement = $query->executeQuery();
193
+        $result = $statement->fetch();
194
+        $statement->closeCursor();
195
+        return (string)$result['shared_secret'];
196
+    }
197
+
198
+    /**
199
+     * Set server status
200
+     */
201
+    public function setServerStatus(string $url, int $status, ?string $token = null): void {
202
+        $hash = $this->hash($url);
203
+        $query = $this->connection->getQueryBuilder();
204
+        $query->update($this->dbTable)
205
+            ->set('status', $query->createNamedParameter($status))
206
+            ->where($query->expr()->eq('url_hash', $query->createNamedParameter($hash)));
207
+        if (!is_null($token)) {
208
+            $query->set('sync_token', $query->createNamedParameter($token));
209
+        }
210
+        $query->executeStatement();
211
+    }
212
+
213
+    /**
214
+     * Get server status
215
+     */
216
+    public function getServerStatus(string $url): int {
217
+        $hash = $this->hash($url);
218
+        $query = $this->connection->getQueryBuilder();
219
+        $query->select('status')->from($this->dbTable)
220
+            ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash')))
221
+            ->setParameter('url_hash', $hash);
222
+
223
+        $statement = $query->executeQuery();
224
+        $result = $statement->fetch();
225
+        $statement->closeCursor();
226
+        return (int)$result['status'];
227
+    }
228
+
229
+    /**
230
+     * Create hash from URL
231
+     */
232
+    protected function hash(string $url): string {
233
+        $normalized = $this->normalizeUrl($url);
234
+        return sha1($normalized);
235
+    }
236
+
237
+    /**
238
+     * Normalize URL, used to create the sha1 hash
239
+     */
240
+    protected function normalizeUrl(string $url): string {
241
+        $normalized = $url;
242
+
243
+        if (strpos($url, 'https://') === 0) {
244
+            $normalized = substr($url, strlen('https://'));
245
+        } elseif (strpos($url, 'http://') === 0) {
246
+            $normalized = substr($url, strlen('http://'));
247
+        }
248
+
249
+        $normalized = Filesystem::normalizePath($normalized);
250
+        $normalized = trim($normalized, '/');
251
+
252
+        return $normalized;
253
+    }
254
+
255
+    public function auth(string $username, string $password): bool {
256
+        if ($username !== 'system') {
257
+            return false;
258
+        }
259
+        $query = $this->connection->getQueryBuilder();
260
+        $query->select('url')->from($this->dbTable)
261
+            ->where($query->expr()->eq('shared_secret', $query->createNamedParameter($password)));
262
+
263
+        $statement = $query->executeQuery();
264
+        $result = $statement->fetch();
265
+        $statement->closeCursor();
266
+        return !empty($result);
267
+    }
268 268
 }
Please login to merge, or discard this patch.