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.             // Save this information if we aren't in the hack case so that any other
  100.             // calls to the internal functions still work.
  101.             $this->_user $user;
  102.             $this->_pass $pass;
  103.             $this->_host $host;
  104.             $this->_port $port;
  105.             if (PEAR::isError($ret = parent::connect($host$port) )){
  106.                 return $ret;
  107.             }
  108.             if(PEAR::isError($ret $this->login($user$pass$methodfalse ))) {
  109.                 return $ret;
  110.             }
  111.         }
  112.         return true;
  113.     }
  114.  
  115.  
  116.  
  117.  
  118.  
  119.     /**
  120.     * Handles the errors the class can find
  121.     * on the server
  122.     *
  123.     * @access private
  124.     * @return PEAR_Error 
  125.     */
  126.  
  127.     function _raiseError($msg$code)
  128.     {
  129.     include_once 'PEAR.php';
  130.     return PEAR::raiseError($msg$code);
  131.     }
  132.  
  133.  
  134.  
  135.     /**
  136.      * Ends the session. Issues the LOGOUT command first.
  137.      * @access public
  138.      */
  139.     function disconnect()
  140.     {
  141.         return parent::disconnect(false);
  142.  
  143.     }
  144.  
  145.  
  146.     /**
  147.      * Sets admin privileges on a folder/mailbox.
  148.      * useful because by default cyrus don't add delete permission to the admin user
  149.      *
  150.      * @param string $mailbox  Mailbox
  151.      *
  152.      * @return string  Previous permissions for admin user on this mailbox.
  153.      *
  154.      * @access private
  155.      */
  156.     function _setAdminPriv($mailbox)
  157.     {
  158.         $oldPrivs $this->getACL($mailbox$this->_user);
  159.         $this->setACL($mailbox$this->_user'lrswipcda');
  160.         return $oldPrivs;
  161.     }
  162.  
  163.     /**
  164.      * Removes admin privileges on a folder/mailbox
  165.      * after the above function has been used. If the
  166.      * ACLs passed in is null, then the privs are deleted.
  167.      *
  168.      * @param string $mailbox  Mailbox
  169.      * @param string $privs    Previous privileges as returned
  170.      *                          by the _setAdminPriv() method
  171.      *
  172.      * @access private
  173.      */
  174.     function _resetAdminPriv($mailbox$privs = null)
  175.     {
  176.         if (is_null($privs)) {
  177.             $this->deleteACL($mailbox$this->_user);
  178.         else {
  179.             $this->setACL($mailbox$this->_user$privs);
  180.         }
  181.     }
  182.  
  183.     /**
  184.      * Returns quota details.
  185.      *
  186.      * @param string $mailbox  Mailbox to get quota info for.
  187.      *
  188.      * @return mixed  Array of current usage and quota limit or
  189.      *                 false on failure.
  190.      * @access public
  191.      */
  192.     function getQuota($mailbox)
  193.     {
  194.         if(PEAR::isError($ret=$this->getStorageQuota($mailbox))){
  195.             return $ret;
  196.         }
  197.         return array(0=>$ret['USED']1=>$ret['QMAX']);
  198.  
  199.     }
  200.  
  201.     /**
  202.      * Sets a quota.
  203.      *
  204.      * @param string $mailbox  Mailbox to get quota info for
  205.      * @param integer $quota   The quota to set
  206.      *
  207.      * @return mixed  True on success, PEAR_Error otherwise
  208.      */
  209.     function setQuota($mailbox$quota)
  210.     {
  211.         return $this->setStorageQuota($mailbox$quota);
  212.     }
  213.  
  214.     /**
  215.      * Copies a quota from one mailbox to another.
  216.      *
  217.      * @param string $from  Mailbox to copy quota from
  218.      * @param string $to    Mailbox to set quota on
  219.      * @access public
  220.      */
  221.     function copyQuota($from$to)
  222.     {
  223.         $currentQuota $this->getQuota($from);
  224.         $oldQuotaMax trim($currentQuota[1]);
  225.         if ($oldQuotaMax != 'NOT-SET'{
  226.             $this->setQuota($to$oldQuotaMax);
  227.         }
  228.     }
  229.  
  230.  
  231.  
  232.  
  233.     /**
  234.      * Retrieves details of current ACL.
  235.      *
  236.      * @param string $mailbox  Name of mailbox
  237.      * @param  string $user    Optional user to get ACL for
  238.      *
  239.      * @return string  Access stuff
  240.      * @access public
  241.      */
  242.     function getACL($mailbox$user = null)
  243.     {
  244.  
  245.         $acl=parent::getACL($mailbox);
  246.         $acl_arr=array();
  247.         if(is_array($acl)){
  248.             foreach($acl as $a){
  249.                 if$user === null ){
  250.                     $acl_arr[$a['USER']]=$a['RIGHTS'];
  251.                 }else{
  252.                     if$user == $a['USER'){
  253.                         return $a['RIGHTS'];
  254.                     }
  255.                 }
  256.             }
  257.             return $acl_arr;
  258.         }else{
  259.             return $acl;
  260.         }
  261.  
  262.     }
  263.  
  264.  
  265.  
  266.     /**
  267.      * Sets ACL on a mailbox.
  268.      *
  269.      * @param string $mailbox  Name of mailbox
  270.      * @param string $user     Username to apply ACL to
  271.      * @param string $acl      The ACL
  272.      *
  273.      * @return mixed  True on success, PEAR_Error otherwise
  274.      * @access public
  275.      */
  276.     function setACL($mailbox$user$acl)
  277.     {
  278.         return parent::setACL($mailbox$user$acl);
  279.     }
  280.  
  281.  
  282.  
  283.  
  284.     /**
  285.      * Deletes ACL from a mailbox.
  286.      *
  287.      * @param string $mailbox  Name of mailbox
  288.      * @param string $user     Username to remove ACL from
  289.      *
  290.      *
  291.      * @return mixed  True on success, PEAR_Error otherwise
  292.      * @access public
  293.      */
  294.     function deleteACL($mailbox$user)
  295.     {
  296.         return parent::deleteACL($mailbox$user);
  297.     }
  298.  
  299.  
  300.  
  301.     /**
  302.      * Creates a mailbox.
  303.      *
  304.      * @param string $mailbox  Name of mailbox to create
  305.      * @param array  $options  options to pass to create
  306.      *
  307.      *
  308.      * @return mixed  True on success, PEAR error otherwise
  309.      * @access public
  310.      */
  311.     function createMailbox($mailbox$options = null)
  312.     {
  313.         $res = parent::createMailbox($mailbox$options);
  314.         return $res;
  315.     }
  316.  
  317.     /**
  318.      * Renames a mailbox.
  319.      *
  320.      * @param string $mailbox  Name of mailbox to rename
  321.      * @param string $newname  New name of mailbox
  322.      * @param array  $options  options to pass to rename
  323.      *
  324.      * @return mixed  True on success, PEAR error otherwise
  325.      * @access public
  326.      */
  327.     function renameMailbox($mailbox$newname$options = null)
  328.     {
  329.         $oldPrivs $this->_setAdminPriv($mailbox);
  330.         ifPEAR::isError$response = parent::renameMailbox($mailbox$newname$options) )){
  331.             return $response;
  332.         }
  333.         $this->_resetAdminPriv($mailbox$oldPrivs);
  334.         return true;
  335.     }
  336.  
  337.  
  338.  
  339.     /**
  340.      * Deletes a mailbox.
  341.      *
  342.      * @param string $mailbox  Name of mailbox to delete
  343.      *
  344.      * @return mixed  True on success, PEAR error otherwise
  345.      * @access public
  346.      */
  347.     function deleteMailbox($mailbox)
  348.     {
  349.         $oldPrivs $this->_setAdminPriv($mailbox);
  350.         ifPEAR::isError$response = parent::deleteMailbox($mailbox) )){
  351.             $this->_resetAdminPriv($mailbox$oldPrivs);
  352.             return $response;
  353.         }
  354.         return true;
  355.     }
  356.  
  357.     /**
  358.      * Returns a list of folders for a particular user.
  359.      *
  360.      * @param string $prepend  Optional string to prepend
  361.      *
  362.      * @return array  Array of folders matched
  363.      * @access public
  364.      */
  365.     function getFolderList($folderMask = null )
  366.     {
  367.         if$folderMask === null ){
  368.             $folderMask 'user' $this->getHierarchyDelimiter'*' ;
  369.         }
  370.         //echo "FOLDERLIST: $folderMask\n";
  371.         return $this->getMailboxes(''  $folderMask false );
  372.     }
  373.  
  374.  
  375.     /**
  376.      * Returns a list of users.
  377.      *
  378.      * @return array  Array of users found
  379.      * @access public
  380.      */
  381.     function getUserList()
  382.     {
  383.  
  384.         $hierarchyDelimiter$this->getHierarchyDelimiter();
  385.         $user_base='user' $hierarchyDelimiter '%' ;
  386.         if(PEAR::isError$user_list $this->getFolderList($user_base) ) ){
  387.             return $user_list;
  388.         }
  389.         $users = array();
  390.         foreach ($user_list as $user{
  391.             $user_arr=explode($hierarchyDelimiter$user);
  392.             $users[]=$user_arr[1];
  393.         }
  394.         return $users;
  395.     }
  396.  
  397.  
  398.  
  399.     /**
  400.     * Parses a user name
  401.     *
  402.     * @param string $user_name  the user parse
  403.     * @param boolean $append_userPart  true if the method appends 'user.' to the user name
  404.     * @access public
  405.     * @since  1.0
  406.     */
  407.     function getUserName($user_name$append_userPart = true)
  408.     {
  409.          if(strtolower(substr($user_name,0,5)) == 'user.'){
  410.             $user_arr=explode('user.',$user_name);
  411.             $user_name=$user_arr[1];
  412.         }
  413.         if($append_userPart){
  414.             return 'user' $this->getHierarchyDelimiter($user_name;
  415.         }else{
  416.             return $user_name;
  417.         }
  418.  
  419.     }
  420.  
  421.  
  422.  
  423.  
  424.  
  425.     /**
  426.     * deletes a user. Use this instead of deleteMailbox
  427.     *
  428.     * @param string $user_name  the user to deletes
  429.     *
  430.     * @return mixed true on Success/PearError on Failure
  431.     * @access public
  432.     */
  433.     function deleteUser($user_name)
  434.     {
  435.         $user_name=$this->getUserName($user_name);
  436.         return $this->deleteMailbox($user_name);
  437.     }
  438.  
  439.  
  440.  
  441.  
  442.     /**
  443.     * creates a user. Use this instead of createMailbox
  444.     *
  445.     * @param string $user_name  the user to create
  446.     * @param array  $options    options to pass to createMailbox
  447.     *
  448.     * @return mixed true on Success/PearError on Failure
  449.     * @access public
  450.     */
  451.     function createUser($user_name$options = null)
  452.     {
  453.         if$this->userExists($user_name) ){
  454.             return $this->_raiseError("The user $user_name already exists503);
  455.         }
  456.         $user_name=$this->getUserName($user_name);
  457.         return $this->createMailbox($user_name$options);
  458.     }
  459.  
  460.  
  461.  
  462.    /**
  463.     * check if the user name exists
  464.     *
  465.     * @param string $mailbox     user name to check existance
  466.     *
  467.     * @return boolean true on Success/false on Failure
  468.     * @since 1.0
  469.     */
  470.     function userExists($user_name)
  471.     {
  472.         $user_name $this->getUserName($user_name);
  473.         return $this->mailboxExist($user_name);
  474.     }
  475.  
  476.  
  477.     /**
  478.      * Renames a user. This is here since the RENAME command
  479.      * is not allowed on a user's INBOX (ie. the user.<username>
  480.      * mailbox). Supplied args can be either with or without
  481.      * the "user." at the beginning.
  482.      *
  483.      * @param string $oldUser  Name of user to rename
  484.      * @param string $newUser  New name of user
  485.      * @param array  $options  options to pass to createMailbox and renameMailbox
  486.      *
  487.      * @return mixed true on Success/PearError on Failure
  488.      * @access public
  489.      */
  490.     function renameUser($oldUser$newUser$options = null)
  491.     {
  492.  
  493.         $oldUsername $this->getUserName($oldUser,false);
  494.         $newUsername $this->getUserName($newUser,false);
  495.  
  496.         $oldUser =$this->getUserName($oldUser);
  497.         $newUser =$this->getUserName($newUser);
  498.  
  499.         // Check new user doesn't already exist and old user exists
  500.         if (!$this->userExists($oldUsername) ) {
  501.             $msg=sprintf('The user "%s" doesn\'t exist'$oldUsername);
  502.             $code=502;
  503.             return $this->_raiseError($msg$code);
  504.         }
  505.  
  506.         if ($this->userExists($newUsername) ) {
  507.             $msg=sprintf('the user "%s" already exists. choose another user name'$newUsername);
  508.             $code=503;
  509.             return $this->_raiseError($msg$code);
  510.         }
  511.  
  512.         // Create the new mailbox
  513.         $this->createMailbox($newUser$options);
  514.         $oldAdminPrivs $this->_setAdminPriv($newUser);
  515.  
  516.         // Copy Mail and quotas
  517.         $this->copyMail($oldUser$newUser);
  518.         $this->copyQuota($oldUser$newUser);
  519.  
  520.         // Copy the folders
  521.         $folderList $this->getFolderList($oldUser $this->getHierarchyDelimiter('*');
  522.  
  523.         if (!empty($folderList)) {
  524.             foreach ($folderList as $folder{
  525.                 $newFolderName str_replace($oldUser$newUser$folder);
  526.                 $this->renameMailbox($folder$newFolderName$options);
  527.                 $this->setACL($newFolderName$newUsername'lrswipcd');
  528.                 $this->deleteACL($newFolderName$oldUsername);
  529.             }
  530.         }
  531.         $this->_resetAdminPriv($newUser$oldAdminPrivs);
  532.         $this->deleteUser($oldUser);
  533.     }
  534.  
  535.     /**
  536.      * Copies mail from one folder to another.
  537.      *
  538.      * @param string $from  From mailbox name
  539.      * @param string $to    To mailbox name
  540.      *
  541.      * @return mixed true on Success/PearError on Failure
  542.      * @access public
  543.      */
  544.     function copyMail($from$to)
  545.     {
  546.         $oldFromPrivs $this->_setAdminPriv($from);
  547.         $oldToPrivs   $this->_setAdminPriv($to);
  548.  
  549.         $this->selectMailbox($from);
  550.         $this->copyMessages($to);
  551.  
  552.         $this->_resetAdminPriv($from$oldFromPrivs);
  553.         $this->_resetAdminPriv($to$oldToPrivs);
  554.     }
  555.  
  556. }
  557.  
  558. ?>

Documentation generated on Mon, 11 Mar 2019 15:39:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.