ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
Imap.php
Go to the documentation of this file.
1 <?php
2 
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)) {
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)) {
199  'You must specify a mailbox via '.get_class($this)
200  .'->setMailbox($mailbox)'
201  );
202  }
203 
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 ?>
setServiceUrl($url)
Set the URL of the service to pass to CAS for proxy-ticket retrieval.
Definition: Imap.php:102
$_url
The target service url.
Definition: Imap.php:71
initializeProxyTicket()
Fetch our proxy ticket.
Definition: Abstract.php:133
setOptions($options)
Set the options for opening the stream.
Definition: Imap.php:161
$url
Definition: shib_logout.php:72
An Exception for problems communicating with a proxied service.
Definition: Exception.php:40
This class implements common methods for ProxiedService implementations included with phpCAS...
Definition: Abstract.php:41
$_username
The username to send via imap_open.
Definition: Imap.php:49
__construct($username)
Constructor.
Definition: Imap.php:58
static trace($str)
This method is used to log something in debug mode.
Definition: CAS.php:579
$_options
A bit mask of options to pass to imap_open() as the $options parameter.
Definition: Imap.php:150
Provides access to a proxy-authenticated IMAP stream.
Definition: Imap.php:40
This class defines Exceptions that should be thrown when the sequence of operations is invalid...
if(!is_array($argv)) $options
hasBeenOpened()
Answer true if our request has been sent yet.
Definition: Imap.php:232
Exception that denotes invalid arguments were passed.
setMailbox($mailbox)
Set the mailbox to open.
Definition: Imap.php:131
getServiceUrl()
Answer a service identifier (URL) for whom we should fetch a proxy ticket.
Definition: Imap.php:79
open()
Open the IMAP stream (similar to imap_open()).
Definition: Imap.php:192
$_mailbox
The mailbox to open.
Definition: Imap.php:121
static traceBegin()
This method is used to indicate the start of the execution of a function in debug mode...
Definition: CAS.php:591