/CAS/CAS-1.3.2/CAS/ProxiedService/Imap.php

https://github.com/medieteknik/Medieteknik.nu · PHP · 260 lines · 96 code · 26 blank · 138 comment · 17 complexity · 98191df46680a99236c48afff8f0d659 MD5 · raw file

  1. <?php
  2. /**
  3. * Licensed to Jasig under one or more contributor license
  4. * agreements. See the NOTICE file distributed with this work for
  5. * additional information regarding copyright ownership.
  6. *
  7. * Jasig licenses this file to you under the Apache License,
  8. * Version 2.0 (the "License"); you may not use this file except in
  9. * compliance with the License. You may obtain a copy of the License at:
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * PHP Version 5
  20. *
  21. * @file CAS/ProxiedService/Imap.php
  22. * @category Authentication
  23. * @package PhpCAS
  24. * @author Adam Franco <afranco@middlebury.edu>
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  26. * @link https://wiki.jasig.org/display/CASC/phpCAS
  27. */
  28. /**
  29. * Provides access to a proxy-authenticated IMAP stream
  30. *
  31. * @class CAS_ProxiedService_Imap
  32. * @category Authentication
  33. * @package PhpCAS
  34. * @author Adam Franco <afranco@middlebury.edu>
  35. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  36. * @link https://wiki.jasig.org/display/CASC/phpCAS
  37. */
  38. class CAS_ProxiedService_Imap
  39. extends CAS_ProxiedService_Abstract
  40. {
  41. /**
  42. * The username to send via imap_open.
  43. *
  44. * @var string $_username;
  45. */
  46. private $_username;
  47. /**
  48. * Constructor.
  49. *
  50. * @param string $username Username
  51. *
  52. * @return void
  53. */
  54. public function __construct ($username)
  55. {
  56. if (!is_string($username) || !strlen($username)) {
  57. throw new CAS_InvalidArgumentException('Invalid username.');
  58. }
  59. $this->_username = $username;
  60. }
  61. /**
  62. * The target service url.
  63. * @var string $_url;
  64. */
  65. private $_url;
  66. /**
  67. * Answer a service identifier (URL) for whom we should fetch a proxy ticket.
  68. *
  69. * @return string
  70. * @throws Exception If no service url is available.
  71. */
  72. public function getServiceUrl ()
  73. {
  74. if (empty($this->_url)) {
  75. throw new CAS_ProxiedService_Exception('No URL set via '.get_class($this).'->getServiceUrl($url).');
  76. }
  77. return $this->_url;
  78. }
  79. /*********************************************************
  80. * Configure the Stream
  81. *********************************************************/
  82. /**
  83. * Set the URL of the service to pass to CAS for proxy-ticket retrieval.
  84. *
  85. * @param string $url Url to set
  86. *
  87. * @return void
  88. * @throws CAS_OutOfSequenceException If called after the stream has been opened.
  89. */
  90. public function setServiceUrl ($url)
  91. {
  92. if ($this->hasBeenOpened()) {
  93. throw new CAS_OutOfSequenceException('Cannot set the URL, stream already opened.');
  94. }
  95. if (!is_string($url) || !strlen($url)) {
  96. throw new CAS_InvalidArgumentException('Invalid url.');
  97. }
  98. $this->_url = $url;
  99. }
  100. /**
  101. * The mailbox to open. See the $mailbox parameter of imap_open().
  102. *
  103. * @var string $_mailbox
  104. */
  105. private $_mailbox;
  106. /**
  107. * Set the mailbox to open. See the $mailbox parameter of imap_open().
  108. *
  109. * @param string $mailbox Mailbox to set
  110. *
  111. * @return void
  112. * @throws CAS_OutOfSequenceException If called after the stream has been opened.
  113. */
  114. public function setMailbox ($mailbox)
  115. {
  116. if ($this->hasBeenOpened()) {
  117. throw new CAS_OutOfSequenceException('Cannot set the mailbox, stream already opened.');
  118. }
  119. if (!is_string($mailbox) || !strlen($mailbox)) {
  120. throw new CAS_InvalidArgumentException('Invalid mailbox.');
  121. }
  122. $this->_mailbox = $mailbox;
  123. }
  124. /**
  125. * A bit mask of options to pass to imap_open() as the $options parameter.
  126. *
  127. * @var int $_options
  128. */
  129. private $_options = null;
  130. /**
  131. * Set the options for opening the stream. See the $options parameter of
  132. * imap_open().
  133. *
  134. * @param int $options Options for the stream
  135. *
  136. * @return void
  137. * @throws CAS_OutOfSequenceException If called after the stream has been opened.
  138. */
  139. public function setOptions ($options)
  140. {
  141. if ($this->hasBeenOpened()) {
  142. throw new CAS_OutOfSequenceException('Cannot set options, stream already opened.');
  143. }
  144. if (!is_int($options)) {
  145. throw new CAS_InvalidArgumentException('Invalid options.');
  146. }
  147. $this->_options = $options;
  148. }
  149. /*********************************************************
  150. * 2. Open the stream
  151. *********************************************************/
  152. /**
  153. * Open the IMAP stream (similar to imap_open()).
  154. *
  155. * @return resource Returns an IMAP stream on success
  156. * @throws CAS_OutOfSequenceException If called multiple times.
  157. * @throws CAS_ProxyTicketException If there is a proxy-ticket failure.
  158. * The code of the Exception will be one of:
  159. * PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE
  160. * PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE
  161. * PHPCAS_SERVICE_PT_FAILURE
  162. * @throws CAS_ProxiedService_Exception If there is a failure sending the request to the target service. */
  163. public function open ()
  164. {
  165. if ($this->hasBeenOpened()) {
  166. throw new CAS_OutOfSequenceException('Stream already opened.');
  167. }
  168. if (empty($this->_mailbox)) {
  169. throw new CAS_ProxiedService_Exception('You must specify a mailbox via '.get_class($this).'->setMailbox($mailbox)');
  170. }
  171. phpCAS::traceBegin();
  172. // Get our proxy ticket and append it to our URL.
  173. $this->initializeProxyTicket();
  174. phpCAS::trace('opening IMAP mailbox `'.$this->_mailbox.'\'...');
  175. $this->_stream = @imap_open($this->_mailbox, $this->_username, $this->getProxyTicket(), $this->_options);
  176. if ($this->_stream) {
  177. phpCAS::trace('ok');
  178. } else {
  179. phpCAS::trace('could not open mailbox');
  180. // @todo add localization integration.
  181. $message = 'IMAP Error: '.$url.' '. var_export(imap_errors(), true);
  182. phpCAS::trace($message);
  183. throw new CAS_ProxiedService_Exception($message);
  184. }
  185. phpCAS::traceEnd();
  186. return $this->_stream;
  187. }
  188. /**
  189. * Answer true if our request has been sent yet.
  190. *
  191. * @return bool
  192. */
  193. protected function hasBeenOpened ()
  194. {
  195. return !empty($this->_stream);
  196. }
  197. /*********************************************************
  198. * 3. Access the result
  199. *********************************************************/
  200. /**
  201. * The IMAP stream
  202. *
  203. * @var resource $_stream
  204. */
  205. private $_stream;
  206. /**
  207. * Answer the IMAP stream
  208. *
  209. * @return resource
  210. */
  211. public function getStream ()
  212. {
  213. if (!$this->hasBeenOpened()) {
  214. throw new CAS_OutOfSequenceException('Cannot access stream, not opened yet.');
  215. }
  216. return $this->_stream;
  217. }
  218. /**
  219. * CAS_Client::serviceMail() needs to return the proxy ticket for some reason,
  220. * so this method provides access to it.
  221. *
  222. * @return string
  223. * @throws CAS_OutOfSequenceException If called before the stream has been
  224. * opened.
  225. */
  226. public function getImapProxyTicket ()
  227. {
  228. if (!$this->hasBeenOpened()) {
  229. throw new CAS_OutOfSequenceException('Cannot access errors, stream not opened yet.');
  230. }
  231. return $this->getProxyTicket();
  232. }
  233. }
  234. ?>