<?php
if(@$_FILES['image']['size']){
$uploaddir = "../userfiles/";//设置文件保存目录 注意包含/   
$type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型
$patch="userfiles/";//程序所在路径
   
   //获取文件后缀名函数
      function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }
   //生成随机文件名函数   
    function random($length)
    {
        $hash = 'tm_';
        $chars = '0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
            for($i = 0; $i < $length; $i++)
            {
                $hash .= $chars[mt_rand(0, $max)];
            }
        return $hash;
    }

   $a=strtolower(fileext($_FILES['image']['name']));
   //判断文件类型
   if(!in_array(strtolower(fileext($_FILES['image']['name'])),$type))
     {
        $text=implode(",",$type);
        echo "您只能上傳以下類型文件: ",$text,"<br>";
     }
   //生成目标文件的文件名   
   else{
    $filename=explode(".",$_FILES['image']['name']);
        do
        {
            $filename[0]=random(5); //设置随机数长度
            $name=implode(".",$filename);
            //$name1=$name.".Mcncc";
            $uploadfile=$uploaddir.$name;
        }

   while(file_exists($uploadfile));

        if (move_uploaded_file($_FILES['image']['tmp_name'],$uploadfile)){
            
			   $img=substr($uploadfile,3);
			   }
		else
			{
			  $img="";
			}
   }
}

include("qs.php");
require( '../data/config.php');
$conn=mysql_connect($db_host,$db_user,$db_pass);
mysql_query("set names 'utf8'");
mysql_select_db($db_name);
if (PHP_VERSION >= '5.1' && !empty($timezone))
{
    date_default_timezone_set($timezone);
}
$action=_get('action');
if($action=="")
$action=_post('action');
$id=_get('id');
if($id=="")
$id=_post('id');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<META http-equiv="Pragma" content="no-cache" />
<LINK href="css/general.css" rel="stylesheet" type="text/css" />
<LINK href="css/main.css" rel="stylesheet" type="text/css" />
</HEAD><BODY>
<?php 
if($action=="del")
 {
if(isset($_POST['chkkindid']) && is_array($_POST['chkkindid'])){
foreach($_POST['chkkindid'] as $tempid){
$exec="delete from tm_ad_list where id=".$tempid;
mysql_query($exec);
}
}
echo "<script>alert('删除成功!');location.href='adlist.php';</script>";
exit();
 }
elseif($action=="add")
{
$title=_post('title');
$typelist=_post('typelist');
$adlist=_post('adlist');
$adcode=_post('adcode');
$url=_post('url');
@$rs=mysql_query("insert into tm_ad_list(name,adtype,adcode,adacer,url,img) values('$title','$typelist','$adcode','$adlist','$url','$img')");
if($rs)
echo "<script>location.href='adlist.php';</script>";
else
echo "<script>alert('数据不能入库操作，请联系tmblog开发人员！');location.href='adlist.php';</script>";
exit();
}
elseif($action=="edit")
{
$title=_post('title');
$typelist=_post('typelist');
$adlist=_post('adlist');
$adcode=_post('adcode');
$url=_post('url');
if(@$img<>"")
 @$rs=mysql_query("update tm_ad_list set name='$title',adtype='$typelist',adcode='$adcode',adacer='$adlist',url='$url',img='$img' where id='$id'");
else
 @$rs=mysql_query("update tm_ad_list set name='$title',adtype='$typelist',adcode='$adcode',adacer='$adlist',url='$url' where id='$id'");
if($rs)
echo "<script>location.href='adlist.php';</script>";
else
echo "<script>alert('数据不能入库操作，请联系tmblog开发人员！');location.href='adlist.php';</script>";
 exit();
}
?>
<div class="h2">系统管理 >> 广告列表管理 >> <?php if($id=="") echo "添加";else echo "编辑";?></div>
<div class="list-div">

<table cellspacing='1' cellpadding='3'>
<tr>
  <th align='left'><table width="100" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td><div align="left">&nbsp;&nbsp; 广告列表管理</div></td>
        <td width="150">&nbsp;</td>
      </tr>
    </table></th>
</tr>
<tr><td align="left" valign="top" style="background:#F5F5F5;padding-bottom:8px;">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0 CALLPADDING="0">
<?php 			  
if($action=="" and $id=="")
{
?>
<form id="form1" name="form1" method="post" action="adlist_w.php" enctype="multipart/form-data">
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告名称：
                                  <input name="title" type="text" id="title" size="50" />
			                  </span></TD>
                    </TR>
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告类型：
              <select name="typelist" id="typelist" onChange="changelocation1(document.form1.typelist.options[document.form1.typelist.selectedIndex].value)">
                    <option value="0">图片广告</option>
					<option value="1">代码广告</option>
              </select>
			  </span></TD>
                    </TR>
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告位置：
                                  <select name="adlist" id="adlist">
                    <option value="0">顶部通栏广告</option>
					<option value="1">右边轮播广告</option>
					<option value="2">右边其它广告</option>
              </select>
			                      <span class="post_date">(提示：右边轮播广告不可使用代码,否则无效！)</span></span></TD>
            </TR>

			<TR BORDER="0" WIDTH="100%" id="adurl01">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告链接：
                                  
			                      <input name="url" type="text" id="url" size="50" />
			                  </span></TD>
            </TR>
			<TR BORDER="0" WIDTH="100%" id="adimg02">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">上传图片：
                                  
			                      <input name="image" type="file" id="image" />
			                  </span></TD>
            </TR>
			<TR BORDER="0" WIDTH="100%" id="adcode03" style="display:none;">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告代码：
                                  
			                      <textarea name="adcode" cols="50" rows="5" id="adcode"></textarea>
			                  </span></TD>
            </TR>
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30><span class="help">
                  <input type="image" 
                        src="images/submit.gif" align="absmiddle" border="0" 
                        name="submit" onclick="return checkform(this.form)" />
                  <input name="action" type="hidden" id="action" value="add" />
                </span></TD>
              </TR>
 </form>
 <script language="javascript">
function checkform(theform)
{
if(theform.title.value=="")
  {
  alert("请输入广告名称！");
  theform.title.focus();
  return false;
  }
return true;
}
</script>
 <?php 
 }
if($action=="" and $id<>"")
{
$sql="select * from tm_ad_list where id='$id'";
$rsql=mysql_query($sql);
$rs=mysql_fetch_object($rsql);
 ?>
 <form id="form1" name="form1" method="post" action="adlist_w.php" enctype="multipart/form-data">
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告名称：
                                  <input name="title" type="text" id="title" value="<?php echo $rs->name;?>" size="50" />
			                  </span>			                  </TD>
    </TR>
	<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告类型：
             <select name="typelist" id="typelist" onChange="changelocation1(document.form1.typelist.options[document.form1.typelist.selectedIndex].value)">
                    <option value="0" <?php if($rs->adtype==0) echo 'selected="selected"';?>>图片广告</option>
					<option value="1" <?php if($rs->adtype==1) echo 'selected="selected"';?>>代码广告</option>
              </select>
			                  </span></TD>
                    </TR>
			<TR BORDER="0" WIDTH="100%">
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告位置：
                                  <select name="adlist" id="adlist">
                    <option value="0" <?php if($rs->adacer==0) echo 'selected="selected"';?>>顶部通栏广告</option>
					<option value="1" <?php if($rs->adacer==1) echo 'selected="selected"';?>>右边轮播广告</option>
					<option value="2" <?php if($rs->adacer==2) echo 'selected="selected"';?>>右边其它广告</option>
              </select>
			                      <span class="post_date">(提示：右边轮播广告不可使用代码,否则无效！)</span></span></TD>
            </TR>
			<TR BORDER="0" WIDTH="100%" id="adurl01" <?php if($rs->adtype==1) echo 'style="display:none;"';?>>
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">广告链接：
                                  
			                      <input name="url" type="text" id="url" value="<?php echo $rs->url;?>" size="50" />
			                  </span></TD>
            </TR>
			<TR BORDER="0" WIDTH="100%" id="adimg02" <?php if($rs->adtype==1) echo 'style="display:none;"';?>>
			                  <TD height="30"><span style="PADDING-LEFT: 8px; PADDING-TOP: 8px">
							  <img src="../<?php echo $rs->img;?>" />
							  <br />
							  上传图片：
                                  
			                      <input name="image" type="file" id="image" />
			                  </span></TD>
            </TR>
			<TR BORDER="0" WIDTH="100%" id="adcode03" <?php if($rs->adtype==0) echo 'style="display:none;"';?>>
              <TD height="30">广告代码：
                                  
			                      <textarea name="adcode" cols="50" rows="5" id="adcode"><?php echo $rs->adcode;?></textarea></TD>
            </TR>
			<TR BORDER="0" WIDTH="100%">
                <TD style="PADDING-LEFT: 8px; PADDING-TOP: 8px" vAlign=top 
                align=left bgColor=#ffffff height=30><span class="help">
                  <input type="image" 
                        src="images/submit.gif" align="absmiddle" border="0" 
                        name="submit" onclick="return checkform(this.form)" />
                  <input name="action" type="hidden" id="action" value="edit" />
                  <input name="id" type="hidden" id="id" value="<?php echo $id;?>" />
                </span></TD>
              </TR>
 </form>
  <script language="javascript">
function checkform(theform)
{
if(theform.title.value=="")
  {
  alert("请输入广告名称！");
  theform.title.focus();
  return false;
  }
return true;
}
</script>
 <?PHP }
 ?>
      </TABLE>
</td>
</tr></table></div>

<?php include "boot.php";?>
</body></html>
<script language="javascript">
function changelocation1(locationid)
    {
	if(locationid==0)
	   {
	   document.getElementById("adcode03").style.display="none";
	   document.getElementById("adurl01").style.display="block";
	   document.getElementById("adimg02").style.display="block";
	   
	   }
	else
	   {
	   document.getElementById("adcode03").style.display="block";
	   document.getElementById("adurl01").style.display="none";
	   document.getElementById("adimg02").style.display="none";
	   }
        
    }  
</script> 