File: 0.00.0a/setup.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeDESK
  4: 
  5: FreeDESK is (C) Copyright 2012 David Cutting
  6: 
  7: FreeDESK is free software: you can redistribute it and/or modify
  8: it under the terms of the GNU General Public License as published by
  9: the Free Software Foundation, either version 3 of the License, or
 10: (at your option) any later version.
 11: 
 12: FreeDESK is distributed in the hope that it will be useful,
 13: but WITHOUT ANY WARRANTY; without even the implied warranty of
 14: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15: GNU General Public License for more details.
 16: 
 17: You should have received a copy of the GNU General Public License
 18: along with FreeDESK.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freedesk/
 21: -------------------------------------------------------------- */
 22: 
 23: /**
 24:  * The main setup file
 25: **/
 26: ?>
 27: <!DOCTYPE html>
 28: <head>
 29: <title>FreeDESK Setup</title>
 30: <link rel=\"stylesheet\" type=\"text/css\" href=\"skin/default/css/main.css\" />
 31: <style type="text/css">
 32: span.previous
 33: {
 34: 	font-size: 16pt;
 35: 	font-weight: bold;
 36: 	color: #90e090;
 37: 	padding: 1em;
 38: }
 39: span.current
 40: {
 41: 	font-size: 16pt;
 42: 	font-weight: bold;
 43: 	color: black;
 44: 	padding: 1em;
 45: }
 46: span.tocome
 47: {
 48: 	font-size: 16pt;
 49: 	font-weight: bold;
 50: 	color: #909090;
 51: 	padding: 1em;
 52: }
 53: </style>
 54: </head>
 55: <body>
 56: <?php
 57: $setup=$_SERVER['SCRIPT_NAME'];
 58: function step_list($steps, $current)
 59: {
 60: 	echo "<div id=\"stepsdiv\">\n";
 61: 	for ($a=1; $a<=$steps; ++$a)
 62: 	{
 63: 		if ($a<$current)
 64: 			echo "<span class=\"previous\">";
 65: 		else if ($a==$current)
 66: 			echo "<span class=\"current\">";
 67: 		else
 68: 			echo "<span class=\"tocome\">";
 69: 		echo $a;
 70: 		echo "</span>";
 71: 	}
 72: 	echo "\n</div>\n";
 73: }
 74: 
 75: function fdsql($file, $prefix, &$sql)
 76: {
 77: 	$fp=@fopen("sql/".$file,"r");
 78: 	if (!$fp)
 79: 	{
 80: 		echo "Error opening ".$file."<br /><br />";
 81: 		return false;
 82: 	}
 83: 	$q="";
 84: 	while ($str = fgets($fp, 1024))
 85: 	{
 86: 		if ($str[0]!="-")
 87: 		{
 88: 			for ($a=0; $a<strlen($str); ++$a)
 89: 			{
 90: 				$chr = $str[$a];
 91: 				if ($chr==";")
 92: 				{
 93: 					$q=str_replace("%%$%%", $prefix, $q);
 94: 					mysql_query($q, $sql);
 95: 					$q="";
 96: 					$a=strlen($str)+10;
 97: 				}
 98: 				else
 99: 					$q.=$chr;
100: 			}
101: 		}
102: 	}
103: }
104: 
105: if (!isset($_REQUEST['step']))
106: 	$step=1;
107: else
108: 	$step=$_REQUEST['step'];
109: if (!is_numeric($step))
110: 	$step=1;
111: 
112: step_list(10, $step);
113: ?>
114: <h2>FreeDESK Setup Process</h2>
115: <?php
116: if ($step == 1) // First page
117: {
118: ?>
119: <h3>Initial Setup</h3>
120: This process will take you through a fresh install or upgrade of the FreeDESK system.<br /><br />
121: <b>If you are setup but are seeing this message then you need to delete the setup.php file in the freedesk directory</b><br /><br />
122: <?php
123: if (file_exists("config/Config.php"))
124: {
125: 	echo "Configuration Already Exists - <a href=\"".$setup."?step=4\">Keep Existing Database Connection Settings</a><br /><br />";
126: }
127: echo "<a href=\"".$setup."?step=2\">Create New Database Connection Settings</a>";
128: }
129: else if ($step == 2) // Database Connection Settings
130: {
131: ?>
132: <h3>Database Connection Settings</h3>
133: The following settings are for a connection to a MySQL database where the FreeDESK data will be stored.<br /><br />
134: <?php
135: echo "<form action=\"".$setup."\" method=\"post\">\n";
136: ?>
137: <input type="hidden" name="step" value="3">
138: <table border="0">
139: 
140: <tr><td>Server</td>
141: <td><input type="text" name="db_server" value="localhost"></td></tr>
142: 
143: <tr><td>Username</td>
144: <td><input type="text" name="db_username" value=""></td></tr>
145: <tr><td>Password</td>
146: <td><input type="text" name="db_password" value=""></td></tr>
147: 
148: <tr><td>Table Prefix</td>
149: <td><input type="text" name="db_prefix" value="fd_"></td></tr>
150: 
151: <tr><td>Database</td>
152: <td><input type="text" name="db_database" value="freedesk"></td></tr>
153: 
154: <tr><td>Create Database</td>
155: <td><input type="checkbox" name="db_create" value="1" checked></td></tr>
156: 
157: <tr><td>Password Salt</td>
158: <td><input type="text" name="pwd_salt" value=""> e.g. three or four random characters</td></tr>
159: 
160: </table>
161: <input type="submit" value="Validate Settings and Create Configuration">
162: </form>
163: <?php
164: 
165: $fp=@fopen("config/Config.php","a");
166: if (!$fp)
167: {
168: 	echo "Warning: the config/Config.php file is not writeable so you will have to manually update the file.<br /><br />";
169: 	echo "Or if you make the file world-writeable (e.g. chmod 777 config in Linux) then the script will create it automatically.";
170: }
171: else
172: 	fclose($fp);
173: 
174: }
175: 
176: else if ($step == 3) // Validate and Write
177: {
178: echo "<h3>Validating Connection Settings</h3>";
179: $sql=mysql_connect($_REQUEST['db_server'],$_REQUEST['db_username'],$_REQUEST['db_password']);
180: if (!$sql)
181: {
182: 	echo "Error: Could not connect to the database. Please go back in your browser and input correct credentials<br /><br />";
183: 	echo "</body></html>";
184: 	exit();
185: }
186: echo "Successful connection to database server<br /><br />";
187: 
188: if (isset($_REQUEST['db_create']) && ($_REQUEST['db_create']=="1"))
189: {
190: 	echo "Creating Database...<br /><br />";
191: 	$q="CREATE DATABASE ".$_REQUEST['db_database'];
192: 	mysql_query($q);
193: }
194: 
195: if (mysql_select_db($_REQUEST['db_database']))
196: {
197: 	echo "Connected to database ".$_REQUEST['db_database']."<br /><br />";
198: }
199: else
200: {
201: 	echo "Error: Could not select database ".$_REQUEST['db_database'].", go back on your browser and input correct database<br /><br />";
202: 	echo "</body></html>";
203: 	exit();
204: }
205: 
206: echo "<h3>Creating Config File</h3>";
207: $config = "<?php // Config.php created by setup.php for FreeDESK\n";
208: $config.="\nclass FreeDESK_Configuration\n{\n";
209: $config.="var \$db_System = \"MySQL\";\n";
210: $config.="var \$db_Server = \"".$_REQUEST['db_server']."\";\n";
211: $config.="var \$db_Username = \"".$_REQUEST['db_username']."\";\n";
212: $config.="var \$db_Password = \"".$_REQUEST['db_password']."\";\n";
213: $config.="var \$db_Database = \"".$_REQUEST['db_database']."\";\n";
214: $config.="var \$db_Prefix = \"".$_REQUEST['db_prefix']."\";\n";
215: $config.="var \$pwd_Hash = \"".$_REQUEST['pwd_salt']."\";\n";
216: $config.="}\n?>";
217: 
218: $fp = @fopen("config/Config.php","w");
219: if ($fp)
220: {
221: 	echo "Writing config/Config.php...<br /><br />";
222: 	fputs($fp, $config);
223: 	fclose($fp);
224: }
225: else
226: {
227: 	echo "Sorry cannot write to config/Config.php file.<br /><br />";
228: 	echo "Either make the file writeable by the web server and then refresh this page or...<br /><br />";
229: 	echo "Copy and paste the following into a new file and upload it as config/Config.php within the freedesk directory.<br /><br />";
230: 	echo "<textarea cols=\"80\" rows=\"10\" readonly=\"readonly\">".$config."</textarea><br /><br />\n";
231: }
232: echo "<a href=\"".$setup."?step=4\">Continue With Installation</a>";
233: 
234: }
235: 
236: 
237: else if ($step == 4)
238: {
239: echo "<h3>Database Schema Setup/Upgrade</h3>";
240: echo "Select a mode for the schema setup<br /><br />";
241: echo "<a href=\"".$setup."?step=5&mode=fresh\">Perform a fresh install (any existing data will be lost or overriten)</a> - SELECT FOR NEW INSTALL OR TO START FROM SCRATCH<br /><br />";
242: echo "<a href=\"".$setup."?step=5&mode=upgrade\">Perform an update install (no existing data will be lost)</a> - SELECT TO UPDATE AN EXISTING SYSTEM<br /><br />";
243: }
244: 
245: else if ($step == 5)
246: {
247: require("config/Config.php");
248: echo "<h3>Import or Setup Schema</h3>";
249: $fdc = new FreeDESK_Configuration();
250: 
251: $sql = mysql_connect($fdc->db_Server, $fdc->db_Username, $fdc->db_Password)
252: 	or die("Cannot connect to database");
253: mysql_select_db($fdc->db_Database)
254: 	or die("Cannot select database");
255: 
256: if (isset($_REQUEST['mode']) && ($_REQUEST['mode']=="fresh"))
257: {
258: 	fdsql("schema-drop.fdsql", $fdc->db_Prefix, $sql);
259: 	fdsql("default.fdsql", $fdc->db_Prefix, $sql);
260: }
261: else
262: {
263: 	fdsql("schema.fdsql", $fdc->db_Prefix, $sql);
264: 	fdsql("upgrade.fdsql", $fdc->db_Prefix, $sql);
265: 	fdsql("default.fdsql", $fdc->db_Prefix, $sql);
266: }
267: echo "Database Schema setup - <a href=\"".$setup."?step=6\">click here to continue</a><br /><br />\n";
268: }
269: 
270: else if ($step == 6)
271: {
272: echo "<h3>Final Stages</h3>";
273: echo "Trying to start FreeDESK...<br /><br />";
274: require("core/FreeDESK.php");
275: $DESK = new FreeDESK("./");
276: if ($DESK->Start())
277: {
278: 	echo "FreeDESK started!<br /><br />";
279: 	echo "<h3>Set Admin Password</h3>";
280: 	echo "Set a password for the admin user - this is essential if this is a new install, fresh schema update or the password salt has changed.<br /><br />";
281: 	echo "If you are using an existing setup and no connection data has changed just leave it blank to not set and just click go.<br /><br />";
282: 	echo "<form action=\"".$setup."\" method=\"post\"><input type=\"hidden\" name=\"step\" value=\"7\">";
283: 	echo "Password: <input type=\"text\" name=\"admin_password\" value=\"\"> ";
284: 	echo "<input type=\"submit\" value=\"Go\"></form>";
285: }
286: else
287: {
288: 	echo "Sorry there was a problem starting FreeDESK<br /><br />";
289: }
290: 
291: $DESK->Stop();
292: }
293: 
294: else if ($step == 7)
295: {
296: require("core/FreeDESK.php");
297: $DESK = new FreeDESK("./");
298: $DESK->Start();
299: 
300: if (isset($_REQUEST['admin_password']) && ($_REQUEST['admin_password'] != ""))
301: {
302: 	echo "Setting admin password... ";
303: 	$amb=new AuthMethodStandard($DESK);
304: 	$amb->SetPassword("admin",$_REQUEST['admin_password']);
305: 	echo "Done<br /><br />";
306: }
307: 
308: echo "Your setup is now complete and can be further configured from within the system.<br /><br />";
309: echo "<b>PLEASE NOW DELETE THIS FILE (freedesk/setup.php) OR YOU WILL BE UNABLE TO USE FREEDESK.</b><br /><br />";
310: echo "<a href=\"./\">Click here to login to FreeDESK</a><br /><br />";
311: 
312: $DESK->Stop();
313: }
314: else
315: {
316: echo "<h3>Unknown Mode</h3>";
317: }
318: ?>
319: 
320: </body>
321: </html>
322: