Go to the documentation of this file.00001 <?php
00002
00040 class CAS_ProxiedService_Imap
00041 extends CAS_ProxiedService_Abstract
00042 {
00043
00049 private $_username;
00050
00058 public function __construct ($username)
00059 {
00060 if (!is_string($username) || !strlen($username)) {
00061 throw new CAS_InvalidArgumentException('Invalid username.');
00062 }
00063
00064 $this->_username = $username;
00065 }
00066
00071 private $_url;
00072
00079 public function getServiceUrl ()
00080 {
00081 if (empty($this->_url)) {
00082 throw new CAS_ProxiedService_Exception('No URL set via '.get_class($this).'->getServiceUrl($url).');
00083 }
00084
00085 return $this->_url;
00086 }
00087
00088
00089
00090
00091
00100 public function setServiceUrl ($url)
00101 {
00102 if ($this->hasBeenOpened()) {
00103 throw new CAS_OutOfSequenceException('Cannot set the URL, stream already opened.');
00104 }
00105 if (!is_string($url) || !strlen($url)) {
00106 throw new CAS_InvalidArgumentException('Invalid url.');
00107 }
00108
00109 $this->_url = $url;
00110 }
00111
00117 private $_mailbox;
00118
00127 public function setMailbox ($mailbox)
00128 {
00129 if ($this->hasBeenOpened()) {
00130 throw new CAS_OutOfSequenceException('Cannot set the mailbox, stream already opened.');
00131 }
00132 if (!is_string($mailbox) || !strlen($mailbox)) {
00133 throw new CAS_InvalidArgumentException('Invalid mailbox.');
00134 }
00135
00136 $this->_mailbox = $mailbox;
00137 }
00138
00144 private $_options = null;
00145
00155 public function setOptions ($options)
00156 {
00157 if ($this->hasBeenOpened()) {
00158 throw new CAS_OutOfSequenceException('Cannot set options, stream already opened.');
00159 }
00160 if (!is_int($options)) {
00161 throw new CAS_InvalidArgumentException('Invalid options.');
00162 }
00163
00164 $this->_options = $options;
00165 }
00166
00167
00168
00169
00170
00182 public function open ()
00183 {
00184 if ($this->hasBeenOpened()) {
00185 throw new CAS_OutOfSequenceException('Stream already opened.');
00186 }
00187 if (empty($this->_mailbox)) {
00188 throw new CAS_ProxiedService_Exception('You must specify a mailbox via '.get_class($this).'->setMailbox($mailbox)');
00189 }
00190
00191 phpCAS::traceBegin();
00192
00193
00194 $this->initializeProxyTicket();
00195 phpCAS::trace('opening IMAP mailbox `'.$this->_mailbox.'\'...');
00196 $this->_stream = @imap_open($this->_mailbox, $this->_username, $this->getProxyTicket(), $this->_options);
00197 if ($this->_stream) {
00198 phpCAS::trace('ok');
00199 } else {
00200 phpCAS::trace('could not open mailbox');
00201 // @todo add localization integration.
00202 $message = 'IMAP Error: '.$url.' '. var_export(imap_errors(), true);
00203 phpCAS::trace($message);
00204 throw new CAS_ProxiedService_Exception($message);
00205 }
00206
00207 phpCAS::traceEnd();
00208 return $this->_stream;
00209 }
00210
00216 protected function hasBeenOpened ()
00217 {
00218 return !empty($this->_stream);
00219 }
00220
00221 /*********************************************************
00222 * 3. Access the result
00223 *********************************************************/
00229 private $_stream;
00230
00236 public function getStream ()
00237 {
00238 if (!$this->hasBeenOpened()) {
00239 throw new CAS_OutOfSequenceException('Cannot access stream, not opened yet.');
00240 }
00241 return $this->_stream;
00242 }
00243
00252 public function getImapProxyTicket ()
00253 {
00254 if (!$this->hasBeenOpened()) {
00255 throw new CAS_OutOfSequenceException('Cannot access errors, stream not opened yet.');
00256 }
00257 return $this->getProxyTicket();
00258 }
00259 }
00260 ?>