Location: PHPKode > projects > Kwalbum > kwalbum/pages/AdminRestoreDatabasePage.php
<?php


/*
 * Created on Sep 27, 2008
 * 2.0
 */
require_once 'include/IPage.php';

class AdminRestoreDatabasePage implements IPage
{
	function GetHead(& $title)
	{
		if (false == USER_IS_ADMIN)
			return;

		$title = 'Restore Database';
		return '';
	}

	function ShowBody()
	{
		if (false == USER_IS_ADMIN)
			return;
		global $URLP, $P;
		$url = $URLP->GetExtendedURL();
		$msg = '<b><big>Warning! Restoring an old copy of the database will erase any changes since the copy was made.</big></b>';

		if (isset ($P['del']) and isset ($_FILES['fn']))
		{
			$file = $_FILES['fn'];
			$fname = $file['tmp_name'];
			$fp = @ fopen($fname, "r");
			if ($fp)
			{
				$msg = "<big><b>" . $file['name'] . " has been uploaded...</b></big>\n";
			} else
			{
				fclose($fp);
				unlink($fname); // in case ph doesn't automatically delete uploaded files
				unset ($fp);
				$msg = "<span class='error'>Error: " . $file['name'] . " could not be opened.</span>";
			}
		}

		require 'AdminRestoreDatabasePage.html.php';
	}

	private function Restore($fp, $fname)
	{
		global $DB;
		echo '<b>Recreating tables...</b><br/>';
		$query = '';
		$hasInvalids = false;
		while ($data = utf8_encode(fread($fp, 4096)))
		{
			do
			{
				$pos = strpos($data, "\n");
				if (false === $pos)
				{
					$query = $data;
				} else
				{
					$query .= substr($data, 0, $pos);
					if (35 == ord($query))
					{
						echo $query . "<br>\n";
					}
					elseif (!@ $DB->Query($query))
					{
						$hasInvalids = true;
						echo "<b>Invalid query:</b>\n$query\n<br>\n";
					}
					$query = '';
				}
				$pos++;
				$data = substr($data, $pos);
			} while ($pos);
		}

		fclose($fp);
		unlink($fname); // in case ph doesn't automatically delete uploaded files
		echo '<big><b>Data has been restored.' .
		 ($hasInvalids ? '<br/>There are invalid queries that were not run.' .
		 		'  They are shown above and must be run manually after the errors have been corrected.' .
		 		'  Viewing the source code for this page is recommended in case the query contains html tags.' : null) .
		'</b></big>';
	}
}
?>
Return current item: Kwalbum