From 3263b80ea94a2d3c8a7d68d8190a93db198e2203 Mon Sep 17 00:00:00 2001
From: Robin Appelman <icewind1991@gmail.com>
Date: Sun, 17 Apr 2011 00:45:05 +0200
Subject: [PATCH] first-run wizzard wip

---
 img/task-attention.png     | Bin 0 -> 1696 bytes
 index.php                  |   7 +++
 js/js.js                   |  18 ++++++
 lib/installer.php          | 123 +++++++++++++++++++++++++++++++++++++
 templates/error.php        |   2 +-
 templates/installation.php |  52 ++++++++++++++++
 6 files changed, 201 insertions(+), 1 deletion(-)
 create mode 100644 img/task-attention.png
 create mode 100644 lib/installer.php
 create mode 100644 templates/installation.php

diff --git a/img/task-attention.png b/img/task-attention.png
new file mode 100644
index 0000000000000000000000000000000000000000..35a112e9d53429da221cfd0b7110a6eae0594b85
GIT binary patch
literal 1696
zcmeAS@N?(olHy`uVBq!ia0y~yU{C;I4mJh`hT^KKFANL}jKx9jP7LeL$-HD>U|>t~
zc6VX;4}uH!E}zW6z`$AH5n0T@z<V8p8J#p{S1>Rzu$OrHy0YJ5ljY_T*y;a+g@J)h
z!PCVtB;xSZX|Xx7?vicu^V84YEbcqM(Ld>UVNOt!^rRDvx0ZEft`y3eI_X1jr_f4)
zz>Cwkx|CKpWmGk`L@l{jaG16HPi%psi=fj~E<vY-f39$5YA#*Uvc!-x%<Esy@xJr#
zKK{IU_velqr8RG?R!$aD36nbe=FOQs@AuvReU|5sz1^zrFK_qn+j=LP?Z4cC^%tja
zufDRQQ%7=BQB>Y_hyLcAAMqLm6KZVsypjGmvv%r-3ws;oCdj`^T>iT1TG2YrA3sC>
z^%SR<l)cjI+Af}Yb#d!4*4il^Wv>d)@Tw|&XFPmKFm}rN;5&0l7tNWm&G*q4TZf8?
zcUjU*8|-Qqi~KN~ke+QLT7Ty5hbdWqnXVi!xc*LU)seqfe$0_OFjd;Mp?Zn&tb%VR
zPVGK=a*Y7za>Y**pQ#%CoqxT+t30si*ZL`%?>icPaV-2S7~T8++?t<VLcNW@8u$xY
zFK>3v(b#M9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xr8_if8I>?w<bOXMXC>gUXis{s;8M
z+fDYp;^Aoc`D#$>Hfx=)n~f6AZ*Y#EyL;E!nFX^;e(1YZ-~H#?xBJfu6*E8g8LYYN
zet)hl-OKV;)uQmgT=Cznt~woZ%X2@N&8;&1C-rt&e%bDFh7ys3Gjy*8zuVTQJ27j|
zfq-qPj@&LAeqI*5CcZ{L=boUW@=c|VOG4YP6n<K35i$K)#5dcAg1;B+n-FuC#o_<6
zDeB*EKc90o+Ac<}%8+r_q@QWt(mng<aqLJsaL9)%C7R>>w#Pg7J@^r8_4#=7^Ih`B
z<tNX6%vcmV`^(Y9kJnP8b#E~kzHA9&%6t{e7sVX*Wm($1s0A02ST22kl)I4S@Z7yO
zPBPwg_DX2mv0<9lraiG;*{qLMG7Y)cWxf%wexsC_rFuEa-OxpQt3d%z)~yHDo23I)
z>o%_~f95{@n|$4-x>Zk1&-*r9{dufv_v@=`oWgg-FWB)^DDJ-ZrHv~;3cq^RA)s6(
zlA`>=ak<YuzDFl_7xXUK7&eEs-tzW(-2>-RCt3A>ktwrsUnuDxR2=s(wK}dW)=cq)
z?#p#weOulhVEHgR<?z8}mT?)8HDT90A8b7`CI9A==-My;p3Iq`{`~b9`JJ0iBsl9B
SJYryAVDNPHb6Mw<&;$TxeLm3u

literal 0
HcmV?d00001

diff --git a/index.php b/index.php
index b0d48b4acf..b8e1cb24dd 100644
--- a/index.php
+++ b/index.php
@@ -29,6 +29,13 @@ require_once( 'template.php' );
 $errors=OC_UTIL::checkServer();
 if(count($errors)>0){
 	OC_TEMPLATE::printGuestPage( "", "error", array( "errors" => $errors ));
+}elseif(isset($_POST['install']) and $_POST['install']=='true'){
+	require_once 'installer.php';
+}elseif (!OC_CONFIG::getValue('installed',false)) {
+	$hasSQLite=is_callable('sqlite_open');
+	$hasMySQL=is_callable('mysql_connect');
+	$datadir=OC_CONFIG::getValue('datadir',$SERVERROOT.'/data');
+	OC_TEMPLATE::printGuestPage( "", "installation",array('hasSQLite'=>$hasSQLite,'hasMySQL'=>$hasMySQL,'datadir'=>$datadir));
 }elseif( OC_USER::isLoggedIn()){
 	if( isset($_GET["logout"]) and ($_GET["logout"]) ){
 		OC_USER::logout();
diff --git a/js/js.js b/js/js.js
index 244433184f..86f5885ce6 100644
--- a/js/js.js
+++ b/js/js.js
@@ -9,4 +9,22 @@ $(document).ready(function() {
         $('#user_menu').slideToggle(250);
         return false;
     });
+	
+	//hide the advanced config
+	$('#advanced_options').hide();
+	$('#use_mysql').hide();
+	
+	// Sets advanced_options link behaviour :
+	$('#advanced_options_link').click(function() {
+		$('#advanced').toggleClass('userLinkOn');
+		$('#advanced_options').slideToggle(250);
+		return false;
+	});
+	
+	$('#mysql').click(function() {
+		$('#use_mysql').slideDown(250);
+	});
+	$('#sqlite').click(function() {
+		$('#use_mysql').slideUp(250);
+	});
 });
diff --git a/lib/installer.php b/lib/installer.php
new file mode 100644
index 0000000000..a87e7541fc
--- /dev/null
+++ b/lib/installer.php
@@ -0,0 +1,123 @@
+<?php
+
+if(isset($_POST['install']) and $_POST['install']=='true'){
+	$errors=OC_INSTALLER::install($_POST);
+	if(count($errors)>0){
+		OC_TEMPLATE::printGuestPage( "", "error", array( "errors" => $errors ));
+	}else{
+		header( "Location: $WEBROOT");
+		exit();
+	}
+}
+
+class OC_INSTALLER{
+	public static function install($options){
+		$error=array();
+		$dbtype=$options['dbtype'];
+		if(empty($options['login'])){
+			$error[]=array('error'=>'username not set');
+		};
+		if(empty($options['pass'])){
+			$error[]=array('error'=>'password not set');
+		};
+		if(empty($options['directory'])){
+			$error[]=array('error'=>'data directory not set');
+		};
+		if($dbtype=='mysql'){//mysql needs more config options
+			if(empty($options['dbuser'])){
+				$error[]=array('error'=>'database user directory not set');
+			};
+			if(empty($options['dbpass'])){
+				$error[]=array('error'=>'database password directory not set');
+			};
+			if(empty($options['dbname'])){
+				$error[]=array('error'=>'database name directory not set');
+			};
+			if(empty($options['dbhost'])){
+				$error[]=array('error'=>'database host directory not set');
+			};
+			if(!isset($options['dbtableprefix'])){
+				$error[]=array('error'=>'database table prefix directory not set');
+			};
+		}
+		if(count($error)==0){ //no errors, good
+			$username=$options['login'];
+			$password=$options['pass'];
+			$datadir=$options['directory'];
+			
+			//write the config file
+			OC_CONFIG::setValue('datadirectory',$datadir);
+			OC_CONFIG::setValue('dbtype',$dbtype);
+			if($dbtype=='mysql'){
+				$dbuser=$options['dbuser'];
+				$dbpass=$options['dbpass'];
+				$dbname=$options['dbname'];
+				$dbhost=$options['dbhost'];
+				$dbtableprefix=$options['dbtableprefix'];
+				OC_CONFIG::setValue('dbname',$dbname);
+				OC_CONFIG::setValue('dbhost',$dbhost);
+				OC_CONFIG::setValue('dbtableprefix',$dbtableprefix);
+				
+				//check if the database user has admin right
+				$connection=mysql_connect($dbhost, $dbuser, $dbpass);
+				if(!$connection) {
+					$error[]=array('error'=>'mysql username and/or password not valid','you need to enter either an existing account, or the administrative account if you wish to create a new user for ownCloud');
+				}else{
+					$query="SELECT user FROM mysql.user WHERE user='$dbuser'";//this should be enough to check for admin rights in mysql
+					if(mysql_query($query,$connection)){
+						//use the admin login data for the new database user
+						self::createDBUser($username,$password);
+						OC_CONFIG::setValue('dbuser',$username);
+						OC_CONFIG::setValue('dbpass',$password);
+					}else{
+						OC_CONFIG::setValue('dbuser',$dbuser);
+						OC_CONFIG::setValue('dbpass',$dbpass);
+						
+						//create the database
+						self::createDatabase($dbname,$dbuser);
+					}
+				}
+				mysql_close($connection);
+			}
+			OC_USER::createUser($username,$password);
+			OC_GROUP::createGroup('admin');
+			OC_GROUP::addToGroup($username,'admin');
+			OC_CONFIG::setValue('installed',true);
+		}
+		return $error;
+	}
+	
+	public static function createDatabase($name,$adminUser,$adminPwd){//TODO refactoring this
+		$CONFIG_DBHOST=$options['host'];
+		$CONFIG_DBNAME=$options['name'];
+		$CONFIG_DBUSER=$options['user'];
+		$CONFIG_DBPWD=$options['pass'];
+		$CONFIG_DBTYPE=$options['type'];
+		//we cant user OC_BD functions here because we need to connect as the administrative user.
+		$query="CREATE DATABASE IF NOT EXISTS  `$name`";
+		$result = mysql_query($query,$connection);
+		if (!$result) {
+			$entry='DB Error: "'.mysql_error($connection).'"<br />';
+			$entry.='Offending command was: '.$query.'<br />';
+			echo($entry);
+		}
+		$query="GRANT ALL PRIVILEGES ON  `$name` . * TO  '$user'";
+		$result = mysql_query($query,$connection);
+		if (!$result) {
+			$entry='DB Error: "'.mysql_error($connection).'"<br />';
+			$entry.='Offending command was: '.$query.'<br />';
+			echo($entry);
+		}
+	}
+	
+	private static function createDBUser($name,$password){
+		//we need to create 2 accounts, one for global use and one for local user. if we don't speccify the local one,
+				//  the anonymous user would take precedence when there is one.
+		$query="CREATE USER 'name'@'localhost' IDENTIFIED BY '$password'";
+		$result = mysql_query($query,$connection);
+		$query="CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
+		$result = mysql_query($query,$connection);
+	}
+}
+
+?>
\ No newline at end of file
diff --git a/templates/error.php b/templates/error.php
index 5b71c1ef75..ae3f029708 100644
--- a/templates/error.php
+++ b/templates/error.php
@@ -9,7 +9,7 @@
 		<?php foreach($_["errors"] as $error):?>
 			<li class='error'>
 				<?php echo $error['error'] ?><br/>
-				<p class='hint'><?php echo $error['hint'] ?></p>
+				<p class='hint'><?php if(isset($error['hint']))echo $error['hint'] ?></p>
 			</li>
 		<?php endforeach ?>
 	</ul>
diff --git a/templates/installation.php b/templates/installation.php
new file mode 100644
index 0000000000..6e555e843f
--- /dev/null
+++ b/templates/installation.php
@@ -0,0 +1,52 @@
+<?php
+/*
+ * Template for installation page
+ */
+?>
+<div id="login">
+	<img src="<?php echo image_path("", "owncloud-logo-medium-white.png"); ?>" alt="ownCloud" />
+	<form action="" method="post">
+		<input type='hidden' name='install' value='true'/>
+		<fieldset>
+			<p><input type="text" name="login" value="username" /></p>
+			<p><input type="password" name="pass" value="password" /></p>
+        </fieldset>
+		<fieldset>
+			<?php if(!$_['hasSQLite']): ?>
+				<legend><abbr title="to use SQLite instead, install it on your server">MySQL</abbr> Database</legend>
+				<p><input type="text" name="dbuser" value="admin / username" /></p>
+				<p><input type="password" name="dbpass" value="password" /></p>
+				<p><input type="text" name="dbname" value="database name" /></p>
+			<?php endif;?>
+		</fieldset>
+		<fieldset id="advanced">
+			<legend><a id="advanced_options_link" href="">Advanced ▾</a></legend>
+			<div id="advanced_options">
+				<p><label class="left">Data directory</label></p><p><input type="text" name="directory" value="<?php echo($_['datadir']);?>" /></p>
+				<?php if($_['hasMySQL'] and $_['hasSQLite']): ?>
+					<p><label class="left">Database</label></p>
+					<p><input type="radio" name="dbtype" value='sqlite' id="sqlite" checked="checked" /><label for="sqlite">SQLite</label>
+					<input type="radio" name="dbtype" value='mysql' id="mysql"><label for="mysql">MySQL</label></p>
+					<div id="use_mysql">
+						<p><input type="text" name="dbuser" value="admin / username" /></p>
+						<p><input type="password" name="dbpass" value="password" /></p>
+						<p><input type="text" name="dbname" value="database name" /></p>
+				<?php endif;?>
+				<?php if($_['hasMySQL'] and !$_['hasSQLite']): ?>
+					<input type='hidden' name='dbtype' value='mysql'/>
+				<?php endif;?>
+				<?php if(!$_['hasMySQL'] and $_['hasSQLite']): ?>
+					<input type='hidden' name='dbtype' value='sqlite'/>
+				<?php endif;?>
+				<?php if($_['hasMySQL']): ?>
+						<p><label class="left">Host</label></p><p><input type="text" name="dbhost" value="localhost" /></p>
+						<p><label class="left">Table prefix</label></p><p><input type="text" name="dbtableprefix" value="oc_" /></p>
+					</div>
+				<?php endif;?>
+			</div>
+		</fieldset>
+		<fieldset>
+			<p class="submit"><input type="submit" value="Create" /></p>
+		</fieldset>
+	</form>
+</div>
\ No newline at end of file
-- 
GitLab