235
314
* 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));
319
// ########################################################################
321
// ########################################################################
324
* @addtogroup publicInit
329
* phpCAS client initializer.
330
* @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
331
* called, only once, and before all other methods (except phpCAS::getVersion()
332
* and phpCAS::setDebug()).
334
* @param $server_version the version of the CAS server
335
* @param $server_hostname the hostname of the CAS server
336
* @param $server_port the port the CAS server is running on
337
* @param $server_uri the URI the CAS server is responding on
338
* @param $start_session Have phpCAS start PHP sessions (default true)
340
* @return a newly created CASClient object
342
function client($server_version, $server_hostname, $server_port, $server_uri, $start_session = true) {
343
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'] . ')');
349
if (gettype($server_version) != 'string') {
350
phpCAS :: error('type mismatched for parameter $server_version (should be `string\')');
352
if (gettype($server_hostname) != 'string') {
353
phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')');
355
if (gettype($server_port) != 'integer') {
356
phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')');
358
if (gettype($server_uri) != 'string') {
359
phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')');
362
// store where the initializer is called from
363
$dbg = phpCAS :: backtrace();
364
$PHPCAS_INIT_CALL = array (
366
'file' => $dbg[0]['file'],
367
'line' => $dbg[0]['line'],
368
'method' => __CLASS__ . '::' . __FUNCTION__
371
// initialize the global object $PHPCAS_CLIENT
372
$PHPCAS_CLIENT = new CASClient($server_version, FALSE /*proxy*/
373
, $server_hostname, $server_port, $server_uri, $start_session);
374
phpCAS :: traceEnd();
378
* phpCAS proxy initializer.
379
* @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
380
* called, only once, and before all other methods (except phpCAS::getVersion()
381
* and phpCAS::setDebug()).
383
* @param $server_version the version of the CAS server
384
* @param $server_hostname the hostname of the CAS server
385
* @param $server_port the port the CAS server is running on
386
* @param $server_uri the URI the CAS server is responding on
387
* @param $start_session Have phpCAS start PHP sessions (default true)
389
* @return a newly created CASClient object
391
function proxy($server_version, $server_hostname, $server_port, $server_uri, $start_session = true) {
392
global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL;
394
phpCAS :: traceBegin();
395
if (is_object($PHPCAS_CLIENT)) {
396
phpCAS :: error($PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . $PHPCAS_INIT_CALL['file'] . ':' . $PHPCAS_INIT_CALL['line'] . ')');
398
if (gettype($server_version) != 'string') {
399
phpCAS :: error('type mismatched for parameter $server_version (should be `string\')');
401
if (gettype($server_hostname) != 'string') {
402
phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')');
404
if (gettype($server_port) != 'integer') {
405
phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')');
407
if (gettype($server_uri) != 'string') {
408
phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')');
411
// store where the initialzer is called from
412
$dbg = phpCAS :: backtrace();
413
$PHPCAS_INIT_CALL = array (
415
'file' => $dbg[0]['file'],
416
'line' => $dbg[0]['line'],
417
'method' => __CLASS__ . '::' . __FUNCTION__
420
// initialize the global object $PHPCAS_CLIENT
421
$PHPCAS_CLIENT = new CASClient($server_version, TRUE /*proxy*/
422
, $server_hostname, $server_port, $server_uri, $start_session);
423
phpCAS :: traceEnd();
427
// ########################################################################
429
// ########################################################################
432
* @addtogroup publicDebug
437
* Set/unset debug mode
439
* @param $filename the name of the file used for logging, or FALSE to stop debugging.
441
function setDebug($filename = '') {
442
global $PHPCAS_DEBUG;
444
if ($filename != FALSE && gettype($filename) != 'string') {
445
phpCAS :: error('type mismatched for parameter $dbg (should be FALSE or the name of the log file)');
448
if (empty ($filename)) {
449
if (preg_match('/^Win.*/', getenv('OS'))) {
450
if (isset ($_ENV['TMP'])) {
451
$debugDir = $_ENV['TMP'] . '/';
453
if (isset ($_ENV['TEMP'])) {
454
$debugDir = $_ENV['TEMP'] . '/';
459
$debugDir = DEFAULT_DEBUG_DIR;
461
$filename = $debugDir . 'phpCAS.log';
464
if (empty ($PHPCAS_DEBUG['unique_id'])) {
465
$PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))), 0, 4);
468
$PHPCAS_DEBUG['filename'] = $filename;
470
phpCAS :: trace('START phpCAS-' . PHPCAS_VERSION . ' ******************');
475
* @addtogroup internalDebug
480
* This method is a wrapper for debug_backtrace() that is not available
481
* in all PHP versions (>= 4.3.0 only)
483
function backtrace() {
484
if (function_exists('debug_backtrace')) {
485
return debug_backtrace();
487
// poor man's hack ... but it does work ...
493
* Logs a string in debug mode.
495
* @param $str the string to write
501
global $PHPCAS_DEBUG;
503
if ($PHPCAS_DEBUG['filename']) {
504
for ($i = 0; $i < $PHPCAS_DEBUG['indent']; $i++) {
507
error_log($PHPCAS_DEBUG['unique_id'] . ' ' . $indent_str . $str . "\n", 3, $PHPCAS_DEBUG['filename']);
513
* This method is used by interface methods to print an error and where the function
514
* was originally called from.
516
* @param $msg the message to print
520
function error($msg) {
521
$dbg = phpCAS :: backtrace();
525
if (is_array($dbg)) {
526
for ($i = 1; $i < sizeof($dbg); $i++) {
527
if (is_array($dbg[$i])) {
528
if ($dbg[$i]['class'] == __CLASS__) {
529
$function = $dbg[$i]['function'];
530
$file = $dbg[$i]['file'];
531
$line = $dbg[$i]['line'];
536
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";
537
phpCAS :: trace($msg);
538
phpCAS :: traceExit();
543
* This method is used to log something in debug mode.
545
function trace($str) {
546
$dbg = phpCAS :: backtrace();
547
phpCAS :: log($str . ' [' . basename($dbg[1]['file']) . ':' . $dbg[1]['line'] . ']');
551
* This method is used to indicate the start of the execution of a function in debug mode.
553
function traceBegin() {
554
global $PHPCAS_DEBUG;
556
$dbg = phpCAS :: backtrace();
558
if (!empty ($dbg[2]['class'])) {
559
$str .= $dbg[2]['class'] . '::';
561
$str .= $dbg[2]['function'] . '(';
562
if (is_array($dbg[2]['args'])) {
563
foreach ($dbg[2]['args'] as $index => $arg) {
567
$str .= str_replace("\n", "", var_export($arg, TRUE));
570
$str .= ') [' . basename($dbg[2]['file']) . ':' . $dbg[2]['line'] . ']';
572
$PHPCAS_DEBUG['indent']++;
576
* This method is used to indicate the end of the execution of a function in debug mode.
578
* @param $res the result of the function
580
function traceEnd($res = '') {
581
global $PHPCAS_DEBUG;
583
$PHPCAS_DEBUG['indent']--;
584
$dbg = phpCAS :: backtrace();
586
$str .= '<= ' . str_replace("\n", "", var_export($res, TRUE));
591
* This method is used to indicate the end of the execution of the program
593
function traceExit() {
594
global $PHPCAS_DEBUG;
596
phpCAS :: log('exit()');
597
while ($PHPCAS_DEBUG['indent'] > 0) {
599
$PHPCAS_DEBUG['indent']--;
604
// ########################################################################
605
// INTERNATIONALIZATION
606
// ########################################################################
608
* @addtogroup publicLang
613
* This method is used to set the language used by phpCAS.
614
* @note Can be called only once.
616
* @param $lang a string representing the language.
618
* @sa PHPCAS_LANG_FRENCH, PHPCAS_LANG_ENGLISH
620
function setLang($lang) {
621
global $PHPCAS_CLIENT;
622
if (!is_object($PHPCAS_CLIENT)) {
623
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
625
if (gettype($lang) != 'string') {
626
phpCAS :: error('type mismatched for parameter $lang (should be `string\')');
628
$PHPCAS_CLIENT->setLang($lang);
632
// ########################################################################
634
// ########################################################################
641
* This method returns the phpCAS version.
643
* @return the phpCAS version.
645
function getVersion() {
646
return PHPCAS_VERSION;
650
// ########################################################################
652
// ########################################################################
654
* @addtogroup publicOutput
659
* This method sets the HTML header used for all outputs.
661
* @param $header the HTML header.
663
function setHTMLHeader($header) {
664
global $PHPCAS_CLIENT;
665
if (!is_object($PHPCAS_CLIENT)) {
666
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
668
if (gettype($header) != 'string') {
669
phpCAS :: error('type mismatched for parameter $header (should be `string\')');
671
$PHPCAS_CLIENT->setHTMLHeader($header);
675
* This method sets the HTML footer used for all outputs.
677
* @param $footer the HTML footer.
679
function setHTMLFooter($footer) {
680
global $PHPCAS_CLIENT;
681
if (!is_object($PHPCAS_CLIENT)) {
682
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
684
if (gettype($footer) != 'string') {
685
phpCAS :: error('type mismatched for parameter $footer (should be `string\')');
687
$PHPCAS_CLIENT->setHTMLFooter($footer);
691
// ########################################################################
693
// ########################################################################
695
* @addtogroup publicPGTStorage
700
* This method is used to tell phpCAS to store the response of the
701
* CAS server to PGT requests onto the filesystem.
703
* @param $format the format used to store the PGT's (`plain' and `xml' allowed)
704
* @param $path the path where the PGT's should be stored
706
function setPGTStorageFile($format = '', $path = '') {
707
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
709
phpCAS :: traceBegin();
710
if (!is_object($PHPCAS_CLIENT)) {
711
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
713
if (!$PHPCAS_CLIENT->isProxy()) {
714
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
716
if ($PHPCAS_AUTH_CHECK_CALL['done']) {
717
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'] . ')');
719
if (gettype($format) != 'string') {
720
phpCAS :: error('type mismatched for parameter $format (should be `string\')');
722
if (gettype($path) != 'string') {
723
phpCAS :: error('type mismatched for parameter $format (should be `string\')');
725
$PHPCAS_CLIENT->setPGTStorageFile($format, $path);
726
phpCAS :: traceEnd();
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) {
753
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
755
phpCAS :: traceBegin();
756
if (!is_object($PHPCAS_CLIENT)) {
757
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
759
if (!$PHPCAS_CLIENT->isProxy()) {
760
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
762
if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
763
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()');
765
if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
766
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');
768
if (gettype($url) != 'string') {
769
phpCAS :: error('type mismatched for parameter $url (should be `string\')');
772
$res = $PHPCAS_CLIENT->serviceWeb($url, $err_code, $output);
774
phpCAS :: traceEnd($res);
779
* This method is used to access an IMAP/POP3/NNTP service.
781
* @param $url a string giving the URL of the service, including the mailing box
782
* for IMAP URLs, as accepted by imap_open().
783
* @param $service a string giving for CAS retrieve Proxy ticket
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, $service, $flags, & $err_code, & $err_msg, & $pt) {
796
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
798
phpCAS :: traceBegin();
799
if (!is_object($PHPCAS_CLIENT)) {
800
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
802
if (!$PHPCAS_CLIENT->isProxy()) {
803
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
805
if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
806
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()');
808
if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
809
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');
811
if (gettype($url) != 'string') {
812
phpCAS :: error('type mismatched for parameter $url (should be `string\')');
815
if (gettype($flags) != 'integer') {
816
phpCAS :: error('type mismatched for parameter $flags (should be `integer\')');
819
$res = $PHPCAS_CLIENT->serviceMail($url, $service, $flags, $err_code, $err_msg, $pt);
821
phpCAS :: traceEnd($res);
826
// ########################################################################
828
// ########################################################################
830
* @addtogroup publicAuth
835
* Set the times authentication will be cached before really accessing the CAS server in gateway mode:
836
* - -1: check only once, and then never again (until you pree login)
838
* - n: check every "n" time
840
* @param $n an integer.
842
function setCacheTimesForAuthRecheck($n) {
843
global $PHPCAS_CLIENT;
844
if (!is_object($PHPCAS_CLIENT)) {
845
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
847
if (gettype($n) != 'integer') {
848
phpCAS :: error('type mismatched for parameter $header (should be `string\')');
850
$PHPCAS_CLIENT->setCacheTimesForAuthRecheck($n);
854
* This method is called to check if the user is authenticated (use the gateway feature).
855
* @return TRUE when the user is authenticated; otherwise FALSE.
857
function checkAuthentication() {
858
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
860
phpCAS :: traceBegin();
861
if (!is_object($PHPCAS_CLIENT)) {
862
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
865
$auth = $PHPCAS_CLIENT->checkAuthentication();
867
// store where the authentication has been checked and the result
868
$dbg = phpCAS :: backtrace();
869
$PHPCAS_AUTH_CHECK_CALL = array (
871
'file' => $dbg[0]['file'],
872
'line' => $dbg[0]['line'],
873
'method' => __CLASS__ . '::' . __FUNCTION__,
876
phpCAS :: traceEnd($auth);
881
* This method is called to force authentication if the user was not already
882
* authenticated. If the user is not authenticated, halt by redirecting to
885
function forceAuthentication() {
886
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
888
phpCAS :: traceBegin();
889
if (!is_object($PHPCAS_CLIENT)) {
890
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
893
$auth = $PHPCAS_CLIENT->forceAuthentication();
895
// store where the authentication has been checked and the result
896
$dbg = phpCAS :: backtrace();
897
$PHPCAS_AUTH_CHECK_CALL = array (
899
'file' => $dbg[0]['file'],
900
'line' => $dbg[0]['line'],
901
'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)');
912
phpCAS :: traceEnd();
917
* This method is called to renew the authentication.
919
function renewAuthentication() {
920
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
922
phpCAS :: traceBegin();
923
if (!is_object($PHPCAS_CLIENT)) {
924
phpCAS :: error('this method should not be called before' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
927
// store where the authentication has been checked and the result
928
$dbg = phpCAS :: backtrace();
929
$PHPCAS_AUTH_CHECK_CALL = array (
931
'file' => $dbg[0]['file'],
932
'line' => $dbg[0]['line'],
933
'method' => __CLASS__ . '::' . __FUNCTION__,
937
$PHPCAS_CLIENT->renewAuthentication();
938
phpCAS :: traceEnd();
942
* This method has been left from version 0.4.1 for compatibility reasons.
944
function authenticate() {
945
phpCAS :: error('this method is deprecated. You should use ' . __CLASS__ . '::forceAuthentication() instead');
949
* This method is called to check if the user is authenticated (previously or by
950
* tickets given in the URL).
952
* @return TRUE when the user is authenticated.
954
function isAuthenticated() {
955
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
957
phpCAS :: traceBegin();
958
if (!is_object($PHPCAS_CLIENT)) {
959
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
962
// call the isAuthenticated method of the global $PHPCAS_CLIENT object
963
$auth = $PHPCAS_CLIENT->isAuthenticated();
965
// store where the authentication has been checked and the result
966
$dbg = phpCAS :: backtrace();
967
$PHPCAS_AUTH_CHECK_CALL = array (
969
'file' => $dbg[0]['file'],
970
'line' => $dbg[0]['line'],
971
'method' => __CLASS__ . '::' . __FUNCTION__,
974
phpCAS :: traceEnd($auth);
979
* Checks whether authenticated based on $_SESSION. Useful to avoid
981
* @return true if authenticated, false otherwise.
982
* @since 0.4.22 by Brendan Arnold
984
function isSessionAuthenticated() {
985
global $PHPCAS_CLIENT;
986
if (!is_object($PHPCAS_CLIENT)) {
987
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
989
return ($PHPCAS_CLIENT->isSessionAuthenticated());
993
* This method returns the CAS user's login name.
994
* @warning should not be called only after phpCAS::forceAuthentication()
995
* or phpCAS::checkAuthentication().
997
* @return the login name of the authenticated user
1000
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
1001
if (!is_object($PHPCAS_CLIENT)) {
1002
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
1004
if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
1005
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
1007
if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
1008
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');
1010
return $PHPCAS_CLIENT->getUser();
1014
* This method returns the CAS user's login name.
1015
* @warning should not be called only after phpCAS::forceAuthentication()
1016
* or phpCAS::checkAuthentication().
1018
* @return the login name of the authenticated user
1020
function getAttributes() {
1021
global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
1022
if (!is_object($PHPCAS_CLIENT)) {
1023
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
1025
if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
1026
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()');
1028
if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
1029
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');
1031
return $PHPCAS_CLIENT->getAttributes();
1034
* Handle logout requests.
1036
function handleLogoutRequests($check_client = true, $allowed_clients = false) {
1037
global $PHPCAS_CLIENT;
1038
if (!is_object($PHPCAS_CLIENT)) {
1039
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
1041
return ($PHPCAS_CLIENT->handleLogoutRequests($check_client, $allowed_clients));
1045
* This method returns the URL to be used to login.
1046
* or phpCAS::isAuthenticated().
1048
* @return the login name of the authenticated user
1050
function getServerLoginURL() {
1051
global $PHPCAS_CLIENT;
1052
if (!is_object($PHPCAS_CLIENT)) {
1053
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
1055
return $PHPCAS_CLIENT->getServerLoginURL();
1059
* Set the login URL of the CAS server.
1060
* @param $url the login URL
1061
* @since 0.4.21 by Wyman Chan
1063
function setServerLoginURL($url = '') {
1064
global $PHPCAS_CLIENT;
1065
phpCAS :: traceBegin();
1066
if (!is_object($PHPCAS_CLIENT)) {
1067
phpCAS :: error('this method should only be called after
1068
' . __CLASS__ . '::client()');
1070
if (gettype($url) != 'string') {
1071
phpCAS :: error('type mismatched for parameter $url (should be
1074
$PHPCAS_CLIENT->setServerLoginURL($url);
1075
phpCAS :: traceEnd();
1079
* Set the serviceValidate URL of the CAS server.
1080
* Used only in CAS 1.0 validations
1081
* @param $url the serviceValidate URL
1082
* @since 1.1.0 by Joachim Fritschi
1084
function setServerServiceValidateURL($url = '') {
1085
global $PHPCAS_CLIENT;
1086
phpCAS :: traceBegin();
1087
if (!is_object($PHPCAS_CLIENT)) {
1088
phpCAS :: error('this method should only be called after
1089
' . __CLASS__ . '::client()');
1091
if (gettype($url) != 'string') {
1092
phpCAS :: error('type mismatched for parameter $url (should be
1095
$PHPCAS_CLIENT->setServerServiceValidateURL($url);
1096
phpCAS :: traceEnd();
1100
* Set the proxyValidate URL of the CAS server.
1101
* Used for all CAS 2.0 validations
1102
* @param $url the proxyValidate URL
1103
* @since 1.1.0 by Joachim Fritschi
1105
function setServerProxyValidateURL($url = '') {
1106
global $PHPCAS_CLIENT;
1107
phpCAS :: traceBegin();
1108
if (!is_object($PHPCAS_CLIENT)) {
1109
phpCAS :: error('this method should only be called after
1110
' . __CLASS__ . '::client()');
1112
if (gettype($url) != 'string') {
1113
phpCAS :: error('type mismatched for parameter $url (should be
1116
$PHPCAS_CLIENT->setServerProxyValidateURL($url);
1117
phpCAS :: traceEnd();
1121
* Set the samlValidate URL of the CAS server.
1122
* @param $url the samlValidate URL
1123
* @since 1.1.0 by Joachim Fritschi
1125
function setServerSamlValidateURL($url = '') {
1126
global $PHPCAS_CLIENT;
1127
phpCAS :: traceBegin();
1128
if (!is_object($PHPCAS_CLIENT)) {
1129
phpCAS :: error('this method should only be called after
1130
' . __CLASS__ . '::client()');
1132
if (gettype($url) != 'string') {
1133
phpCAS :: error('type mismatched for parameter $url (should be
1136
$PHPCAS_CLIENT->setServerSamlValidateURL($url);
1137
phpCAS :: traceEnd();
1141
* This method returns the URL to be used to login.
1142
* or phpCAS::isAuthenticated().
1144
* @return the login name of the authenticated user
1146
function getServerLogoutURL() {
1147
global $PHPCAS_CLIENT;
1148
if (!is_object($PHPCAS_CLIENT)) {
1149
phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()');
1151
return $PHPCAS_CLIENT->getServerLogoutURL();
1155
* Set the logout URL of the CAS server.
1156
* @param $url the logout URL
1157
* @since 0.4.21 by Wyman Chan
1159
function setServerLogoutURL($url = '') {
1160
global $PHPCAS_CLIENT;
1161
phpCAS :: traceBegin();
1162
if (!is_object($PHPCAS_CLIENT)) {
1163
phpCAS :: error('this method should only be called after
1164
' . __CLASS__ . '::client()');
1166
if (gettype($url) != 'string') {
1167
phpCAS :: error('type mismatched for parameter $url (should be
1170
$PHPCAS_CLIENT->setServerLogoutURL($url);
1171
phpCAS :: traceEnd();
1175
* This method is used to logout from CAS.
1176
* @params $params an array that contains the optional url and service parameters that will be passed to the CAS server
1179
function logout($params = "") {
1180
global $PHPCAS_CLIENT;
1181
phpCAS :: traceBegin();
1182
if (!is_object($PHPCAS_CLIENT)) {
1183
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1185
$parsedParams = array ();
1186
if ($params != "") {
1187
if (is_string($params)) {
1188
phpCAS :: error('method `phpCAS::logout($url)\' is now deprecated, use `phpCAS::logoutWithUrl($url)\' instead');
1190
if (!is_array($params)) {
1191
phpCAS :: error('type mismatched for parameter $params (should be `array\')');
1193
foreach ($params as $key => $value) {
1194
if ($key != "service" && $key != "url") {
1195
phpCAS :: error('only `url\' and `service\' parameters are allowed for method `phpCAS::logout($params)\'');
1197
$parsedParams[$key] = $value;
1200
$PHPCAS_CLIENT->logout($parsedParams);
1202
phpCAS :: traceEnd();
1206
* This method is used to logout from CAS. Halts by redirecting to the CAS server.
1207
* @param $service a URL that will be transmitted to the CAS server
1209
function logoutWithRedirectService($service) {
1210
global $PHPCAS_CLIENT;
1211
phpCAS :: traceBegin();
1212
if (!is_object($PHPCAS_CLIENT)) {
1213
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1215
if (!is_string($service)) {
1216
phpCAS :: error('type mismatched for parameter $service (should be `string\')');
1218
$PHPCAS_CLIENT->logout(array (
1219
"service" => $service
1222
phpCAS :: traceEnd();
1226
* This method is used to logout from CAS. Halts by redirecting to the CAS server.
1227
* @param $url a URL that will be transmitted to the CAS server
1229
function logoutWithUrl($url) {
1230
global $PHPCAS_CLIENT;
1231
phpCAS :: traceBegin();
1232
if (!is_object($PHPCAS_CLIENT)) {
1233
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1235
if (!is_string($url)) {
1236
phpCAS :: error('type mismatched for parameter $url (should be `string\')');
1238
$PHPCAS_CLIENT->logout(array (
1242
phpCAS :: traceEnd();
1246
* This method is used to logout from CAS. Halts by redirecting to the CAS server.
1247
* @param $service a URL that will be transmitted to the CAS server
1248
* @param $url a URL that will be transmitted to the CAS server
1250
function logoutWithRedirectServiceAndUrl($service, $url) {
1251
global $PHPCAS_CLIENT;
1252
phpCAS :: traceBegin();
1253
if (!is_object($PHPCAS_CLIENT)) {
1254
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1256
if (!is_string($service)) {
1257
phpCAS :: error('type mismatched for parameter $service (should be `string\')');
1259
if (!is_string($url)) {
1260
phpCAS :: error('type mismatched for parameter $url (should be `string\')');
1262
$PHPCAS_CLIENT->logout(array (
1263
"service" => $service,
1267
phpCAS :: traceEnd();
1271
* Set the fixed URL that will be used by the CAS server to transmit the PGT.
1272
* When this method is not called, a phpCAS script uses its own URL for the callback.
1274
* @param $url the URL
1276
function setFixedCallbackURL($url = '') {
1277
global $PHPCAS_CLIENT;
1278
phpCAS :: traceBegin();
1279
if (!is_object($PHPCAS_CLIENT)) {
1280
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
1282
if (!$PHPCAS_CLIENT->isProxy()) {
1283
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
1285
if (gettype($url) != 'string') {
1286
phpCAS :: error('type mismatched for parameter $url (should be `string\')');
1288
$PHPCAS_CLIENT->setCallbackURL($url);
1289
phpCAS :: traceEnd();
1293
* Set the fixed URL that will be set as the CAS service parameter. When this
1294
* method is not called, a phpCAS script uses its own URL.
1296
* @param $url the URL
1298
function setFixedServiceURL($url) {
1299
global $PHPCAS_CLIENT;
1300
phpCAS :: traceBegin();
1301
if (!is_object($PHPCAS_CLIENT)) {
1302
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
1304
if (gettype($url) != 'string') {
1305
phpCAS :: error('type mismatched for parameter $url (should be `string\')');
1307
$PHPCAS_CLIENT->setURL($url);
1308
phpCAS :: traceEnd();
1312
* Get the URL that is set as the CAS service parameter.
1314
function getServiceURL() {
1315
global $PHPCAS_CLIENT;
1316
if (!is_object($PHPCAS_CLIENT)) {
1317
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
1319
return ($PHPCAS_CLIENT->getURL());
1323
* Retrieve a Proxy Ticket from the CAS server.
1325
function retrievePT($target_service, & $err_code, & $err_msg) {
1326
global $PHPCAS_CLIENT;
1327
if (!is_object($PHPCAS_CLIENT)) {
1328
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()');
1330
if (gettype($target_service) != 'string') {
1331
phpCAS :: error('type mismatched for parameter $target_service(should be `string\')');
1333
return ($PHPCAS_CLIENT->retrievePT($target_service, $err_code, $err_msg));
1337
* Set the certificate of the CAS server.
1339
* @param $cert the PEM certificate
1341
function setCasServerCert($cert) {
1342
global $PHPCAS_CLIENT;
1343
phpCAS :: traceBegin();
1344
if (!is_object($PHPCAS_CLIENT)) {
1345
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1347
if (gettype($cert) != 'string') {
1348
phpCAS :: error('type mismatched for parameter $cert (should be `string\')');
1350
$PHPCAS_CLIENT->setCasServerCert($cert);
1351
phpCAS :: traceEnd();
1355
* Set the certificate of the CAS server CA.
1357
* @param $cert the CA certificate
1359
function setCasServerCACert($cert) {
1360
global $PHPCAS_CLIENT;
1361
phpCAS :: traceBegin();
1362
if (!is_object($PHPCAS_CLIENT)) {
1363
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1365
if (gettype($cert) != 'string') {
1366
phpCAS :: error('type mismatched for parameter $cert (should be `string\')');
1368
$PHPCAS_CLIENT->setCasServerCACert($cert);
1369
phpCAS :: traceEnd();
1373
* Set no SSL validation for the CAS server.
1375
function setNoCasServerValidation() {
1376
global $PHPCAS_CLIENT;
1377
phpCAS :: traceBegin();
1378
if (!is_object($PHPCAS_CLIENT)) {
1379
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1381
$PHPCAS_CLIENT->setNoCasServerValidation();
1382
phpCAS :: traceEnd();
1388
* Change CURL options.
1389
* CURL is used to connect through HTTPS to CAS server
1390
* @param $key the option key
1391
* @param $value the value to set
1393
function setExtraCurlOption($key, $value) {
1394
global $PHPCAS_CLIENT;
1395
phpCAS :: traceBegin();
1396
if (!is_object($PHPCAS_CLIENT)) {
1397
phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()');
1399
$PHPCAS_CLIENT->setExtraCurlOption($key, $value);
1400
phpCAS :: traceEnd();