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.         return parent::createMailbox($mailbox);
  307.     }
  308.  
  309.     /**
  310.      * Renames a mailbox.
  311.      *
  312.      * @param string $mailbox  Name of mailbox to rename
  313.      * @param string $newname  New name of mailbox
  314.      *
  315.      * @return mixed  True on success, PEAR error otherwise
  316.      * @access public
  317.      */
  318.     function renameMailbox($mailbox$newname)
  319.     {
  320.         $oldPrivs $this->_setAdminPriv($mailbox);
  321.         ifPEAR::isError$response = parent::renameMailbox($mailbox$newname) )){
  322.             return $response;
  323.         }
  324.         $this->_resetAdminPriv($mailbox$oldPrivs);
  325.         return true;
  326.     }
  327.  
  328.  
  329.  
  330.     /**
  331.      * Deletes a mailbox.
  332.      *
  333.      * @param string $mailbox  Name of mailbox to delete
  334.      *
  335.      * @return mixed  True on success, PEAR error otherwise
  336.      * @access public
  337.      */
  338.     function deleteMailbox($mailbox)
  339.     {
  340.         $oldPrivs $this->_setAdminPriv($mailbox);
  341.         ifPEAR::isError$response = parent::deleteMailbox($mailbox) )){
  342.             return $response;
  343.         }
  344.         $this->_resetAdminPriv($mailbox$oldPrivs);
  345.         return true;
  346.     }
  347.  
  348.     /**
  349.      * Returns a list of folders for a particular user.
  350.      *
  351.      * @param string $prepend  Optional string to prepend
  352.      *
  353.      * @return array  Array of folders matched
  354.      * @access public
  355.      */
  356.     function getFolderList($folderMask = null )
  357.     {
  358.         if$folderMask === null ){
  359.             $folderMask 'user' $this->getHierarchyDelimiter'*' ;
  360.         }
  361.         //echo "FOLDERLIST: $folderMask\n";
  362.         return $this->getMailboxes(''  $folderMask false );
  363.     }
  364.  
  365.  
  366.     /**
  367.      * Returns a list of users.
  368.      *
  369.      * @return array  Array of users found
  370.      * @access public
  371.      */
  372.     function getUserList()
  373.     {
  374.  
  375.         $hierarchyDelimiter$this->getHierarchyDelimiter();
  376.         $user_base='user' $hierarchyDelimiter '%' ;
  377.         if(PEAR::isError$user_list $this->getFolderList($user_base) ) ){
  378.             return $user_list;
  379.         }
  380.         $users = array();
  381.         foreach ($user_list as $user{
  382.             $user_arr=explode($hierarchyDelimiter$user);
  383.             $users[]=$user_arr[1];
  384.         }
  385.         return $users;
  386.     }
  387.  
  388.  
  389.  
  390.     /**
  391.     * Parses a user name
  392.     *
  393.     * @param string $user_name  the user parse
  394.     * @param boolean $append_userPart  true if the method appends 'user.' to the user name
  395.     * @access public
  396.     * @since  1.0
  397.     */
  398.     function getUserName($user_name$append_userPart = true)
  399.     {
  400.          if(strtolower(substr($user_name,0,5)) == 'user.'){
  401.             $user_arr=explode('user.',$user_name);
  402.             $user_name=$user_arr[1];
  403.         }
  404.         if($append_userPart){
  405.             return 'user' $this->getHierarchyDelimiter($user_name;
  406.         }else{
  407.             return $user_name;
  408.         }
  409.  
  410.     }
  411.  
  412.                              s
  413.  
  414.  
  415.  
  416.     /**
  417.     * deletes a user. Use this instead of deleteMailbox
  418.     *
  419.     * @param string $user_name  the user to deletes
  420.     *
  421.     * @return mixed true on Success/PearError on Failure
  422.     * @access public
  423.     */
  424.     function deleteUser($user_name)
  425.     {
  426.         $user_name=$this->getUserName($user_name);
  427.         return $this->deleteMailbox($user_name);
  428.     }
  429.  
  430.  
  431.  
  432.  
  433.     /**
  434.     * creates a user. Use this instead of createMailbox
  435.     *
  436.     * @param string $user_name  the user to create
  437.     *
  438.     * @return mixed true on Success/PearError on Failure
  439.     * @access public
  440.     */
  441.     function createUser($user_name)
  442.     {
  443.         if$this->userExists($user_name) ){
  444.             return false;
  445.         }
  446.         $user_name=$this->getUserName($user_name);
  447.         return $this->createMailbox($user_name);
  448.     }
  449.  
  450.  
  451.  
  452.    /**
  453.     * check if the user name exists
  454.     *
  455.     * @param string $mailbox     user name to check existance
  456.     *
  457.     * @return boolean true on Success/false on Failure
  458.     * @since 1.0
  459.     */
  460.     function userExists($user_name)
  461.     {
  462.         $user_name $this->getUserName($user_name);
  463.         return $this->mailboxExist($user_name);
  464.     }
  465.  
  466.  
  467.     /**
  468.      * Renames a user. This is here since the RENAME command
  469.      * is not allowed on a user's INBOX (ie. the user.<username>
  470.      * mailbox). Supplied args can be either with or without
  471.      * the "user." at the beginning.
  472.      *
  473.      * @param string $oldUser  Name of user to rename
  474.      * @param string $newUser  New name of user
  475.      *
  476.      * @return mixed true on Success/PearError on Failure
  477.      * @access public
  478.      */
  479.     function renameUser($oldUser$newUser)
  480.     {
  481.  
  482.         $oldUser =$this->getUserName($oldUser);
  483.  
  484.         $newUser =$this->getUserName($newUser);
  485.  
  486.  
  487.         $oldUsername $this->getUserName($oldUser,false);
  488.         $newUsername $this->getUserName($newUser,false);
  489.  
  490.         // Check new user doesn't already exist and old user exists
  491.         if (!$this->userExists($oldUsername) ) {
  492.             $msg=sprintf('The user "%s" doesn\'t exist'$oldUsername);
  493.             $code=502;
  494.             return $this->_raiseError($msg$code);
  495.  
  496.  
  497.         }
  498.         if ($this->userExists($newUsername) ) {
  499.             $msg=sprintf('the user "%s" already exists. choose another user name'$newUsername);
  500.             $code=503;
  501.             return $this->_raiseError($msg$code);
  502.         }
  503.  
  504.         // Create the new mailbox
  505.         $this->createMailbox($newUser);
  506.         $oldAdminPrivs $this->_setAdminPriv($newUser);
  507.  
  508.         // Copy Mail and quotas
  509.         $this->copyMail($oldUser$newUser);
  510.         $this->copyQuota($oldUser$newUser);
  511.  
  512.         // Copy the folders
  513.         $folderList $this->getFolderList($oldUser $this->getHierarchyDelimiter('*');
  514.  
  515.         if (!empty($folderList)) {
  516.             foreach ($folderList as $folder{
  517.                 $newFolderName str_replace($oldUser$newUser$folder);
  518.                 $this->renameMailbox($folder$newFolderName);
  519.                 $this->setACL($newFolderName$newUsername'lrswipcd');
  520.                 $this->deleteACL($newFolderName$oldUsername);
  521.             }
  522.         }
  523.         $this->_resetAdminPriv($newUser$oldAdminPrivs);
  524.         $this->deleteMailbox($oldUser);
  525.     }
  526.  
  527.  
  528.  
  529.  
  530.     /**
  531.      * Copies mail from one folder to another.
  532.      *
  533.      * @param string $from  From mailbox name
  534.      * @param string $to    To mailbox name
  535.      *
  536.      * @return mixed true on Success/PearError on Failure
  537.      * @access public
  538.      */
  539.     function copyMail($from$to)
  540.     {
  541.         $oldFromPrivs $this->_setAdminPriv($from);
  542.         $oldToPrivs   $this->_setAdminPriv($to);
  543.  
  544.         $this->selectMailbox($from);
  545.         $this->copyMessages($to);
  546.  
  547.         $this->_resetAdminPriv($from$oldFromPrivs);
  548.         $this->_resetAdminPriv($to$oldToPrivs);
  549.     }
  550.  
  551. }

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