#!/usr/bin/env php exec('CREATE DATABASE ' . ($config['DB_NAME'])); $pdo->exec('GRANT ALL PRIVILEGES ON ' . ($config['DB_NAME']) . '.* TO ' . $pdo->quote($config['DB_USER']) . '@"%" IDENTIFIED BY ' . $pdo->quote($config['DB_PASS'])); } else { $pdo->exec('CREATE ROLE ' . ($config['DB_USER']) . ' WITH LOGIN PASSWORD ' . $pdo->quote($config['DB_PASS'])); $pdo->exec('CREATE DATABASE ' . ($config['DB_NAME']) . ' WITH OWNER ' . ($config['DB_USER'])); } unset($pdo); if (dbcheck($config)) { echo 'Database login created and confirmed' . PHP_EOL; } else { error('Database login failed, trying to create login failed as well'); } } $pdo = dbconnect($config); try { $pdo->query('SELECT 1 FROM ttrss_feeds'); // reached this point => table found, assume db is complete } catch (PDOException $e) { echo 'Database table not found, applying schema... ' . PHP_EOL; $schema = file_get_contents('schema/ttrss_schema_' . $config['DB_TYPE'] . '.sql'); $schema = preg_replace('/--(.*?);/', '', $schema); $schema = preg_replace('/[\r\n]/', ' ', $schema); $schema = trim($schema, ' ;'); foreach (explode(';', $schema) as $stm) { $pdo->exec($stm); } unset($pdo); } $contents = file_get_contents($confpath); foreach ($config as $name => $value) { $contents = preg_replace('/(define\s*\(\'' . $name . '\',\s*)(.*)(\);)/', '$1"' . $value . '"$3', $contents); } file_put_contents($confpath, $contents); $urlParts = parse_url($config['SELF_URL_PATH']); if( $urlParts['path'] != '' && $urlParts['path'] != '/' ) { $nginx = 'server { listen 80; index index.php index.html; location / { rewrite ^ ' . $config['SELF_URL_PATH'] . '; } location ' . rtrim($urlParts['path'], '/') . ' { alias /var/www; try_files $uri $uri/ =404; location ~ \.php$ { allow all; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } }'; file_put_contents("/etc/nginx/sites-enabled/ttrss", $nginx); } function env($name, $default = null) { $v = getenv($name) ?: $default; if ($v === null) { error('The env ' . $name . ' does not exist'); } return $v; } function error($text) { echo 'Error: ' . $text . PHP_EOL; exit(1); } function dbconnect($config) { $map = array('host' => 'HOST', 'port' => 'PORT', 'dbname' => 'NAME'); $dsn = $config['DB_TYPE'] . ':'; foreach ($map as $d => $h) { if (isset($config['DB_' . $h])) { $dsn .= $d . '=' . $config['DB_' . $h] . ';'; } } $pdo = new \PDO($dsn, $config['DB_USER'], $config['DB_PASS']); $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); return $pdo; } function dbcheck($config) { try { dbconnect($config); return true; } catch (PDOException $e) { return false; } }