phpCAS  version 1.4.0
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 
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 }
281 ?>
setOptions($options)
Definition: Imap.php:161
__construct($username)
Definition: Imap.php:58
static trace($str)
Definition: CAS.php:616
setMailbox($mailbox)
Definition: Imap.php:131
static traceBegin()
Definition: CAS.php:628