<?php
session_start();
include "header.html";
require_once "../MessageBox.php";
include "../datasource.inc.php";
include "../Function.php";
$Message = new MessageBox;
if ( empty($_SESSION["usernumber"]) and $_SESSION["userjs"] != "1") {
    $Message->setMessage("对不起，您没有权限进入后台管理!","logout.php");
    $Message->showMessage();
}
$page      = $_REQUEST['page'];
$id        = $_REQUEST['id'];
$title     = $_REQUEST['title'];
$url       = $_REQUEST['url'];
$body       = $_REQUEST['body'];
$action    = $_REQUEST['action'];
$date      = Date("Y-m-j H:i:s");
$file_name = $_FILES['picpath']['name'];
mt_srand((double)microtime()*1000000);
$shpid     =uniqid(mt_rand());
$shpid     = substr($shpid,0,6);
$file_name =$shpid.$file_name;
$picpath   = $path.$file_name;
if ($action == 'add') {
	$file_name    = $_FILES['picpath']['name'];
    mt_srand((double)microtime()*1000000);
    $shpid = uniqid(mt_rand());
    $shpid = substr($shpid,0,6);
	$file_name1 = $shpid.$file_name;
	$picpath 	= $image_path.$file_name1;
	if ($file_name) {
		copy($_FILES['picpath']['tmp_name'],$picpath);
		$shpidimgname   = $shpid.".".substr($file_name,-3);
		@rename("$image_path/$file_name1","$image_path/$shpidimgname");
	}
    $sql     = "insert into zht_links values ('','$_REQUEST[url]','$_REQUEST[title]','$shpidimgname','$date');";
    $result  = mysql_query($sql);
    ShowMessage(4,"添加成功!","links.php");
}
if ($_REQUEST['action'] == 'update') {
	$file_name    = $_FILES['picpath']['name'];
    mt_srand((double)microtime()*1000000);
    $shpid = uniqid(mt_rand());
    $shpid = substr($shpid,0,6);
	$file_name1 = $shpid.$file_name;
	$picpath 	= $image_path.$file_name1;
	if ($file_name) {
		copy($_FILES['picpath']['tmp_name'],$picpath);
		$shpidimgname   = $shpid.".".substr($file_name,-3);
		@rename("$image_path/$file_name1","$image_path/$shpidimgname");
	}
    if (!empty($file_name)){
        $sql     = "update zht_links set url='$_REQUEST[url]',title='$_REQUEST[title]',picpath='$shpidimgname' where id='$_REQUEST[id]';";
	} else {
	    $sql     = "update zht_links set url='$_REQUEST[url]',title='$_REQUEST[title]' where id='$_REQUEST[id]';";
    }
    $result  = mysql_query($sql);
    ShowMessage(4,"编辑成功!","links.php");
}
//echo "<body background=../images/back02.jpg>";
switch ($page){
    case "add":
?>
<script language=javascript>
function check_input()
{
	if (document.form.title.value=='') {
		alert("请输入网站名称!");
		return false;
	}
	if (document.form.url.value=='') {
		alert("请输入链接地址!");
		return false;
	}
 return true;
}
</script>
<center>
<table width="700" border="0" cellspacing="0" cellpadding="0">
        <tr>
         <TD vAlign=top><FONT color=#565656><IMG height=14 
      src="../images/closedfold.gif" width=14>&nbsp;&nbsp;您当前的位置<BR><IMG 
      height=15 src="../images/bar.gif" width=15><IMG height=14 
      src="../images/openfold.gif" width=14>&nbsp;&nbsp;添加友情链接</FONT></TD>
        </tr>
</table>
<p align="center">&nbsp;<b><font size="5">请添加友情链接</font></b></p>
<form enctype="multipart/form-data" name=form  method="post" action="links.php" onsubmit="return check_input()">
<table width="460" border="0" cellspacing="1" cellpadding="0" bgColor=#6795B4>
  <tr> 
    <td width="30%" bgColor=#EBF1F5 align="center"><b>网站名称</b></td>
    <td width="70%" bgColor=#ffffff><input type="text" name="title" size="30"></td>
  </tr>
  <tr> 
    <td width="30%" bgColor=#EBF1F5 align="center"><b>链接地址</b></td>
    <td width="70%" bgColor=#ffffff><input type="text" size="30" name="url" value="http://"></td>
  </tr>
  <tr>
    <td width="30%" bgColor=#EBF1F5><center><b>网站图标</b></center></td>
    <td width="70%" bgColor=#ffffff><input type="file" name="picpath" size="30"></td>
  </tr>    
</table><br>
<input type="hidden" value="add" name="action">
<input type="submit" value="提 交" >&nbsp;&nbsp;<input type="reset"  value="取 消">
</form>
</center>
<?
		break;
	case "delete":
	     $query     = "delete from zht_links where id='$id';";
         mysql_query($query);
         $Message->setMessage("删除成功!","links.php");
         $Message->showMessage();
		 break;
	case "update":
         $sql       = "select * from zht_links where id='$_REQUEST[id]';";
         $result    = mysql_query($sql);
         $row       = mysql_fetch_array($result);
?>
<script language=javascript>
function check_input()
{
	if (document.form.title.value=='') {
		alert("请输入网站名称!");
		return false;
	}
	if (document.form.url.value=='') {
		alert("请输入链接地址!");
		return false;
	}
 return true;
}
</script>
<center>
<table width="700" border="0" cellspacing="0" cellpadding="0">
        <tr>
         <TD vAlign=top><FONT color=#565656><IMG height=14 
      src="../images/closedfold.gif" width=14>&nbsp;&nbsp;您当前的位置<BR><IMG 
      height=15 src="../images/bar.gif" width=15><IMG height=14 
      src="../images/openfold.gif" width=14>&nbsp;&nbsp;友情链接管理</FONT></TD>
        </tr>
</table>
<p align="center">&nbsp;<b><font size="5">请编辑友情链接</font></b></p>
<form enctype="multipart/form-data" name=form  method="post" action="links.php" onsubmit="return check_input()">
<table width="460" border="0" cellspacing="1" cellpadding="0" bgcolor=#6795B4>
  <tr> 
    <td width="30%" bgColor=#EBF1F5 align="center"><b>网站名称</b></td>
    <td width="70%" bgColor=#ffffff> 
     <?php echo "<input type=text name=\"title\" size=30 value=\"$row[title]\">"; ?>
    </td>
  </tr>
  <tr> 
    <td width="30%" bgColor=#EBF1F5 align="center"><b>链接地址</b></td>
    <td width="70%" bgColor=#ffffff> 
     <?php echo "<input type=text size=30 name=\"url\" value=\"$row[url]\">";?>
    </td>
  </tr>
  <tr>
    <td width="30%" bgColor=#EBF1F5><center><b>网站图标</b></center></td>
    <td width="70%" bgColor=#ffffff><input type="file" name="picpath" size="30"></td>
  </tr>    
</table><br>
<input type="hidden" value="update" name="action">
<?php echo "<input type=hidden name=\"id\"  value=\"$row[id]\">"; ?>  
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="submit" value="编 辑" name="update1">&nbsp;&nbsp;<input type="reset"  value="取 消">
</form>
</center>
<?php
    break;

	case "text":
         $sql     = "select * from pro where id='$id';";
         $result  = mysql_query($sql);
         $row     = mysql_fetch_array($result);
		 $picpath = $row['picpath'];
?>
<br>
<center>
<?php echo "<img border=0 width=182 height=120 src=../upload-images/$row[picpath]>"; ?>
<table width="400" border="1">
  <tr><td><b><center>产品分类</b></center></td><td><?php echo $row['fl']; ?></td></tr>
  <tr><td><b><center>产品名称</b></center></td><td><?php echo $row['name']; ?></td></tr>
  <tr><td><b><center>产品图片</b></center></td><td><?php echo $row['picpath']; ?></td></tr>
  <tr><td><b><center>产品介绍</b></center></td><td><?php echo $row['body']; ?></td></tr>
</table>
<br><a href="pro.php">返回</a>
</center>
<?php
	break;
	
	default:
?>
<center>
<table width="700" border="0" cellspacing="0" cellpadding="0">
        <tr>
         <TD vAlign=top><FONT color=#565656><IMG height=14 
      src="../images/closedfold.gif" width=14>&nbsp;&nbsp;您当前的位置<BR><IMG 
      height=15 src="../images/bar.gif" width=15><IMG height=14 
      src="../images/openfold.gif" width=14>&nbsp;&nbsp;友情链接管理</FONT></TD>
        </tr>
</table>
</center>
<?php
    $sql    = "select * from zht_links order by id desc;";
    $result = mysql_query($sql);
    $num    = mysql_num_rows($result);		
    if ( $num>0 ){
    $last   = intval(($num-1)/10)+1;
    if ( $page < 1 ) $page=1;
    if ( $page > $last ) $page=$last;
    mysql_data_seek($result,($page-1)*10);
	echo "<center><br><table border=0>";
	echo "<tr>";	
	echo "<table border=0 width=600 cellspacing=1 cellpadding=2 bgcolor=6795B4>";
	echo "<tr><td align=center><b>网站名称</b></td><td align=center><b>网站图标</b></td><td align=center><b>链接地址</b></td><td colspan=2 align=center><b>操 作</b></td></tr>";
	for ( $i=0;$i<10;$i++ ) {
         $row     = mysql_fetch_array($result);
	     $id      = $row['id'];
		 $picpath = $row['picpath'];
		 if ( $id ) {
		          echo "<tr height=20 bgcolor=ffffff align=center><td width=200><a href=$row[url] target=_blank>$row[title]</a></td><td width=110 align=center><img src=\"../upload-images/$row[picpath]\" width=103 height=34></td>";
		          echo "<td width=220 align=center><font color=red>$row[url]</font><td width=30 align=right><a href=links.php?page=update&id=$id>编辑</a></td></td><td width=30 align=right><a href=links.php?page=delete&id=$id>删除</a></td></tr>";
		 }
    }
    echo "</table><br>";
          echo "<table width=500 border=0 cellspacing=0 cellpadding=0 height=24>";
          echo "<tbody>";
          echo "<tr><td><center>";	
		  echo "总共找到<font color=red>$num</font>条记录,第<font color=red>$page</font>页,共<font color=red>$last</font>页。";		  
          echo "</center></td></tr><tr>";	  
          if ( $page <> 1 ) {
              echo "<td align=center>&nbsp;&nbsp;&nbsp;<a href=links.php?page=1>第一页</a></td>";
              echo "<td align=center><a href=links.php?page=".($page-1).">上一页</a></td>";
          }
          if ( $page <> $last ) {
              echo "<td align=center><a href=links.php?page=".($page+1).">下一页</a></td>";
              echo "<td align=center><a href=links.php?page=$last>最后一页</a></td>";
          }
          echo "</tbody>";
          echo "</tr>";
          echo "</table><br>";
		  echo "</center>";
   } else {
      echo "<center>对不起，您还没有添加记录,<a href=links.php?page=add>我要添加</a>!</center>";
   }

	break;
}
?>