. */ // This script is disabled by default. Change below to true before running. $enabled = false; /* -------------------------------------------- */ /* no user modifications should be needed below */ if (version_compare(PHP_VERSION, '5.4.0', '<')) { die("Sorry, ulogger will not work with PHP version lower than 5.4 (you have " . PHP_VERSION . ")"); } define("ROOT_DIR", dirname(__DIR__)); require_once(ROOT_DIR . "/helpers/user.php"); require_once(ROOT_DIR . "/helpers/config.php"); require_once(ROOT_DIR . "/helpers/utils.php"); require_once(ROOT_DIR . "/helpers/lang.php"); $command = uUtils::postString('command'); $lang = (new uLang(uConfig::$lang))->getStrings(); $langSetup = (new uLang(uConfig::$lang))->getSetupStrings(); $prefix = preg_replace('/[^a-z0-9_]/i', '', uConfig::$dbprefix); $tPositions = $prefix . "positions"; $tTracks = $prefix . "tracks"; $tUsers = $prefix . "users"; $dbDriver = null; $messages = []; switch ($command) { case "setup": $error = false; try { $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 ) { $messages[] = "{$langSetup["dbconnectfailed"]}"; $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); $messages[] = $langSetup["checkdbsettings"]; break; } try { $queries = getQueries($pdo); foreach ($queries as $query) { $pdo->query($query); } } catch (PDOException $e) { $messages[] = "{$langSetup["dbqueryfailed"]}"; $messages[] = sprintf($langSetup["serversaid"], "" . $e->getMessage() . ""); $error = true; } $pdo = null; if (!$error) { $messages[] = "{$langSetup["dbtablessuccess"]}"; $messages[] = $langSetup["setupuser"]; $form = "
"; $form .= ""; $form .= ""; $form .= ""; $form .= "
"; $form .= "
"; $messages[] = $form; } break; case "adduser": $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"]}"; } break; default: $messages[] = "" . $langSetup["welcome"]; if (!isset($enabled) || $enabled === false) { $messages[] = sprintf($langSetup["disabledwarn"], "\$enabled", "true"); $messages[] = sprintf($langSetup["lineshouldread"], "
\$enabled = false;
", "
\$enabled = true;"); $messages[] = $langSetup["dorestart"]; $messages[] = "
"; break; } if (!function_exists('password_hash')) { $messages[] = $langSetup["passfuncwarn"]; $messages[] = $langSetup["passfunchack"]; $messages[] = sprintf($langSetup["lineshouldread"], "
//require_once(ROOT_DIR . \"/helpers/password.php\");
", "
require_once(ROOT_DIR . \"/helpers/password.php\");"); $messages[] = $langSetup["dorestart"]; $messages[] = "
"; break; } if (!uConfig::isFileLoaded()) { $messages[] = $langSetup["createconfig"]; $messages[] = $langSetup["dorestart"]; $messages[] = "
"; break; } if (empty(uConfig::$dbdsn) || ($dbDriver != "sqlite" && empty(uConfig::$dbuser))) { if ($dbDriver == "sqlite") { $required = "\$dbdsn"; } else { $required = "\$dbdsn, \$dbuser, \$dbpass"; } $messages[] = sprintf($langSetup["nodbsettings"], $required); $messages[] = $langSetup["dorestart"]; $messages[] = "
"; break; } $messages[] = sprintf($langSetup["scriptdesc"], "'$tPositions', '$tTracks', '$tUsers'", "" . getDbname(uConfig::$dbdsn) . ""); $messages[] = $langSetup["scriptdesc2"]; $messages[] = "
"; break; } function getQueries($pdo) { global $tPositions, $tUsers, $tTracks, $dbDriver; $queries = []; switch($dbDriver) { case "mysql": // users $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, `password` varchar(255) CHARACTER SET latin1 NOT NULL DEFAULT '' ) 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, `name` varchar(255) DEFAULT NULL, `comment` varchar(1024) DEFAULT NULL, INDEX `idx_user_id` (`user_id`), 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, `user_id` int(11) NOT NULL, `track_id` int(11) NOT NULL, `latitude` double NOT NULL, `longitude` double NOT NULL, `altitude` double DEFAULT NULL, `speed` double DEFAULT NULL, `bearing` double DEFAULT NULL, `accuracy` int(11) DEFAULT NULL, `provider` varchar(100) DEFAULT NULL, `comment` varchar(255) DEFAULT NULL, `image_id` int(11) DEFAULT NULL, INDEX `idx_track_id` (`track_id`), INDEX `idx_user_id` (`user_id`), FOREIGN KEY(`user_id`) REFERENCES `$tUsers`(`id`), FOREIGN KEY(`track_id`) REFERENCES `$tTracks`(`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8"; break; case "pgsql": // users $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, 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)"; // positions $queries[] = "DROP TABLE IF EXISTS $tPositions"; $queries[] = "CREATE TABLE $tPositions ( id SERIAL PRIMARY KEY, time TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP, user_id INT NOT NULL, track_id INT NOT NULL, latitude DOUBLE PRECISION NOT NULL, longitude DOUBLE PRECISION NOT NULL, altitude DOUBLE PRECISION DEFAULT NULL, speed DOUBLE PRECISION DEFAULT NULL, bearing DOUBLE PRECISION DEFAULT NULL, accuracy INT DEFAULT NULL, provider VARCHAR(100) DEFAULT NULL, comment VARCHAR(255) DEFAULT NULL, image_id INT DEFAULT NULL, 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; case "sqlite": // users $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` ( `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`)"; // positions $queries[] = "DROP TABLE IF EXISTS `$tPositions`"; $queries[] = "CREATE TABLE `$tPositions` ( `id` integer PRIMARY KEY AUTOINCREMENT, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, `user_id` integer NOT NULL, `track_id` integer NOT NULL, `latitude` double NOT NULL, `longitude` double NOT NULL, `altitude` double DEFAULT NULL, `speed` double DEFAULT NULL, `bearing` double DEFAULT NULL, `accuracy` integer DEFAULT NULL, `provider` varchar(100) DEFAULT NULL, `comment` varchar(255) DEFAULT NULL, `image_id` integer DEFAULT NULL, 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; default: throw InvalidArgumentException("Driver not supported"); } return $queries; } function getDbname($dsn) { if (strpos($dsn, ':') !== false) { list($scheme, $dsnWithoutScheme) = explode(':', $dsn, 2); switch ($scheme) { case 'sqlite': case 'sqlite2': case 'sqlite3': return $dsnWithoutScheme; break; default: $pattern = '~dbname=([^;]*)(?:;|$)~'; $result = preg_match($pattern, $dsnWithoutScheme, $matches); if ($result === 1 && !empty($matches[1])) { return $matches[1]; } break; } } return "noname"; } ?> <?= $lang["title"] ?>