<?php session_start();
$hostname=$_SESSION[host];
$username=$_SESSION[user];
$userpwd=$_SESSION[pwd];
if(!$mbox=@imap_open("$hostname","$username","$userpwd")){
   echo "<script>alert('登录超时，请重新登录!');history.back();</script>";
   exit;
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>查找邮件</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<style type="text/css">
<!--
body {
	background-image: url(images/mrbg.gif);
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
-->
</style>
</head>
<script language="javascript">
  function chkinput(form){
    if(form.content.value==""){
	  alert("请输入要查找的内容！");
	  form.content.select();
	  return(false);
	}
   var i=form.content.value.indexOf("@");
	var j=form.content.value.indexOf(".");
	if((form.method.value=="1")&&((i<0)||(i-j>0)||(j<0)))
	 {
       alert("请输入正确的收件人E-mail地址!");
	   form.content.select();
	   return(false);
	 }	
  
  }

</script>
<body>
<table width="575" height="104" border="0" cellpadding="0" cellspacing="0" background="images/首页(2)_5.jpg">
  <tr>
    <td width="82" height="60">&nbsp;</td>
    <td width="493">&nbsp;</td>
  </tr>
  <tr>
    <td height="32">&nbsp;</td>
    <td><span class="STYLE34"><?php echo $lmbs;?></span></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
  <table width="550" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td valign="top">
<table width="550" height="25" border="0" cellpadding="0" cellspacing="0">
                        <tr>
                          <form action="index.php?lmbs=查找" method="post" name="form1" id="form1" onSubmit="return chkinput(this)">
                            <td align="center">查找邮件：
                                    <select name="method">
                                      <option value="1">发件人</option>
                                      <option value="2">主&nbsp;&nbsp;题</option>
                                    </select>
                    &nbsp;
                                    <input type="text" name="content" class="inputcss"  style="background-color:#e8f4ff " onMouseOver="this.style.backgroundColor='#ffffff'" onMouseOut="this.style.backgroundColor='#e8f4ff'" size="30" />
                    &nbsp;
                                    <input type="submit" value="查找" class="buttoncss" name="submit" /></td>
                          </form>
                        </tr>
</table>
<?php
	 if($_POST[submit]!=""){
	?>
                    <table width="550" height="50" border="1" cellpadding="1" cellspacing="1" bordercolor="#FFFFFF" bgcolor="#C6C5CA">
                                <tr align="center">
                                  <td width="186"  height="25" bgcolor="#FFECB7" class="STYLE1">邮件主题</td>
                                  <td width="153" bgcolor="#FFECB7" class="STYLE1">发件人</td>
                                  <td width="136" bgcolor="#FFECB7" class="STYLE1">发件时间</td>
                                  <td width="100" bgcolor="#FFECB7" class="STYLE1">大小</td>
                                </tr>
                                <?php
				  $method=$_POST[method];
				  $content=$_POST[content];
	 if($method=="1"){
				  $arr=imap_search($mbox,"FROM $content");
                            
				  if($arr!=false){
		            for($a=0;$a<count($arr);$a++){
				       $i=$arr[$a];
		               $obj=imap_headerinfo($mbox,$i); 
				?>
                                <tr>
                                  <td height="25" bgcolor="#FFFFFF" class="STYLE1"><div align="left">&nbsp;<a href="lookmailinfo.php?id=<?php echo $i?>" class="a1">
                                        <?php
									if(strtolower(substr($obj->Subject,0,10))==strtolower("=?gb2312?B"))
										echo base64_decode(substr($obj->Subject,11,(strlen($obj->Subject)-13)));
									elseif(strtolower(substr($obj->Subject,0,8))==strtolower("=?gbk?b?"))
										echo base64_decode(substr($obj->Subject,8,(strlen($obj->Subject)-11)));
									else
										echo $obj->Subject;

										?>
                                  </a></div></td>
                                  <td bgcolor="#FFFFFF" class="STYLE1"><div align="center"><?php echo ($obj->fromaddress);?></div></td>
                                  <td bgcolor="#FFFFFF" class="STYLE1"><div align="center">
                                      <?php 
			       $array=getdate(strtotime($obj->date));
			        echo $array[year]."-".$array[mon]."-".$array[mday]."&nbsp;".$array[hours].":".$array[minutes];
			   ?>
                                  </div></td>
                                  <td bgcolor="#FFFFFF" class="STYLE1"><div align="center">
                                      <?php 
			   $size=$obj->Size;
			    if($size>=1024)
				  {
				    echo number_format(($size/1024),2)."&nbsp;KB";
				  
				  }
				  elseif($size>1024*1024)
				  {
				    echo number_format(($size/(1024*1024)),2)."&nbsp;M";
				  }
				  elseif($size>1024*1024*1024)
				  {
				    echo number_format(($size/(1024*1024*1024)),2)."&nbsp;G";
				  }
				  elseif($size<1024)
				  {
				    echo ($size)."&nbsp;字节";
				  }
			?>
                                  </div></td>
                                </tr>
                                <?php   
				    }
				  }else{
				 
				  ?>
                                <tr>
                                  <td height="25" colspan="4" bgcolor="#FFFFFF" class="STYLE1"><div align="center">没有查找到您要找的邮件！</div></td>
                                </tr>
                                <?php
				  }
	    }elseif($method=="2"){
			     $check = imap_check($mbox);
		         $sum=$check->Nmsgs;
				 $arr=imap_search($mbox,"SUBJECT $content");
				if($arr!=false){
				 for($a=0;$a<count($arr);$a++){ 
				   $i=$arr[$a];
		         $obj=imap_headerinfo($mbox,$i); 
				?>
                                <tr>
                                  <td height="25" bgcolor="#FFFFFF" class="STYLE1"><div align="left">&nbsp;<a href="lookmailinfo.php?id=<?php echo $i?>" class="a1">
                                        <?php
									if(strtolower(substr($obj->Subject,0,10))==strtolower("=?gb2312?B"))
										echo base64_decode(substr($obj->Subject,11,(strlen($obj->Subject)-13)));
									elseif(strtolower(substr($obj->Subject,0,8))==strtolower("=?gbk?b?"))
										echo base64_decode(substr($obj->Subject,8,(strlen($obj->Subject)-11)));
									else
										echo $obj->Subject;

										?>
                                  </a></div></td>
                                  <td bgcolor="#FFFFFF" class="STYLE1"><div align="center"><?php echo ($obj->fromaddress);?></div></td>
                                  <td bgcolor="#FFFFFF" class="STYLE1"><div align="center">
                                      <?php 
			       $array=getdate(strtotime($obj->date));
			        echo $array[year]."-".$array[mon]."-".$array[mday]."&nbsp;".$array[hours].":".$array[minutes];
			   ?>
                                  </div></td>
                                  <td bgcolor="#FFFFFF" class="STYLE1"><div align="center">
                                      <?php 
			   $size=$obj->Size;
			    if($size>=1024)
				  {
				    echo number_format(($size/1024),2)."&nbsp;KB";
				  
				  }
				  elseif($size>1024*1024)
				  {
				    echo number_format(($size/(1024*1024)),2)."&nbsp;M";
				  }
				  elseif($size>1024*1024*1024)
				  {
				    echo number_format(($size/(1024*1024*1024)),2)."&nbsp;G";
				  }
				  elseif($size<1024)
				  {
				    echo ($size)."&nbsp;字节";
				  }
			?>
                                  </div></td>
                                </tr>
                                <?php
				     
				    }
				  }else{
				  ?>
                                <tr>
                                  <td height="25" colspan="4" bgcolor="#FFFFFF" class="STYLE1"><div align="center">没有查找到您要找的邮件！</div></td>
                                </tr>
                                <?php
				  }
			}	  
				?>
</table>                        
                  
<?php
	 }
	?></td>
                      </tr>
</table>
<?php
imap_close($mbox);
?>
</body>
</html>
