Passed
Push — master ( 20b348...4d82a9 )
by Joas
14:00 queued 12s
created
lib/private/AppFramework/Bootstrap/Coordinator.php 2 patches
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -45,153 +45,153 @@
 block discarded – undo
45 45
 
46 46
 class Coordinator {
47 47
 
48
-	/** @var IServerContainer */
49
-	private $serverContainer;
50
-
51
-	/** @var Registry */
52
-	private $registry;
53
-
54
-	/** @var IManager */
55
-	private $dashboardManager;
56
-
57
-	/** @var IEventDispatcher */
58
-	private $eventDispatcher;
59
-
60
-	/** @var LoggerInterface */
61
-	private $logger;
62
-
63
-	/** @var RegistrationContext|null */
64
-	private $registrationContext;
65
-
66
-	/** @var string[] */
67
-	private $bootedApps = [];
68
-
69
-	public function __construct(IServerContainer $container,
70
-								Registry $registry,
71
-								IManager $dashboardManager,
72
-								IEventDispatcher $eventListener,
73
-								LoggerInterface $logger) {
74
-		$this->serverContainer = $container;
75
-		$this->registry = $registry;
76
-		$this->dashboardManager = $dashboardManager;
77
-		$this->eventDispatcher = $eventListener;
78
-		$this->logger = $logger;
79
-	}
80
-
81
-	public function runInitialRegistration(): void {
82
-		$this->registerApps(OC_App::getEnabledApps());
83
-	}
84
-
85
-	public function runLazyRegistration(string $appId): void {
86
-		$this->registerApps([$appId]);
87
-	}
88
-
89
-	/**
90
-	 * @param string[] $appIds
91
-	 */
92
-	private function registerApps(array $appIds): void {
93
-		if ($this->registrationContext === null) {
94
-			$this->registrationContext = new RegistrationContext($this->logger);
95
-		}
96
-		$apps = [];
97
-		foreach ($appIds as $appId) {
98
-			/*
48
+    /** @var IServerContainer */
49
+    private $serverContainer;
50
+
51
+    /** @var Registry */
52
+    private $registry;
53
+
54
+    /** @var IManager */
55
+    private $dashboardManager;
56
+
57
+    /** @var IEventDispatcher */
58
+    private $eventDispatcher;
59
+
60
+    /** @var LoggerInterface */
61
+    private $logger;
62
+
63
+    /** @var RegistrationContext|null */
64
+    private $registrationContext;
65
+
66
+    /** @var string[] */
67
+    private $bootedApps = [];
68
+
69
+    public function __construct(IServerContainer $container,
70
+                                Registry $registry,
71
+                                IManager $dashboardManager,
72
+                                IEventDispatcher $eventListener,
73
+                                LoggerInterface $logger) {
74
+        $this->serverContainer = $container;
75
+        $this->registry = $registry;
76
+        $this->dashboardManager = $dashboardManager;
77
+        $this->eventDispatcher = $eventListener;
78
+        $this->logger = $logger;
79
+    }
80
+
81
+    public function runInitialRegistration(): void {
82
+        $this->registerApps(OC_App::getEnabledApps());
83
+    }
84
+
85
+    public function runLazyRegistration(string $appId): void {
86
+        $this->registerApps([$appId]);
87
+    }
88
+
89
+    /**
90
+     * @param string[] $appIds
91
+     */
92
+    private function registerApps(array $appIds): void {
93
+        if ($this->registrationContext === null) {
94
+            $this->registrationContext = new RegistrationContext($this->logger);
95
+        }
96
+        $apps = [];
97
+        foreach ($appIds as $appId) {
98
+            /*
99 99
 			 * First, we have to enable the app's autoloader
100 100
 			 *
101 101
 			 * @todo use $this->appManager->getAppPath($appId) here
102 102
 			 */
103
-			$path = OC_App::getAppPath($appId);
104
-			if ($path === false) {
105
-				// Ignore
106
-				continue;
107
-			}
108
-			OC_App::registerAutoloading($appId, $path);
109
-
110
-			/*
103
+            $path = OC_App::getAppPath($appId);
104
+            if ($path === false) {
105
+                // Ignore
106
+                continue;
107
+            }
108
+            OC_App::registerAutoloading($appId, $path);
109
+
110
+            /*
111 111
 			 * Next we check if there is an application class and it implements
112 112
 			 * the \OCP\AppFramework\Bootstrap\IBootstrap interface
113 113
 			 */
114
-			$appNameSpace = App::buildAppNamespace($appId);
115
-			$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
116
-			try {
117
-				if (class_exists($applicationClassName) && in_array(IBootstrap::class, class_implements($applicationClassName), true)) {
118
-					try {
119
-						/** @var IBootstrap|App $application */
120
-						$apps[$appId] = $application = $this->serverContainer->query($applicationClassName);
121
-					} catch (QueryException $e) {
122
-						// Weird, but ok
123
-						continue;
124
-					}
125
-
126
-					$application->register($this->registrationContext->for($appId));
127
-				}
128
-			} catch (Throwable $e) {
129
-				$this->logger->emergency('Error during app service registration: ' . $e->getMessage(), [
130
-					'exception' => $e,
131
-					'app' => $appId,
132
-				]);
133
-				continue;
134
-			}
135
-		}
136
-
137
-		/**
138
-		 * Now that all register methods have been called, we can delegate the registrations
139
-		 * to the actual services
140
-		 */
141
-		$this->registrationContext->delegateCapabilityRegistrations($apps);
142
-		$this->registrationContext->delegateCrashReporterRegistrations($apps, $this->registry);
143
-		$this->registrationContext->delegateDashboardPanelRegistrations($apps, $this->dashboardManager);
144
-		$this->registrationContext->delegateEventListenerRegistrations($this->eventDispatcher);
145
-		$this->registrationContext->delegateContainerRegistrations($apps);
146
-		$this->registrationContext->delegateMiddlewareRegistrations($apps);
147
-	}
148
-
149
-	public function getRegistrationContext(): ?RegistrationContext {
150
-		return $this->registrationContext;
151
-	}
152
-
153
-	public function bootApp(string $appId): void {
154
-		if (isset($this->bootedApps[$appId])) {
155
-			return;
156
-		}
157
-		$this->bootedApps[$appId] = true;
158
-
159
-		$appNameSpace = App::buildAppNamespace($appId);
160
-		$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
161
-		if (!class_exists($applicationClassName)) {
162
-			// Nothing to boot
163
-			return;
164
-		}
165
-
166
-		/*
114
+            $appNameSpace = App::buildAppNamespace($appId);
115
+            $applicationClassName = $appNameSpace . '\\AppInfo\\Application';
116
+            try {
117
+                if (class_exists($applicationClassName) && in_array(IBootstrap::class, class_implements($applicationClassName), true)) {
118
+                    try {
119
+                        /** @var IBootstrap|App $application */
120
+                        $apps[$appId] = $application = $this->serverContainer->query($applicationClassName);
121
+                    } catch (QueryException $e) {
122
+                        // Weird, but ok
123
+                        continue;
124
+                    }
125
+
126
+                    $application->register($this->registrationContext->for($appId));
127
+                }
128
+            } catch (Throwable $e) {
129
+                $this->logger->emergency('Error during app service registration: ' . $e->getMessage(), [
130
+                    'exception' => $e,
131
+                    'app' => $appId,
132
+                ]);
133
+                continue;
134
+            }
135
+        }
136
+
137
+        /**
138
+         * Now that all register methods have been called, we can delegate the registrations
139
+         * to the actual services
140
+         */
141
+        $this->registrationContext->delegateCapabilityRegistrations($apps);
142
+        $this->registrationContext->delegateCrashReporterRegistrations($apps, $this->registry);
143
+        $this->registrationContext->delegateDashboardPanelRegistrations($apps, $this->dashboardManager);
144
+        $this->registrationContext->delegateEventListenerRegistrations($this->eventDispatcher);
145
+        $this->registrationContext->delegateContainerRegistrations($apps);
146
+        $this->registrationContext->delegateMiddlewareRegistrations($apps);
147
+    }
148
+
149
+    public function getRegistrationContext(): ?RegistrationContext {
150
+        return $this->registrationContext;
151
+    }
152
+
153
+    public function bootApp(string $appId): void {
154
+        if (isset($this->bootedApps[$appId])) {
155
+            return;
156
+        }
157
+        $this->bootedApps[$appId] = true;
158
+
159
+        $appNameSpace = App::buildAppNamespace($appId);
160
+        $applicationClassName = $appNameSpace . '\\AppInfo\\Application';
161
+        if (!class_exists($applicationClassName)) {
162
+            // Nothing to boot
163
+            return;
164
+        }
165
+
166
+        /*
167 167
 		 * Now it is time to fetch an instance of the App class. For classes
168 168
 		 * that implement \OCP\AppFramework\Bootstrap\IBootstrap this means
169 169
 		 * the instance was already created for register, but any other
170 170
 		 * (legacy) code will now do their magic via the constructor.
171 171
 		 */
172
-		try {
173
-			/** @var App $application */
174
-			$application = $this->serverContainer->query($applicationClassName);
175
-			if ($application instanceof IBootstrap) {
176
-				/** @var BootContext $context */
177
-				$context = new BootContext($application->getContainer());
178
-				$application->boot($context);
179
-			}
180
-		} catch (QueryException $e) {
181
-			$this->logger->error("Could not boot $appId" . $e->getMessage(), [
182
-				'exception' => $e,
183
-			]);
184
-		} catch (Throwable $e) {
185
-			$this->logger->emergency("Could not boot $appId" . $e->getMessage(), [
186
-				'exception' => $e,
187
-			]);
188
-		}
189
-	}
190
-
191
-	public function isBootable(string $appId) {
192
-		$appNameSpace = App::buildAppNamespace($appId);
193
-		$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
194
-		return class_exists($applicationClassName) &&
195
-			in_array(IBootstrap::class, class_implements($applicationClassName), true);
196
-	}
172
+        try {
173
+            /** @var App $application */
174
+            $application = $this->serverContainer->query($applicationClassName);
175
+            if ($application instanceof IBootstrap) {
176
+                /** @var BootContext $context */
177
+                $context = new BootContext($application->getContainer());
178
+                $application->boot($context);
179
+            }
180
+        } catch (QueryException $e) {
181
+            $this->logger->error("Could not boot $appId" . $e->getMessage(), [
182
+                'exception' => $e,
183
+            ]);
184
+        } catch (Throwable $e) {
185
+            $this->logger->emergency("Could not boot $appId" . $e->getMessage(), [
186
+                'exception' => $e,
187
+            ]);
188
+        }
189
+    }
190
+
191
+    public function isBootable(string $appId) {
192
+        $appNameSpace = App::buildAppNamespace($appId);
193
+        $applicationClassName = $appNameSpace . '\\AppInfo\\Application';
194
+        return class_exists($applicationClassName) &&
195
+            in_array(IBootstrap::class, class_implements($applicationClassName), true);
196
+    }
197 197
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
 			 * the \OCP\AppFramework\Bootstrap\IBootstrap interface
113 113
 			 */
114 114
 			$appNameSpace = App::buildAppNamespace($appId);
115
-			$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
115
+			$applicationClassName = $appNameSpace.'\\AppInfo\\Application';
116 116
 			try {
117 117
 				if (class_exists($applicationClassName) && in_array(IBootstrap::class, class_implements($applicationClassName), true)) {
118 118
 					try {
@@ -126,7 +126,7 @@  discard block
 block discarded – undo
126 126
 					$application->register($this->registrationContext->for($appId));
127 127
 				}
128 128
 			} catch (Throwable $e) {
129
-				$this->logger->emergency('Error during app service registration: ' . $e->getMessage(), [
129
+				$this->logger->emergency('Error during app service registration: '.$e->getMessage(), [
130 130
 					'exception' => $e,
131 131
 					'app' => $appId,
132 132
 				]);
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
 		$this->bootedApps[$appId] = true;
158 158
 
159 159
 		$appNameSpace = App::buildAppNamespace($appId);
160
-		$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
160
+		$applicationClassName = $appNameSpace.'\\AppInfo\\Application';
161 161
 		if (!class_exists($applicationClassName)) {
162 162
 			// Nothing to boot
163 163
 			return;
@@ -178,11 +178,11 @@  discard block
 block discarded – undo
178 178
 				$application->boot($context);
179 179
 			}
180 180
 		} catch (QueryException $e) {
181
-			$this->logger->error("Could not boot $appId" . $e->getMessage(), [
181
+			$this->logger->error("Could not boot $appId".$e->getMessage(), [
182 182
 				'exception' => $e,
183 183
 			]);
184 184
 		} catch (Throwable $e) {
185
-			$this->logger->emergency("Could not boot $appId" . $e->getMessage(), [
185
+			$this->logger->emergency("Could not boot $appId".$e->getMessage(), [
186 186
 				'exception' => $e,
187 187
 			]);
188 188
 		}
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
 
191 191
 	public function isBootable(string $appId) {
192 192
 		$appNameSpace = App::buildAppNamespace($appId);
193
-		$applicationClassName = $appNameSpace . '\\AppInfo\\Application';
193
+		$applicationClassName = $appNameSpace.'\\AppInfo\\Application';
194 194
 		return class_exists($applicationClassName) &&
195 195
 			in_array(IBootstrap::class, class_implements($applicationClassName), true);
196 196
 	}
Please login to merge, or discard this patch.
lib/private/legacy/OC_App.php 2 patches
Indentation   +1129 added lines, -1129 removed lines patch added patch discarded remove patch
@@ -69,1133 +69,1133 @@
 block discarded – undo
69 69
  * upgrading and removing apps.
70 70
  */
71 71
 class OC_App {
72
-	private static $adminForms = [];
73
-	private static $personalForms = [];
74
-	private static $appTypes = [];
75
-	private static $loadedApps = [];
76
-	private static $altLogin = [];
77
-	private static $alreadyRegistered = [];
78
-	public const supportedApp = 300;
79
-	public const officialApp = 200;
80
-
81
-	/**
82
-	 * clean the appId
83
-	 *
84
-	 * @psalm-taint-escape file
85
-	 * @psalm-taint-escape include
86
-	 *
87
-	 * @param string $app AppId that needs to be cleaned
88
-	 * @return string
89
-	 */
90
-	public static function cleanAppId(string $app): string {
91
-		return str_replace(['\0', '/', '\\', '..'], '', $app);
92
-	}
93
-
94
-	/**
95
-	 * Check if an app is loaded
96
-	 *
97
-	 * @param string $app
98
-	 * @return bool
99
-	 */
100
-	public static function isAppLoaded(string $app): bool {
101
-		return isset(self::$loadedApps[$app]);
102
-	}
103
-
104
-	/**
105
-	 * loads all apps
106
-	 *
107
-	 * @param string[] $types
108
-	 * @return bool
109
-	 *
110
-	 * This function walks through the ownCloud directory and loads all apps
111
-	 * it can find. A directory contains an app if the file /appinfo/info.xml
112
-	 * exists.
113
-	 *
114
-	 * if $types is set to non-empty array, only apps of those types will be loaded
115
-	 */
116
-	public static function loadApps(array $types = []): bool {
117
-		if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
118
-			return false;
119
-		}
120
-		// Load the enabled apps here
121
-		$apps = self::getEnabledApps();
122
-
123
-		// Add each apps' folder as allowed class path
124
-		foreach ($apps as $app) {
125
-			// If the app is already loaded then autoloading it makes no sense
126
-			if (!isset(self::$loadedApps[$app])) {
127
-				$path = self::getAppPath($app);
128
-				if ($path !== false) {
129
-					self::registerAutoloading($app, $path);
130
-				}
131
-			}
132
-		}
133
-
134
-		// prevent app.php from printing output
135
-		ob_start();
136
-		foreach ($apps as $app) {
137
-			if (!isset(self::$loadedApps[$app]) && ($types === [] || self::isType($app, $types))) {
138
-				try {
139
-					self::loadApp($app);
140
-				} catch (\Throwable $e) {
141
-					\OC::$server->get(LoggerInterface::class)->emergency('Error during app loading: ' . $e->getMessage(), [
142
-						'exception' => $e,
143
-						'app' => $app,
144
-					]);
145
-				}
146
-			}
147
-		}
148
-		ob_end_clean();
149
-
150
-		return true;
151
-	}
152
-
153
-	/**
154
-	 * load a single app
155
-	 *
156
-	 * @param string $app
157
-	 * @throws Exception
158
-	 */
159
-	public static function loadApp(string $app) {
160
-		self::$loadedApps[$app] = true;
161
-		$appPath = self::getAppPath($app);
162
-		if ($appPath === false) {
163
-			return;
164
-		}
165
-
166
-		// in case someone calls loadApp() directly
167
-		self::registerAutoloading($app, $appPath);
168
-
169
-		/** @var Coordinator $coordinator */
170
-		$coordinator = \OC::$server->query(Coordinator::class);
171
-		$isBootable = $coordinator->isBootable($app);
172
-
173
-		$hasAppPhpFile = is_file($appPath . '/appinfo/app.php');
174
-
175
-		if ($isBootable && $hasAppPhpFile) {
176
-			\OC::$server->getLogger()->error('/appinfo/app.php is not loaded when \OCP\AppFramework\Bootstrap\IBootstrap on the application class is used. Migrate everything from app.php to the Application class.', [
177
-				'app' => $app,
178
-			]);
179
-		} elseif ($hasAppPhpFile) {
180
-			\OC::$server->getLogger()->debug('/appinfo/app.php is deprecated, use \OCP\AppFramework\Bootstrap\IBootstrap on the application class instead.', [
181
-				'app' => $app,
182
-			]);
183
-			\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
184
-			try {
185
-				self::requireAppFile($app);
186
-			} catch (Throwable $ex) {
187
-				if ($ex instanceof ServerNotAvailableException) {
188
-					throw $ex;
189
-				}
190
-				if (!\OC::$server->getAppManager()->isShipped($app) && !self::isType($app, ['authentication'])) {
191
-					\OC::$server->getLogger()->logException($ex, [
192
-						'message' => "App $app threw an error during app.php load and will be disabled: " . $ex->getMessage(),
193
-					]);
194
-
195
-					// Only disable apps which are not shipped and that are not authentication apps
196
-					\OC::$server->getAppManager()->disableApp($app, true);
197
-				} else {
198
-					\OC::$server->getLogger()->logException($ex, [
199
-						'message' => "App $app threw an error during app.php load: " . $ex->getMessage(),
200
-					]);
201
-				}
202
-			}
203
-			\OC::$server->getEventLogger()->end('load_app_' . $app);
204
-		}
205
-		$coordinator->bootApp($app);
206
-
207
-		$info = self::getAppInfo($app);
208
-		if (!empty($info['activity']['filters'])) {
209
-			foreach ($info['activity']['filters'] as $filter) {
210
-				\OC::$server->getActivityManager()->registerFilter($filter);
211
-			}
212
-		}
213
-		if (!empty($info['activity']['settings'])) {
214
-			foreach ($info['activity']['settings'] as $setting) {
215
-				\OC::$server->getActivityManager()->registerSetting($setting);
216
-			}
217
-		}
218
-		if (!empty($info['activity']['providers'])) {
219
-			foreach ($info['activity']['providers'] as $provider) {
220
-				\OC::$server->getActivityManager()->registerProvider($provider);
221
-			}
222
-		}
223
-
224
-		if (!empty($info['settings']['admin'])) {
225
-			foreach ($info['settings']['admin'] as $setting) {
226
-				\OC::$server->getSettingsManager()->registerSetting('admin', $setting);
227
-			}
228
-		}
229
-		if (!empty($info['settings']['admin-section'])) {
230
-			foreach ($info['settings']['admin-section'] as $section) {
231
-				\OC::$server->getSettingsManager()->registerSection('admin', $section);
232
-			}
233
-		}
234
-		if (!empty($info['settings']['personal'])) {
235
-			foreach ($info['settings']['personal'] as $setting) {
236
-				\OC::$server->getSettingsManager()->registerSetting('personal', $setting);
237
-			}
238
-		}
239
-		if (!empty($info['settings']['personal-section'])) {
240
-			foreach ($info['settings']['personal-section'] as $section) {
241
-				\OC::$server->getSettingsManager()->registerSection('personal', $section);
242
-			}
243
-		}
244
-
245
-		if (!empty($info['collaboration']['plugins'])) {
246
-			// deal with one or many plugin entries
247
-			$plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
248
-				[$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
249
-			foreach ($plugins as $plugin) {
250
-				if ($plugin['@attributes']['type'] === 'collaborator-search') {
251
-					$pluginInfo = [
252
-						'shareType' => $plugin['@attributes']['share-type'],
253
-						'class' => $plugin['@value'],
254
-					];
255
-					\OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo);
256
-				} elseif ($plugin['@attributes']['type'] === 'autocomplete-sort') {
257
-					\OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']);
258
-				}
259
-			}
260
-		}
261
-	}
262
-
263
-	/**
264
-	 * @internal
265
-	 * @param string $app
266
-	 * @param string $path
267
-	 * @param bool $force
268
-	 */
269
-	public static function registerAutoloading(string $app, string $path, bool $force = false) {
270
-		$key = $app . '-' . $path;
271
-		if (!$force && isset(self::$alreadyRegistered[$key])) {
272
-			return;
273
-		}
274
-
275
-		self::$alreadyRegistered[$key] = true;
276
-
277
-		// Register on PSR-4 composer autoloader
278
-		$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
279
-		\OC::$server->registerNamespace($app, $appNamespace);
280
-
281
-		if (file_exists($path . '/composer/autoload.php')) {
282
-			require_once $path . '/composer/autoload.php';
283
-		} else {
284
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
285
-			// Register on legacy autoloader
286
-			\OC::$loader->addValidRoot($path);
287
-		}
288
-
289
-		// Register Test namespace only when testing
290
-		if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
291
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
292
-		}
293
-	}
294
-
295
-	/**
296
-	 * Load app.php from the given app
297
-	 *
298
-	 * @param string $app app name
299
-	 * @throws Error
300
-	 */
301
-	private static function requireAppFile(string $app) {
302
-		// encapsulated here to avoid variable scope conflicts
303
-		require_once $app . '/appinfo/app.php';
304
-	}
305
-
306
-	/**
307
-	 * check if an app is of a specific type
308
-	 *
309
-	 * @param string $app
310
-	 * @param array $types
311
-	 * @return bool
312
-	 */
313
-	public static function isType(string $app, array $types): bool {
314
-		$appTypes = self::getAppTypes($app);
315
-		foreach ($types as $type) {
316
-			if (array_search($type, $appTypes) !== false) {
317
-				return true;
318
-			}
319
-		}
320
-		return false;
321
-	}
322
-
323
-	/**
324
-	 * get the types of an app
325
-	 *
326
-	 * @param string $app
327
-	 * @return array
328
-	 */
329
-	private static function getAppTypes(string $app): array {
330
-		//load the cache
331
-		if (count(self::$appTypes) == 0) {
332
-			self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
333
-		}
334
-
335
-		if (isset(self::$appTypes[$app])) {
336
-			return explode(',', self::$appTypes[$app]);
337
-		}
338
-
339
-		return [];
340
-	}
341
-
342
-	/**
343
-	 * read app types from info.xml and cache them in the database
344
-	 */
345
-	public static function setAppTypes(string $app) {
346
-		$appManager = \OC::$server->getAppManager();
347
-		$appData = $appManager->getAppInfo($app);
348
-		if (!is_array($appData)) {
349
-			return;
350
-		}
351
-
352
-		if (isset($appData['types'])) {
353
-			$appTypes = implode(',', $appData['types']);
354
-		} else {
355
-			$appTypes = '';
356
-			$appData['types'] = [];
357
-		}
358
-
359
-		$config = \OC::$server->getConfig();
360
-		$config->setAppValue($app, 'types', $appTypes);
361
-
362
-		if ($appManager->hasProtectedAppType($appData['types'])) {
363
-			$enabled = $config->getAppValue($app, 'enabled', 'yes');
364
-			if ($enabled !== 'yes' && $enabled !== 'no') {
365
-				$config->setAppValue($app, 'enabled', 'yes');
366
-			}
367
-		}
368
-	}
369
-
370
-	/**
371
-	 * Returns apps enabled for the current user.
372
-	 *
373
-	 * @param bool $forceRefresh whether to refresh the cache
374
-	 * @param bool $all whether to return apps for all users, not only the
375
-	 * currently logged in one
376
-	 * @return string[]
377
-	 */
378
-	public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array {
379
-		if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
380
-			return [];
381
-		}
382
-		// in incognito mode or when logged out, $user will be false,
383
-		// which is also the case during an upgrade
384
-		$appManager = \OC::$server->getAppManager();
385
-		if ($all) {
386
-			$user = null;
387
-		} else {
388
-			$user = \OC::$server->getUserSession()->getUser();
389
-		}
390
-
391
-		if (is_null($user)) {
392
-			$apps = $appManager->getInstalledApps();
393
-		} else {
394
-			$apps = $appManager->getEnabledAppsForUser($user);
395
-		}
396
-		$apps = array_filter($apps, function ($app) {
397
-			return $app !== 'files';//we add this manually
398
-		});
399
-		sort($apps);
400
-		array_unshift($apps, 'files');
401
-		return $apps;
402
-	}
403
-
404
-	/**
405
-	 * checks whether or not an app is enabled
406
-	 *
407
-	 * @param string $app app
408
-	 * @return bool
409
-	 * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
410
-	 *
411
-	 * This function checks whether or not an app is enabled.
412
-	 */
413
-	public static function isEnabled(string $app): bool {
414
-		return \OC::$server->getAppManager()->isEnabledForUser($app);
415
-	}
416
-
417
-	/**
418
-	 * enables an app
419
-	 *
420
-	 * @param string $appId
421
-	 * @param array $groups (optional) when set, only these groups will have access to the app
422
-	 * @throws \Exception
423
-	 * @return void
424
-	 *
425
-	 * This function set an app as enabled in appconfig.
426
-	 */
427
-	public function enable(string $appId,
428
-						   array $groups = []) {
429
-
430
-		// Check if app is already downloaded
431
-		/** @var Installer $installer */
432
-		$installer = \OC::$server->query(Installer::class);
433
-		$isDownloaded = $installer->isDownloaded($appId);
434
-
435
-		if (!$isDownloaded) {
436
-			$installer->downloadApp($appId);
437
-		}
438
-
439
-		$installer->installApp($appId);
440
-
441
-		$appManager = \OC::$server->getAppManager();
442
-		if ($groups !== []) {
443
-			$groupManager = \OC::$server->getGroupManager();
444
-			$groupsList = [];
445
-			foreach ($groups as $group) {
446
-				$groupItem = $groupManager->get($group);
447
-				if ($groupItem instanceof \OCP\IGroup) {
448
-					$groupsList[] = $groupManager->get($group);
449
-				}
450
-			}
451
-			$appManager->enableAppForGroups($appId, $groupsList);
452
-		} else {
453
-			$appManager->enableApp($appId);
454
-		}
455
-	}
456
-
457
-	/**
458
-	 * Get the path where to install apps
459
-	 *
460
-	 * @return string|false
461
-	 */
462
-	public static function getInstallPath() {
463
-		if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
464
-			return false;
465
-		}
466
-
467
-		foreach (OC::$APPSROOTS as $dir) {
468
-			if (isset($dir['writable']) && $dir['writable'] === true) {
469
-				return $dir['path'];
470
-			}
471
-		}
472
-
473
-		\OCP\Util::writeLog('core', 'No application directories are marked as writable.', ILogger::ERROR);
474
-		return null;
475
-	}
476
-
477
-
478
-	/**
479
-	 * search for an app in all app-directories
480
-	 *
481
-	 * @param string $appId
482
-	 * @return false|string
483
-	 */
484
-	public static function findAppInDirectories(string $appId) {
485
-		$sanitizedAppId = self::cleanAppId($appId);
486
-		if ($sanitizedAppId !== $appId) {
487
-			return false;
488
-		}
489
-		static $app_dir = [];
490
-
491
-		if (isset($app_dir[$appId])) {
492
-			return $app_dir[$appId];
493
-		}
494
-
495
-		$possibleApps = [];
496
-		foreach (OC::$APPSROOTS as $dir) {
497
-			if (file_exists($dir['path'] . '/' . $appId)) {
498
-				$possibleApps[] = $dir;
499
-			}
500
-		}
501
-
502
-		if (empty($possibleApps)) {
503
-			return false;
504
-		} elseif (count($possibleApps) === 1) {
505
-			$dir = array_shift($possibleApps);
506
-			$app_dir[$appId] = $dir;
507
-			return $dir;
508
-		} else {
509
-			$versionToLoad = [];
510
-			foreach ($possibleApps as $possibleApp) {
511
-				$version = self::getAppVersionByPath($possibleApp['path'] . '/' . $appId);
512
-				if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
513
-					$versionToLoad = [
514
-						'dir' => $possibleApp,
515
-						'version' => $version,
516
-					];
517
-				}
518
-			}
519
-			$app_dir[$appId] = $versionToLoad['dir'];
520
-			return $versionToLoad['dir'];
521
-			//TODO - write test
522
-		}
523
-	}
524
-
525
-	/**
526
-	 * Get the directory for the given app.
527
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
528
-	 *
529
-	 * @psalm-taint-specialize
530
-	 *
531
-	 * @param string $appId
532
-	 * @return string|false
533
-	 * @deprecated 11.0.0 use \OC::$server->getAppManager()->getAppPath()
534
-	 */
535
-	public static function getAppPath(string $appId) {
536
-		if ($appId === null || trim($appId) === '') {
537
-			return false;
538
-		}
539
-
540
-		if (($dir = self::findAppInDirectories($appId)) != false) {
541
-			return $dir['path'] . '/' . $appId;
542
-		}
543
-		return false;
544
-	}
545
-
546
-	/**
547
-	 * Get the path for the given app on the access
548
-	 * If the app is defined in multiple directories, the first one is taken. (false if not found)
549
-	 *
550
-	 * @param string $appId
551
-	 * @return string|false
552
-	 * @deprecated 18.0.0 use \OC::$server->getAppManager()->getAppWebPath()
553
-	 */
554
-	public static function getAppWebPath(string $appId) {
555
-		if (($dir = self::findAppInDirectories($appId)) != false) {
556
-			return OC::$WEBROOT . $dir['url'] . '/' . $appId;
557
-		}
558
-		return false;
559
-	}
560
-
561
-	/**
562
-	 * get the last version of the app from appinfo/info.xml
563
-	 *
564
-	 * @param string $appId
565
-	 * @param bool $useCache
566
-	 * @return string
567
-	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion()
568
-	 */
569
-	public static function getAppVersion(string $appId, bool $useCache = true): string {
570
-		return \OC::$server->getAppManager()->getAppVersion($appId, $useCache);
571
-	}
572
-
573
-	/**
574
-	 * get app's version based on it's path
575
-	 *
576
-	 * @param string $path
577
-	 * @return string
578
-	 */
579
-	public static function getAppVersionByPath(string $path): string {
580
-		$infoFile = $path . '/appinfo/info.xml';
581
-		$appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
582
-		return isset($appData['version']) ? $appData['version'] : '';
583
-	}
584
-
585
-
586
-	/**
587
-	 * Read all app metadata from the info.xml file
588
-	 *
589
-	 * @param string $appId id of the app or the path of the info.xml file
590
-	 * @param bool $path
591
-	 * @param string $lang
592
-	 * @return array|null
593
-	 * @note all data is read from info.xml, not just pre-defined fields
594
-	 * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppInfo()
595
-	 */
596
-	public static function getAppInfo(string $appId, bool $path = false, string $lang = null) {
597
-		return \OC::$server->getAppManager()->getAppInfo($appId, $path, $lang);
598
-	}
599
-
600
-	/**
601
-	 * Returns the navigation
602
-	 *
603
-	 * @return array
604
-	 * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll()
605
-	 *
606
-	 * This function returns an array containing all entries added. The
607
-	 * entries are sorted by the key 'order' ascending. Additional to the keys
608
-	 * given for each app the following keys exist:
609
-	 *   - active: boolean, signals if the user is on this navigation entry
610
-	 */
611
-	public static function getNavigation(): array {
612
-		return OC::$server->getNavigationManager()->getAll();
613
-	}
614
-
615
-	/**
616
-	 * Returns the Settings Navigation
617
-	 *
618
-	 * @return string[]
619
-	 * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll('settings')
620
-	 *
621
-	 * This function returns an array containing all settings pages added. The
622
-	 * entries are sorted by the key 'order' ascending.
623
-	 */
624
-	public static function getSettingsNavigation(): array {
625
-		return OC::$server->getNavigationManager()->getAll('settings');
626
-	}
627
-
628
-	/**
629
-	 * get the id of loaded app
630
-	 *
631
-	 * @return string
632
-	 */
633
-	public static function getCurrentApp(): string {
634
-		$request = \OC::$server->getRequest();
635
-		$script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
636
-		$topFolder = substr($script, 0, strpos($script, '/') ?: 0);
637
-		if (empty($topFolder)) {
638
-			$path_info = $request->getPathInfo();
639
-			if ($path_info) {
640
-				$topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
641
-			}
642
-		}
643
-		if ($topFolder == 'apps') {
644
-			$length = strlen($topFolder);
645
-			return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1) ?: '';
646
-		} else {
647
-			return $topFolder;
648
-		}
649
-	}
650
-
651
-	/**
652
-	 * @param string $type
653
-	 * @return array
654
-	 */
655
-	public static function getForms(string $type): array {
656
-		$forms = [];
657
-		switch ($type) {
658
-			case 'admin':
659
-				$source = self::$adminForms;
660
-				break;
661
-			case 'personal':
662
-				$source = self::$personalForms;
663
-				break;
664
-			default:
665
-				return [];
666
-		}
667
-		foreach ($source as $form) {
668
-			$forms[] = include $form;
669
-		}
670
-		return $forms;
671
-	}
672
-
673
-	/**
674
-	 * register an admin form to be shown
675
-	 *
676
-	 * @param string $app
677
-	 * @param string $page
678
-	 */
679
-	public static function registerAdmin(string $app, string $page) {
680
-		self::$adminForms[] = $app . '/' . $page . '.php';
681
-	}
682
-
683
-	/**
684
-	 * register a personal form to be shown
685
-	 * @param string $app
686
-	 * @param string $page
687
-	 */
688
-	public static function registerPersonal(string $app, string $page) {
689
-		self::$personalForms[] = $app . '/' . $page . '.php';
690
-	}
691
-
692
-	/**
693
-	 * @param array $entry
694
-	 * @deprecated 20.0.0 Please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface
695
-	 */
696
-	public static function registerLogIn(array $entry) {
697
-		\OC::$server->getLogger()->debug('OC_App::registerLogIn() is deprecated, please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface');
698
-		self::$altLogin[] = $entry;
699
-	}
700
-
701
-	/**
702
-	 * @return array
703
-	 */
704
-	public static function getAlternativeLogIns(): array {
705
-		/** @var Coordinator $bootstrapCoordinator */
706
-		$bootstrapCoordinator = \OC::$server->query(Coordinator::class);
707
-
708
-		foreach ($bootstrapCoordinator->getRegistrationContext()->getAlternativeLogins() as $registration) {
709
-			if (!in_array(IAlternativeLogin::class, class_implements($registration->getService()), true)) {
710
-				\OC::$server->getLogger()->error('Alternative login option {option} does not implement {interface} and is therefore ignored.', [
711
-					'option' => $registration->getService(),
712
-					'interface' => IAlternativeLogin::class,
713
-					'app' => $registration->getAppId(),
714
-				]);
715
-				continue;
716
-			}
717
-
718
-			try {
719
-				/** @var IAlternativeLogin $provider */
720
-				$provider = \OC::$server->query($registration->getService());
721
-			} catch (QueryException $e) {
722
-				\OC::$server->getLogger()->logException($e, [
723
-					'message' => 'Alternative login option {option} can not be initialised.',
724
-					'option' => $registration->getService(),
725
-					'app' => $registration->getAppId(),
726
-				]);
727
-			}
728
-
729
-			try {
730
-				$provider->load();
731
-
732
-				self::$altLogin[] = [
733
-					'name' => $provider->getLabel(),
734
-					'href' => $provider->getLink(),
735
-					'style' => $provider->getClass(),
736
-				];
737
-			} catch (Throwable $e) {
738
-				\OC::$server->getLogger()->logException($e, [
739
-					'message' => 'Alternative login option {option} had an error while loading.',
740
-					'option' => $registration->getService(),
741
-					'app' => $registration->getAppId(),
742
-				]);
743
-			}
744
-		}
745
-
746
-		return self::$altLogin;
747
-	}
748
-
749
-	/**
750
-	 * get a list of all apps in the apps folder
751
-	 *
752
-	 * @return string[] an array of app names (string IDs)
753
-	 * @todo: change the name of this method to getInstalledApps, which is more accurate
754
-	 */
755
-	public static function getAllApps(): array {
756
-		$apps = [];
757
-
758
-		foreach (OC::$APPSROOTS as $apps_dir) {
759
-			if (!is_readable($apps_dir['path'])) {
760
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], ILogger::WARN);
761
-				continue;
762
-			}
763
-			$dh = opendir($apps_dir['path']);
764
-
765
-			if (is_resource($dh)) {
766
-				while (($file = readdir($dh)) !== false) {
767
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
768
-						$apps[] = $file;
769
-					}
770
-				}
771
-			}
772
-		}
773
-
774
-		$apps = array_unique($apps);
775
-
776
-		return $apps;
777
-	}
778
-
779
-	/**
780
-	 * List all apps, this is used in apps.php
781
-	 *
782
-	 * @return array
783
-	 */
784
-	public function listAllApps(): array {
785
-		$installedApps = OC_App::getAllApps();
786
-
787
-		$appManager = \OC::$server->getAppManager();
788
-		//we don't want to show configuration for these
789
-		$blacklist = $appManager->getAlwaysEnabledApps();
790
-		$appList = [];
791
-		$langCode = \OC::$server->getL10N('core')->getLanguageCode();
792
-		$urlGenerator = \OC::$server->getURLGenerator();
793
-		/** @var \OCP\Support\Subscription\IRegistry $subscriptionRegistry */
794
-		$subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class);
795
-		$supportedApps = $subscriptionRegistry->delegateGetSupportedApps();
796
-
797
-		foreach ($installedApps as $app) {
798
-			if (array_search($app, $blacklist) === false) {
799
-				$info = OC_App::getAppInfo($app, false, $langCode);
800
-				if (!is_array($info)) {
801
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', ILogger::ERROR);
802
-					continue;
803
-				}
804
-
805
-				if (!isset($info['name'])) {
806
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', ILogger::ERROR);
807
-					continue;
808
-				}
809
-
810
-				$enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no');
811
-				$info['groups'] = null;
812
-				if ($enabled === 'yes') {
813
-					$active = true;
814
-				} elseif ($enabled === 'no') {
815
-					$active = false;
816
-				} else {
817
-					$active = true;
818
-					$info['groups'] = $enabled;
819
-				}
820
-
821
-				$info['active'] = $active;
822
-
823
-				if ($appManager->isShipped($app)) {
824
-					$info['internal'] = true;
825
-					$info['level'] = self::officialApp;
826
-					$info['removable'] = false;
827
-				} else {
828
-					$info['internal'] = false;
829
-					$info['removable'] = true;
830
-				}
831
-
832
-				if (in_array($app, $supportedApps)) {
833
-					$info['level'] = self::supportedApp;
834
-				}
835
-
836
-				$appPath = self::getAppPath($app);
837
-				if ($appPath !== false) {
838
-					$appIcon = $appPath . '/img/' . $app . '.svg';
839
-					if (file_exists($appIcon)) {
840
-						$info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
841
-						$info['previewAsIcon'] = true;
842
-					} else {
843
-						$appIcon = $appPath . '/img/app.svg';
844
-						if (file_exists($appIcon)) {
845
-							$info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
846
-							$info['previewAsIcon'] = true;
847
-						}
848
-					}
849
-				}
850
-				// fix documentation
851
-				if (isset($info['documentation']) && is_array($info['documentation'])) {
852
-					foreach ($info['documentation'] as $key => $url) {
853
-						// If it is not an absolute URL we assume it is a key
854
-						// i.e. admin-ldap will get converted to go.php?to=admin-ldap
855
-						if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
856
-							$url = $urlGenerator->linkToDocs($url);
857
-						}
858
-
859
-						$info['documentation'][$key] = $url;
860
-					}
861
-				}
862
-
863
-				$info['version'] = OC_App::getAppVersion($app);
864
-				$appList[] = $info;
865
-			}
866
-		}
867
-
868
-		return $appList;
869
-	}
870
-
871
-	public static function shouldUpgrade(string $app): bool {
872
-		$versions = self::getAppVersions();
873
-		$currentVersion = OC_App::getAppVersion($app);
874
-		if ($currentVersion && isset($versions[$app])) {
875
-			$installedVersion = $versions[$app];
876
-			if (!version_compare($currentVersion, $installedVersion, '=')) {
877
-				return true;
878
-			}
879
-		}
880
-		return false;
881
-	}
882
-
883
-	/**
884
-	 * Adjust the number of version parts of $version1 to match
885
-	 * the number of version parts of $version2.
886
-	 *
887
-	 * @param string $version1 version to adjust
888
-	 * @param string $version2 version to take the number of parts from
889
-	 * @return string shortened $version1
890
-	 */
891
-	private static function adjustVersionParts(string $version1, string $version2): string {
892
-		$version1 = explode('.', $version1);
893
-		$version2 = explode('.', $version2);
894
-		// reduce $version1 to match the number of parts in $version2
895
-		while (count($version1) > count($version2)) {
896
-			array_pop($version1);
897
-		}
898
-		// if $version1 does not have enough parts, add some
899
-		while (count($version1) < count($version2)) {
900
-			$version1[] = '0';
901
-		}
902
-		return implode('.', $version1);
903
-	}
904
-
905
-	/**
906
-	 * Check whether the current ownCloud version matches the given
907
-	 * application's version requirements.
908
-	 *
909
-	 * The comparison is made based on the number of parts that the
910
-	 * app info version has. For example for ownCloud 6.0.3 if the
911
-	 * app info version is expecting version 6.0, the comparison is
912
-	 * made on the first two parts of the ownCloud version.
913
-	 * This means that it's possible to specify "requiremin" => 6
914
-	 * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
915
-	 *
916
-	 * @param string $ocVersion ownCloud version to check against
917
-	 * @param array $appInfo app info (from xml)
918
-	 *
919
-	 * @return boolean true if compatible, otherwise false
920
-	 */
921
-	public static function isAppCompatible(string $ocVersion, array $appInfo, bool $ignoreMax = false): bool {
922
-		$requireMin = '';
923
-		$requireMax = '';
924
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
925
-			$requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
926
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
927
-			$requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
928
-		} elseif (isset($appInfo['requiremin'])) {
929
-			$requireMin = $appInfo['requiremin'];
930
-		} elseif (isset($appInfo['require'])) {
931
-			$requireMin = $appInfo['require'];
932
-		}
933
-
934
-		if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
935
-			$requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
936
-		} elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
937
-			$requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
938
-		} elseif (isset($appInfo['requiremax'])) {
939
-			$requireMax = $appInfo['requiremax'];
940
-		}
941
-
942
-		if (!empty($requireMin)
943
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
944
-		) {
945
-			return false;
946
-		}
947
-
948
-		if (!$ignoreMax && !empty($requireMax)
949
-			&& version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
950
-		) {
951
-			return false;
952
-		}
953
-
954
-		return true;
955
-	}
956
-
957
-	/**
958
-	 * get the installed version of all apps
959
-	 */
960
-	public static function getAppVersions() {
961
-		static $versions;
962
-
963
-		if (!$versions) {
964
-			$appConfig = \OC::$server->getAppConfig();
965
-			$versions = $appConfig->getValues(false, 'installed_version');
966
-		}
967
-		return $versions;
968
-	}
969
-
970
-	/**
971
-	 * update the database for the app and call the update script
972
-	 *
973
-	 * @param string $appId
974
-	 * @return bool
975
-	 */
976
-	public static function updateApp(string $appId): bool {
977
-		$appPath = self::getAppPath($appId);
978
-		if ($appPath === false) {
979
-			return false;
980
-		}
981
-
982
-		if (is_file($appPath . '/appinfo/database.xml')) {
983
-			\OC::$server->getLogger()->error('The appinfo/database.xml file is not longer supported. Used in ' . $appId);
984
-			return false;
985
-		}
986
-
987
-		\OC::$server->getAppManager()->clearAppsCache();
988
-		$appData = self::getAppInfo($appId);
989
-
990
-		$ignoreMaxApps = \OC::$server->getConfig()->getSystemValue('app_install_overwrite', []);
991
-		$ignoreMax = in_array($appId, $ignoreMaxApps, true);
992
-		\OC_App::checkAppDependencies(
993
-			\OC::$server->getConfig(),
994
-			\OC::$server->getL10N('core'),
995
-			$appData,
996
-			$ignoreMax
997
-		);
998
-
999
-		self::registerAutoloading($appId, $appPath, true);
1000
-		self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1001
-
1002
-		$ms = new MigrationService($appId, \OC::$server->get(\OC\DB\Connection::class));
1003
-		$ms->migrate();
1004
-
1005
-		self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1006
-		self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
1007
-		// update appversion in app manager
1008
-		\OC::$server->getAppManager()->clearAppsCache();
1009
-		\OC::$server->getAppManager()->getAppVersion($appId, false);
1010
-
1011
-		self::setupBackgroundJobs($appData['background-jobs']);
1012
-
1013
-		//set remote/public handlers
1014
-		if (array_key_exists('ocsid', $appData)) {
1015
-			\OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
1016
-		} elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
1017
-			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1018
-		}
1019
-		foreach ($appData['remote'] as $name => $path) {
1020
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1021
-		}
1022
-		foreach ($appData['public'] as $name => $path) {
1023
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1024
-		}
1025
-
1026
-		self::setAppTypes($appId);
1027
-
1028
-		$version = \OC_App::getAppVersion($appId);
1029
-		\OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version);
1030
-
1031
-		\OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
1032
-			ManagerEvent::EVENT_APP_UPDATE, $appId
1033
-		));
1034
-
1035
-		return true;
1036
-	}
1037
-
1038
-	/**
1039
-	 * @param string $appId
1040
-	 * @param string[] $steps
1041
-	 * @throws \OC\NeedsUpdateException
1042
-	 */
1043
-	public static function executeRepairSteps(string $appId, array $steps) {
1044
-		if (empty($steps)) {
1045
-			return;
1046
-		}
1047
-		// load the app
1048
-		self::loadApp($appId);
1049
-
1050
-		$dispatcher = OC::$server->getEventDispatcher();
1051
-
1052
-		// load the steps
1053
-		$r = new Repair([], $dispatcher, \OC::$server->get(LoggerInterface::class));
1054
-		foreach ($steps as $step) {
1055
-			try {
1056
-				$r->addStep($step);
1057
-			} catch (Exception $ex) {
1058
-				$r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1059
-				\OC::$server->getLogger()->logException($ex);
1060
-			}
1061
-		}
1062
-		// run the steps
1063
-		$r->run();
1064
-	}
1065
-
1066
-	public static function setupBackgroundJobs(array $jobs) {
1067
-		$queue = \OC::$server->getJobList();
1068
-		foreach ($jobs as $job) {
1069
-			$queue->add($job);
1070
-		}
1071
-	}
1072
-
1073
-	/**
1074
-	 * @param string $appId
1075
-	 * @param string[] $steps
1076
-	 */
1077
-	private static function setupLiveMigrations(string $appId, array $steps) {
1078
-		$queue = \OC::$server->getJobList();
1079
-		foreach ($steps as $step) {
1080
-			$queue->add('OC\Migration\BackgroundRepair', [
1081
-				'app' => $appId,
1082
-				'step' => $step]);
1083
-		}
1084
-	}
1085
-
1086
-	/**
1087
-	 * @param string $appId
1088
-	 * @return \OC\Files\View|false
1089
-	 */
1090
-	public static function getStorage(string $appId) {
1091
-		if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1092
-			if (\OC::$server->getUserSession()->isLoggedIn()) {
1093
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1094
-				if (!$view->file_exists($appId)) {
1095
-					$view->mkdir($appId);
1096
-				}
1097
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1098
-			} else {
1099
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', ILogger::ERROR);
1100
-				return false;
1101
-			}
1102
-		} else {
1103
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', ILogger::ERROR);
1104
-			return false;
1105
-		}
1106
-	}
1107
-
1108
-	protected static function findBestL10NOption(array $options, string $lang): string {
1109
-		// only a single option
1110
-		if (isset($options['@value'])) {
1111
-			return $options['@value'];
1112
-		}
1113
-
1114
-		$fallback = $similarLangFallback = $englishFallback = false;
1115
-
1116
-		$lang = strtolower($lang);
1117
-		$similarLang = $lang;
1118
-		if (strpos($similarLang, '_')) {
1119
-			// For "de_DE" we want to find "de" and the other way around
1120
-			$similarLang = substr($lang, 0, strpos($lang, '_'));
1121
-		}
1122
-
1123
-		foreach ($options as $option) {
1124
-			if (is_array($option)) {
1125
-				if ($fallback === false) {
1126
-					$fallback = $option['@value'];
1127
-				}
1128
-
1129
-				if (!isset($option['@attributes']['lang'])) {
1130
-					continue;
1131
-				}
1132
-
1133
-				$attributeLang = strtolower($option['@attributes']['lang']);
1134
-				if ($attributeLang === $lang) {
1135
-					return $option['@value'];
1136
-				}
1137
-
1138
-				if ($attributeLang === $similarLang) {
1139
-					$similarLangFallback = $option['@value'];
1140
-				} elseif (strpos($attributeLang, $similarLang . '_') === 0) {
1141
-					if ($similarLangFallback === false) {
1142
-						$similarLangFallback = $option['@value'];
1143
-					}
1144
-				}
1145
-			} else {
1146
-				$englishFallback = $option;
1147
-			}
1148
-		}
1149
-
1150
-		if ($similarLangFallback !== false) {
1151
-			return $similarLangFallback;
1152
-		} elseif ($englishFallback !== false) {
1153
-			return $englishFallback;
1154
-		}
1155
-		return (string) $fallback;
1156
-	}
1157
-
1158
-	/**
1159
-	 * parses the app data array and enhanced the 'description' value
1160
-	 *
1161
-	 * @param array $data the app data
1162
-	 * @param string $lang
1163
-	 * @return array improved app data
1164
-	 */
1165
-	public static function parseAppInfo(array $data, $lang = null): array {
1166
-		if ($lang && isset($data['name']) && is_array($data['name'])) {
1167
-			$data['name'] = self::findBestL10NOption($data['name'], $lang);
1168
-		}
1169
-		if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1170
-			$data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1171
-		}
1172
-		if ($lang && isset($data['description']) && is_array($data['description'])) {
1173
-			$data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1174
-		} elseif (isset($data['description']) && is_string($data['description'])) {
1175
-			$data['description'] = trim($data['description']);
1176
-		} else {
1177
-			$data['description'] = '';
1178
-		}
1179
-
1180
-		return $data;
1181
-	}
1182
-
1183
-	/**
1184
-	 * @param \OCP\IConfig $config
1185
-	 * @param \OCP\IL10N $l
1186
-	 * @param array $info
1187
-	 * @throws \Exception
1188
-	 */
1189
-	public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info, bool $ignoreMax) {
1190
-		$dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1191
-		$missing = $dependencyAnalyzer->analyze($info, $ignoreMax);
1192
-		if (!empty($missing)) {
1193
-			$missingMsg = implode(PHP_EOL, $missing);
1194
-			throw new \Exception(
1195
-				$l->t('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s',
1196
-					[$info['name'], $missingMsg]
1197
-				)
1198
-			);
1199
-		}
1200
-	}
72
+    private static $adminForms = [];
73
+    private static $personalForms = [];
74
+    private static $appTypes = [];
75
+    private static $loadedApps = [];
76
+    private static $altLogin = [];
77
+    private static $alreadyRegistered = [];
78
+    public const supportedApp = 300;
79
+    public const officialApp = 200;
80
+
81
+    /**
82
+     * clean the appId
83
+     *
84
+     * @psalm-taint-escape file
85
+     * @psalm-taint-escape include
86
+     *
87
+     * @param string $app AppId that needs to be cleaned
88
+     * @return string
89
+     */
90
+    public static function cleanAppId(string $app): string {
91
+        return str_replace(['\0', '/', '\\', '..'], '', $app);
92
+    }
93
+
94
+    /**
95
+     * Check if an app is loaded
96
+     *
97
+     * @param string $app
98
+     * @return bool
99
+     */
100
+    public static function isAppLoaded(string $app): bool {
101
+        return isset(self::$loadedApps[$app]);
102
+    }
103
+
104
+    /**
105
+     * loads all apps
106
+     *
107
+     * @param string[] $types
108
+     * @return bool
109
+     *
110
+     * This function walks through the ownCloud directory and loads all apps
111
+     * it can find. A directory contains an app if the file /appinfo/info.xml
112
+     * exists.
113
+     *
114
+     * if $types is set to non-empty array, only apps of those types will be loaded
115
+     */
116
+    public static function loadApps(array $types = []): bool {
117
+        if ((bool) \OC::$server->getSystemConfig()->getValue('maintenance', false)) {
118
+            return false;
119
+        }
120
+        // Load the enabled apps here
121
+        $apps = self::getEnabledApps();
122
+
123
+        // Add each apps' folder as allowed class path
124
+        foreach ($apps as $app) {
125
+            // If the app is already loaded then autoloading it makes no sense
126
+            if (!isset(self::$loadedApps[$app])) {
127
+                $path = self::getAppPath($app);
128
+                if ($path !== false) {
129
+                    self::registerAutoloading($app, $path);
130
+                }
131
+            }
132
+        }
133
+
134
+        // prevent app.php from printing output
135
+        ob_start();
136
+        foreach ($apps as $app) {
137
+            if (!isset(self::$loadedApps[$app]) && ($types === [] || self::isType($app, $types))) {
138
+                try {
139
+                    self::loadApp($app);
140
+                } catch (\Throwable $e) {
141
+                    \OC::$server->get(LoggerInterface::class)->emergency('Error during app loading: ' . $e->getMessage(), [
142
+                        'exception' => $e,
143
+                        'app' => $app,
144
+                    ]);
145
+                }
146
+            }
147
+        }
148
+        ob_end_clean();
149
+
150
+        return true;
151
+    }
152
+
153
+    /**
154
+     * load a single app
155
+     *
156
+     * @param string $app
157
+     * @throws Exception
158
+     */
159
+    public static function loadApp(string $app) {
160
+        self::$loadedApps[$app] = true;
161
+        $appPath = self::getAppPath($app);
162
+        if ($appPath === false) {
163
+            return;
164
+        }
165
+
166
+        // in case someone calls loadApp() directly
167
+        self::registerAutoloading($app, $appPath);
168
+
169
+        /** @var Coordinator $coordinator */
170
+        $coordinator = \OC::$server->query(Coordinator::class);
171
+        $isBootable = $coordinator->isBootable($app);
172
+
173
+        $hasAppPhpFile = is_file($appPath . '/appinfo/app.php');
174
+
175
+        if ($isBootable && $hasAppPhpFile) {
176
+            \OC::$server->getLogger()->error('/appinfo/app.php is not loaded when \OCP\AppFramework\Bootstrap\IBootstrap on the application class is used. Migrate everything from app.php to the Application class.', [
177
+                'app' => $app,
178
+            ]);
179
+        } elseif ($hasAppPhpFile) {
180
+            \OC::$server->getLogger()->debug('/appinfo/app.php is deprecated, use \OCP\AppFramework\Bootstrap\IBootstrap on the application class instead.', [
181
+                'app' => $app,
182
+            ]);
183
+            \OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
184
+            try {
185
+                self::requireAppFile($app);
186
+            } catch (Throwable $ex) {
187
+                if ($ex instanceof ServerNotAvailableException) {
188
+                    throw $ex;
189
+                }
190
+                if (!\OC::$server->getAppManager()->isShipped($app) && !self::isType($app, ['authentication'])) {
191
+                    \OC::$server->getLogger()->logException($ex, [
192
+                        'message' => "App $app threw an error during app.php load and will be disabled: " . $ex->getMessage(),
193
+                    ]);
194
+
195
+                    // Only disable apps which are not shipped and that are not authentication apps
196
+                    \OC::$server->getAppManager()->disableApp($app, true);
197
+                } else {
198
+                    \OC::$server->getLogger()->logException($ex, [
199
+                        'message' => "App $app threw an error during app.php load: " . $ex->getMessage(),
200
+                    ]);
201
+                }
202
+            }
203
+            \OC::$server->getEventLogger()->end('load_app_' . $app);
204
+        }
205
+        $coordinator->bootApp($app);
206
+
207
+        $info = self::getAppInfo($app);
208
+        if (!empty($info['activity']['filters'])) {
209
+            foreach ($info['activity']['filters'] as $filter) {
210
+                \OC::$server->getActivityManager()->registerFilter($filter);
211
+            }
212
+        }
213
+        if (!empty($info['activity']['settings'])) {
214
+            foreach ($info['activity']['settings'] as $setting) {
215
+                \OC::$server->getActivityManager()->registerSetting($setting);
216
+            }
217
+        }
218
+        if (!empty($info['activity']['providers'])) {
219
+            foreach ($info['activity']['providers'] as $provider) {
220
+                \OC::$server->getActivityManager()->registerProvider($provider);
221
+            }
222
+        }
223
+
224
+        if (!empty($info['settings']['admin'])) {
225
+            foreach ($info['settings']['admin'] as $setting) {
226
+                \OC::$server->getSettingsManager()->registerSetting('admin', $setting);
227
+            }
228
+        }
229
+        if (!empty($info['settings']['admin-section'])) {
230
+            foreach ($info['settings']['admin-section'] as $section) {
231
+                \OC::$server->getSettingsManager()->registerSection('admin', $section);
232
+            }
233
+        }
234
+        if (!empty($info['settings']['personal'])) {
235
+            foreach ($info['settings']['personal'] as $setting) {
236
+                \OC::$server->getSettingsManager()->registerSetting('personal', $setting);
237
+            }
238
+        }
239
+        if (!empty($info['settings']['personal-section'])) {
240
+            foreach ($info['settings']['personal-section'] as $section) {
241
+                \OC::$server->getSettingsManager()->registerSection('personal', $section);
242
+            }
243
+        }
244
+
245
+        if (!empty($info['collaboration']['plugins'])) {
246
+            // deal with one or many plugin entries
247
+            $plugins = isset($info['collaboration']['plugins']['plugin']['@value']) ?
248
+                [$info['collaboration']['plugins']['plugin']] : $info['collaboration']['plugins']['plugin'];
249
+            foreach ($plugins as $plugin) {
250
+                if ($plugin['@attributes']['type'] === 'collaborator-search') {
251
+                    $pluginInfo = [
252
+                        'shareType' => $plugin['@attributes']['share-type'],
253
+                        'class' => $plugin['@value'],
254
+                    ];
255
+                    \OC::$server->getCollaboratorSearch()->registerPlugin($pluginInfo);
256
+                } elseif ($plugin['@attributes']['type'] === 'autocomplete-sort') {
257
+                    \OC::$server->getAutoCompleteManager()->registerSorter($plugin['@value']);
258
+                }
259
+            }
260
+        }
261
+    }
262
+
263
+    /**
264
+     * @internal
265
+     * @param string $app
266
+     * @param string $path
267
+     * @param bool $force
268
+     */
269
+    public static function registerAutoloading(string $app, string $path, bool $force = false) {
270
+        $key = $app . '-' . $path;
271
+        if (!$force && isset(self::$alreadyRegistered[$key])) {
272
+            return;
273
+        }
274
+
275
+        self::$alreadyRegistered[$key] = true;
276
+
277
+        // Register on PSR-4 composer autoloader
278
+        $appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
279
+        \OC::$server->registerNamespace($app, $appNamespace);
280
+
281
+        if (file_exists($path . '/composer/autoload.php')) {
282
+            require_once $path . '/composer/autoload.php';
283
+        } else {
284
+            \OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
285
+            // Register on legacy autoloader
286
+            \OC::$loader->addValidRoot($path);
287
+        }
288
+
289
+        // Register Test namespace only when testing
290
+        if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
291
+            \OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
292
+        }
293
+    }
294
+
295
+    /**
296
+     * Load app.php from the given app
297
+     *
298
+     * @param string $app app name
299
+     * @throws Error
300
+     */
301
+    private static function requireAppFile(string $app) {
302
+        // encapsulated here to avoid variable scope conflicts
303
+        require_once $app . '/appinfo/app.php';
304
+    }
305
+
306
+    /**
307
+     * check if an app is of a specific type
308
+     *
309
+     * @param string $app
310
+     * @param array $types
311
+     * @return bool
312
+     */
313
+    public static function isType(string $app, array $types): bool {
314
+        $appTypes = self::getAppTypes($app);
315
+        foreach ($types as $type) {
316
+            if (array_search($type, $appTypes) !== false) {
317
+                return true;
318
+            }
319
+        }
320
+        return false;
321
+    }
322
+
323
+    /**
324
+     * get the types of an app
325
+     *
326
+     * @param string $app
327
+     * @return array
328
+     */
329
+    private static function getAppTypes(string $app): array {
330
+        //load the cache
331
+        if (count(self::$appTypes) == 0) {
332
+            self::$appTypes = \OC::$server->getAppConfig()->getValues(false, 'types');
333
+        }
334
+
335
+        if (isset(self::$appTypes[$app])) {
336
+            return explode(',', self::$appTypes[$app]);
337
+        }
338
+
339
+        return [];
340
+    }
341
+
342
+    /**
343
+     * read app types from info.xml and cache them in the database
344
+     */
345
+    public static function setAppTypes(string $app) {
346
+        $appManager = \OC::$server->getAppManager();
347
+        $appData = $appManager->getAppInfo($app);
348
+        if (!is_array($appData)) {
349
+            return;
350
+        }
351
+
352
+        if (isset($appData['types'])) {
353
+            $appTypes = implode(',', $appData['types']);
354
+        } else {
355
+            $appTypes = '';
356
+            $appData['types'] = [];
357
+        }
358
+
359
+        $config = \OC::$server->getConfig();
360
+        $config->setAppValue($app, 'types', $appTypes);
361
+
362
+        if ($appManager->hasProtectedAppType($appData['types'])) {
363
+            $enabled = $config->getAppValue($app, 'enabled', 'yes');
364
+            if ($enabled !== 'yes' && $enabled !== 'no') {
365
+                $config->setAppValue($app, 'enabled', 'yes');
366
+            }
367
+        }
368
+    }
369
+
370
+    /**
371
+     * Returns apps enabled for the current user.
372
+     *
373
+     * @param bool $forceRefresh whether to refresh the cache
374
+     * @param bool $all whether to return apps for all users, not only the
375
+     * currently logged in one
376
+     * @return string[]
377
+     */
378
+    public static function getEnabledApps(bool $forceRefresh = false, bool $all = false): array {
379
+        if (!\OC::$server->getSystemConfig()->getValue('installed', false)) {
380
+            return [];
381
+        }
382
+        // in incognito mode or when logged out, $user will be false,
383
+        // which is also the case during an upgrade
384
+        $appManager = \OC::$server->getAppManager();
385
+        if ($all) {
386
+            $user = null;
387
+        } else {
388
+            $user = \OC::$server->getUserSession()->getUser();
389
+        }
390
+
391
+        if (is_null($user)) {
392
+            $apps = $appManager->getInstalledApps();
393
+        } else {
394
+            $apps = $appManager->getEnabledAppsForUser($user);
395
+        }
396
+        $apps = array_filter($apps, function ($app) {
397
+            return $app !== 'files';//we add this manually
398
+        });
399
+        sort($apps);
400
+        array_unshift($apps, 'files');
401
+        return $apps;
402
+    }
403
+
404
+    /**
405
+     * checks whether or not an app is enabled
406
+     *
407
+     * @param string $app app
408
+     * @return bool
409
+     * @deprecated 13.0.0 use \OC::$server->getAppManager()->isEnabledForUser($appId)
410
+     *
411
+     * This function checks whether or not an app is enabled.
412
+     */
413
+    public static function isEnabled(string $app): bool {
414
+        return \OC::$server->getAppManager()->isEnabledForUser($app);
415
+    }
416
+
417
+    /**
418
+     * enables an app
419
+     *
420
+     * @param string $appId
421
+     * @param array $groups (optional) when set, only these groups will have access to the app
422
+     * @throws \Exception
423
+     * @return void
424
+     *
425
+     * This function set an app as enabled in appconfig.
426
+     */
427
+    public function enable(string $appId,
428
+                            array $groups = []) {
429
+
430
+        // Check if app is already downloaded
431
+        /** @var Installer $installer */
432
+        $installer = \OC::$server->query(Installer::class);
433
+        $isDownloaded = $installer->isDownloaded($appId);
434
+
435
+        if (!$isDownloaded) {
436
+            $installer->downloadApp($appId);
437
+        }
438
+
439
+        $installer->installApp($appId);
440
+
441
+        $appManager = \OC::$server->getAppManager();
442
+        if ($groups !== []) {
443
+            $groupManager = \OC::$server->getGroupManager();
444
+            $groupsList = [];
445
+            foreach ($groups as $group) {
446
+                $groupItem = $groupManager->get($group);
447
+                if ($groupItem instanceof \OCP\IGroup) {
448
+                    $groupsList[] = $groupManager->get($group);
449
+                }
450
+            }
451
+            $appManager->enableAppForGroups($appId, $groupsList);
452
+        } else {
453
+            $appManager->enableApp($appId);
454
+        }
455
+    }
456
+
457
+    /**
458
+     * Get the path where to install apps
459
+     *
460
+     * @return string|false
461
+     */
462
+    public static function getInstallPath() {
463
+        if (\OC::$server->getSystemConfig()->getValue('appstoreenabled', true) == false) {
464
+            return false;
465
+        }
466
+
467
+        foreach (OC::$APPSROOTS as $dir) {
468
+            if (isset($dir['writable']) && $dir['writable'] === true) {
469
+                return $dir['path'];
470
+            }
471
+        }
472
+
473
+        \OCP\Util::writeLog('core', 'No application directories are marked as writable.', ILogger::ERROR);
474
+        return null;
475
+    }
476
+
477
+
478
+    /**
479
+     * search for an app in all app-directories
480
+     *
481
+     * @param string $appId
482
+     * @return false|string
483
+     */
484
+    public static function findAppInDirectories(string $appId) {
485
+        $sanitizedAppId = self::cleanAppId($appId);
486
+        if ($sanitizedAppId !== $appId) {
487
+            return false;
488
+        }
489
+        static $app_dir = [];
490
+
491
+        if (isset($app_dir[$appId])) {
492
+            return $app_dir[$appId];
493
+        }
494
+
495
+        $possibleApps = [];
496
+        foreach (OC::$APPSROOTS as $dir) {
497
+            if (file_exists($dir['path'] . '/' . $appId)) {
498
+                $possibleApps[] = $dir;
499
+            }
500
+        }
501
+
502
+        if (empty($possibleApps)) {
503
+            return false;
504
+        } elseif (count($possibleApps) === 1) {
505
+            $dir = array_shift($possibleApps);
506
+            $app_dir[$appId] = $dir;
507
+            return $dir;
508
+        } else {
509
+            $versionToLoad = [];
510
+            foreach ($possibleApps as $possibleApp) {
511
+                $version = self::getAppVersionByPath($possibleApp['path'] . '/' . $appId);
512
+                if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
513
+                    $versionToLoad = [
514
+                        'dir' => $possibleApp,
515
+                        'version' => $version,
516
+                    ];
517
+                }
518
+            }
519
+            $app_dir[$appId] = $versionToLoad['dir'];
520
+            return $versionToLoad['dir'];
521
+            //TODO - write test
522
+        }
523
+    }
524
+
525
+    /**
526
+     * Get the directory for the given app.
527
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
528
+     *
529
+     * @psalm-taint-specialize
530
+     *
531
+     * @param string $appId
532
+     * @return string|false
533
+     * @deprecated 11.0.0 use \OC::$server->getAppManager()->getAppPath()
534
+     */
535
+    public static function getAppPath(string $appId) {
536
+        if ($appId === null || trim($appId) === '') {
537
+            return false;
538
+        }
539
+
540
+        if (($dir = self::findAppInDirectories($appId)) != false) {
541
+            return $dir['path'] . '/' . $appId;
542
+        }
543
+        return false;
544
+    }
545
+
546
+    /**
547
+     * Get the path for the given app on the access
548
+     * If the app is defined in multiple directories, the first one is taken. (false if not found)
549
+     *
550
+     * @param string $appId
551
+     * @return string|false
552
+     * @deprecated 18.0.0 use \OC::$server->getAppManager()->getAppWebPath()
553
+     */
554
+    public static function getAppWebPath(string $appId) {
555
+        if (($dir = self::findAppInDirectories($appId)) != false) {
556
+            return OC::$WEBROOT . $dir['url'] . '/' . $appId;
557
+        }
558
+        return false;
559
+    }
560
+
561
+    /**
562
+     * get the last version of the app from appinfo/info.xml
563
+     *
564
+     * @param string $appId
565
+     * @param bool $useCache
566
+     * @return string
567
+     * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppVersion()
568
+     */
569
+    public static function getAppVersion(string $appId, bool $useCache = true): string {
570
+        return \OC::$server->getAppManager()->getAppVersion($appId, $useCache);
571
+    }
572
+
573
+    /**
574
+     * get app's version based on it's path
575
+     *
576
+     * @param string $path
577
+     * @return string
578
+     */
579
+    public static function getAppVersionByPath(string $path): string {
580
+        $infoFile = $path . '/appinfo/info.xml';
581
+        $appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
582
+        return isset($appData['version']) ? $appData['version'] : '';
583
+    }
584
+
585
+
586
+    /**
587
+     * Read all app metadata from the info.xml file
588
+     *
589
+     * @param string $appId id of the app or the path of the info.xml file
590
+     * @param bool $path
591
+     * @param string $lang
592
+     * @return array|null
593
+     * @note all data is read from info.xml, not just pre-defined fields
594
+     * @deprecated 14.0.0 use \OC::$server->getAppManager()->getAppInfo()
595
+     */
596
+    public static function getAppInfo(string $appId, bool $path = false, string $lang = null) {
597
+        return \OC::$server->getAppManager()->getAppInfo($appId, $path, $lang);
598
+    }
599
+
600
+    /**
601
+     * Returns the navigation
602
+     *
603
+     * @return array
604
+     * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll()
605
+     *
606
+     * This function returns an array containing all entries added. The
607
+     * entries are sorted by the key 'order' ascending. Additional to the keys
608
+     * given for each app the following keys exist:
609
+     *   - active: boolean, signals if the user is on this navigation entry
610
+     */
611
+    public static function getNavigation(): array {
612
+        return OC::$server->getNavigationManager()->getAll();
613
+    }
614
+
615
+    /**
616
+     * Returns the Settings Navigation
617
+     *
618
+     * @return string[]
619
+     * @deprecated 14.0.0 use \OC::$server->getNavigationManager()->getAll('settings')
620
+     *
621
+     * This function returns an array containing all settings pages added. The
622
+     * entries are sorted by the key 'order' ascending.
623
+     */
624
+    public static function getSettingsNavigation(): array {
625
+        return OC::$server->getNavigationManager()->getAll('settings');
626
+    }
627
+
628
+    /**
629
+     * get the id of loaded app
630
+     *
631
+     * @return string
632
+     */
633
+    public static function getCurrentApp(): string {
634
+        $request = \OC::$server->getRequest();
635
+        $script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
636
+        $topFolder = substr($script, 0, strpos($script, '/') ?: 0);
637
+        if (empty($topFolder)) {
638
+            $path_info = $request->getPathInfo();
639
+            if ($path_info) {
640
+                $topFolder = substr($path_info, 1, strpos($path_info, '/', 1) - 1);
641
+            }
642
+        }
643
+        if ($topFolder == 'apps') {
644
+            $length = strlen($topFolder);
645
+            return substr($script, $length + 1, strpos($script, '/', $length + 1) - $length - 1) ?: '';
646
+        } else {
647
+            return $topFolder;
648
+        }
649
+    }
650
+
651
+    /**
652
+     * @param string $type
653
+     * @return array
654
+     */
655
+    public static function getForms(string $type): array {
656
+        $forms = [];
657
+        switch ($type) {
658
+            case 'admin':
659
+                $source = self::$adminForms;
660
+                break;
661
+            case 'personal':
662
+                $source = self::$personalForms;
663
+                break;
664
+            default:
665
+                return [];
666
+        }
667
+        foreach ($source as $form) {
668
+            $forms[] = include $form;
669
+        }
670
+        return $forms;
671
+    }
672
+
673
+    /**
674
+     * register an admin form to be shown
675
+     *
676
+     * @param string $app
677
+     * @param string $page
678
+     */
679
+    public static function registerAdmin(string $app, string $page) {
680
+        self::$adminForms[] = $app . '/' . $page . '.php';
681
+    }
682
+
683
+    /**
684
+     * register a personal form to be shown
685
+     * @param string $app
686
+     * @param string $page
687
+     */
688
+    public static function registerPersonal(string $app, string $page) {
689
+        self::$personalForms[] = $app . '/' . $page . '.php';
690
+    }
691
+
692
+    /**
693
+     * @param array $entry
694
+     * @deprecated 20.0.0 Please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface
695
+     */
696
+    public static function registerLogIn(array $entry) {
697
+        \OC::$server->getLogger()->debug('OC_App::registerLogIn() is deprecated, please register your alternative login option using the registerAlternativeLogin() on the RegistrationContext in your Application class implementing the OCP\Authentication\IAlternativeLogin interface');
698
+        self::$altLogin[] = $entry;
699
+    }
700
+
701
+    /**
702
+     * @return array
703
+     */
704
+    public static function getAlternativeLogIns(): array {
705
+        /** @var Coordinator $bootstrapCoordinator */
706
+        $bootstrapCoordinator = \OC::$server->query(Coordinator::class);
707
+
708
+        foreach ($bootstrapCoordinator->getRegistrationContext()->getAlternativeLogins() as $registration) {
709
+            if (!in_array(IAlternativeLogin::class, class_implements($registration->getService()), true)) {
710
+                \OC::$server->getLogger()->error('Alternative login option {option} does not implement {interface} and is therefore ignored.', [
711
+                    'option' => $registration->getService(),
712
+                    'interface' => IAlternativeLogin::class,
713
+                    'app' => $registration->getAppId(),
714
+                ]);
715
+                continue;
716
+            }
717
+
718
+            try {
719
+                /** @var IAlternativeLogin $provider */
720
+                $provider = \OC::$server->query($registration->getService());
721
+            } catch (QueryException $e) {
722
+                \OC::$server->getLogger()->logException($e, [
723
+                    'message' => 'Alternative login option {option} can not be initialised.',
724
+                    'option' => $registration->getService(),
725
+                    'app' => $registration->getAppId(),
726
+                ]);
727
+            }
728
+
729
+            try {
730
+                $provider->load();
731
+
732
+                self::$altLogin[] = [
733
+                    'name' => $provider->getLabel(),
734
+                    'href' => $provider->getLink(),
735
+                    'style' => $provider->getClass(),
736
+                ];
737
+            } catch (Throwable $e) {
738
+                \OC::$server->getLogger()->logException($e, [
739
+                    'message' => 'Alternative login option {option} had an error while loading.',
740
+                    'option' => $registration->getService(),
741
+                    'app' => $registration->getAppId(),
742
+                ]);
743
+            }
744
+        }
745
+
746
+        return self::$altLogin;
747
+    }
748
+
749
+    /**
750
+     * get a list of all apps in the apps folder
751
+     *
752
+     * @return string[] an array of app names (string IDs)
753
+     * @todo: change the name of this method to getInstalledApps, which is more accurate
754
+     */
755
+    public static function getAllApps(): array {
756
+        $apps = [];
757
+
758
+        foreach (OC::$APPSROOTS as $apps_dir) {
759
+            if (!is_readable($apps_dir['path'])) {
760
+                \OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], ILogger::WARN);
761
+                continue;
762
+            }
763
+            $dh = opendir($apps_dir['path']);
764
+
765
+            if (is_resource($dh)) {
766
+                while (($file = readdir($dh)) !== false) {
767
+                    if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
768
+                        $apps[] = $file;
769
+                    }
770
+                }
771
+            }
772
+        }
773
+
774
+        $apps = array_unique($apps);
775
+
776
+        return $apps;
777
+    }
778
+
779
+    /**
780
+     * List all apps, this is used in apps.php
781
+     *
782
+     * @return array
783
+     */
784
+    public function listAllApps(): array {
785
+        $installedApps = OC_App::getAllApps();
786
+
787
+        $appManager = \OC::$server->getAppManager();
788
+        //we don't want to show configuration for these
789
+        $blacklist = $appManager->getAlwaysEnabledApps();
790
+        $appList = [];
791
+        $langCode = \OC::$server->getL10N('core')->getLanguageCode();
792
+        $urlGenerator = \OC::$server->getURLGenerator();
793
+        /** @var \OCP\Support\Subscription\IRegistry $subscriptionRegistry */
794
+        $subscriptionRegistry = \OC::$server->query(\OCP\Support\Subscription\IRegistry::class);
795
+        $supportedApps = $subscriptionRegistry->delegateGetSupportedApps();
796
+
797
+        foreach ($installedApps as $app) {
798
+            if (array_search($app, $blacklist) === false) {
799
+                $info = OC_App::getAppInfo($app, false, $langCode);
800
+                if (!is_array($info)) {
801
+                    \OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', ILogger::ERROR);
802
+                    continue;
803
+                }
804
+
805
+                if (!isset($info['name'])) {
806
+                    \OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', ILogger::ERROR);
807
+                    continue;
808
+                }
809
+
810
+                $enabled = \OC::$server->getConfig()->getAppValue($app, 'enabled', 'no');
811
+                $info['groups'] = null;
812
+                if ($enabled === 'yes') {
813
+                    $active = true;
814
+                } elseif ($enabled === 'no') {
815
+                    $active = false;
816
+                } else {
817
+                    $active = true;
818
+                    $info['groups'] = $enabled;
819
+                }
820
+
821
+                $info['active'] = $active;
822
+
823
+                if ($appManager->isShipped($app)) {
824
+                    $info['internal'] = true;
825
+                    $info['level'] = self::officialApp;
826
+                    $info['removable'] = false;
827
+                } else {
828
+                    $info['internal'] = false;
829
+                    $info['removable'] = true;
830
+                }
831
+
832
+                if (in_array($app, $supportedApps)) {
833
+                    $info['level'] = self::supportedApp;
834
+                }
835
+
836
+                $appPath = self::getAppPath($app);
837
+                if ($appPath !== false) {
838
+                    $appIcon = $appPath . '/img/' . $app . '.svg';
839
+                    if (file_exists($appIcon)) {
840
+                        $info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
841
+                        $info['previewAsIcon'] = true;
842
+                    } else {
843
+                        $appIcon = $appPath . '/img/app.svg';
844
+                        if (file_exists($appIcon)) {
845
+                            $info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
846
+                            $info['previewAsIcon'] = true;
847
+                        }
848
+                    }
849
+                }
850
+                // fix documentation
851
+                if (isset($info['documentation']) && is_array($info['documentation'])) {
852
+                    foreach ($info['documentation'] as $key => $url) {
853
+                        // If it is not an absolute URL we assume it is a key
854
+                        // i.e. admin-ldap will get converted to go.php?to=admin-ldap
855
+                        if (stripos($url, 'https://') !== 0 && stripos($url, 'http://') !== 0) {
856
+                            $url = $urlGenerator->linkToDocs($url);
857
+                        }
858
+
859
+                        $info['documentation'][$key] = $url;
860
+                    }
861
+                }
862
+
863
+                $info['version'] = OC_App::getAppVersion($app);
864
+                $appList[] = $info;
865
+            }
866
+        }
867
+
868
+        return $appList;
869
+    }
870
+
871
+    public static function shouldUpgrade(string $app): bool {
872
+        $versions = self::getAppVersions();
873
+        $currentVersion = OC_App::getAppVersion($app);
874
+        if ($currentVersion && isset($versions[$app])) {
875
+            $installedVersion = $versions[$app];
876
+            if (!version_compare($currentVersion, $installedVersion, '=')) {
877
+                return true;
878
+            }
879
+        }
880
+        return false;
881
+    }
882
+
883
+    /**
884
+     * Adjust the number of version parts of $version1 to match
885
+     * the number of version parts of $version2.
886
+     *
887
+     * @param string $version1 version to adjust
888
+     * @param string $version2 version to take the number of parts from
889
+     * @return string shortened $version1
890
+     */
891
+    private static function adjustVersionParts(string $version1, string $version2): string {
892
+        $version1 = explode('.', $version1);
893
+        $version2 = explode('.', $version2);
894
+        // reduce $version1 to match the number of parts in $version2
895
+        while (count($version1) > count($version2)) {
896
+            array_pop($version1);
897
+        }
898
+        // if $version1 does not have enough parts, add some
899
+        while (count($version1) < count($version2)) {
900
+            $version1[] = '0';
901
+        }
902
+        return implode('.', $version1);
903
+    }
904
+
905
+    /**
906
+     * Check whether the current ownCloud version matches the given
907
+     * application's version requirements.
908
+     *
909
+     * The comparison is made based on the number of parts that the
910
+     * app info version has. For example for ownCloud 6.0.3 if the
911
+     * app info version is expecting version 6.0, the comparison is
912
+     * made on the first two parts of the ownCloud version.
913
+     * This means that it's possible to specify "requiremin" => 6
914
+     * and "requiremax" => 6 and it will still match ownCloud 6.0.3.
915
+     *
916
+     * @param string $ocVersion ownCloud version to check against
917
+     * @param array $appInfo app info (from xml)
918
+     *
919
+     * @return boolean true if compatible, otherwise false
920
+     */
921
+    public static function isAppCompatible(string $ocVersion, array $appInfo, bool $ignoreMax = false): bool {
922
+        $requireMin = '';
923
+        $requireMax = '';
924
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['min-version'])) {
925
+            $requireMin = $appInfo['dependencies']['nextcloud']['@attributes']['min-version'];
926
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['min-version'])) {
927
+            $requireMin = $appInfo['dependencies']['owncloud']['@attributes']['min-version'];
928
+        } elseif (isset($appInfo['requiremin'])) {
929
+            $requireMin = $appInfo['requiremin'];
930
+        } elseif (isset($appInfo['require'])) {
931
+            $requireMin = $appInfo['require'];
932
+        }
933
+
934
+        if (isset($appInfo['dependencies']['nextcloud']['@attributes']['max-version'])) {
935
+            $requireMax = $appInfo['dependencies']['nextcloud']['@attributes']['max-version'];
936
+        } elseif (isset($appInfo['dependencies']['owncloud']['@attributes']['max-version'])) {
937
+            $requireMax = $appInfo['dependencies']['owncloud']['@attributes']['max-version'];
938
+        } elseif (isset($appInfo['requiremax'])) {
939
+            $requireMax = $appInfo['requiremax'];
940
+        }
941
+
942
+        if (!empty($requireMin)
943
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMin), $requireMin, '<')
944
+        ) {
945
+            return false;
946
+        }
947
+
948
+        if (!$ignoreMax && !empty($requireMax)
949
+            && version_compare(self::adjustVersionParts($ocVersion, $requireMax), $requireMax, '>')
950
+        ) {
951
+            return false;
952
+        }
953
+
954
+        return true;
955
+    }
956
+
957
+    /**
958
+     * get the installed version of all apps
959
+     */
960
+    public static function getAppVersions() {
961
+        static $versions;
962
+
963
+        if (!$versions) {
964
+            $appConfig = \OC::$server->getAppConfig();
965
+            $versions = $appConfig->getValues(false, 'installed_version');
966
+        }
967
+        return $versions;
968
+    }
969
+
970
+    /**
971
+     * update the database for the app and call the update script
972
+     *
973
+     * @param string $appId
974
+     * @return bool
975
+     */
976
+    public static function updateApp(string $appId): bool {
977
+        $appPath = self::getAppPath($appId);
978
+        if ($appPath === false) {
979
+            return false;
980
+        }
981
+
982
+        if (is_file($appPath . '/appinfo/database.xml')) {
983
+            \OC::$server->getLogger()->error('The appinfo/database.xml file is not longer supported. Used in ' . $appId);
984
+            return false;
985
+        }
986
+
987
+        \OC::$server->getAppManager()->clearAppsCache();
988
+        $appData = self::getAppInfo($appId);
989
+
990
+        $ignoreMaxApps = \OC::$server->getConfig()->getSystemValue('app_install_overwrite', []);
991
+        $ignoreMax = in_array($appId, $ignoreMaxApps, true);
992
+        \OC_App::checkAppDependencies(
993
+            \OC::$server->getConfig(),
994
+            \OC::$server->getL10N('core'),
995
+            $appData,
996
+            $ignoreMax
997
+        );
998
+
999
+        self::registerAutoloading($appId, $appPath, true);
1000
+        self::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1001
+
1002
+        $ms = new MigrationService($appId, \OC::$server->get(\OC\DB\Connection::class));
1003
+        $ms->migrate();
1004
+
1005
+        self::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1006
+        self::setupLiveMigrations($appId, $appData['repair-steps']['live-migration']);
1007
+        // update appversion in app manager
1008
+        \OC::$server->getAppManager()->clearAppsCache();
1009
+        \OC::$server->getAppManager()->getAppVersion($appId, false);
1010
+
1011
+        self::setupBackgroundJobs($appData['background-jobs']);
1012
+
1013
+        //set remote/public handlers
1014
+        if (array_key_exists('ocsid', $appData)) {
1015
+            \OC::$server->getConfig()->setAppValue($appId, 'ocsid', $appData['ocsid']);
1016
+        } elseif (\OC::$server->getConfig()->getAppValue($appId, 'ocsid', null) !== null) {
1017
+            \OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1018
+        }
1019
+        foreach ($appData['remote'] as $name => $path) {
1020
+            \OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1021
+        }
1022
+        foreach ($appData['public'] as $name => $path) {
1023
+            \OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1024
+        }
1025
+
1026
+        self::setAppTypes($appId);
1027
+
1028
+        $version = \OC_App::getAppVersion($appId);
1029
+        \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version);
1030
+
1031
+        \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
1032
+            ManagerEvent::EVENT_APP_UPDATE, $appId
1033
+        ));
1034
+
1035
+        return true;
1036
+    }
1037
+
1038
+    /**
1039
+     * @param string $appId
1040
+     * @param string[] $steps
1041
+     * @throws \OC\NeedsUpdateException
1042
+     */
1043
+    public static function executeRepairSteps(string $appId, array $steps) {
1044
+        if (empty($steps)) {
1045
+            return;
1046
+        }
1047
+        // load the app
1048
+        self::loadApp($appId);
1049
+
1050
+        $dispatcher = OC::$server->getEventDispatcher();
1051
+
1052
+        // load the steps
1053
+        $r = new Repair([], $dispatcher, \OC::$server->get(LoggerInterface::class));
1054
+        foreach ($steps as $step) {
1055
+            try {
1056
+                $r->addStep($step);
1057
+            } catch (Exception $ex) {
1058
+                $r->emit('\OC\Repair', 'error', [$ex->getMessage()]);
1059
+                \OC::$server->getLogger()->logException($ex);
1060
+            }
1061
+        }
1062
+        // run the steps
1063
+        $r->run();
1064
+    }
1065
+
1066
+    public static function setupBackgroundJobs(array $jobs) {
1067
+        $queue = \OC::$server->getJobList();
1068
+        foreach ($jobs as $job) {
1069
+            $queue->add($job);
1070
+        }
1071
+    }
1072
+
1073
+    /**
1074
+     * @param string $appId
1075
+     * @param string[] $steps
1076
+     */
1077
+    private static function setupLiveMigrations(string $appId, array $steps) {
1078
+        $queue = \OC::$server->getJobList();
1079
+        foreach ($steps as $step) {
1080
+            $queue->add('OC\Migration\BackgroundRepair', [
1081
+                'app' => $appId,
1082
+                'step' => $step]);
1083
+        }
1084
+    }
1085
+
1086
+    /**
1087
+     * @param string $appId
1088
+     * @return \OC\Files\View|false
1089
+     */
1090
+    public static function getStorage(string $appId) {
1091
+        if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1092
+            if (\OC::$server->getUserSession()->isLoggedIn()) {
1093
+                $view = new \OC\Files\View('/' . OC_User::getUser());
1094
+                if (!$view->file_exists($appId)) {
1095
+                    $view->mkdir($appId);
1096
+                }
1097
+                return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1098
+            } else {
1099
+                \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', ILogger::ERROR);
1100
+                return false;
1101
+            }
1102
+        } else {
1103
+            \OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', ILogger::ERROR);
1104
+            return false;
1105
+        }
1106
+    }
1107
+
1108
+    protected static function findBestL10NOption(array $options, string $lang): string {
1109
+        // only a single option
1110
+        if (isset($options['@value'])) {
1111
+            return $options['@value'];
1112
+        }
1113
+
1114
+        $fallback = $similarLangFallback = $englishFallback = false;
1115
+
1116
+        $lang = strtolower($lang);
1117
+        $similarLang = $lang;
1118
+        if (strpos($similarLang, '_')) {
1119
+            // For "de_DE" we want to find "de" and the other way around
1120
+            $similarLang = substr($lang, 0, strpos($lang, '_'));
1121
+        }
1122
+
1123
+        foreach ($options as $option) {
1124
+            if (is_array($option)) {
1125
+                if ($fallback === false) {
1126
+                    $fallback = $option['@value'];
1127
+                }
1128
+
1129
+                if (!isset($option['@attributes']['lang'])) {
1130
+                    continue;
1131
+                }
1132
+
1133
+                $attributeLang = strtolower($option['@attributes']['lang']);
1134
+                if ($attributeLang === $lang) {
1135
+                    return $option['@value'];
1136
+                }
1137
+
1138
+                if ($attributeLang === $similarLang) {
1139
+                    $similarLangFallback = $option['@value'];
1140
+                } elseif (strpos($attributeLang, $similarLang . '_') === 0) {
1141
+                    if ($similarLangFallback === false) {
1142
+                        $similarLangFallback = $option['@value'];
1143
+                    }
1144
+                }
1145
+            } else {
1146
+                $englishFallback = $option;
1147
+            }
1148
+        }
1149
+
1150
+        if ($similarLangFallback !== false) {
1151
+            return $similarLangFallback;
1152
+        } elseif ($englishFallback !== false) {
1153
+            return $englishFallback;
1154
+        }
1155
+        return (string) $fallback;
1156
+    }
1157
+
1158
+    /**
1159
+     * parses the app data array and enhanced the 'description' value
1160
+     *
1161
+     * @param array $data the app data
1162
+     * @param string $lang
1163
+     * @return array improved app data
1164
+     */
1165
+    public static function parseAppInfo(array $data, $lang = null): array {
1166
+        if ($lang && isset($data['name']) && is_array($data['name'])) {
1167
+            $data['name'] = self::findBestL10NOption($data['name'], $lang);
1168
+        }
1169
+        if ($lang && isset($data['summary']) && is_array($data['summary'])) {
1170
+            $data['summary'] = self::findBestL10NOption($data['summary'], $lang);
1171
+        }
1172
+        if ($lang && isset($data['description']) && is_array($data['description'])) {
1173
+            $data['description'] = trim(self::findBestL10NOption($data['description'], $lang));
1174
+        } elseif (isset($data['description']) && is_string($data['description'])) {
1175
+            $data['description'] = trim($data['description']);
1176
+        } else {
1177
+            $data['description'] = '';
1178
+        }
1179
+
1180
+        return $data;
1181
+    }
1182
+
1183
+    /**
1184
+     * @param \OCP\IConfig $config
1185
+     * @param \OCP\IL10N $l
1186
+     * @param array $info
1187
+     * @throws \Exception
1188
+     */
1189
+    public static function checkAppDependencies(\OCP\IConfig $config, \OCP\IL10N $l, array $info, bool $ignoreMax) {
1190
+        $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l);
1191
+        $missing = $dependencyAnalyzer->analyze($info, $ignoreMax);
1192
+        if (!empty($missing)) {
1193
+            $missingMsg = implode(PHP_EOL, $missing);
1194
+            throw new \Exception(
1195
+                $l->t('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s',
1196
+                    [$info['name'], $missingMsg]
1197
+                )
1198
+            );
1199
+        }
1200
+    }
1201 1201
 }
Please login to merge, or discard this patch.
Spacing   +37 added lines, -37 removed lines patch added patch discarded remove patch
@@ -138,7 +138,7 @@  discard block
 block discarded – undo
138 138
 				try {
139 139
 					self::loadApp($app);
140 140
 				} catch (\Throwable $e) {
141
-					\OC::$server->get(LoggerInterface::class)->emergency('Error during app loading: ' . $e->getMessage(), [
141
+					\OC::$server->get(LoggerInterface::class)->emergency('Error during app loading: '.$e->getMessage(), [
142 142
 						'exception' => $e,
143 143
 						'app' => $app,
144 144
 					]);
@@ -170,7 +170,7 @@  discard block
 block discarded – undo
170 170
 		$coordinator = \OC::$server->query(Coordinator::class);
171 171
 		$isBootable = $coordinator->isBootable($app);
172 172
 
173
-		$hasAppPhpFile = is_file($appPath . '/appinfo/app.php');
173
+		$hasAppPhpFile = is_file($appPath.'/appinfo/app.php');
174 174
 
175 175
 		if ($isBootable && $hasAppPhpFile) {
176 176
 			\OC::$server->getLogger()->error('/appinfo/app.php is not loaded when \OCP\AppFramework\Bootstrap\IBootstrap on the application class is used. Migrate everything from app.php to the Application class.', [
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 			\OC::$server->getLogger()->debug('/appinfo/app.php is deprecated, use \OCP\AppFramework\Bootstrap\IBootstrap on the application class instead.', [
181 181
 				'app' => $app,
182 182
 			]);
183
-			\OC::$server->getEventLogger()->start('load_app_' . $app, 'Load app: ' . $app);
183
+			\OC::$server->getEventLogger()->start('load_app_'.$app, 'Load app: '.$app);
184 184
 			try {
185 185
 				self::requireAppFile($app);
186 186
 			} catch (Throwable $ex) {
@@ -189,18 +189,18 @@  discard block
 block discarded – undo
189 189
 				}
190 190
 				if (!\OC::$server->getAppManager()->isShipped($app) && !self::isType($app, ['authentication'])) {
191 191
 					\OC::$server->getLogger()->logException($ex, [
192
-						'message' => "App $app threw an error during app.php load and will be disabled: " . $ex->getMessage(),
192
+						'message' => "App $app threw an error during app.php load and will be disabled: ".$ex->getMessage(),
193 193
 					]);
194 194
 
195 195
 					// Only disable apps which are not shipped and that are not authentication apps
196 196
 					\OC::$server->getAppManager()->disableApp($app, true);
197 197
 				} else {
198 198
 					\OC::$server->getLogger()->logException($ex, [
199
-						'message' => "App $app threw an error during app.php load: " . $ex->getMessage(),
199
+						'message' => "App $app threw an error during app.php load: ".$ex->getMessage(),
200 200
 					]);
201 201
 				}
202 202
 			}
203
-			\OC::$server->getEventLogger()->end('load_app_' . $app);
203
+			\OC::$server->getEventLogger()->end('load_app_'.$app);
204 204
 		}
205 205
 		$coordinator->bootApp($app);
206 206
 
@@ -267,7 +267,7 @@  discard block
 block discarded – undo
267 267
 	 * @param bool $force
268 268
 	 */
269 269
 	public static function registerAutoloading(string $app, string $path, bool $force = false) {
270
-		$key = $app . '-' . $path;
270
+		$key = $app.'-'.$path;
271 271
 		if (!$force && isset(self::$alreadyRegistered[$key])) {
272 272
 			return;
273 273
 		}
@@ -278,17 +278,17 @@  discard block
 block discarded – undo
278 278
 		$appNamespace = \OC\AppFramework\App::buildAppNamespace($app);
279 279
 		\OC::$server->registerNamespace($app, $appNamespace);
280 280
 
281
-		if (file_exists($path . '/composer/autoload.php')) {
282
-			require_once $path . '/composer/autoload.php';
281
+		if (file_exists($path.'/composer/autoload.php')) {
282
+			require_once $path.'/composer/autoload.php';
283 283
 		} else {
284
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\', $path . '/lib/', true);
284
+			\OC::$composerAutoloader->addPsr4($appNamespace.'\\', $path.'/lib/', true);
285 285
 			// Register on legacy autoloader
286 286
 			\OC::$loader->addValidRoot($path);
287 287
 		}
288 288
 
289 289
 		// Register Test namespace only when testing
290 290
 		if (defined('PHPUNIT_RUN') || defined('CLI_TEST_RUN')) {
291
-			\OC::$composerAutoloader->addPsr4($appNamespace . '\\Tests\\', $path . '/tests/', true);
291
+			\OC::$composerAutoloader->addPsr4($appNamespace.'\\Tests\\', $path.'/tests/', true);
292 292
 		}
293 293
 	}
294 294
 
@@ -300,7 +300,7 @@  discard block
 block discarded – undo
300 300
 	 */
301 301
 	private static function requireAppFile(string $app) {
302 302
 		// encapsulated here to avoid variable scope conflicts
303
-		require_once $app . '/appinfo/app.php';
303
+		require_once $app.'/appinfo/app.php';
304 304
 	}
305 305
 
306 306
 	/**
@@ -393,8 +393,8 @@  discard block
 block discarded – undo
393 393
 		} else {
394 394
 			$apps = $appManager->getEnabledAppsForUser($user);
395 395
 		}
396
-		$apps = array_filter($apps, function ($app) {
397
-			return $app !== 'files';//we add this manually
396
+		$apps = array_filter($apps, function($app) {
397
+			return $app !== 'files'; //we add this manually
398 398
 		});
399 399
 		sort($apps);
400 400
 		array_unshift($apps, 'files');
@@ -494,7 +494,7 @@  discard block
 block discarded – undo
494 494
 
495 495
 		$possibleApps = [];
496 496
 		foreach (OC::$APPSROOTS as $dir) {
497
-			if (file_exists($dir['path'] . '/' . $appId)) {
497
+			if (file_exists($dir['path'].'/'.$appId)) {
498 498
 				$possibleApps[] = $dir;
499 499
 			}
500 500
 		}
@@ -508,7 +508,7 @@  discard block
 block discarded – undo
508 508
 		} else {
509 509
 			$versionToLoad = [];
510 510
 			foreach ($possibleApps as $possibleApp) {
511
-				$version = self::getAppVersionByPath($possibleApp['path'] . '/' . $appId);
511
+				$version = self::getAppVersionByPath($possibleApp['path'].'/'.$appId);
512 512
 				if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) {
513 513
 					$versionToLoad = [
514 514
 						'dir' => $possibleApp,
@@ -538,7 +538,7 @@  discard block
 block discarded – undo
538 538
 		}
539 539
 
540 540
 		if (($dir = self::findAppInDirectories($appId)) != false) {
541
-			return $dir['path'] . '/' . $appId;
541
+			return $dir['path'].'/'.$appId;
542 542
 		}
543 543
 		return false;
544 544
 	}
@@ -553,7 +553,7 @@  discard block
 block discarded – undo
553 553
 	 */
554 554
 	public static function getAppWebPath(string $appId) {
555 555
 		if (($dir = self::findAppInDirectories($appId)) != false) {
556
-			return OC::$WEBROOT . $dir['url'] . '/' . $appId;
556
+			return OC::$WEBROOT.$dir['url'].'/'.$appId;
557 557
 		}
558 558
 		return false;
559 559
 	}
@@ -577,7 +577,7 @@  discard block
 block discarded – undo
577 577
 	 * @return string
578 578
 	 */
579 579
 	public static function getAppVersionByPath(string $path): string {
580
-		$infoFile = $path . '/appinfo/info.xml';
580
+		$infoFile = $path.'/appinfo/info.xml';
581 581
 		$appData = \OC::$server->getAppManager()->getAppInfo($infoFile, true);
582 582
 		return isset($appData['version']) ? $appData['version'] : '';
583 583
 	}
@@ -677,7 +677,7 @@  discard block
 block discarded – undo
677 677
 	 * @param string $page
678 678
 	 */
679 679
 	public static function registerAdmin(string $app, string $page) {
680
-		self::$adminForms[] = $app . '/' . $page . '.php';
680
+		self::$adminForms[] = $app.'/'.$page.'.php';
681 681
 	}
682 682
 
683 683
 	/**
@@ -686,7 +686,7 @@  discard block
 block discarded – undo
686 686
 	 * @param string $page
687 687
 	 */
688 688
 	public static function registerPersonal(string $app, string $page) {
689
-		self::$personalForms[] = $app . '/' . $page . '.php';
689
+		self::$personalForms[] = $app.'/'.$page.'.php';
690 690
 	}
691 691
 
692 692
 	/**
@@ -757,14 +757,14 @@  discard block
 block discarded – undo
757 757
 
758 758
 		foreach (OC::$APPSROOTS as $apps_dir) {
759 759
 			if (!is_readable($apps_dir['path'])) {
760
-				\OCP\Util::writeLog('core', 'unable to read app folder : ' . $apps_dir['path'], ILogger::WARN);
760
+				\OCP\Util::writeLog('core', 'unable to read app folder : '.$apps_dir['path'], ILogger::WARN);
761 761
 				continue;
762 762
 			}
763 763
 			$dh = opendir($apps_dir['path']);
764 764
 
765 765
 			if (is_resource($dh)) {
766 766
 				while (($file = readdir($dh)) !== false) {
767
-					if ($file[0] != '.' and is_dir($apps_dir['path'] . '/' . $file) and is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')) {
767
+					if ($file[0] != '.' and is_dir($apps_dir['path'].'/'.$file) and is_file($apps_dir['path'].'/'.$file.'/appinfo/info.xml')) {
768 768
 						$apps[] = $file;
769 769
 					}
770 770
 				}
@@ -798,12 +798,12 @@  discard block
 block discarded – undo
798 798
 			if (array_search($app, $blacklist) === false) {
799 799
 				$info = OC_App::getAppInfo($app, false, $langCode);
800 800
 				if (!is_array($info)) {
801
-					\OCP\Util::writeLog('core', 'Could not read app info file for app "' . $app . '"', ILogger::ERROR);
801
+					\OCP\Util::writeLog('core', 'Could not read app info file for app "'.$app.'"', ILogger::ERROR);
802 802
 					continue;
803 803
 				}
804 804
 
805 805
 				if (!isset($info['name'])) {
806
-					\OCP\Util::writeLog('core', 'App id "' . $app . '" has no name in appinfo', ILogger::ERROR);
806
+					\OCP\Util::writeLog('core', 'App id "'.$app.'" has no name in appinfo', ILogger::ERROR);
807 807
 					continue;
808 808
 				}
809 809
 
@@ -835,12 +835,12 @@  discard block
 block discarded – undo
835 835
 
836 836
 				$appPath = self::getAppPath($app);
837 837
 				if ($appPath !== false) {
838
-					$appIcon = $appPath . '/img/' . $app . '.svg';
838
+					$appIcon = $appPath.'/img/'.$app.'.svg';
839 839
 					if (file_exists($appIcon)) {
840
-						$info['preview'] = $urlGenerator->imagePath($app, $app . '.svg');
840
+						$info['preview'] = $urlGenerator->imagePath($app, $app.'.svg');
841 841
 						$info['previewAsIcon'] = true;
842 842
 					} else {
843
-						$appIcon = $appPath . '/img/app.svg';
843
+						$appIcon = $appPath.'/img/app.svg';
844 844
 						if (file_exists($appIcon)) {
845 845
 							$info['preview'] = $urlGenerator->imagePath($app, 'app.svg');
846 846
 							$info['previewAsIcon'] = true;
@@ -979,8 +979,8 @@  discard block
 block discarded – undo
979 979
 			return false;
980 980
 		}
981 981
 
982
-		if (is_file($appPath . '/appinfo/database.xml')) {
983
-			\OC::$server->getLogger()->error('The appinfo/database.xml file is not longer supported. Used in ' . $appId);
982
+		if (is_file($appPath.'/appinfo/database.xml')) {
983
+			\OC::$server->getLogger()->error('The appinfo/database.xml file is not longer supported. Used in '.$appId);
984 984
 			return false;
985 985
 		}
986 986
 
@@ -1017,10 +1017,10 @@  discard block
 block discarded – undo
1017 1017
 			\OC::$server->getConfig()->deleteAppValue($appId, 'ocsid');
1018 1018
 		}
1019 1019
 		foreach ($appData['remote'] as $name => $path) {
1020
-			\OC::$server->getConfig()->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
1020
+			\OC::$server->getConfig()->setAppValue('core', 'remote_'.$name, $appId.'/'.$path);
1021 1021
 		}
1022 1022
 		foreach ($appData['public'] as $name => $path) {
1023
-			\OC::$server->getConfig()->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
1023
+			\OC::$server->getConfig()->setAppValue('core', 'public_'.$name, $appId.'/'.$path);
1024 1024
 		}
1025 1025
 
1026 1026
 		self::setAppTypes($appId);
@@ -1090,17 +1090,17 @@  discard block
 block discarded – undo
1090 1090
 	public static function getStorage(string $appId) {
1091 1091
 		if (\OC::$server->getAppManager()->isEnabledForUser($appId)) { //sanity check
1092 1092
 			if (\OC::$server->getUserSession()->isLoggedIn()) {
1093
-				$view = new \OC\Files\View('/' . OC_User::getUser());
1093
+				$view = new \OC\Files\View('/'.OC_User::getUser());
1094 1094
 				if (!$view->file_exists($appId)) {
1095 1095
 					$view->mkdir($appId);
1096 1096
 				}
1097
-				return new \OC\Files\View('/' . OC_User::getUser() . '/' . $appId);
1097
+				return new \OC\Files\View('/'.OC_User::getUser().'/'.$appId);
1098 1098
 			} else {
1099
-				\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ', user not logged in', ILogger::ERROR);
1099
+				\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.', user not logged in', ILogger::ERROR);
1100 1100
 				return false;
1101 1101
 			}
1102 1102
 		} else {
1103
-			\OCP\Util::writeLog('core', 'Can\'t get app storage, app ' . $appId . ' not enabled', ILogger::ERROR);
1103
+			\OCP\Util::writeLog('core', 'Can\'t get app storage, app '.$appId.' not enabled', ILogger::ERROR);
1104 1104
 			return false;
1105 1105
 		}
1106 1106
 	}
@@ -1137,7 +1137,7 @@  discard block
 block discarded – undo
1137 1137
 
1138 1138
 				if ($attributeLang === $similarLang) {
1139 1139
 					$similarLangFallback = $option['@value'];
1140
-				} elseif (strpos($attributeLang, $similarLang . '_') === 0) {
1140
+				} elseif (strpos($attributeLang, $similarLang.'_') === 0) {
1141 1141
 					if ($similarLangFallback === false) {
1142 1142
 						$similarLangFallback = $option['@value'];
1143 1143
 					}
Please login to merge, or discard this patch.