<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.1 www.exoops.info 2006/05
// $Id: upgrade.php,v 1.6 2003/03/19 06:57:10 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Half-Dead
// Author Website : http://www.e-xoops.com
// License Type   : LGPL: See /manual/LICENSES/LGPL.txt
// ------------------------------------------------------------------------- //
ob_start();
include_once('include/functions.php');

function checkchmod() {

	$errorcount = 0;
	$errors = "";
	$errors .= "<table>";
	
	// Open, read, and close file.
		$fp = fopen("./cfg/chmodlist.txt", "r");
		$ftp_data = file("./cfg/chmodlist.txt");
	
	// Begin for loop.
	for ($n=0; $n<count($ftp_data); $n++) {
		// Read each line and split into path and chmod value.
		$chmod_line = explode(" ", $ftp_data[$n]);
		// Correct 'extra space' error.
		$chmod_line[1] = eregi_replace("[[:space:]]", "", $chmod_line[1]);
		
		// Assign variables (for clarity sake).
		$path_value = XOOPS_ROOT_PATH."/"; 
		$path_value .= $chmod_line[0];
		$chmod_value = $chmod_line[1];
		$write = is_writable($path_value);
		$read = is_readable($path_value);
		
		switch ($chmod_value) {
		
			case '644':
				if ($write == false && $read == true) { 
				} else { 
					$errors .= "<tr><td><span style='color:#ff0000;'>$path_value $chmod_value </span></td></tr>";
					$errorcount++;
				}
			break;
			
			case '755':
				if ($write == false && $read == true) { 
				} else {
					$errors .= "<tr><td><span style='color:#ff0000;'>$path_value $chmod_value </span></td></tr>";
					$errorcount++;
				}
			break;
			
			case '666':
				if ($write == true && $read == true) { 
				} else {
					$errors .= "<tr><td><span style='color:#ff0000;'>$path_value $chmod_value </span></td></tr>";
					$errorcount++;
				}
			break;
			
			case '777':
				if ($write == true && $read == true) { 
				} else {
					$errors .= "<tr><td><span style='color:#ff0000;'>$path_value $chmod_value </span></td></tr>";
					$errorcount++;
				}
			break;
			
			case '444':
				if ($write == false && $read == true) {
				} else { 
					$errors .= "<tr><td><span style='color:#ff0000;'>$path_value $chmod_value </span></td></tr>";
					$errorcount++;
				}
			break;
			
		}
		
		
}
	// End for loop.
	$errors .= "</table>";
	if ($errorcount > 0) {return $errors;} else { return true;}
}

if ( !empty($_POST['action']) ) {
	include_once('../mainfile.php');

	$package = $_POST['action'];

	if ( file_exists('upgrade/'.$package.'/language/'.$bcoosConfig['language'].'/main.php') ) {
		include_once('upgrade/'.$package.'/language/'.$bcoosConfig['language'].'/main.php');
		} elseif ( file_exists('upgrade/'.$package.'/language/english/main.php') ) {
			include_once('upgrade/'.$package.'/language/english/main.php');
		}

	wiz_header(_UPGRADE_TITLE);
	if ( file_exists('upgrade/'.$package.'/language/'.$bcoosConfig['language'].'/readme.txt') ) {
		echo "<div align='center'>";
		printf(_INSTALL_U_README, 'upgrade/'.$package.'/language/'.$bcoosConfig['language'].'/readme.txt');
		echo "</div><br /><br />";
		} elseif ( file_exists('upgrade/'.$package.'/language/english/readme.txt') ) {
			echo "<div align='center'>";
			printf(_INSTALL_U_README, 'upgrade/'.$package.'/language/english/readme.txt');
			echo "</div><br /><br />";
		}

	if ( empty($_POST['upgrade']) ) {
		if ( file_exists('upgrade/'.$package.'/language/'.$bcoosConfig['language'].'/main.html') ) {
			readfile('upgrade/'.$package.'/language/'.$bcoosConfig['language'].'/main.html');
			} elseif ( file_exists('upgrade/'.$package.'/language/english/main.html') ) {
				readfile('upgrade/'.$package.'/language/english/main.html');
			}

		?>
		<div align="center">
		<form method="post" action="upgrade.php">
		<input type="hidden" name="action" value="<?php echo $package;?>">
		<input type="hidden" name="upgrade" value="1">
		<input type="submit" class="button" name="submit" value="<?php echo _SUBMIT;?>">
		</form>
		</div>
		<?php

		} else {
			echo "<table width='75%' align='center'><tr><td>";
			if (defined('_UPGRADE_HEADER')) {
				echo _UPGRADE_HEADER;
			}
			include_once('upgrade/'.$package.'/sql.php');
			if (defined('_UPGRADE_FOOTER')) {
				echo _UPGRADE_FOOTER;
			}
			echo "</td></tr></table>";
			
			
			$chmoddone = checkchmod();

			if (is_bool($chmoddone)) {
			
				echo "<table width='60%' align='center'><tr><td>";
				echo "<br /><br />"._MI_DOCHMOD_OKTITLE."<br />";
				echo _MI_DOCHMOD_OKDESCRIPTION."<br />";
				echo _INSTALL_F_CONGRAT."<br /><br />";
				echo "<a href='".XOOPS_URL."'>".XOOPS_URL."</a><br /><br />";
				echo "</td></tr></table>";
			
			} else {
			
				$extensions = get_loaded_extensions();
				if (in_array("ftp", $extensions)) {
				?>
				<table width="60%" align="center"><tr>
				<td colspan="2">
				
				<p><?php echo _MI_DOCHMOD_TEXT ?></p>
				<form action="ftp.php" method="POST">
				<p><?php echo _MI_DOCHMOD_FTPDOMAIN ?><br><input type="text" size="50" name="ftp_server" value="<?php echo $_POST['ftp_server']; ?>" class="text"></p>
				<p><?php echo _MI_DOCHMOD_FTPUSER ?><br><input type="text" size="20" name="ftp_user_name" value="<?php echo $_POST['ftp_user_name']; ?>" class="text"></p>
				<p><?php echo _MI_DOCHMOD_FTPPASS ?><br><input type="password" size="20" name="ftp_user_pass" value="<?php echo $_POST['ftp_user_pass']; ?>" class="text"></p>
				<p><input type="submit" name="login" value="<?php echo _MI_DOCHMOD_BUTTON ?>" class="button"></p>
				<p><?php echo "<br />"; ?></p>
				<p><?php echo _MI_DOCHMOD_MANUAL."<br /><br />" ?></p>
				<p><?php echo $chmoddone ?></p>
				</td>
				
				</tr><tr><td colspan=2 ><span style="color:#ff0000;">
				<?php 
				} else {
				echo _MI_DOCHMOD_MANUAL."<br /><br />";
				echo $chmoddone;
				} 
				
				?>
				</span></td></tr>
				</table>
				<?php
				
			}			
			
			
			
		}

} else {
		wiz_header(_UPDATE);
		include_once('../class/xoopslists.php');
		$upgrade_options = XoopsLists::getDirListAsArray('upgrade/');
		?>
		<div align="center"><br />
		<?php echo _INSTALL_U_CHOOSE;?><br />
		<form method="post" action="upgrade.php">
		<select class="select" name="action">
		<?php
		foreach ($upgrade_options as $key) {
			echo "<option value='$key'>$key</option>";
		}
		?>
		</select>
		<input type="submit" class="button" name="submit" value="<?php echo _SUBMIT;?>">
		</form>
		(<?php echo _INSTALL_U_NOTE;?>)
		<br /><br />
		</div>
		<?php
}

wiz_footer();
ob_end_flush();
?>
?>
