3 #error_reporting(E_ALL ^ E_NOTICE);
8 if (!$_SERVER[
'REQUEST_URI']) {
9 $_SERVER[
'REQUEST_URI'] = $_SERVER[
'SCRIPT_NAME'].
'?'.$_SERVER[
'QUERY_STRING'];
15 if (version_compare(PHP_VERSION,
'5',
'>=')) {
37 define(
'PHPCAS_VERSION',
'0.4.20-1');
50 define(
"CAS_VERSION_1_0",
'1.0');
54 define(
"CAS_VERSION_2_0",
'2.0');
67 define(
"CAS_PGT_STORAGE_FILE_DEFAULT_PATH",
'/tmp');
71 define(
"CAS_PGT_STORAGE_FILE_FORMAT_PLAIN",
'plain');
75 define(
"CAS_PGT_STORAGE_FILE_FORMAT_XML",
'xml');
86 define(
"CAS_PGT_STORAGE_DB_DEFAULT_DATABASE_TYPE",
'mysql');
90 define(
"CAS_PGT_STORAGE_DB_DEFAULT_HOSTNAME",
'localhost');
94 define(
"CAS_PGT_STORAGE_DB_DEFAULT_PORT",
'');
98 define(
"CAS_PGT_STORAGE_DB_DEFAULT_DATABASE",
'phpCAS');
102 define(
"CAS_PGT_STORAGE_DB_DEFAULT_TABLE",
'pgt');
116 define(
"PHPCAS_SERVICE_OK",0);
121 define(
"PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE",1);
126 define(
"PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE",2);
131 define(
"PHPCAS_SERVICE_PT_FAILURE",3);
135 define(
"PHPCAS_SERVICE_NOT AVAILABLE",4);
146 define(
"PHPCAS_LANG_ENGLISH",
'english');
147 define(
"PHPCAS_LANG_FRENCH",
'french');
148 define(
"PHPCAS_LANG_GREEK",
'greek');
217 if (version_compare(PHP_VERSION,
'5',
'>='))
219 include_once(dirname(__FILE__).
'/client.php');
223 include_once(dirname(__FILE__).
'/client4.php');
270 function client($server_version,
274 $start_session =
true)
278 phpCAS::traceBegin();
279 if ( is_object($PHPCAS_CLIENT) ) {
280 phpCAS::error($PHPCAS_INIT_CALL[
'method'].
'() has already been called (at '.$PHPCAS_INIT_CALL[
'file'].
':'.$PHPCAS_INIT_CALL[
'line'].
')');
282 if ( gettype($server_version) !=
'string' ) {
283 phpCAS::error(
'type mismatched for parameter $server_version (should be `string\')');
285 if ( gettype($server_hostname) !=
'string' ) {
286 phpCAS::error(
'type mismatched for parameter $server_hostname (should be `string\')');
288 if ( gettype($server_port) !=
'integer' ) {
289 phpCAS::error(
'type mismatched for parameter $server_port (should be `integer\')');
291 if ( gettype($server_uri) !=
'string' ) {
292 phpCAS::error(
'type mismatched for parameter $server_uri (should be `string\')');
296 $dbg = phpCAS::backtrace();
297 $PHPCAS_INIT_CALL = array(
'done' => TRUE,
298 'file' => $dbg[0][
'file'],
299 'line' => $dbg[0][
'line'],
300 'method' => __CLASS__.
'::'.__FUNCTION__);
303 $PHPCAS_CLIENT =
new CASClient($server_version,FALSE,$server_hostname,$server_port,$server_uri,$start_session);
321 function proxy($server_version,
325 $start_session =
true)
329 phpCAS::traceBegin();
330 if ( is_object($PHPCAS_CLIENT) ) {
331 phpCAS::error($PHPCAS_INIT_CALL[
'method'].
'() has already been called (at '.$PHPCAS_INIT_CALL[
'file'].
':'.$PHPCAS_INIT_CALL[
'line'].
')');
333 if ( gettype($server_version) !=
'string' ) {
334 phpCAS::error(
'type mismatched for parameter $server_version (should be `string\')');
336 if ( gettype($server_hostname) !=
'string' ) {
337 phpCAS::error(
'type mismatched for parameter $server_hostname (should be `string\')');
339 if ( gettype($server_port) !=
'integer' ) {
340 phpCAS::error(
'type mismatched for parameter $server_port (should be `integer\')');
342 if ( gettype($server_uri) !=
'string' ) {
343 phpCAS::error(
'type mismatched for parameter $server_uri (should be `string\')');
347 $dbg = phpCAS::backtrace();
348 $PHPCAS_INIT_CALL = array(
'done' => TRUE,
349 'file' => $dbg[0][
'file'],
350 'line' => $dbg[0][
'line'],
351 'method' => __CLASS__.
'::'.__FUNCTION__);
354 $PHPCAS_CLIENT =
new CASClient($server_version,TRUE,$server_hostname,$server_port,$server_uri,$start_session);
378 phpCAS::error(
'type mismatched for parameter $dbg (should be FALSE or the name of the log file)');
382 if ( preg_match(
'/^Win.*/',getenv(
'OS')) ) {
383 if ( isset($_ENV[
'TMP']) ) {
384 $debugDir = $_ENV[
'TMP'].
'/';
385 }
else if ( isset($_ENV[
'TEMP']) ) {
386 $debugDir = $_ENV[
'TEMP'].
'/';
396 if ( empty($PHPCAS_DEBUG[
'unique_id']) ) {
397 $PHPCAS_DEBUG[
'unique_id'] = substr(strtoupper(md5(uniqid(
''))),0,4);
402 phpCAS::trace(
'START ******************');
417 if ( function_exists(
'debug_backtrace') ) {
418 return debug_backtrace();
437 if ( $PHPCAS_DEBUG[
'filename'] ) {
438 for ($i=0;$i<$PHPCAS_DEBUG[
'indent'];$i++) {
441 error_log($PHPCAS_DEBUG[
'unique_id'].
' '.$indent_str.$str.
"\n",3,$PHPCAS_DEBUG[
'filename']);
456 $dbg = phpCAS::backtrace();
460 if ( is_array($dbg) ) {
461 for ( $i=1; $i<
sizeof($dbg); $i++) {
462 if ( is_array($dbg[$i]) ) {
463 if ( $dbg[$i][
'class'] == __CLASS__ ) {
464 $function = $dbg[$i][
'function'];
465 $file = $dbg[$i][
'file'];
466 $line = $dbg[$i][
'line'];
471 echo
"<br />\n<b>phpCAS error</b>: <font color=\"FF0000\"><b>".__CLASS__.
"::".$function.
'(): '.htmlentities($msg).
"</b></font> in <b>".
$file.
"</b> on line <b>".$line.
"</b><br />\n";
482 $dbg = phpCAS::backtrace();
483 phpCAS::log($str.
' ['.basename($dbg[1][
'file']).
':'.$dbg[1][
'line'].
']');
489 function traceBegin()
493 $dbg = phpCAS::backtrace();
495 if ( !empty($dbg[2][
'class']) ) {
496 $str .= $dbg[2][
'class'].
'::';
498 $str .= $dbg[2][
'function'].
'(';
499 if ( is_array($dbg[2][
'args']) ) {
500 foreach ($dbg[2][
'args'] as $index => $arg) {
504 $str .= str_replace(
"\n",
"",var_export($arg,TRUE));
507 $str .=
') ['.basename($dbg[2][
'file']).
':'.$dbg[2][
'line'].
']';
509 $PHPCAS_DEBUG[
'indent'] ++;
517 function traceEnd(
$res=
'')
521 $PHPCAS_DEBUG[
'indent'] --;
522 $dbg = phpCAS::backtrace();
524 $str .=
'<= '.str_replace(
"\n",
"",var_export(
$res,TRUE));
535 phpCAS::log(
'exit()');
536 while ( $PHPCAS_DEBUG[
'indent'] > 0 ) {
538 $PHPCAS_DEBUG[
'indent'] --;
559 function setLang(
$lang)
562 if ( !is_object($PHPCAS_CLIENT) ) {
563 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
565 if ( gettype(
$lang) !=
'string' ) {
566 phpCAS::error(
'type mismatched for parameter $lang (should be `string\')');
568 $PHPCAS_CLIENT->setLang(
$lang);
585 function getVersion()
604 function setHTMLHeader($header)
607 if ( !is_object($PHPCAS_CLIENT) ) {
608 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
610 if ( gettype($header) !=
'string' ) {
611 phpCAS::error(
'type mismatched for parameter $header (should be `string\')');
613 $PHPCAS_CLIENT->setHTMLHeader($header);
621 function setHTMLFooter($footer)
624 if ( !is_object($PHPCAS_CLIENT) ) {
625 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
627 if ( gettype($footer) !=
'string' ) {
628 phpCAS::error(
'type mismatched for parameter $footer (should be `string\')');
630 $PHPCAS_CLIENT->setHTMLHeader($header);
649 function setPGTStorageFile($format=
'',
654 phpCAS::traceBegin();
655 if ( !is_object($PHPCAS_CLIENT) ) {
656 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
658 if ( !$PHPCAS_CLIENT->isProxy() ) {
659 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
661 if ( $PHPCAS_AUTH_CHECK_CALL[
'done'] ) {
662 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'].
')');
664 if ( gettype($format) !=
'string' ) {
665 phpCAS::error(
'type mismatched for parameter $format (should be `string\')');
667 if ( gettype($path) !=
'string' ) {
668 phpCAS::error(
'type mismatched for parameter $format (should be `string\')');
670 $PHPCAS_CLIENT->setPGTStorageFile($format,$path);
689 function setPGTStorageDB(
$user,
699 phpCAS::traceBegin();
700 if ( !is_object($PHPCAS_CLIENT) ) {
701 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
703 if ( !$PHPCAS_CLIENT->isProxy() ) {
704 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
706 if ( $PHPCAS_AUTH_CHECK_CALL[
'done'] ) {
707 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'].
')');
709 if ( gettype(
$user) !=
'string' ) {
710 phpCAS::error(
'type mismatched for parameter $user (should be `string\')');
712 if ( gettype($password) !=
'string' ) {
713 phpCAS::error(
'type mismatched for parameter $password (should be `string\')');
715 if ( gettype($database_type) !=
'string' ) {
716 phpCAS::error(
'type mismatched for parameter $database_type (should be `string\')');
718 if ( gettype($hostname) !=
'string' ) {
719 phpCAS::error(
'type mismatched for parameter $hostname (should be `string\')');
721 if ( gettype($port) !=
'integer' ) {
722 phpCAS::error(
'type mismatched for parameter $port (should be `integer\')');
724 if ( gettype($database) !=
'string' ) {
725 phpCAS::error(
'type mismatched for parameter $database (should be `string\')');
727 if ( gettype($table) !=
'string' ) {
728 phpCAS::error(
'type mismatched for parameter $table (should be `string\')');
730 $PHPCAS_CLIENT->setPGTStorageDB($this,
$user,$password,$hostname,$port,$database,$table);
756 function serviceWeb($url,&$err_code,&$output)
760 phpCAS::traceBegin();
761 if ( !is_object($PHPCAS_CLIENT) ) {
762 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
764 if ( !$PHPCAS_CLIENT->isProxy() ) {
765 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
767 if ( !$PHPCAS_AUTH_CHECK_CALL[
'done'] ) {
768 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()');
770 if ( !$PHPCAS_AUTH_CHECK_CALL[
'result'] ) {
771 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');
773 if ( gettype($url) !=
'string' ) {
774 phpCAS::error(
'type mismatched for parameter $url (should be `string\')');
777 $res = $PHPCAS_CLIENT->serviceWeb($url,$err_code,$output);
779 phpCAS::traceEnd(
$res);
799 function serviceMail($url,$flags,&$err_code,&$err_msg,&$pt)
803 phpCAS::traceBegin();
804 if ( !is_object($PHPCAS_CLIENT) ) {
805 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
807 if ( !$PHPCAS_CLIENT->isProxy() ) {
808 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
810 if ( !$PHPCAS_AUTH_CHECK_CALL[
'done'] ) {
811 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()');
813 if ( !$PHPCAS_AUTH_CHECK_CALL[
'result'] ) {
814 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');
816 if ( gettype($url) !=
'string' ) {
817 phpCAS::error(
'type mismatched for parameter $url (should be `string\')');
820 if ( gettype($flags) !=
'integer' ) {
821 phpCAS::error(
'type mismatched for parameter $flags (should be `integer\')');
824 $res = $PHPCAS_CLIENT->serviceMail($url,$flags,$err_code,$err_msg,$pt);
826 phpCAS::traceEnd(
$res);
843 function checkAuthentication()
847 phpCAS::traceBegin();
848 if ( !is_object($PHPCAS_CLIENT) ) {
849 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
852 $auth = $PHPCAS_CLIENT->checkAuthentication();
855 $dbg = phpCAS::backtrace();
856 $PHPCAS_AUTH_CHECK_CALL = array(
'done' => TRUE,
857 'file' => $dbg[0][
'file'],
858 'line' => $dbg[0][
'line'],
859 'method' => __CLASS__.
'::'.__FUNCTION__,
861 phpCAS::traceEnd(
$auth);
870 function forceAuthentication()
874 phpCAS::traceBegin();
875 if ( !is_object($PHPCAS_CLIENT) ) {
876 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
879 $auth = $PHPCAS_CLIENT->forceAuthentication();
882 $dbg = phpCAS::backtrace();
883 $PHPCAS_AUTH_CHECK_CALL = array(
'done' => TRUE,
884 'file' => $dbg[0][
'file'],
885 'line' => $dbg[0][
'line'],
886 'method' => __CLASS__.
'::'.__FUNCTION__,
890 phpCAS::trace(
'user is not authenticated, redirecting to the CAS server');
891 $PHPCAS_CLIENT->forceAuthentication();
893 phpCAS::trace(
'no need to authenticate (user `'.phpCAS::getUser().
'\' is already authenticated)
');
902 function authenticate()
904 phpCAS::error('this method is deprecated. You should use
'.__CLASS__.'::forceAuthentication() instead');
910 function isAuthenticated()
912 phpCAS::error(
'this method is deprecated. You should use '.__CLASS__.
'::forceAuthentication() instead');
925 if ( !is_object($PHPCAS_CLIENT) ) {
926 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
928 if ( !$PHPCAS_AUTH_CHECK_CALL[
'done'] ) {
929 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::forceAuthentication() or '.__CLASS__.
'::isAuthenticated()');
931 if ( !$PHPCAS_AUTH_CHECK_CALL[
'result'] ) {
932 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');
934 return $PHPCAS_CLIENT->getUser();
943 function getServerLoginURL()
946 if ( !is_object($PHPCAS_CLIENT) ) {
947 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
949 return $PHPCAS_CLIENT->getServerLoginURL();
958 function getServerLogoutURL()
961 if ( !is_object($PHPCAS_CLIENT) ) {
962 phpCAS::error(
'this method should not be called before '.__CLASS__.
'::client() or '.__CLASS__.
'::proxy()');
964 return $PHPCAS_CLIENT->getServerLogoutURL();
971 function logout($url =
"")
975 phpCAS::traceBegin();
976 if ( !is_object($PHPCAS_CLIENT) ) {
977 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::client() or'.__CLASS__.
'::proxy()');
979 $PHPCAS_CLIENT->logout($url);
990 function setFixedCallbackURL($url=
'')
993 phpCAS::traceBegin();
994 if ( !is_object($PHPCAS_CLIENT) ) {
995 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
997 if ( !$PHPCAS_CLIENT->isProxy() ) {
998 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
1000 if ( gettype($url) !=
'string' ) {
1001 phpCAS::error(
'type mismatched for parameter $url (should be `string\')');
1003 $PHPCAS_CLIENT->setCallbackURL($url);
1013 function setFixedServiceURL($url)
1016 phpCAS::traceBegin();
1017 if ( !is_object($PHPCAS_CLIENT) ) {
1018 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
1020 if ( gettype($url) !=
'string' ) {
1021 phpCAS::error(
'type mismatched for parameter $url (should be `string\')');
1023 $PHPCAS_CLIENT->setURL($url);
1030 function getServiceURL()
1033 if ( !is_object($PHPCAS_CLIENT) ) {
1034 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
1036 return($PHPCAS_CLIENT->getURL());
1042 function retrievePT($target_service,&$err_code,&$err_msg)
1045 if ( !is_object($PHPCAS_CLIENT) ) {
1046 phpCAS::error(
'this method should only be called after '.__CLASS__.
'::proxy()');
1048 if ( gettype($target_service) !=
'string' ) {
1049 phpCAS::error(
'type mismatched for parameter $target_service(should be `string\')');
1051 return($PHPCAS_CLIENT->retrievePT($target_service,$err_code,$err_msg));