phpCAS version 1.3.6

Imap.php
Go to the documentation of this file.
1 <?php
2
40 class CAS_ProxiedService_Imap
41 extends CAS_ProxiedService_Abstract
42 {
43
49 private $_username;
50
58 public function __construct ($username)
59 {
60 if (!is_string($username) || !strlen($username)) {
61 throw new CAS_InvalidArgumentException('Invalid username.');
62 }
63
64 $this->_username = $username;
65 }
66
71 private $_url;
72
79 public function getServiceUrl ()
80 {
81 if (empty($this->_url)) {
82 throw new CAS_ProxiedService_Exception(
83 'No URL set via '.get_class($this).'->getServiceUrl($url).'
84 );
85 }
86
87 return $this->_url;
88 }
89
90 /*********************************************************
91 * Configure the Stream
92 *********************************************************/
93
102 public function setServiceUrl ($url)
103 {
104 if ($this->hasBeenOpened()) {
105 throw new CAS_OutOfSequenceException(
106 'Cannot set the URL, stream already opened.'
107 );
108 }
109 if (!is_string($url) || !strlen($url)) {
110 throw new CAS_InvalidArgumentException('Invalid url.');
111 }
112
113 $this->_url = $url;
114 }
115
121 private $_mailbox;
122
131 public function setMailbox ($mailbox)
132 {
133 if ($this->hasBeenOpened()) {
134 throw new CAS_OutOfSequenceException(
135 'Cannot set the mailbox, stream already opened.'
136 );
137 }
138 if (!is_string($mailbox) || !strlen($mailbox)) {
139 throw new CAS_InvalidArgumentException('Invalid mailbox.');
140 }
141
142 $this->_mailbox = $mailbox;
143 }
144
150 private $_options = null;
151
161 public function setOptions ($options)
162 {
163 if ($this->hasBeenOpened()) {
164 throw new CAS_OutOfSequenceException(
165 'Cannot set options, stream already opened.'
166 );
167 }
168 if (!is_int($options)) {
169 throw new CAS_InvalidArgumentException('Invalid options.');
170 }
171
172 $this->_options = $options;
173 }
174
175 /*********************************************************
176 * 2. Open the stream
177 *********************************************************/
178
192 public function open ()
193 {
194 if ($this->hasBeenOpened()) {
195 throw new CAS_OutOfSequenceException('Stream already opened.');
196 }
197 if (empty($this->_mailbox)) {
198 throw new CAS_ProxiedService_Exception(
199 'You must specify a mailbox via '.get_class($this)
200 .'->setMailbox($mailbox)'
201 );
202 }
203
204 phpCAS::traceBegin();
205
206 // Get our proxy ticket and append it to our URL.
207 $this->initializeProxyTicket();
208 phpCAS::trace('opening IMAP mailbox `'.$this->_mailbox.'\'...');
209 $this->_stream = @imap_open(
210 $this->_mailbox, $this->_username, $this->getProxyTicket(),
211 $this->_options
212 );
213 if ($this->_stream) {
214 phpCAS::trace('ok');
215 } else {
216 phpCAS::trace('could not open mailbox');
217 // @todo add localization integration.
218 $message = 'IMAP Error: '.$this->_url.' '. var_export(imap_errors(), true);
219 phpCAS::trace($message);
220 throw new CAS_ProxiedService_Exception($message);
221 }
222
223 phpCAS::traceEnd();
224 return $this->_stream;
225 }
226
232 protected function hasBeenOpened ()
233 {
234 return !empty($this->_stream);
235 }
236
237 /*********************************************************
238 * 3. Access the result
239 *********************************************************/
245 private $_stream;
246
252 public function getStream ()
253 {
254 if (!$this->hasBeenOpened()) {
255 throw new CAS_OutOfSequenceException(
256 'Cannot access stream, not opened yet.'
257 );
258 }
259 return $this->_stream;
260 }
261
270 public function getImapProxyTicket ()
271 {
272 if (!$this->hasBeenOpened()) {
273 throw new CAS_OutOfSequenceException(
274 'Cannot access errors, stream not opened yet.'
275 );
276 }
277 return $this->getProxyTicket();
278 }
279 }
280 ?>
CAS_ProxiedService_Imap\setServiceUrl
setServiceUrl($url)
Definition: Imap.php:102
CAS_ProxiedService_Imap\$_url
$_url
Definition: Imap.php:71
CAS_ProxiedService_Abstract\initializeProxyTicket
initializeProxyTicket()
Definition: Abstract.php:133
CAS_ProxiedService_Imap\setOptions
setOptions($options)
Definition: Imap.php:161
CAS_ProxiedService_Exception
Definition: Exception.php:40
CAS_ProxiedService_Abstract
Definition: Abstract.php:41
CAS_ProxiedService_Imap\$_username
$_username
Definition: Imap.php:49
CAS_ProxiedService_Imap\__construct
__construct($username)
Definition: Imap.php:58
phpCAS\trace
static trace($str)
Definition: CAS.php:579
CAS_ProxiedService_Imap\$_options
$_options
Definition: Imap.php:150
CAS_ProxiedService_Imap
Definition: Imap.php:40
CAS_OutOfSequenceException
Definition: OutOfSequenceException.php:44
CAS_ProxiedService_Imap\hasBeenOpened
hasBeenOpened()
Definition: Imap.php:232
CAS_InvalidArgumentException
Definition: InvalidArgumentException.php:40
CAS_ProxiedService_Imap\setMailbox
setMailbox($mailbox)
Definition: Imap.php:131
CAS_ProxiedService_Imap\getServiceUrl
getServiceUrl()
Definition: Imap.php:79
CAS_ProxiedService_Imap\open
open()
Definition: Imap.php:192
CAS_ProxiedService_Imap\$_mailbox
$_mailbox
Definition: Imap.php:121
phpCAS\traceBegin
static traceBegin()
Definition: CAS.php:591