<?php

/**
 * @file
 * Functions for the interface to the call monitor recordings
 */

/**
 * Class for settings
 */
class Up_tool {
	function check_imagefile($image_dir,$upload_file){
		$show=true;
		exec("/bin/tar tzf ".$upload_file."
			 /bin/echo $?",$result);
		if ($result[count($result)-1]!="0"){
			$ret.= '<tr><td><font color="red">'.UPDATE_TOOL_CORRUPT_FILE_DIS.'!</font></td></tr>';
		}else{
			chdir($image_dir);
			exec("/bin/tar -x content.txt -zf ".$upload_file. " -C /tmp" );
			if (is_file("/tmp/content.txt"))
				$content=file("/tmp/content.txt");
			chdir($image_dir);
			exec("/bin/tar tzf ".$upload_file,$result);
			for ($i=0;$i<count($result);$i++)
				$result[$i]=trim($result[$i]," /\n\t");
			for ($i=0;$i<count($content);$i++)
				$content[$i]=trim($content[$i]," /\n\t");
			$corrupt=false;


			for ($i=0;$i<count($content);$i++){
					if (!in_array($content[$i],$result)){
						$corrupt=true;
						break;
					}
			}
			if ($corrupt)
				$ret.= '<tr><td><font color="red">'.UPDATE_TOOL_WRONG_FILE_DIS.'!</font></td></tr>';
			else{
				$file = basename($_FILES['userfile']['name']);
				$ret.= '<tr><td style="width:500px"><p><h5>'.UPDATE_TOOL_MESSAGE_INSTALL_DIS.'</h5></p>';
				$ret.= '<p><h3>'.UPDATE_TOOL_QUESTION_INSTALL_DIS.' <font color="red">' . $file . '</font>?</h3></p>';
				$ret.= '<p><h5>('.UPDATE_TOOL_INFO_INSTALL_DIS.').</h5></p>';
				$ret.= "<p><b><a href=index.php?m=up_tool>".UPDATE_TOOL_NO_DIS."</a></b> ";
				$ret.= "<b><a onclick='document.getElementById(\"wait_div\").style.display=\"block\";' href=index.php?m=up_tool&action=do_install&file=$file>".UPDATE_TOOL_YES_DIS."</a></b></p></td></tr></table>";
				$show=false;
			}
		}
		$r[0]=$ret;
		$r[1]=$show;
		return $r;
	}

	function display($args) {
		include '/usr/local/php/beroConf.php';
		#$image_dir = "/mnt/images";
                $image_dir = "/tmp/images";
                system("mkdir -p ".$image_dir);
		//Verbindung mit der root db
		$bc = new beroConf('root');
		//Verbindung mit der beroAri db
		$ba = new beroAri();

		// args
		$m = $_GET['m'];
		$q = $_GET['q'];
		$start = $_GET['start'];
		$show=true;
		include('./includes/header.php');
		$ret.= '<br/><table class="contenttoc" width="55%">';
		switch ($_GET['action']) {
			//Hochladen image file
			case 'upload':
				if (is_dir($image_dir) && ($dh = opendir($image_dir))) {
					while (($file = readdir($dh)) != false) {
						if (strlen($file) > 7 && substr_compare($file, ".tar.gz", -7, 7) == 0) {
							$file_ = $image_dir . '/' . $file;
							if (strlen($file_) != 0 && file_exists($file_)) unlink($file_);
						}
						if ($file=="content.txt"){
							$file_ = $image_dir . '/' . $file;
							if (strlen($file_) != 0 && file_exists($file_)) unlink($file_);
						}
					}
				}
				$split_ext=explode(".",$_FILES['userfile']['name']);

				if ($split_ext[count($split_ext)-1]=="gz" && $split_ext[count($split_ext)-2]=="tar" && count($split_ext)>=3){
					$upload_file = $image_dir . '/' . basename($_FILES['userfile']['name']);
					if ((filesize($_FILES['userfile']['tmp_name'])/1024)/1024>7.9){
						$ret.= '<tr><td><font color="red">'.UPDATE_TOOL_ERROR_SIZE_DIS.'!</font></td></tr>';
					}else{
						if (move_uploaded_file($_FILES['userfile']['tmp_name'], $upload_file)){
						/*	$r=$this->check_imagefile($image_dir,$upload_file);
							$ret.=$r[0];
							$show=$r[1];*/


							//Falls die Zeilen davor wieder einkommentiert werden, müssen die folgenden Zeilen bis $show=false; gelöscht werden
							$file = basename($_FILES['userfile']['name']);
							$fpga=exec("/bin/cat /proc/FPGA_VERSION");
							if ($fpga<8) $ret.= '<tr><td style="width:500px"><p><font color="red">'.UPDATE_TOOL_FPGA_WARNING_DIS.'</font></p>';
							$ret.= '<tr><td style="width:500px"><p><h5>'.UPDATE_TOOL_MESSAGE_INSTALL_DIS.'</h5></p>';
							$ret.= '<p><h3>'.UPDATE_TOOL_QUESTION_INSTALL_DIS.' <font color="red">' . $file . '</font>?</h3></p>';
							$ret.= '<p><h5>('.UPDATE_TOOL_INFO_INSTALL_DIS.').</h5></p>';
							$ret.= "<p><b><a href=index.php?m=up_tool>".UPDATE_TOOL_NO_DIS."</a></b> ";
							$ret.= "<b><a onclick='document.getElementById(\"wait_div\").style.display=\"block\";' href=index.php?m=up_tool&action=do_install&file=$file>".UPDATE_TOOL_YES_DIS."</a></b></p></td></tr></table>";
							$show=false;
						}else{
							$ret.= '<tr><td>'.UPDATE_TOOL_ERROR_FAILED_DIS.'!</td></tr>';
						}
					}
				}else{
					$ret.= '<tr><td><font color="red">'.UPDATE_TOOL_ERROR_FILE_EXTENSION_DIS.'!<br/>'.UPDATE_TOOL_ERROR_FAILED_DIS.'!</font></td></tr>';
				}
				break;
			//Installieren des ausgewaehlten image file
			case 'do_install':
				system("ps ax|grep -v grep|grep install.sh > /dev/null", $retval);
				if ($retval == 0)
					break;
					$old=$bc->get("root", "app-image");
				$file = $_GET['file'];
				if (strlen($file) != 0 && file_exists($image_dir . '/' . $file)) {
					$ret.= '<p><b>Install log:</b></p>';
					$ret.= '<pre>';
					exec("/usr/local/sbin/install.sh $file noreboot", $retval);
					foreach($retval as $key=>$value) $ret.=$value."<br/>";
					$ret.= '</pre>';
					$new=$bc->get("root", "app-image");
					$date=exec("date");
					exec("echo '$date $old => $new'>> /usr/conf/update.log",$val);

				}

				break;
		}


		switch ($show) {
			case true:
			if (!$bc->valid()) {
				$ret.= "<tr><td><h3>Error:</h3><b>".UPDATE_TOOL_ERROR_CONNECTION_DIS."!</b></td></tr>";
				break;
			}
			if (!is_dir($image_dir) || !($dh = opendir($image_dir))) {
				$ret.= "<tr><td><h3>Error:</h3><b>".UPDATE_TOOL_ERROR_READ_DIS." $image_dir!</b></td></tr>";
				break;
			}




			$ret.= '<tr><td><font size="3"><b>'.UPDATE_TOOL_TABLE_HEADER_DIS.':</b></font></td></tr>';
			$ret.= '<form class="beroari_form" enctype="multipart/form-data" action="index.php?m=up_tool&action=upload" method="POST">';
			$ret.= '<input type="hidden" name="MAX_FILE_SIZE" value="52428800" />';
			$ret.= '<tr><td>&bull;&nbsp;'.UPDATE_TOOL_IMAGE_FILE_DIS.': <input name="userfile" type="file" onmouseover=\'Tip("'.UPDATE_TOOL_FILE.'")\' onmouseout="UnTip()"/>';
			$ret.= '&nbsp;&nbsp;&nbsp;<input type="submit" value="'.BUTTON_UPLOAD_FILE.'" onclick="document.getElementById(\'wait_div\').style.display=\'block\';" onmouseover=\'Tip("'.UPDATE_TOOL_UPLOAD.'")\' onmouseout="UnTip()"/></td></tr>';
			$ret.= '</form></table>';
			break;
		}


		return $ret;
	}


}


?>
