PHP操作IMAP服务器的类

  我认为是相当经典的,帮过我大忙,可以实现很多PHP的IMAP函数无法实现的功能。以前在广州站贴过,后来北京站还没有PHP版本,现在再贴一个吧。:)<
  ?php
  /*****************************************************************
  文件:cyradm.inc.php
  作者:忘记,嘻嘻
  日期:2000-11-01
  这是IMAP Access for PHP的全新实现
  。它基于与服务器的套接字连接,
  独立于PHP的imap函数
  ***************************************************************/
  类cyradm{
  var$host;
  var$端口;
  var$mbox;
  var$列表;
  var$admin;
  var$pass;
  var$fp;
  var$行;
  var$error_msg;
  /*
  #
  #Konstruktor
  #
  */
  函数cyradm($IMAP_HOST="localhost",$IMAP_ADMIN="",$IMAP_PW="",$IMAP_PORT="143"){
  $this->host=$IMAP_HOST;
  $这个->端口=$IMAP_PORT;
  $this->mbox="";
  $this->list=array();
  $this->admin=$IMAP_ADMIN;
  $this->pass=$IMAP_PW;
  $this->fp=0;
  $this->line="";
  $this->error_msg="";
  }
  /*
  #
  #通过Telnet连接在服务器上进行SOCKETLOGIN!
  #
  */
  function imap_login(){
  $this->fp=fsockopen($this->host,$this->port,&$errno,&$errstr);
  $this->error_msg=$errstr;
  if(!$this->fp){
  echo"<br>ERRORNO:($errno)<br>ERRSTR:($errstr)<br><hr>\n";
  }else{
  $this->command(".login\"$this->admin\"\"$this->pass\"");
  返回
  $errno;
  }
  /*
  #
  #通过Telnet连接从服务器进行SOCKETLOGOUT!
  #
  */
  function imap_logout(){
  $this->command(".logout");
  fclose($this->fp);
  }
  /*
  #
  #通过Telnet连接向服务器发送命令!
  #
  */
  function command($line){
  /*print("$line<br>");*/
  $结果=数组();
  $i=0;$f=0;
  $returntext="";
  $r=fputs($this->fp,"$line\n");
  while(!((strstr($returntext,".OK")||(strstr($returntext,".NO"))||(strstr($returntext,".
  if(strstr($returntext,".BAD")||(strstr($returntext,".
  NO"))){
  $
  result[0]="$returntext";}}if(strstr($returntext,".BAD")||(strstr($returntext,".NO"))){
  $this->error_msg=$returntext;
  if((strstr($returntext,".无配额")))
  {
  }
  else
  {
  print"<br><hr><H1><center><blink>错误:</blink>意外的IMAP-SERVER-ERROR</center></H1><hr><br>
  <table color=red border=0align=center cellpadding=5callspacing=3>
  <tr><td>发送命令:</td><td>$line</td></tr>
  <tr><td>服务器返回:</td><td></td></tr>
  ";
  for($i=0;$i<count($result);$i++){
  print"<tr><td></td><td>$result[$i]</td></tr>";
  }
  打印“</table><hr><br><br>”;
  返回$
  结果
  ;
  }
  /*
  #
  #通过Telnet连接从服务器读取!
  #
  */
  function getline(){
  $this->line=fgets($this->fp,256);
  返回$this->行;
  }
  /*
  #
  #QUOTA函数
  #
  */
  //获取配额
  function getquota($mb_name){
  $output=$this->command(".getquota\"$mb_name\"");
  if(strstr($output[0],".NO"))
  {
  $ret["used"]="NOT-SET";}
  $ret["qmax"]="未设置";
  }
  else
  {
  $realoutput=str_replace(")","",$output[0]);
  $tok_list=split("",$realoutput);
  $si_used=sizeof($tok_list)-2;
  $si_max=sizeof($tok_list)-1;
  $ret["used"]=str_replace(")","",$tok_list[$si_used]);
  $ret["qmax"]=$tok_list[$si_max];
  返回
  $ret;
  }
  //设置配额
  function setmbquota($mb_name,$quota){
  $this->command(".setquota\"$mb_name\"(STORAGE$quota)");
  }
  /*
  #
  #邮箱函数
  #
  */
  function createmb($mb_name,$mb_partition=""){
  $this->command(".create\"$mb_name\"$mb_partition");
  }
  function deletemb($mb_name){
  $this->command(".setacl\"$mb_name\"$this->admin d");
  $this->命令(".
  $all="lrswipcda";$find_out=数组();$split_res=数组();$所有者=“”;$oldowner="";
  /*Anlegen和Kopieren der INBOX*/
  $this->createmb($to_mb_name);
  $this->setacl($to_mb_name,$this->admin,$all);
  $this->copymailsfromfolder($from_mb_name,$to_mb_name);
  /*配额uebernehmen*/
  $quota=$this->getquota($from_mb_name);
  $oldquota=trim($quota["qmax"]);
  if(strcmp($oldquota,"NOT-SET")!=0){
  $this->setmbquota($to_mb_name,$oldquota);
  }
  /*Den Rest Umbenennen*/
  $username=str_replace(".","/",$from_mb_name);
  $split_res=explode(".",$to_mb_name);
  if(strcmp($split_res[0],"用户")==0){
  $owner=$split_res[1];
  $
  split_res=explode(".",$from_mb_name);
  if(strcmp($split_res[0],"用户")==0){
  $oldowner=$split_res[1];
  $
  find_out=$this->GetFolders($用户名);
  for($i=0;$i<count($find_out);$i++){
  if(strcmp($find_out[$i],$username)!=0){
  $split_res=split("$username",$find_out[$i]);
  $split_res[1]=str_replace("/",".",$split_res[1]);
  $this->renamemb((str_replace("/",".",$find_out[$i])),("$to_mb_name"."$split_res[1]"));
  if($owner){
  $this->setacl(("$to_mb_name"."$split_res[1]"),$owner,$all);
  }
  if($oldowner){
  $this->deleteacl(("$to_mb_name"."$split_res[1]"),$oldowner);
  }
  };
  $
  this->deleteacl($to_mb_name,$this->admin);
  $this->imap_logout();
  $this->imap_login();
  $this->deletemb($from_mb_name);
  }
  函数copymailsfromfolder($from_mb_name,$to_mb_name){
  $com_ret=array();
  $find_out=数组();
  $all="lrswipcda";
  $邮件=0;
  $this->setacl($from_mb_name,$this->admin,$all);
  $com_ret=$this->command(".选择$from_mb_name");
  for($i=0;$i<count($com_ret);$i++){
  if(strstr($com_ret[$i],"EXISTS"))
  {
  $findout=explode("",$com_ret[$i]);
  $mails=$findout[1];
  }
  }
  if($mails!=0){
  $com_ret=$this->command(".copy 1:$mails$to_mb_name");
  for($i=0;$i<count($com_ret);$i++){
  print"$com_ret[$i]<br>";
  $
  this-
  >deleteacl($from_mb_name,$this->admin);
  }
  /*
  #
  #ACL函数
  #
  */
  function setacl($mb_name,$user,$acl){
  $this->command(".setacl\"$mb_name\"\"$user\"$acl");
  }
  function deleteacl($mb_name,$user){
  $result=$this->command(".deleteacl\"$mb_name\"\"$user\"");
  }
  函数getacl($mb_name){
  $aclflag=1;$tmp_pos=0;
  $output=$this->command(".getacl\"$mb_name\"");
  $输出=爆炸("",$输出[0]);
  $i=计数($输出)-1;
  while($i>3){
  if(strstr($output[$i],'"')){
  $i++;
  }
  if(strstr($output[$i-1],'"')){
  $acflag=1;
  $lauf=$i-1;
  $spacestring=$output[$lauf];
  $tmp_pos=$i;
  $i=$i-2;
  while($aclflag!=0)
  {
  $spacestring=$output[$i]."".$spacestring;
  if(strstr($output[$i],'"')){$aclflag=0;}
  $i--;
  }
  $spacestring=str_replace("\"","",$spacestring);
  if($i>2){
  $ret[$spacestring]=$output[$tmp_pos];
  }
  }
  else
  {
  $ret[$output[$i-1]]=$output[$i];
  $i=$i-2;
  返回$
  ret
  ;
  }
  /*
  #
  #文件夹函数
  #
  */
  function GetFolders($username){
  $username=str_replace("/",".",$username);
  $output=$this->command(".list\"$用户名\"*");
  for($i=0;$i<count($output);$i++){
  $splitfolder=split("\"",$output[$i]);
  $output[$i]=str_replace(".","/",$splitfolder[3]);
  }
  return$output;
  }
  function EGetFolders($username){
  $lastfolder=split("/",$username);
  $position=count($lastfolder)-1;
  $last=$lastfolder[$position];
  $username=str_replace("/",".",$username);
  $output=$this->command(".list\"$username\"*");
  for($i=0;$i<计数($output);$i++){
  $splitfolder=split("\"",$output[$i]);
  $currentfolder=split("\.",$splitfolder[3]);
  $当前=$当前文件夹[$位置];
  //echo"<br>文件夹:($)CURRENTFOLDER:($splitfolder[3])CURRENT:($current)LAST:($last)POSITION:($position)<br>";
  if(strcmp($current,$last)==0){
  $newoutput[$i]=str_replace(".","/",$分割文件夹[3]);
  返回$
  newoutput
  ;
  }
  /*
  #
  #文件夹输出函数
  #
  */
  functionGenerateFolderList($folder_array,$username)
  {
  print"<table border=0align=center>";
  for($l=0;$l<count($folder_array);$l++)
  {
  echo"<tr><td><a href=\"acl.php?username=",
  urlencode($username),
  "&folder=",
  urlencode($folder_array[$l]),
  "\">/$folder_array[$l]</td></tr>\n";
  };
  print"</table>";
  }
  函数GetUsers($char){
  $users=array();
  $this->imap_login();
  $output=$this->GetFolders("user.$char");
  $this->imap_logout();
  $j=0;
  $prev=0;
  for($i=0;$i<count($output);$i++){
  $username=split("/",$output[$i],-1);
  $this->debug("($username[1]),
  $users[$prev])");
  if((isset($username))&&(isset($users))){
  if(strcmp($username[1],$users[$prev])){
  $users[$j]=$username[1];
  $j++;
  }
  }
  if($j!=0){$prev=$j-1;}
  }
  return$users;
  }
  function debug($message){
  //echo"<hr>$message<br><hr>";
  }
  }//KLASSEN ENDE
  ?>