Factoring code into a single class
This commit is contained in:
parent
bbddcc3231
commit
3898e64c97
@ -31,4 +31,6 @@ if (ALLOW_NB_MIN != NB_CHAR)
|
||||
else
|
||||
define("RGXP_NB", NB_CHAR);
|
||||
|
||||
require_once("paste.class.php");
|
||||
|
||||
?>
|
@ -13,51 +13,27 @@ require_once("../common.php");
|
||||
|
||||
foreach ($_GET as $k => $t)
|
||||
{
|
||||
if (preg_match("#^[a-zA-Z0-9]{".RGXP_NB."}$#", $k)
|
||||
&& is_file(DESTINATION . "/" . $k . ".xml"))
|
||||
if (preg_match("#^([a-zA-Z0-9]{".RGXP_NB."})(:([a-zA-Z0-9]{".RGXP_NB."}))?$#", $k, $kout)
|
||||
&& is_file(Paste::get_path($kout[1])))
|
||||
{
|
||||
require_once("../geshi/geshi.php");
|
||||
|
||||
$doc = new DOMDocument();
|
||||
$doc->load(DESTINATION . "/" . $k . ".xml");
|
||||
$paste = new Paste($kout[1]);
|
||||
|
||||
$lang = strtolower($doc->getElementsByTagName("language")->item(0)->textContent);
|
||||
if (empty($lang) || !is_file(GESHI_DIR.$lang.".php"))
|
||||
$lang = "text";
|
||||
|
||||
$geshi = new GeSHi(
|
||||
$doc->getElementsByTagName("content")->item(0)->textContent,
|
||||
$lang);
|
||||
$geshi = new GeSHi($paste->content, $paste->language);
|
||||
|
||||
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5);
|
||||
|
||||
?>
|
||||
<div id="corps" style="text-align: center;">
|
||||
<h1>
|
||||
<?php echo htmlentities(utf8_decode($doc->getElementsByTagName("title")->item(0)->textContent)); ?>
|
||||
<?php echo htmlentities($paste->title); ?>
|
||||
</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>
|
||||
<h2><?php echo $paste->get_subtitle(); ?></h2>
|
||||
<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>';
|
||||
}
|
||||
?>
|
||||
<a href="/?a=<?php echo $kout[1]; ?>">Répondre</a>
|
||||
<?php echo $paste->get_ref(); ?>
|
||||
</div>
|
||||
<?php
|
||||
echo $geshi->parse_code();
|
||||
@ -70,8 +46,17 @@ foreach ($_GET as $k => $t)
|
||||
$view = true;
|
||||
}
|
||||
}
|
||||
|
||||
//Don't show the creation part when we show paste
|
||||
if (!empty($view))
|
||||
exit;
|
||||
|
||||
//Load answer paste
|
||||
if (!empty($_GET["a"]) && preg_match("#^[a-zA-Z0-9]{".RGXP_NB."}$#", $_GET["a"])
|
||||
&& is_file(Paste::get_path($k = $_GET["a"])))
|
||||
$paste = new Paste($k);
|
||||
else
|
||||
$paste = new Paste();
|
||||
?>
|
||||
<header>
|
||||
<h1><span>Pommultimédia</span></h1>
|
||||
@ -81,21 +66,14 @@ if (!empty($view))
|
||||
<form method="post" action="save.php">
|
||||
<fieldset class="paste_form">
|
||||
<label for="title">Titre :</label>
|
||||
<input type="text" maxlength="200" size="50" id="title" name="title">
|
||||
<input type="text" size="50" id="title" name="title" value="<?php
|
||||
echo $paste->title;
|
||||
?>">
|
||||
<br><br>
|
||||
|
||||
<label for="content">Contenu :</label><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(utf8_decode($doc->getElementsByTagName("content")->item(0)->textContent));
|
||||
$language = strtolower($doc->getElementsByTagName("language")->item(0)->textContent);
|
||||
$ref = $k;
|
||||
}
|
||||
echo htmlentities(utf8_decode($paste->content));
|
||||
?></textarea><br><br>
|
||||
|
||||
<label for="author">Auteur :</label>
|
||||
@ -120,7 +98,7 @@ if (!empty($view))
|
||||
|
||||
foreach ($lg as $l)
|
||||
{
|
||||
if (isset($language) && $language == $l)
|
||||
if (!empty($paste->language) && $paste->language == $l)
|
||||
echo "<option selected=\"selected\"> ".ucfirst($l)."</option>\n";
|
||||
else
|
||||
echo "<option> ".ucfirst($l)."</option>\n";
|
||||
@ -130,8 +108,8 @@ if (!empty($view))
|
||||
?>
|
||||
</select>
|
||||
<?php
|
||||
if (!empty($ref))
|
||||
echo '<input type="hidden" name="ref" value="'.$ref.'">';
|
||||
if (!empty($paste->fileref))
|
||||
echo '<input type="hidden" name="ref" value="'.$paste->fileref.'">';
|
||||
?>
|
||||
<input type="submit" value="Envoyer">
|
||||
</fieldset>
|
||||
|
@ -3,48 +3,11 @@ require_once("../common.php");
|
||||
|
||||
if (!empty($_POST["content"]))
|
||||
{
|
||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||
$xml->formatOutput = true;
|
||||
$paste = new Paste();
|
||||
$paste->create($_POST);
|
||||
|
||||
$xml_paste = $xml->createElement("paste");
|
||||
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("title", $_POST["title"]));
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("author", $_POST["author"]));
|
||||
$xml_paste->appendChild(
|
||||
$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);
|
||||
|
||||
//Save the paste
|
||||
$filename = substr(
|
||||
str_replace("+", "",
|
||||
str_replace("/", "",
|
||||
$hash)), 0, NB_CHAR);
|
||||
|
||||
if ($xml->save(DESTINATION . "/" . $filename . ".xml"))
|
||||
{
|
||||
//Redirect the user to is paste
|
||||
header("Location: /?".$filename);
|
||||
exit;
|
||||
}
|
||||
else
|
||||
die ("Sorry, an error occured while saving the file. Please try again later.");
|
||||
header("Location: /?".$paste->save());
|
||||
exit;
|
||||
}
|
||||
|
||||
header("Location: /");
|
||||
|
178
paste.class.php
Normal file
178
paste.class.php
Normal file
@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
require_once("common.php");
|
||||
|
||||
class Paste
|
||||
{
|
||||
var $filename;
|
||||
var $fileref = NULL;
|
||||
var $title;
|
||||
var $language;
|
||||
var $author;
|
||||
var $date;
|
||||
var $content;
|
||||
var $ip;
|
||||
var $ref = NULL;
|
||||
var $hash;
|
||||
|
||||
/**
|
||||
* Create a new paste or open a existing file
|
||||
*/
|
||||
function __construct($filename=NULL)
|
||||
{
|
||||
if (!empty($filename))
|
||||
{
|
||||
$this->filename = Paste::get_path($filename);
|
||||
|
||||
if ($this->load())
|
||||
$this->fileref = $filename;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load or reload the file and set attributes
|
||||
*/
|
||||
function load()
|
||||
{
|
||||
if (is_file($this->filename))
|
||||
{
|
||||
$doc = new DOMDocument();
|
||||
$doc->load($this->filename);
|
||||
|
||||
$this->title = utf8_decode(
|
||||
$doc->getElementsByTagName("title")->item(0)->textContent);
|
||||
$this->language = strtolower(
|
||||
$doc->getElementsByTagName("language")->item(0)->textContent);
|
||||
$this->date = $doc->getElementsByTagName("date")->item(0)->textContent;
|
||||
$this->author = utf8_decode(
|
||||
$doc->getElementsByTagName("author")->item(0)->textContent);
|
||||
$this->content = $doc->getElementsByTagName("content")->item(0)->textContent;
|
||||
if ($doc->getElementsByTagName("ref")->length > 0)
|
||||
$this->ref = $doc->getElementsByTagName("ref")->item(0)->textContent;
|
||||
else
|
||||
$this->ref = NULL;
|
||||
$this->ip = $doc->getElementsByTagName("ip")->item(0)->textContent;
|
||||
$this->hash = $doc->getElementsByTagName("hash")->item(0)->textContent;
|
||||
|
||||
//Check the lang exists
|
||||
if (empty($this->language) || !is_file(GESHI_DIR.$this->language.".php"))
|
||||
$this->language = "text";
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static function get_path($filename)
|
||||
{
|
||||
return DESTINATION . "/" . $filename . ".xml";
|
||||
}
|
||||
|
||||
static function speed_cmp($filename, $hash)
|
||||
{
|
||||
if (is_file($filename))
|
||||
{
|
||||
$doc = new DOMDocument();
|
||||
$doc->load($filename);
|
||||
|
||||
return ($hash == $doc->getElementsByTagName("hash")->item(0)->textContent);
|
||||
}
|
||||
else
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the current paste
|
||||
*/
|
||||
function save($filename=NULL)
|
||||
{
|
||||
$this->date = time();
|
||||
$this->hash = base64_encode(sha1($this->content, true));
|
||||
$this->ip = $_SERVER["REMOTE_ADDR"];
|
||||
|
||||
if (empty($filename))
|
||||
{
|
||||
$i = 0;
|
||||
do
|
||||
{
|
||||
$filename = substr(
|
||||
str_replace("+", "",
|
||||
str_replace("/", "",
|
||||
$this->hash)), $i++, NB_CHAR);
|
||||
}
|
||||
//If the file already exists, find another name if the content is different
|
||||
while(is_file(Paste::get_path($filename))
|
||||
&& !Paste::speed_cmp(Paste::get_path($filename), $hash));
|
||||
}
|
||||
$this->filename = $filename;
|
||||
|
||||
$xml = new DOMDocument('1.0', 'UTF-8');
|
||||
$xml->formatOutput = true;
|
||||
|
||||
$xml_paste = $xml->createElement("paste");
|
||||
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("title", $this->title));
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("author", $this->author));
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("language", $this->language));
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("date", $this->date));
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("ip", $this->ip));
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("content", $this->content));
|
||||
|
||||
if (!empty($this->ref))
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("ref", $this->ref));
|
||||
|
||||
$xml_paste->appendChild(
|
||||
$xml->createElement("hash", $this->hash));
|
||||
|
||||
$xml->appendChild($xml_paste);
|
||||
|
||||
if ($xml->save(Paste::get_path($this->filename)))
|
||||
return $this->filename;
|
||||
else
|
||||
{
|
||||
die ("Sorry, an error occured while saving the file. Please try again later.");
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
function create($dict)
|
||||
{
|
||||
$this->title = $dict["title"];
|
||||
$this->author = $dict["author"];
|
||||
$this->language = $dict["lang"];
|
||||
if (isset($dict["ref"]))
|
||||
$this->ref = $dict["ref"];
|
||||
|
||||
//TODO: allow uploading file
|
||||
$this->content = $dict["content"];
|
||||
}
|
||||
|
||||
function get_subtitle()
|
||||
{
|
||||
if (empty($this->author))
|
||||
$author = "<em>un inconnu</em>";
|
||||
else
|
||||
$author = htmlentities($this->author);
|
||||
|
||||
return "Posté par ".$author.", le ".strftime("%A %e %B %G à %H:%M:%S",
|
||||
$this->date);
|
||||
}
|
||||
|
||||
function get_ref()
|
||||
{
|
||||
if (!empty($this->ref))
|
||||
return '<a href="/?'.$this->ref.'">Voir l\'original</a>';
|
||||
else
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user