Code:<?php // Licence : GPL
/**
* Guiki as PHP-Nuke module
* A simple php wiki in one-page and a crossplatform WYSIWYG editor
*
*/
if (!defined('MODULE_FILE')) {
die ("You can't access this file directly...");
}
require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
$index = 1;
//get_lang($module_name); // no languages yet
$pagetitle = "$module_name";
global $user, $prefix;
$prefix_guiki = $prefix."_guiki";
if(!is_array($user)) {
$user_get = base64_decode($user);
$user_get = explode(":", $user_get);
$user_name = "$user_get[1]";
} else {
$user_name = "$user[1]";
}
include("modules/$module_name/config.php"); // Get user set variables
$TPLT["EDIT"] = "$TEMPLATE/edit.html"; // Set edit template
$TPLT["SHOW"] = "$TEMPLATE/show.html"; // Set show template
$TPLT["SEARCH"] = "$TEMPLATE/show.html"; // Set search template
$TPLT["INDEX"] = "$TEMPLATE/show.html"; // Set index template
$TPLT["HISTORY"] = "$TEMPLATE/show.html";
$CONTENT = $HTTP_POST_VARS["CONTENT"]; // Get content from edit page
$SEARCH = $HTTP_POST_VARS["SEARCH"]; // Get search term
$MODE = $HTTP_GET_VARS["MODE"]; // Get mode
$DOCPOST = $HTTP_POST_VARS["docpost"]; // Testing out new editor delete me
$PAGE = $HTTP_GET_VARS["PAGE"]; // Get page name
$CONTENT = preg_replace("/<\?/","",$CONTENT); // No PHP uploads
$SEARCH = checkVars($SEARCH); // No special chars
$PAGE = checkVars($PAGE); // No special chars
$MODE = checkVars($MODE); // No special chars
if (! $MODE) {$MODE = "SHOW";} // Set default mode
if (! $PAGE) {$PAGE = $HOME;} // Set default page
# include("header.php");
function head() {
global $slogan, $sitename, $banners, $nukeurl, $Version_Num, $artpage, $topic, $hlpfile, $user, $hr, $theme, $cookie, $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $forumpage, $adminpage, $userpage, $pagetitle;
include("includes/ipban.php");
$ThemeSel = get_theme();
include_once("themes/$ThemeSel/theme.php");
echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
echo "<html>\n";
echo "<head>\n";
echo "<title>$sitename $pagetitle</title>\n";
include("includes/meta.php");
include("includes/javascript.php");
if (file_exists("themes/$ThemeSel/images/favicon.ico")) {
echo "<link REL=\"shortcut icon\" HREF=\"themes/$ThemeSel/images/favicon.ico\" TYPE=\"image/x-icon\">\n";
}
echo "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"RSS\" href=\"backend.php\">\n";
echo "<LINK REL=\"StyleSheet\" HREF=\"themes/$ThemeSel/style/style.css\" TYPE=\"text/css\">\n\n\n";
if (file_exists("includes/custom_files/custom_head.php")) {
include_once("includes/custom_files/custom_head.php");
}
echo "\n\n\n</head>\n\n";
if (file_exists("includes/custom_files/custom_header.php")) {
include_once("includes/custom_files/custom_header.php");
}
echo "\n\n\n</head>\n\n";
themeheader();
}
online();
head();
include("includes/counter.php");
global $user_name, $home;
if ($home == 1) {
message_box();
blocks(Center);
}
title("$sitename: $pagetitle - $MODE Page $PAGE");
if( $CONTENT )
if (validate($user_name))
savePage($PAGE,$CONTENT);
if( $DOCPOST )
if (validate($user_name))
savePage($PAGE,$DOCPOST);
if (! page_exists($PAGE))
if ($MODE != "INDEX"&&$MODE != "SEARCH" && $MODE != "HISTORY")
$MODE = "EDIT"; // Go into edit mode
if ($MODE == "EDIT"||$MODE == "DELETE") // If $MODE is edit or delete
if (! validate($user_name)) // Challenge for user and password
$MODE = "SHOW"; // Change to show if they dont know login
if ($MODE == "DELETE"){ // If mode is delete (AUTH done above)
deletepage($PAGE); // Delete the page
$PAGE = "$HOME"; // Set $PAGE to default
$MODE = "SHOW"; // Set $MODE to show
}
if ($MODE == "REVERT") {
revertpage($PAGE, $DATA);
$MODE = "HISTORY";
$DATA = "";
}
OpenTable();
template($MODE,$PAGE,$DATA,$TPLT[$MODE],$SEARCH); // Make replacements
CloseTable();
include("footer.php");
//die();
/*
** FUNCTIONS
*/
function template($MODE,$PAGE,$DATA,$TEMPLATE,$SEARCH){
$OUTPUT = implode( "", file($TEMPLATE) );
if (page_exists($PAGE)){
$CONTENT = stripslashes(getpage($PAGE));
$MODIFIED = "Last Modified at ".date("H:i:s F d Y",pagemtime($PAGE))." by ".pagemuser($PAGE);
}
if ($MODE == "SEARCH")
$CONTENT = findPage($SEARCH);
if ($MODE == "INDEX")
$CONTENT = indexPage();
if ($MODE == "HISTORY")
$CONTENT = historypage($PAGE,$DATA);
$OUTPUT = str_replace("<!--MODIFIED-->" ,$MODIFIED ,$OUTPUT);
$OUTPUT = str_replace("<!--REVERT-->" ,showRevertLink() ,$OUTPUT);
$OUTPUT = str_replace("<!--CANCEL-->" ,showCancelLink() ,$OUTPUT);
$OUTPUT = str_replace("<!--EDIT-->" ,showEditLink($MODE, $PAGE) ,$OUTPUT);
$OUTPUT = str_replace("<!--HISTORY-->" ,showHistoryLink($PAGE) ,$OUTPUT);
$OUTPUT = str_replace("<!--INDEX-->" ,showIndexLink($PAGE) ,$OUTPUT);
$OUTPUT = str_replace("<!--DELETE-->" ,showDeleteLink() ,$OUTPUT);
$OUTPUT = str_replace("<!--PAGE-->" ,$PAGE ,$OUTPUT);
$OUTPUT = str_replace("<!--CONTENT-->" ,$CONTENT ,$OUTPUT);
$OUTPUT = str_replace("<!--TEMPLATE-->" ,dirname($TEMPLATE) ,$OUTPUT);
if ($MODE == "SHOW")
//$OUTPUT = preg_replace("/a href=/i","a target='_blank' href="),$OUTPUT); // now working popup all external links
$OUTPUT = preg_replace("/\[\[(.*?)\]\]/",writeLink("\\1"),$OUTPUT);
writeHeaders();
echo $OUTPUT;
}
function checkVars($DATA){
return preg_replace("/[\`|\.|\\|\/|<\?]/","",$DATA);
}
function findPage($SEARCH) {
$output = "<ul>\n";
foreach (getDataFiles() as $page) {
if (!preg_match("/.gif|.jpg|.png/i",$page)){
$current = getpage($page);
if (preg_match("/$SEARCH/i",$current) )
$output .= "<li>".writeLink($page)."</li>\n";
}
}
$output .= "</ul>\n";
return $output;
}
function validate($user){
global $db, $prefix, $MAX_KARMA, $admin;
if(is_admin($admin)) return true;
$result = $db->sql_fetchrow($db->sql_query("SELECT karma FROM ".$prefix."_users WHERE username='$user'"));
$karma = $result['karma'];
if ($user != "" && $karma <= $MAX_KARMA) {
return true;
} else {
return false;
}
}
function showCancelLink(){
global $module_name;
return "<a href=\"modules.php?name=$module_name&MODE=SHOW&PAGE=<!--PAGE-->\">Cancel</a>";
}
function showRevertLink(){
global $module_name;
return "<a href=\"modules.php?name=$module_name&MODE=EDIT&PAGE=<!--PAGE-->\">Revert</font>";
}
function showDeleteLink(){
global $module_name;
return "<a href=\"modules.php?name=$module_name&MODE=DELETE&PAGE=<!--PAGE-->\"><font color='red'>Delete</font></a>";
}
function showEditLink($MODE, $PAGE){
global $module_name, $user_name;
if ((validate($user_name)) && ( $MODE != "HISTORY" ) && ( $PAGE != "Index" )) {
return "<a href=\"modules.php?name=$module_name&MODE=EDIT&PAGE=<!--PAGE-->\">Edit</a>";
}
}
function showIndexLink($PAGE){
global $module_name;
if ( $PAGE != "Index" )
return "<a href=\"modules.php?name=$module_name&MODE=INDEX&PAGE=Index\">Index</a>";
}
function showHistoryLink($PAGE){
global $module_name;
if ( $PAGE != "Index" )
return "<a href=\"modules.php?name=$module_name&MODE=HISTORY&PAGE=<!--PAGE-->\">History</a>";
else
return "<a href=\"modules.php?name=$module_name&MODE=HISTORY&PAGE=<!--PAGE-->\">Recent Changes</a>";
}
function savePage($PAGE,$CONTENT) {
global $db, $prefix_guiki, $user_name;
if (validate($user_name)) {
$PAGE = addslashes($PAGE);
$CONTENT = addslashes($CONTENT);
if(page_exists($PAGE)){
// Make a copy
$query = "INSERT INTO ".$prefix_guiki."_history SELECT * FROM $prefix_guiki WHERE page = '$PAGE'";
$db->sql_query($query);
//echo $query;
//$error = $db->sql_error();
//echo $error["message"];
$db->sql_query("UPDATE $prefix_guiki SET content = '$CONTENT' WHERE page = '$PAGE'");
}else{
$db->sql_query("INSERT INTO $prefix_guiki VALUES('$PAGE','$CONTENT',0,'')");
//echo $query;
//$error = $db->sql_error();
//echo $error["message"];
}
//Update time
$time=time();
if($user_name=="")
{
$guser_name="Administrator";
}else
{
$guser_name=$user_name;
}
$query = "UPDATE $prefix_guiki SET modtime = $time, editedby = '$guser_name' WHERE page = '$PAGE'";
//echo $query;
//$error = $db->sql_error();
//echo $error["message"];
$db->sql_query($query);
} else {
echo "Illegal attempt to change wiki. Admin notified";
}
}
function revertpage($PAGE, $DATA) {
global $db, $prefix_guiki, $user_name;
if (validate($user_name)) {
$PAGE = addslashes($PAGE);
// Make a copy for history
$query = "INSERT INTO ".$prefix_guiki."_history SELECT * FROM $prefix_guiki WHERE page = '$PAGE'";
$db->sql_query($query);
// Delete current page
$query = "DELETE FROM $prefix_guiki WHERE page = '$PAGE'";
$db->sql_query($query);
// Copy page from history
$query = "INSERT INTO ".$prefix_guiki." SELECT * FROM ".$prefix_guiki."_history WHERE page = '$PAGE' and modtime = '$DATA'";
$db->sql_query($query);
// Update edit time and user
$time=time();
$query = "UPDATE $prefix_guiki SET modtime = $time, editedby = '$user_name' WHERE page = '$PAGE'";
$db->sql_query($query);
} else {
echo "Illegal attempt to change wiki. Admin notified";
}
}
function writeHeaders() {
header ("Expires: Mon, 26 Jul 1990 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
}
function writeLink($PAGE) {
global $module_name;
if ( preg_match("/.gif|.jpg|.png/i",$PAGE)){ //not working mat
return "<img src=\"modules/$module_name/data/$PAGE\">"; //data is hardcoded this is BAD
} else {
return "<a href=\"modules.php?name=$module_name&MODE=SHOW&PAGE=$PAGE\">$PAGE</a>";
}
}
function getDataFiles() {
global $db, $prefix_guiki;
$pagelist = $db->sql_query("SELECT page FROM $prefix_guiki ORDER BY page");
//Convert to array
while( $pagename = $db->sql_fetchrow($pagelist)){
$allpages[] = $pagename['page'];
}
return $allpages;
}
function getpage($PAGE){
global $db, $prefix_guiki;
$PAGE = addslashes($PAGE);
$sqlrow = $db->sql_fetchrow($db->sql_query("SELECT content from $prefix_guiki WHERE page = '$PAGE'"));
return $sqlrow['content'];
}
function page_exists($PAGE){
global $db, $prefix_guiki;
$PAGE = addslashes($PAGE);
return $db->sql_numrows($db->sql_query("SELECT page from $prefix_guiki WHERE page = '$PAGE'"));
}
function deletepage($PAGE){
global $db, $prefix_guiki, $user_name;
if (validate($user_name)) {
$PAGE = addslashes($PAGE);
$db->sql_query("DELETE FROM ".$prefix_guiki."_history WHERE page = '$PAGE'");
return $db->sql_query("DELETE FROM $prefix_guiki WHERE page = '$PAGE'");
} else {
echo "Illegal attempt to delete page. Admins notified";
}
}
function pagemtime($PAGE){
global $db, $prefix_guiki;
$PAGE = addslashes($PAGE);
$sqlrow = $db->sql_fetchrow($db->sql_query("SELECT modtime from $prefix_guiki WHERE page = '$PAGE'"));
return $sqlrow['modtime'];
}
function pagemuser($PAGE){
global $db, $prefix_guiki;
$PAGE = addslashes($PAGE);
$sqlrow = $db->sql_fetchrow($db->sql_query("SELECT editedby from $prefix_guiki WHERE page = '$PAGE'"));
return $sqlrow['editedby'];
}
function indexpage(){
global $db, $prefix_guiki;
$output = "<ul>\n";
foreach(getDataFiles() as $page){
$output .= "<li>".writeLink($page)."</li>\n";
}
$output .= "</ul>\n";
return $output;
}
function historypage($PAGE,$DATA) {
global $db, $prefix_guiki, $module_name;
if ($PAGE == "Index"){
$query = "SELECT page, modtime, editedby FROM ".$prefix_guiki."_history ORDER BY modtime ASC LIMIT 0,10";
$result = $db->sql_query($query);
$output = "<table width=75% border=1>";
$output .= "<tr><th>Date</th><th>Author</th><th>Page</th></tr>";
while ($row = $db->sql_fetchrow($result)) {
$output .= "<tr><td>".date("r", $row['modtime'])."</td><td>".$row['editedby']."</td><td>".$row["page"]."</td></tr>";
}
$output .= "</table>";
return $output;
} else {
$PAGE = addslashes($PAGE);
$output = "<br>";
if ( $DATA != "" ) {
$output .= "<font color=red>History for $PAGE: ".date("r", $DATA)."</font><br>";
$query = "SELECT content from ".$prefix_guiki."_history WHERE page = '$PAGE' AND modtime =' $DATA'";
$sqlrow = $db->sql_fetchrow($db->sql_query($query));
$output .= stripslashes($sqlrow['content']);
$output = preg_replace("/\[\[(.*?)\]\]/",writeLink("\\1"),$output);
$output .= "<br><hr><br>";
}
$output .= "<table width=75% border=1>";
$output .= "<tr><th>Date</th><th>Author</th><th>Action</th></tr>";
$query = "SELECT * FROM ".$prefix_guiki."_history WHERE page = '$PAGE' ORDER BY modtime DESC";
$result = $db->sql_query($query);
while ($row = $db->sql_fetchrow($result)) {
$output .= "<tr><td>".date("r", $row['modtime'])."</td><td>".$row['editedby']."</td><td><a href=\"modules.php?name=$module_name&MODE=HISTORY&PAGE=$PAGE&DATA=".$row['modtime']."\">view</a> <a href=\"modules.php?name=$module_name&MODE=REVERT&PAGE=$PAGE&DATA=".$row['modtime']."\">revert</a> </td></tr>";
}
$output .= "</table>";
return $output;
}
}
?>
|