巴黎豆豆 2008-8-8 10:54
PHP操作IMAP服务器的类
我认为是相当经典的,帮过我大忙,可以实现很多[url=http://whatis.ctocio.com.cn/searchwhatis/77/6026077.shtml]PHP[/url]的IMAP函数不能实现的功能。以前在广州站贴过,那时北京站还没PHP版,现在再贴一个吧。:) <BR><BR><?php <BR>/************************************************************* <BR><BR>File: cyradm.inc.php <BR>Author: 忘了,嘻嘻 <BR>Date: 2000-11-01 <BR><BR>This is a completely new [url=http://whatis.ctocio.com.cn/searchwhatis/479/6025479.shtml]implementation[/url] of the [url=http://whatis.ctocio.com.cn/searchwhatis/474/6025474.shtml]IMAP[/url] Access for <BR>PHP. It is based on a socket [url=http://whatis.ctocio.com.cn/searchwhatis/393/5947393.shtml]connection[/url] to the [url=http://whatis.ctocio.com.cn/searchwhatis/497/5948997.shtml]server[/url] an is <BR>independent from the imap-Functions of PHP <BR><BR>***************************************************************/ <BR><BR>[url=http://whatis.ctocio.com.cn/searchwhatis/213/5947213.shtml]class[/url] cyradm { <BR><BR>var $[url=http://whatis.ctocio.com.cn/searchwhatis/128/5948128.shtml]host[/url]; <BR>var $[url=http://whatis.ctocio.com.cn/searchwhatis/199/6026199.shtml]port[/url]; <BR>var $mbox; <BR>var $list; <BR><BR>var $admin; <BR>var $pass; <BR>var $fp; <BR>var $line; <BR>var $error_msg; <BR><BR>/* <BR># <BR>#Konstruktor <BR># <BR>*/ <BR>function cyradm($IMAP_HOST="localhost", $IMAP_ADMIN="", $IMAP_PW="", $IMAP_PORT="143"){ <BR>$this->host = $IMAP_HOST; <BR>$this->port = $IMAP_PORT; <BR>$this->mbox = ""; <BR>$this->list = array(); <BR><BR>$this->admin = $IMAP_ADMIN; <BR>$this->pass = $IMAP_PW; <BR>$this->fp = 0; <BR>$this->line = ""; <BR>$this->error_msg = ""; <BR>} <BR><BR><BR>/* <BR># <BR># SOCKETLOGIN on Server via [url=http://whatis.ctocio.com.cn/searchwhatis/136/6092636.shtml]Telnet[/url]-Connection! <BR># <BR>*/ <BR>function imap_login() { <BR>$this->fp = fsockopen($this->host, $this->port, &$errno, &$errstr); <BR>$this->error_msg=$errstr; <BR>if(!$this->fp) { <BR>echo "<br>ERRORNO: ($errno) <br>ERRSTR: ($errstr)<br><hr>\n"; <BR>} else { <BR>$this->[url=http://whatis.ctocio.com.cn/searchwhatis/311/5947311.shtml]command[/url](". login \"$this->admin\" \"$this->pass\""); <BR>} <BR>return $errno; <BR>} <BR><BR><BR>/* <BR># <BR># SOCKETLOGOUT from Server via Telnet-Connection! <BR># <BR>*/ <BR>function imap_logout() { <BR>$this->command(". logout"); <BR>fclose($this->fp); <BR>} <BR><BR><BR>/* <BR># <BR># SENDING COMMAND to Server via Telnet-Connection! <BR># <BR>*/ <BR>function command($line) { <BR>/* print ("$line <br>"); */ <BR>$result = array(); <BR>$i=0; $f=0; <BR>$returntext=""; <BR>$r = fputs($this->fp,"$line\n"); <BR>while (!((strstr($returntext,". [url=http://whatis.ctocio.com.cn/searchwhatis/358/6025858.shtml]OK[/url]")||(strstr($returntext,". NO"))||(strstr($returntext,". BAD"))))) <BR>{ <BR>$returntext=$this->getline(); <BR>/* print ("$returntext <br>"); */ <BR>if ($returntext) <BR>{ <BR>if (!((strstr($returntext,". OK")||(strstr($returntext,". NO"))||(strstr($returntext,". BAD"))))) <BR>{ <BR>$result[$i]=$returntext; <BR>} <BR>$i++; <BR>} <BR>} <BR><BR>if (strstr($returntext,". BAD")||(strstr($returntext,". NO"))) <BR>{ <BR>$result[0]="$returntext"; <BR>$this->error_msg = $returntext; <BR><BR>if (( strstr($returntext,". NO Quota") )) <BR>{ <BR><BR>} <BR>else <BR>{ <BR>print "<br><hr><H1><center><blink>ERROR: </blink>UNEXPECTED IMAP-SERVER-ERROR</center></H1><hr><br> <BR><[url=http://whatis.ctocio.com.cn/searchwhatis/456/6028456.shtml]table[/url] color=red border=0 align=center cellpadding=5 callspacing=3> <BR><tr><td>SENT COMMAND: </td><td>$line</td></tr> <BR><tr><td>SERVER RETURNED:</td><td></td></tr> <BR>"; <BR>for ($i=0; $i < count($result); $i++) { <BR>print "<tr><td></td><td>$result[$i]</td></tr>"; <BR>} <BR>print "</table><hr><br><br>"; <BR>} <BR>} <BR>return $result; <BR>} <BR><BR><BR>/* <BR># <BR># READING from Server via Telnet-Connection! <BR># <BR>*/ <BR><BR>function getline() { <BR>$this->line = fgets($this->fp, 256); <BR>return $this->line; <BR>} <BR><BR>/* <BR># <BR># QUOTA Functions <BR># <BR>*/ <BR><BR>// GETTING QUOTA <BR><BR>function getquota($mb_name) { <BR>$output=$this->command(". getquota \"$mb_name\""); <BR>if (strstr($output[0],". NO")) <BR>{ <BR>$ret["used"] = "[url=http://whatis.ctocio.com.cn/searchwhatis/471/6093471.shtml]NOT[/url]-SET"; <BR>$ret["qmax"] = "NOT-SET"; <BR>} <BR>else <BR>{ <BR>$realoutput = str_replace(")", "", $output[0]); <BR>$tok_list = split(" ",$realoutput); <BR>$si_used=sizeof($tok_list)-2; <BR>$si_max=sizeof($tok_list)-1; <BR>$ret["used"] = str_replace(")","",$tok_list[$si_used]); <BR>$ret["qmax"] = $tok_list[$si_max]; <BR>} <BR>return $ret; <BR>} <BR><BR><BR>// SETTING QUOTA <BR><BR>function setmbquota($mb_name, $quota) { <BR>$this->command(". setquota \"$mb_name\" (STORAGE $quota)"); <BR>} <BR><BR><BR><BR>/* <BR># <BR># MAILBOX Functions <BR># <BR>*/ <BR><BR>function createmb($mb_name, $mb_partition="") { <BR>$this->command(". create \"$mb_name\" $mb_partition"); <BR>} <BR><BR><BR>function deletemb($mb_name) { <BR>$this->command(". setacl \"$mb_name\" $this->admin d"); <BR>$this->command(". delete \"$mb_name\""); <BR>} <BR><BR>function renamemb($mb_name, $newmbname) { <BR>$all="lrswipcda"; <BR>$this->setacl($mb_name, $this->admin,$all); <BR>$this->command(". rename \"$mb_name\" \"$newmbname\""); <BR>$this->deleteacl($newmbname, $this->admin); <BR>} <BR><BR>function renameuser($from_mb_name, $to_mb_name) { <BR>$all="lrswipcda"; $find_out=array(); $split_res=array(); $owner=""; $oldowner=""; <BR><BR>/* Anlegen und Kopieren der INBOX */ <BR>$this->createmb($to_mb_name); <BR>$this->setacl($to_mb_name, $this->admin,$all); <BR>$this->copymailsfromfolder($from_mb_name, $to_mb_name); <BR><BR>/* Quotas uebernehmen */ <BR>$quota=$this->getquota($from_mb_name); <BR>$oldquota=trim($quota["qmax"]); <BR><BR>if (strcmp($oldquota,"NOT-SET")!=0) { <BR>$this->setmbquota($to_mb_name, $oldquota); <BR>} <BR><BR>/* Den Rest Umbenennen */ <BR>$username=str_replace(".","/",$from_mb_name); <BR>$split_res=explode(".", $to_mb_name); <BR>if (strcmp($split_res[0],"user")==0) { <BR>$owner=$split_res[1]; <BR>} <BR>$split_res=explode(".", $from_mb_name); <BR>if (strcmp($split_res[0],"user")==0) { <BR>$oldowner=$split_res[1]; <BR>} <BR><BR>$find_out=$this->GetFolders($username); <BR><BR>for ($i=0; $i < count($find_out); $i++) { <BR><BR>if (strcmp($find_out[$i],$username)!=0) { <BR>$split_res=split("$username",$find_out[$i]); <BR>$split_res[1]=str_replace("/",".",$split_res[1]); <BR>$this->renamemb((str_replace("/",".",$find_out[$i])), ("$to_mb_name"."$split_res[1]")); <BR>if ($owner) { <BR>$this->setacl(("$to_mb_name"."$split_res[1]"),$owner,$all); <BR>} <BR>if ($oldowner) { <BR>$this->deleteacl(("$to_mb_name"."$split_res[1]"),$oldowner); <BR>} <BR>}; <BR>} <BR>$this->deleteacl($to_mb_name, $this->admin); <BR>$this->imap_logout(); <BR>$this->imap_login(); <BR>$this->deletemb($from_mb_name); <BR>} <BR><BR>function copymailsfromfolder($from_mb_name, $to_mb_name) { <BR>$com_ret=array(); <BR>$find_out=array(); <BR>$all="lrswipcda"; <BR>$mails=0; <BR><BR>$this->setacl($from_mb_name, $this->admin,$all); <BR>$com_ret=$this->command(". select $from_mb_name"); <BR>for ($i=0; $i < count($com_ret); $i++) { <BR>if (strstr( $com_ret[$i], "EXISTS")) <BR>{ <BR>$findout=explode(" ", $com_ret[$i]); <BR>$mails=$findout[1]; <BR>} <BR>} <BR>if ( $mails != 0 ) { <BR>$com_ret=$this->command(". copy 1:$mails $to_mb_name"); <BR>for ($i=0; $i < count($com_ret); $i++) { <BR>print "$com_ret[$i]<br>"; <BR>} <BR>} <BR>$this->deleteacl($from_mb_name, $this->admin); <BR>} <BR><BR>/* <BR># <BR># [url=http://whatis.ctocio.com.cn/searchwhatis/378/7462378.shtml]ACL[/url] Functions <BR># <BR>*/ <BR><BR>function setacl($mb_name, $user, $acl) { <BR>$this->command(". setacl \"$mb_name\" \"$user\" $acl"); <BR>} <BR><BR><BR>function deleteacl($mb_name, $user) { <BR>$result=$this->command(". deleteacl \"$mb_name\" \"$user\""); <BR>} <BR><BR><BR>function getacl($mb_name) { <BR>$aclflag=1; $tmp_pos=0; <BR>$output = $this->command(". getacl \"$mb_name\""); <BR>$output = explode(" ", $output[0]); <BR>$i=count($output)-1; <BR>while ($i>3) { <BR>if (strstr($output[$i],'"')) { <BR>$i++; <BR>} <BR><BR>if (strstr($output[$i-1],'"')) { <BR>$aclflag=1; <BR>$lauf=$i-1; <BR>$spacestring=$output[$lauf]; <BR>$tmp_pos=$i; <BR>$i=$i-2; <BR>while ($aclflag!=0) <BR>{ <BR>$spacestring=$output[$i]." ".$spacestring; <BR>if (strstr($output[$i],'"')) { $aclflag=0; } <BR>$i--; <BR>} <BR>$spacestring=str_replace("\"","",$spacestring); <BR>if ($i>2) { <BR>$ret[$spacestring] = $output[$tmp_pos]; <BR>} <BR>} <BR>else <BR>{ <BR>$ret[$output[$i-1]] = $output[$i]; <BR>$i = $i - 2; <BR>} <BR>} <BR>return $ret; <BR>} <BR><BR>/* <BR># <BR># Folder Functions <BR># <BR>*/ <BR><BR>function GetFolders($username){ <BR>$username=str_replace("/",".",$username); <BR>$output = $this->command(". list \"$username\" *"); <BR><BR>for ($i=0; $i < count($output); $i++) { <BR>$splitfolder=split("\"",$output[$i]); <BR>$output[$i]=str_replace(".","/",$splitfolder[3]); <BR>} <BR>return $output; <BR>} <BR><BR>function EGetFolders($username){ <BR>$lastfolder=split("/",$username); <BR>$position=count($lastfolder)-1; <BR>$last=$lastfolder[$position]; <BR>$username=str_replace("/",".",$username); <BR>$output = $this->command(". list \"$username\" *"); <BR><BR>for ($i=0; $i < count($output); $i++) { <BR>$splitfolder=split("\"",$output[$i]); <BR>$currentfolder=split("\.",$splitfolder[3]); <BR>$[url=http://whatis.ctocio.com.cn/searchwhatis/25/5947525.shtml]current[/url]=$currentfolder[$position]; <BR>// echo "<br>FOLDER:($) CURRENTFOLDER:($splitfolder[3]) CURRENT:($current) LAST:($last) POSITION:($position)<br>"; <BR>if (strcmp($current,$last)==0){ <BR>$newoutput[$i]=str_replace(".","/",$splitfolder[3]); <BR>} <BR>} <BR>return $newoutput; <BR>} <BR><BR><BR>/* <BR># <BR># Folder-Output Functions <BR># <BR>*/ <BR><BR>function GenerateFolderList($folder_array, $username) <BR>{ <BR>print "<table border=0 align=center>"; <BR>for ($l=0; $l<count($folder_array); $l++) <BR>{ <BR>echo "<tr><td><a href=\"acl.php?username=", <BR>urlencode($username), <BR>"&[url=http://whatis.ctocio.com.cn/searchwhatis/450/5947950.shtml]folder[/url]=", <BR>urlencode($folder_array[$l]), <BR>"\">/$folder_array[$l]</td></tr>\n"; <BR>}; <BR>print "</table>"; <BR>} <BR><BR><BR>function GetUsers($char) { <BR>$users = array(); <BR>$this->imap_login(); <BR>$output=$this->GetFolders("user.$char"); <BR>$this->imap_logout(); <BR>$j = 0; <BR>$prev = 0; <BR>for ($i=0; $i < count($output); $i++) { <BR>$username = split("/", $output[$i],-1); <BR>$this->[url=http://whatis.ctocio.com.cn/searchwhatis/168/5947668.shtml]debug[/url]("($username[1]), <BR>$users[$prev])"); <BR>if ((isset($username)) && (isset($users))) { <BR>if (strcmp($username[1], $users[$prev])) { <BR>$users[$j] = $username[1]; <BR>$j++; <BR>} <BR>} <BR>if ($j != 0) { $prev = $j - 1; } <BR>} <BR>return $users; <BR>} <BR><BR>function debug($message) { <BR>// echo "<hr>$message<br><hr>"; <BR>} <BR><BR><BR>} //KLASSEN ENDE <BR><BR>?>