PHP class for IMAP server operations

Source: Internet
Author: User
Tags imap
I think the class for PHP to operate IMAP servers is quite classic. it has helped me a lot and can implement functions that many PHP IMAP functions cannot implement. I posted a post on the Guangzhou site before. at that time, there was no PHP version on the Beijing site. now I think it is quite classic and it has helped me a lot, many php imap functions cannot be implemented. I posted a post on the Guangzhou site before. at that time, there was no PHP version on the Beijing site. now I want to post another one. :)

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

File: cyradm. inc. php
Author: forget it.
Date: 2000-11-01

This is a completely new implementation of the IMAP Access
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"
ERRORNO: ($ errno)
ERRSTR: ($ errstr)
\ 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
");*/
$ 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
");*/
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"
ERROR: UNEXPECTED IMAP-SERVER-ERROR








";For ($ I = 0; $ I <count ($ result); $ I ++ ){Print" ";}Print"
Sent command: $ Line
Server returned:
$ Result [$ I]


";
}
}
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]
";
}
}
$ 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"
FOLDER :( $) CURRENTFOLDER :( $ splitfolder [3]) CURRENT :( $ current) LAST :( $ last) POSITION :( $ position)
";
If (strcmp ($ current, $ last) = 0 ){
$ Newoutput [$ I] = str_replace (".", "/", $ splitfolder [3]);
}
}
Return $ newoutput;
}


/*
#
# Folder-Output Functions
#
*/

Function GenerateFolderList ($ folder_array, $ username)
{
Print"



";For ($ l = 0; $ l{Echo" \ N ";};Print"
Urlencode ($ username ),
"& Folder = ",
Urlencode ($ folder_array [$ l]),
"\">/$ Folder_array [$ l]
";
}


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 "$ message
";
}


} // KLASSEN ENDE

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.