[ 'adapter' => AdapterMySQL::class, 'charset' => 'UTF8', 'driver' => 'pdo_mysql', 'wrapperClass' => Connection::class, ], 'oci' => [ 'adapter' => AdapterOCI8::class, 'charset' => 'AL32UTF8', 'driver' => 'oci8', 'wrapperClass' => OracleConnection::class, ], 'pgsql' => [ 'adapter' => AdapterPgSql::class, 'driver' => 'pdo_pgsql', 'wrapperClass' => Connection::class, ], 'sqlite3' => [ 'adapter' => AdapterSqlite::class, 'driver' => 'pdo_sqlite', 'wrapperClass' => Connection::class, ], ]; private ShardConnectionManager $shardConnectionManager; private ICacheFactory $cacheFactory; public function __construct( private SystemConfig $config, ?ICacheFactory $cacheFactory = null, ) { if ($this->config->getValue('mysql.utf8mb4', false)) { $this->defaultConnectionParams['mysql']['charset'] = 'utf8mb4'; } $collationOverride = $this->config->getValue('mysql.collation', null); if ($collationOverride) { $this->defaultConnectionParams['mysql']['collation'] = $collationOverride; } $this->shardConnectionManager = new ShardConnectionManager($this->config, $this); $this->cacheFactory = $cacheFactory ?? Server::get(ICacheFactory::class); } /** * @brief Get default connection parameters for a given DBMS. * @param string $type DBMS type * @throws \InvalidArgumentException If $type is invalid * @return array Default connection parameters. */ public function getDefaultConnectionParams($type) { $normalizedType = $this->normalizeType($type); if (!isset($this->defaultConnectionParams[$normalizedType])) { throw new \InvalidArgumentException("Unsupported type: $type"); } $result = $this->defaultConnectionParams[$normalizedType]; // \PDO::MYSQL_ATTR_FOUND_ROWS may not be defined, e.g. when the MySQL // driver is missing. In this case, we won't be able to connect anyway. if ($normalizedType === 'mysql' && defined('\PDO::MYSQL_ATTR_FOUND_ROWS')) { $result['driverOptions'] = [ \PDO::MYSQL_ATTR_FOUND_ROWS => true, ]; } return $result; } /** * @brief Get default connection parameters for a given DBMS. * @param string $type DBMS type * @param array $additionalConnectionParams Additional connection parameters * @return \OC\DB\Connection */ public function getConnection(string $type, array $additionalConnectionParams): Connection { $normalizedType = $this->normalizeType($type); $eventManager = new EventManager(); $eventManager->addEventSubscriber(new SetTransactionIsolationLevel()); $connectionParams = $this->createConnectionParams('', $additionalConnectionParams, $type); switch ($normalizedType) { case 'pgsql': // pg_connect used by Doctrine DBAL does not support URI notation (enclosed in brackets) $matches = []; if (preg_match('/^\[([^\]]+)\]$/', $connectionParams['host'], $matches)) { // Host variable carries a port or socket. $connectionParams['host'] = $matches[1]; } break; case 'oci': $eventManager->addEventSubscriber(new OracleSessionInit); // the driverOptions are unused in dbal and need to be mapped to the parameters if (isset($connectionParams['driverOptions'])) { $connectionParams = array_merge($connectionParams, $connectionParams['driverOptions']); } $host = $connectionParams['host']; $port = $connectionParams['port'] ?? null; $dbName = $connectionParams['dbname']; // we set the connect string as dbname and unset the host to coerce doctrine into using it as connect string if ($host === '') { $connectionParams['dbname'] = $dbName; // use dbname as easy connect name } else { $connectionParams['dbname'] = '//' . $host . (!empty($port) ? ":{$port}" : '') . '/' . $dbName; } unset($connectionParams['host']); break; case 'sqlite3': $journalMode = $connectionParams['sqlite.journal_mode']; $connectionParams['platform'] = new OCSqlitePlatform(); $eventManager->addEventSubscriber(new SQLiteSessionInit(true, $journalMode)); break; } /** @var Connection $connection */ $connection = DriverManager::getConnection( $connectionParams, new Configuration(), $eventManager ); return $connection; } /** * @brief Normalize DBMS type * @param string $type DBMS type * @return string Normalized DBMS type */ public function normalizeType($type) { return $type === 'sqlite' ? 'sqlite3' : $type; } /** * Checks whether the specified DBMS type is valid. * * @param string $type * @return bool */ public function isValidType($type) { $normalizedType = $this->normalizeType($type); return isset($this->defaultConnectionParams[$normalizedType]); } /** * Create the connection parameters for the config */ public function createConnectionParams(string $configPrefix = '', array $additionalConnectionParams = [], ?string $type = null) { // use provided type or if null use type from config $type = $type ?? $this->config->getValue('dbtype', 'sqlite'); $connectionParams = array_merge($this->getDefaultConnectionParams($type), [ 'user' => $this->config->getValue($configPrefix . 'dbuser', $this->config->getValue('dbuser', '')), 'password' => $this->config->getValue($configPrefix . 'dbpassword', $this->config->getValue('dbpassword', '')), ]); $name = $this->config->getValue($configPrefix . 'dbname', $this->config->getValue('dbname', self::DEFAULT_DBNAME)); if ($this->normalizeType($type) === 'sqlite3') { $dataDir = $this->config->getValue('datadirectory', \OC::$SERVERROOT . '/data'); $connectionParams['path'] = $dataDir . '/' . $name . '.db'; } else { $host = $this->config->getValue($configPrefix . 'dbhost', $this->config->getValue('dbhost', '')); $connectionParams = array_merge($connectionParams, $this->splitHostFromPortAndSocket($host)); $connectionParams['dbname'] = $name; } $connectionParams['tablePrefix'] = $this->config->getValue('dbtableprefix', self::DEFAULT_DBTABLEPREFIX); $connectionParams['sqlite.journal_mode'] = $this->config->getValue('sqlite.journal_mode', 'WAL'); //additional driver options, eg. for mysql ssl $driverOptions = $this->config->getValue($configPrefix . 'dbdriveroptions', $this->config->getValue('dbdriveroptions', null)); if ($driverOptions) { $connectionParams['driverOptions'] = $driverOptions; } // set default table creation options $connectionParams['defaultTableOptions'] = [ 'collate' => 'utf8_bin', 'tablePrefix' => $connectionParams['tablePrefix'] ]; if ($type === 'mysql' && $this->config->getValue('mysql.utf8mb4', false)) { $connectionParams['defaultTableOptions'] = [ 'collate' => 'utf8mb4_bin', 'charset' => 'utf8mb4', 'tablePrefix' => $connectionParams['tablePrefix'] ]; } if ($this->config->getValue('dbpersistent', false)) { $connectionParams['persistent'] = true; } $connectionParams['sharding'] = $this->config->getValue('dbsharding', []); if (!empty($connectionParams['sharding'])) { $connectionParams['shard_connection_manager'] = $this->shardConnectionManager; $connectionParams['auto_increment_handler'] = new AutoIncrementHandler( $this->cacheFactory, $this->shardConnectionManager, ); } else { // just in case only the presence could lead to funny behaviour unset($connectionParams['sharding']); } $connectionParams = array_merge($connectionParams, $additionalConnectionParams); $replica = $this->config->getValue($configPrefix . 'dbreplica', $this->config->getValue('dbreplica', [])) ?: [$connectionParams]; return array_merge($connectionParams, [ 'primary' => $connectionParams, 'replica' => $replica, ]); } /** * @param string $host * @return array */ protected function splitHostFromPortAndSocket($host): array { $params = [ 'host' => $host, ]; $matches = []; if (preg_match('/^(.*):([^\]:]+)$/', $host, $matches)) { // Host variable carries a port or socket. $params['host'] = $matches[1]; if (is_numeric($matches[2])) { $params['port'] = (int)$matches[2]; } else { $params['unix_socket'] = $matches[2]; } } return $params; } } _age_rentention_files_versions Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/core/Controller/RecommendedAppsController.php
blob: fe3435d9be8a3086c6001a2e3b438091da429a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php

declare(strict_types=1);

/**
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OC\Core\Controller;

use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\StandaloneTemplateResponse;
use OCP\IInitialStateService;
use OCP\IRequest;

class RecommendedAppsController extends Controller {

	/** @var IInitialStateService */
	private $initialStateService;

	public function __construct(IRequest $request,
								IInitialStateService $initialStateService) {
		parent::__construct('core', $request);
		$this->initialStateService = $initialStateService;
	}

	/**
	 * @NoCSRFRequired
	 * @return Response
	 */
	public function index(): Response {
		$this->initialStateService->provideInitialState('core', 'defaultPageUrl', \OC_Util::getDefaultPageUrl());
		return new StandaloneTemplateResponse($this->appName, 'recommendedapps', [], 'guest');
	}

}