#!/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); } if(getenv('AUTH_METHOD') == "ldap") { $config['PLUGINS'] = 'auth_ldap, note'; $contents .= "define('LDAP_AUTH_SERVER_URI', '" . env("LDAP_AUTH_SERVER_URI", "ldap://ldap") . "');\n"; $contents .= "define('LDAP_AUTH_USETLS', " . env("LDAP_AUTH_USETLS", "FALSE") . "); \n"; $contents .= "define('LDAP_AUTH_ALLOW_UNTRUSTED_CERT', " . env("LDAP_AUTH_ALLOW_UNTRUSTED_CERT", "TRUE") . ");\n"; $contents .= "define('LDAP_AUTH_BASEDN', '" . env("LDAP_AUTH_BASEDN") . "');\n"; $contents .= "define('LDAP_AUTH_ANONYMOUSBEFOREBIND', " . env("LDAP_AUTH_ANONYMOUSBEFOREBIND", "FALSE") . ";\n"; // ??? will be replaced with the entered username(escaped) at login $contents .= "define('LDAP_AUTH_SEARCHFILTER', '" .env("LDAP_AUTH_SEARCHFILTER", "(&(objectClass=user)(sAMAccountName=???))") . "');\n"; $contents .= "define('LDAP_AUTH_BINDDN', '" . env("LDAP_AUTH_BINDDN") . "');\n"; $contents .= "define('LDAP_AUTH_BINDPW', '" . env("LDAP_AUTH_BINDPW") . "');\n"; $contents .= "define('LDAP_AUTH_LOGIN_ATTRIB', '" . env("LDAP_AUTH_LOGIN_ATTRIB", "sAMAccountName") . "');\n"; $contents .= "define('LDAP_AUTH_LOG_ATTEMPTS', " . env("LDAP_AUTH_LOG_ATTEMPTS", "FALSE") . ");\n"; $contents .= "define('LDAP_AUTH_DEBUG', " . env("LDAP_AUTH_DEBUG", "FALSE") . ");\n"; } file_put_contents($confpath, $contents); 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; } }