Add an answer system

This commit is contained in:
Némunaire 2012-01-19 00:23:36 +01:00
parent e77133c393
commit c70382760a
4 changed files with 131 additions and 25 deletions

View File

@ -1,7 +1,34 @@
<?php <?php
// The directory where code will be stored
define("DESTINATION", "../files/"); define("DESTINATION", "../files/");
if (!is_writable(DESTINATION))
die ("Destination folder not writable");
// Path to the GeSHi languages
define("GESHI_DIR", "../geshi/geshi/"); define("GESHI_DIR", "../geshi/geshi/");
// The size of the generated identifier
define("NB_CHAR", 5);
// If you increase NB_CHAR, in order to help review the old codes published, you
// can adjust the minimum
define("ALLOW_NB_MIN", 5);
/*********************************************
* Don't make any change under this comment ! *
*********************************************/
if (ALLOW_NB_MIN != NB_CHAR)
{
if (ALLOW_NB_MIN > NB_CHAR)
define("RGXP_NB", NB_CHAR.",".ALLOW_NB_MIN);
else
define("RGXP_NB", ALLOW_NB_MIN.",".NB_CHAR);
}
else
define("RGXP_NB", NB_CHAR);
?> ?>

View File

@ -12,26 +12,52 @@ require_once("../common.php");
foreach ($_GET as $k => $t) foreach ($_GET as $k => $t)
{ {
if (preg_match("#^[a-zA-Z0-9]{5}$#", $k) && is_file(DESTINATION . "/" . $k . ".xml")) if (preg_match("#^[a-zA-Z0-9]{".RGXP_NB."}$#", $k)
&& is_file(DESTINATION . "/" . $k . ".xml"))
{ {
require_once("../geshi/geshi.php"); require_once("../geshi/geshi.php");
$doc = new DOMDocument(); $doc = new DOMDocument();
$doc->load(DESTINATION . "/" . $k . ".xml"); $doc->load(DESTINATION . "/" . $k . ".xml");
$lang = $doc->getElementsByTagName("language")->item(0)->textContent; $lang = strtolower($doc->getElementsByTagName("language")->item(0)->textContent);
if (empty($lang) || !is_file(GESHI_DIR.$lang.".php")) if (empty($lang) || !is_file(GESHI_DIR.$lang.".php"))
$lang = "whitespace"; $lang = "text";
$geshi = new GeSHi( $geshi = new GeSHi(
$doc->getElementsByTagName("content")->item(0)->textContent, $doc->getElementsByTagName("content")->item(0)->textContent,
$lang); $lang);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
?> ?>
<div id="corps" style="text-align: center;"> <div id="corps" style="text-align: center;">
<h1><?php echo $doc->getElementsByTagName("title")->item(0)->textContent ?></h1> <h1>
<h2>Posté par <?php $a = $doc->getElementsByTagName("author")->item(0)->textContent; if (empty($a)) echo "<em>un anonyme</em>"; else echo $a; ?>, le <?php echo strftime("%A %e %B %G à %H:%M:%S", $doc->getElementsByTagName("date")->item(0)->textContent); ?></h2> <?php echo htmlentities(utf8_decode($doc->getElementsByTagName("title")->item(0)->textContent)); ?>
</h1>
<h2>
Posté par <?php
$a = $doc->getElementsByTagName("author")->item(0)->textContent;
if (empty($a))
echo "<em>un anonyme</em>";
else
echo htmlentities(utf8_decode($a));
?>, le <?php
echo strftime("%A %e %B %G à %H:%M:%S",
$doc->getElementsByTagName("date")->item(0)->textContent);
?></h2>
<div id="content"> <div id="content">
<div class="answer">
<a href="/?a=<?php echo $k; ?>">Répondre</a>
<?php
$ref = $doc->getElementsByTagName("ref");
if ($ref->length > 0)
{
$r = $ref->item(0)->textContent;
echo '<a href="/?'.$r.'">Voir l\'original</a>';
}
?>
</div>
<?php <?php
echo $geshi->parse_code(); echo $geshi->parse_code();
?> ?>
@ -54,17 +80,29 @@ if (!empty($view))
<form method="post" action="save.php"> <form method="post" action="save.php">
<fieldset class="paste_form"> <fieldset class="paste_form">
<label for="title">Titre :</label> <label for="title">Titre :</label>
<input type="text" maxlength="200" size="50" id="title" name="title"><br><br> <input type="text" maxlength="200" size="50" id="title" name="title">
<br><br>
<label for="content">Contenu :</label><br> <label for="content">Contenu :</label><br>
<textarea id="content" name="content"></textarea><br><br> <textarea id="content" name="content"><?php
if (!empty($_GET["a"]) && preg_match("#^[a-zA-Z0-9]{".RGXP_NB."}$#", $_GET["a"])
&& is_file(DESTINATION . "/" . ($k = $_GET["a"]) . ".xml"))
{
$doc = new DOMDocument();
$doc->load(DESTINATION . "/" . $k . ".xml");
echo htmlentities($doc->getElementsByTagName("content")->item(0)->textContent);
$language = strtolower($doc->getElementsByTagName("language")->item(0)->textContent);
$ref = $k;
}
?></textarea><br><br>
<label for="author">Auteur :</label> <label for="author">Auteur :</label>
<input type="text" maxlength="64" size="35" id="author" name="author"> <input type="text" maxlength="64" size="35" id="author" name="author">
<label for="lang">Langage :</label> <label for="lang">Langage :</label>
<select id="lang" name="lang"> <select id="lang" name="lang">
<option value=""> Fundamental</option> <option value=""> Text</option>
<?php <?php
if ($dh = opendir(GESHI_DIR)) if ($dh = opendir(GESHI_DIR))
@ -80,12 +118,20 @@ if (!empty($view))
sort($lg); sort($lg);
foreach ($lg as $l) foreach ($lg as $l)
echo "<option> ".$l."</option>\n"; {
if (isset($language) && $language == $l)
echo "<option selected=\"selected\"> ".ucfirst($l)."</option>\n";
else
echo "<option> ".ucfirst($l)."</option>\n";
}
} }
?> ?>
</select> </select>
<?php
if (!empty($ref))
echo '<input type="hidden" name="ref" value="'.$ref.'">';
?>
<input type="submit" value="Envoyer"> <input type="submit" value="Envoyer">
</fieldset> </fieldset>
</form> </form>

View File

@ -8,22 +8,44 @@ if (!empty($_POST["content"]))
$xml_paste = $xml->createElement("paste"); $xml_paste = $xml->createElement("paste");
$xml_paste->appendChild($xml->createElement("title", $_POST["title"])); $xml_paste->appendChild(
$xml_paste->appendChild($xml->createElement("author", $_POST["author"])); $xml->createElement("title", $_POST["title"]));
$xml_paste->appendChild($xml->createElement("language", $_POST["lang"])); $xml_paste->appendChild(
$xml_paste->appendChild($xml->createElement("date", time())); $xml->createElement("author", $_POST["author"]));
$xml_paste->appendChild($xml->createElement("ip", $_SERVER["REMOTE_ADDR"])); $xml_paste->appendChild(
$xml_paste->appendChild($xml->createElement("content", $_POST["content"])); $xml->createElement("language", $_POST["lang"]));
$xml_paste->appendChild(
$xml->createElement("date", time()));
$xml_paste->appendChild(
$xml->createElement("ip", $_SERVER["REMOTE_ADDR"]));
$xml_paste->appendChild(
$xml->createElement("content", $_POST["content"]));
if (!empty($_POST["ref"]))
$xml_paste->appendChild(
$xml->createElement("ref", $_POST["ref"]));
$hash = base64_encode(md5($_POST["content"], true));
$xml_paste->appendChild(
$xml->createElement("hash", $hash));
$xml->appendChild($xml_paste); $xml->appendChild($xml_paste);
$filename = substr(str_replace("+", "", str_replace("/", "", base64_encode(md5($xml->saveXML(), true)), 0, 5))); //Save the paste
$filename = substr(
$xml->save(DESTINATION . "/" . $filename . ".xml"); str_replace("+", "",
str_replace("/", "",
$hash)), 0, NB_CHAR);
if ($xml->save(DESTINATION . "/" . $filename . ".xml"))
{
//Redirect the user to is paste
header("Location: /?".$filename); header("Location: /?".$filename);
exit; exit;
} }
else
die ("Sorry, an error occured while saving the file. Please try again later.");
}
header("Location: /"); header("Location: /");
?> ?>

View File

@ -1,6 +1,7 @@
body body
{ {
background: url('img/background.jpg'); background: url('img/background.jpg');
margin-bottom: 234px;
} }
header header
@ -95,10 +96,20 @@ fieldset, div#content {
div#content { div#content {
padding: 5px; padding: 5px;
text-align: left; text-align: left;
width: 80%; min-width: 80%;
}
div#content pre {
overflow-x: auto;
} }
textarea#content { textarea#content {
height: 200px; height: 200px;
width: 90%; width: 90%;
} }
div.answer {
float: right;
font-size: 65%;
font-style: italic;
}