PHP Operations IMAP server classes
Last Update:2017-02-28
Source: Internet
Author: User
The server I think is quite classic, helped me a lot, can achieve many PHP IMAP functions are not implemented. Previously posted in Guangzhou station, then Beijing station has not PHP version, now put a bar. :)
<?php
/*************************************************************
File:cyradm.inc.php
Author: Forget it.
Date:2000-11-01
This is a completely new implementation to the IMAP Access for
Php. It is based on a socket connection to the "server" 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>} 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><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>}
}
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 "}
}//klassen ENDE
?>