The config file $cfg_file_name is already present";
return;
}
echo "Creating cfg file: " . $cfg_file_name;
// get the FSYNC_ROOT url
//
$fsRoot ="https://";
if ( ! isset($_SERVER['HTTPS']) ) {
$fsRoot = "http://";
}
$fsRoot .= $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . "/";
if( strpos( $_SERVER['REQUEST_URI'], 'index.php') !== 0 ) {
$fsRoot .= "index.php/";
}
// now build the content of the config file
//
$cfg_content = "\n";
// now write everything
//
$cfg_file = fopen($cfg_file_name, "a");
fputs($cfg_file, "$cfg_content");
fclose($cfg_file);
}
/*
print the html header for the form
*/
function print_header( $title ) {
if ( ! isset( $title ) ) {
$title = "";
}
print '' . $title . '
Setup FSyncMS
';
}
/*
print the html for for the mysql connection credentials
*/
function print_mysql_connection_form() {
print_header("MySQL database connection setup");
print 'MySQL database connection setup
Host
Instance name
Username
Password
';
print_footer();
}
// --------------------------------------------
// functions end
// --------------------------------------------
// check if we have no configuration at the moment
//
if ( file_exists("settings.php") && filesize( "settings.php" ) > 0 ) {
echo "
The setup looks like it's completed, else please delete settings.php
";
exit;
}
// inital page - select the database type
//
if ( ! $action ) {
// first check if we have pdo installed (untested)
//
if ( ! extension_loaded('PDO') ) {
print "ERROR - PDO is missing in the php installation!";
exit();
}
$validPdoDriver = 0;
print_header("Setup FSyncMS - DB Selection");
print 'Which database type should be used? ';
if ( extension_loaded('pdo_mysql') ) {
print ' MySQL ';
$validPdoDriver++;
} else {
print 'MySQL not possible (Driver missing) ';
}
if ( extension_loaded('pdo_sqlite') ) {
print ' SQLite ';
$validPdoDriver++;
} else {
print 'SQLite not possible (Driver missing) ';
}
if ( $validPdoDriver < 1 ) {
print ' No valid pdo driver found! Please install a valid pdo driver first ';
} else {
print '
';
}
// ensure we bail out at this point ;)
exit();
};
// step 2 (connection data) below
//
if ( $action == "step1" ) {
// now check if the database is in place
//
print_header("Setup FSyncMS - DB Setup: $dbType!");
switch ( $dbType ) {
case "sqlite":
$action = "step2";
break;
case "mysql":
print_mysql_connection_form();
break;
default:
print "ERROR - This type of database ($dbType) is not valid at the moment!";
exit();
break;
}
}
// now generate the database
//
if ( $action == "step2" ) {
$dbInstalled = false;
$dbHandle = null;
try {
if ( $dbType == "sqlite" ) {
$path = explode('/', $_SERVER['SCRIPT_FILENAME']);
$db_name = 'weave_db';
array_pop($path);
array_push($path, $db_name);
$db_name = implode('/', $path);
if ( file_exists($db_name) && filesize( $db_name ) > 0 ) {
$dbInstalled = true;
} else {
// echo("Creating sqlite weave storage: DBname". $db_name ." | username: ". $username);
// echo(" ");
$dbHandle = new PDO('sqlite:' . $db_name);
$dbHandle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
} else if ( $dbType == "mysql" ) {
$dbHandle = new PDO("mysql:host=". $dbHost .";dbname=". $dbName, $dbUser, $dbPass);
$select_stmt = "show tables like 'wbo'";
$sth = $dbHandle->prepare($select_stmt);
$sth->execute();
$count = $sth->rowCount();
if ( $count > 0 ) {
$dbInstalled = true;
}
};
} catch ( PDOException $exception ) {
echo("database unavailable " . $exception->getMessage());
throw new Exception("Database unavailable " . $exception->getMessage() , 503);
}
if ( $dbInstalled ) {
echo "DB is already installed! ";
} else {
echo "Now going to install the new database! Type is: $dbType ";
try {
$create_statement = " create table wbo ( username varchar(100), id varchar(65), collection varchar(100),
parentid varchar(65), predecessorid int, modified real, sortindex int,
payload text, payload_size int, ttl int, primary key (username,collection,id))";
$create_statement2 = " create table users ( username varchar(255), md5 varchar(64), primary key (username)) ";
$index1 = 'create index parentindex on wbo (username, parentid)';
$index2 = 'create index predecessorindex on wbo (username, predecessorid)';
$index3 = 'create index modifiedindex on wbo (username, collection, modified)';
$sth = $dbHandle->prepare($create_statement);
$sth->execute();
$sth = $dbHandle->prepare($create_statement2);
$sth->execute();
$sth = $dbHandle->prepare($index1);
$sth->execute();
$sth = $dbHandle->prepare($index2);
$sth->execute();
$sth = $dbHandle->prepare($index3);
$sth->execute();
echo "Database created ";
} catch( PDOException $exception ) {
throw new Exception("Database unavailable", 503);
}
}
// write settings.php, if not possible, display the needed contant
//
write_config_file($dbType, $dbHost, $dbName, $dbUser, $dbPass);
echo " Finished the setup, please delete setup.php and go on with the FFSync";
}
?>