<?php
/**
 *
 * 留言板
 *
 * @version        $Id: guestbook.php 1 10:09 2010-11-10 tianya $
 * @package        NameCMS.Site
 * @copyright      Copyright (c) 2007 - 2010, DesDev, Inc.
 * @license        http://help.NameCMS.com/usersguide/license.html
 * @link           http://www.NameCMS.com
 */
require_once(dirname(__FILE__).'/guestbook/guestbook.inc.php');
require_once(DEDEINC.'/datalistcp.class.php');
if(empty($action)) $action = '';
//修改留言
if($action=='admin')
{
    if(!defined('DEDEINC')) exit('Request Error!');

if(!empty($_COOKIE['GUEST_BOOK_POS'])) $GUEST_BOOK_POS = $_COOKIE['GUEST_BOOK_POS'];
else $GUEST_BOOK_POS = "guestbook.php";

$id = intval($id);
if(empty($job)) $job='view';

if($job=='del' && $g_isadmin)
{
    $dsql->ExecuteNoneQuery(" DELETE FROM `#@__guestbook_y` WHERE id='$id' ");
    ShowMsg("成功删除一条留言！", $GUEST_BOOK_POS);
    exit();
}
else if($job=='check' && $g_isadmin)
{
    $dsql->ExecuteNoneQuery(" UPDATE `#@__guestbook_y` SET ischeck=1 WHERE id='$id' ");
    ShowMsg("成功审核一条留言！", $GUEST_BOOK_POS);
    exit();
}
else if($job=='editok')
{
    $remsg = trim($remsg);
    if($remsg!='')
    {
        //管理员回复不过滤HTML
        if($g_isadmin)
        {
            $msg = "<div class=\\'rebox\\'>".$msg."</div>\n".$remsg; 
            //$remsg <br><font color=red>管理员回复：</font>
        }
        else
        {
            $row = $dsql->GetOne("SELECT msg From `#@__guestbook_y` WHERE id='$id' ");
            $oldmsg = "<div class=\\'rebox\\'>".addslashes($row['msg'])."</div>\n";
            $remsg = trimMsg(cn_substrR($remsg, 1024), 1);
            $msg = $oldmsg.$remsg;
        }
    }
    $dsql->ExecuteNoneQuery("UPDATE `#@__guestbook_y` SET `msg`='$msg', `posttime`='".time()."' WHERE id='$id' ");
    ShowMsg("成功更改或回复一条留言！", $GUEST_BOOK_POS);
    exit();
}

if($g_isadmin)
{
    $row = $dsql->GetOne("SELECT * FROM `#@__guestbook_y` WHERE id='$id'");
    require_once(DEDETEMPLATE.'/plus/guestbook-admin.htm');
}
else
{
    $row = $dsql->GetOne("SELECT id,title FROM `#@__guestbook_y` WHERE id='$id'");
    require_once(DEDETEMPLATE.'/plus/guestbook-user.htm');
}
}
//保存留言
else if($action=='save')
{
    if(!empty($_COOKIE['GUEST_BOOK_POS'])) $GUEST_BOOK_POS = $_COOKIE['GUEST_BOOK_POS'];
    else $GUEST_BOOK_POS = 'guestbook_y.php';
    if(empty($validate)) $validate=='';
    else $validate = strtolower($validate);
    $svali = GetCkVdValue();
    if($validate=='' || $validate!=$svali)
    {
         ShowMsg("验证码不正确!",'-1');
         exit();
    }
    $ip = GetIP();
    $dtime = time();
    $uname = trimMsg($uname);
    $email = trimMsg($email);
    $homepage = trimMsg($homepage);
    $qq = trimMsg($qq);
    $msg = trimMsg($msg);
    $tid = empty($tid) ? 0 : intval($tid);
    $reid = empty($reid) ? 0 : intval($reid);

    if($msg=='' || $uname=='') {
        showMsg('客户名称和留言内容不能为空!','-1');
        exit();
    }
    $title =trimMsg($title);
    if($title=='') $title = '无标题';
    
    if($reid != 0)
    {
        $row = $dsql->GetOne("SELECT msg FROM `#@__guestbook_y` WHERE id='$reid' ");
        $msg = "<div class=\\'rebox\\'>".addslashes($row['msg'])."</div>\n".$msg;
    }

    $query = "INSERT INTO `#@__guestbook_y`(title,tid,mid,uname,email,homepage,qq,face,msg,ip,dtime,ischeck)
                  VALUES ('$title','$tid','{$g_mid}','$uname','$email','$homepage','$qq','$img','$msg','$ip','$dtime','$needCheck'); ";
    $dsql->ExecuteNoneQuery($query);
    $gid = $dsql->GetLastID();
    if($needCheck==1)
    {
//        require_once(DEDEINC."/oxwindow.class.php");
//        $msg = "
//        <font color='red'><b>成功发送或回复留言！</b></font> &nbsp; <a href='guestbook.php' style='font-size:14px;font-weight:bold'><u>我已经知道了，点击此返回&gt;&gt;</u></a>";
//      $wintitle = "留言发布成功提示";
//        $wecome_info = "留言发布成功：";
//        $win = new OxWindow();
//        $win->Init("","js/blank.js","post");
//        $win->AddTitle("提示：");
//        $win->AddMsgItem("<div style='padding:20px;line-height:300%;font-size:14px'>$msg</div>");
//        $winform = $win->GetWindow("hand");
//        $win->Display();
    }
    else {
        ShowMsg('成功发送一则留言，但需审核后才能显示！','guestbook_y.php',0,3000);
    }
    exit();
}
//显示所有留言
else
{
    setcookie('GUEST_BOOK_POS',GetCurUrl(),time()+3600,'/');

    if($g_isadmin) $sql = 'SELECT * FROM `#@__guestbook_y` ORDER BY id DESC';
    else $sql = 'SELECT * FROM `#@__guestbook_y` WHERE ischeck=1 ORDER BY id DESC';

    $dlist = new DataListCP();
    $dlist->pageSize = 10;
    $dlist->SetParameter('gotopagerank',$gotopagerank);
    $dlist->SetTemplate(DEDETEMPLATE.'/plus/guestbook_y.htm');
    $dlist->SetSource($sql);
    $dlist->Display();
}