upgrade.php 25.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
<?php
/**
 * $Id$
 *
 * KnowledgeTree Open Source Edition
 * Document Management Made Simple
 * Copyright (C) 2004 - 2008 The Jam Warehouse Software (Pty) Limited
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 3 as published by the
 * Free Software Foundation.
 * 
 * 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, see <http://www.gnu.org/licenses/>.
 * 
 * You can contact The Jam Warehouse Software (Pty) Limited, Unit 1, Tramber Place,
 * Blake Street, Observatory, 7925 South Africa. or email info@knowledgetree.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * KnowledgeTree" logo and retain the original copyright notice. If the display of the 
 * logo is not reasonably feasible for technical reasons, the Appropriate Legal Notices
 * must display the words "Powered by KnowledgeTree" and retain the original 
 * copyright notice. 
 * Contributor( s): ______________________________________
 *
 */

$GLOBALS["checkup"] = true;
session_start();
require_once('../config/dmsDefaults.php');
require_once(KT_LIB_DIR . '/authentication/authenticationutil.inc.php');
require_once(KT_LIB_DIR . '/upgrades/upgrade.inc.php');
require_once(KT_LIB_DIR . '/plugins/pluginutil.inc.php');

function generateUpgradeTable () {
	global $default;
	$query = sprintf('SELECT value FROM %s WHERE name = "databaseVersion"', $default->system_settings_table);
	$lastVersion = DBUtil::getOneResultKey($query, 'value');
	$currentVersion = $default->systemVersion;

	$upgrades = describeUpgrade($lastVersion, $currentVersion);

	$ret = "<table border=1 cellpadding=1 cellspacing=1 width='100%'>\n";
	$ret .= "<tr bgcolor='darkgrey'><th width='10'>Code</th><th width='100%'>Description</th><th width='30'>Applied</th></tr>\n";
	$i=0;
	foreach ($upgrades as $upgrade) {
		$color=((($i++)%2)==0)?'white':'lightgrey';
		$ret .= sprintf("<tr bgcolor='$color'><td>%s</td><td>%s</td><td>%s</td></tr>\n",
		htmlspecialchars($upgrade->getDescriptor()),
		htmlspecialchars($upgrade->getDescription()),
		$upgrade->isAlreadyApplied() ? "Yes" : "No"
		);
	}
	$ret .= '</table>';
	return $ret;
}

function showResult($res) {
	if (PEAR::isError($res)) {
		if (is_a($res, 'Upgrade_Already_Applied')) {
			return '<span style="color: orange">Already applied</span>';
		}
		return sprintf('<span style="color: red">%s</span>', htmlspecialchars($res->toString()));
	}
	if ($res === true) {
		return '<span style="color: green">Success</span>';
	}
	if ($res === false) {
		return '<span style="color: red">Failure</span>';
	}
	return $res;
}

$GLOBALS['row'] = 1;

function performAllUpgrades () {
	global $default;
	$query = sprintf('SELECT value FROM %s WHERE name = "databaseVersion"', $default->system_settings_table);
	$lastVersion = DBUtil::getOneResultKey($query, 'value');
	$currentVersion = $default->systemVersion;

	$upgrades = describeUpgrade($lastVersion, $currentVersion);

	foreach ($upgrades as $upgrade) {
		if (($GLOBALS['row'] % 2) == 1) {
			$class = "odd";
		} else {
			$class = "even";
		}
		printf('<div class="row %s"><div class="foo">%s</div>' . "\n", $class, htmlspecialchars($upgrade->getDescription()));
		$GLOBALS['row']++;
		ob_flush();
		flush();
		$res = $upgrade->performUpgrade();
		printf('<div class="bar">%s</div>', showResult($res));
		print '<br style="clear: both">' . "\n";
		ob_flush();
		flush();
		print "</div>\n";
		if (PEAR::isError($res)) {
			if (!is_a($res, 'Upgrade_Already_Applied')) {
				break;
			} else {
				$res = true;
			}
		}
		if ($res === false) {
			$res = PEAR::raiseError("Upgrade returned false");
			break;
		}
	}
	
    return $res;
}

function performPreUpgradeActions() {
    
    // This is just to test and needs to be updated to a more sane and error resistent architrcture if it works.
    // It should idealy work the same as the upgrades.
    
    return true;

}

function performPostUpgradeActions() {
    
    // This is just to test and needs to be updated to a more sane and error resistent architrcture if it works.
    // It should idealy work the same as the upgrades.
    
    // Clean out the plugin_helper table
    $sql = "DELETE FROM plugin_helper";
    $res = DBUtil::runQuery($sql);

    return $res;

}

function failWritablePath($name, $path) {
	if (!is_writable($path)) {
		sprintf("The path for setting %s, which is set to %s, can not be written to.  Correct this situation before continuing.", $name, $path);
		exit(1);
	}
}

failWritablePath('Log directory', $default->logDirectory);
failWritablePath('Document directory', $default->documentRoot);

if (PEAR::isError($loggingSupport)) {
	print '<p><font color="red">Logging support is not currently working.  Check post-installation checkup.</font></p>';
	exit(1);
}

if (PEAR::isError($dbSupport)) {
	print '<p><font color="red">Database support is not currently working.  Check post-installation checkup or refresh this page (F5) to try again.</font></p>';
	exit(1);
}



?>
<html>
  <head>
    <title><?php echo APP_NAME;?> Upgrade</title>
    <style>
th { text-align: left; }
td { vertical-align: top; }
.foo { float: left; }
.bar { padding-left: 2em; float: right; }
.odd { background-color: #eeeeee; }
.even { background-color: #dddddd; }
.row { padding: 0.5em 1em; }
    </style>
  </head>

  <body>
  <img src="<?php 
  	if($oKTConfig->get('ui/mainLogo')){
  		echo $oKTConfig->get('ui/mainLogo');
  	}else{
	  	echo '../resources/graphics/ktlogo-topbar_base.png';
	}?>">
  <p>
  <img src="upgrade-title.jpg">
  <table width=800 height=500>
<tr><td>
<P>
   <script>
function do_start(action)
{
	document.location='?go=' + action;
}
</script>
<?php

$action = trim($_REQUEST["go"]);
switch ($action)
{
	case 'UpgradeConfirm':
	case 'UpgradePreview':
		UpgradePreview();
		break;
	case 'Upgrade':
		Upgrade();
		break;
	case 'BackupConfirm':
		backupConfirm();
		break;
	case 'Backup':
		backup();
		break;
	case 'BackupDone':		 
		backupDone();
		break;		
	case 'RestoreConfirm':		 
		restoreConfirm();
		break;		
	case 'RestoreSelect':		 
		restoreSelect();
		break;		
	case 'RestoreSelected':		 
		restoreSelected();
		break;		
	case 'Restore':		 
		restore();
		break;		
	case 'RestoreDone':		 
		restoreDone();
		break;		
	case 'Login':
		login();
		break;
	case 'LoginProcess':
		loginProcess();
		break;
	default:		
		if (!isset($_SESSION['setup_user']))
			login();
		else
			welcome();
		break;
}

function login()
{
?>
<P>
The database upgrade wizard completes the upgrade process on an existing <?php echo APP_NAME;?> installation. It applies
any upgrades to the database that may be required.
<P>
Only administrator users may access the upgrade wizard.
<P>

<form method=post action="?go=LoginProcess">
<table>
<tr><td>Username<td><input name=username>
<tr><td>Password<td><input name=password type="password">
<tr><td colspan=2 align=center><input type=submit value="login">
</table>
</form>
<?php	
}

function loginProcess()
{
	$username=$_REQUEST['username'];
	$password=$_REQUEST['password'];
		
	$authenticated = checkPassword($username, $password);
	
	if (!$authenticated)
	{
		session_unset();
		loginFailed(_kt('Could not authenticate administrative user'));
		return;
	}
		
	$_SESSION['setup_user'] = $username;
		
	welcome();	
}

function checkPassword($username, $password) {
	global $default;

	$sTable = KTUtil::getTableName('users');
	$sQuery = "SELECT count(*) AS match_count FROM $sTable WHERE username = ? AND password = ?";
	$aParams = array($username, md5($password));
	$res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'match_count');
	if (PEAR::isError($res)) { return false; }
	else {
		$sTable = KTUtil::getTableName('users_groups_link');
		$sQuery = "SELECT count(*) AS match_count FROM $sTable WHERE user_id = ? AND group_id = 1";
		$aParams = array($res);
		$res = DBUtil::getOneResultKey(array($sQuery, $aParams), 'match_count');
		if (PEAR::isError($res)) { return false; }
		else {
			return ($res == 1);
		}
	}
}

function loginFailed($message)
{
	print "<font color=red>$message</font>";
	login();
}

function resolveMysqlDir()
{	
	// possibly detect existing installations:
	
	if (OS_UNIX)
	{
		$dirs = array('/opt/mysql/bin','/usr/local/mysql/bin');
		$mysqlname ='mysql';
	}
	else 
	{
		$dirs = explode(';', $_SERVER['PATH']);	
		$dirs[] ='c:/Program Files/MySQL/MySQL Server 5.0/bin';
		$dirs[] = 'c:/program files/ktdms/mysql/bin';
		$mysqlname ='mysql.exe';
	}
	
	$oKTConfig =& KTConfig::getSingleton();
	$mysqldir = $oKTConfig->get('backup/mysqlDirectory',$mysqldir);
	$dirs[] = $mysqldir;	
	
	if (strpos(__FILE__,'knowledgeTree') !== false && strpos(__FILE__,'ktdms') != false)
	{
		$dirs [] = realpath(dirname($FILE) . '/../../mysql/bin');
	}
	
	foreach($dirs as $dir)
	{
		if (is_file($dir . '/' . $mysqlname))
		{
			return $dir;
		}
	}
	
	return '';
}


function create_backup_stmt($targetfile=null)
{
	$oKTConfig =& KTConfig::getSingleton();

	$adminUser = $oKTConfig->get('db/dbAdminUser');
	$adminPwd = $oKTConfig->get('db/dbAdminPass');
	$dbHost = $oKTConfig->get('db/dbHost');
	$dbName = $oKTConfig->get('db/dbName');
	
	$dbPort = trim($oKTConfig->get('db/dbPort'));
	if (empty($dbPort) || $dbPort=='default') $dbPort = get_cfg_var('mysql.default_port');
	if (empty($dbPort)) $dbPort='3306';
	$dbSocket = trim($oKTConfig->get('db/dbSocket'));
	if (empty($dbSocket) || $dbSocket=='default') $dbSocket = get_cfg_var('mysql.default_socket');
	if (empty($dbSocket)) $dbSocket='../tmp/mysql.sock';
	
	$date=date('Y-m-d-H-i-s');

	$dir=resolveMysqlDir();
	 
	$info['dir']=$dir;

	$prefix='';
	if (OS_UNIX)
	{
		$prefix .= "./";
	}
	
	if (@stat($dbSocket) !== false)
	{
		$mechanism="--socket=\"$dbSocket\"";
	}
	else 
	{
		$mechanism="--port=\"$dbPort\"";
	}
	
	$tmpdir=resolveTempDir();
	
	if (is_null($targetfile))
	{
		$targetfile="$tmpdir/kt-backup-$date.sql";
	}
	
	$stmt = $prefix . "mysqldump --user=\"$adminUser\" -p $mechanism \"$dbName\" > \"$targetfile\"";
	$info['display']=$stmt;
	$info['target']=$targetfile;

	 
	$stmt  = $prefix. "mysqldump --user=\"$adminUser\" --password=\"$adminPwd\" $mechanism \"$dbName\" > \"$targetfile\"";
	$info['cmd']=$stmt;
	return $info;
}

function create_restore_stmt($targetfile)
{
	$oKTConfig =& KTConfig::getSingleton();

	$adminUser = $oKTConfig->get('db/dbAdminUser');
	$adminPwd = $oKTConfig->get('db/dbAdminPass');
	$dbHost = $oKTConfig->get('db/dbHost');
	$dbName = $oKTConfig->get('db/dbName');
	$dbPort = trim($oKTConfig->get('db/dbPort'));
	if ($dbPort=='' || $dbPort=='default')$dbPort = get_cfg_var('mysql.default_port');
	if (empty($dbPort)) $dbPort='3306';
	$dbSocket = trim($oKTConfig->get('db/dbSocket'));
	if (empty($dbSocket) || $dbSocket=='default') $dbSocket = get_cfg_var('mysql.default_socket');
	if (empty($dbSocket)) $dbSocket='../tmp/mysql.sock';

	$dir=resolveMysqlDir();
	 
	$info['dir']=$dir;

	$prefix='';
	if (OS_UNIX)
	{
		$prefix .= "./";
	}
	  
	if (@stat($dbSocket) !== false)
	{
		$mechanism="--socket=\"$dbSocket\"";
	}
	else 
	{
		$mechanism="--port=\"$dbPort\"";
	}
	
	$tmpdir=resolveTempDir();
	
	$stmt = $prefix ."mysqladmin --user=\"$adminUser\" -p $mechanism drop  \"$dbName\"<br>";
	$stmt .= $prefix ."mysqladmin --user=\"$adminUser\" -p $mechanism create  \"$dbName\"<br>";
	
	
	$stmt .= $prefix ."mysql --user=\"$adminUser\" -p $mechanism \"$dbName\" < \"$targetfile\"\n";
	$info['display']=$stmt;

	 
	$stmt = $prefix ."mysqladmin --user=\"$adminUser\" --force --password=\"$adminPwd\" $mechanism drop  \"$dbName\"\n";
	$stmt .= $prefix ."mysqladmin --user=\"$adminUser\" --password=\"$adminPwd\" $mechanism create  \"$dbName\"\n";
	 
	$stmt .=  $prefix ."mysql --user=\"$adminUser\" --password=\"$adminPwd\" $mechanism \"$dbName\" < \"$targetfile\"";
	$info['cmd']=$stmt;
	return $info;
}

function title($title)
{
	if (!isset($_SESSION['setup_user']))
	{
		print "<script>document.location='?go=Login'</script>";
	}
	print "<h1>$title</h1>";
}

function resolveTempDir()
{
 
	if (OS_UNIX)
	{
		$dir='/tmp/kt-db-backup'; 
	}
	else 
	{
		$dir='c:/kt-db-backup'; 
	}
	$oKTConfig =& KTConfig::getSingleton();
	$dir = $oKTConfig->get('backup/backupDirectory',$dir);
	
	if (!is_dir($dir))
	{
			mkdir($dir);
	}
	return $dir;
}


function upgradeConfirm()
{
	title('Confirm Upgrade');
	if (!isset($_SESSION['backupStatus']) || $_SESSION['backupStatus'] === false)
	{
?>
<br>
<font color="Red">Please ensure that you have made a backup before continuing with the upgrade process.</font>
<p>
<br>
<?php
	}
?>
<p>
We are about to start the upgrade process.  
<P>
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('welcome')"> 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="next" onclick="javascript:do_start('UpgradePreview')"> 

<?php
	
}


function backupConfirm()
{
	title('Confirm Backup');
	$stmt=create_backup_stmt();
	$_SESSION['backupFile'] = $stmt['target'];
	
	$dir=$stmt['dir'];
	if ($dir != '')
	{
?>
 
Are you sure you want to perform the backup?
 
<P>
Your mysql installation has been resolved. Manually, you would do the following:
<P>
<table bgcolor="lightgrey">
<tr>
<td>
<nobr>cd "<?php echo $dir;?>"</nobr>
<br>
<?php	
	}
	else
	{
?>
The mysql backup utility could not be found automatically. Either do a manual backup, or edit the config.ini and update the backup/mysql Directory entry.
<P>
You can continue to do the backup manually using the following process:
<P>
<table bgcolor="lightgrey">
<tr>
<td>
<?php

	}
?>
<nobr><?php echo $stmt['display'];?>
</table>
<P>
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('home')"> &nbsp;&nbsp; &nbsp; &nbsp; 

<?php
if ($dir != '')
{
?>

<input type=button value="next" onclick="javascript:do_start('Backup')"> 


<?php
}
}

function restoreSelect()
{
	title('Select Backup to Restore');
	
	$dir = resolveTempDir();
	
	$files = array();
	if ($dh = opendir($dir)) 
	{
        while (($file = readdir($dh)) !== false) 
        {
			if (!preg_match('/kt-backup.+\.sql/',$file))
			{
				continue;
			}
        	$files[] = $file;
        }
        closedir($dh);
    }
    
    if (count($files) == 0)
    {
 ?>
 	There don't seem to be any backups to restore from the <i>"<?php echo $dir;?>"</i> directory.
 <?php
    }
    else 
    {
 ?>
 	<P>
 	Select a backup to restore from the list below:
 	<P>
 	<script>
 	function selectRestore(filename)
 	{
 		document.location='?go=RestoreSelected&file=' + filename;
 	}
 	</script>
 	<table border=1 cellpadding=1 cellspacing=1>
 			<tr bgcolor="darkgrey">
			<td>Filename
			<td>File Size
			<td>Action
<?php
	$i=0;
	foreach($files as $file)
	{
		$color=((($i++)%2)==0)?'white':'lightgrey';		
?>
		<tr bgcolor="<?php echo $color;?>">
			<td><?php echo $file;?>
			<td><?php echo filesize($dir . '/'.$file);?>
			<td><input type=button value="restore" onclick="javascript:selectRestore('<?php echo $file;?>')">
<?php		
	}
?>
 	</table>
 <?php   	
    }
   ?>

   <p>
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('welcome')"> 
   <?php

}

function restoreSelected()
{
	$file=$_REQUEST['file'];
	
	$dir = resolveTempDir();
	$_SESSION['backupFile'] = $dir . '/' . $file;
?>
<script>
document.location='?go=RestoreConfirm';
</script>
<?php

}

function restoreConfirm()
{
	if (!isset($_SESSION['backupFile']) || !is_file($_SESSION['backupFile']) || filesize($_SESSION['backupFile']) == 0)
	{
		restoreSelect();
		exit;
	}
		
	title('Confirm Restore');
	$status = $_SESSION['backupStatus'];
	$filename=$_SESSION['backupFile'];	
	$stmt=create_restore_stmt($filename);

	$dir=$stmt['dir'];
	if ($dir != '')
	{
?>
<P>
<P>
Manually, you would do the following to restore the backup:
<P>
<table bgcolor="lightgrey">
<tr>
<td>
<nobr>cd "<?php echo $dir;?>"</nobr>
<br>
<?php	
	}
	else
	{
?>
The mysql backup utility could not be found automatically. Either do a manual restore, or edit the config.ini and update the backup/mysql Directory entry.
<P>
You can continue to do the restore manually using the following command(s):
<P>
<table bgcolor="lightgrey">
<tr>
<td>
<?php

	}
?>
<nobr><?php echo $stmt['display'];?>
</table>
<P>
<?php
if ($dir != '')
{
?>
Press <i>continue to restore</i> to attempt the command(s) above.

<P>
<?php
}
?>
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('home')"> 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="select another backup" onclick="javascript:do_start('RestoreSelect')"> 

<?php
if ($dir != '')
{
?>
<script>
function restore()
{
	if (confirm('Are you sure you want to restore? This is your last chance if the current data has not been backed up.'))
	{
		do_start('Restore');
	}
}
</script>
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="next" onclick="javascript:restore()"> 


<?php
}
}


function backupDone()
{
	check_state(2);
	set_state(3);
	title('Backup Status');
	$status = $_SESSION['backupStatus'];
	$filename=$_SESSION['backupFile'];
	 
	if ($status)
	{
		$stmt=create_restore_stmt($filename);
?>
		The backup file <nobr><I>"<?php echo $filename;?>"</i></nobr> has been created.
		<P> It appears as though the <font color=green>backup has been successful</font>.
		<P>
		<?php
			if ($stmt['dir'] != '')
			{
		?>
				Manually, you would do the following to restore the backup:
				<P>
				<table bgcolor="lightgrey">
				<tr>
					<td>
						<nobr>cd <?php echo $stmt['dir'];?></nobr>
						<br>
		<?php	
			}
			else
			{
		?>
			The mysql backup utility could not be found automatically. Please edit the config.ini and update the backup/mysql Directory entry.
				<P>
				If you need to restore from this backup, you should be able to use the following statements:
				<P>
				<table bgcolor="lightgrey">
				<tr>
					<td>
		<?php
			}
		?>
						<nobr><?php echo $stmt['display'];?>
				</table>
			
<?php
	}
	else 
	{
?>
It appears as though <font color=red>the backup process has failed</font>.<P></P> Unfortunately, it is difficult to diagnose these problems automatically
and would recommend that you try to do the backup process manually.
<P>
We appologise for the inconvenience.
<P>
<table bgcolor="lightgrey">
<tr>
<td>
<?php echo $_SESSION['backupOutput'];?>
</table>
<?php
		
	}
?>
<br>				
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('welcome')"> 
<?php
	if ($status)
	{
		?>
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="next" onclick="javascript:do_start('UpgradeConfirm')"> 
	
<?php	
	}
}
function restoreDone()
{
	check_state(5);
	set_state(6);
	title('Restore Status');
	$status = $_SESSION['restoreStatus'];
	 $filename=$_SESSION['backupFile'];
	 
	if ($status)
	{
		 
?>
		The restore of <nobr><I>"<?php echo $filename;?>"</i></nobr> has been completed. 
		<P>
		It appears as though the <font color=green>restore has been successful</font>.
		<P>


				
<?php
	}
	else 
	{
?>
It appears as though <font color=red>the restore process has failed</font>. <P>
Unfortunately, it is difficult to diagnose these problems automatically
and would recommend that you try to do the backup process manually.
<P>
We appologise for the inconvenience.
<P>
<table bgcolor="lightgrey">
<tr>
<td>
<?php echo $_SESSION['restoreOutput'];?>
</table>
<?php
		
	}
?>

<br>				
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('welcome')"> 

<?php	
	
}

function set_state($value)
{
	$_SESSION['state'] = $value;
}
function check_state($value, $state='Home')
{
	if ($_SESSION['state'] != $value)
	{
		?>
			<script>
			document.location="?go=<?php echo $state;?>";
			</script>
			<?php
			exit;
	}
}

function backup()
{
	check_state(1);
	set_state(2);
	title('Backup In Progress');
	$targetfile=$_SESSION['backupFile'];
	$stmt=create_backup_stmt($targetfile);
	$dir=$stmt['dir'];


	 
	
	if (is_file($dir . '/mysqladmin') || is_file($dir . '/mysqladmin.exe'))
	{
		ob_flush();
		flush();
?>
		The backup is now underway. Please wait till it completes.
<?php

		ob_flush();
		flush();
		$curdir=getcwd();
		chdir($dir);
		ob_flush();
		flush();

		$handle = popen($stmt['cmd'], 'r');
		$read = fread($handle, 10240);
		pclose($handle);
		$_SESSION['backupOutput']=$read;
		$dir=resolveTempDir();
		$_SESSION['backupFile'] =   $stmt['target'];
		
			if (OS_UNIX)
			{
				chmod($stmt['target'],0600);
			}
		
		if (is_file($stmt['target']) && filesize($stmt['target']) > 0)
		{
			$_SESSION['backupStatus'] = true;
	
		}
		else 
		{
			$_SESSION['backupStatus'] = false;
		}
?>
			<script>
			document.location="?go=BackupDone";
			</script>
<?php	
		
		 
	}
	else 
	{
?>
<P>
	The <i>mysqldump</i> utility was not found in the <?php echo $dir;?> subdirectory.
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('welcome')"> 
<?php		
	}



}


function restore()
{
	check_state(1);
	set_state(5);
	title('Restore In Progress');
	$status = $_SESSION['backupStatus'];
	$filename=$_SESSION['backupFile'];	
	$stmt=create_restore_stmt($filename);
	$dir=$stmt['dir'];

	
	
	
	if (is_file($dir . '/mysql') || is_file($dir . '/mysql.exe'))
	{
		 
?>
		The restore is now underway. Please wait till it completes.
<?php
		print "\n";

		 
		$curdir=getcwd();
		chdir($dir);
		 

		$ok=true;
		$stmts=explode("\n",$stmt['cmd']);
		foreach($stmts as $stmt)
		{
			 
			$handle = popen($stmt, 'r');
			if ($handle=='false')
			{
				$ok=false;
				break;
			}
			$read = fread($handle, 10240);
			pclose($handle);
			$_SESSION['restoreOutput']=$read;
		}
		 
		
		 
		
		 
			$_SESSION['restoreStatus'] = $ok;
	
		 
?>
			<script>
			document.location="?go=RestoreDone";
			</script>
<?php	
		
		 
	}
	else 
	{
?>
<P>
	The <i>mysql</i> utility was not found in the <?php echo $dir;?> subdirectory.
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('welcome')"> 
<?php		
	}



}


function welcome()
{
	set_state(1);
?>
<br>
Welcome to the <?php echo APP_NAME;?> Database Upgrade Wizard.<P> If you have just updated
your <?php echo APP_NAME;?> code base, you will need to complete the upgrade process in order to ensure your system is fully operational with the new version.
<P>
You will not be able to log into <?php echo APP_NAME;?> until your the database upgrade process is completed.
<P>
<font color=orange>!!NB!! You are advised to backup the database before attempting the upgrade. !!NB!!</font>
<P>
If you have already done this, you may skip this step can continue directly to the upgrade.
<P>
 
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="cancel" onclick="document.location='..';"> 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="backup now" onclick="javascript:do_start('BackupConfirm');"> 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="next" onclick="javascript:do_start('UpgradeConfirm');"> 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="restore database" onclick="javascript:do_start('RestoreConfirm');"> 


<?php


}


function UpgradePreview()
{
	title('Preview Upgrade');
	global $default;
?>
        <p>The table below describes the upgrades that need to occur to
        upgrade your <?php echo APP_NAME;?> installation to <strong><?php echo $default->systemVersion;?></strong>.
        Click on the button below the table to perform the upgrades.</p>
  <?php
        $upgradeTable = generateUpgradeTable();
	print $upgradeTable;
	?>
	<br> 
 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('home')"> 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="next" onclick="javascript:do_start('Upgrade')"> 
	<?php

}


function Upgrade()
{
	title('Upgrade In Progress');	
	global $default;
?>
        <p>The table below describes the upgrades that have occurred to
        upgrade your <?php echo APP_NAME;?> installation to <strong><?php echo $default->systemVersion;?></strong>.
 
  <?php
    $pre_res = performPreUpgradeActions();
	if (PEAR::isError($pre_res)) 
	{
?>
<font color="red">Pre-Upgrade actions failed.</font><br>
<?php
	} 
	else 
	{
?>
<p>
<font color="green">Pre-Upgrade actions succeeded.</font><br>
<?php
	}
?>
<p>
  <?php
	$res = performAllUpgrades();
	if (PEAR::isError($res) || PEAR::isError($pres)) 
	{
?>
<font color="red">Upgrade failed.</font>
<?php
	} 
	else 
	{
?>
<p>
<font color="green">Upgrade succeeded.</font>
<?php
	}
?>
<p>
  <?php
    $post_pres = performPostUpgradeActions();
	if (PEAR::isError($post_res)) 
	{
?>
<font color="red">Post-Upgrade actions failed.</font><br><br>
<?php
	} 
	else 
	{
?>
<p>
<font color="green">Post-Upgrade actions succeeded.</font><br><br>
<script>
    alert("To complete the upgrade please do the following before continuing:\n\n1. Restart the KnowledgeTree services as appropriate for your environment.\n\n\nOn first run of your upgraded KnowledgeTree installaton please do the following:\n\n1. Hard refresh your bowser (CTRL-F5) on first view of the KnowledgeTree Dashboard.\n2. Enable the new plugins you wish to use.\n\n\nSelect 'next' at the bottom of this page to continue.")
</script>
<?php
	}
?>

&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="back" onclick="javascript:do_start('home')"> 
&nbsp;&nbsp; &nbsp; &nbsp;  <input type=button value="next" onclick="javascript:document.location='..';"> 
<?php	 
}

?>
<tr>
<td height=80 <?php 
  	if($oKTConfig->get('ui/poweredByDisabled') == '0'){
  		?> align="right"><img src="<?php echo $oKTConfig->get('ui/powerLogo');?>"></td>
  	<?php }else{ ?>
	  	background="../resources/graphics/ktbg.png">&nbsp;</td>
	<?php }?>
</table>