PHP操作IMAP服务器的类

服务器

我认为是相当经典的,帮过我大忙,可以实现很多PHP的IMAP函数不能实现的功能。以前在广州站贴过,那时北京站还没PHP版,现在再贴一个吧。:)

<?php
/*************************************************************

File: cyradm.inc.php
Author: 忘了,嘻嘻
Date: 2000-11-01

This is a completely new implementation of the IMAP Access for
PHP. It is based on a socket connection to the server an is
independent from the imap-Functions of PHP

***************************************************************/

class cyradm {

var $host;
var $port;
var $mbox;
var $list;

var $admin;
var $pass;
var $fp;
var $line;
var $error_msg;

/*
#
#Konstruktor
#
*/
function cyradm($IMAP_HOST="localhost", $IMAP_ADMIN="", $IMAP_PW="", $IMAP_PORT="143"){
$this->host = $IMAP_HOST;
$this->port = $IMAP_PORT;
$this->mbox = "";
$this->list = array();

$this->admin = $IMAP_ADMIN;
$this->pass = $IMAP_PW;
$this->fp = 0;
$this->line = "";
$this->error_msg = "";
}

/*
#
# SOCKETLOGIN on Server via Telnet-Connection!
#
*/
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\"");
}
return $errno;
}

/*
#
# SOCKETLOGOUT from Server via Telnet-Connection!
#
*/
function imap_logout() {
$this->command(". logout");
fclose($this->fp);
}

/*
#
# SENDING COMMAND to Server via Telnet-Connection!
#
*/
function command($line) {
/* print ("$line <br>"); */
$result = array();
$i=0; $f=0;
$returntext="";
$r = fputs($this->fp,"$line\n");
while (!((strstr($returntext,". OK")||(strstr($returntext,". NO"))||(strstr($returntext,". BAD")))))
{
$returntext=$this->getline();
/* print ("$returntext <br>"); */
if ($returntext)
{
if (!((strstr($returntext,". OK")||(strstr($returntext,". NO"))||(strstr($returntext,". BAD")))))
{
$result[$i]=$returntext;
}
$i++;
}
}

if (strstr($returntext,". BAD")||(strstr($returntext,". NO")))
{
$result[0]="$returntext";
$this->error_msg = $returntext;

if (( strstr($returntext,". NO Quota") ))
{

}
else
{
print "<br><hr><H1><center><blink>ERROR: </blink>UNEXPECTED IMAP-SERVER-ERROR</center></H1><hr><br>
<table color=red border=0 align=center cellpadding=5 callspacing=3>
<tr><td>SENT COMMAND: </td><td>$line</td></tr>
<tr><td>SERVER RETURNED:</td><td></td></tr>
";
for ($i=0; $i < count($result); $i++) {
print "<tr><td></td><td>$result[$i]</td></tr>";
}
print "</table><hr><br><br>";
}
}
return $result;
}

/*
#
# READING from Server via Telnet-Connection!
#
*/

function getline() {
$this->line = fgets($this->fp, 256);
return $this->line;
}

/*
#
# QUOTA Functions
#
*/

// GETTING QUOTA

function getquota($mb_name) {
$output=$this->command(". getquota \"$mb_name\"");
if (strstr($output[0],". NO"))
{
$ret["used"] = "NOT-SET";
$ret["qmax"] = "NOT-SET";
}
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];
}
return $ret;
}

// SETTING QUOTA

function setmbquota($mb_name, $quota) {
$this->command(". setquota \"$mb_name\" (STORAGE $quota)");
}

/*
#
# MAILBOX Functions
#
*/

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->command(". delete \"$mb_name\"");
}

function renamemb($mb_name, $newmbname) {
$all="lrswipcda";
$this->setacl($mb_name, $this->admin,$all);
$this->command(". rename \"$mb_name\" \"$newmbname\"");
$this->deleteacl($newmbname, $this->admin);
}

function renameuser($from_mb_name, $to_mb_name) {
$all="lrswipcda"; $find_out=array(); $split_res=array(); $owner=""; $oldowner="";

/* Anlegen und Kopieren der INBOX */
$this->createmb($to_mb_name);
$this->setacl($to_mb_name, $this->admin,$all);
$this->copymailsfromfolder($from_mb_name, $to_mb_name);

/* Quotas 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],"user")==0) {
$owner=$split_res[1];
}
$split_res=explode(".", $from_mb_name);
if (strcmp($split_res[0],"user")==0) {
$oldowner=$split_res[1];
}

$find_out=$this->GetFolders($username);

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);
}

function copymailsfromfolder($from_mb_name, $to_mb_name) {
$com_ret=array();
$find_out=array();
$all="lrswipcda";
$mails=0;

$this->setacl($from_mb_name, $this->admin,$all);
$com_ret=$this->command(". select $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 Functions
#
*/

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\"");
}

function getacl($mb_name) {
$aclflag=1; $tmp_pos=0;
$output = $this->command(". getacl \"$mb_name\"");
$output = explode(" ", $output[0]);
$i=count($output)-1;
while ($i>3) {
if (strstr($output[$i],'"')) {
$i++;
}

if (strstr($output[$i-1],'"')) {
$aclflag=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;
}
}
return $ret;
}

/*
#
# Folder Functions
#
*/

function GetFolders($username){
$username=str_replace("/",".",$username);
$output = $this->command(". list \"$username\" *");

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 < count($output); $i++) {
$splitfolder=split("\"",$output[$i]);
$currentfolder=split("\.",$splitfolder[3]);
$current=$currentfolder[$position];
// echo "<br>FOLDER:($) CURRENTFOLDER:($splitfolder[3]) CURRENT:($current) LAST:($last) POSITION:($position)<br>";
if (strcmp($current,$last)==0){
$newoutput[$i]=str_replace(".","/",$splitfolder[3]);
}
}
return $newoutput;
}

/*
#
# Folder-Output Functions
#
*/

function GenerateFolderList($folder_array, $username)
{
print "<table border=0 align=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>";
}

function 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

?> 

时间: 2024-09-17 04:30:48

PHP操作IMAP服务器的类的相关文章

thrift的TServer类体系原理及源码详解:服务器基类TServer

这一部分主要实现了底层IO通信,还涉及到通信服务器的堵塞.非堵塞.单线程.多线程 等运行模式,所以实现比较复杂.这一部分涉及到的类的关系图如下: 由上面的 类关系图可以看出,这一部分的类关系比较复杂,复杂的不是继承关系,而是相互之间的依 赖关系.因为服务器需要处理很多的任务,也需要处理多个客户端的连接,这就涉及到多线 程编程以及多线程之间通信及并发的情况.这一部分涉及到的并发编程的类容将在后面章节 单独分析,本章主要介绍服务器模型和IO通信的具体细节. 第一节 服务器基类 TServer 所有具

使用C#.NET的不能操作linux服务器下的ftp,这是为什么?

问题描述 使用C#.NET的不能操作linux服务器下的ftp,这是为什么? 我在windows7电脑上分别使用IIS和Quick easy ftp server软件搭建了ftp环境,用C#写了ftp上传的程序,程序能正常运行并上传文件.但是同样的代码连接linux下的ftp服务器,却无法上传成功,提示创建目录失败,(使用其他flashFxp软件和windows默认的ftp登录工具是可以正常操作的),这个是我程序代码问题,还是windows和linux的差异有关,或者是因为服务器的设置问题.具体

sqlserver-尝试写了一个 JDBC 操作 SQLServer 的工具类,但是好像有问题,求大神帮忙指正

问题描述 尝试写了一个 JDBC 操作 SQLServer 的工具类,但是好像有问题,求大神帮忙指正 java新人,尝试写了一个 JDBC 操作 SQLServer 的工具类,但是好像有问题,调用 insert 指令没问题,而 select 指令出现如下报错: com.microsoft.sqlserver.jdbc.SQLServerException: ')' 附近有语法错误. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFr

oracle操作不同服务器上的sql server2000

问题描述 oracle操作不同服务器上的sql server2000 我使用透明网关把oracle连接sql server2000,在oracle上做了个触发器绑定到一张表A,表A如果insert或者update将数据同步到sql server2000上,结果insert成功了,update部分语句一直编译不通过,语法有问题!下面图中update中的code与status一直说标示符无效,语法不知道怎么写?(有没有做过类似的数据库问题帮忙下,谢谢) 解决方案 ..额!后来想起来,要加双引号,才能

有关更改项目的服务器地址类的问题大神帮帮忙

问题描述 有关更改项目的服务器地址类的问题大神帮帮忙 老板让我更改一下服务器地址,还有登录账号密码等, 刚接手项目,注释不全,怎么快速的查找这些在哪里更改啊.求助 解决方案 用ultraedit等工具对整个源码目录全文搜索

php入门教程六(php实现连接ftp服务器与IMAP服务器)

用php教程实现连接服务器,还有实现连接imap服务器,最后通过php编写的代码来实现上传和下载文件  代码如下 复制代码 <?php //连接 imap 服务器链接,imap 的端口为 143. $mbox = imap_open("{localhost:143}inbox","user_id","password"); //连接pop3 服务器链接,pop3 的端口为 110. $mbox = imap_open("{loca

源代码-在eclipse连接TFS后,在哪里新建文件,修改一个文件是本地的,哪里操作是服务器的

问题描述 在eclipse连接TFS后,在哪里新建文件,修改一个文件是本地的,哪里操作是服务器的 在团队资源管理器的源代码资源管理器上操作的是属于本地吗还是在项目的资源管理器上操作

模拟Ping操作的一个Java类_java

本文为大家分享了模拟Ping操作的一个Java类,具体内容如下 import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; /** * Created by QiuJU * on 2014/9/21. */ public class SimplePing implements Runnable { private final Object mEndLock = new

php简单操作mysql数据库的类_php技巧

本文实例讲述了php简单操作mysql数据库的类.分享给大家供大家参考.具体如下: <?php /** * Database class * * @version: 2.2 * @revised: 27 may 2007 * **/ class Database { var $host; var $name; var $user; var $pass; var $prefix; var $linkId; function Database($mysql) { foreach($mysql as