Source for file Cyrus.php
Documentation is available at Cyrus.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar> |
// +----------------------------------------------------------------------+
require_once('Net/IMAP.php');
* Net_Cyrus class provides an API for the administration of Cyrus IMAP servers.
* http://asg.web.cmu.edu/cyrus/imapd/
* @author Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
* Username used to connect
* Password used to connect
* Timeout for socket connect
* @param string $user Cyrus admin username
* @param string $pass Cyrus admin password
* @param string $host Server hostname
* @param integer $port Server port number
* @param integer $timeout Connection timeout value
function Net_Cyrus($user = null , $pass = null , $host = 'localhost', $port = 143 , $timeout = 5 )
$this->_timeout = $timeout;
* Connects and logs into the server. Uses the Auth_SASL
* library to produce the LOGIN command if available
function connect($user = null , $pass = null , $host = null , $port = null , $method=true )
$this->Net_IMAPProtocol ();
// Backward compatibility hack Horde's Net_Cyrus don't allow params in connect()
if( ($user === null ) && ($pass === null ) && ($host === null ) && ($port === null ) ){
if (PEAR ::isError ($ret = parent ::connect ($this->_host, $this->_port) )){
if(PEAR ::isError ($ret = $this->login ($this->_user, $this->_pass, true , false ))) {
// Save this information if we aren't in the hack case so that any other
// calls to the internal functions still work.
if (PEAR ::isError ($ret = parent ::connect ($host, $port) )){
if(PEAR ::isError ($ret = $this->login ($user, $pass, $method, false ))) {
* Handles the errors the class can find
function _raiseError ($msg, $code)
return PEAR ::raiseError ($msg, $code);
* Ends the session. Issues the LOGOUT command first.
return parent ::disconnect (false );
* Sets admin privileges on a folder/mailbox.
* useful because by default cyrus don't add delete permission to the admin user
* @param string $mailbox Mailbox
* @return string Previous permissions for admin user on this mailbox.
function _setAdminPriv ($mailbox)
$oldPrivs = $this->getACL($mailbox, $this->_user);
$this->setACL($mailbox, $this->_user, 'lrswipcda');
* Removes admin privileges on a folder/mailbox
* after the above function has been used. If the
* ACLs passed in is null, then the privs are deleted.
* @param string $mailbox Mailbox
* @param string $privs Previous privileges as returned
* by the _setAdminPriv() method
function _resetAdminPriv ($mailbox, $privs = null )
$this->setACL($mailbox, $this->_user, $privs);
* @param string $mailbox Mailbox to get quota info for.
* @return mixed Array of current usage and quota limit or
if(PEAR ::isError ($ret= $this->getStorageQuota ($mailbox))){
return array (0=> $ret['USED'], 1=> $ret['QMAX']);
* @param string $mailbox Mailbox to get quota info for
* @param integer $quota The quota to set
* @return mixed True on success, PEAR_Error otherwise
return $this->setStorageQuota ($mailbox, $quota);
* Copies a quota from one mailbox to another.
* @param string $from Mailbox to copy quota from
* @param string $to Mailbox to set quota on
$oldQuotaMax = trim($currentQuota[1 ]);
if ($oldQuotaMax != 'NOT-SET') {
* Retrieves details of current ACL.
* @param string $mailbox Name of mailbox
* @param string $user Optional user to get ACL for
* @return string Access stuff
function getACL($mailbox, $user = null )
$acl=parent ::getACL ($mailbox);
$acl_arr[$a['USER']]= $a['RIGHTS'];
if( $user == $a['USER'] ){
* @param string $mailbox Name of mailbox
* @param string $user Username to apply ACL to
* @param string $acl The ACL
* @return mixed True on success, PEAR_Error otherwise
function setACL($mailbox, $user, $acl)
return parent ::setACL ($mailbox, $user, $acl);
* Deletes ACL from a mailbox.
* @param string $mailbox Name of mailbox
* @param string $user Username to remove ACL from
* @return mixed True on success, PEAR_Error otherwise
return parent ::deleteACL ($mailbox, $user);
* @param string $mailbox Name of mailbox to create
* @param array $options options to pass to create
* @return mixed True on success, PEAR error otherwise
$res = parent ::createMailbox ($mailbox, $options);
* @param string $mailbox Name of mailbox to rename
* @param string $newname New name of mailbox
* @param array $options options to pass to rename
* @return mixed True on success, PEAR error otherwise
$oldPrivs = $this->_setAdminPriv ($mailbox);
if( PEAR ::isError ( $response = parent ::renameMailbox ($mailbox, $newname, $options) )){
$this->_resetAdminPriv ($mailbox, $oldPrivs);
* @param string $mailbox Name of mailbox to delete
* @return mixed True on success, PEAR error otherwise
$oldPrivs = $this->_setAdminPriv ($mailbox);
if( PEAR ::isError ( $response = parent ::deleteMailbox ($mailbox) )){
$this->_resetAdminPriv ($mailbox, $oldPrivs);
* Returns a list of folders for a particular user.
* @param string $prepend Optional string to prepend
* @return array Array of folders matched
if( $folderMask === null ){
$folderMask = 'user' . $this->getHierarchyDelimiter ( ) . '*' ;
//echo "FOLDERLIST: $folderMask\n";
return $this->getMailboxes ('' , $folderMask , false );
* Returns a list of users.
* @return array Array of users found
$hierarchyDelimiter= $this->getHierarchyDelimiter ();
$user_base= 'user' . $hierarchyDelimiter . '%' ;
if(PEAR ::isError ( $user_list = $this->getFolderList($user_base) ) ){
foreach ($user_list as $user) {
$user_arr= explode($hierarchyDelimiter, $user);
* @param string $user_name the user parse
* @param boolean $append_userPart true if the method appends 'user.' to the user name
function getUserName($user_name, $append_userPart = true )
$user_arr= explode('user.',$user_name);
return 'user' . $this->getHierarchyDelimiter () . $user_name;
* deletes a user. Use this instead of deleteMailbox
* @param string $user_name the user to deletes
* @return mixed true on Success/PearError on Failure
* creates a user. Use this instead of createMailbox
* @param string $user_name the user to create
* @param array $options options to pass to createMailbox
* @return mixed true on Success/PearError on Failure
return $this->_raiseError (" The user $user_name already exists" , 503 );
* check if the user name exists
* @param string $mailbox user name to check existance
* @return boolean true on Success/false on Failure
return $this->mailboxExist ($user_name);
* Renames a user. This is here since the RENAME command
* is not allowed on a user's INBOX (ie. the user.<username>
* mailbox). Supplied args can be either with or without
* the "user." at the beginning.
* @param string $oldUser Name of user to rename
* @param string $newUser New name of user
* @param array $options options to pass to createMailbox and renameMailbox
* @return mixed true on Success/PearError on Failure
function renameUser($oldUser, $newUser, $options = null )
// Check new user doesn't already exist and old user exists
$msg= sprintf('The user "%s" doesn\'t exist', $oldUsername);
return $this->_raiseError ($msg, $code);
$msg= sprintf('the user "%s" already exists. choose another user name', $newUsername);
return $this->_raiseError ($msg, $code);
// Create the new mailbox
$oldAdminPrivs = $this->_setAdminPriv ($newUser);
$folderList = $this->getFolderList($oldUser . $this->getHierarchyDelimiter () . '*');
if (!empty ($folderList)) {
foreach ($folderList as $folder) {
$newFolderName = str_replace($oldUser, $newUser, $folder);
$this->setACL($newFolderName, $newUsername, 'lrswipcd');
$this->deleteACL($newFolderName, $oldUsername);
$this->_resetAdminPriv ($newUser, $oldAdminPrivs);
* Copies mail from one folder to another.
* @param string $from From mailbox name
* @param string $to To mailbox name
* @return mixed true on Success/PearError on Failure
$oldFromPrivs = $this->_setAdminPriv ($from);
$oldToPrivs = $this->_setAdminPriv ($to);
$this->selectMailbox ($from);
$this->copyMessages ($to);
$this->_resetAdminPriv ($from, $oldFromPrivs);
$this->_resetAdminPriv ($to, $oldToPrivs);
Documentation generated on Mon, 11 Mar 2019 15:39:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|