Location: PHPKode > projects > raSMP > rasmp/admin/database.php
<?php

/*******************************************************************

 Name		: raSMP 2.0
 Copyright	: 2002, Adam Alkins
 Website	: http://www.rasmp.com
 email		: hide@address.com

 $Id: database.php,v 1.17 2003/03/16 18:26:44 rasadam Exp $: 

*******************************************************************/

/*******************************************************************

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the 
Free Software Foundation Inc., 59 Temple Place, Suite 330,
Boston, MA  02111-1307  USA

*******************************************************************/

/*

Database Tools

*/

define('RASMP',true);
define('RASMP_ADMIN',true);
define('SCRIPT_PATH','../');
include SCRIPT_PATH.'common/extension.inc';
include SCRIPT_PATH.'common/admin_common.'.FILE_EXT;

// Check authentication
check_auth('database');

function do_header($title)
{
  	display_header($title);
	display_menu();
	display_body();
}

function display_menu()
{
?>

<table width="98%" border="0" cellspacing="0" cellpadding="0" align="center">
  <tr>
    <td bgcolor="#0066CC">
      <div align="center"><font color="#FFFFFF" size="3">Database Tools</font></div>
    </td>
  </tr>
</table>

<?php

}

if(isset($_POST['doquery']))
{
	if(!isset($_POST['query']))
	{
		redirect_page('Query not provided',attach_sid("database.".FILE_EXT));
	}
	
	if($_POST['query']=='')
	{
		redirect_page('Nothing to Execute',attach_sid("database.".FILE_EXT));
	}
	
	// if we can't find a delimiter in the query, it most likely is incorrect
	if(!strstr($_POST['query'],DELIMITER))
	{
		redirect_page('Not a complete Query.',attach_sid("database.".FILE_EXT."?query=".urlencode($_POST['query'])));
	}
	
	unset($query);
			
	// split the queries to run one at a time
	$query = split_sql_file($_POST['query'], DELIMITER);		

	// counts amount of queries
	$sql_count = count($query);
	
	// will loop and execute each query at an iteration
	for($i = 0; $i < $sql_count; $i++)
	{
		// get the result resource
		$result = db_query(stripslashes($query[$i]),'Could not execute query on database',0);
		
		// if query failed
		if(!$result)
		{
			// display nice screen with error and number (and query)
			query_result(stripslashes($query[$i]),'Failed',db_errormsg($result),db_errorno());
		}
		else
		{
			query_result(stripslashes($query[$i]),'Success');
		}
	}
	
	echo '<p align="center">Return to <a href="'.attach_sid("database.".FILE_EXT).'">Database Tools</a>.</p>';
	
	die;						
}
else if(isset($_POST['douninstall']))
{
	unset($constants);
	// get all the defined constants
	$constants = get_defined_constants();
	
	// loop through the constants
	while( $element = each($constants) )
	{
		// see if the array key matches the basic layout for a table according to the coding
		// standards, that is, a table should be split into [tablename]_TABLE, tables should
		// only have Characters A-Z (Capitals), Numbers 0-9 and Underscores to separate words.
		if(preg_match("/^[A-Z0-9_]+_TABLE$/",$element["key"]))
		{
			$query = "DROP TABLE ".$element["value"];
			db_query($query,'Could not drop table',0);
			
			if(DB_TYPE == 'pgsql')
			{
				$query = "DROP SEQUENCE ".$element["value"]."_seq";
				db_query($query,'Could not drop sequence',0);
			}
		}
	}
	
?>
<table width="75%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
  <tr> 
    <td align="center" valign="middle" bgcolor="#0066CC"><div align="center"><font color="#FFFFFF" size="3"><strong>raSMP 
        Uninstalled Successfully</strong></font></div></td>
  </tr>
  <tr>
    <td height="300" align="center" valign="middle" bgcolor="#00458A"><p><font color="#FFFFFF" size="3">raSMP Tables  
        have been successfully uninstalled. Delete the entire raSMP Directory to 
        complete uninstallation.</font></p>
      <p><font color="#FFFFFF" size="3">Thank you for trying raSMP.</font></p></td>
  </tr>
</table>
<?php
	die;				
}
else
{
	do_header("Admin Panel >> Database Tools");
?>
<div align="center"> 
  <form name="form1" method="post" action="<?php attach_sid("database.".FILE_EXT); ?>">
    <p align="center">
    Execute a Query/Multiple queries on your database (Will not return data from 
    SELECT statements). Note: Add trailing delimiter (Semicolon for MySQL and PostgreSQL to your SQL Queries).
    </p><p>
      <textarea name="query" cols="75" rows="6"><?php echo htmlentities(urldecode($_GET['query'])); ?></textarea>
    </p>
    <p> 
      <input type="submit" name="doquery" value="Execute" />
      &nbsp; 
      <input type="reset" name="reset" value="Reset" />
    </p>
  </form>
  <p>&nbsp;</p>
  <form name="form2" method="post" action="<?php attach_sid("database.".FILE_EXT); ?>">
    <p>This will delete all the raSMP Tables in your database. (Note: <strong>Permanent</strong>!)</p>
    <p>
	  <input type="submit" name="douninstall" value="Uninstall raSMP" />
    </p>
  </form>
</div>
<?php
	display_footer();
}

?>
Return current item: raSMP