Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 require_once(dirname(__FILE__).'/Abstract.php');
00032 include_once(dirname(__FILE__).'/../Exception.php');
00033 include_once(dirname(__FILE__).'/../InvalidArgumentException.php');
00034 include_once(dirname(__FILE__).'/../OutOfSequenceException.php');
00035
00039 class CAS_ProxiedService_Imap
00040 extends CAS_ProxiedService_Abstract
00041 {
00042
00048 private $_username;
00049
00056 public function __construct ($username) {
00057 if (!is_string($username) || !strlen($username))
00058 throw new CAS_InvalidArgumentException('Invalid username.');
00059
00060 $this->_username = $username;
00061 }
00062
00067 private $_url;
00068
00075 public function getServiceUrl () {
00076 if (empty($this->_url))
00077 throw new CAS_ProxiedService_Exception('No URL set via '.get_class($this).'->getServiceUrl($url).');
00078
00079 return $this->_url;
00080 }
00081
00082
00083
00084
00085
00093 public function setServiceUrl ($url) {
00094 if ($this->hasBeenOpened())
00095 throw new CAS_OutOfSequenceException('Cannot set the URL, stream already opened.');
00096 if (!is_string($url) || !strlen($url))
00097 throw new CAS_InvalidArgumentException('Invalid url.');
00098
00099 $this->_url = $url;
00100 }
00101
00107 private $_mailbox;
00108
00116 public function setMailbox ($mailbox) {
00117 if ($this->hasBeenOpened())
00118 throw new CAS_OutOfSequenceException('Cannot set the mailbox, stream already opened.');
00119 if (!is_string($mailbox) || !strlen($mailbox))
00120 throw new CAS_InvalidArgumentException('Invalid mailbox.');
00121
00122 $this->_mailbox = $mailbox;
00123 }
00124
00130 private $_options = NULL;
00131
00139 public function setOptions ($options) {
00140 if ($this->hasBeenOpened())
00141 throw new CAS_OutOfSequenceException('Cannot set options, stream already opened.');
00142 if (!is_int($options))
00143 throw new CAS_InvalidArgumentException('Invalid options.');
00144
00145 $this->_options = $options;
00146 }
00147
00148
00149
00150
00151
00163 public function open () {
00164 if ($this->hasBeenOpened())
00165 throw new CAS_OutOfSequenceException('Stream already opened.');
00166 if (empty($this->_mailbox))
00167 throw new CAS_ProxiedService_Exception('You must specify a mailbox via '.get_class($this).'->setMailbox($mailbox)');
00168
00169 phpCAS::traceBegin();
00170
00171
00172 $this->initializeProxyTicket();
00173 phpCAS::trace('opening IMAP mailbox `'.$this->_mailbox.'\'...');
00174 $this->_stream = @imap_open($this->_mailbox, $this->_username, $this->getProxyTicket(), $this->_options);
00175 if ($this->_stream) {
00176 phpCAS::trace('ok');
00177 } else {
00178 phpCAS::trace('could not open mailbox');
00179 // @todo add localization integration.
00180 // $this->_errorMessage = sprintf($this->getString(CAS_STR_SERVICE_UNAVAILABLE), $url, var_export(imap_errors(),TRUE));
00181 $message = 'IMAP Error: '.$url.' '. var_export(imap_errors(),TRUE);
00182 phpCAS::trace($message);
00183 throw new CAS_ProxiedService_Exception($message);
00184 }
00185
00186 phpCAS::traceEnd();
00187 return $this->_stream;
00188 }
00189
00195 protected function hasBeenOpened () {
00196 return !empty($this->_stream);
00197 }
00198
00199 /*********************************************************
00200 * 3. Access the result
00201 *********************************************************/
00207 private $_stream;
00208
00214 public function getStream () {
00215 if (!$this->hasBeenOpened())
00216 throw new CAS_OutOfSequenceException('Cannot access stream, not opened yet.');
00217
00218 return $this->_stream;
00219 }
00220
00228 public function getImapProxyTicket () {
00229 if (!$this->hasBeenOpened())
00230 throw new CAS_OutOfSequenceException('Cannot access errors, stream not opened yet.');
00231
00232 return $this->getProxyTicket();
00233 }
00234 }