[ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * The IMAP_Admin:: class allow managing of mailboxes on IMAP servers. 4 * 5 * Required parameters:<pre> 6 * 'admin_user' The name of a user with admin privileges. 7 * 'admin_password' The password of the adminstrator.</pre> 8 * 9 * Optional parameters:<pre> 10 * 'hostspec' The hostname or IP address of the server. 11 * DEFAULT: 'localhost' 12 * 'port' The server port to which we will connect. 13 * IMAP is generally 143, while IMAP-SSL is generally 993. 14 * DEFAULT: 143 15 * 'protocol' The connection protocol (e.g. 'imap', 'pop3', 'nntp'). 16 * Protocol is one of 'imap/notls' (or only 'imap' if you 17 * have a c-client version 2000c or older), 'imap/ssl', 18 * or 'imap/ssl/novalidate-cert' (for a self-signed 19 * certificate). 20 * DEFAULT: 'imap' 21 * 'userhierarchy' The hierarchy where user mailboxes are stored. 22 * DEFAULT: 'user.' 23 * 'dsn' The full IMAP connection string. 24 * If not present, this is built from 'hostspec', 'port' 25 * and 'protocol' parameters.</pre> 26 * 27 * $Horde: framework/IMAP/IMAP/Admin.php,v 1.5.6.9 2006/05/31 19:39:47 slusarz Exp $ 28 * 29 * Copyright 2004-2006 Jan Schneider <jan@horde.org> 30 * 31 * See the enclosed file COPYING for license information (LGPL). If you 32 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 33 * 34 * @author Jan Schneider <jan@horde.org> 35 * @since Horde 3.0 36 * @package Horde_IMAP 37 */ 38 class IMAP_Admin { 39 40 /** 41 * Parameter hash. 42 * 43 * @var array 44 */ 45 var $_params; 46 47 /** 48 * IMAP resource. 49 * 50 * @var resource 51 */ 52 var $_imap; 53 54 /** 55 * Constructor. 56 * 57 * @param array $params A hash with all necessary parameters 58 */ 59 function IMAP_Admin($params) 60 { 61 $default_params = array( 62 'hostspec' => 'localhost', 63 'port' => '143', 64 'protocol' => 'imap', 65 'userhierarchy' => 'user.' 66 ); 67 $this->_params = array_merge($default_params, $params); 68 69 /* Create DSN string. */ 70 if (!isset($this->_params['dsn'])) { 71 $this->_params['dsn'] = sprintf('{%s:%d/%s}', 72 $this->_params['hostspec'], 73 $this->_params['port'], 74 $this->_params['protocol']); 75 $this->_ref = $this->_params['dsn']; 76 } else { 77 if (preg_match('/^{([^:]+):(\d+)\/([^}]+)}/', $this->_params['dsn'], $matches)) { 78 $this->_params['hostspec'] = $matches[1]; 79 $this->_params['port'] = $matches[2]; 80 $this->_params['protocol'] = $matches[3]; 81 $this->_ref = sprintf('{%s:%d/%s}', 82 $this->_params['hostspec'], 83 $this->_params['port'], 84 $this->_params['protocol']); 85 } 86 } 87 } 88 89 /** 90 * Connects to the IMAP server with the parameters passed to the 91 * constructor. 92 * 93 * @return resource|object An IMAP resource or a PEAR_Error on failure 94 */ 95 function _connect() 96 { 97 if (!isset($this->_imap)) { 98 $this->_imap = @imap_open($this->_ref, 99 $this->_params['admin_user'], 100 $this->_params['admin_password'], 101 OP_HALFOPEN); 102 if (!$this->_imap) { 103 $this->_imap = PEAR::raiseError(_("Authentication at IMAP server failed."), 'horde.error'); 104 } 105 } 106 return $this->_imap; 107 } 108 109 /** 110 * Adds a mailbox. 111 * 112 * @param string $mailbox The mailbox name to add. 113 * 114 * @return mixed True on success or a PEAR_Error object on failure. 115 */ 116 function addMailbox($mailbox) 117 { 118 if (is_a($imap = $this->_connect(), 'PEAR_Error')) { 119 return $imap; 120 } 121 if (!@imap_createmailbox($imap, $this->_ref . $this->_params['userhierarchy'] . $mailbox)) { 122 return PEAR::raiseError(imap_last_error(), 'horde.warning'); 123 } 124 125 return $this->_grantAdminPerms($mailbox); 126 } 127 128 /** 129 * Grant the admin user all rights on the mailbox. 130 * 131 * @access private 132 * 133 * @param string $mailbox the name of the mailbox on which we will 134 * grant the permission 135 * @return mixed True if successful, or PEAR_Error on failure 136 */ 137 function _grantAdminPerms($mailbox) 138 { 139 require_once dirname(__FILE__) . '/ACL.php'; 140 141 $params = array('username' => $this->_params['admin_user'], 142 'password' => $this->_params['admin_password'], 143 'hostspec' => $this->_params['hostspec'], 144 'port' => $this->_params['port'], 145 'protocol' => $this->_params['protocol']); 146 $acl = &IMAP_ACL::factory('rfc2086', $params); 147 $result = $acl->createACL($this->_params['userhierarchy'] . $mailbox, 148 $this->_params['admin_user'], 149 array('l' => true, 150 'r' => true, 151 's' => true, 152 'w' => true, 153 'i' => true, 154 'p' => true, 155 'c' => true, 156 'd' => true, 157 'a' => true)); 158 if (is_a($result, 'PEAR_Error')) { 159 return $result; 160 } 161 return true; 162 } 163 164 /** 165 * Deletes a mailbox. 166 * 167 * @param string $mailbox The mailbox to delete. 168 * 169 * @return mixed True on success or a PEAR_Error object on failure. 170 */ 171 function removeMailbox($mailbox) 172 { 173 if (is_a($imap = $this->_connect(), 'PEAR_Error')) { 174 return $imap; 175 } 176 177 $this->_grantAdminPerms($mailbox); 178 if (!@imap_deletemailbox($imap, $this->_ref . $this->_params['userhierarchy'] . $mailbox)) { 179 return PEAR::raiseError(imap_last_error(), 'horde.warning'); 180 } 181 return true; 182 } 183 184 /** 185 * List all mailboxes. 186 * 187 * Note that this does not work on a virtual-domain enabled Cyrus (it will 188 * only return mailboxes in the default domain). 189 * 190 * @return mixed The array of mailboxes, or a PEAR_Error object on failure. 191 */ 192 function listMailboxes() 193 { 194 if (is_a($imap = $this->_connect(), 'PEAR_Error')) { 195 return $imap; 196 } 197 198 $list = @imap_list($imap, $this->_ref, $this->_params['userhierarchy'] . '%'); 199 if (!$list) { 200 return array(); 201 } 202 return preg_replace('/.*' . preg_quote($this->_params['userhierarchy'], '/') . '(.*)/', '\\1', $list); 203 } 204 205 /** 206 * Check whether a mailbox exists. 207 * 208 * @param string $mailbox The mailbox to check. 209 * @return mixed True if mailbox exists, false if not, or a PEAR_Error. 210 */ 211 function mailboxExists($mailbox) 212 { 213 if (is_a($imap = $this->_connect(), 'PEAR_Error')) { 214 return $imap; 215 } 216 217 $list = @imap_list($imap, $this->_ref, $this->_params['userhierarchy'] . $mailbox); 218 if ($list === false) { 219 return false; 220 } 221 return count($list) > 0; 222 } 223 224 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |