<?php
// variables
$filename = "./config.conf";
$memStep = 64;
$memDefault = 512;
$memMax = 3584; // 32-bit JRE limit
$guid = "n/a";
$app_log_file = "../log/app.log";
$cp_version = "Could not find it";
$ex_ip = "192.168.1.2";

if(file_exists($app_log_file)) {
    exec("/bin/grep -i CPVERSION $app_log_file | awk '/CPVERSION/{print $3}'", $temp);
    $cp_version = $temp[0];
    $temp = array();
    exec("/bin/grep -i CPGUID $app_log_file | awk '/CPGUID/{print $3}'", $temp);
    $guid = $temp[0];
}

// find memory available on device
$memTotal = ceil(round(exec("awk '/^MemTotal:/{print $2}' /proc/meminfo") / 1024, 0) / $memStep) * $memStep;
if($memTotal > $memMax) $memTotal = $memMax;

// find network interfaces available on device
$net_ifaces = array();
exec("for iface in $(/usr/bin/find /sys/class/net/ -type l | /bin/grep -iv \"lo\"); do iface=$(/usr/bin/basename \$iface); if /sbin/ifconfig \$iface | /bin/grep -i inet >/dev/null 2>&1; then echo \"\$iface;\$(/sbin/ifconfig \$iface | awk '/addr:/{print \$2}' | cut -f2 -d:;)\"; fi; done", $call_output);

// create an array from that for next operations (even though most people will only have a single interface w/ an ip addr defined)
foreach($call_output as $tmp) {
    list($iface, $ip) = explode(';', $tmp);
    if($ip) { $net_ifaces[$iface] = $ip; $ex_ip = $ip; }
    unset($tmp, $iface, $ip);
}
unset($call_output);

// Save posted data
$config = array();
if(isset($_POST['posted'])) {
    $config = array("interface" => $_POST['interface'], "memory" => $_POST['memory']);

    // Save new config
    $handle = fopen($filename, "w+");
    if($config['interface']) { fwrite($handle, "interface=" . $config['interface'] .  "\n"); }
    fwrite($handle, "memory=" . $config['memory']);
    fclose($handle);
} elseif(file_exists($filename)) {
    // Read config file content
    $fileContent = explode("\n", file_get_contents($filename));
    while(list(, $item) = each($fileContent)) {
        $cfgLine = explode("=", $item);
        $config[$cfgLine[0]] = $cfgLine[1];
    }
}

// was an iface/ip configured
$ip_configured = true;
if(!isset($config['interface']) || (isset($config['interface']) && !array_key_exists($config['interface'], $net_ifaces))) { $ip_configured = false; }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>CrashPlan Administration</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <link rel="icon" type="image/gif" href="images/favicon.ico" />
        <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
        <div id="main">
            <div id="top">
                Online destination status: <a href="http://crashplan.probackup.nl/status" target="_blank">crashplan.probackup.nl</a>
                <div class="topSpace"><a href="cgi-bin/backup.cgi"><img src="images/download.gif" />Download configuration and log files</a></div>
            </div>
            <form method="post">
                <div id="bottomLeft">
		    		<img src="images/id.gif" width="16" height="16" />
                    Version: <b><?php echo $cp_version; ?></b>
					<div class="topSpace">
                        <img src="images/id.gif" width="16" height="16" />
                        GUID: <b><?php echo $guid; ?></b>
		    		</div>
					<div class="topSpace">
                        <img src="images/<?php if(!$ip_configured) { echo "warning.gif"; } else { echo "success.gif";  } ?>"<?php if(!$ip_configured) { echo " title=\"Listening IP not yet set!\""; } ?> width="16" height="16" />
                        IP CrashPlan will be listening on:
                        <select name="interface">
                            <?php if(!$ip_configured) { ?><option value="" SELECTED>-</option><?php } ?>
                            <?php foreach($net_ifaces as $iface => $ip) { ?>
                            <option value="<?php echo $iface ?>"<?php if(isset($config['interface']) && $config['interface']=="$iface") { echo " SELECTED"; $ex_ip = $ip; }?>><?php echo $ip; ?></option>
                            <?php } ?>
                        </select>
                    </div>
					<div class="topSpace">
                        <img src="images/ram.gif" width="16" height="16" />
                        CrashPlan's Java maximum memory allocation
                        <select name="memory">
                            <?php for($m = $memStep; $m <= $memTotal; $m += $memStep) { ?>
                            <option value="<?php echo ($m); ?>"<?php if(isset($config['memory']) && $config['memory'] == $m || !isset($config['memory']) && $memDefault == $m) { echo " selected"; } ?>><?php echo ($m) ?> Mb<?php if($m == $memDefault) { echo " (default)"; } ?></option>
                            <?php } ?>
                        </select>
                    </div>
                </div>
				<div id="bottomRight">
                    <input type="submit" value="Save" name="posted" /> You will have to stop and start CrashPlan to take changes into account!
                </div>
            </form>
			<div>
				<h3>Workstation installation</h3>
				<ul>
					<li>Install the CrashPlan client.</li>
					<li>Optional: stop and disable the &quot;CrashPlan Backup Service&quot; on your workstation.</li>
					<li>Locate your ./CrashPlan/conf/<strong>ui.properties</strong> file</li>
					<li>Open that file with a text editor</li>
					<li>Insert a new line without the quotes: &quot;<tt>serviceHost=<?php echo ($ex_ip) ?></tt>&quot;</li>
					<li>Save ui.properties, and start CrashPlan</li>
					<li>In case &quot;<em>Unable to connect to the local backup engine, retry? [ No ] [ Yes ]</em>&quot;, do stop and start CrashPlan on your QNAP.</li>
				</ul>
			
			</div>
        </div>
    </body>
</html>
