<?
/*
This file is part of Yet Another Nopaste.
Foobar is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
function initCookies() {
$expire = time()+60*60*24*30;
if(isset($_POST["language"])) {
setCookie("YANOPASTE_language", $_POST["language"], $expire);
}
if(isset($_POST["name"])) {
setCookie("YANOPASTE_name", $_POST["name"], $expire);
}
if(isset($_POST["setpwd"])) {
setCookie("YANOPASTE_setpwd", $_POST["setpwd"], $expire);
}
}
function saveXML() {
global $_YANOPASTE;
// Creating result file
$xmlFind = array("&" ,"<" , ">" , "\"" , "'");
$xmlReplace = array("&", "<", ">", """, "'");
// Creating filename
$now = getdate();
$id = generateId();
$filename = generateFileName($id) . ".xml";
$out = fopen($filename, "w");
fwrite($out, "<?xml version=\"1.0\"?>\n");
fwrite($out, "<yanopaste id=\"" . $id . "\"\n");
fwrite($out, " name=\"".$_POST["name"]."\"\n");
fwrite($out, " language=\"".$_POST["language"]."\"\n");
fwrite($out, " setpwd=\"".(isset($_POST["setpwd"]) ? "true" : "false")."\"\n");
fwrite($out, " pwd=\"".$_POST["pwd"]."\"\n");
fwrite($out, " ga-enabled=\"".$_YANOPASTE["ga_enabled"]."\"\n");
fwrite($out, " ga-id=\"".$_YANOPASTE["ga_id"]."\"\n");
fwrite($out, " base-url=\"" . generateFileURL("") . "\">\n");
fwrite($out, " <paste>\n");
fwrite($out, " <original>" . str_replace($xmlFind, $xmlReplace, stripslashes($_POST["content"])) . "</original>\n");
fwrite($out, " <transform engine=\"geshi\">" . str_replace($xmlFind, $xmlReplace, parseGeshi()) . "</transform>\n");
fwrite($out, " </paste>\n");
fwrite($out, "</yanopaste>");
fclose($out);
return generateFileURLFromId($id);
}
function parseGeshi() {
// Creating geshi object
$geshi = new GeSHi(stripslashes($_POST["content"]), $_POST["language"]);
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
//$geshi->enable_classes();
$geshi->enable_strict_mode(true);
return $geshi->parse_code();
}
function generateFileName($id) {
global $_YANOPASTE;
// ensuring the directory exist, otherwise trying to create it
if (!file_exists($_YANOPASTE["repository"])) {
mkdir($_YANOPASTE["repository"], 0755, true);
}
return $_YANOPASTE["repository"] . "/paste" . $id;
}
function generateId() {
$now = getdate();
return $now["year"]
. str_pad($now["mon"], 2, "0", "STR_PAD_LEFT")
. str_pad($now["mday"], 2, "0", "STR_PAD_LEFT")
. str_pad($now["hours"], 2, "0", "STR_PAD_LEFT")
. str_pad($now["minutes"], 2, "0", "STR_PAD_LEFT")
. str_pad($now["seconds"], 2, "0", "STR_PAD_LEFT")
. "_" . rand(10000,99999);
}
function generateFileURL($path) {
return "http://" . $_SERVER["SERVER_NAME"] . substr($_SERVER["PHP_SELF"], 0, strrpos($_SERVER["PHP_SELF"], "/")) . "/" . $path;
}
function generateFileURLFromId($id) {
return "http://" . $_SERVER["SERVER_NAME"] . substr($_SERVER["PHP_SELF"], 0, strrpos($_SERVER["PHP_SELF"], "/")) . "/index.php?id=" . $id;
}
function retentionTimeAsString() {
global $_YANOPASTE;
$hours = floor($_YANOPASTE["lifetime"] / 3600);
$days = floor($hours / 24);
$hours = $hours % 24;
return $days . " day" . ($days > 1 ? "s " : " ") . $hours . " hour" . ($hours > 1 ? "s " : "");
}
function getHTMLHeader($extTitle, $rootPath) {
global $_YANOPASTE;
if ($_YANOPASTE["ga_enabled"] == "true") {
$ga_include = "<script src=\"http://www.google-analytics.com/ga.js\" type=\"text/javascript\"></script>";
} else {
$ga_include = "";
}
return "<html>" .
" <head>" .
" <title>Yet Another Nopaste " . $extTitle . "</title>" .
" <link rel=\"stylesheet\" type=\"text/css\" href=\"" . $rootPath . "screen.css\" media=\"screen\"/>" .
" <script type=\"text/javascript\" src=\"" . $rootPath . "common.js\"></script>" .
" " . $ga_include .
" </head>" .
" <body onload=\"initPaste()\">" .
" <input type=\"hidden\" id=\"ga_enabled\" value=\"" . $_YANOPASTE["ga_enabled"] . "\"/>" .
" <input type=\"hidden\" id=\"ga_id\" value=\"" . $_YANOPASTE["ga_id"] . "\"/>" .
" <div id=\"header\" onclick=\"window.location.href='" . generateFileURL("") . "'\"></div>" .
" <div id=\"body\">";
}
function getHTMLFooter() {
return " </div>" .
" <div id=\"footer\">" .
" Yet Another Nopaste - version 1.2.0- GPL software - Sources available at " .
" <a href=\"http://sourceforge.net/projects/yanopaste\"><img id=\"sourceforge_logo\" src=\"http://sflogo.sourceforge.net/sflogo.php?group_id=237687&type=1\" alt=\"SourceForge.net Logo\" /></a>" .
" - <a href=\"http://yanopaste.sourceforge.net\">Demo</a>" .
" <br/> Copyright (C) 2008 <a href=\"http://www.jujunie.com\">Julien BETI</a>" .
" </div>" .
" </body>" .
"</html>";
}
function cleanRepository() {
$now = time();
$path = "";
global $_YANOPASTE;
// Iterating over all files in the repository
foreach (scandir($_YANOPASTE["repository"]) as $f) {
$path = $_YANOPASTE["repository"] . "/" . $f;
if (is_file($path)) {
if ($now - filemtime($path) > $_YANOPASTE["lifetime"]) {
unlink($path);
} else {
// File must be kept
}
} else {
//nothing to do
}
}
}
function getDocument($id) {
// Loading XML
$xml = new DOMDocument;
$xml->load(generateFileName($id) . ".xml");
return $xml;
}
function checkAccess($xml) {
$nodes=$xml->getElementsByTagName('yanopaste');
$root = $nodes->item(0);
// If not protected, access granted :)
if($root->getAttribute("setpwd") == "false") {
return true;
} else {
return $_POST["pwd"] == $root->getAttribute("pwd");
}
}
function xslTransform($xml) {
$xsl = new DOMDocument;
$xsl->load('paste.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($xml);
}
?>