3 // commented in 0.4.22-RC2 for Sylvain Derosiaux
4 // error_reporting(E_ALL ^ E_NOTICE);
7 // hack by Vangelis Haniotakis to handle the absence of $_SERVER['REQUEST_URI'] in IIS
9 if (!$_SERVER['REQUEST_URI']) {
10 $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
14 // another one by Vangelis Haniotakis also to make phpCAS work with PHP5
16 if (version_compare(PHP_VERSION
,'5','>=')) {
17 require_once(dirname(__FILE__
).'/domxml-php4-php5.php');
22 * Interface class of the phpCAS library
27 // ########################################################################
29 // ########################################################################
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
36 * phpCAS version. accessible for the user by phpCAS::getVersion().
38 define('PHPCAS_VERSION','0.5.1-1');
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
51 define("CAS_VERSION_1_0",'1.0');
55 define("CAS_VERSION_2_0",'2.0');
59 * @addtogroup publicPGTStorage
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
66 * Default path used when storing PGT's to file
68 define("CAS_PGT_STORAGE_FILE_DEFAULT_PATH",'/tmp');
70 * phpCAS::setPGTStorageFile()'s 2nd parameter to write plain text files
72 define("CAS_PGT_STORAGE_FILE_FORMAT_PLAIN",'plain');
74 * phpCAS::setPGTStorageFile()'s 2nd parameter to write xml files
76 define("CAS_PGT_STORAGE_FILE_FORMAT_XML",'xml');
78 * Default format used when storing PGT's to file
80 define("CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT",CAS_PGT_STORAGE_FILE_FORMAT_PLAIN
);
81 // ------------------------------------------------------------------------
82 // DATABASE PGT STORAGE
83 // ------------------------------------------------------------------------
85 * default database type when storing PGT's to database
87 define("CAS_PGT_STORAGE_DB_DEFAULT_DATABASE_TYPE",'mysql');
89 * default host when storing PGT's to database
91 define("CAS_PGT_STORAGE_DB_DEFAULT_HOSTNAME",'localhost');
93 * default port when storing PGT's to database
95 define("CAS_PGT_STORAGE_DB_DEFAULT_PORT",'');
97 * default database when storing PGT's to database
99 define("CAS_PGT_STORAGE_DB_DEFAULT_DATABASE",'phpCAS');
101 * default table when storing PGT's to database
103 define("CAS_PGT_STORAGE_DB_DEFAULT_TABLE",'pgt');
106 // ------------------------------------------------------------------------
107 // SERVICE ACCESS ERRORS
108 // ------------------------------------------------------------------------
110 * @addtogroup publicServices
115 * phpCAS::service() error code on success
117 define("PHPCAS_SERVICE_OK",0);
119 * phpCAS::service() error code when the PT could not retrieve because
120 * the CAS server did not respond.
122 define("PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE",1);
124 * phpCAS::service() error code when the PT could not retrieve because
125 * the response of the CAS server was ill-formed.
127 define("PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE",2);
129 * phpCAS::service() error code when the PT could not retrieve because
130 * the CAS server did not want to.
132 define("PHPCAS_SERVICE_PT_FAILURE",3);
134 * phpCAS::service() error code when the service was not available.
136 define("PHPCAS_SERVICE_NOT AVAILABLE",4);
139 // ------------------------------------------------------------------------
141 // ------------------------------------------------------------------------
143 * @addtogroup publicLang
147 define("PHPCAS_LANG_ENGLISH", 'english');
148 define("PHPCAS_LANG_FRENCH", 'french');
149 define("PHPCAS_LANG_GREEK", 'greek');
150 define("PHPCAS_LANG_GERMAN", 'german');
151 define("PHPCAS_LANG_JAPANESE", 'japanese');
156 * @addtogroup internalLang
161 * phpCAS default language (when phpCAS::setLang() is not used)
163 define("PHPCAS_LANG_DEFAULT", PHPCAS_LANG_ENGLISH
);
166 // ------------------------------------------------------------------------
168 // ------------------------------------------------------------------------
170 * @addtogroup internalMisc
175 * This global variable is used by the interface class phpCAS.
179 $PHPCAS_CLIENT = null;
182 * This global variable is used to store where the initializer is called from
183 * (to print a comprehensive error in case of multiple calls).
187 $PHPCAS_INIT_CALL = array('done' => FALSE,
193 * This global variable is used to store where the method checking
194 * the authentication is called from (to print comprehensive errors)
198 $PHPCAS_AUTH_CHECK_CALL = array('done' => FALSE,
205 * This global variable is used to store phpCAS debug mode.
209 $PHPCAS_DEBUG = array('filename' => FALSE,
215 // ########################################################################
217 // ########################################################################
219 // include client class
220 include_once(dirname(__FILE__
).'/client.php');
222 // ########################################################################
224 // ########################################################################
228 * The phpCAS class is a simple container for the phpCAS library. It provides CAS
229 * authentication for web applications written in PHP.
232 * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
234 * \internal All its methods access the same object ($PHPCAS_CLIENT, declared
235 * at the end of CAS/client.php).
243 // ########################################################################
245 // ########################################################################
248 * @addtogroup publicInit
253 * phpCAS client initializer.
254 * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
255 * called, only once, and before all other methods (except phpCAS::getVersion()
256 * and phpCAS::setDebug()).
258 * @param $server_version the version of the CAS server
259 * @param $server_hostname the hostname of the CAS server
260 * @param $server_port the port the CAS server is running on
261 * @param $server_uri the URI the CAS server is responding on
262 * @param $start_session Have phpCAS start PHP sessions (default true)
264 * @return a newly created CASClient object
266 function client($server_version,
270 $start_session = true)
272 global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL;
274 phpCAS
::traceBegin();
275 if ( is_object($PHPCAS_CLIENT) ) {
276 phpCAS
::error($PHPCAS_INIT_CALL['method'].'() has already been called (at '.$PHPCAS_INIT_CALL['file'].':'.$PHPCAS_INIT_CALL['line'].')');
278 if ( gettype($server_version) != 'string' ) {
279 phpCAS
::error('type mismatched for parameter $server_version (should be `string\')');
281 if ( gettype($server_hostname) != 'string' ) {
282 phpCAS
::error('type mismatched for parameter $server_hostname (should be `string\')');
284 if ( gettype($server_port) != 'integer' ) {
285 phpCAS
::error('type mismatched for parameter $server_port (should be `integer\')');
287 if ( gettype($server_uri) != 'string' ) {
288 phpCAS
::error('type mismatched for parameter $server_uri (should be `string\')');
291 // store where the initialzer is called from
292 $dbg = phpCAS
::backtrace();
293 $PHPCAS_INIT_CALL = array('done' => TRUE,
294 'file' => $dbg[0]['file'],
295 'line' => $dbg[0]['line'],
296 'method' => __CLASS__
.'::'.__FUNCTION__
);
298 // initialize the global object $PHPCAS_CLIENT
299 $PHPCAS_CLIENT = new CASClient($server_version,FALSE/*proxy*/,$server_hostname,$server_port,$server_uri,$start_session);
304 * phpCAS proxy initializer.
305 * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
306 * called, only once, and before all other methods (except phpCAS::getVersion()
307 * and phpCAS::setDebug()).
309 * @param $server_version the version of the CAS server
310 * @param $server_hostname the hostname of the CAS server
311 * @param $server_port the port the CAS server is running on
312 * @param $server_uri the URI the CAS server is responding on
313 * @param $start_session Have phpCAS start PHP sessions (default true)
315 * @return a newly created CASClient object
317 function proxy($server_version,
321 $start_session = true)
323 global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL;
325 phpCAS
::traceBegin();
326 if ( is_object($PHPCAS_CLIENT) ) {
327 phpCAS
::error($PHPCAS_INIT_CALL['method'].'() has already been called (at '.$PHPCAS_INIT_CALL['file'].':'.$PHPCAS_INIT_CALL['line'].')');
329 if ( gettype($server_version) != 'string' ) {
330 phpCAS
::error('type mismatched for parameter $server_version (should be `string\')');
332 if ( gettype($server_hostname) != 'string' ) {
333 phpCAS
::error('type mismatched for parameter $server_hostname (should be `string\')');
335 if ( gettype($server_port) != 'integer' ) {
336 phpCAS
::error('type mismatched for parameter $server_port (should be `integer\')');
338 if ( gettype($server_uri) != 'string' ) {
339 phpCAS
::error('type mismatched for parameter $server_uri (should be `string\')');
342 // store where the initialzer is called from
343 $dbg = phpCAS
::backtrace();
344 $PHPCAS_INIT_CALL = array('done' => TRUE,
345 'file' => $dbg[0]['file'],
346 'line' => $dbg[0]['line'],
347 'method' => __CLASS__
.'::'.__FUNCTION__
);
349 // initialize the global object $PHPCAS_CLIENT
350 $PHPCAS_CLIENT = new CASClient($server_version,TRUE/*proxy*/,$server_hostname,$server_port,$server_uri,$start_session);
355 // ########################################################################
357 // ########################################################################
360 * @addtogroup publicDebug
365 * Set/unset debug mode
367 * @param $filename the name of the file used for logging, or FALSE to stop debugging.
369 function setDebug($filename='')
371 global $PHPCAS_DEBUG;
373 if ( $filename != FALSE && gettype($filename) != 'string' ) {
374 phpCAS
::error('type mismatched for parameter $dbg (should be FALSE or the name of the log file)');
377 if ( empty($filename) ) {
378 if ( preg_match('/^Win.*/',getenv('OS')) ) {
379 if ( isset($_ENV['TMP']) ) {
380 $debugDir = $_ENV['TMP'].'/';
381 } else if ( isset($_ENV['TEMP']) ) {
382 $debugDir = $_ENV['TEMP'].'/';
389 $filename = $debugDir . 'phpCAS.log';
392 if ( empty($PHPCAS_DEBUG['unique_id']) ) {
393 $PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))),0,4);
396 $PHPCAS_DEBUG['filename'] = $filename;
398 phpCAS
::trace('START ******************');
403 * @addtogroup internalDebug
408 * This method is a wrapper for debug_backtrace() that is not available
409 * in all PHP versions (>= 4.3.0 only)
413 if ( function_exists('debug_backtrace') ) {
414 return debug_backtrace();
416 // poor man's hack ... but it does work ...
422 * Logs a string in debug mode.
424 * @param $str the string to write
431 global $PHPCAS_DEBUG;
433 if ( $PHPCAS_DEBUG['filename'] ) {
434 for ($i=0;$i<$PHPCAS_DEBUG['indent'];$i++
) {
437 error_log($PHPCAS_DEBUG['unique_id'].' '.$indent_str.$str."\n",3,$PHPCAS_DEBUG['filename']);
443 * This method is used by interface methods to print an error and where the function
444 * was originally called from.
446 * @param $msg the message to print
452 $dbg = phpCAS
::backtrace();
456 if ( is_array($dbg) ) {
457 for ( $i=1; $i<sizeof($dbg); $i++
) {
458 if ( is_array($dbg[$i]) ) {
459 if ( $dbg[$i]['class'] == __CLASS__
) {
460 $function = $dbg[$i]['function'];
461 $file = $dbg[$i]['file'];
462 $line = $dbg[$i]['line'];
467 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";
474 * This method is used to log something in debug mode.
478 $dbg = phpCAS
::backtrace();
479 phpCAS
::log($str.' ['.basename($dbg[1]['file']).':'.$dbg[1]['line'].']');
483 * This method is used to indicate the start of the execution of a function in debug mode.
485 function traceBegin()
487 global $PHPCAS_DEBUG;
489 $dbg = phpCAS
::backtrace();
491 if ( !empty($dbg[2]['class']) ) {
492 $str .= $dbg[2]['class'].'::';
494 $str .= $dbg[2]['function'].'(';
495 if ( is_array($dbg[2]['args']) ) {
496 foreach ($dbg[2]['args'] as $index => $arg) {
500 $str .= str_replace("\n","",var_export($arg,TRUE));
503 $str .= ') ['.basename($dbg[2]['file']).':'.$dbg[2]['line'].']';
505 $PHPCAS_DEBUG['indent'] ++
;
509 * This method is used to indicate the end of the execution of a function in debug mode.
511 * @param $res the result of the function
513 function traceEnd($res='')
515 global $PHPCAS_DEBUG;
517 $PHPCAS_DEBUG['indent'] --;
518 $dbg = phpCAS
::backtrace();
520 $str .= '<= '.str_replace("\n","",var_export($res,TRUE));
525 * This method is used to indicate the end of the execution of the program
529 global $PHPCAS_DEBUG;
531 phpCAS
::log('exit()');
532 while ( $PHPCAS_DEBUG['indent'] > 0 ) {
534 $PHPCAS_DEBUG['indent'] --;
539 // ########################################################################
540 // INTERNATIONALIZATION
541 // ########################################################################
543 * @addtogroup publicLang
548 * This method is used to set the language used by phpCAS.
549 * @note Can be called only once.
551 * @param $lang a string representing the language.
553 * @sa PHPCAS_LANG_FRENCH, PHPCAS_LANG_ENGLISH
555 function setLang($lang)
557 global $PHPCAS_CLIENT;
558 if ( !is_object($PHPCAS_CLIENT) ) {
559 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
561 if ( gettype($lang) != 'string' ) {
562 phpCAS
::error('type mismatched for parameter $lang (should be `string\')');
564 $PHPCAS_CLIENT->setLang($lang);
568 // ########################################################################
570 // ########################################################################
577 * This method returns the phpCAS version.
579 * @return the phpCAS version.
581 function getVersion()
583 return PHPCAS_VERSION
;
587 // ########################################################################
589 // ########################################################################
591 * @addtogroup publicOutput
596 * This method sets the HTML header used for all outputs.
598 * @param $header the HTML header.
600 function setHTMLHeader($header)
602 global $PHPCAS_CLIENT;
603 if ( !is_object($PHPCAS_CLIENT) ) {
604 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
606 if ( gettype($header) != 'string' ) {
607 phpCAS
::error('type mismatched for parameter $header (should be `string\')');
609 $PHPCAS_CLIENT->setHTMLHeader($header);
613 * This method sets the HTML footer used for all outputs.
615 * @param $footer the HTML footer.
617 function setHTMLFooter($footer)
619 global $PHPCAS_CLIENT;
620 if ( !is_object($PHPCAS_CLIENT) ) {
621 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
623 if ( gettype($footer) != 'string' ) {
624 phpCAS
::error('type mismatched for parameter $footer (should be `string\')');
626 $PHPCAS_CLIENT->setHTMLFooter($footer);
630 // ########################################################################
632 // ########################################################################
634 * @addtogroup publicPGTStorage
639 * This method is used to tell phpCAS to store the response of the
640 * CAS server to PGT requests onto the filesystem.
642 * @param $format the format used to store the PGT's (`plain' and `xml' allowed)
643 * @param $path the path where the PGT's should be stored
645 function setPGTStorageFile($format='',
648 global $PHPCAS_CLIENT,$PHPCAS_AUTH_CHECK_CALL;
650 phpCAS
::traceBegin();
651 if ( !is_object($PHPCAS_CLIENT) ) {
652 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
654 if ( !$PHPCAS_CLIENT->isProxy() ) {
655 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
657 if ( $PHPCAS_AUTH_CHECK_CALL['done'] ) {
658 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'].')');
660 if ( gettype($format) != 'string' ) {
661 phpCAS
::error('type mismatched for parameter $format (should be `string\')');
663 if ( gettype($path) != 'string' ) {
664 phpCAS
::error('type mismatched for parameter $format (should be `string\')');
666 $PHPCAS_CLIENT->setPGTStorageFile($format,$path);
671 * This method is used to tell phpCAS to store the response of the
672 * CAS server to PGT requests into a database.
673 * @note The connection to the database is done only when needed.
674 * As a consequence, bad parameters are detected only when
675 * initializing PGT storage, except in debug mode.
677 * @param $user the user to access the data with
678 * @param $password the user's password
679 * @param $database_type the type of the database hosting the data
680 * @param $hostname the server hosting the database
681 * @param $port the port the server is listening on
682 * @param $database the name of the database
683 * @param $table the name of the table storing the data
685 function setPGTStorageDB($user,
693 global $PHPCAS_CLIENT,$PHPCAS_AUTH_CHECK_CALL;
695 phpCAS
::traceBegin();
696 if ( !is_object($PHPCAS_CLIENT) ) {
697 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
699 if ( !$PHPCAS_CLIENT->isProxy() ) {
700 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
702 if ( $PHPCAS_AUTH_CHECK_CALL['done'] ) {
703 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'].')');
705 if ( gettype($user) != 'string' ) {
706 phpCAS
::error('type mismatched for parameter $user (should be `string\')');
708 if ( gettype($password) != 'string' ) {
709 phpCAS
::error('type mismatched for parameter $password (should be `string\')');
711 if ( gettype($database_type) != 'string' ) {
712 phpCAS
::error('type mismatched for parameter $database_type (should be `string\')');
714 if ( gettype($hostname) != 'string' ) {
715 phpCAS
::error('type mismatched for parameter $hostname (should be `string\')');
717 if ( gettype($port) != 'integer' ) {
718 phpCAS
::error('type mismatched for parameter $port (should be `integer\')');
720 if ( gettype($database) != 'string' ) {
721 phpCAS
::error('type mismatched for parameter $database (should be `string\')');
723 if ( gettype($table) != 'string' ) {
724 phpCAS
::error('type mismatched for parameter $table (should be `string\')');
726 $PHPCAS_CLIENT->setPGTStorageDB($this,$user,$password,$hostname,$port,$database,$table);
731 // ########################################################################
732 // ACCESS TO EXTERNAL SERVICES
733 // ########################################################################
735 * @addtogroup publicServices
740 * This method is used to access an HTTP[S] service.
742 * @param $url the service to access.
743 * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on
744 * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE,
745 * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE.
746 * @param $output the output of the service (also used to give an error
747 * message on failure).
749 * @return TRUE on success, FALSE otherwise (in this later case, $err_code
750 * gives the reason why it failed and $output contains an error message).
752 function serviceWeb($url,&$err_code,&$output)
754 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
756 phpCAS
::traceBegin();
757 if ( !is_object($PHPCAS_CLIENT) ) {
758 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
760 if ( !$PHPCAS_CLIENT->isProxy() ) {
761 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
763 if ( !$PHPCAS_AUTH_CHECK_CALL['done'] ) {
764 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()');
766 if ( !$PHPCAS_AUTH_CHECK_CALL['result'] ) {
767 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');
769 if ( gettype($url) != 'string' ) {
770 phpCAS
::error('type mismatched for parameter $url (should be `string\')');
773 $res = $PHPCAS_CLIENT->serviceWeb($url,$err_code,$output);
775 phpCAS
::traceEnd($res);
780 * This method is used to access an IMAP/POP3/NNTP service.
782 * @param $url a string giving the URL of the service, including the mailing box
783 * for IMAP URLs, as accepted by imap_open().
784 * @param $flags options given to imap_open().
785 * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on
786 * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE,
787 * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE.
788 * @param $err_msg an error message on failure
789 * @param $pt the Proxy Ticket (PT) retrieved from the CAS server to access the URL
790 * on success, FALSE on error).
792 * @return an IMAP stream on success, FALSE otherwise (in this later case, $err_code
793 * gives the reason why it failed and $err_msg contains an error message).
795 function serviceMail($url,$flags,&$err_code,&$err_msg,&$pt)
797 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
799 phpCAS
::traceBegin();
800 if ( !is_object($PHPCAS_CLIENT) ) {
801 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
803 if ( !$PHPCAS_CLIENT->isProxy() ) {
804 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
806 if ( !$PHPCAS_AUTH_CHECK_CALL['done'] ) {
807 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()');
809 if ( !$PHPCAS_AUTH_CHECK_CALL['result'] ) {
810 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');
812 if ( gettype($url) != 'string' ) {
813 phpCAS
::error('type mismatched for parameter $url (should be `string\')');
816 if ( gettype($flags) != 'integer' ) {
817 phpCAS
::error('type mismatched for parameter $flags (should be `integer\')');
820 $res = $PHPCAS_CLIENT->serviceMail($url,$flags,$err_code,$err_msg,$pt);
822 phpCAS
::traceEnd($res);
827 // ########################################################################
829 // ########################################################################
831 * @addtogroup publicAuth
836 * Set the times authentication will be cached before really accessing the CAS server in gateway mode:
837 * - -1: check only once, and then never again (until you pree login)
839 * - n: check every "n" time
841 * @param $n an integer.
843 function setCacheTimesForAuthRecheck($n)
845 global $PHPCAS_CLIENT;
846 if ( !is_object($PHPCAS_CLIENT) ) {
847 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
849 if ( gettype($header) != 'integer' ) {
850 phpCAS
::error('type mismatched for parameter $header (should be `string\')');
852 $PHPCAS_CLIENT->setCacheTimesForAuthRecheck($n);
856 * This method is called to check if the user is authenticated (use the gateway feature).
857 * @return TRUE when the user is authenticated; otherwise FALSE.
859 function checkAuthentication()
861 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
863 phpCAS
::traceBegin();
864 if ( !is_object($PHPCAS_CLIENT) ) {
865 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
868 $auth = $PHPCAS_CLIENT->checkAuthentication();
870 // store where the authentication has been checked and the result
871 $dbg = phpCAS
::backtrace();
872 $PHPCAS_AUTH_CHECK_CALL = array('done' => TRUE,
873 'file' => $dbg[0]['file'],
874 'line' => $dbg[0]['line'],
875 'method' => __CLASS__
.'::'.__FUNCTION__
,
877 phpCAS
::traceEnd($auth);
882 * This method is called to force authentication if the user was not already
883 * authenticated. If the user is not authenticated, halt by redirecting to
886 function forceAuthentication()
888 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
890 phpCAS
::traceBegin();
891 if ( !is_object($PHPCAS_CLIENT) ) {
892 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
895 $auth = $PHPCAS_CLIENT->forceAuthentication();
897 // store where the authentication has been checked and the result
898 $dbg = phpCAS
::backtrace();
899 $PHPCAS_AUTH_CHECK_CALL = array('done' => TRUE,
900 'file' => $dbg[0]['file'],
901 'line' => $dbg[0]['line'],
902 'method' => __CLASS__
.'::'.__FUNCTION__
,
906 phpCAS
::trace('user is not authenticated, redirecting to the CAS server');
907 $PHPCAS_CLIENT->forceAuthentication();
909 phpCAS
::trace('no need to authenticate (user `'.phpCAS
::getUser().'\' is already authenticated)');
917 * This method has been left from version 0.4.1 for compatibility reasons.
919 function authenticate()
921 phpCAS
::error('this method is deprecated. You should use '.__CLASS__
.'::forceAuthentication() instead');
925 * This method is called to check if the user is authenticated (previously or by
926 * tickets given in the URL).
928 * @return TRUE when the user is authenticated.
930 function isAuthenticated()
932 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
934 phpCAS
::traceBegin();
935 if ( !is_object($PHPCAS_CLIENT) ) {
936 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
939 // call the isAuthenticated method of the global $PHPCAS_CLIENT object
940 $auth = $PHPCAS_CLIENT->isAuthenticated();
942 // store where the authentication has been checked and the result
943 $dbg = phpCAS
::backtrace();
944 $PHPCAS_AUTH_CHECK_CALL = array('done' => TRUE,
945 'file' => $dbg[0]['file'],
946 'line' => $dbg[0]['line'],
947 'method' => __CLASS__
.'::'.__FUNCTION__
,
949 phpCAS
::traceEnd($auth);
954 * Checks whether authenticated based on $_SESSION. Useful to avoid
956 * @return true if authenticated, false otherwise.
957 * @since 0.4.22 by Brendan Arnold
959 function isSessionAuthenticated ()
961 global $PHPCAS_CLIENT;
962 if ( !is_object($PHPCAS_CLIENT) ) {
963 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
965 return($PHPCAS_CLIENT->isSessionAuthenticated());
969 * This method returns the CAS user's login name.
970 * @warning should not be called only after phpCAS::forceAuthentication()
971 * or phpCAS::checkAuthentication().
973 * @return the login name of the authenticated user
977 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
978 if ( !is_object($PHPCAS_CLIENT) ) {
979 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
981 if ( !$PHPCAS_AUTH_CHECK_CALL['done'] ) {
982 phpCAS
::error('this method should only be called after '.__CLASS__
.'::forceAuthentication() or '.__CLASS__
.'::isAuthenticated()');
984 if ( !$PHPCAS_AUTH_CHECK_CALL['result'] ) {
985 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');
987 return $PHPCAS_CLIENT->getUser();
991 * This method returns the URL to be used to login.
992 * or phpCAS::isAuthenticated().
994 * @return the login name of the authenticated user
996 function getServerLoginURL()
998 global $PHPCAS_CLIENT;
999 if ( !is_object($PHPCAS_CLIENT) ) {
1000 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
1002 return $PHPCAS_CLIENT->getServerLoginURL();
1006 * Set the login URL of the CAS server.
1007 * @param $url the login URL
1008 * @since 0.4.21 by Wyman Chan
1010 function setServerLoginURL($url='')
1012 global $PHPCAS_CLIENT;
1013 phpCAS
::traceBegin();
1014 if ( !is_object($PHPCAS_CLIENT) ) {
1015 phpCAS
::error('this method should only be called after
1016 '.__CLASS__
.'::client()');
1018 if ( gettype($url) != 'string' ) {
1019 phpCAS
::error('type mismatched for parameter $url (should be
1022 $PHPCAS_CLIENT->setServerLoginURL($url);
1027 * This method returns the URL to be used to login.
1028 * or phpCAS::isAuthenticated().
1030 * @return the login name of the authenticated user
1032 function getServerLogoutURL()
1034 global $PHPCAS_CLIENT;
1035 if ( !is_object($PHPCAS_CLIENT) ) {
1036 phpCAS
::error('this method should not be called before '.__CLASS__
.'::client() or '.__CLASS__
.'::proxy()');
1038 return $PHPCAS_CLIENT->getServerLogoutURL();
1042 * Set the logout URL of the CAS server.
1043 * @param $url the logout URL
1044 * @since 0.4.21 by Wyman Chan
1046 function setServerLogoutURL($url='')
1048 global $PHPCAS_CLIENT;
1049 phpCAS
::traceBegin();
1050 if ( !is_object($PHPCAS_CLIENT) ) {
1051 phpCAS
::error('this method should only be called after
1052 '.__CLASS__
.'::client()');
1054 if ( gettype($url) != 'string' ) {
1055 phpCAS
::error('type mismatched for parameter $url (should be
1058 $PHPCAS_CLIENT->setServerLogoutURL($url);
1063 * This method is used to logout from CAS. Halts by redirecting to the CAS server.
1064 * @param $url a URL that will be transmitted to the CAS server (to come back to when logged out)
1066 function logout($url = "")
1068 global $PHPCAS_CLIENT;
1070 phpCAS
::traceBegin();
1071 if ( !is_object($PHPCAS_CLIENT) ) {
1072 phpCAS
::error('this method should only be called after '.__CLASS__
.'::client() or'.__CLASS__
.'::proxy()');
1074 $PHPCAS_CLIENT->logout($url);
1080 * Set the fixed URL that will be used by the CAS server to transmit the PGT.
1081 * When this method is not called, a phpCAS script uses its own URL for the callback.
1083 * @param $url the URL
1085 function setFixedCallbackURL($url='')
1087 global $PHPCAS_CLIENT;
1088 phpCAS
::traceBegin();
1089 if ( !is_object($PHPCAS_CLIENT) ) {
1090 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
1092 if ( !$PHPCAS_CLIENT->isProxy() ) {
1093 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
1095 if ( gettype($url) != 'string' ) {
1096 phpCAS
::error('type mismatched for parameter $url (should be `string\')');
1098 $PHPCAS_CLIENT->setCallbackURL($url);
1103 * Set the fixed URL that will be set as the CAS service parameter. When this
1104 * method is not called, a phpCAS script uses its own URL.
1106 * @param $url the URL
1108 function setFixedServiceURL($url)
1110 global $PHPCAS_CLIENT;
1111 phpCAS
::traceBegin();
1112 if ( !is_object($PHPCAS_CLIENT) ) {
1113 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
1115 if ( gettype($url) != 'string' ) {
1116 phpCAS
::error('type mismatched for parameter $url (should be `string\')');
1118 $PHPCAS_CLIENT->setURL($url);
1123 * Get the URL that is set as the CAS service parameter.
1125 function getServiceURL()
1127 global $PHPCAS_CLIENT;
1128 if ( !is_object($PHPCAS_CLIENT) ) {
1129 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
1131 return($PHPCAS_CLIENT->getURL());
1135 * Retrieve a Proxy Ticket from the CAS server.
1137 function retrievePT($target_service,&$err_code,&$err_msg)
1139 global $PHPCAS_CLIENT;
1140 if ( !is_object($PHPCAS_CLIENT) ) {
1141 phpCAS
::error('this method should only be called after '.__CLASS__
.'::proxy()');
1143 if ( gettype($target_service) != 'string' ) {
1144 phpCAS
::error('type mismatched for parameter $target_service(should be `string\')');
1146 return($PHPCAS_CLIENT->retrievePT($target_service,$err_code,$err_msg));
1152 // ########################################################################
1154 // ########################################################################
1156 // ########################################################################
1162 * The following pages only show the source documentation.
1164 * For more information on phpCAS, please refer to http://esup-phpcas.sourceforge.net
1168 // ########################################################################
1169 // MODULES DEFINITION
1171 /** @defgroup public User interface */
1173 /** @defgroup publicInit Initialization
1174 * @ingroup public */
1176 /** @defgroup publicAuth Authentication
1177 * @ingroup public */
1179 /** @defgroup publicServices Access to external services
1180 * @ingroup public */
1182 /** @defgroup publicConfig Configuration
1183 * @ingroup public */
1185 /** @defgroup publicLang Internationalization
1186 * @ingroup publicConfig */
1188 /** @defgroup publicOutput HTML output
1189 * @ingroup publicConfig */
1191 /** @defgroup publicPGTStorage PGT storage
1192 * @ingroup publicConfig */
1194 /** @defgroup publicDebug Debugging
1195 * @ingroup public */
1198 /** @defgroup internal Implementation */
1200 /** @defgroup internalAuthentication Authentication
1201 * @ingroup internal */
1203 /** @defgroup internalBasic CAS Basic client features (CAS 1.0, Service Tickets)
1204 * @ingroup internal */
1206 /** @defgroup internalProxy CAS Proxy features (CAS 2.0, Proxy Granting Tickets)
1207 * @ingroup internal */
1209 /** @defgroup internalPGTStorage PGT storage
1210 * @ingroup internalProxy */
1212 /** @defgroup internalPGTStorageDB PGT storage in a database
1213 * @ingroup internalPGTStorage */
1215 /** @defgroup internalPGTStorageFile PGT storage on the filesystem
1216 * @ingroup internalPGTStorage */
1218 /** @defgroup internalCallback Callback from the CAS server
1219 * @ingroup internalProxy */
1221 /** @defgroup internalProxied CAS proxied client features (CAS 2.0, Proxy Tickets)
1222 * @ingroup internal */
1224 /** @defgroup internalConfig Configuration
1225 * @ingroup internal */
1227 /** @defgroup internalOutput HTML output
1228 * @ingroup internalConfig */
1230 /** @defgroup internalLang Internationalization
1231 * @ingroup internalConfig
1233 * To add a new language:
1234 * - 1. define a new constant PHPCAS_LANG_XXXXXX in CAS/CAS.php
1235 * - 2. copy any file from CAS/languages to CAS/languages/XXXXXX.php
1236 * - 3. Make the translations
1239 /** @defgroup internalDebug Debugging
1240 * @ingroup internal */
1242 /** @defgroup internalMisc Miscellaneous
1243 * @ingroup internal */
1245 // ########################################################################
1249 * @example example_simple.php
1252 * @example example_proxy.php
1255 * @example example_proxy2.php
1258 * @example example_lang.php
1261 * @example example_html.php
1264 * @example example_file.php
1267 * @example example_db.php
1270 * @example example_service.php
1273 * @example example_session_proxy.php
1276 * @example example_session_service.php
1279 * @example example_gateway.php