File: 0.02.32a/server/web/firstrun-.php (View as HTML)

  1: <?php
  2: /* -------------------------------------------------------------
  3: This file is part of FreeNATS
  4: 
  5: FreeNATS is (C) Copyright 2008 PurplePixie Systems
  6: 
  7: FreeNATS 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: FreeNATS 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 FreeNATS.  If not, see www.gnu.org/licenses
 19: 
 20: For more information see www.purplepixie.org/freenats
 21: -------------------------------------------------------------- */
 22: 
 23: require("include.php");
 24: $sn=$_SERVER['SCRIPT_NAME'];
 25: $script="";
 26: for ($a=strlen($sn)-1; $a>0; $a--)
 27: 	{
 28: 	$c=$sn[$a];
 29: 	if ($c=="/") $a=-1;
 30: 	else $script=$c.$script;
 31: 	}
 32: if ($script!="firstrun.php")
 33: 	{
 34: 	echo "<b>Error:</b> This script is not correctly named. In order to run it please rename &quot;".$script."&quot; to &quot;firstrun.php&quot;.";
 35: 	echo "<br><br>";
 36: 	exit();
 37: 	}
 38: if (isset($_REQUEST['stage'])) $stage=$_REQUEST['stage'];
 39: else $stage=0;
 40: 
 41: function sqlfile($file)
 42: {
 43: global $BaseDir;
 44: $fn=$BaseDir."sql/".$file.".sql";
 45: echo "Processing ".$fn."<br><br>";
 46: $fp=fopen($fn,"r");
 47: if ($fp<=0)
 48: 	{
 49: 	echo "<b>ERROR: Cannot Open File!</b><br><br>";
 50: 	return false;
 51: 	}
 52: $q="";
 53: $qc=0;
 54: $qok=0;
 55: $qerr=0;
 56: while ($s=fgets($fp,1024))
 57: 	{
 58: 	if ($s[0]!="-")
 59: 		{
 60: 		for ($a=0; $a<strlen($s); $a++)
 61: 			{
 62: 			$c=$s[$a];
 63: 			if ($c==";")
 64: 				{
 65: 				mysql_query($q);
 66: 				$qc++;
 67: 				if (mysql_errno()!=0)
 68: 					{
 69: 					echo "<b>Warning/Error</b><br>";
 70: 					echo "<b>SQL:</b> ".$q."<br>";
 71: 					echo "<b>Error:</b> ".mysql_error()." (Code ".mysql_errno().")<br><br>";
 72: 					$qerr++;
 73: 					}
 74: 				else $qok++;
 75: 				$q="";
 76: 				}
 77: 			else
 78: 				$q.=$c;
 79: 			}
 80: 		}
 81: 	}
 82: echo "Finished Processing: ".$qc." queries (".$qok." ok, ".$qerr." warnings/errors)<br><br>";
 83: fclose($fp);
 84: }
 85: 
 86: echo "<h1>First Run Setup: Stage ".$stage."</h1>";
 87: 
 88: echo "Testing database connectivity...<br>";
 89: 		$sql=mysql_connect($fnCfg['db.server'],$fnCfg['db.username'],$fnCfg['db.password'])
 90: 			or die("Failed to connect to database server ".$fnCfg['db.server']." with username ".$fnCfg['db.username']."<br>Details: ".mysql_error());
 91: 		mysql_select_db($fnCfg['db.database'])
 92: 			or die("Connected ok but failed to select database ".$fnCfg['db.database']."<br>Details: ".mysql_error());
 93: 		echo "Database connection succeeded!<br><br>";
 94: 
 95: switch($stage)
 96: 	{
 97: 	case 0:
 98: 		echo "<form action=firstrun.php method=post>";
 99: 		echo "<input type=hidden name=stage value=1>";
100: 		echo "<b>Setup Database Schema and Defaults</b><br><br>";
101: 		echo "<input type=checkbox name=tracker value=1 checked> Submit Anonymous Usage Data Automatically<br>";
102: 		echo "<i>This will help us track use of the product and aid future development.</i><br><br>";
103: 		echo "<input type=checkbox name=example value=1 checked> Include Example Setup (Recommended)<br><br>";
104: 		echo "<input type=submit value=\"Proceed With Setup\">";
105: 		echo "</form>";
106: 		exit();
107: 		break;
108: 		
109: 	case 1:
110: 		echo "<b>Setting Up Schema...</b><br><br>";
111: 		sqlfile("schema");
112: 		echo "<b>Setting Up Defaults...</b><br><br>";
113: 		sqlfile("default");
114: 		if (isset($_REQUEST['example']))
115: 			{
116: 			echo "<b>Setting Up Examples...</b><br><br>";
117: 			sqlfile("example");
118: 			}
119: 			
120: 		if (isset($_REQUEST['tracker']))
121: 			{
122: 			echo "<b>Enabling Usage Tracker</b><br><br>";
123: 			$q="INSERT INTO fnconfig(fnc_var,fnc_val) VALUES(\"freenats.tracker\",\"1\")";
124: 			mysql_query($q);
125: 			}
126: 		echo "<br><br>";
127: 		echo "<b>CONGRATULATIONS!!</b><br><br>";
128: 		echo "Setup should now be complete. If you saw errors etc above please see the <a href=http://www.purplepixie.org/freenats/>";
129: 		echo "project homepage</a> for help.<br><br>";
130: 		echo "<b>RENAME THIS FILE &quot;firstrun.php&quot; TO SOMETHING ELSE LIKE &quot;firstrun-.php&quot; TO STOP OTHERS RUNNING IT</b>";
131: 		echo "<br><br>";
132: 		echo "<a href=./>Click here to continue</a> - login admin password admin (click settings once logged in to change)<br><br>";
133: 		
134: 		
135: 		exit();
136: 		break;
137: 		
138: 	default:
139: 		echo "Sorry - unknown step in setup process!<br><br>";
140: 		exit();
141: 	}
142: 
143: ?>