ILIAS  release_7 Revision v7.30-3-g800a261c036
Imap.php
Go to the documentation of this file.
1<?php
2
41{
42
48 private $_username;
49
57 public function __construct($username)
58 {
59 if (!is_string($username) || !strlen($username)) {
60 throw new CAS_InvalidArgumentException('Invalid username.');
61 }
62
63 $this->_username = $username;
64 }
65
70 private $_url;
71
78 public function getServiceUrl()
79 {
80 if (empty($this->_url)) {
82 'No URL set via ' . get_class($this) . '->getServiceUrl($url).'
83 );
84 }
85
86 return $this->_url;
87 }
88
89 /*********************************************************
90 * Configure the Stream
91 *********************************************************/
92
101 public function setServiceUrl($url)
102 {
103 if ($this->hasBeenOpened()) {
105 'Cannot set the URL, stream already opened.'
106 );
107 }
108 if (!is_string($url) || !strlen($url)) {
109 throw new CAS_InvalidArgumentException('Invalid url.');
110 }
111
112 $this->_url = $url;
113 }
114
120 private $_mailbox;
121
130 public function setMailbox($mailbox)
131 {
132 if ($this->hasBeenOpened()) {
134 'Cannot set the mailbox, stream already opened.'
135 );
136 }
137 if (!is_string($mailbox) || !strlen($mailbox)) {
138 throw new CAS_InvalidArgumentException('Invalid mailbox.');
139 }
140
141 $this->_mailbox = $mailbox;
142 }
143
149 private $_options = null;
150
160 public function setOptions($options)
161 {
162 if ($this->hasBeenOpened()) {
164 'Cannot set options, stream already opened.'
165 );
166 }
167 if (!is_int($options)) {
168 throw new CAS_InvalidArgumentException('Invalid options.');
169 }
170
171 $this->_options = $options;
172 }
173
174 /*********************************************************
175 * 2. Open the stream
176 *********************************************************/
177
191 public function open()
192 {
193 if ($this->hasBeenOpened()) {
194 throw new CAS_OutOfSequenceException('Stream already opened.');
195 }
196 if (empty($this->_mailbox)) {
198 'You must specify a mailbox via ' . get_class($this)
199 . '->setMailbox($mailbox)'
200 );
201 }
202
204
205 // Get our proxy ticket and append it to our URL.
206 $this->initializeProxyTicket();
207 phpCAS::trace('opening IMAP mailbox `' . $this->_mailbox . '\'...');
208 $this->_stream = @imap_open(
209 $this->_mailbox,
210 $this->_username,
211 $this->getProxyTicket(),
212 $this->_options
213 );
214 if ($this->_stream) {
215 phpCAS::trace('ok');
216 } else {
217 phpCAS::trace('could not open mailbox');
218 // @todo add localization integration.
219 $message = 'IMAP Error: ' . $this->_url . ' ' . var_export(imap_errors(), true);
220 phpCAS::trace($message);
221 throw new CAS_ProxiedService_Exception($message);
222 }
223
224 phpCAS::traceEnd();
225 return $this->_stream;
226 }
227
233 protected function hasBeenOpened()
234 {
235 return !empty($this->_stream);
236 }
237
238 /*********************************************************
239 * 3. Access the result
240 *********************************************************/
246 private $_stream;
247
253 public function getStream()
254 {
255 if (!$this->hasBeenOpened()) {
256 throw new CAS_OutOfSequenceException(
257 'Cannot access stream, not opened yet.'
258 );
259 }
260 return $this->_stream;
261 }
262
271 public function getImapProxyTicket()
272 {
273 if (!$this->hasBeenOpened()) {
274 throw new CAS_OutOfSequenceException(
275 'Cannot access errors, stream not opened yet.'
276 );
277 }
278 return $this->getProxyTicket();
279 }
280}
An exception for terminatinating execution or to throw for unit testing.
Exception that denotes invalid arguments were passed.
This class defines Exceptions that should be thrown when the sequence of operations is invalid.
This class implements common methods for ProxiedService implementations included with phpCAS.
Definition: Abstract.php:42
initializeProxyTicket()
Fetch our proxy ticket.
Definition: Abstract.php:132
An Exception for problems communicating with a proxied service.
Definition: Exception.php:41
Provides access to a proxy-authenticated IMAP stream.
Definition: Imap.php:41
getServiceUrl()
Answer a service identifier (URL) for whom we should fetch a proxy ticket.
Definition: Imap.php:78
$_url
The target service url.
Definition: Imap.php:70
$_options
A bit mask of options to pass to imap_open() as the $options parameter.
Definition: Imap.php:149
setMailbox($mailbox)
Set the mailbox to open.
Definition: Imap.php:130
setServiceUrl($url)
Set the URL of the service to pass to CAS for proxy-ticket retrieval.
Definition: Imap.php:101
setOptions($options)
Set the options for opening the stream.
Definition: Imap.php:160
__construct($username)
Constructor.
Definition: Imap.php:57
$_username
The username to send via imap_open.
Definition: Imap.php:48
$_mailbox
The mailbox to open.
Definition: Imap.php:120
hasBeenOpened()
Answer true if our request has been sent yet.
Definition: Imap.php:233
open()
Open the IMAP stream (similar to imap_open()).
Definition: Imap.php:191
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:599
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode.
Definition: CAS.php:611
$url