Net_Cyrus
[ class tree: Net_Cyrus ] [ index: Net_Cyrus ] [ all elements ]

Source for file Cyrus.php

Documentation is available at Cyrus.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>       |
  17. // +----------------------------------------------------------------------+
  18.  
  19.  
  20. require_once('Net/IMAP.php');
  21.  
  22. /**
  23.  * Net_Cyrus class provides an API for the administration of Cyrus IMAP servers.
  24.  * please see:
  25.  *      http://asg.web.cmu.edu/cyrus/imapd/
  26.  * @author  Damian Alejandro Fernandez Sosa <damlists@cnba.uba.ar>
  27.  * @package Net_Cyrus
  28.  */
  29.  
  30. class Net_Cyrus extends Net_IMAP
  31. {
  32.  
  33.     /**
  34.      * Hostname of server
  35.      * @var string 
  36.      */
  37.     var $_host;
  38.  
  39.     /**
  40.      * Port number of server
  41.      * @var string 
  42.      */
  43.     var $_port;
  44.  
  45.     /**
  46.      * Username used to connect
  47.      * @var string 
  48.      */
  49.     var $_user;
  50.  
  51.     /**
  52.      * Password used to connect
  53.      * @var string 
  54.      */
  55.     var $_pass;
  56.  
  57.     /**
  58.      * Timeout for socket connect
  59.      * @var integer 
  60.      */
  61.     var $_timeout;
  62.  
  63.     /**
  64.      * Constructor.
  65.      *
  66.      * @param string  $user     Cyrus admin username
  67.      * @param string  $pass     Cyrus admin password
  68.      * @param string  $host     Server hostname
  69.      * @param integer $port     Server port number
  70.      * @param integer $timeout  Connection timeout value
  71.      */
  72.     function Net_Cyrus($user = null $pass = null $host 'localhost'$port = 143$timeout = 5)
  73.     {
  74.         $this->_user      $user;
  75.         $this->_pass      $pass;
  76.         $this->_host      $host;
  77.         $this->_port      $port;
  78.         $this->_timeout   $timeout;
  79.     }
  80.  
  81.     /**
  82.      * Connects and logs into the server. Uses the Auth_SASL
  83.      * library to produce the LOGIN command if available
  84.      *
  85.      * @access public
  86.      */
  87.     function connect($user = null$pass = null$host = null $port = null$method=true)
  88.     {
  89.         $this->Net_IMAPProtocol();
  90.         // Backward compatibility hack Horde's Net_Cyrus don't allow params in connect()
  91.         if( ($user === null&& ($pass === null&& ($host === null&& ($port === null) ){
  92.             if (PEAR::isError($ret = parent::connect($this->_host$this->_port) )){
  93.                 return $ret;
  94.             }
  95.             if(PEAR::isError($ret $this->login($this->_user$this->_passtruefalse ))) {
  96.                 return $ret;
  97.             }
  98.         }else{
  99.             if (PEAR::isError($ret = parent::connect($host$port) )){
  100.                 return $ret;
  101.             }
  102.             if(PEAR::isError($ret $this->login($user$pass$methodfalse ))) {
  103.                 return $ret;
  104.             }
  105.         }
  106.         return true;
  107.     }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.     /**
  114.     * Handles the errors the class can find
  115.     * on the server
  116.     *
  117.     * @access private
  118.     * @return PEAR_Error 
  119.     */
  120.  
  121.     function _raiseError($msg$code)
  122.     {
  123.     include_once 'PEAR.php';
  124.     return PEAR::raiseError($msg$code);
  125.     }
  126.  
  127.  
  128.  
  129.     /**
  130.      * Ends the session. Issues the LOGOUT command first.
  131.      * @access public
  132.      */
  133.     function disconnect()
  134.     {
  135.         return parent::disconnect(false);
  136.  
  137.     }
  138.  
  139.  
  140.     /**
  141.      * Sets admin privileges on a folder/mailbox.
  142.      * useful because by default cyrus don't add delete permission to the admin user
  143.      *
  144.      * @param string $mailbox  Mailbox
  145.      *
  146.      * @return string  Previous permissions for admin user on this mailbox.
  147.      *
  148.      * @access private
  149.      */
  150.     function _setAdminPriv($mailbox)
  151.     {
  152.         $oldPrivs $this->getACL($mailbox$this->_user);
  153.         $this->setACL($mailbox$this->_user'lrswipcda');
  154.         return $oldPrivs;
  155.     }
  156.  
  157.     /**
  158.      * Removes admin privileges on a folder/mailbox
  159.      * after the above function has been used. If the
  160.      * ACLs passed in is null, then the privs are deleted.
  161.      *
  162.      * @param string $mailbox  Mailbox
  163.      * @param string $privs    Previous privileges as returned
  164.      *                          by the _setAdminPriv() method
  165.      *
  166.      * @access private
  167.      */
  168.     function _resetAdminPriv($mailbox$privs = null)
  169.     {
  170.         if (is_null($privs)) {
  171.             $this->deleteACL($mailbox$this->_user);
  172.         else {
  173.             $this->setACL($mailbox$this->_user$privs);
  174.         }
  175.     }
  176.  
  177.     /**
  178.      * Returns quota details.
  179.      *
  180.      * @param string $mailbox  Mailbox to get quota info for.
  181.      *
  182.      * @return mixed  Array of current usage and quota limit or
  183.      *                 false on failure.
  184.      * @access public
  185.      */
  186.     function getQuota($mailbox)
  187.     {
  188.         if(PEAR::isError($ret=$this->getStorageQuota($mailbox))){
  189.             return $ret;
  190.         }
  191.         return array(0=>$ret['USED']1=>$ret['QMAX']);
  192.  
  193.     }
  194.  
  195.     /**
  196.      * Sets a quota.
  197.      *
  198.      * @param string $mailbox  Mailbox to get quota info for
  199.      * @param integer $quota   The quota to set
  200.      *
  201.      * @return mixed  True on success, PEAR_Error otherwise
  202.      */
  203.     function setQuota($mailbox$quota)
  204.     {
  205.         return $this->setStorageQuota($mailbox$quota);
  206.     }
  207.  
  208.     /**
  209.      * Copies a quota from one mailbox to another.
  210.      *
  211.      * @param string $from  Mailbox to copy quota from
  212.      * @param string $to    Mailbox to set quota on
  213.      * @access public
  214.      */
  215.     function copyQuota($from$to)
  216.     {
  217.         $currentQuota $this->getQuota($from);
  218.         $oldQuotaMax trim($currentQuota[1]);
  219.         if ($oldQuotaMax != 'NOT-SET'{
  220.             $this->setQuota($to$oldQuotaMax);
  221.         }
  222.     }
  223.  
  224.  
  225.  
  226.  
  227.     /**
  228.      * Retrieves details of current ACL.
  229.      *
  230.      * @param string $mailbox  Name of mailbox
  231.      * @param  string $user    Optional user to get ACL for
  232.      *
  233.      * @return string  Access stuff
  234.      * @access public
  235.      */
  236.     function getACL($mailbox$user = null)
  237.     {
  238.  
  239.         $acl=parent::getACL($mailbox);
  240.         $acl_arr=array();
  241.         if(is_array($acl)){
  242.             foreach($acl as $a){
  243.                 if$user === null ){
  244.                     $acl_arr[$a['USER']]=$a['RIGHTS'];
  245.                 }else{
  246.                     if$user == $a['USER'){
  247.                         return $a['RIGHTS'];
  248.                     }
  249.                 }
  250.             }
  251.             return $acl_arr;
  252.         }else{
  253.             return $acl;
  254.         }
  255.  
  256.     }
  257.  
  258.  
  259.  
  260.     /**
  261.      * Sets ACL on a mailbox.
  262.      *
  263.      * @param string $mailbox  Name of mailbox
  264.      * @param string $user     Username to apply ACL to
  265.      * @param string $acl      The ACL
  266.      *
  267.      * @return mixed  True on success, PEAR_Error otherwise
  268.      * @access public
  269.      */
  270.     function setACL($mailbox$user$acl)
  271.     {
  272.         return parent::setACL($mailbox$user$acl);
  273.     }
  274.  
  275.  
  276.  
  277.  
  278.     /**
  279.      * Deletes ACL from a mailbox.
  280.      *
  281.      * @param string $mailbox  Name of mailbox
  282.      * @param string $user     Username to remove ACL from
  283.      *
  284.      *
  285.      * @return mixed  True on success, PEAR_Error otherwise
  286.      * @access public
  287.      */
  288.     function deleteACL($mailbox$user)
  289.     {
  290.         return parent::deleteACL($mailbox$user);
  291.     }
  292.  
  293.  
  294.  
  295.     /**
  296.      * Creates a mailbox.
  297.      *
  298.      * @param string $mailbox  Name of mailbox to create
  299.      *
  300.      *
  301.      * @return mixed  True on success, PEAR error otherwise
  302.      * @access public
  303.      */
  304.     function createMailbox($mailbox)
  305.     {
  306.         $res= parent::createMailbox($mailbox);
  307.         return $res;
  308.     }
  309.  
  310.     /**
  311.      * Renames a mailbox.
  312.      *
  313.      * @param string $mailbox  Name of mailbox to rename
  314.      * @param string $newname  New name of mailbox
  315.      *
  316.      * @return mixed  True on success, PEAR error otherwise
  317.      * @access public
  318.      */
  319.     function renameMailbox($mailbox$newname)
  320.     {
  321.         $oldPrivs $this->_setAdminPriv($mailbox);
  322.         ifPEAR::isError$response = parent::renameMailbox($mailbox$newname) )){
  323.             return $response;
  324.         }
  325.         $this->_resetAdminPriv($mailbox$oldPrivs);
  326.         return true;
  327.     }
  328.  
  329.  
  330.  
  331.     /**
  332.      * Deletes a mailbox.
  333.      *
  334.      * @param string $mailbox  Name of mailbox to delete
  335.      *
  336.      * @return mixed  True on success, PEAR error otherwise
  337.      * @access public
  338.      */
  339.     function deleteMailbox($mailbox)
  340.     {
  341.         $oldPrivs $this->_setAdminPriv($mailbox);
  342.         ifPEAR::isError$response = parent::deleteMailbox($mailbox) )){
  343.             return $response;
  344.         }
  345.         $this->_resetAdminPriv($mailbox$oldPrivs);
  346.         return true;
  347.     }
  348.  
  349.     /**
  350.      * Returns a list of folders for a particular user.
  351.      *
  352.      * @param string $prepend  Optional string to prepend
  353.      *
  354.      * @return array  Array of folders matched
  355.      * @access public
  356.      */
  357.     function getFolderList($folderMask = null )
  358.     {
  359.         if$folderMask === null ){
  360.             $folderMask 'user' $this->getHierarchyDelimiter'*' ;
  361.         }
  362.         //echo "FOLDERLIST: $folderMask\n";
  363.         return $this->getMailboxes(''  $folderMask false );
  364.     }
  365.  
  366.  
  367.     /**
  368.      * Returns a list of users.
  369.      *
  370.      * @return array  Array of users found
  371.      * @access public
  372.      */
  373.     function getUserList()
  374.     {
  375.  
  376.         $hierarchyDelimiter$this->getHierarchyDelimiter();
  377.         $user_base='user' $hierarchyDelimiter '%' ;
  378.         if(PEAR::isError$user_list $this->getFolderList($user_base) ) ){
  379.             return $user_list;
  380.         }
  381.         $users = array();
  382.         foreach ($user_list as $user{
  383.             $user_arr=explode($hierarchyDelimiter$user);
  384.             $users[]=$user_arr[1];
  385.         }
  386.         return $users;
  387.     }
  388.  
  389.  
  390.  
  391.     /**
  392.     * Parses a user name
  393.     *
  394.     * @param string $user_name  the user parse
  395.     * @param boolean $append_userPart  true if the method appends 'user.' to the user name
  396.     * @access public
  397.     * @since  1.0
  398.     */
  399.     function getUserName($user_name$append_userPart = true)
  400.     {
  401.          if(strtolower(substr($user_name,0,5)) == 'user.'){
  402.             $user_arr=explode('user.',$user_name);
  403.             $user_name=$user_arr[1];
  404.         }
  405.         if($append_userPart){
  406.             return 'user' $this->getHierarchyDelimiter($user_name;
  407.         }else{
  408.             return $user_name;
  409.         }
  410.  
  411.     }
  412.  
  413.  
  414.  
  415.  
  416.  
  417.     /**
  418.     * deletes a user. Use this instead of deleteMailbox
  419.     *
  420.     * @param string $user_name  the user to deletes
  421.     *
  422.     * @return mixed true on Success/PearError on Failure
  423.     * @access public
  424.     */
  425.     function deleteUser($user_name)
  426.     {
  427.         $user_name=$this->getUserName($user_name);
  428.         return $this->deleteMailbox($user_name);
  429.     }
  430.  
  431.  
  432.  
  433.  
  434.     /**
  435.     * creates a user. Use this instead of createMailbox
  436.     *
  437.     * @param string $user_name  the user to create
  438.     *
  439.     * @return mixed true on Success/PearError on Failure
  440.     * @access public
  441.     */
  442.     function createUser($user_name)
  443.     {
  444.         if$this->userExists($user_name) ){
  445.             return $this->_raiseError("The user $user_name already exists503);
  446.         }
  447.         $user_name=$this->getUserName($user_name);
  448.         return $this->createMailbox($user_name);
  449.     }
  450.  
  451.  
  452.  
  453.    /**
  454.     * check if the user name exists
  455.     *
  456.     * @param string $mailbox     user name to check existance
  457.     *
  458.     * @return boolean true on Success/false on Failure
  459.     * @since 1.0
  460.     */
  461.     function userExists($user_name)
  462.     {
  463.         $user_name $this->getUserName($user_name);
  464.         return $this->mailboxExist($user_name);
  465.     }
  466.  
  467.  
  468.     /**
  469.      * Renames a user. This is here since the RENAME command
  470.      * is not allowed on a user's INBOX (ie. the user.<username>
  471.      * mailbox). Supplied args can be either with or without
  472.      * the "user." at the beginning.
  473.      *
  474.      * @param string $oldUser  Name of user to rename
  475.      * @param string $newUser  New name of user
  476.      *
  477.      * @return mixed true on Success/PearError on Failure
  478.      * @access public
  479.      */
  480.     function renameUser($oldUser$newUser)
  481.     {
  482.  
  483.         $oldUser =$this->getUserName($oldUser);
  484.  
  485.         $newUser =$this->getUserName($newUser);
  486.  
  487.  
  488.         $oldUsername $this->getUserName($oldUser,false);
  489.         $newUsername $this->getUserName($newUser,false);
  490.  
  491.         // Check new user doesn't already exist and old user exists
  492.         if (!$this->userExists($oldUsername) ) {
  493.             $msg=sprintf('The user "%s" doesn\'t exist'$oldUsername);
  494.             $code=502;
  495.             return $this->_raiseError($msg$code);
  496.  
  497.  
  498.         }
  499.         if ($this->userExists($newUsername) ) {
  500.             $msg=sprintf('the user "%s" already exists. choose another user name'$newUsername);
  501.             $code=503;
  502.             return $this->_raiseError($msg$code);
  503.         }
  504.  
  505.         // Create the new mailbox
  506.         $this->createMailbox($newUser);
  507.         $oldAdminPrivs $this->_setAdminPriv($newUser);
  508.  
  509.         // Copy Mail and quotas
  510.         $this->copyMail($oldUser$newUser);
  511.         $this->copyQuota($oldUser$newUser);
  512.  
  513.         // Copy the folders
  514.         $folderList $this->getFolderList($oldUser $this->getHierarchyDelimiter('*');
  515.  
  516.         if (!empty($folderList)) {
  517.             foreach ($folderList as $folder{
  518.                 $newFolderName str_replace($oldUser$newUser$folder);
  519.                 $this->renameMailbox($folder$newFolderName);
  520.                 $this->setACL($newFolderName$newUsername'lrswipcd');
  521.                 $this->deleteACL($newFolderName$oldUsername);
  522.             }
  523.         }
  524.         $this->_resetAdminPriv($newUser$oldAdminPrivs);
  525.         $this->deleteMailbox($oldUser);
  526.     }
  527.  
  528.  
  529.  
  530.  
  531.     /**
  532.      * Copies mail from one folder to another.
  533.      *
  534.      * @param string $from  From mailbox name
  535.      * @param string $to    To mailbox name
  536.      *
  537.      * @return mixed true on Success/PearError on Failure
  538.      * @access public
  539.      */
  540.     function copyMail($from$to)
  541.     {
  542.         $oldFromPrivs $this->_setAdminPriv($from);
  543.         $oldToPrivs   $this->_setAdminPriv($to);
  544.  
  545.         $this->selectMailbox($from);
  546.         $this->copyMessages($to);
  547.  
  548.         $this->_resetAdminPriv($from$oldFromPrivs);
  549.         $this->_resetAdminPriv($to$oldToPrivs);
  550.     }
  551.  
  552. }

Documentation generated on Mon, 11 Mar 2019 13:55:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.