• Main Page
  • Related Pages
  • Modules
  • Classes
  • Files
  • Examples
  • File List
  • File Members

CAS/ProxiedService/Imap.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *
00009  *               * Redistributions of source code must retain the above copyright notice,
00010  *                       this list of conditions and the following disclaimer.
00011  *               * Redistributions in binary form must reproduce the above copyright notice,
00012  *                       this list of conditions and the following disclaimer in the documentation
00013  *                       and/or other materials provided with the distribution.
00014  *               * Neither the name of the ESUP-Portail consortium & the JA-SIG
00015  *                       Collaborative nor the names of its contributors may be used to endorse or
00016  *                       promote products derived from this software without specific prior
00017  *                       written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
00023  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
00026  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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          * Configure the Stream
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          * 2. Open the stream
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                 // Get our proxy ticket and append it to our URL.
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 }

Generated on Sun Jun 5 2011 19:05:26 for phpCAS by  doxygen 1.7.1