<?php
include_once("page.class.php");
class Exception_catch
{
    var $Exception        = False;
    var $ExceptionMessage = "";
    var $ExceptionReport  = 1;
    var $ExceptionDie     = 1;    function Exception_catch($ExceptionMessage = "", $ExceptionDie = 1, $ExceptionReport  = 1)
    {
        $this->Exception = True;
        $this->ExceptionMessage = $ExceptionMessage;
        $this->ExceptionReport  = $ExceptionReport;
        $this->ExceptionDie     = $ExceptionDie;
    }
    function EMessage()
    {   global $adminmail;
        $message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
                    <html>
                    <head>
                    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">
                    <title>异常捕获</title>
                    <style type=\"text/css\">
                    <!--
                        body {
                            font-family: \"宋体\", \"Arial\";
                            font-size: 12px;
                        }
                        table {
                            font-family: \"宋体\", \"Arial\";
                            font-size: 12px;
                        }
                        a:link {
                            font-family: \"宋体\", \"Arial\";
                            font-size: 12px;
                            color: #006699;
                            text-decoration: none;
                        }
                        a:visited {
                            font-family: \"宋体\", \"Arial\";
                            font-size: 12px;
                            color: #003366;
                            text-decoration: none;
                        }
                        a:hover {
                            font-family: \"宋体\", \"Arial\";
                            font-size: 12px;
                            color: #003366;
                            text-decoration: none;
                        }
                        a:active {
                            font-family: \"宋体\", \"Arial\";
                            font-size: 12px;
                            color: #003366;
                            text-decoration: none;
                        }
                    -->
                   </style>
                   </head><body>
<table width=\"40%\" height=\"118\" border=\"1\" cellpadding=\"00\" cellspacing=\"0\" bordercolordark=\"#FFFFFF\" bordercolorlight=\"#CCCCCC\">
  <tr>
    <td height=\"23\" bgcolor=\"#006699\">
<div align=\"center\"><font color=\"#FFFFFF\"><strong>错误捕获</strong></font></div></td>
  </tr>
  <tr>
    <td valign=\"top\"><div align=\"center\">程序出现异常,请联系系统<a href=\"mailto:".$adminmail."\">管理员</a>解决此问题,以下是出错信息 <br>
        <br>
        <textarea name=\"textarea\" cols=\"50\" rows=\"10\" wrap=\"VIRTUAL\" style=\"border : 1 solid #CCCCCC\">".$this->ExceptionMessage."</textarea>
        <br>
        <br>
        如果您在意此问题,您可以将出错信息复制,并将其发送给<a href=\"mailto:".$adminmail."\">管理员</a><br>
        点击这里<a href=\"./\">返回首页</a> </div></td>
  </tr>
</table>
</body>
</html>";
        if ($this->ExceptionDie) {
            die($message);
        } else {
            return $message;
        }
    }
    function Exception_throw($message, $ExceptionDie = 1) {
        $this->ExceptionMessage = $message;
        $this->ExceptionDie = $ExceptionDie;
        if ($this->ExceptionReport) {
            return  $this->EMessage();
        } else {
            return 1;
        }
    }
    function isE($object) {
        if (is_object($object)) {
            return 1;
        } else {
            return 0;
        }
    }
}
class db2html
{
    function db2html()
    {
        return 1;
    }
    function db2html_page($id)
    {
        global $DSN;
        if (page::Sar('power') == 2) {
            return 0;
        }
        $qs = "select * from `content` where id=".$id;
        $DB = page::useDB($DSN);
        if (DB::isError($DB)) {
            $Exception = New Exception_catch;
            return $Exception->Exception_throw("不能连接到数据库");
        }
        $queryRes = $DB->query($qs);
        if (DB::isError($DB)) {
            $Exception = New eException_catch;
            return $Exception->Exception_throw("数据库查询错误".mysql_error());
        }
        $res = $queryRes->fetchRow();
        $tpl = page::useTPL();
        $tpl->set_file(array("commonHead"=>"commonHead.html", "commonFoot"=>"commonFoot.html", "display"=>"display.html"));
        $tpl->set_var("createtime", date("r"));
        $tpl->set_var(array("id"=>$id, "htitle"=>$res[0]." - ","title"=>$res[0],"content"=>$res[1],"date"=>$res[2],"author"=>$res[3],"fromsite"=>$res[4],"editor"=>$res[5],"feditor"=>$res[6]));
        if ($res[7] == "") {
            $HtmlFileName = date("Y,m,d,His").".html";
            $ud = $DB->query("UPDATE `content` SET htmlfile='".$HtmlFileName."' WHERE id=".$id);
            if (DB::isError($ud)) {
                $Exception = New Exception_catch;
                return $Exception->Exception_throw("数据库查询错误".mysql_error());
            }
        } else {
            $HtmlFileName = $res[7];
        }
        ob_start();
        $tpl->pparse("commonHeads","commonHead");
        $tpl->pparse("displays", "display");
        $tpl->pparse("commonFoot", "commonFoot");
        $content = ob_get_contents();
        ob_end_clean();
        if (!page::writefile("content/".$HtmlFileName, $content)) {
            $Exception = New Exception_catch;
            return $Exception->Exception_throw("写文件的时候发生错误,程序中止,请检查是否有写权限");
        }
        $this->db2html_main();
    }
    function db2html_main()
    {
        global $DSN;
        $DB = page::useDB($DSN);
        if (DB::isError($DB)) {
            $Exception = New Exception_catch;
            return $Exception->Exception_throw("不能连接到数据库");
        }
        $tpl = page::useTpl();
        $tpl->set_file(array("commonHead"=>"commonHead.html", "list"=>"list.html", "commonFoot"=>"commonFoot.html"));
        $tpl->set_var(array("createtime"=>date("r")));
        $qs = "SELECT `title`,`htmlfile`,`date`,`isPermited` FROM `content` ORDER BY `id` DESC LIMIT 40";
        $queryRes = $DB->query($qs);
        if (DB::isError($queryRes)) {
            $Exception = New Exception_catch;
            return $Exception->Exception_throw("数据库查询错误,语句为:".$qs);
        }
        $tpl->set_block("list", "newsListBlock", "newsListBlocks");
        while ($res = $queryRes->fetchRow()) {
            if ($res[1] == "" || !$res[3]) {
            continue;
            }
            $tpl->set_var(array("htitle"=>"", "newsListTitle"=>$res[0], "newsListDate"=>$res[2], "newsListHtmlFile"=>$res[1]));
            $tpl->parse("newsListBlocks", "newsListBlock", TRUE);
        }
        /*--------------写文件--------------------------*/
        ob_start();
        $tpl->pparse("commonHeads", "commonHead");
        $tpl->pparse("lists", "list");
        $tpl->pparse("commonFoots", "commonFoot");
        $content = ob_get_contents();
        ob_end_clean();
        if (!page::writefile("content/ServerIndex.html", $content)) {
            $Exception = New Exception_catch;
            return $Exception->Exception_throw("写文件的时候发生错误,程序中止,请检查是否有写权限");
        } else {
            return 1;
        }
    }
}
?>
代码很乱,是我两年前写的。

解决方案 »

  1.   

    db2html::db2html_page()是生成子页面,db2html::db2html_main()是生成首页。db2html::db2html_page()自动调用db2html::db2html_main()调用: include_once("inc/makeHtml.class.php");    $queryRes = $DB->query("SELECT `id` FROM `content` ORDER BY id DESC limit 1");
        $res      = $queryRes->fetchRow();
        $db2html  = & New db2html;
        $page     = $db2html->db2html_page($res[0]);
        $main     = $db2html->db2html_main();
        if (Exception_catch::isE($page)) {
            die($page->ExceptionMessage);
        } elseif (Exception_catch::isE($main)) {
            die($main->ExceptionMessage);
        }
        $tpl->set_block("res", "actionResult", "actionResults");
        $qs = "insert into `content` values('".page::safePost('title')."', '".page::showtitle($contents, 50)."', '".page::safePost('date')."', '".page::safePost('author')."', '".page::safePost('fromsite')."',
                        '".page::safePost('editor')."',  '".page::safePost('feditor')."', '".page::safePost('htmlfile')."', '".page::safePost('isPermited')."', '".page::safePost('counter')."', '', '');";
                    page::Sar('power') == 2 ? $otherMessage = "2.您不是管理员或者审核编辑,请等待具有审核权限的用户审核发表此新闻" : $otherMessage = "2.已经更新相关文件";
                    $tpl->set_var(array("action"=>"添加新闻", "sql"=>$qs, "result"=>"1.新闻“".page::safePost('title')."”已经成功添加进数据库<br><font color=\"#006699\">".$otherMessage."</font>"));
                    $tpl->set_var("body", $tpl->parse("actionResults", "actionResult"));
                    $tpl->pparse("mains", "main");
      

  2.   

    首页用shtm,#include file="……"
    ……………………
    <tr>
    <td width="21" background="images/mid_10.jpg" bgcolor="#FFFFFF">
      </td>
    <td height="135" colspan="2" valign="top" bgcolor="#FFFFFF">帮助:<br>
     <!--#include file="contains/xinwen1.htm" --> 
      </td>
    <td width="32" background="images/mid_13.jpg" bgcolor="#FFFFFF">
      </td>
    </tr>
    <tr>
    ……
    <tr>
    <td width="21" background="images/mid_10.jpg" bgcolor="#FFFFFF">
      </td>
    <td height="135" colspan="2" valign="top" bgcolor="#FFFFFF">帮助:<br>
     <!--#include file="contains/xinwen2.htm" --> 
      </td>
    <td width="32" background="images/mid_13.jpg" bgcolor="#FFFFFF">
      </td>
    </tr>
    <tr>
    …………
    xinwen2.htm内容象这样:
    <table width="377"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="226">标题</td>
        <td width="62">点击</td>
        <td width="89">日期</td>
      </tr>
      <tr>
        <td><a href="xx.php?id=1">新闻一</a></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><a href="xx.php?id=2">新闻二</a></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>每次重新生成有变化的新闻内容表格就行了,这样似乎还科学一些。
      

  3.   

    $page = file_get_contents("http://localhost/index.php");
    file_put_contents("index.htm",$page);/** file_put_contents 只在php5中可用,php4中可自行定义 **/
    if(! function_exists('file_put_contents')) {
      function file_put_contents($filename, $str) {
        if( ! ($fp = fopen($filename, "wb")))
          return false;
        $n = fwrite($fp, $str);
        fclose($fp);
        return $n;
      }
    }
      

  4.   

    首页用shtm,#include file="……"
    ……………………
    <tr>
    <td width="21" background="images/mid_10.jpg" bgcolor="#FFFFFF">
      </td>
    <td height="135" colspan="2" valign="top" bgcolor="#FFFFFF">帮助:<br/>
     <!--#include file="contains/xinwen1.htm" --> 
      </td>
    <td width="32" background="images/mid_13.jpg" bgcolor="#FFFFFF">
      </td>
    </tr>
    <tr>
    ……
    <tr>
    <td width="21" background="images/mid_10.jpg" bgcolor="#FFFFFF">
      </td>
    <td height="135" colspan="2" valign="top" bgcolor="#FFFFFF">帮助:<br/>
     <!--#include file="contains/xinwen2.htm" --> 
      </td>
    <td width="32" background="images/mid_13.jpg" bgcolor="#FFFFFF">
      </td>
    </tr>
    <tr>
    …………
    xinwen2.htm内容象这样:
    <table width="377"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="226">标题</td>
        <td width="62">点击</td>
        <td width="89">日期</td>
      </tr>
      <tr>
        <td><a href="xx.php?id=1">新闻一</a></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><a href="xx.php?id=2">新闻二</a></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>每次重新生成有变化的新闻内容表格就行了,这样似乎还科学一些。