PageRenderTime 53ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/auth/cas/CAS/CAS.php

https://bitbucket.org/ngmares/moodle
PHP | 1531 lines | 695 code | 165 blank | 671 comment | 145 complexity | eee09fa1185b09fdd92704b471e67453 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the ESUP-Portail consortium & the JA-SIG
  15. * Collaborative nor the names of its contributors may be used to endorse or
  16. * promote products derived from this software without specific prior
  17. * written permission.
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. //
  30. // hack by Vangelis Haniotakis to handle the absence of $_SERVER['REQUEST_URI'] in IIS
  31. //
  32. if (!isset($_SERVER['REQUEST_URI'])) {
  33. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
  34. }
  35. //
  36. // another one by Vangelis Haniotakis also to make phpCAS work with PHP5
  37. //
  38. if (version_compare(PHP_VERSION, '5', '>=') && !(function_exists('domxml_new_doc'))) {
  39. require_once (dirname(__FILE__) . '/CAS/domxml-php4-to-php5.php');
  40. }
  41. /**
  42. * @file CAS/CAS.php
  43. * Interface class of the phpCAS library
  44. *
  45. * @ingroup public
  46. */
  47. // ########################################################################
  48. // CONSTANTS
  49. // ########################################################################
  50. // ------------------------------------------------------------------------
  51. // CAS VERSIONS
  52. // ------------------------------------------------------------------------
  53. /**
  54. * phpCAS version. accessible for the user by phpCAS::getVersion().
  55. */
  56. define('PHPCAS_VERSION', '1.1.3');
  57. // ------------------------------------------------------------------------
  58. // CAS VERSIONS
  59. // ------------------------------------------------------------------------
  60. /**
  61. * @addtogroup public
  62. * @{
  63. */
  64. /**
  65. * CAS version 1.0
  66. */
  67. define("CAS_VERSION_1_0", '1.0');
  68. /*!
  69. * CAS version 2.0
  70. */
  71. define("CAS_VERSION_2_0", '2.0');
  72. // ------------------------------------------------------------------------
  73. // SAML defines
  74. // ------------------------------------------------------------------------
  75. /**
  76. * SAML protocol
  77. */
  78. define("SAML_VERSION_1_1", 'S1');
  79. /**
  80. * XML header for SAML POST
  81. */
  82. define("SAML_XML_HEADER", '<?xml version="1.0" encoding="UTF-8"?>');
  83. /**
  84. * SOAP envelope for SAML POST
  85. */
  86. define("SAML_SOAP_ENV", '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/>');
  87. /**
  88. * SOAP body for SAML POST
  89. */
  90. define("SAML_SOAP_BODY", '<SOAP-ENV:Body>');
  91. /**
  92. * SAMLP request
  93. */
  94. define("SAMLP_REQUEST", '<samlp:Request xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol" MajorVersion="1" MinorVersion="1" RequestID="_192.168.16.51.1024506224022" IssueInstant="2002-06-19T17:03:44.022Z">');
  95. define("SAMLP_REQUEST_CLOSE", '</samlp:Request>');
  96. /**
  97. * SAMLP artifact tag (for the ticket)
  98. */
  99. define("SAML_ASSERTION_ARTIFACT", '<samlp:AssertionArtifact>');
  100. /**
  101. * SAMLP close
  102. */
  103. define("SAML_ASSERTION_ARTIFACT_CLOSE", '</samlp:AssertionArtifact>');
  104. /**
  105. * SOAP body close
  106. */
  107. define("SAML_SOAP_BODY_CLOSE", '</SOAP-ENV:Body>');
  108. /**
  109. * SOAP envelope close
  110. */
  111. define("SAML_SOAP_ENV_CLOSE", '</SOAP-ENV:Envelope>');
  112. /**
  113. * SAML Attributes
  114. */
  115. define("SAML_ATTRIBUTES", 'SAMLATTRIBS');
  116. /** @} */
  117. /**
  118. * @addtogroup publicPGTStorage
  119. * @{
  120. */
  121. // ------------------------------------------------------------------------
  122. // FILE PGT STORAGE
  123. // ------------------------------------------------------------------------
  124. /**
  125. * Default path used when storing PGT's to file
  126. */
  127. define("CAS_PGT_STORAGE_FILE_DEFAULT_PATH", '/tmp');
  128. /**
  129. * phpCAS::setPGTStorageFile()'s 2nd parameter to write plain text files
  130. */
  131. define("CAS_PGT_STORAGE_FILE_FORMAT_PLAIN", 'plain');
  132. /**
  133. * phpCAS::setPGTStorageFile()'s 2nd parameter to write xml files
  134. */
  135. define("CAS_PGT_STORAGE_FILE_FORMAT_XML", 'xml');
  136. /**
  137. * Default format used when storing PGT's to file
  138. */
  139. define("CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT", CAS_PGT_STORAGE_FILE_FORMAT_PLAIN);
  140. /** @} */
  141. // ------------------------------------------------------------------------
  142. // SERVICE ACCESS ERRORS
  143. // ------------------------------------------------------------------------
  144. /**
  145. * @addtogroup publicServices
  146. * @{
  147. */
  148. /**
  149. * phpCAS::service() error code on success
  150. */
  151. define("PHPCAS_SERVICE_OK", 0);
  152. /**
  153. * phpCAS::service() error code when the PT could not retrieve because
  154. * the CAS server did not respond.
  155. */
  156. define("PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE", 1);
  157. /**
  158. * phpCAS::service() error code when the PT could not retrieve because
  159. * the response of the CAS server was ill-formed.
  160. */
  161. define("PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE", 2);
  162. /**
  163. * phpCAS::service() error code when the PT could not retrieve because
  164. * the CAS server did not want to.
  165. */
  166. define("PHPCAS_SERVICE_PT_FAILURE", 3);
  167. /**
  168. * phpCAS::service() error code when the service was not available.
  169. */
  170. define("PHPCAS_SERVICE_NOT AVAILABLE", 4);
  171. /** @} */
  172. // ------------------------------------------------------------------------
  173. // LANGUAGES
  174. // ------------------------------------------------------------------------
  175. /**
  176. * @addtogroup publicLang
  177. * @{
  178. */
  179. define("PHPCAS_LANG_ENGLISH", 'english');
  180. define("PHPCAS_LANG_FRENCH", 'french');
  181. define("PHPCAS_LANG_GREEK", 'greek');
  182. define("PHPCAS_LANG_GERMAN", 'german');
  183. define("PHPCAS_LANG_JAPANESE", 'japanese');
  184. define("PHPCAS_LANG_SPANISH", 'spanish');
  185. define("PHPCAS_LANG_CATALAN", 'catalan');
  186. /** @} */
  187. /**
  188. * @addtogroup internalLang
  189. * @{
  190. */
  191. /**
  192. * phpCAS default language (when phpCAS::setLang() is not used)
  193. */
  194. define("PHPCAS_LANG_DEFAULT", PHPCAS_LANG_ENGLISH);
  195. /** @} */
  196. // ------------------------------------------------------------------------
  197. // DEBUG
  198. // ------------------------------------------------------------------------
  199. /**
  200. * @addtogroup publicDebug
  201. * @{
  202. */
  203. /**
  204. * The default directory for the debug file under Unix.
  205. */
  206. define('DEFAULT_DEBUG_DIR', '/tmp/');
  207. /** @} */
  208. // ------------------------------------------------------------------------
  209. // MISC
  210. // ------------------------------------------------------------------------
  211. /**
  212. * @addtogroup internalMisc
  213. * @{
  214. */
  215. /**
  216. * This global variable is used by the interface class phpCAS.
  217. *
  218. * @hideinitializer
  219. */
  220. $GLOBALS['PHPCAS_CLIENT'] = null;
  221. /**
  222. * This global variable is used to store where the initializer is called from
  223. * (to print a comprehensive error in case of multiple calls).
  224. *
  225. * @hideinitializer
  226. */
  227. $GLOBALS['PHPCAS_INIT_CALL'] = array (
  228. 'done' => FALSE,
  229. 'file' => '?',
  230. 'line' => -1,
  231. 'method' => '?'
  232. );
  233. /**
  234. * This global variable is used to store where the method checking
  235. * the authentication is called from (to print comprehensive errors)
  236. *
  237. * @hideinitializer
  238. */
  239. $GLOBALS['PHPCAS_AUTH_CHECK_CALL'] = array (
  240. 'done' => FALSE,
  241. 'file' => '?',
  242. 'line' => -1,
  243. 'method' => '?',
  244. 'result' => FALSE
  245. );
  246. /**
  247. * This global variable is used to store phpCAS debug mode.
  248. *
  249. * @hideinitializer
  250. */
  251. $GLOBALS['PHPCAS_DEBUG'] = array (
  252. 'filename' => FALSE,
  253. 'indent' => 0,
  254. 'unique_id' => ''
  255. );
  256. /** @} */
  257. // ########################################################################
  258. // CLIENT CLASS
  259. // ########################################################################
  260. // include client class
  261. include_once (dirname(__FILE__) . '/CAS/client.php');
  262. // ########################################################################
  263. // INTERFACE CLASS
  264. // ########################################################################
  265. /**
  266. * @class phpCAS
  267. * The phpCAS class is a simple container for the phpCAS library. It provides CAS
  268. * authentication for web applications written in PHP.
  269. *
  270. * @ingroup public
  271. * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
  272. *
  273. * \internal All its methods access the same object ($PHPCAS_CLIENT, declared
  274. * at the end of CAS/client.php).
  275. */
  276. class phpCAS {
  277. // ########################################################################
  278. // INITIALIZATION
  279. // ########################################################################
  280. /**
  281. * @addtogroup publicInit
  282. * @{
  283. */
  284. /**
  285. * phpCAS client initializer.
  286. * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
  287. * called, only once, and before all other methods (except phpCAS::getVersion()
  288. * and phpCAS::setDebug()).
  289. *
  290. * @param $server_version the version of the CAS server
  291. * @param $server_hostname the hostname of the CAS server
  292. * @param $server_port the port the CAS server is running on
  293. * @param $server_uri the URI the CAS server is responding on
  294. * @param $start_session Have phpCAS start PHP sessions (default true)
  295. *
  296. * @return a newly created CASClient object
  297. */
  298. function client($server_version, $server_hostname, $server_port, $server_uri, $start_session = true) {
  299. global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL;
  300. phpCAS :: traceBegin();
  301. if (is_object($PHPCAS_CLIENT)) {
  302. phpCAS :: error($PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . $PHPCAS_INIT_CALL['file'] . ':' . $PHPCAS_INIT_CALL['line'] . ')');
  303. }
  304. if (gettype($server_version) != 'string') {
  305. phpCAS :: error('type mismatched for parameter $server_version (should be `string\')');
  306. }
  307. if (gettype($server_hostname) != 'string') {
  308. phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')');
  309. }
  310. if (gettype($server_port) != 'integer') {
  311. phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')');
  312. }
  313. if (gettype($server_uri) != 'string') {
  314. phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')');
  315. }
  316. // store where the initializer is called from
  317. $dbg = phpCAS :: backtrace();
  318. $PHPCAS_INIT_CALL = array (
  319. 'done' => TRUE,
  320. 'file' => $dbg[0]['file'],
  321. 'line' => $dbg[0]['line'],
  322. 'method' => __CLASS__ . '::' . __FUNCTION__
  323. );
  324. // initialize the global object $PHPCAS_CLIENT
  325. $PHPCAS_CLIENT = new CASClient($server_version, FALSE /*proxy*/
  326. , $server_hostname, $server_port, $server_uri, $start_session);
  327. phpCAS :: traceEnd();
  328. }
  329. /**
  330. * phpCAS proxy initializer.
  331. * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
  332. * called, only once, and before all other methods (except phpCAS::getVersion()
  333. * and phpCAS::setDebug()).
  334. *
  335. * @param $server_version the version of the CAS server
  336. * @param $server_hostname the hostname of the CAS server
  337. * @param $server_port the port the CAS server is running on
  338. * @param $server_uri the URI the CAS server is responding on
  339. * @param $start_session Have phpCAS start PHP sessions (default true)
  340. *
  341. * @return a newly created CASClient object
  342. */
  343. function proxy($server_version, $server_hostname, $server_port, $server_uri, $start_session = true) {
  344. global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL;
  345. phpCAS :: traceBegin();
  346. if (is_object($PHPCAS_CLIENT)) {
  347. phpCAS :: error($PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . $PHPCAS_INIT_CALL['file'] . ':' . $PHPCAS_INIT_CALL['line'] . ')');
  348. }
  349. if (gettype($server_version) != 'string') {
  350. phpCAS :: error('type mismatched for parameter $server_version (should be `string\')');
  351. }
  352. if (gettype($server_hostname) != 'string') {
  353. phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')');
  354. }
  355. if (gettype($server_port) != 'integer') {
  356. phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')');
  357. }
  358. if (gettype($server_uri) != 'string') {
  359. phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')');
  360. }
  361. // store where the initialzer is called from
  362. $dbg = phpCAS :: backtrace();
  363. $PHPCAS_INIT_CALL = array (
  364. 'done' => TRUE,
  365. 'file' => $dbg[0]['file'],
  366. 'line' => $dbg[0]['line'],
  367. 'method' => __CLASS__ . '::' . __FUNCTION__
  368. );
  369. // initialize the global object $PHPCAS_CLIENT
  370. $PHPCAS_CLIENT = new CASClient($server_version, TRUE /*proxy*/
  371. , $server_hostname, $server_port, $server_uri, $start_session);
  372. phpCAS :: traceEnd();
  373. }
  374. /** @} */
  375. // ########################################################################
  376. // DEBUGGING
  377. // ########################################################################
  378. /**
  379. * @addtogroup publicDebug
  380. * @{
  381. */
  382. /**
  383. * Set/unset debug mode
  384. *
  385. * @param $filename the name of the file used for logging, or FALSE to stop debugging.
  386. */
  387. function setDebug($filename = '') {
  388. global $PHPCAS_DEBUG;
  389. if ($filename != FALSE && gettype($filename) != 'string') {
  390. phpCAS :: error('type mismatched for parameter $dbg (should be FALSE or the name of the log file)');
  391. }
  392. if (empty ($filename)) {
  393. if (preg_match('/^Win.*/', getenv('OS'))) {
  394. if (isset ($_ENV['TMP'])) {
  395. $debugDir = $_ENV['TMP'] . '/';
  396. } else
  397. if (isset ($_ENV['TEMP'])) {
  398. $debugDir = $_ENV['TEMP'] . '/';
  399. } else {
  400. $debugDir = '';
  401. }
  402. } else {
  403. $debugDir = DEFAULT_DEBUG_DIR;
  404. }
  405. $filename = $debugDir . 'phpCAS.log';
  406. }
  407. if (empty ($PHPCAS_DEBUG['unique_id'])) {
  408. $PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))), 0, 4);
  409. }
  410. $PHPCAS_DEBUG['filename'] = $filename;
  411. phpCAS :: trace('START phpCAS-' . PHPCAS_VERSION . ' ******************');
  412. }
  413. /** @} */
  414. /**
  415. * @addtogroup internalDebug
  416. * @{
  417. */
  418. /**
  419. * This method is a wrapper for debug_backtrace() that is not available
  420. * in all PHP versions (>= 4.3.0 only)
  421. */
  422. function backtrace() {
  423. if (function_exists('debug_backtrace')) {
  424. return debug_backtrace();
  425. } else {
  426. // poor man's hack ... but it does work ...
  427. return array ();
  428. }
  429. }
  430. /**
  431. * Logs a string in debug mode.
  432. *
  433. * @param $str the string to write
  434. *
  435. * @private
  436. */
  437. function log($str) {
  438. $indent_str = ".";
  439. global $PHPCAS_DEBUG;
  440. if ($PHPCAS_DEBUG['filename']) {
  441. for ($i = 0; $i < $PHPCAS_DEBUG['indent']; $i++) {
  442. $indent_str .= '| ';
  443. }
  444. error_log($PHPCAS_DEBUG['unique_id'] . ' ' . $indent_str . $str . "\n", 3, $PHPCAS_DEBUG['filename']);
  445. }
  446. }
  447. /**
  448. * This method is used by interface methods to print an error and where the function
  449. * was originally called from.
  450. *
  451. * @param $msg the message to print
  452. *
  453. * @private
  454. */
  455. function error($msg) {
  456. $dbg = phpCAS :: backtrace();
  457. $function = '?';
  458. $file = '?';
  459. $line = '?';
  460. if (is_array($dbg)) {
  461. for ($i = 1; $i < sizeof($dbg); $i++) {
  462. if (is_array($dbg[$i])) {
  463. if ($dbg[$i]['class'] == __CLASS__) {
  464. $function = $dbg[$i]['function'];
  465. $file = $dbg[$i]['file'];
  466. $line = $dbg[$i]['line'];
  467. }
  468. }
  469. }
  470. }
  471. echo "<br />\n<b>phpCAS error</b>: <font color=\"FF0000\"><b>" . __CLASS__ . "::" . $function . '(): ' . htmlentities($msg) . "</b></font> in <b>" . $file . "</b> on line <b>" . $line . "</b><br />\n";
  472. phpCAS :: trace($msg);
  473. phpCAS :: traceExit();
  474. exit ();
  475. }
  476. /**
  477. * This method is used to log something in debug mode.
  478. */
  479. function trace($str) {
  480. $dbg = phpCAS :: backtrace();
  481. phpCAS :: log($str . ' [' . basename($dbg[1]['file']) . ':' . $dbg[1]['line'] . ']');
  482. }
  483. /**
  484. * This method is used to indicate the start of the execution of a function in debug mode.
  485. */
  486. function traceBegin() {
  487. global $PHPCAS_DEBUG;
  488. $dbg = phpCAS :: backtrace();
  489. $str = '=> ';
  490. if (!empty ($dbg[2]['class'])) {
  491. $str .= $dbg[2]['class'] . '::';
  492. }
  493. $str .= $dbg[2]['function'] . '(';
  494. if (is_array($dbg[2]['args'])) {
  495. foreach ($dbg[2]['args'] as $index => $arg) {
  496. if ($index != 0) {
  497. $str .= ', ';
  498. }
  499. $str .= str_replace("\n", "", var_export($arg, TRUE));
  500. }
  501. }
  502. $str .= ') [' . basename($dbg[2]['file']) . ':' . $dbg[2]['line'] . ']';
  503. phpCAS :: log($str);
  504. $PHPCAS_DEBUG['indent']++;
  505. }
  506. /**
  507. * This method is used to indicate the end of the execution of a function in debug mode.
  508. *
  509. * @param $res the result of the function
  510. */
  511. function traceEnd($res = '') {
  512. global $PHPCAS_DEBUG;
  513. $PHPCAS_DEBUG['indent']--;
  514. $dbg = phpCAS :: backtrace();
  515. $str = '';
  516. $str .= '<= ' . str_replace("\n", "", var_export($res, TRUE));
  517. phpCAS :: log($str);
  518. }
  519. /**
  520. * This method is used to indicate the end of the execution of the program
  521. */
  522. function traceExit() {
  523. global $PHPCAS_DEBUG;
  524. phpCAS :: log('exit()');
  525. while ($PHPCAS_DEBUG['indent'] > 0) {
  526. phpCAS :: log('-');
  527. $PHPCAS_DEBUG['indent']--;
  528. }
  529. }
  530. /** @} */
  531. // ########################################################################
  532. // INTERNATIONALIZATION
  533. // ########################################################################
  534. /**
  535. * @addtogroup publicLang
  536. * @{
  537. */
  538. /**
  539. * This method is used to set the language used by phpCAS.
  540. * @note Can be called only once.
  541. *
  542. * @param $lang a string representing the language.
  543. *
  544. * @sa PHPCAS_LANG_FRENCH, PHPCAS_LANG_ENGLISH
  545. */
  546. function setLang($lang) {
  547. global $PHPCAS_CLIENT;
  548. if (!is_object($PHPCAS_CLIENT)) {
  549. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  550. }
  551. if (gettype($lang) != 'string') {
  552. phpCAS :: error('type mismatched for parameter $lang (should be `string\')');
  553. }
  554. $PHPCAS_CLIENT->setLang($lang);
  555. }
  556. /** @} */
  557. // ########################################################################
  558. // VERSION
  559. // ########################################################################
  560. /**
  561. * @addtogroup public
  562. * @{
  563. */
  564. /**
  565. * This method returns the phpCAS version.
  566. *
  567. * @return the phpCAS version.
  568. */
  569. function getVersion() {
  570. return PHPCAS_VERSION;
  571. }
  572. /** @} */
  573. // ########################################################################
  574. // HTML OUTPUT
  575. // ########################################################################
  576. /**
  577. * @addtogroup publicOutput
  578. * @{
  579. */
  580. /**
  581. * This method sets the HTML header used for all outputs.
  582. *
  583. * @param $header the HTML header.
  584. */
  585. function setHTMLHeader($header) {
  586. global $PHPCAS_CLIENT;
  587. if (!is_object($PHPCAS_CLIENT)) {
  588. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  589. }
  590. if (gettype($header) != 'string') {
  591. phpCAS :: error('type mismatched for parameter $header (should be `string\')');
  592. }
  593. $PHPCAS_CLIENT->setHTMLHeader($header);
  594. }
  595. /**
  596. * This method sets the HTML footer used for all outputs.
  597. *
  598. * @param $footer the HTML footer.
  599. */
  600. function setHTMLFooter($footer) {
  601. global $PHPCAS_CLIENT;
  602. if (!is_object($PHPCAS_CLIENT)) {
  603. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  604. }
  605. if (gettype($footer) != 'string') {
  606. phpCAS :: error('type mismatched for parameter $footer (should be `string\')');
  607. }
  608. $PHPCAS_CLIENT->setHTMLFooter($footer);
  609. }
  610. /** @} */
  611. // ########################################################################
  612. // PGT STORAGE
  613. // ########################################################################
  614. /**
  615. * @addtogroup publicPGTStorage
  616. * @{
  617. */
  618. /**
  619. * This method is used to tell phpCAS to store the response of the
  620. * CAS server to PGT requests onto the filesystem.
  621. *
  622. * @param $format the format used to store the PGT's (`plain' and `xml' allowed)
  623. * @param $path the path where the PGT's should be stored
  624. */
  625. function setPGTStorageFile($format = '', $path = '') {
  626. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  627. phpCAS :: traceBegin();
  628. if (!is_object($PHPCAS_CLIENT)) {
  629. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  630. }
  631. if (!$PHPCAS_CLIENT->isProxy()) {
  632. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  633. }
  634. if ($PHPCAS_AUTH_CHECK_CALL['done']) {
  635. phpCAS :: error('this method should only be called before ' . $PHPCAS_AUTH_CHECK_CALL['method'] . '() (called at ' . $PHPCAS_AUTH_CHECK_CALL['file'] . ':' . $PHPCAS_AUTH_CHECK_CALL['line'] . ')');
  636. }
  637. if (gettype($format) != 'string') {
  638. phpCAS :: error('type mismatched for parameter $format (should be `string\')');
  639. }
  640. if (gettype($path) != 'string') {
  641. phpCAS :: error('type mismatched for parameter $format (should be `string\')');
  642. }
  643. $PHPCAS_CLIENT->setPGTStorageFile($format, $path);
  644. phpCAS :: traceEnd();
  645. }
  646. /** @} */
  647. // ########################################################################
  648. // ACCESS TO EXTERNAL SERVICES
  649. // ########################################################################
  650. /**
  651. * @addtogroup publicServices
  652. * @{
  653. */
  654. /**
  655. * This method is used to access an HTTP[S] service.
  656. *
  657. * @param $url the service to access.
  658. * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on
  659. * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE,
  660. * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE.
  661. * @param $output the output of the service (also used to give an error
  662. * message on failure).
  663. *
  664. * @return TRUE on success, FALSE otherwise (in this later case, $err_code
  665. * gives the reason why it failed and $output contains an error message).
  666. */
  667. function serviceWeb($url, & $err_code, & $output) {
  668. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  669. phpCAS :: traceBegin();
  670. if (!is_object($PHPCAS_CLIENT)) {
  671. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  672. }
  673. if (!$PHPCAS_CLIENT->isProxy()) {
  674. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  675. }
  676. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  677. phpCAS :: error('this method should only be called after the programmer is sure the user has been authenticated (by calling ' . __CLASS__ . '::checkAuthentication() or ' . __CLASS__ . '::forceAuthentication()');
  678. }
  679. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  680. phpCAS :: error('authentication was checked (by ' . $PHPCAS_AUTH_CHECK_CALL['method'] . '() at ' . $PHPCAS_AUTH_CHECK_CALL['file'] . ':' . $PHPCAS_AUTH_CHECK_CALL['line'] . ') but the method returned FALSE');
  681. }
  682. if (gettype($url) != 'string') {
  683. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  684. }
  685. $res = $PHPCAS_CLIENT->serviceWeb($url, $err_code, $output);
  686. phpCAS :: traceEnd($res);
  687. return $res;
  688. }
  689. /**
  690. * This method is used to access an IMAP/POP3/NNTP service.
  691. *
  692. * @param $url a string giving the URL of the service, including the mailing box
  693. * for IMAP URLs, as accepted by imap_open().
  694. * @param $service a string giving for CAS retrieve Proxy ticket
  695. * @param $flags options given to imap_open().
  696. * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on
  697. * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE,
  698. * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE.
  699. * @param $err_msg an error message on failure
  700. * @param $pt the Proxy Ticket (PT) retrieved from the CAS server to access the URL
  701. * on success, FALSE on error).
  702. *
  703. * @return an IMAP stream on success, FALSE otherwise (in this later case, $err_code
  704. * gives the reason why it failed and $err_msg contains an error message).
  705. */
  706. function serviceMail($url, $service, $flags, & $err_code, & $err_msg, & $pt) {
  707. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  708. phpCAS :: traceBegin();
  709. if (!is_object($PHPCAS_CLIENT)) {
  710. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  711. }
  712. if (!$PHPCAS_CLIENT->isProxy()) {
  713. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  714. }
  715. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  716. phpCAS :: error('this method should only be called after the programmer is sure the user has been authenticated (by calling ' . __CLASS__ . '::checkAuthentication() or ' . __CLASS__ . '::forceAuthentication()');
  717. }
  718. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  719. phpCAS :: error('authentication was checked (by ' . $PHPCAS_AUTH_CHECK_CALL['method'] . '() at ' . $PHPCAS_AUTH_CHECK_CALL['file'] . ':' . $PHPCAS_AUTH_CHECK_CALL['line'] . ') but the method returned FALSE');
  720. }
  721. if (gettype($url) != 'string') {
  722. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  723. }
  724. if (gettype($flags) != 'integer') {
  725. phpCAS :: error('type mismatched for parameter $flags (should be `integer\')');
  726. }
  727. $res = $PHPCAS_CLIENT->serviceMail($url, $service, $flags, $err_code, $err_msg, $pt);
  728. phpCAS :: traceEnd($res);
  729. return $res;
  730. }
  731. /** @} */
  732. // ########################################################################
  733. // AUTHENTICATION
  734. // ########################################################################
  735. /**
  736. * @addtogroup publicAuth
  737. * @{
  738. */
  739. /**
  740. * Set the times authentication will be cached before really accessing the CAS server in gateway mode:
  741. * - -1: check only once, and then never again (until you pree login)
  742. * - 0: always check
  743. * - n: check every "n" time
  744. *
  745. * @param $n an integer.
  746. */
  747. function setCacheTimesForAuthRecheck($n) {
  748. global $PHPCAS_CLIENT;
  749. if (!is_object($PHPCAS_CLIENT)) {
  750. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  751. }
  752. if (gettype($n) != 'integer') {
  753. phpCAS :: error('type mismatched for parameter $header (should be `string\')');
  754. }
  755. $PHPCAS_CLIENT->setCacheTimesForAuthRecheck($n);
  756. }
  757. /**
  758. * This method is called to check if the user is authenticated (use the gateway feature).
  759. * @return TRUE when the user is authenticated; otherwise FALSE.
  760. */
  761. function checkAuthentication() {
  762. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  763. phpCAS :: traceBegin();
  764. if (!is_object($PHPCAS_CLIENT)) {
  765. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  766. }
  767. $auth = $PHPCAS_CLIENT->checkAuthentication();
  768. // store where the authentication has been checked and the result
  769. $dbg = phpCAS :: backtrace();
  770. $PHPCAS_AUTH_CHECK_CALL = array (
  771. 'done' => TRUE,
  772. 'file' => $dbg[0]['file'],
  773. 'line' => $dbg[0]['line'],
  774. 'method' => __CLASS__ . '::' . __FUNCTION__,
  775. 'result' => $auth
  776. );
  777. phpCAS :: traceEnd($auth);
  778. return $auth;
  779. }
  780. /**
  781. * This method is called to force authentication if the user was not already
  782. * authenticated. If the user is not authenticated, halt by redirecting to
  783. * the CAS server.
  784. */
  785. function forceAuthentication() {
  786. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  787. phpCAS :: traceBegin();
  788. if (!is_object($PHPCAS_CLIENT)) {
  789. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  790. }
  791. $auth = $PHPCAS_CLIENT->forceAuthentication();
  792. // store where the authentication has been checked and the result
  793. $dbg = phpCAS :: backtrace();
  794. $PHPCAS_AUTH_CHECK_CALL = array (
  795. 'done' => TRUE,
  796. 'file' => $dbg[0]['file'],
  797. 'line' => $dbg[0]['line'],
  798. 'method' => __CLASS__ . '::' . __FUNCTION__,
  799. 'result' => $auth
  800. );
  801. if (!$auth) {
  802. phpCAS :: trace('user is not authenticated, redirecting to the CAS server');
  803. $PHPCAS_CLIENT->forceAuthentication();
  804. } else {
  805. phpCAS :: trace('no need to authenticate (user `' . phpCAS :: getUser() . '\' is already authenticated)');
  806. }
  807. phpCAS :: traceEnd();
  808. return $auth;
  809. }
  810. /**
  811. * This method is called to renew the authentication.
  812. **/
  813. function renewAuthentication() {
  814. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  815. phpCAS :: traceBegin();
  816. if (!is_object($PHPCAS_CLIENT)) {
  817. phpCAS :: error('this method should not be called before' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  818. }
  819. // store where the authentication has been checked and the result
  820. $dbg = phpCAS :: backtrace();
  821. $PHPCAS_AUTH_CHECK_CALL = array (
  822. 'done' => TRUE,
  823. 'file' => $dbg[0]['file'],
  824. 'line' => $dbg[0]['line'],
  825. 'method' => __CLASS__ . '::' . __FUNCTION__,
  826. 'result' => $auth
  827. );
  828. $PHPCAS_CLIENT->renewAuthentication();
  829. phpCAS :: traceEnd();
  830. }
  831. /**
  832. * This method has been left from version 0.4.1 for compatibility reasons.
  833. */
  834. function authenticate() {
  835. phpCAS :: error('this method is deprecated. You should use ' . __CLASS__ . '::forceAuthentication() instead');
  836. }
  837. /**
  838. * This method is called to check if the user is authenticated (previously or by
  839. * tickets given in the URL).
  840. *
  841. * @return TRUE when the user is authenticated.
  842. */
  843. function isAuthenticated() {
  844. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  845. phpCAS :: traceBegin();
  846. if (!is_object($PHPCAS_CLIENT)) {
  847. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  848. }
  849. // call the isAuthenticated method of the global $PHPCAS_CLIENT object
  850. $auth = $PHPCAS_CLIENT->isAuthenticated();
  851. // store where the authentication has been checked and the result
  852. $dbg = phpCAS :: backtrace();
  853. $PHPCAS_AUTH_CHECK_CALL = array (
  854. 'done' => TRUE,
  855. 'file' => $dbg[0]['file'],
  856. 'line' => $dbg[0]['line'],
  857. 'method' => __CLASS__ . '::' . __FUNCTION__,
  858. 'result' => $auth
  859. );
  860. phpCAS :: traceEnd($auth);
  861. return $auth;
  862. }
  863. /**
  864. * Checks whether authenticated based on $_SESSION. Useful to avoid
  865. * server calls.
  866. * @return true if authenticated, false otherwise.
  867. * @since 0.4.22 by Brendan Arnold
  868. */
  869. function isSessionAuthenticated() {
  870. global $PHPCAS_CLIENT;
  871. if (!is_object($PHPCAS_CLIENT)) {
  872. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  873. }
  874. return ($PHPCAS_CLIENT->isSessionAuthenticated());
  875. }
  876. /**
  877. * This method returns the CAS user's login name.
  878. * @warning should not be called only after phpCAS::forceAuthentication()
  879. * or phpCAS::checkAuthentication().
  880. *
  881. * @return the login name of the authenticated user
  882. */
  883. function getUser() {
  884. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  885. if (!is_object($PHPCAS_CLIENT)) {
  886. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  887. }
  888. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  889. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
  890. }
  891. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  892. phpCAS :: error('authentication was checked (by ' . $PHPCAS_AUTH_CHECK_CALL['method'] . '() at ' . $PHPCAS_AUTH_CHECK_CALL['file'] . ':' . $PHPCAS_AUTH_CHECK_CALL['line'] . ') but the method returned FALSE');
  893. }
  894. return $PHPCAS_CLIENT->getUser();
  895. }
  896. /**
  897. * This method returns the CAS user's login name.
  898. * @warning should not be called only after phpCAS::forceAuthentication()
  899. * or phpCAS::checkAuthentication().
  900. *
  901. * @return the login name of the authenticated user
  902. */
  903. function getAttributes() {
  904. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  905. if (!is_object($PHPCAS_CLIENT)) {
  906. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  907. }
  908. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  909. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
  910. }
  911. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  912. phpCAS :: error('authentication was checked (by ' . $PHPCAS_AUTH_CHECK_CALL['method'] . '() at ' . $PHPCAS_AUTH_CHECK_CALL['file'] . ':' . $PHPCAS_AUTH_CHECK_CALL['line'] . ') but the method returned FALSE');
  913. }
  914. return $PHPCAS_CLIENT->getAttributes();
  915. }
  916. /**
  917. * Handle logout requests.
  918. */
  919. function handleLogoutRequests($check_client = true, $allowed_clients = false) {
  920. global $PHPCAS_CLIENT;
  921. if (!is_object($PHPCAS_CLIENT)) {
  922. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  923. }
  924. return ($PHPCAS_CLIENT->handleLogoutRequests($check_client, $allowed_clients));
  925. }
  926. /**
  927. * This method returns the URL to be used to login.
  928. * or phpCAS::isAuthenticated().
  929. *
  930. * @return the login name of the authenticated user
  931. */
  932. function getServerLoginURL() {
  933. global $PHPCAS_CLIENT;
  934. if (!is_object($PHPCAS_CLIENT)) {
  935. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  936. }
  937. return $PHPCAS_CLIENT->getServerLoginURL();
  938. }
  939. /**
  940. * Set the login URL of the CAS server.
  941. * @param $url the login URL
  942. * @since 0.4.21 by Wyman Chan
  943. */
  944. function setServerLoginURL($url = '') {
  945. global $PHPCAS_CLIENT;
  946. phpCAS :: traceBegin();
  947. if (!is_object($PHPCAS_CLIENT)) {
  948. phpCAS :: error('this method should only be called after
  949. ' . __CLASS__ . '::client()');
  950. }
  951. if (gettype($url) != 'string') {
  952. phpCAS :: error('type mismatched for parameter $url (should be
  953. `string\')');
  954. }
  955. $PHPCAS_CLIENT->setServerLoginURL($url);
  956. phpCAS :: traceEnd();
  957. }
  958. /**
  959. * Set the serviceValidate URL of the CAS server.
  960. * Used only in CAS 1.0 validations
  961. * @param $url the serviceValidate URL
  962. * @since 1.1.0 by Joachim Fritschi
  963. */
  964. function setServerServiceValidateURL($url = '') {
  965. global $PHPCAS_CLIENT;
  966. phpCAS :: traceBegin();
  967. if (!is_object($PHPCAS_CLIENT)) {
  968. phpCAS :: error('this method should only be called after
  969. ' . __CLASS__ . '::client()');
  970. }
  971. if (gettype($url) != 'string') {
  972. phpCAS :: error('type mismatched for parameter $url (should be
  973. `string\')');
  974. }
  975. $PHPCAS_CLIENT->setServerServiceValidateURL($url);
  976. phpCAS :: traceEnd();
  977. }
  978. /**
  979. * Set the proxyValidate URL of the CAS server.
  980. * Used for all CAS 2.0 validations
  981. * @param $url the proxyValidate URL
  982. * @since 1.1.0 by Joachim Fritschi
  983. */
  984. function setServerProxyValidateURL($url = '') {
  985. global $PHPCAS_CLIENT;
  986. phpCAS :: traceBegin();
  987. if (!is_object($PHPCAS_CLIENT)) {
  988. phpCAS :: error('this method should only be called after
  989. ' . __CLASS__ . '::client()');
  990. }
  991. if (gettype($url) != 'string') {
  992. phpCAS :: error('type mismatched for parameter $url (should be
  993. `string\')');
  994. }
  995. $PHPCAS_CLIENT->setServerProxyValidateURL($url);
  996. phpCAS :: traceEnd();
  997. }
  998. /**
  999. * Set the samlValidate URL of the CAS server.
  1000. * @param $url the samlValidate URL
  1001. * @since 1.1.0 by Joachim Fritschi
  1002. */
  1003. function setServerSamlValidateURL($url = '') {
  1004. global $PHPCAS_CLIENT;
  1005. phpCAS :: traceBegin();
  1006. if (!is_object($PHPCAS_CLIENT)) {
  1007. phpCAS :: error('this method should only be called after
  1008. ' . __CLASS__ . '::client()');
  1009. }
  1010. if (gettype($url) != 'string') {
  1011. phpCAS :: error('type mismatched for parameter $url (should be
  1012. `string\')');
  1013. }
  1014. $PHPCAS_CLIENT->setServerSamlValidateURL($url);
  1015. phpCAS :: traceEnd();
  1016. }
  1017. /**
  1018. * This method returns the URL to be used to login.
  1019. * or phpCAS::isAuthenticated().
  1020. *
  1021. * @return the login name of the authenticated user
  1022. */
  1023. function getServerLogoutURL() {
  1024. global $PHPCAS_CLIENT;
  1025. if (!is_object($PHPCAS_CLIENT)) {
  1026. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1027. }
  1028. return $PHPCAS_CLIENT->getServerLogoutURL();
  1029. }
  1030. /**
  1031. * Set the logout URL of the CAS server.
  1032. * @param $url the logout URL
  1033. * @since 0.4.21 by Wyman Chan
  1034. */
  1035. function setServerLogoutURL($url = '') {
  1036. global $PHPCAS_CLIENT;
  1037. phpCAS :: traceBegin();
  1038. if (!is_object($PHPCAS_CLIENT)) {
  1039. phpCAS :: error('this method should only be called after
  1040. ' . __CLASS__ . '::client()');
  1041. }
  1042. if (gettype($url) != 'string') {
  1043. phpCAS :: error('type mismatched for parameter $url (should be
  1044. `string\')');
  1045. }
  1046. $PHPCAS_CLIENT->setServerLogoutURL($url);
  1047. phpCAS :: traceEnd();
  1048. }
  1049. /**
  1050. * This method is used to logout from CAS.
  1051. * @params $params an array that contains the optional url and service parameters that will be passed to the CAS server
  1052. * @public
  1053. */
  1054. function logout($params = "") {
  1055. global $PHPCAS_CLIENT;
  1056. phpCAS :: traceBegin();
  1057. if (!is_object($PHPCAS_CLIENT)) {
  1058. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1059. }
  1060. $parsedParams = array ();
  1061. if ($params != "") {
  1062. if (is_string($params)) {
  1063. phpCAS :: error('method `phpCAS::logout($url)\' is now deprecated, use `phpCAS::logoutWithUrl($url)\' instead');
  1064. }
  1065. if (!is_array($params)) {
  1066. phpCAS :: error('type mismatched for parameter $params (should be `array\')');
  1067. }
  1068. foreach ($params as $key => $value) {
  1069. if ($key != "service" && $key != "url") {
  1070. phpCAS :: error('only `url\' and `service\' parameters are allowed for method `phpCAS::logout($params)\'');
  1071. }
  1072. $parsedParams[$key] = $value;
  1073. }
  1074. }
  1075. $PHPCAS_CLIENT->logout($parsedParams);
  1076. // never reached
  1077. phpCAS :: traceEnd();
  1078. }
  1079. /**
  1080. * This method is used to logout from CAS. Halts by redirecting to the CAS server.
  1081. * @param $service a URL that will be transmitted to the CAS server
  1082. */
  1083. function logoutWithRedirectService($service) {
  1084. global $PHPCAS_CLIENT;
  1085. phpCAS :: traceBegin();
  1086. if (!is_object($PHPCAS_CLIENT)) {
  1087. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1088. }
  1089. if (!is_string($service)) {
  1090. phpCAS :: error('type mismatched for parameter $service (should be `string\')');
  1091. }
  1092. $PHPCAS_CLIENT->logout(array (
  1093. "service" => $service
  1094. ));
  1095. // never reached
  1096. phpCAS :: traceEnd();
  1097. }
  1098. /**
  1099. * This method is used to logout from CAS. Halts by redirecting to the CAS server.
  1100. * @param $url a URL that will be transmitted to the CAS server
  1101. */
  1102. function logoutWithUrl($url) {
  1103. global $PHPCAS_CLIENT;
  1104. phpCAS :: traceBegin();
  1105. if (!is_object($PHPCAS_CLIENT)) {
  1106. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1107. }
  1108. if (!is_string($url)) {
  1109. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  1110. }
  1111. $PHPCAS_CLIENT->logout(array (
  1112. "url" => $url
  1113. ));
  1114. // never reached
  1115. phpCAS :: traceEnd();
  1116. }
  1117. /**
  1118. * This method is used to logout from CAS. Halts by redirecting to the CAS server.
  1119. * @param $service a URL that will be transmitted to the CAS server
  1120. * @param $url a URL that will be transmitted to the CAS server
  1121. */
  1122. function logoutWithRedirectServiceAndUrl($service, $url) {
  1123. global $PHPCAS_CLIENT;
  1124. phpCAS :: traceBegin();
  1125. if (!is_object($PHPCAS_CLIENT)) {
  1126. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1127. }
  1128. if (!is_string($service)) {
  1129. phpCAS :: error('type mismatched for parameter $service (should be `string\')');
  1130. }
  1131. if (!is_string($url)) {
  1132. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  1133. }
  1134. $PHPCAS_CLIENT->logout(array (
  1135. "service" => $service,
  1136. "url" => $url
  1137. ));
  1138. // never reached
  1139. phpCAS :: traceEnd();
  1140. }
  1141. /**
  1142. * Set the fixed URL that will be used by the CAS server to transmit the PGT.
  1143. * When this method is not called, a phpCAS script uses its own URL for the callback.
  1144. *
  1145. * @param $url the URL
  1146. */
  1147. function setFixedCallbackURL($url = '') {
  1148. global $PHPCAS_CLIENT;
  1149. phpCAS :: traceBegin();
  1150. if (!is_object($PHPCAS_CLIENT)) {
  1151. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  1152. }
  1153. if (!$PHPCAS_CLIENT->isProxy()) {
  1154. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  1155. }
  1156. if (gettype($url) != 'string') {
  1157. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  1158. }
  1159. $PHPCAS_CLIENT->setCallbackURL($url);
  1160. phpCAS :: traceEnd();
  1161. }
  1162. /**
  1163. * Set the fixed URL that will be set as the CAS service parameter. When this
  1164. * method is not called, a phpCAS script uses its own URL.
  1165. *
  1166. * @param $url the URL
  1167. */
  1168. function setFixedServiceURL($url) {
  1169. global $PHPCAS_CLIENT;
  1170. phpCAS :: traceBegin();
  1171. if (!is_object($PHPCAS_CLIENT)) {
  1172. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  1173. }
  1174. if (gettype($url) != 'string') {
  1175. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  1176. }
  1177. $PHPCAS_CLIENT->setURL($url);
  1178. phpCAS :: traceEnd();
  1179. }
  1180. /**
  1181. * Get the URL that is set as the CAS service parameter.
  1182. */
  1183. function getServiceURL() {
  1184. global $PHPCAS_CLIENT;
  1185. if (!is_object($PHPCAS_CLIENT)) {
  1186. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  1187. }
  1188. return ($PHPCAS_CLIENT->getURL());
  1189. }
  1190. /**
  1191. * Retrieve a Proxy Ticket from the CAS server.
  1192. */
  1193. function retrievePT($target_service, & $err_code, & $err_msg) {
  1194. global $PHPCAS_CLIENT;
  1195. if (!is_object($PHPCAS_CLIENT)) {
  1196. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  1197. }
  1198. if (gettype($target_service) != 'string') {
  1199. phpCAS :: error('type mismatched for parameter $target_service(should be `string\')');
  1200. }
  1201. return ($PHPCAS_CLIENT->retrievePT($target_service, $err_code, $err_msg));
  1202. }
  1203. /**
  1204. * Set the certificate of the CAS server.
  1205. *
  1206. * @param $cert the PEM certificate
  1207. */
  1208. function setCasServerCert($cert) {
  1209. global $PHPCAS_CLIENT;
  1210. phpCAS :: traceBegin();
  1211. if (!is_object($PHPCAS_CLIENT)) {
  1212. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1213. }
  1214. if (gettype($cert) != 'string') {
  1215. phpCAS :: error('type mismatched for parameter $cert (should be `string\')');
  1216. }
  1217. $PHPCAS_CLIENT->setCasServerCert($cert);
  1218. phpCAS :: traceEnd();
  1219. }
  1220. /**
  1221. * Set the certificate of the CAS server CA.
  1222. *
  1223. * @param $cert the CA certificate
  1224. */
  1225. function setCasServerCACert($cert) {
  1226. global $PHPCAS_CLIENT;
  1227. phpCAS :: traceBegin();
  1228. if (!is_object($PHPCAS_CLIENT)) {
  1229. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1230. }
  1231. if (gettype($cert) != 'string') {
  1232. phpCAS :: error('type mismatched for parameter $cert (should be `string\')');
  1233. }
  1234. $PHPCAS_CLIENT->setCasServerCACert($cert);
  1235. phpCAS :: traceEnd();
  1236. }
  1237. /**
  1238. * Set no SSL validation for the CAS server.
  1239. */
  1240. function setNoCasServerValidation() {
  1241. global $PHPCAS_CLIENT;
  1242. phpCAS :: traceBegin();
  1243. if (!is_object($PHPCAS_CLIENT)) {
  1244. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1245. }
  1246. $PHPCAS_CLIENT->setNoCasServerValidation();
  1247. phpCAS :: traceEnd();
  1248. }
  1249. /** @} */
  1250. /**
  1251. * Change CURL options.
  1252. * CURL is used to connect through HTTPS to CAS server
  1253. * @param $key the option key
  1254. * @param $value the value to set
  1255. */
  1256. function setExtraCurlOption($key, $value) {
  1257. global $PHPCAS_CLIENT;
  1258. phpCAS :: traceBegin();
  1259. if (!is_object($PHPCAS_CLIENT)) {
  1260. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
  1261. }
  1262. $PHPCAS_CLIENT->setExtraCurlOption($key, $value);
  1263. phpCAS :: traceEnd();
  1264. }
  1265. }
  1266. // ########################################################################
  1267. // DOCUMENTATION
  1268. // ########################################################################
  1269. // ########################################################################
  1270. // MAIN PAGE
  1271. /**
  1272. * @mainpage
  1273. *
  1274. * The following pages only show the source documentation.
  1275. *
  1276. */
  1277. // ########################################################################
  1278. // MODULES DEFINITION
  1279. /** @defgroup public User interface */
  1280. /** @defgroup publicInit Initialization
  1281. * @ingroup public */
  1282. /** @defgroup publicAuth Authentication
  1283. * @ingroup public */
  1284. /** @defgroup publicServices Access to external services
  1285. * @ingroup public */
  1286. /** @defgroup publicConfig Configuration
  1287. * @ingroup public */
  1288. /** @defgroup publicLang Internationalization
  1289. * @ingroup publicConfig */
  1290. /** @defgroup publicOutput HTML output
  1291. * @ingroup publicConfig */
  1292. /** @defgroup publicPGTStorage PGT storage
  1293. * @ingroup publicConfig */
  1294. /** @defgroup publicDebug Debugging
  1295. * @ingroup public */
  1296. /** @defgroup internal Implementation */
  1297. /** @defgroup internalAuthentication Authentication
  1298. * @ingroup internal */
  1299. /** @defgroup internalBasic CAS Basic client features (CAS 1.0, Service Tickets)
  1300. * @ingroup internal */
  1301. /** @defgroup internalProxy CAS Proxy features (CAS 2.0, Proxy Granting Tickets)
  1302. * @ingroup internal */
  1303. /** @defgroup internalPGTStorage PGT storage
  1304. * @ingroup internalProxy */
  1305. /** @defgroup internalPGTStorageFile PGT storage on the filesystem
  1306. * @ingroup internalPGTStorage */
  1307. /** @defgroup internalCallback Callback from the CAS server
  1308. * @ingroup internalProxy */
  1309. /** @defgroup internalProxied CAS proxied client features (CAS 2.0, Proxy Tickets)
  1310. * @ingroup internal */
  1311. /** @defgroup internalConfig Configuration
  1312. * @ingroup internal */
  1313. /** @defgroup internalOutput HTML output
  1314. * @ingroup internalConfig */
  1315. /** @defgroup internalLang Internationalization
  1316. * @ingroup internalConfig
  1317. *
  1318. * To add a new language:
  1319. * - 1. define a new constant PHPCAS_LANG_XXXXXX in CAS/CAS.php
  1320. * - 2. copy any file from CAS/languages to CAS/languages/XXXXXX.php
  1321. * - 3. Make the translations
  1322. */
  1323. /** @defgroup internalDebug Debugging
  1324. * @ingroup internal */
  1325. /** @defgroup internalMisc Miscellaneous
  1326. * @ingroup internal */
  1327. // ########################################################################
  1328. // EXAMPLES
  1329. /**
  1330. * @example example_simple.php
  1331. */
  1332. /**
  1333. * @example example_proxy.php
  1334. */
  1335. /**
  1336. * @example example_proxy2.php
  1337. */
  1338. /**
  1339. * @example example_lang.php
  1340. */
  1341. /**
  1342. * @example example_html.php
  1343. */
  1344. /**
  1345. * @example example_file.php
  1346. */
  1347. /**
  1348. * @example example_db.php
  1349. */
  1350. /**
  1351. * @example example_service.php
  1352. */
  1353. /**
  1354. * @example example_session_proxy.php
  1355. */
  1356. /**
  1357. * @example example_session_service.php
  1358. */
  1359. /**
  1360. * @example example_gateway.php
  1361. */
  1362. /**
  1363. * @example example_custom_urls.php
  1364. */
  1365. ?>