PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/auth/cas/CAS/CAS.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 1980 lines | 847 code | 179 blank | 954 comment | 195 complexity | c88fc046446b19acf7bebde15bb59150 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0

Large files files are truncated, but you can click here to view the full 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. *
  20. *
  21. * Interface class of the phpCAS library
  22. * PHP Version 5
  23. *
  24. * @file CAS/CAS.php
  25. * @category Authentication
  26. * @package PhpCAS
  27. * @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
  28. * @author Olivier Berger <olivier.berger@it-sudparis.eu>
  29. * @author Brett Bieber <brett.bieber@gmail.com>
  30. * @author Joachim Fritschi <jfritschi@freenet.de>
  31. * @author Adam Franco <afranco@middlebury.edu>
  32. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  33. * @link https://wiki.jasig.org/display/CASC/phpCAS
  34. * @ingroup public
  35. */
  36. //
  37. // hack by Vangelis Haniotakis to handle the absence of $_SERVER['REQUEST_URI']
  38. // in IIS
  39. //
  40. if (php_sapi_name() != 'cli') {
  41. if (!isset($_SERVER['REQUEST_URI'])) {
  42. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
  43. }
  44. }
  45. // Add a E_USER_DEPRECATED for php versions <= 5.2
  46. if (!defined('E_USER_DEPRECATED')) {
  47. define('E_USER_DEPRECATED', E_USER_NOTICE);
  48. }
  49. // ########################################################################
  50. // CONSTANTS
  51. // ########################################################################
  52. // ------------------------------------------------------------------------
  53. // CAS VERSIONS
  54. // ------------------------------------------------------------------------
  55. /**
  56. * phpCAS version. accessible for the user by phpCAS::getVersion().
  57. */
  58. define('PHPCAS_VERSION', '1.3.2');
  59. /**
  60. * @addtogroup public
  61. * @{
  62. */
  63. /**
  64. * CAS version 1.0
  65. */
  66. define("CAS_VERSION_1_0", '1.0');
  67. /*!
  68. * CAS version 2.0
  69. */
  70. define("CAS_VERSION_2_0", '2.0');
  71. // ------------------------------------------------------------------------
  72. // SAML defines
  73. // ------------------------------------------------------------------------
  74. /**
  75. * SAML protocol
  76. */
  77. define("SAML_VERSION_1_1", 'S1');
  78. /**
  79. * XML header for SAML POST
  80. */
  81. define("SAML_XML_HEADER", '<?xml version="1.0" encoding="UTF-8"?>');
  82. /**
  83. * SOAP envelope for SAML POST
  84. */
  85. define("SAML_SOAP_ENV", '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/>');
  86. /**
  87. * SOAP body for SAML POST
  88. */
  89. define("SAML_SOAP_BODY", '<SOAP-ENV:Body>');
  90. /**
  91. * SAMLP request
  92. */
  93. 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">');
  94. define("SAMLP_REQUEST_CLOSE", '</samlp:Request>');
  95. /**
  96. * SAMLP artifact tag (for the ticket)
  97. */
  98. define("SAML_ASSERTION_ARTIFACT", '<samlp:AssertionArtifact>');
  99. /**
  100. * SAMLP close
  101. */
  102. define("SAML_ASSERTION_ARTIFACT_CLOSE", '</samlp:AssertionArtifact>');
  103. /**
  104. * SOAP body close
  105. */
  106. define("SAML_SOAP_BODY_CLOSE", '</SOAP-ENV:Body>');
  107. /**
  108. * SOAP envelope close
  109. */
  110. define("SAML_SOAP_ENV_CLOSE", '</SOAP-ENV:Envelope>');
  111. /**
  112. * SAML Attributes
  113. */
  114. define("SAML_ATTRIBUTES", 'SAMLATTRIBS');
  115. /** @} */
  116. /**
  117. * @addtogroup publicPGTStorage
  118. * @{
  119. */
  120. // ------------------------------------------------------------------------
  121. // FILE PGT STORAGE
  122. // ------------------------------------------------------------------------
  123. /**
  124. * Default path used when storing PGT's to file
  125. */
  126. define("CAS_PGT_STORAGE_FILE_DEFAULT_PATH", session_save_path());
  127. /** @} */
  128. // ------------------------------------------------------------------------
  129. // SERVICE ACCESS ERRORS
  130. // ------------------------------------------------------------------------
  131. /**
  132. * @addtogroup publicServices
  133. * @{
  134. */
  135. /**
  136. * phpCAS::service() error code on success
  137. */
  138. define("PHPCAS_SERVICE_OK", 0);
  139. /**
  140. * phpCAS::service() error code when the PT could not retrieve because
  141. * the CAS server did not respond.
  142. */
  143. define("PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE", 1);
  144. /**
  145. * phpCAS::service() error code when the PT could not retrieve because
  146. * the response of the CAS server was ill-formed.
  147. */
  148. define("PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE", 2);
  149. /**
  150. * phpCAS::service() error code when the PT could not retrieve because
  151. * the CAS server did not want to.
  152. */
  153. define("PHPCAS_SERVICE_PT_FAILURE", 3);
  154. /**
  155. * phpCAS::service() error code when the service was not available.
  156. */
  157. define("PHPCAS_SERVICE_NOT_AVAILABLE", 4);
  158. // ------------------------------------------------------------------------
  159. // SERVICE TYPES
  160. // ------------------------------------------------------------------------
  161. /**
  162. * phpCAS::getProxiedService() type for HTTP GET
  163. */
  164. define("PHPCAS_PROXIED_SERVICE_HTTP_GET", 'CAS_ProxiedService_Http_Get');
  165. /**
  166. * phpCAS::getProxiedService() type for HTTP POST
  167. */
  168. define("PHPCAS_PROXIED_SERVICE_HTTP_POST", 'CAS_ProxiedService_Http_Post');
  169. /**
  170. * phpCAS::getProxiedService() type for IMAP
  171. */
  172. define("PHPCAS_PROXIED_SERVICE_IMAP", 'CAS_ProxiedService_Imap');
  173. /** @} */
  174. // ------------------------------------------------------------------------
  175. // LANGUAGES
  176. // ------------------------------------------------------------------------
  177. /**
  178. * @addtogroup publicLang
  179. * @{
  180. */
  181. define("PHPCAS_LANG_ENGLISH", 'CAS_Languages_English');
  182. define("PHPCAS_LANG_FRENCH", 'CAS_Languages_French');
  183. define("PHPCAS_LANG_GREEK", 'CAS_Languages_Greek');
  184. define("PHPCAS_LANG_GERMAN", 'CAS_Languages_German');
  185. define("PHPCAS_LANG_JAPANESE", 'CAS_Languages_Japanese');
  186. define("PHPCAS_LANG_SPANISH", 'CAS_Languages_Spanish');
  187. define("PHPCAS_LANG_CATALAN", 'CAS_Languages_Catalan');
  188. /** @} */
  189. /**
  190. * @addtogroup internalLang
  191. * @{
  192. */
  193. /**
  194. * phpCAS default language (when phpCAS::setLang() is not used)
  195. */
  196. define("PHPCAS_LANG_DEFAULT", PHPCAS_LANG_ENGLISH);
  197. /** @} */
  198. // ------------------------------------------------------------------------
  199. // DEBUG
  200. // ------------------------------------------------------------------------
  201. /**
  202. * @addtogroup publicDebug
  203. * @{
  204. */
  205. /**
  206. * The default directory for the debug file under Unix.
  207. */
  208. define('DEFAULT_DEBUG_DIR', '/tmp/');
  209. /** @} */
  210. // include the class autoloader
  211. require_once dirname(__FILE__) . '/CAS/Autoload.php';
  212. /**
  213. * The phpCAS class is a simple container for the phpCAS library. It provides CAS
  214. * authentication for web applications written in PHP.
  215. *
  216. * @ingroup public
  217. * @class phpCAS
  218. * @category Authentication
  219. * @package PhpCAS
  220. * @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
  221. * @author Olivier Berger <olivier.berger@it-sudparis.eu>
  222. * @author Brett Bieber <brett.bieber@gmail.com>
  223. * @author Joachim Fritschi <jfritschi@freenet.de>
  224. * @author Adam Franco <afranco@middlebury.edu>
  225. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  226. * @link https://wiki.jasig.org/display/CASC/phpCAS
  227. */
  228. class phpCAS
  229. {
  230. /**
  231. * This variable is used by the interface class phpCAS.
  232. *
  233. * @hideinitializer
  234. */
  235. private static $_PHPCAS_CLIENT;
  236. /**
  237. * This variable is used to store where the initializer is called from
  238. * (to print a comprehensive error in case of multiple calls).
  239. *
  240. * @hideinitializer
  241. */
  242. private static $_PHPCAS_INIT_CALL;
  243. /**
  244. * This variable is used to store phpCAS debug mode.
  245. *
  246. * @hideinitializer
  247. */
  248. private static $_PHPCAS_DEBUG;
  249. // ########################################################################
  250. // INITIALIZATION
  251. // ########################################################################
  252. /**
  253. * @addtogroup publicInit
  254. * @{
  255. */
  256. /**
  257. * phpCAS client initializer.
  258. *
  259. * @param string $server_version the version of the CAS server
  260. * @param string $server_hostname the hostname of the CAS server
  261. * @param string $server_port the port the CAS server is running on
  262. * @param string $server_uri the URI the CAS server is responding on
  263. * @param bool $changeSessionID Allow phpCAS to change the session_id (Single
  264. * Sign Out/handleLogoutRequests is based on that change)
  265. *
  266. * @return a newly created CAS_Client object
  267. * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
  268. * called, only once, and before all other methods (except phpCAS::getVersion()
  269. * and phpCAS::setDebug()).
  270. */
  271. public static function client($server_version, $server_hostname,
  272. $server_port, $server_uri, $changeSessionID = true
  273. ) {
  274. phpCAS :: traceBegin();
  275. if (is_object(self::$_PHPCAS_CLIENT)) {
  276. phpCAS :: error(self::$_PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . self::$_PHPCAS_INIT_CALL['file'] . ':' . self::$_PHPCAS_INIT_CALL['line'] . ')');
  277. }
  278. if (gettype($server_version) != 'string') {
  279. phpCAS :: error('type mismatched for parameter $server_version (should be `string\')');
  280. }
  281. if (gettype($server_hostname) != 'string') {
  282. phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')');
  283. }
  284. if (gettype($server_port) != 'integer') {
  285. phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')');
  286. }
  287. if (gettype($server_uri) != 'string') {
  288. phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')');
  289. }
  290. // store where the initializer is called from
  291. $dbg = debug_backtrace();
  292. self::$_PHPCAS_INIT_CALL = array (
  293. 'done' => true,
  294. 'file' => $dbg[0]['file'],
  295. 'line' => $dbg[0]['line'],
  296. 'method' => __CLASS__ . '::' . __FUNCTION__
  297. );
  298. // initialize the object $_PHPCAS_CLIENT
  299. self::$_PHPCAS_CLIENT = new CAS_Client(
  300. $server_version, false, $server_hostname, $server_port, $server_uri,
  301. $changeSessionID
  302. );
  303. phpCAS :: traceEnd();
  304. }
  305. /**
  306. * phpCAS proxy initializer.
  307. *
  308. * @param string $server_version the version of the CAS server
  309. * @param string $server_hostname the hostname of the CAS server
  310. * @param string $server_port the port the CAS server is running on
  311. * @param string $server_uri the URI the CAS server is responding on
  312. * @param bool $changeSessionID Allow phpCAS to change the session_id (Single
  313. * Sign Out/handleLogoutRequests is based on that change)
  314. *
  315. * @return a newly created CAS_Client object
  316. * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
  317. * called, only once, and before all other methods (except phpCAS::getVersion()
  318. * and phpCAS::setDebug()).
  319. */
  320. public static function proxy($server_version, $server_hostname,
  321. $server_port, $server_uri, $changeSessionID = true
  322. ) {
  323. phpCAS :: traceBegin();
  324. if (is_object(self::$_PHPCAS_CLIENT)) {
  325. phpCAS :: error(self::$_PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . self::$_PHPCAS_INIT_CALL['file'] . ':' . self::$_PHPCAS_INIT_CALL['line'] . ')');
  326. }
  327. if (gettype($server_version) != 'string') {
  328. phpCAS :: error('type mismatched for parameter $server_version (should be `string\')');
  329. }
  330. if (gettype($server_hostname) != 'string') {
  331. phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')');
  332. }
  333. if (gettype($server_port) != 'integer') {
  334. phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')');
  335. }
  336. if (gettype($server_uri) != 'string') {
  337. phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')');
  338. }
  339. // store where the initialzer is called from
  340. $dbg = debug_backtrace();
  341. self::$_PHPCAS_INIT_CALL = array (
  342. 'done' => true,
  343. 'file' => $dbg[0]['file'],
  344. 'line' => $dbg[0]['line'],
  345. 'method' => __CLASS__ . '::' . __FUNCTION__
  346. );
  347. // initialize the object $_PHPCAS_CLIENT
  348. self::$_PHPCAS_CLIENT = new CAS_Client(
  349. $server_version, true, $server_hostname, $server_port, $server_uri,
  350. $changeSessionID
  351. );
  352. phpCAS :: traceEnd();
  353. }
  354. /** @} */
  355. // ########################################################################
  356. // DEBUGGING
  357. // ########################################################################
  358. /**
  359. * @addtogroup publicDebug
  360. * @{
  361. */
  362. /**
  363. * Set/unset debug mode
  364. *
  365. * @param string $filename the name of the file used for logging, or false
  366. * to stop debugging.
  367. *
  368. * @return void
  369. */
  370. public static function setDebug($filename = '')
  371. {
  372. if ($filename != false && gettype($filename) != 'string') {
  373. phpCAS :: error('type mismatched for parameter $dbg (should be false or the name of the log file)');
  374. }
  375. if ($filename === false) {
  376. self::$_PHPCAS_DEBUG['filename'] = false;
  377. } else {
  378. if (empty ($filename)) {
  379. if (preg_match('/^Win.*/', getenv('OS'))) {
  380. if (isset ($_ENV['TMP'])) {
  381. $debugDir = $_ENV['TMP'] . '/';
  382. } else {
  383. $debugDir = '';
  384. }
  385. } else {
  386. $debugDir = DEFAULT_DEBUG_DIR;
  387. }
  388. $filename = $debugDir . 'phpCAS.log';
  389. }
  390. if (empty (self::$_PHPCAS_DEBUG['unique_id'])) {
  391. self::$_PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))), 0, 4);
  392. }
  393. self::$_PHPCAS_DEBUG['filename'] = $filename;
  394. self::$_PHPCAS_DEBUG['indent'] = 0;
  395. phpCAS :: trace('START phpCAS-' . PHPCAS_VERSION . ' ******************');
  396. }
  397. }
  398. /**
  399. * Logs a string in debug mode.
  400. *
  401. * @param string $str the string to write
  402. *
  403. * @return void
  404. * @private
  405. */
  406. public static function log($str)
  407. {
  408. $indent_str = ".";
  409. if (!empty(self::$_PHPCAS_DEBUG['filename'])) {
  410. // Check if file exists and modifiy file permissions to be only
  411. // readable by the webserver
  412. if (!file_exists(self::$_PHPCAS_DEBUG['filename'])) {
  413. touch(self::$_PHPCAS_DEBUG['filename']);
  414. // Chmod will fail on windows
  415. @chmod(self::$_PHPCAS_DEBUG['filename'], 0600);
  416. }
  417. for ($i = 0; $i < self::$_PHPCAS_DEBUG['indent']; $i++) {
  418. $indent_str .= '| ';
  419. }
  420. // allow for multiline output with proper identing. Usefull for
  421. // dumping cas answers etc.
  422. $str2 = str_replace("\n", "\n" . self::$_PHPCAS_DEBUG['unique_id'] . ' ' . $indent_str, $str);
  423. error_log(self::$_PHPCAS_DEBUG['unique_id'] . ' ' . $indent_str . $str2 . "\n", 3, self::$_PHPCAS_DEBUG['filename']);
  424. }
  425. }
  426. /**
  427. * This method is used by interface methods to print an error and where the
  428. * function was originally called from.
  429. *
  430. * @param string $msg the message to print
  431. *
  432. * @return void
  433. * @private
  434. */
  435. public static function error($msg)
  436. {
  437. $dbg = debug_backtrace();
  438. $function = '?';
  439. $file = '?';
  440. $line = '?';
  441. if (is_array($dbg)) {
  442. for ($i = 1; $i < sizeof($dbg); $i++) {
  443. if (is_array($dbg[$i]) && isset($dbg[$i]['class']) ) {
  444. if ($dbg[$i]['class'] == __CLASS__) {
  445. $function = $dbg[$i]['function'];
  446. $file = $dbg[$i]['file'];
  447. $line = $dbg[$i]['line'];
  448. }
  449. }
  450. }
  451. }
  452. 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";
  453. phpCAS :: trace($msg);
  454. phpCAS :: traceEnd();
  455. throw new CAS_GracefullTerminationException(__CLASS__ . "::" . $function . '(): ' . $msg);
  456. }
  457. /**
  458. * This method is used to log something in debug mode.
  459. *
  460. * @param string $str string to log
  461. *
  462. * @return void
  463. */
  464. public static function trace($str)
  465. {
  466. $dbg = debug_backtrace();
  467. phpCAS :: log($str . ' [' . basename($dbg[0]['file']) . ':' . $dbg[0]['line'] . ']');
  468. }
  469. /**
  470. * This method is used to indicate the start of the execution of a function in debug mode.
  471. *
  472. * @return void
  473. */
  474. public static function traceBegin()
  475. {
  476. $dbg = debug_backtrace();
  477. $str = '=> ';
  478. if (!empty ($dbg[1]['class'])) {
  479. $str .= $dbg[1]['class'] . '::';
  480. }
  481. $str .= $dbg[1]['function'] . '(';
  482. if (is_array($dbg[1]['args'])) {
  483. foreach ($dbg[1]['args'] as $index => $arg) {
  484. if ($index != 0) {
  485. $str .= ', ';
  486. }
  487. if (is_object($arg)) {
  488. $str .= get_class($arg);
  489. } else {
  490. $str .= str_replace(array("\r\n", "\n", "\r"), "", var_export($arg, true));
  491. }
  492. }
  493. }
  494. if (isset($dbg[1]['file'])) {
  495. $file = basename($dbg[1]['file']);
  496. } else {
  497. $file = 'unknown_file';
  498. }
  499. if (isset($dbg[1]['line'])) {
  500. $line = $dbg[1]['line'];
  501. } else {
  502. $line = 'unknown_line';
  503. }
  504. $str .= ') [' . $file . ':' . $line . ']';
  505. phpCAS :: log($str);
  506. if (!isset(self::$_PHPCAS_DEBUG['indent'])) {
  507. self::$_PHPCAS_DEBUG['indent'] = 0;
  508. } else {
  509. self::$_PHPCAS_DEBUG['indent']++;
  510. }
  511. }
  512. /**
  513. * This method is used to indicate the end of the execution of a function in
  514. * debug mode.
  515. *
  516. * @param string $res the result of the function
  517. *
  518. * @return void
  519. */
  520. public static function traceEnd($res = '')
  521. {
  522. if (empty(self::$_PHPCAS_DEBUG['indent'])) {
  523. self::$_PHPCAS_DEBUG['indent'] = 0;
  524. } else {
  525. self::$_PHPCAS_DEBUG['indent']--;
  526. }
  527. $dbg = debug_backtrace();
  528. $str = '';
  529. if (is_object($res)) {
  530. $str .= '<= ' . get_class($res);
  531. } else {
  532. $str .= '<= ' . str_replace(array("\r\n", "\n", "\r"), "", var_export($res, true));
  533. }
  534. phpCAS :: log($str);
  535. }
  536. /**
  537. * This method is used to indicate the end of the execution of the program
  538. *
  539. * @return void
  540. */
  541. public static function traceExit()
  542. {
  543. phpCAS :: log('exit()');
  544. while (self::$_PHPCAS_DEBUG['indent'] > 0) {
  545. phpCAS :: log('-');
  546. self::$_PHPCAS_DEBUG['indent']--;
  547. }
  548. }
  549. /** @} */
  550. // ########################################################################
  551. // INTERNATIONALIZATION
  552. // ########################################################################
  553. /**
  554. * @addtogroup publicLang
  555. * @{
  556. */
  557. /**
  558. * This method is used to set the language used by phpCAS.
  559. *
  560. * @param string $lang string representing the language.
  561. *
  562. * @return void
  563. *
  564. * @sa PHPCAS_LANG_FRENCH, PHPCAS_LANG_ENGLISH
  565. * @note Can be called only once.
  566. */
  567. public static function setLang($lang)
  568. {
  569. if (!is_object(self::$_PHPCAS_CLIENT)) {
  570. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  571. }
  572. if (gettype($lang) != 'string') {
  573. phpCAS :: error('type mismatched for parameter $lang (should be `string\')');
  574. }
  575. self::$_PHPCAS_CLIENT->setLang($lang);
  576. }
  577. /** @} */
  578. // ########################################################################
  579. // VERSION
  580. // ########################################################################
  581. /**
  582. * @addtogroup public
  583. * @{
  584. */
  585. /**
  586. * This method returns the phpCAS version.
  587. *
  588. * @return the phpCAS version.
  589. */
  590. public static function getVersion()
  591. {
  592. return PHPCAS_VERSION;
  593. }
  594. /** @} */
  595. // ########################################################################
  596. // HTML OUTPUT
  597. // ########################################################################
  598. /**
  599. * @addtogroup publicOutput
  600. * @{
  601. */
  602. /**
  603. * This method sets the HTML header used for all outputs.
  604. *
  605. * @param string $header the HTML header.
  606. *
  607. * @return void
  608. */
  609. public static function setHTMLHeader($header)
  610. {
  611. if (!is_object(self::$_PHPCAS_CLIENT)) {
  612. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  613. }
  614. if (gettype($header) != 'string') {
  615. phpCAS :: error('type mismatched for parameter $header (should be `string\')');
  616. }
  617. self::$_PHPCAS_CLIENT->setHTMLHeader($header);
  618. }
  619. /**
  620. * This method sets the HTML footer used for all outputs.
  621. *
  622. * @param string $footer the HTML footer.
  623. *
  624. * @return void
  625. */
  626. public static function setHTMLFooter($footer)
  627. {
  628. if (!is_object(self::$_PHPCAS_CLIENT)) {
  629. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  630. }
  631. if (gettype($footer) != 'string') {
  632. phpCAS :: error('type mismatched for parameter $footer (should be `string\')');
  633. }
  634. self::$_PHPCAS_CLIENT->setHTMLFooter($footer);
  635. }
  636. /** @} */
  637. // ########################################################################
  638. // PGT STORAGE
  639. // ########################################################################
  640. /**
  641. * @addtogroup publicPGTStorage
  642. * @{
  643. */
  644. /**
  645. * This method can be used to set a custom PGT storage object.
  646. *
  647. * @param CAS_PGTStorage $storage a PGT storage object that inherits from the
  648. * CAS_PGTStorage class
  649. *
  650. * @return void
  651. */
  652. public static function setPGTStorage($storage)
  653. {
  654. phpCAS :: traceBegin();
  655. if (!is_object(self::$_PHPCAS_CLIENT)) {
  656. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  657. }
  658. if (!self::$_PHPCAS_CLIENT->isProxy()) {
  659. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  660. }
  661. if (self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  662. phpCAS :: error('this method should only be called before ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() (called at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ')');
  663. }
  664. if ( !($storage instanceof CAS_PGTStorage) ) {
  665. phpCAS :: error('type mismatched for parameter $storage (should be a CAS_PGTStorage `object\')');
  666. }
  667. self::$_PHPCAS_CLIENT->setPGTStorage($storage);
  668. phpCAS :: traceEnd();
  669. }
  670. /**
  671. * This method is used to tell phpCAS to store the response of the
  672. * CAS server to PGT requests in a database.
  673. *
  674. * @param string $dsn_or_pdo a dsn string to use for creating a PDO
  675. * object or a PDO object
  676. * @param string $username the username to use when connecting to the
  677. * database
  678. * @param string $password the password to use when connecting to the
  679. * database
  680. * @param string $table the table to use for storing and retrieving
  681. * PGT's
  682. * @param string $driver_options any driver options to use when connecting
  683. * to the database
  684. *
  685. * @return void
  686. */
  687. public static function setPGTStorageDb($dsn_or_pdo, $username='',
  688. $password='', $table='', $driver_options=null
  689. ) {
  690. phpCAS :: traceBegin();
  691. if (!is_object(self::$_PHPCAS_CLIENT)) {
  692. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  693. }
  694. if (!self::$_PHPCAS_CLIENT->isProxy()) {
  695. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  696. }
  697. if (self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  698. phpCAS :: error('this method should only be called before ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() (called at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ')');
  699. }
  700. if (gettype($username) != 'string') {
  701. phpCAS :: error('type mismatched for parameter $username (should be `string\')');
  702. }
  703. if (gettype($password) != 'string') {
  704. phpCAS :: error('type mismatched for parameter $password (should be `string\')');
  705. }
  706. if (gettype($table) != 'string') {
  707. phpCAS :: error('type mismatched for parameter $table (should be `string\')');
  708. }
  709. self::$_PHPCAS_CLIENT->setPGTStorageDb($dsn_or_pdo, $username, $password, $table, $driver_options);
  710. phpCAS :: traceEnd();
  711. }
  712. /**
  713. * This method is used to tell phpCAS to store the response of the
  714. * CAS server to PGT requests onto the filesystem.
  715. *
  716. * @param string $path the path where the PGT's should be stored
  717. *
  718. * @return void
  719. */
  720. public static function setPGTStorageFile($path = '')
  721. {
  722. phpCAS :: traceBegin();
  723. if (!is_object(self::$_PHPCAS_CLIENT)) {
  724. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  725. }
  726. if (!self::$_PHPCAS_CLIENT->isProxy()) {
  727. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  728. }
  729. if (self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  730. phpCAS :: error('this method should only be called before ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() (called at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ')');
  731. }
  732. if (gettype($path) != 'string') {
  733. phpCAS :: error('type mismatched for parameter $path (should be `string\')');
  734. }
  735. self::$_PHPCAS_CLIENT->setPGTStorageFile($path);
  736. phpCAS :: traceEnd();
  737. }
  738. /** @} */
  739. // ########################################################################
  740. // ACCESS TO EXTERNAL SERVICES
  741. // ########################################################################
  742. /**
  743. * @addtogroup publicServices
  744. * @{
  745. */
  746. /**
  747. * Answer a proxy-authenticated service handler.
  748. *
  749. * @param string $type The service type. One of
  750. * PHPCAS_PROXIED_SERVICE_HTTP_GET; PHPCAS_PROXIED_SERVICE_HTTP_POST;
  751. * PHPCAS_PROXIED_SERVICE_IMAP
  752. *
  753. * @return CAS_ProxiedService
  754. * @throws InvalidArgumentException If the service type is unknown.
  755. */
  756. public static function getProxiedService ($type)
  757. {
  758. phpCAS :: traceBegin();
  759. if (!is_object(self::$_PHPCAS_CLIENT)) {
  760. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  761. }
  762. if (!self::$_PHPCAS_CLIENT->isProxy()) {
  763. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  764. }
  765. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  766. 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()');
  767. }
  768. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  769. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  770. }
  771. if (gettype($type) != 'string') {
  772. phpCAS :: error('type mismatched for parameter $type (should be `string\')');
  773. }
  774. $res = self::$_PHPCAS_CLIENT->getProxiedService($type);
  775. phpCAS :: traceEnd();
  776. return $res;
  777. }
  778. /**
  779. * Initialize a proxied-service handler with the proxy-ticket it should use.
  780. *
  781. * @param CAS_ProxiedService $proxiedService Proxied Service Handler
  782. *
  783. * @return void
  784. * @throws CAS_ProxyTicketException If there is a proxy-ticket failure.
  785. * The code of the Exception will be one of:
  786. * PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE
  787. * PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE
  788. * PHPCAS_SERVICE_PT_FAILURE
  789. */
  790. public static function initializeProxiedService (CAS_ProxiedService $proxiedService)
  791. {
  792. if (!is_object(self::$_PHPCAS_CLIENT)) {
  793. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  794. }
  795. if (!self::$_PHPCAS_CLIENT->isProxy()) {
  796. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  797. }
  798. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  799. 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()');
  800. }
  801. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  802. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  803. }
  804. self::$_PHPCAS_CLIENT->initializeProxiedService($proxiedService);
  805. }
  806. /**
  807. * This method is used to access an HTTP[S] service.
  808. *
  809. * @param string $url the service to access.
  810. * @param string &$err_code an error code Possible values are
  811. * PHPCAS_SERVICE_OK (on success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE,
  812. * PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE, PHPCAS_SERVICE_PT_FAILURE,
  813. * PHPCAS_SERVICE_NOT_AVAILABLE.
  814. * @param string &$output the output of the service (also used to give an
  815. * error message on failure).
  816. *
  817. * @return bool true on success, false otherwise (in this later case,
  818. * $err_code gives the reason why it failed and $output contains an error
  819. * message).
  820. */
  821. public static function serviceWeb($url, & $err_code, & $output)
  822. {
  823. phpCAS :: traceBegin();
  824. if (!is_object(self::$_PHPCAS_CLIENT)) {
  825. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  826. }
  827. if (!self::$_PHPCAS_CLIENT->isProxy()) {
  828. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  829. }
  830. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  831. 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()');
  832. }
  833. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  834. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  835. }
  836. if (gettype($url) != 'string') {
  837. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  838. }
  839. $res = self::$_PHPCAS_CLIENT->serviceWeb($url, $err_code, $output);
  840. phpCAS :: traceEnd($res);
  841. return $res;
  842. }
  843. /**
  844. * This method is used to access an IMAP/POP3/NNTP service.
  845. *
  846. * @param string $url a string giving the URL of the service,
  847. * including the mailing box for IMAP URLs, as accepted by imap_open().
  848. * @param string $service a string giving for CAS retrieve Proxy ticket
  849. * @param string $flags options given to imap_open().
  850. * @param string &$err_code an error code Possible values are
  851. * PHPCAS_SERVICE_OK (on success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE,
  852. * PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE, PHPCAS_SERVICE_PT_FAILURE,
  853. * PHPCAS_SERVICE_NOT_AVAILABLE.
  854. * @param string &$err_msg an error message on failure
  855. * @param string &$pt the Proxy Ticket (PT) retrieved from the CAS
  856. * server to access the URL on success, false on error).
  857. *
  858. * @return object IMAP stream on success, false otherwise (in this later
  859. * case, $err_code gives the reason why it failed and $err_msg contains an
  860. * error message).
  861. */
  862. public static function serviceMail($url, $service, $flags, & $err_code, & $err_msg, & $pt)
  863. {
  864. phpCAS :: traceBegin();
  865. if (!is_object(self::$_PHPCAS_CLIENT)) {
  866. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  867. }
  868. if (!self::$_PHPCAS_CLIENT->isProxy()) {
  869. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
  870. }
  871. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  872. 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()');
  873. }
  874. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  875. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  876. }
  877. if (gettype($url) != 'string') {
  878. phpCAS :: error('type mismatched for parameter $url (should be `string\')');
  879. }
  880. if (gettype($flags) != 'integer') {
  881. phpCAS :: error('type mismatched for parameter $flags (should be `integer\')');
  882. }
  883. $res = self::$_PHPCAS_CLIENT->serviceMail($url, $service, $flags, $err_code, $err_msg, $pt);
  884. phpCAS :: traceEnd($res);
  885. return $res;
  886. }
  887. /** @} */
  888. // ########################################################################
  889. // AUTHENTICATION
  890. // ########################################################################
  891. /**
  892. * @addtogroup publicAuth
  893. * @{
  894. */
  895. /**
  896. * Set the times authentication will be cached before really accessing the
  897. * CAS server in gateway mode:
  898. * - -1: check only once, and then never again (until you pree login)
  899. * - 0: always check
  900. * - n: check every "n" time
  901. *
  902. * @param int $n an integer.
  903. *
  904. * @return void
  905. */
  906. public static function setCacheTimesForAuthRecheck($n)
  907. {
  908. if (!is_object(self::$_PHPCAS_CLIENT)) {
  909. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  910. }
  911. if (gettype($n) != 'integer') {
  912. phpCAS :: error('type mismatched for parameter $n (should be `integer\')');
  913. }
  914. self::$_PHPCAS_CLIENT->setCacheTimesForAuthRecheck($n);
  915. }
  916. /**
  917. * Set a callback function to be run when a user authenticates.
  918. *
  919. * The callback function will be passed a $logoutTicket as its first
  920. * parameter, followed by any $additionalArgs you pass. The $logoutTicket
  921. * parameter is an opaque string that can be used to map the session-id to
  922. * logout request in order to support single-signout in applications that
  923. * manage their own sessions (rather than letting phpCAS start the session).
  924. *
  925. * phpCAS::forceAuthentication() will always exit and forward client unless
  926. * they are already authenticated. To perform an action at the moment the user
  927. * logs in (such as registering an account, performing logging, etc), register
  928. * a callback function here.
  929. *
  930. * @param string $function Callback function
  931. * @param array $additionalArgs optional array of arguments
  932. *
  933. * @return void
  934. */
  935. public static function setPostAuthenticateCallback ($function, array $additionalArgs = array())
  936. {
  937. if (!is_object(self::$_PHPCAS_CLIENT)) {
  938. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  939. }
  940. self::$_PHPCAS_CLIENT->setPostAuthenticateCallback($function, $additionalArgs);
  941. }
  942. /**
  943. * Set a callback function to be run when a single-signout request is
  944. * received. The callback function will be passed a $logoutTicket as its
  945. * first parameter, followed by any $additionalArgs you pass. The
  946. * $logoutTicket parameter is an opaque string that can be used to map a
  947. * session-id to the logout request in order to support single-signout in
  948. * applications that manage their own sessions (rather than letting phpCAS
  949. * start and destroy the session).
  950. *
  951. * @param string $function Callback function
  952. * @param array $additionalArgs optional array of arguments
  953. *
  954. * @return void
  955. */
  956. public static function setSingleSignoutCallback ($function, array $additionalArgs = array())
  957. {
  958. if (!is_object(self::$_PHPCAS_CLIENT)) {
  959. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  960. }
  961. self::$_PHPCAS_CLIENT->setSingleSignoutCallback($function, $additionalArgs);
  962. }
  963. /**
  964. * This method is called to check if the user is already authenticated
  965. * locally or has a global cas session. A already existing cas session is
  966. * determined by a cas gateway call.(cas login call without any interactive
  967. * prompt)
  968. *
  969. * @return true when the user is authenticated, false when a previous
  970. * gateway login failed or the function will not return if the user is
  971. * redirected to the cas server for a gateway login attempt
  972. */
  973. public static function checkAuthentication()
  974. {
  975. phpCAS :: traceBegin();
  976. if (!is_object(self::$_PHPCAS_CLIENT)) {
  977. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  978. }
  979. $auth = self::$_PHPCAS_CLIENT->checkAuthentication();
  980. // store where the authentication has been checked and the result
  981. self::$_PHPCAS_CLIENT->markAuthenticationCall($auth);
  982. phpCAS :: traceEnd($auth);
  983. return $auth;
  984. }
  985. /**
  986. * This method is called to force authentication if the user was not already
  987. * authenticated. If the user is not authenticated, halt by redirecting to
  988. * the CAS server.
  989. *
  990. * @return bool Authentication
  991. */
  992. public static function forceAuthentication()
  993. {
  994. phpCAS :: traceBegin();
  995. if (!is_object(self::$_PHPCAS_CLIENT)) {
  996. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  997. }
  998. $auth = self::$_PHPCAS_CLIENT->forceAuthentication();
  999. // store where the authentication has been checked and the result
  1000. self::$_PHPCAS_CLIENT->markAuthenticationCall($auth);
  1001. /* if (!$auth) {
  1002. phpCAS :: trace('user is not authenticated, redirecting to the CAS server');
  1003. self::$_PHPCAS_CLIENT->forceAuthentication();
  1004. } else {
  1005. phpCAS :: trace('no need to authenticate (user `' . phpCAS :: getUser() . '\' is already authenticated)');
  1006. }*/
  1007. phpCAS :: traceEnd();
  1008. return $auth;
  1009. }
  1010. /**
  1011. * This method is called to renew the authentication.
  1012. *
  1013. * @return void
  1014. **/
  1015. public static function renewAuthentication()
  1016. {
  1017. phpCAS :: traceBegin();
  1018. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1019. phpCAS :: error('this method should not be called before' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1020. }
  1021. $auth = self::$_PHPCAS_CLIENT->renewAuthentication();
  1022. // store where the authentication has been checked and the result
  1023. self::$_PHPCAS_CLIENT->markAuthenticationCall($auth);
  1024. //self::$_PHPCAS_CLIENT->renewAuthentication();
  1025. phpCAS :: traceEnd();
  1026. }
  1027. /**
  1028. * This method is called to check if the user is authenticated (previously or by
  1029. * tickets given in the URL).
  1030. *
  1031. * @return true when the user is authenticated.
  1032. */
  1033. public static function isAuthenticated()
  1034. {
  1035. phpCAS :: traceBegin();
  1036. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1037. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1038. }
  1039. // call the isAuthenticated method of the $_PHPCAS_CLIENT object
  1040. $auth = self::$_PHPCAS_CLIENT->isAuthenticated();
  1041. // store where the authentication has been checked and the result
  1042. self::$_PHPCAS_CLIENT->markAuthenticationCall($auth);
  1043. phpCAS :: traceEnd($auth);
  1044. return $auth;
  1045. }
  1046. /**
  1047. * Checks whether authenticated based on $_SESSION. Useful to avoid
  1048. * server calls.
  1049. *
  1050. * @return bool true if authenticated, false otherwise.
  1051. * @since 0.4.22 by Brendan Arnold
  1052. */
  1053. public static function isSessionAuthenticated()
  1054. {
  1055. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1056. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1057. }
  1058. return (self::$_PHPCAS_CLIENT->isSessionAuthenticated());
  1059. }
  1060. /**
  1061. * This method returns the CAS user's login name.
  1062. *
  1063. * @return string the login name of the authenticated user
  1064. * @warning should not be called only after phpCAS::forceAuthentication()
  1065. * or phpCAS::checkAuthentication().
  1066. * */
  1067. public static function getUser()
  1068. {
  1069. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1070. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1071. }
  1072. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  1073. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
  1074. }
  1075. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  1076. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  1077. }
  1078. return self::$_PHPCAS_CLIENT->getUser();
  1079. }
  1080. /**
  1081. * Answer attributes about the authenticated user.
  1082. *
  1083. * @warning should not be called only after phpCAS::forceAuthentication()
  1084. * or phpCAS::checkAuthentication().
  1085. *
  1086. * @return array
  1087. */
  1088. public static function getAttributes()
  1089. {
  1090. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1091. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1092. }
  1093. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  1094. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
  1095. }
  1096. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  1097. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  1098. }
  1099. return self::$_PHPCAS_CLIENT->getAttributes();
  1100. }
  1101. /**
  1102. * Answer true if there are attributes for the authenticated user.
  1103. *
  1104. * @warning should not be called only after phpCAS::forceAuthentication()
  1105. * or phpCAS::checkAuthentication().
  1106. *
  1107. * @return bool
  1108. */
  1109. public static function hasAttributes()
  1110. {
  1111. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1112. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1113. }
  1114. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  1115. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
  1116. }
  1117. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  1118. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  1119. }
  1120. return self::$_PHPCAS_CLIENT->hasAttributes();
  1121. }
  1122. /**
  1123. * Answer true if an attribute exists for the authenticated user.
  1124. *
  1125. * @param string $key attribute name
  1126. *
  1127. * @return bool
  1128. * @warning should not be called only after phpCAS::forceAuthentication()
  1129. * or phpCAS::checkAuthentication().
  1130. */
  1131. public static function hasAttribute($key)
  1132. {
  1133. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1134. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1135. }
  1136. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  1137. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
  1138. }
  1139. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  1140. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  1141. }
  1142. return self::$_PHPCAS_CLIENT->hasAttribute($key);
  1143. }
  1144. /**
  1145. * Answer an attribute for the authenticated user.
  1146. *
  1147. * @param string $key attribute name
  1148. *
  1149. * @return mixed string for a single value or an array if multiple values exist.
  1150. * @warning should not be called only after phpCAS::forceAuthentication()
  1151. * or phpCAS::checkAuthentication().
  1152. */
  1153. public static function getAttribute($key)
  1154. {
  1155. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1156. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1157. }
  1158. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCalled()) {
  1159. phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
  1160. }
  1161. if (!self::$_PHPCAS_CLIENT->wasAuthenticationCallSuccessful()) {
  1162. phpCAS :: error('authentication was checked (by ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerMethod() . '() at ' . self::$_PHPCAS_CLIENT->getAuthenticationCallerFile() . ':' . self::$_PHPCAS_CLIENT->getAuthenticationCallerLine() . ') but the method returned false');
  1163. }
  1164. return self::$_PHPCAS_CLIENT->getAttribute($key);
  1165. }
  1166. /**
  1167. * Handle logout requests.
  1168. *
  1169. * @param bool $check_client additional safety check
  1170. * @param array $allowed_clients array of allowed clients
  1171. *
  1172. * @return void
  1173. */
  1174. public static function handleLogoutRequests($check_client = true, $allowed_clients = false)
  1175. {
  1176. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1177. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1178. }
  1179. return (self::$_PHPCAS_CLIENT->handleLogoutRequests($check_client, $allowed_clients));
  1180. }
  1181. /**
  1182. * This method returns the URL to be used to login.
  1183. * or phpCAS::isAuthenticated().
  1184. *
  1185. * @return the login name of the authenticated user
  1186. */
  1187. public static function getServerLoginURL()
  1188. {
  1189. if (!is_object(self::$_PHPCAS_CLIENT)) {
  1190. phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
  1191. }
  1192. return self::$_PHPCAS_CLIENT->getServerLoginURL();
  1193. }
  1194. /**
  1195. * Set the login URL of the CAS server.
  1196. *
  1197. * @param string $url the login URL
  1198. *
  1199. * @return void
  1200. * @since 0.4.21 by Wyman Chan
  1201. */

Large files files are truncated, but you can click here to view the full file