diff --git a/.docker/run.sh b/.docker/run.sh index 3e1856c..12807fb 100644 --- a/.docker/run.sh +++ b/.docker/run.sh @@ -10,6 +10,12 @@ sed -i "s/^\$gkey = .*$/\$gkey = \"${ULOGGER_GKEY}\";/" /var/www/html/config.php sed -i "s/^\$lang = .*$/\$lang = \"${ULOGGER_LANG}\";/" /var/www/html/config.php sed -i "s/^\$units = .*$/\$units = \"${ULOGGER_UNITS}\";/" /var/www/html/config.php +if [ "${ULOGGER_ENABLE_SETUP}" = "1" ]; then + sed -i "s/\$enabled = false;/\$enabled = true;/" /var/www/html/scripts/setup.php; + echo "ulogger setup script enabled" + echo "----------------------------" +fi + # show config variables echo "ulogger configuration" echo "---------------------" diff --git a/.tests/lib/BaseDatabaseTestCase.php b/.tests/lib/BaseDatabaseTestCase.php index cf7108f..35dc482 100644 --- a/.tests/lib/BaseDatabaseTestCase.php +++ b/.tests/lib/BaseDatabaseTestCase.php @@ -1,9 +1,17 @@ query("ALTER SEQUENCE tracks_id_seq RESTART WITH $tracks"); self::$pdo->query("ALTER SEQUENCE positions_id_seq RESTART WITH $positions"); } else if (self::$driver == "sqlite") { - self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'users'"); - self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'tracks'"); - self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'positions'"); + $retry = 1; + do { + try { + self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'users'"); + self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'tracks'"); + self::$pdo->query("DELETE FROM sqlite_sequence WHERE NAME = 'positions'"); + $retry = 0; + } catch (Exception $e) { + // sqlite raises error when db schema changes in another connection. + if (strpos($e->getMessage(), 'database schema has changed') !== false) { + self::$pdo = null; + self::setUpBeforeClass(); + } + } + } while ($retry--); } } diff --git a/.tests/lib/UloggerAPITestCase.php b/.tests/lib/UloggerAPITestCase.php index d22773e..c98334e 100644 --- a/.tests/lib/UloggerAPITestCase.php +++ b/.tests/lib/UloggerAPITestCase.php @@ -5,6 +5,9 @@ require_once("BaseDatabaseTestCase.php"); class UloggerAPITestCase extends BaseDatabaseTestCase { + /** + * @var null|GuzzleHttp\Client $http + */ protected $http = null; public function setUp() { diff --git a/.tests/lib/UloggerDatabaseTestCase.php b/.tests/lib/UloggerDatabaseTestCase.php index f47ad79..bf3ed07 100644 --- a/.tests/lib/UloggerDatabaseTestCase.php +++ b/.tests/lib/UloggerDatabaseTestCase.php @@ -6,6 +6,9 @@ require_once(__DIR__ . "/../../helpers/db.php"); class UloggerDatabaseTestCase extends BaseDatabaseTestCase { + /** + * @var uDb $udb + */ static private $udb = null; public static function setUpBeforeClass() { diff --git a/.travis.yml b/.travis.yml index 21fd9c0..b237bf1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,11 +25,11 @@ before_install: case "$DB_DSN" in mysql*) docker build -t ulogger --build-arg DB_DRIVER=mysql . - docker run -d --name ulogger -p 8080:80 -p 8081:3306 --expose 3306 ulogger + docker run -d --name ulogger -p 8080:80 -p 8081:3306 --expose 3306 -e ULOGGER_ENABLE_SETUP=1 ulogger ;; pgsql*) docker build -t ulogger --build-arg DB_DRIVER=pgsql . - docker run -d --name ulogger -p 8080:80 -p 8081:5432 --expose 5432 ulogger + docker run -d --name ulogger -p 8080:80 -p 8081:5432 --expose 5432 -e ULOGGER_ENABLE_SETUP=1 ulogger ;; sqlite*) sudo apt-get update -qq @@ -39,7 +39,7 @@ before_install: sudo chown -R travis:travis /tmp/data sudo chmod -R 777 /tmp/data docker build -t ulogger --build-arg DB_DRIVER=sqlite . - docker run -d --name ulogger -p 8080:80 -v /tmp/data:/data ulogger + docker run -d --name ulogger -p 8080:80 -v /tmp/data:/data -e ULOGGER_ENABLE_SETUP=1 ulogger ;; esac - composer install @@ -68,4 +68,4 @@ addons: notification_email: scan.coverity@fabiszewski.net build_command_prepend: "" build_command: "--no-command --fs-capture-search ./ --fs-capture-search-exclude-regex vendor/ --fs-capture-search-exclude-regex .tests/ --fs-capture-search-exclude-regex config.php --fs-capture-search-exclude-regex .docker/" - branch_pattern: master \ No newline at end of file + branch_pattern: master diff --git a/Dockerfile b/Dockerfile index b71557f..d33ae33 100644 --- a/Dockerfile +++ b/Dockerfile @@ -16,6 +16,7 @@ ENV ULOGGER_GKEY "" ENV ULOGGER_LANG en ENV ULOGGER_UNITS metric ENV ULOGGER_DB_DRIVER ${DB_DRIVER} +ENV ULOGGER_ENABLE_SETUP 0 ENV LANG=en_US.utf-8 diff --git a/composer.json b/composer.json index f53bf7f..4561df0 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,9 @@ { "require": { - "ulrichsg/getopt-php": "^3.2" + "ulrichsg/getopt-php": "^3.2", + "ext-json": "*", + "ext-pdo": "*", + "ext-xmlwriter": "*" }, "scripts": { "test": "./vendor/bin/phpunit" diff --git a/lang/en.php b/lang/en.php index 6b3235b..05ea3c6 100644 --- a/lang/en.php +++ b/lang/en.php @@ -43,6 +43,8 @@ $langSetup["scriptdesc"] = "This script will set up tables needed for µlogger ( $langSetup["scriptdesc2"] = "When done the script will ask you to provide user name and password for your µlogger user."; $langSetup["startbutton"] = "Press to start"; $langSetup["restartbutton"] = "Restart"; +$langSetup["optionwarn"] = "PHP configuration option %s must be set to %s."; // substitutes option name and value +$langSetup["extensionwarn"] = "Required PHP extension %s is not available."; // substitutes extension name // application strings diff --git a/scripts/setup.php b/scripts/setup.php index 9481710..b606431 100644 --- a/scripts/setup.php +++ b/scripts/setup.php @@ -54,21 +54,21 @@ switch ($command) { $options = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ]; $pdo = new PDO(uConfig::$dbdsn, uConfig::$dbuser, uConfig::$dbpass, $options); $dbDriver = $pdo->getAttribute(PDO::ATTR_DRIVER_NAME); - } catch (PDOException $e ) { + } catch (PDOException $e) { $messages[] = "{$langSetup["dbconnectfailed"]}"; $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); $messages[] = $langSetup["checkdbsettings"]; break; } try { - $queries = getQueries($pdo); + $queries = getQueries(); foreach ($queries as $query) { $pdo->query($query); } } catch (PDOException $e) { - $messages[] = "{$langSetup["dbqueryfailed"]}"; - $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); - $error = true; + $messages[] = "{$langSetup["dbqueryfailed"]}"; + $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); + $error = true; } $pdo = null; if (!$error) { @@ -85,21 +85,21 @@ switch ($command) { break; case "adduser": - $login = uUtils::postString('login'); - $pass = uUtils::postPass('pass'); + $login = uUtils::postString('login'); + $pass = uUtils::postPass('pass'); - if (uUser::add($login, $pass) !== false) { - $messages[] = "{$langSetup["congratulations"]}"; - $messages[] = $langSetup["setupcomplete"]; - $messages[] = "{$langSetup["disablewarn"]}
"; - $messages[] = sprintf($langSetup["disabledesc"], "\$enabled", "false"); - } else { - $messages[] = "{$langSetup["setupfailed"]}"; - } + if (uUser::add($login, $pass) !== false) { + $messages[] = "{$langSetup["congratulations"]}"; + $messages[] = $langSetup["setupcomplete"]; + $messages[] = "{$langSetup["disablewarn"]}
"; + $messages[] = sprintf($langSetup["disabledesc"], "\$enabled", "false"); + } else { + $messages[] = "{$langSetup["setupfailed"]}"; + } break; default: - $messages[] = "" . $langSetup["welcome"]; + $messages[] = "\"µLogger\"" . $langSetup["welcome"]; if (!isset($enabled) || $enabled === false) { $messages[] = sprintf($langSetup["disabledwarn"], "\$enabled", "true"); $messages[] = sprintf($langSetup["lineshouldread"], "
\$enabled = false;
", "
\$enabled = true;"); @@ -132,20 +132,34 @@ switch ($command) { $messages[] = "
"; break; } + if (ini_get("session.auto_start") == '1') { + $messages[] = sprintf($langSetup["optionwarn"], "session.auto_start", "0 (off)"); + $messages[] = $langSetup["dorestart"]; + $messages[] = "
"; + break; + } + if (!extension_loaded("pdo")) { + $messages[] = sprintf($langSetup["extensionwarn"], "PDO"); + $messages[] = $langSetup["dorestart"]; + $messages[] = "
"; + break; + } $messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "" . getDbname(uConfig::$dbdsn) . ""); $messages[] = $langSetup["scriptdesc2"]; $messages[] = "
"; break; } -function getQueries($pdo) { +function getQueries() { global $tPositions, $tUsers, $tTracks, $dbDriver; $queries = []; - switch($dbDriver) { + switch ($dbDriver) { case "mysql": - // users + $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; + $queries[] = "DROP TABLE IF EXISTS `$tTracks`"; $queries[] = "DROP TABLE IF EXISTS `$tUsers`"; + $queries[] = "CREATE TABLE `$tUsers` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `login` varchar(15) CHARACTER SET latin1 NOT NULL UNIQUE, @@ -153,8 +167,6 @@ function getQueries($pdo) { ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; - // tracks - $queries[] = "DROP TABLE IF EXISTS `$tTracks`"; $queries[] = "CREATE TABLE `$tTracks` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `user_id` int(11) NOT NULL, @@ -164,8 +176,6 @@ function getQueries($pdo) { FOREIGN KEY(`user_id`) REFERENCES `$tUsers`(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; - // positions - $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; $queries[] = "CREATE TABLE `$tPositions` ( `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, @@ -188,16 +198,16 @@ function getQueries($pdo) { break; case "pgsql": - // users + $queries[] = "DROP TABLE IF EXISTS $tPositions"; + $queries[] = "DROP TABLE IF EXISTS $tTracks"; $queries[] = "DROP TABLE IF EXISTS $tUsers"; + $queries[] = "CREATE TABLE $tUsers ( id SERIAL PRIMARY KEY, login VARCHAR(15) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL DEFAULT '' )"; - // tracks - $queries[] = "DROP TABLE IF EXISTS $tTracks"; $queries[] = "CREATE TABLE $tTracks ( id SERIAL PRIMARY KEY, user_id INT NOT NULL, @@ -207,8 +217,6 @@ function getQueries($pdo) { )"; $queries[] = "CREATE INDEX idx_user_id ON $tTracks(user_id)"; - // positions - $queries[] = "DROP TABLE IF EXISTS $tPositions"; $queries[] = "CREATE TABLE $tPositions ( id SERIAL PRIMARY KEY, time TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, @@ -228,30 +236,28 @@ function getQueries($pdo) { )"; $queries[] = "CREATE INDEX idx_ptrack_id ON $tPositions(track_id)"; $queries[] = "CREATE INDEX idx_puser_id ON $tPositions(user_id)"; - break; + break; - case "sqlite": - // users - $queries[] = "DROP TABLE IF EXISTS `$tUsers`"; - $queries[] = "CREATE TABLE `$tUsers` ( + case "sqlite": + $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; + $queries[] = "DROP TABLE IF EXISTS `$tTracks`"; + $queries[] = "DROP TABLE IF EXISTS `$tUsers`"; + + $queries[] = "CREATE TABLE `$tUsers` ( `id` integer PRIMARY KEY AUTOINCREMENT, `login` varchar(15) NOT NULL UNIQUE, `password` varchar(255) NOT NULL DEFAULT '' )"; - // tracks - $queries[] = "DROP TABLE IF EXISTS `$tTracks`"; - $queries[] = "CREATE TABLE `$tTracks` ( + $queries[] = "CREATE TABLE `$tTracks` ( `id` integer PRIMARY KEY AUTOINCREMENT, `user_id` integer NOT NULL, `name` varchar(255) DEFAULT NULL, `comment` varchar(1024) DEFAULT NULL, FOREIGN KEY(`user_id`) REFERENCES `$tUsers`(`id`) )"; - $queries[] = "CREATE INDEX `idx_user_id` ON `$tTracks`(`user_id`)"; + $queries[] = "CREATE INDEX `idx_user_id` ON `$tTracks`(`user_id`)"; - // positions - $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; - $queries[] = "CREATE TABLE `$tPositions` ( + $queries[] = "CREATE TABLE `$tPositions` ( `id` integer PRIMARY KEY AUTOINCREMENT, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `user_id` integer NOT NULL, @@ -268,12 +274,12 @@ function getQueries($pdo) { FOREIGN KEY(`user_id`) REFERENCES `$tUsers`(`id`), FOREIGN KEY(`track_id`) REFERENCES `$tTracks`(`id`) )"; - $queries[] = "CREATE INDEX `idx_ptrack_id` ON `$tPositions`(`track_id`)"; - $queries[] = "CREATE INDEX `idx_puser_id` ON `$tPositions`(`user_id`)"; - break; + $queries[] = "CREATE INDEX `idx_ptrack_id` ON `$tPositions`(`track_id`)"; + $queries[] = "CREATE INDEX `idx_puser_id` ON `$tPositions`(`user_id`)"; + break; - default: - throw InvalidArgumentException("Driver not supported"); + default: + throw new InvalidArgumentException("Driver not supported"); } return $queries; } @@ -292,7 +298,7 @@ function getDbname($dsn) { $pattern = '~dbname=([^;]*)(?:;|$)~'; $result = preg_match($pattern, $dsnWithoutScheme, $matches); if ($result === 1 && !empty($matches[1])) { - return $matches[1]; + return $matches[1]; } break; } @@ -303,72 +309,76 @@ function getDbname($dsn) { ?> - - - <?= $lang["title"] ?> - - - - - - - + #message img { + vertical-align: bottom; + } - -
- -

- -
- + #message input[type=text], #message input[type=password] { + width: 40em; + padding: 0.4em; + margin: 0.8em 0; + display: block; + border: 1px solid #ccc; + box-sizing: border-box; + border-radius: 5px; + -moz-border-radius: 5px; + -webkit-border-radius: 5px; + } + + .warn { + color: #ffc747; + } + + .ok { + color: #00e700; + } + + + + + +
+ +

+ +
+ \ No newline at end of file