9 include_once(dirname(__FILE__).
'/languages/languages.php');
12 include_once(dirname(__FILE__).
'/PGTStorage/pgt-main.php');
55 $str = str_replace(
'__PHPCAS_VERSION__',phpCAS::getVersion(),$str);
83 (empty($this->_output_header)
84 ?
'<html><head><title>__TITLE__</title></head><body><h1>__TITLE__</h1>'
85 : $this->output_header)
110 ?(
'<hr><address>phpCAS __PHPCAS_VERSION__ '.$this->
getString(
CAS_STR_USING_SERVER).
' <a href="__SERVER_BASE_URL__">__SERVER_BASE_URL__</a> (CAS __CAS_VERSION__)</a></address></body></html>')
111 :$this->_output_footer);
123 $this->_output_header = $header;
135 $this->_output_footer = $footer;
167 if ( empty($this->_lang) )
197 if ( !isset($this->
_strings[$str]) ) {
198 trigger_error(
'string `'.$str.
'\' not defined
for language `
'.$this->getLang().'\
'',E_USER_ERROR);
215 include_once(dirname(__FILE__).
'/languages/'.
$lang.
'.php');
218 trigger_error(
'language `'.
$lang.
'\' is not implemented
',E_USER_ERROR);
220 $this->_lang = $lang;
224 // ########################################################################
226 // ########################################################################
256 var $_server = array(
258 'hostname
' => 'none
',
268 function getServerVersion()
270 return $this->_server['version
'];
278 function getServerHostname()
279 { return $this->_server['hostname
']; }
286 function getServerPort()
287 { return $this->_server['port
']; }
294 function getServerURI()
295 { return $this->_server['uri
']; }
302 function getServerBaseURL()
304 // the URL is build only when needed
305 if ( empty($this->_server['base_url
']) ) {
308 //$this->_server['base_url
'] = 'https:
309 $this->_server[
'base_url'] =
'https://'
310 .$this->getServerHostname()
312 .$this->getServerPort()
313 .$this->getServerURI();
316 return $this->_server[
'base_url'];
327 phpCAS::traceBegin();
329 if ( empty($this->_server[
'login_url']) ) {
331 $this->_server[
'login_url'] .=
'login?service=';
332 $this->_server[
'login_url'] .= preg_replace(
'/&/',
'%26',$this->
getURL());
334 $this->_server[
'login_url'] .=
'&gateway=true';
337 phpCAS::traceEnd($this->_server[
'login_url']);
338 return $this->_server[
'login_url'];
349 if ( empty($this->_server[
'service_validate_url']) ) {
352 $this->_server[
'service_validate_url'] = $this->
getServerBaseURL().
'validate';
355 $this->_server[
'service_validate_url'] = $this->
getServerBaseURL().
'serviceValidate';
359 return $this->_server[
'service_validate_url'].
'?service='.preg_replace(
'/&/',
'%26',$this->
getURL());
370 if ( empty($this->_server[
'proxy_validate_url']) ) {
373 $this->_server[
'proxy_validate_url'] =
'';
376 $this->_server[
'proxy_validate_url'] = $this->
getServerBaseURL().
'proxyValidate';
380 return $this->_server[
'proxy_validate_url'].
'?service='.preg_replace(
'/&/',
'%26',$this->
getURL());
391 if ( empty($this->_server[
'proxy_url']) ) {
394 $this->_server[
'proxy_url'] =
'';
401 return $this->_server[
'proxy_url'];
412 if ( empty($this->_server[
'logout_url']) ) {
415 return $this->_server[
'logout_url'];
440 $start_session =
true)
442 phpCAS::traceBegin();
445 if ($start_session) {
449 $this->_proxy = $proxy;
452 switch ($server_version) {
455 phpCAS::error(
'CAS proxies are not supported in CAS '
461 phpCAS::error(
'this version of CAS (`'
463 .
'\') is not supported by phpCAS
'
464 .phpCAS::getVersion());
466 $this->_server['version
'] = $server_version;
469 if ( empty($server_hostname)
470 || !preg_match('/[\.\d\-abcdefghijklmnopqrstuvwxyz]*/
',$server_hostname) ) {
471 phpCAS::error('bad CAS server hostname (`
'.$server_hostname.'\
')');
473 $this->_server[
'hostname'] = $server_hostname;
476 if ( $server_port == 0
477 || !is_int($server_port) ) {
478 phpCAS::error(
'bad CAS server port (`'.$server_hostname.
'\')
');
480 $this->_server['port
'] = $server_port;
483 if ( !preg_match('/[\.\d\-_abcdefghijklmnopqrstuvwxyz\/]*/
',$server_uri) ) {
484 phpCAS::error('bad CAS server URI (`
'.$server_uri.'\
')');
487 $server_uri = preg_replace(
'/\/\//',
'/',
'/'.$server_uri.
'/');
488 $this->_server[
'uri'] = $server_uri;
497 if ( $_SERVER[
'HTTPS'] !=
'on' ) {
498 phpCAS::error(
'CAS proxies must be secured to use phpCAS; PGT\'s will not be received from the CAS server');
502 $ticket =
$_GET[
'ticket'];
504 if( preg_match(
'/^ST-/',$ticket)) {
505 phpCAS::trace(
'ST \''.$ticket.
'\' found
');
507 $this->setST($ticket);
509 // in a second time check for a Proxy Ticket (CAS >= 2.0)
510 else if( ($this->getServerVersion()!=CAS_VERSION_1_0) && preg_match('/^PT-/
',$ticket) ) {
511 phpCAS::trace('PT \
''.$ticket.
'\' found
');
512 $this->setPT($ticket);
514 // ill-formed ticket, halt
515 else if ( !empty($ticket) ) {
516 phpCAS::error('ill-formed ticket found in the URL (ticket=`
'.htmlentities($ticket).'\
')');
519 unset(
$_GET[
'ticket']);
555 $this->_user =
$user;
567 if ( empty($this->_user) ) {
568 phpCAS::error(
'this method should be used only after '.__CLASS__.
'::forceAuthentication() or '.__CLASS__.
'::isAuthenticated()');
581 phpCAS::traceBegin();
585 phpCAS::trace(
'no need to authenticate');
589 unset(
$_SESSION[
'phpCAS'][
'auth_checked']);
594 phpCAS::traceEnd(
$res);
605 phpCAS::traceBegin();
608 phpCAS::trace(
'user is authenticated');
610 }
else if (isset(
$_SESSION[
'phpCAS'][
'auth_checked'])) {
612 unset(
$_SESSION[
'phpCAS'][
'auth_checked']);
615 $_SESSION[
'phpCAS'][
'auth_checked'] =
true;
620 phpCAS::traceEnd(
$res);
634 phpCAS::traceBegin();
641 phpCAS::trace(
'user was already authenticated, no need to look for tickets');
643 } elseif ( $this->
hasST() ) {
645 phpCAS::trace(
'ST `'.$this->
getST().
'\' is present
');
646 $this->validateST($validate_url,$text_response,$tree_response); // if it fails, it halts
647 phpCAS::trace('ST `
'.$this->getST().'\
' was validated');
649 $this->
validatePGT($validate_url,$text_response,$tree_response);
650 phpCAS::trace(
'PGT `'.$this->
getPGT().
'\' was validated
');
651 $_SESSION['phpCAS
']['pgt
'] = $this->getPGT();
653 $_SESSION['phpCAS
']['user
'] = $this->getUser();
655 } elseif ( $this->hasPT() ) {
656 // if a Proxy Ticket was given, validate it
657 phpCAS::trace('PT `
'.$this->getPT().'\
' is present');
658 $this->
validatePT($validate_url,$text_response,$tree_response);
659 phpCAS::trace(
'PT `'.$this->
getPT().
'\' was validated
');
660 if ( $this->isProxy() ) {
661 $this->validatePGT($validate_url,$text_response,$tree_response); // idem
662 phpCAS::trace('PGT `
'.$this->getPGT().'\
' was validated');
669 phpCAS::trace(
'no ticket found');
672 phpCAS::traceEnd(
$res);
688 phpCAS::traceBegin();
702 phpCAS::trace(
'user = `'.
$_SESSION[
'phpCAS'][
'user'].
'\', PGT = `
'.$_SESSION['phpCAS
']['pgt
'].'\
'');
704 } elseif ( !empty(
$_SESSION[
'phpCAS'][
'user']) && empty(
$_SESSION[
'phpCAS'][
'pgt']) ) {
706 phpCAS::trace(
'username found (`'.
$_SESSION[
'phpCAS'][
'user'].
'\') but PGT is empty
');
707 // unset all tickets to enforce authentication
708 unset($_SESSION['phpCAS
']);
711 } elseif ( empty($_SESSION['phpCAS
']['user
']) && !empty($_SESSION['phpCAS
']['pgt
']) ) {
712 // these two variables should be empty or not empty at the same time
713 phpCAS::trace('PGT found (`
'.$_SESSION['phpCAS
']['pgt
'].'\
') but username is empty');
719 phpCAS::trace(
'neither user not PGT found');
723 if ( !empty(
$_SESSION[
'phpCAS'][
'user']) ) {
726 phpCAS::trace(
'user = `'.
$_SESSION[
'phpCAS'][
'user'].
'\'');
729 phpCAS::trace(
'no user found');
733 phpCAS::traceEnd(
$auth);
745 phpCAS::traceBegin();
747 header(
'Location: '.$cas_url);
762 phpCAS::traceBegin();
767 $url =
'?service=' . $url;
769 header(
'Location: '.$cas_url . $url);
819 { $this->_st = $st; }
827 {
return !empty($this->_st); }
852 function validateST($validate_url,&$text_response,&$tree_response)
854 phpCAS::traceBegin();
859 $validate_url .=
'&pgtUrl='.$this->getCallbackURL();
863 if ( !$this->
readURL($validate_url,
'',$headers,$text_response,$err_msg) ) {
864 phpCAS::trace(
'could not open URL \''.$validate_url.
'\' to validate (
'.$err_msg.')
');
865 $this->authError('ST not validated
',
867 TRUE/*$no_response*/);
870 // analyze the result depending on the version
871 switch ($this->getServerVersion()) {
872 case CAS_VERSION_1_0:
873 if (preg_match('/^no\
n/
',$text_response)) {
874 phpCAS::trace('ST has not been validated
');
875 $this->authError('ST not validated
',
877 FALSE/*$no_response*/,
878 FALSE/*$bad_response*/,
881 if (!preg_match('/^yes\
n/
',$text_response)) {
882 phpCAS::trace('ill-formed response
');
883 $this->authError('ST not validated
',
885 FALSE/*$no_response*/,
886 TRUE/*$bad_response*/,
889 // ST has been validated, extract the user name
890 $arr = preg_split('/\
n/
',$text_response);
891 $this->setUser(trim($arr[1]));
893 case CAS_VERSION_2_0:
894 // read the response of the CAS server into a DOM object
895 if ( !($dom = domxml_open_mem($text_response))) {
897 $this->authError('ST not validated
',
899 FALSE/*$no_response*/,
900 TRUE/*$bad_response*/,
903 // read the root node of the XML tree
904 if ( !($tree_response = $dom->document_element()) ) {
905 phpCAS::trace('document_element() failed
');
906 $this->authError('ST not validated
',
908 FALSE/*$no_response*/,
909 TRUE/*$bad_response*/,
912 // insure that tag name is 'serviceResponse
'
913 if ( $tree_response->node_name(true) != 'serviceResponse
' ) {
914 phpCAS::trace('bad XML root node (should be `serviceResponse\
' instead of `'.$tree_response->node_name(
true).
'\'');
921 if (
sizeof($success_elements = $tree_response->get_elements_by_tagname(
"authenticationSuccess")) != 0) {
923 if (
sizeof($user_elements = $success_elements[0]->get_elements_by_tagname(
"user")) == 0) {
924 phpCAS::trace(
'<authenticationSuccess> found, but no <user>');
931 $user = trim($user_elements[0]->get_content());
932 phpCAS::trace(
'user = `'.
$user);
935 }
else if (
sizeof($failure_elements = $tree_response->get_elements_by_tagname(
"authenticationFailure")) != 0) {
936 phpCAS::trace(
'<authenticationFailure> found');
943 $failure_elements[0]->get_attribute(
'code'),
944 trim($failure_elements[0]->get_content()));
946 phpCAS::trace(
'neither <authenticationSuccess> nor <authenticationFailure> found');
957 phpCAS::traceEnd(TRUE);
994 return $this->_proxy;
1021 {
return $this->_pgt; }
1028 function setPGT($pgt)
1029 { $this->_pgt = $pgt; }
1037 {
return !empty($this->_pgt); }
1061 var $_callback_mode = FALSE;
1070 function setCallbackMode($callback_mode)
1072 $this->_callback_mode = $callback_mode;
1083 function isCallbackMode()
1085 return $this->_callback_mode;
1096 var $_callback_url =
'';
1107 function getCallbackURL()
1110 if ( empty($this->_callback_url) ) {
1113 $final_uri =
'https://';
1118 if(empty($_SERVER[
'HTTP_X_FORWARDED_SERVER'])){
1122 if (empty($_SERVER[
'SERVER_NAME'])) {
1123 $final_uri .= $_SERVER[
'HTTP_HOST'];
1125 $final_uri .= $_SERVER[
'SERVER_NAME'];
1128 $final_uri .= $_SERVER[
'HTTP_X_FORWARDED_SERVER'];
1130 if ( ($_SERVER[
'HTTPS']==
'on' && $_SERVER[
'SERVER_PORT']!=443)
1131 || ($_SERVER[
'HTTPS']!=
'on' && $_SERVER[
'SERVER_PORT']!=80) ) {
1133 $final_uri .= $_SERVER[
'SERVER_PORT'];
1138 $this->setCallbackURL($final_uri);
1140 return $this->_callback_url;
1150 function setCallbackURL($url)
1152 return $this->_callback_url = $url;
1163 phpCAS::traceBegin();
1164 $this->printHTMLHeader(
'phpCAS callback');
1165 $pgt_iou =
$_GET[
'pgtIou'];
1166 $pgt =
$_GET[
'pgtId'];
1167 phpCAS::trace(
'Storing PGT `'.$pgt.
'\' (
id=`
'.$pgt_iou.'\
')');
1168 echo
'<p>Storing PGT `'.$pgt.
'\' (
id=`
'.$pgt_iou.'\
').</p>';
1169 $this->storePGT($pgt,$pgt_iou);
1170 $this->printHTMLFooter();
1171 phpCAS::traceExit();
1192 var $_pgt_storage = null;
1200 function initPGTStorage()
1203 if ( !is_object($this->_pgt_storage) ) {
1204 $this->setPGTStorageFile();
1208 $this->_pgt_storage->init();
1219 function storePGT($pgt,$pgt_iou)
1222 $this->initPGTStorage();
1224 $this->_pgt_storage->write($pgt,$pgt_iou);
1236 function loadPGT($pgt_iou)
1239 $this->initPGTStorage();
1241 return $this->_pgt_storage->read($pgt_iou);
1253 function setPGTStorageFile($format=
'',
1257 if ( is_object($this->_pgt_storage) ) {
1258 phpCAS::error(
'PGT storage already defined');
1291 if ( is_object($this->_pgt_storage) ) {
1292 phpCAS::error(
'PGT storage already defined');
1296 trigger_error(
'PGT storage into database is an experimental feature, use at your own risk',E_USER_WARNING);
1299 $this->_pgt_storage = &
new PGTStorageDB($this,
$user,$password,$database_type,$hostname,$port,$database,$table);
1318 function validatePGT(&$validate_url,$text_response,$tree_response)
1320 phpCAS::traceBegin();
1321 if (
sizeof($arr = $tree_response->get_elements_by_tagname(
"proxyGrantingTicket")) == 0) {
1322 phpCAS::trace(
'<proxyGrantingTicket> not found');
1324 $this->authError(
'Ticket validated but no PGT Iou transmitted',
1331 $pgt_iou = trim($arr[0]->get_content());
1332 $pgt = $this->loadPGT($pgt_iou);
1333 if ( $pgt == FALSE ) {
1334 phpCAS::trace(
'could not load PGT');
1335 $this->authError(
'PGT Iou was transmitted but PGT could not be retrieved',
1341 $this->setPGT($pgt);
1343 phpCAS::traceEnd(TRUE);
1362 function retrievePT($target_service,&$err_code,&$err_msg)
1364 phpCAS::traceBegin();
1373 $cas_url = $this->getServerProxyURL().
'?targetService='.preg_replace(
'/&/',
'%26',$target_service).
'&pgt='.$this->getPGT();
1376 if ( !$this->readURL($cas_url,
'',$headers,$cas_response,$err_msg) ) {
1377 phpCAS::trace(
'could not open URL \''.$cas_url.
'\' to validate (
'.$err_msg.')
');
1378 $err_code = PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE;
1379 $err_msg = 'could not retrieve PT (no response from the CAS server)
';
1380 phpCAS::traceEnd(FALSE);
1384 $bad_response = FALSE;
1386 if ( !$bad_response ) {
1387 // read the response of the CAS server into a DOM object
1388 if ( !($dom = @domxml_open_mem($cas_response))) {
1391 $bad_response = TRUE;
1395 if ( !$bad_response ) {
1396 // read the root node of the XML tree
1397 if ( !($root = $dom->document_element()) ) {
1398 phpCAS::trace('document_element() failed
');
1400 $bad_response = TRUE;
1404 if ( !$bad_response ) {
1405 // insure that tag name is 'serviceResponse
'
1406 if ( $root->node_name(true) != 'serviceResponse
' ) {
1407 phpCAS::trace('node_name() failed
');
1409 $bad_response = TRUE;
1413 if ( !$bad_response ) {
1414 // look for a proxySuccess tag
1415 if ( sizeof($arr = $root->get_elements_by_tagname("proxySuccess")) != 0) {
1416 // authentication succeded, look for a proxyTicket tag
1417 if ( sizeof($arr = $root->get_elements_by_tagname("proxyTicket")) != 0) {
1418 $err_code = PHPCAS_SERVICE_OK;
1420 $pt = trim($arr[0]->get_content());
1421 phpCAS::traceEnd($pt);
1424 phpCAS::trace('<proxySuccess> was found, but not <proxyTicket>
');
1427 // look for a proxyFailure tag
1428 else if ( sizeof($arr = $root->get_elements_by_tagname("proxyFailure")) != 0) {
1429 // authentication failed, extract the error
1430 $err_code = PHPCAS_SERVICE_PT_FAILURE;
1431 $err_msg = 'PT retrieving failed (code=`
'
1432 .$arr[0]->get_attribute('code
')
1434 .trim($arr[0]->get_content())
1436 phpCAS::traceEnd(FALSE);
1439 phpCAS::trace('neither <proxySuccess> nor <proxyFailure> found
');
1443 // at this step, we are sure that the response of the CAS server was ill-formed
1444 $err_code = PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE;
1445 $err_msg = 'Invalid response from the CAS server (response=`
'.$cas_response.'\
')';
1447 phpCAS::traceEnd(FALSE);
1470 function readURL($url,$cookies,&$headers,&$body,&$err_msg)
1472 phpCAS::traceBegin();
1480 $ch = curl_init($url);
1483 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
1485 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
1488 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
1490 curl_setopt($ch, CURLOPT_HEADER, 1);
1492 if ( is_array($cookies) ) {
1493 curl_setopt($ch,CURLOPT_COOKIE,implode(
';',$cookies));
1496 $buf = curl_exec ($ch);
1497 if ( $buf === FALSE ) {
1498 phpCAS::trace(
'cur_exec() failed');
1499 $err_msg =
'CURL error #'.curl_errno($ch).
': '.curl_error($ch);
1510 for ($i=0; $i<strlen($buf); $i++) {
1511 if ( $buf[$i] == chr(13) )
1512 if ( $buf[$i+1] == chr(10) )
1513 if ( $buf[$i+2] == chr(13) )
1514 if ( $buf[$i+3] == chr(10) ) {
1521 if ( $pos === FALSE ) {
1523 $err_msg =
'no header found';
1524 phpCAS::trace($err_msg);
1528 $headers = preg_split (
"/[\n\r]+/",substr($buf,0,$pos));
1530 $body = substr($buf,$pos+4);
1534 phpCAS::traceEnd(
$res);
1553 function serviceWeb($url,&$err_code,&$output)
1555 phpCAS::traceBegin();
1557 $pt = $this->retrievePT($url,$err_code,$output);
1564 phpCAS::trace(
'PT was not retrieved correctly');
1568 if ( is_array(
$_SESSION[
'phpCAS'][
'services'][$url][
'cookies']) ) {
1569 foreach (
$_SESSION[
'phpCAS'][
'services'][$url][
'cookies'] as $name => $val ) {
1570 $cookies[] = $name.
'='.$val;
1575 if ( strstr($url,
'?') === FALSE ) {
1576 $service_url = $url.
'?ticket='.$pt;
1578 $service_url = $url.
'&ticket='.$pt;
1581 phpCAS::trace(
'reading URL`'.$service_url.
'\'');
1582 if ( !$this->readURL($service_url,$cookies,$headers,$output,$err_msg) ) {
1583 phpCAS::trace(
'could not read URL`'.$service_url.
'\'');
1584 $err_code = PHPCAS_SERVICE_NOT_AVAILABLE;
1592 phpCAS::trace(
'URL`'.$service_url.
'\' has been read, storing cookies:
');
1593 foreach ( $headers as $header ) {
1594 // test if the header is a cookie
1595 if ( preg_match('/^Set-Cookie:/
',$header) ) {
1596 // the header is a cookie, remove the beginning
1597 $header_val = preg_replace('/^Set-Cookie: */
','',$header);
1598 // extract interesting information
1599 $name_val = strtok($header_val,';
');
1600 // extract the name and the value of the cookie
1601 $cookie_name = strtok($name_val,'=
');
1602 $cookie_val = strtok('=
');
1604 $_SESSION['phpCAS
']['services
'][$url]['cookies
'][$cookie_name] = $cookie_val;
1605 phpCAS::trace($cookie_name.' ->
'.$cookie_val);
1611 phpCAS::traceEnd($res);
1633 function serviceMail($url,$flags,&$err_code,&$err_msg,&$pt)
1635 phpCAS::traceBegin();
1636 // at first retrieve a PT
1637 $pt = $this->retrievePT($target_service,$err_code,$output);
1641 // test if PT was retrieved correctly
1643 // note: $err_code and $err_msg are filled by CASClient::retrievePT()
1644 phpCAS::trace('PT was not retrieved correctly
');
1646 phpCAS::trace('opening IMAP URL `
'.$url.'\
'...');
1647 $stream = @imap_open($url,$this->getUser(),$pt,$flags);
1649 phpCAS::trace(
'could not open URL');
1650 $err_code = PHPCAS_SERVICE_NOT_AVAILABLE;
1654 var_export(imap_errors(),TRUE));
1658 phpCAS::trace(
'ok');
1662 phpCAS::traceEnd($stream);
1698 {
return $this->_pt; }
1706 { $this->_pt = $pt; }
1714 {
return !empty($this->_pt); }
1732 function validatePT(&$validate_url,&$text_response,&$tree_response)
1734 phpCAS::traceBegin();
1736 $validate_url = $this->getServerProxyValidateURL().
'&ticket='.$this->getPT();
1738 if ( $this->isProxy() ) {
1740 $validate_url .=
'&pgtUrl='.$this->getCallbackURL();
1744 if ( !$this->readURL($validate_url,
'',$headers,$text_response,$err_msg) ) {
1745 phpCAS::trace(
'could not open URL \''.$validate_url.
'\' to validate (
'.$err_msg.')
');
1746 $this->authError('PT not validated
',
1748 TRUE/*$no_response*/);
1751 // read the response of the CAS server into a DOM object
1752 if ( !($dom = domxml_open_mem($text_response))) {
1754 $this->authError('PT not validated
',
1756 FALSE/*$no_response*/,
1757 TRUE/*$bad_response*/,
1760 // read the root node of the XML tree
1761 if ( !($tree_response = $dom->document_element()) ) {
1763 $this->authError('PT not validated
',
1765 FALSE/*$no_response*/,
1766 TRUE/*$bad_response*/,
1769 // insure that tag name is 'serviceResponse
'
1770 if ( $tree_response->node_name(true) != 'serviceResponse
' ) {
1772 $this->authError('PT not validated
',
1774 FALSE/*$no_response*/,
1775 TRUE/*$bad_response*/,
1778 if ( sizeof($arr = $tree_response->get_elements_by_tagname("authenticationSuccess")) != 0) {
1779 // authentication succeded, extract the user name
1780 if ( sizeof($arr = $tree_response->get_elements_by_tagname("user")) == 0) {
1781 // no user specified => error
1782 $this->authError('PT not validated
',
1784 FALSE/*$no_response*/,
1785 TRUE/*$bad_response*/,
1788 $this->setUser(trim($arr[0]->get_content()));
1790 } else if ( sizeof($arr = $tree_response->get_elements_by_tagname("authenticationFailure")) != 0) {
1791 // authentication succeded, extract the error code and message
1792 $this->authError('PT not validated
',
1794 FALSE/*$no_response*/,
1795 FALSE/*$bad_response*/,
1797 $arr[0]->get_attribute('code
')/*$err_code*/,
1798 trim($arr[0]->get_content())/*$err_msg*/);
1800 $this->authError('PT not validated
',
1802 FALSE/*$no_response*/,
1803 TRUE/*$bad_response*/,
1807 // at this step, PT has been validated and $this->_user has been set,
1809 phpCAS::traceEnd(TRUE);
1815 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1819 // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1826 // ########################################################################
1828 // ########################################################################
1848 phpCAS::traceBegin();
1849 // the URL is built when needed only
1850 if ( empty($this->_url) ) {
1852 // remove the ticket if present in the URL
1853 $final_uri = ($_SERVER['HTTPS
'] == 'on
') ? 'https
' : 'http
';
1858 if(empty($_SERVER[
'HTTP_X_FORWARDED_SERVER'])){
1862 if (empty($_SERVER[
'SERVER_NAME'])) {
1863 $final_uri .= $_SERVER[
'HTTP_HOST'];
1865 $final_uri .= $_SERVER[
'SERVER_NAME'];
1868 $final_uri .= $_SERVER[
'HTTP_X_FORWARDED_SERVER'];
1870 if ( ($_SERVER[
'HTTPS']==
'on' && $_SERVER[
'SERVER_PORT']!=443)
1871 || ($_SERVER[
'HTTPS']!=
'on' && $_SERVER[
'SERVER_PORT']!=80) ) {
1873 $final_uri .= $_SERVER[
'SERVER_PORT'];
1876 $final_uri .= strtok($_SERVER[
'REQUEST_URI'],
"?");
1877 $cgi_params =
'?'.strtok(
"?");
1879 $cgi_params = preg_replace(
'/&ticket=[^&]*/',
'',$cgi_params);
1880 $cgi_params = preg_replace(
'/\?ticket=[^&;]*/',
'?',$cgi_params);
1881 $cgi_params = preg_replace(
'/\?$/',
'',$cgi_params);
1882 $final_uri .= $cgi_params;
1883 $this->setURL($final_uri);
1885 phpCAS::traceEnd($this->_url);
1896 function setURL($url)
1919 function authError(
$failure,$cas_url,$no_response,$bad_response=
'',$cas_response=
'',$err_code=
'',$err_msg=
'')
1921 phpCAS::traceBegin();
1925 phpCAS::trace(
'CAS URL: '.$cas_url);
1926 phpCAS::trace(
'Authentication failure: '.
$failure);
1927 if ( $no_response ) {
1928 phpCAS::trace(
'Reason: no response from the CAS server');
1930 if ( $bad_response ) {
1931 phpCAS::trace(
'Reason: bad response from the CAS server');
1933 switch ($this->getServerVersion()) {
1935 phpCAS::trace(
'Reason: CAS error');
1938 if ( empty($err_code) )
1939 phpCAS::trace(
'Reason: no CAS error');
1941 phpCAS::trace(
'Reason: ['.$err_code.
'] CAS error: '.$err_msg);
1945 phpCAS::trace(
'CAS response: '.$cas_response);
1947 $this->printHTMLFooter();
1948 phpCAS::traceExit();