<?php
	$logos="logos"; // sous repertoire pour mettre des logos

	// astuce pour permettre de recuperer le code
        if(isset($_GET['code']) && $_GET['code'] == "oui") {
            $file=basename($_SERVER['PHP_SELF']);
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.$file.'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            readfile($file);
            exit;

        }

	$text = "";
	if(isset($_POST['text'])) { $mtd = "post"; $text = $_POST['text']; $logo = $_POST['logo']; }
	else { $mtd = "get"; if(isset($_GET['text'])) { $text = $_GET['text']; $logo = $_GET['logo']; } }

	if($text != "") {

	function erreur($fichier) {
		echo "<html><body><pre>impossible d'ouvrir $fichier.html\n";
		$libele = fopen("$fichier.err", "r")
			or die("impossible d'ouvrir $fichier.err");
		echo fread($libele,filesize("$fichier.err"));
		fclose($libele)
			and die("commande <a href=\"https://cdk.readthedocs.io\">cdk</a> plant&eacute;e\n</body></html>");
		
	}
	$fichier="/tmp/presentation" . rand();
	$presentation = fopen($fichier, "w");
	fwrite($presentation, $text) or die("impossible d'ecrire dans $presentation");
	fclose($presentation);
	if(is_file($logo)) { $logo = "--logo $logo"; } else { $logo = ""; }
	exec("/usr/local/bin/cdk $logo $fichier 2> $fichier.err");
	$presentation = fopen("$fichier.html", "r")
		or erreur($fichier);
	echo fread($presentation,filesize("$fichier.html"));
	fclose($presentation);
	exec("/bin/rm $fichier $fichier.html $fichier.err");
	}
	else {
?>
<html><body>
<a href="https://cdk.readthedocs.io">
<h2>cdk --generate=&lt;name&gt;</h2></a>
<hr>
<?php if($mtd == "get") { ?>
le formulaire utilise la methode "get" pour que l'URL stocke la presentation
<form method="post">
<input type="hidden" name="text" value="">
<input type=submit value="methode post + securis&eacute;e">
</form>
<?php } ?>
<form method="<?php echo $mtd; ?>" action="">
<textarea name="text" rows=16 cols="90">
= My Presentation

Author: @somebody

== My First Slide

Presentation software for engineers

[options="incremental"]
* Content should be simple but presentation stylish.
* Author slides in plain text (asciidoc)
* And play them in the browser.

== My Second Slide

Use the source!

[source,python]
----
>>> x = list("Simeon")
>>> x.lower()
"simeon"

---- 

https://<?php echo $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']; ?>[recomposer]

</textarea><br>
<input type=submit value="lancer la pr&eacute;sentation">
<?php
	if(is_dir($logos)) {
		if ($liste = opendir($logos)){
     			echo '<br><input type="radio" name="logo" value="">aucun logo';
			echo "<table><tr>";
    			while (($image = readdir($liste)) !== false){
     				echo '<td><input type="radio" name="logo" value="' . $logos . '/' . $image . '"><img src="' . $logos .'/' . $image . '">';
    			}
     			echo '<td><input type="radio" name="logo" value=""><h2>aucun<br>logo';
			echo '<td><input type=submit value="lancer la pr&eacute;sentation">';
			echo "</table>";
    			closedir($opendirectory);
		}
	}
?>
</form>
[<a href="<?php echo $_SERVER['PHP_SELF'] . "?code=oui"; ?>">le code</a>]
</body></html>
<?php } ?>
