File indexing completed on 2024-12-15 03:45:05

0001 <?php
0002 /*
0003     SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>
0004 
0005     SPDX-License-Identifier: MIT
0006 */
0007 
0008 /** Database configuration. */
0009 class Config
0010 {
0011 
0012 private $type = null;
0013 private $name = null;
0014 private $host = null;
0015 private $port = null;
0016 private $username = null;
0017 private $password = null;
0018 
0019 public function __construct()
0020 {
0021     // allow test code to override this without requiring a localconfig.php
0022     global $USERFEEDBACK_DB_DRIVER;
0023     global $USERFEEDBACK_DB_NAME;
0024 
0025     if (file_exists(__DIR__ . '/../config/localconfig.php'))
0026         include_once(__DIR__ . '/../config/localconfig.php');
0027 
0028     if (isset($USERFEEDBACK_DB_DRIVER))
0029         $this->type = strval($USERFEEDBACK_DB_DRIVER);
0030     else
0031         throw new RESTException('Missing database configuration!', 500);
0032 
0033     if (isset($USERFEEDBACK_DB_NAME))
0034         $this->name = strval($USERFEEDBACK_DB_NAME);
0035     if (isset($USERFEEDBACK_DB_HOST))
0036         $this->host = strval($USERFEEDBACK_DB_HOST);
0037     if (isset($USERFEEDBACK_DB_PORT))
0038         $this->port = intval($USERFEEDBACK_DB_PORT);
0039     if (isset($USERFEEDBACK_DB_USERNAME))
0040         $this->username = strval($USERFEEDBACK_DB_USERNAME);
0041     if (isset($USERFEEDBACK_DB_PASSWORD))
0042         $this->password = strval($USERFEEDBACK_DB_PASSWORD);
0043 
0044     if ($this->type == 'sqlite') {
0045         $dirName = dirname($this->name);
0046         if (!file_exists($dirName))
0047             mkdir(dirname($this->name), $mode = 0700, $recursive = true);
0048     }
0049 }
0050 
0051 /** PDO DSN */
0052 public function dsn()
0053 {
0054     switch ($this->type) {
0055         case 'sqlite':
0056             return 'sqlite:' . $this->name;
0057         case 'mysql':
0058             $s = 'mysql:';
0059             // TODO: support unix_socket=
0060             if ($this->host) $s .= 'host=' . $this->host . ';';
0061             if ($this->port) $s .= 'port=' . $this->port . ';';
0062             if (is_null($this->name)) break;
0063             $s .= 'dbname=' . $this->name;
0064             return $s;
0065         case 'pgsql':
0066             $s = 'pgsql:';
0067             if ($this->host) $s .= 'host=' . $this->host . ';';
0068             if ($this->port) $s .= 'port=' . $this->port . ';';
0069             if (is_null($this->name)) break;
0070             $s .= 'dbname=' . $this->name;
0071             return $s;
0072     }
0073     throw new RESTException('Invalid database configuration!', 500);
0074 }
0075 
0076 /** Database user name. */
0077 public function username()
0078 {
0079     return $this->username;
0080 }
0081 
0082 /** Database password. */
0083 public function password()
0084 {
0085     return $this->password;
0086 }
0087 
0088 }
0089 
0090 ?>