Setup Page für FSyncMS

Initialer Commit
Stefan hat mir diese Erweiterung zukommen lassen,
all credits to him
balu-master
balu 12 years ago
parent f4fe3b46ed
commit 227dbe40a8

@ -0,0 +1,60 @@
Visit http://www.ohnekontur.de/2011/07/24/how-to-install-fsyncms-firefox-sync-eigener-server/ for install instructions
Visit http://www.ohnekontur.de for the newest version
FSyncMS v011
Added dedicated setup script, which will create the database and the config file: settings.php
~~~~~~~~~~ settings.php start ~~~~~~~~~~~~~~
<?php
//you can disable registration to the firefox sync server here,
// by setting ENABLE_REGISTER to false
//
//
//define("ENABLE_REGISTER",false);
define("ENABLE_REGISTER", true);
//pleas set the URL where firefox clients find the root of
// firefox sync server
// this should end with a /
//
define("FSYNCMS_ROOT","https://DOMAIN.de/Folder_und_ggf_/index.php/");
//MYSQL Params
define("MYSQL_ENABLE", false);
define("MYSQL_HOST","localhost");
define("MYSQL_DB","databaseName");
define("MYSQL_USER", "databaseUserName");
define("MYSQL_PASSWORD", "databaseUserPW");
?>
~~~~~~~~~~ settings.php end ~~~~~~~~~~
--------------------------------
FSyncMS v010
MYSQL Support
--------------------------------
FSyncMS v 09
Change Password now supported
working with firefox 12 (and lower)
Changelog:
Added change Password feature
-------------------------------
FSyncMS v 08
Should be working with firefox 11 and lower (tested with 11)
Changelog:
Fixed user registration process,
fixed some delete problems

@ -38,6 +38,19 @@
#
# ***** END LICENSE BLOCK *****
if ( ! file_exists("settings.php") && file_exists("setup.php") ) {
header( 'Location: setup.php' );
exit;
} else if ( ! file_exists("settings.php") ) {
echo "<hr><h2>Maybe the setup is not completed, missing settings.php</h2><hr>";
exit;
} else if ( file_exists("setup.php") ) {
echo "<hr><h2>Maybe the setup is not completed, else please delete setup.php</h2><hr>";
exit;
}
require_once 'weave_storage.php';
require_once 'weave_basic_object.php';
require_once 'weave_utils.php';
@ -48,7 +61,7 @@
$server_time = round(microtime(1), 2);
header("X-Weave-Timestamp: " . $server_time);
#Basic path extraction and validation. No point in going on if these are missing
# Basic path extraction and validation. No point in going on if these are missing
$path = '/';
if (!empty($_SERVER['PATH_INFO']))
$path = $_SERVER['PATH_INFO'];
@ -56,10 +69,12 @@
$path = $_SERVER['ORIG_PATH_INFO'];
else
report_problem("No path found", 404);
$path = substr($path, 1); #chop the lead slash
log_error("start request_____" . $path);
// ensure that we got a valid request
if ( !$path )
report_problem("Invalid request, this was not a firefox sync request!", 400);
list($version, $username, $function, $collection, $id) = explode('/', $path.'///');
if($version == 'user' || $version == 'misc')

@ -0,0 +1,401 @@
<?php
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# The contents of this file are subject to the Mozilla Public License Version
# 1.1 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
# http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
# for the specific language governing rights and limitations under the
# License.
#
# The Original Code is Weave Minimal Server
#
# The Initial Developer of the Original Code is
# Stefan Fischer
# Portions created by the Initial Developer are Copyright (C) 2012
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
#
# Alternatively, the contents of this file may be used under the terms of
# either the GNU General Public License Version 2 or later (the "GPL"), or
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
# in which case the provisions of the GPL or the LGPL are applicable instead
# of those above. If you wish to allow use of your version of this file only
# under the terms of either the GPL or the LGPL, and not to allow others to
# use your version of this file under the terms of the MPL, indicate your
# decision by deleting the provisions above and replace them with the notice
# and other provisions required by the GPL or the LGPL. If you do not delete
# the provisions above, a recipient may use your version of this file under
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
/*
TODO:
! - add license information
! - write settings.php
! - add form to select if we should use sql-lite or mysql
! - add form to store the mysql settings
! - check if the setup is already done
*/
// --------------------------------------------
// variables start
// --------------------------------------------
$action = null;
$dbType = null;
$dbUser = null;
$dbName = null;
$dbPass = null;
$dbHost = null;
// --------------------------------------------
// variables end
// --------------------------------------------
// --------------------------------------------
// post handling start
// --------------------------------------------
if ( isset( $_POST['action'] ) ) {
$action = check_input($_POST['action']);
}
if ( isset( $_POST['dbType'] ) ) {
$dbType = check_input($_POST['dbType']);
}
if ( isset( $_POST['dbhost'] ) ) {
$dbHost = check_input($_POST['dbhost']);
}
if ( isset( $_POST['dbname'] ) ) {
$dbName = check_input($_POST['dbname']);
}
if ( isset( $_POST['dbuser'] ) ) {
$dbUser = check_input($_POST['dbuser']);
}
if ( isset( $_POST['dbpass'] ) ) {
$dbPass = check_input($_POST['dbpass']);
}
// --------------------------------------------
// post handling end
// --------------------------------------------
// --------------------------------------------
// functions start
// --------------------------------------------
/*
ensure that the input is not total waste
*/
function check_input( $data ) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
/*
create the config file with the database type
and the given connection credentials
*/
function write_config_file($dbt, $dbh, $dbn, $dbu, $dbp) {
// construct the name of config file
//
$path = explode('/', $_SERVER['SCRIPT_FILENAME']);
array_pop($path);
array_push($path, 'settings.php');
$cfg_file_name = implode('/', $path);
if ( file_exists($cfg_file_name) && filesize( $cfg_file_name ) > 0 ) {
echo "<hr>The config file $cfg_file_name is already present</hr>";
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 = "<?php\n\n";
$cfg_content .= " // you can disable registration to the firefox sync server here,\n";
$cfg_content .= " // by setting ENABLE_REGISTER to false\n";
$cfg_content .= " // \n";
$cfg_content .= " define(\"ENABLE_REGISTER\", true);\n\n";
$cfg_content .= " // firefox sync server url, this should end with a /\n";
$cfg_content .= " // e.g. https://YourDomain.de/Folder_und_ggf_/index.php/\n";
$cfg_content .= " // \n";
$cfg_content .= " define(\"FSYNCMS_ROOT\", \"$fsRoot\");\n\n";
$cfg_content .= " // Database connection credentials\n";
$cfg_content .= " // \n";
if ( $dbt != "mysql" ) {
$cfg_content .= " define(\"MYSQL_ENABLE\", false);\n";
$cfg_content .= " define(\"MYSQL_HOST\", \"localhost\");\n";
$cfg_content .= " define(\"MYSQL_DB\", \"fsync\");\n";
$cfg_content .= " define(\"MYSQL_USER\", \"fsyncUserName\");\n";
$cfg_content .= " define(\"MYSQL_PASSWORD\", \"fsyncUserPassword\");\n";
} else {
$cfg_content .= " define(\"MYSQL_ENABLE\", true);\n";
$cfg_content .= " define(\"MYSQL_HOST\", \"$dbh\");\n";
$cfg_content .= " define(\"MYSQL_DB\", \"$dbn\");\n";
$cfg_content .= " define(\"MYSQL_USER\", \"$dbu\");\n";
$cfg_content .= " define(\"MYSQL_PASSWORD\", \"$dbp\");\n";
}
$cfg_content .= "\n?>\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 '<html><header><title>' . $title . '</title><body>
<h1>Setup FSyncMS</h1>
<form action="setup.php" method="post">';
}
/*
print the html footer
*/
function print_footer() {
print '</form></body></html>';
}
/*
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
<table>
<tr>
<td>Host</td>
<td><input type="text" name="dbhost" /></td>
</tr>
<tr>
<td>Instance name</td>
<td><input type="text" name="dbname" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="dbuser" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="dbpass" /></td>
</tr>
</table>
<input type="hidden" name="action" value="step2">
<input type="hidden" name="dbType" value="mysql">
<p><input type="submit" value="OK"></p>';
print_footer();
}
// --------------------------------------------
// functions end
// --------------------------------------------
// check if we have no configuration at the moment
//
if ( file_exists("settings.php") && filesize( "settings.php" ) > 0 ) {
echo "<hr><h2>The setup looks like it's completed, else please delete settings.php</h2><hr>";
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?<br>';
if ( extension_loaded('pdo_mysql') ) {
print '<input type="radio" name="dbType" value="mysql" /> MySQL <br>';
$validPdoDriver++;
} else {
print 'MySQL not possible (Driver missing) <br>';
}
if ( extension_loaded('pdo_sqlite') ) {
print '<input type="radio" name="dbType" value="sqlite" checked="checked" /> SQLite ';
$validPdoDriver++;
} else {
print 'SQLite not possible (Driver missing) <br>';
}
if ( $validPdoDriver < 1 ) {
print '<hr> No valid pdo driver found! Please install a valid pdo driver first <hr>';
} else {
print '<input type="hidden" name="action" value="step1">
<p><input type="submit" value="OK" /></p>';
}
// 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("<br>");
$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!<br>";
} else {
echo "Now going to install the new database! Type is: $dbType<br>";
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 <br>";
} 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 "<hr><hr> Finished the setup, please delete setup.php and go on with the FFSync<hr><hr>";
}
?>

@ -21,13 +21,13 @@
$path = $_SERVER['PATH_INFO'];
else if (!empty($_SERVER['ORIG_PATH_INFO']))
$path = $_SERVER['ORIG_PATH_INFO'];
else
else
{
log_error("user.php: No path found");
report_problem("No path found", 404);
}
$path = substr($path, 1); #chop the lead slash
list($preinstr,$version, $username, $function, $collection, $id) = explode('/', $path.'///');
list($preinstr, $version, $username, $function, $collection, $id) = explode('/', $path.'///');
log_error("Pfad:".$path);
if( $preinstr != 'user' && $preinstr != 'misc' )
report_problem('Function not found', 404);

@ -45,13 +45,16 @@ class WeaveStorage
private $_username;
private $_dbh;
function __construct($username)
function __construct($username)
{
$this->_username = $username;
$create_tables = false;
try
log_error("Initalizing DB connecion!");
try
{
if(!MYSQL_ENABLE)
if ( ! MYSQL_ENABLE )
{
$path = explode('/', $_SERVER['SCRIPT_FILENAME']);
$db_name = 'weave_db';
@ -59,40 +62,27 @@ class WeaveStorage
array_push($path, $db_name);
$db_name = implode('/', $path);
$create_tables = !file_exists($db_name);
log_error("Weaave Storage created : DBname".$db_name." | username:".$username);
if ( ! file_exists($db_name) )
{
report_error("The required sqllite database is not present! $db_name");
}
log_error("Starting SQLite connection");
$this->_dbh = new PDO('sqlite:' . $db_name);
$this->_dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
else if(MYSQL_ENABLE)
}
else if ( MYSQL_ENABLE )
{
log_error("MYSQL_ENABLE TRUE");
$this->_dbh = new PDO("mysql:host=".MYSQL_HOST.";dbname=".MYSQL_DB, MYSQL_USER, MYSQL_PASSWORD);
$select_stmt = "show tables like 'wbo'";
$sth = $this->_dbh->prepare($select_stmt);
$sth->execute();
$count = $sth->rowCount();
log_error("count".$count);
if(0 == $count)
{
log_error("CREATE TABLE");
$create_tables = true;
}
else
{
$create_tables = false;
}
log_error("Starting MySQL connection");
$this->_dbh = new PDO("mysql:host=". MYSQL_HOST .";dbname=". MYSQL_DB, MYSQL_USER, MYSQL_PASSWORD);
}
}
catch( PDOException $exception )
}
catch( PDOException $exception )
{
log_error("database unavailable " . $exception->getMessage());
throw new Exception("Database unavailable", 503);
}
if ($create_tables)
{
log_error("db create");
$this->setup_db();
throw new Exception("Database unavailable " . $exception->getMessage() , 503);
}
}
@ -269,7 +259,7 @@ class WeaveStorage
$params = array();
$update_list = array();
#make sure we have an id and collection. No point in continuing otherwise
#make sure we have an id and collection. No point in continuing otherwise
if (!$wbo->id() || !$wbo->collection())
{
error_log('Trying to update without a valid id or collection!');
@ -670,6 +660,7 @@ class WeaveStorage
{
return null;
}
function delete_storage($username)
{
log_error("delete storage");
@ -693,6 +684,7 @@ class WeaveStorage
return 1;
}
function delete_user($username)
{
log_error("delete User");
@ -726,19 +718,6 @@ class WeaveStorage
function create_user($username, $password)
{
log_error("Create User - Username: ".$username."|".$password);
/*try
{
$select_stmt = 'select username from users where username = :username';
$sth = $this->_dbh->prepare($select_stmt);
$username = $this->_username;
$sth->bindParam(':username', $username);
$sth->execute();
}
catch( PDOException $exception )
{
error_log("exists_user: " . $exception->getMessage());
throw new Exception("Database unavailable", 503);
}*/
try
{
@ -779,7 +758,7 @@ class WeaveStorage
return 1;
}
#function checks if user exists
#function checks if user exists
function exists_user()
{
try
@ -806,6 +785,7 @@ class WeaveStorage
function authenticate_user($password)
{
log_error("auth-user: " . $this->_username);
try
{
$select_stmt = 'select username from users where username = :username and md5 = :md5';
@ -830,81 +810,6 @@ class WeaveStorage
return 1;
}
function setup_db()
{
try
{
/* $create_statement ="
create table wbo
(
username text,
id text,
collection text,
parentid text,
predecessorid int,
modified real,
sortindex int,
payload text,
payload_size int,
ttl int,
primary key (username,collection,id)
)";*/
$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 text,
md5 text,
primary key (username)
)
";*/
$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 = $this->_dbh->prepare($create_statement);
$sth->execute();
$sth = $this->_dbh->prepare($create_statement2);
$sth->execute();
$sth = $this->_dbh->prepare($index1);
$sth->execute();
$sth = $this->_dbh->prepare($index2);
$sth->execute();
$sth = $this->_dbh->prepare($index3);
$sth->execute();
}
catch( PDOException $exception )
{
log_error("initialize_user_db:" . $exception->getMessage());
throw new Exception("Database unavailable", 503);
}
}
}

@ -15,13 +15,19 @@
define ('WEAVE_ERROR_NO_EMAIL', 12);
define ('WEAVE_ERROR_INVALID_COLLECTION', 13);
function log_error($msg)
define ('LOG_THE_ERROR', 0);
function log_error($msg)
{
# $datei = fopen("error.txt","a");
# fputs($datei,$msg."
# ");
#fputs($datei,"Server ".print_r( $_SERVER, true));
# fclose($datei);
if ( LOG_THE_ERROR == 1 )
{
$datei = fopen("/tmp/FSyncMS-error.txt","a");
$fmsg = sprintf("$msg\n");
fputs($datei,$fmsg);
fputs($datei,"Server ".print_r( $_SERVER, true));
fclose($datei);
}
}
function report_problem($message, $code = 503)
@ -118,7 +124,7 @@
$auth_pw = array_key_exists('PHP_AUTH_PW', $_SERVER) ? $_SERVER['PHP_AUTH_PW'] : null;
if (is_null($auth_user) || is_null($auth_pw))
{
{
/* CGI/FCGI auth workarounds */
$auth_str = null;
if (array_key_exists('Authorization', $_SERVER))
@ -152,11 +158,11 @@
}
}
if (!$auth_user || !$auth_pw) #do this first to avoid the cryptic error message if auth is missing
if ( ! $auth_user || ! $auth_pw) #do this first to avoid the cryptic error message if auth is missing
{
log_error("Auth failed{");
log_error(" User pw:".$auth_user."|".$auth_pw);
log_error(" Url_user:".$url_user);
log_error("Auth failed 1 {");
log_error(" User pw: ". $auth_user ." | ". $auth_pw);
log_error(" Url_user: ". $url_user);
log_error("}");
report_problem('Authentication failed', '401');
}
@ -169,11 +175,11 @@
try
{
if (!$db->authenticate_user(fix_utf8_encoding($auth_pw)))
if ( ! $db->authenticate_user(fix_utf8_encoding($auth_pw)) )
{
log_error("Auth failed{");
log_error(" User pw:".$auth_user."|".$auth_pw ."|md5:".md5($auth_pw)."|fix:".fix_utf8_encoding($auth_pw)."|fix md5". md5(fix_utf8_encoding($auth_pw)));
log_error(" Url_user:".$url_user);
log_error("Auth failed 2 {");
log_error(" User pw: ". $auth_user ."|".$auth_pw ."|md5:". md5($auth_pw) ."|fix:". fix_utf8_encoding($auth_pw) ."|fix md5 ". md5(fix_utf8_encoding($auth_pw)));
log_error(" Url_user: ".$url_user);
log_error("}");
report_problem('Authentication failed', '401');
}
@ -181,6 +187,7 @@
catch(Exception $e)
{
header("X-Weave-Backoff: 1800");
log_error($e->getMessage(), $e->getCode());
report_problem($e->getMessage(), $e->getCode());
}

Loading…
Cancel
Save