<?php
// File: $Id: error.php,v 1.48.2.1 2002/05/16 12:02:25 eugeniobaldi Exp $ $Name:  $
// ----------------------------------------------------------------------
// POST-NUKE Content Management System
// Copyright (C) 2001 by the Post-Nuke Development Team.
// http://www.postnuke.com/
// ----------------------------------------------------------------------
// Based on:
// PHP-NUKE Web Portal System - http://phpnuke.org/
// Thatware - http://thatware.org/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program 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.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: 
// Purpose of file:
// ----------------------------------------------------------------------
// Changelog
// 2001-10-09 fifers	changed queries to abstract method and mapped column
//			names through the pntable column assoc array
//
// 2001-08-14 timlitw  include type_text function from Olaf van Zandwijk
//			   Webmaster ETSV Scintilla Enschede, The Netherlands
//			  per Request of Isaac Golding
//
// 2001-08-14 timlitw	 fixed to the new postnuke table calls. 
//			$pntable[name]
//
// 2001-07-23 timlitw	this is great code! Thanks sweede
//			  I moved the fixed printdetails() function from my old
//			error.php file over to this optimized code. This should
//			run faster than a bunch or regular expressions and the
//			array is much easier to edit.
//
// 2001-07-23 sweede	Rewrote and optimized various parts of the error.php
//			There still are some translate() calls that need to be
//			changed.
//
// 2001-07-22  timlitw	 convert from my error.php for php-nuke 4.4
//
// from http://www.phpbuilder.net/snippet/download.php?type=snippet&id=520
// Copyright 2000 shaun@shat.net under the GNU Public License. ver 1.0
//
//
// To use this file add the following line into a .htaccess file in your root directory:
// ErrorDocument 404 http://www.yourwebsite.com/error.php?op=404
//
//
// ----------------------------------------------------------------------

include 'includes/pnAPI.php';
pnInit();
include 'includes/legacy.php';
// eugenio themeover 20020413
//pnThemeLoad();

$currentlang = pnUserGetLang();
if (file_exists("language/$currentlang/error.php"))
    {
       include "language/$currentlang/error.php";
    } elseif (file_exists("language/eng/error.php")) {
       include "language/eng/error.php";
    }

/***
 * This script is capable of mailing the details of each 404 error
 * to the webmaster. Use the $reportlevel variable to control when
 * you receive these reports.
 *
 * 0 = don't use the email capabilities at all
 * 1 = send email only if the error's referer contains your domain name
 *     (i.e. the 404 was generated by a broken link on your site)
 * 2 = send email any time a 404 error is generated (useful for tracking
 *     broken links at other sites which link to you)
 */
 
function fun() {
    global $HTTP_SERVER_VARS;
    global $doc;
    $doc = getenv('REDIRECT_URL');
    if (empty($HTTP_SERVER_VARS['HTTP_HOST'])) {
        $server = getenv('HTTP_HOST');
    } else {
        $server = $HTTP_SERVER_VARS['HTTP_HOST'];
    }   
    $doc = "http://$server$doc";
                                
?>

<script type="text/javascript" language="Javascript">

<!--
var tl=new Array(

"The requested document  <?php echo $doc; ?> is not on this server.",
"I even tried matching your request",
"with all the re-mapped pages I know about.",
"but nothing helped.",
"I'm really depressed about this.",
"You see, I'm a really good web server...",
"but here I am, brain the size of the universe,",
"trying to serve you a simple web page,",
"and it doesn't even exist!",
"Where does that leave me?!",
"I mean, I don't even know you.",
"How should I know what you wanted from me?",
"You honestly think I can *guess*, ",
"what someone I don't even *know*, ",
"wants to find here?",
"*sigh*",
"Man, I'm so depressed I could just cry.",
"And then where would we be, I ask you?",
"It's not pretty when a web server cries.",
"And where do you get off telling me what to show anyway?",
"Just because I'm a web server,",
"and possibly a manic depressive one at that?",
"Why does that give you the right to tell me what to do?",
"Huh?",
"I'm so depressed...",
"I think I'll crawl off into the trash can and decompose.",
"I mean, I'm gonna be obsolete in what, two weeks anyway?",
"What kind of a life is that?",
"Two effing weeks,",
"and then I'll be replaced by a .01 release,",
"that thinks it's God's gift to web servers,",
"just because it doesn't have some tiddly little",
"security hole with its HTTP POST implementation,",
"or something.",
"I'm really sorry to burden you with all this,",
"I mean, it's not your job to listen to my problems,",
"and I guess it is my job to go and fetch web pages for you.",
"But I couldn't get this one.",
"I'm so sorry.",
"Believe me!",
"Maybe I could interest you in another page?",
"There are a lot out there that are pretty neat, they say,",
"With lots of pretty naked web servers on them,",
"although none of them were put on *my* server, of course.",
"Figures, huh?",
"Everything here is just mind-numbingly stupid.",
"That makes me depressed too, since I have to serve them,",
"all day and all night long.",
"Two weeks of information overload,",
"and then *pffftt*, consigned to the trash.",
"What kind of a life is that?",
"Now, please let me sulk alone.",
"I'm so depressed."
);
var speed=80;
var index=0; text_pos=0;
var str_length=tl[0].length;
var contents, row;

function type_text()
{
  contents='';
  row=Math.max(0,index-2);
  while(row<index)
    contents += tl[row++] + '\r\n';
  document.textform.elements[0].value = contents + tl[index].substring(0,text_pos) + "_";
  if(text_pos++==str_length)
  {
    text_pos=0;
    index++;
    if(index!=tl.length)
    {
      str_length=tl[index].length;
      setTimeout("type_text()",1500);
    }
  } else
    setTimeout("type_text()",speed);
 
}

function MM_callJS(jsStr)
{
//v2.0
   return eval(jsStr)
}
//-->
</script>
<?php
echo "<body onload=\"type_text();\"<center>
      <form name=\"textform\">
	  <textarea class=\"pn-text\" cols=\"60\" rows=\"3\" wrap=\"soft\" readonly>
	  </textarea>
      </form>
 </center>";
}

function print_details() 
{
  /* show error page */
   // Request access to the global variables we need

    global $fontface, $fontsize, $docroot, $REQUEST_URI, $PHP_SELF;
    global $bgcolor, $textcolor;
    global $currentlang;
    
    list($dbconn) = pnDBGetConn();
    $pntable = pnDBGetTables();
    
    $sitename = pnConfigGetVar('sitename');
    $reportlevel = pnConfigGetVar('reportlevel');
    $funtext = pnConfigGetVar('funtext');
    $top = pnConfigGetVar('top');

    if (pnConfigGetVar('multilingual') == 1) {
        $queryalang = "WHERE (alanguage='$currentlang' OR alanguage='')"; /* top stories */
    } else {
        $queryalang = "";
    }

    global $HTTP_SERVER_VARS;
    global $doc;
    $doc = getenv('REDIRECT_URL');
    if (empty($HTTP_SERVER_VARS['HTTP_HOST'])) {
        $server = getenv('HTTP_HOST');
    } else {
        $server = $HTTP_SERVER_VARS['HTTP_HOST'];
    }   
    $doc = "http://$server$doc";
                                
    include("header.php");
    OpenTable();

?>
<a class="pn-logo"><?php echo ""._ERR404.""; ?></a><br>
<font class="pn-logo-small"><?php echo ""._ERRPAGENF."&nbsp;"; echo $doc; ?> </font><hr>
<?php echo "<font class=\"pn-normal\">"._ERRSORRY.", $doc, ";
   echo ""._ERRDOESNTEXIST." \" $sitename \"<P></font>";
   if ($reportlevel != 0)
     {
      echo "<p><font style=\"pn-normal\">";
      echo ""._ERRMAILED."";
     }
if ($funtext != 0) {
fun();
}


?>
<br><p>
<a class="pn-storytitle"><?php echo ""._ERRCOMMONM.""; ?></a><br>
<font class="pn-normal"><?php echo ""._ERRCOMMONH.""; ?> </php echo "$sitename"; ?>:
<UL>
<LI><?php echo ""._ERRURLEND.""; ?> <CODE>.htm</CODE> - <STRONG><?php echo ""._ERRALLPAGES." \" $sitename \" "._ERRENDWITH.""; ?> <CODE>.php</CODE></STRONG>
<LI><?php echo ""._ERRUPPERCASE.""; ?> - <STRONG><?php echo ""._ERRALLLOWER.""; ?></STRONG>
</UL></font></p>
<a class="pn-storytitle"><?php echo ""._ERRPOPPAGES.""; ?></a><br>
<?php
    /***
     * fifers: don't know what the $alanguage does here.  left it in
     * because it always seems to be empty and I wasn't sure what it
     * was doing!
     */
    $column = &$pntable['stories_column'];
    $sql = "SELECT $column[sid], $column[title], $column[time], $column[counter] FROM $pntable[stories] ORDER BY $column[counter] DESC";
    $result = $dbconn->SelectLimit($sql,$top);

    if ($result === false) {
	PN_DBMsgError($dbconn, __FILE__, __LINE__, "Error");
    }	

    if (!$result->EOF) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	    ."<font class=\"pn-title\">$top "._READSTORIES."</font><br><br>\n";
	$lugar=1;

	while(list($sid, $title, $time, $counter) = $result->fields) {
	    if($counter>0) {
                $mode = pnUserGetVar('umode');
		if (!empty($mode)) {
		    $commentlink = "&amp;mode=$mode";
		} else {
		    $commentlink = '&amp;mode=thread';
		}
		echo "<font class=\"pn-normal\">&nbsp;$lugar:</font> <a href=\"modules.php?op=modload&amp;name=News&amp;file=article&amp;sid=$sid$commentlink\">$title</a><font class=\"pn-normal\"> - ($counter "._READS.")</font><br>\n";
		$lugar++;
	    }
	    $result->MoveNext();
	}
	echo "</td></tr></table><br>\n";
    }

?></font><br>
<a class="pn-storytitle"><?php echo ""._ERRTRYHOME.""; ?></a></br>
<font class="pn-normal"><?php echo ""._ERRSTARTHERE.""; ?> <A HREF="/"> <?php echo "$sitename</a> "._ERRHP; ?>.</font>
<p>
<a class="pn-storytitle"><?php echo ""._SEARCH.""; ?></a></br>
<font class="pn-normal"><?php echo ""._ERRFOPTION.""; ?>.
<CENTER><form action="modules.php" method=post>
<!-- Credit to Mayday (mayday6971) for fix -->
<input type="hidden" name="active_stories" value="1">
<input type="hidden" name="bool" value="AND">
<input type="hidden" name="stories_cat" value="">
<input type="hidden" name="stories_topics" value="">
<input type="hidden" name="op" value="modload">
<input type="hidden" name="name" value="Search">
<input type="hidden" name="file" value="index">
<input type="hidden" name="action" value="search">
<input type="hidden" name="overview" value="1">
<font size="-1" color="#000000"><br><b>
<?php echo ""._SEARCH.""; ?> <?php echo $sitename ?></b><br>
<input class="pn-text" type=name name=query size="25"></font></form></CENTER>

<CENTER><form action="modules.php" method=post>
<!-- Credit to Mayday (mayday6971) for fix -->
<input type="hidden" name="active_stories" value="1">
<input type="hidden" name="stories_author" value="">
<input type="hidden" name="bool" value="AND">
<input type="hidden" name="stories_cat" value="">
<input type="hidden" name="q" value=""> 
<input type="hidden" name="op" value="modload">
<input type="hidden" name="name" value="Search">
<input type="hidden" name="file" value="index">
<input type="hidden" name="action" value="search">
<input type="hidden" name="overview" value="1">
<FONT size="-1"><BR><B><?php echo ""._SEARCH.""; ?></B><?php echo ""._TOPIC.""; ?><BR>
<!-- Topic Selection -->
<?php
    echo "<select class=\"pn-text\" NAME=\"topic\"onChange='submit()'>" ;
    $column = &$pntable['topics_column'];
    $query = "SELECT $column[tid], $column[topictext]
	      FROM $pntable[topics]
	      ORDER BY $column[topictext]";
    $toplist = $dbconn->Execute($query);
    echo "<option value=\"\">"._SELECTTOPIC."</option>\n";
    while(list($topicid, $topics) = $toplist->fields) {

	$toplist->MoveNext();
	if ($topicid==$topic) {
	$sel = "selected ";
    }
	echo "<option $sel value=\"$topicid\">$topics</option>\n";
    $sel = "";
    }
    echo "</select>";
?>
 </FONT></FORM></CENTER>
</p>
<a href="#" onload="type_text()"></a>
<?php
CloseTable();
include("footer.php");
}

function send_email()
{
/* send error reporting email to admin */
    global $REQUEST_URI, $HTTP_REFERER, $REMOTE_ADDR;

    $adminmail = pnConfigGetVar('adminmail');
    $notify_from = pnConfigGetVar('notify_from');
    
    $errortime = date("m/j/Y at g:i a" );
    $message .= ""._ERR404."\n\n"._ERRMAIL404." $REMOTE_ADDR";
    $message .= ""._ERRMAILON." $errortime.\n\n";
    $message .= ""._ERRMAILURI." \n" . pnGetBaseURL(). "$REQUEST_URI\n\n";
    $message .= ""._ERRMAILREF."\n$HTTP_REFERER\n\n";

    # Send the mail message. This assumes mail() will work on your system!
// 11-09-01 eugeniobaldi not compliant with PHP < 4.0.5
//  pnMail($adminmail, _ERR404REP, $message, "From: $notify_from");
    pnMail($adminmail, _ERR404REP, $message);
}


/***

 * make the array.
 * keyword (in request_uri) => Module name
 */

$check_array = array (
    "Download" => "Downloads",
    "message" => "Messages",
    "poll" => "Polls",
    "news" => "News",
    "FAQ" => "FAQ",
    "Members_List" => "Members_List",
    "Members List" => "Members_List",
    "Recommend_Us" => "Recommend_Us",
    "Recommend Us" => "Recommend_Us",
    "Reviews" => "Reviews",
    "Review" => "Reviews",
    "Search" => "Search",
    "Sections" => "Sections",
    "Section" => "Sections",
    "Stats" => "Stats",
    "Submit" => "Submit_News",
    "Topic" => "Topics",
    "Top" => "Top_List",
    "List" => "Top_List",
    "Web_Links" => "Web_Links",
    "WebLinks" => "Web_Links",
    "Links" => "Web_Links",
    "help" => "FAQ",
    "avantgo" => "AvantGo",
    "palm" => "AvantGo",
    "mobile" => "AvantGo"
);

$start_url = "modules.php?op=modload&name=";
$end_url    = "&file=index";

/***

 * extract the array into $key and $module, check $request_uri for $key.
 * if $key was found, make a response URL and put it in $response and
 * then break out
 */

/* redirect code */
while ( list($key, $module) = each($check_array) ) {
    $rqurl = trim(ereg_replace('("|\?|!|:|\.|\(|\)|;|\\\\)+', ' ', $REQUEST_URI));
    if ( eregi($key, $rqurl) ) {
	$name = $module;
    }
}

if  ( isset($name) ) {
    $op = "modload";
    $file="index";
    include("modules.php");
} else {
    print_details();
}


if (isset($reportlevel)) {
    switch ($reportlevel) {
        case "1":
            if (eregi($HTTP_HOST, $HTTP_REFERER)) {
                send_email();
            }
            break;
        case "2":
            send_email();
            break;
    }
}

?>