<?php
/*
	univnautes.inc
	Copyright (C) 2011 Entr'ouvert
*/

function univnautes_sync() {
	global $config;

        $dir = "/var/lib/univnautes";
        if (!is_dir($dir))
		mkdir( $dir, 0700, true );

	if (!is_array($config['installedpackages']['univnautes']))
		return;
	if (!is_array($config['installedpackages']['univnautes']['config']))
		return;
	if (!is_array($config['installedpackages']['univnautes']['config'][0]))
		return;

        $conf = $config['installedpackages']['univnautes']['config'][0];

	conf_mount_rw();

	$vars = array();
	# default values

	$b64s = array( "samlkey", "samlcrt",
		       "samlkeyidp", "samlcrtidp",
		       "mdca", "mdcrt",
		       "mdspca", "mdspcrt",
		       "wlca", "wlstaticips",
		       "mdlocal",
		       "userbl", "macbl", "firsturlbl" );
	foreach($b64s as $b64) {
		if (isset($conf[$b64]) && strlen($conf[$b64]) > 0) {
			$data = base64_decode($conf[$b64]);
			$fname = $dir . "/" . $b64;
			$f = fopen( $fname, "wb");
			chmod( $fname, 0600);
			fwrite($f, $data);
			fclose($f);
			$vars[$b64] = $fname;
		}
	}

	$urls = array( "mdurl", "mdspurl", "wlurl", "defaultidp", "redirecturl" );
	foreach($urls as $url) {
		if ( isset($conf[$url]) && (strlen($conf[$url]) > 0) && (parse_url($conf[$url]) != FALSE) ) {
			$vars[$url] = $conf[$url];
		}
	}

	$booleans = array( "enableidp" );
	foreach($booleans as $boolean) {
		if ( isset($conf[$boolean]) && (strlen($conf[$boolean]) > 0) ) {
			$vars[$boolean] = "on";
		} else {
			$vars[$boolean] = "off";
		}
	}

	$contents = "# DO NOT EDIT THIS FILE\n";
	foreach($vars as $var => $val) {
		$contents = $contents . strtoupper($var) . "=\"" . $val . "\"\n";
	}
	file_put_contents( $dir . "/vars.sh", $contents );

	conf_mount_ro();
}

function univnautes_restart_all() {
	# restart service, update metadata and whitelist
	mwexec_bg("/usr/local/etc/rc.d/univnautes.sh restart");
	mwexec_bg("/usr/local/etc/rc.d/macblacklist.sh restart");
	mwexec_bg("/usr/local/bin/univnautes-update-metadata.sh");
	mwexec_bg("/usr/local/bin/univnautes-update-local-metadata.sh");
	mwexec_bg("/usr/local/bin/univnautes-update-whitelist.sh");
}

function univnautes_texts_sync() {
	mwexec_bg("/usr/local/etc/rc.d/univnautes.sh restart");
	mwexec_bg("/usr/local/bin/univnautes-update-map.sh");
	univnautes_texts_build();
}

function univnautes_texts_build() {
	global $config;

        $dir = "/var/lib/univnautes/templates";
        if (!is_dir($dir))
		mkdir( $dir, 0755, true );

	if (!is_array($config['installedpackages']['univnautestexts']))
		return;
	if (!is_array($config['installedpackages']['univnautestexts']['config']))
		return;
	if (!is_array($config['installedpackages']['univnautestexts']['config'][0]))
		return;

        $conf = $config['installedpackages']['univnautestexts']['config'][0];

	conf_mount_rw();

	$names = array(
		"base" => array("header", "footer", "disclaimer", "univinfo"),
	);

	foreach($names as $basename => $blocks) {
		$contents = "{% extends \"" . $basename . "0.html\" %}\n\n";
		foreach($blocks as $block) {
			print $block . "<br />";
			if (isset($conf[$block]) && strlen($conf[$block]) > 0) {
				$html = utf8_encode(base64_decode($conf[$block]));
				$contents = $contents . "{% block " . $block . " %}\n" . $html . "\n{% endblock %}\n";
			}
		}
		file_put_contents( $dir . "/" . $basename . ".html", $contents );
	}

        $dir = "/var/lib/univnautes";
	$b64s = array( "geourls", "geolocal");
	foreach($b64s as $b64) {
		$fname = $dir . "/" . $b64;
		if (isset($conf[$b64]) && strlen($conf[$b64]) > 0) {
			$data = base64_decode($conf[$b64]);
			$f = fopen( $fname, "wb");
			chmod( $fname, 0600);
			fwrite($f, $data);
			fclose($f);
		} else {
			unlink($fname);
		}
	}

	conf_mount_ro();
}

?>
