📄 client.php
字号:
<?php/** * @file CAS/client.php * Main class of the phpCAS library */// include internationalization stuffinclude_once(dirname(__FILE__).'/languages/languages.php');// include PGT storage classesinclude_once(dirname(__FILE__).'/PGTStorage/pgt-main.php');/** * @class CASClient * The CASClient class is a client interface that provides CAS authentication * to PHP applications. * * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr> */class CASClient{ // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // XX XX // XX CONFIGURATION XX // XX XX // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX // ######################################################################## // HTML OUTPUT // ######################################################################## /** * @addtogroup internalOutput * @{ */ /** * This method filters a string by replacing special tokens by appropriate values * and prints it. The corresponding tokens are taken into account: * - __CAS_VERSION__ * - __PHPCAS_VERSION__ * - __SERVER_BASE_URL__ * * Used by CASClient::PrintHTMLHeader() and CASClient::printHTMLFooter(). * * @param $str the string to filter and output * * @private */ function HTMLFilterOutput($str) { $str = str_replace('__CAS_VERSION__',$this->getServerVersion(),$str); $str = str_replace('__PHPCAS_VERSION__',phpCAS::getVersion(),$str); $str = str_replace('__SERVER_BASE_URL__',$this->getServerBaseURL(),$str); echo $str; } /** * A string used to print the header of HTML pages. Written by CASClient::setHTMLHeader(), * read by CASClient::printHTMLHeader(). * * @hideinitializer * @private * @see CASClient::setHTMLHeader, CASClient::printHTMLHeader() */ var $_output_header = ''; /** * This method prints the header of the HTML output (after filtering). If * CASClient::setHTMLHeader() was not used, a default header is output. * * @param $title the title of the page * * @see HTMLFilterOutput() * @private */ function printHTMLHeader($title) { $this->HTMLFilterOutput(str_replace('__TITLE__', $title, (empty($this->_output_header) ? '<html><head><title>__TITLE__</title></head><body><h1>__TITLE__</h1>' : $this->_output_header) ) ); } /** * A string used to print the footer of HTML pages. Written by CASClient::setHTMLFooter(), * read by printHTMLFooter(). * * @hideinitializer * @private * @see CASClient::setHTMLFooter, CASClient::printHTMLFooter() */ var $_output_footer = ''; /** * This method prints the footer of the HTML output (after filtering). If * CASClient::setHTMLFooter() was not used, a default footer is output. * * @see HTMLFilterOutput() * @private */ function printHTMLFooter() { $this->HTMLFilterOutput(empty($this->_output_footer) ?('<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>') :$this->_output_footer); } /** * This method set the HTML header used for all outputs. * * @param $header the HTML header. * * @public */ function setHTMLHeader($header) { $this->_output_header = $header; } /** * This method set the HTML footer used for all outputs. * * @param $footer the HTML footer. * * @public */ function setHTMLFooter($footer) { $this->_output_footer = $footer; } /** @} */ // ######################################################################## // INTERNATIONALIZATION // ######################################################################## /** * @addtogroup internalLang * @{ */ /** * A string corresponding to the language used by phpCAS. Written by * CASClient::setLang(), read by CASClient::getLang(). * @note debugging information is always in english (debug purposes only). * * @hideinitializer * @private * @sa CASClient::_strings, CASClient::getString() */ var $_lang = ''; /** * This method returns the language used by phpCAS. * * @return a string representing the language * * @private */ function getLang() { if ( empty($this->_lang) ) $this->setLang(PHPCAS_LANG_DEFAULT); return $this->_lang; } /** * array containing the strings used by phpCAS. Written by CASClient::setLang(), read by * CASClient::getString() and used by CASClient::setLang(). * * @note This array is filled by instructions in CAS/languages/<$this->_lang>.php * * @private * @see CASClient::_lang, CASClient::getString(), CASClient::setLang(), CASClient::getLang() */ var $_strings; /** * This method returns a string depending on the language. * * @param $str the index of the string in $_string. * * @return the string corresponding to $index in $string. * * @private */ function getString($str) { // call CASclient::getLang() to be sure the language is initialized $this->getLang(); if ( !isset($this->_strings[$str]) ) { trigger_error('string `'.$str.'\' not defined for language `'.$this->getLang().'\'',E_USER_ERROR); } return $this->_strings[$str]; } /** * This method is used to set the language used by phpCAS. * @note Can be called only once. * * @param $lang a string representing the language. * * @public * @sa CAS_LANG_FRENCH, CAS_LANG_ENGLISH */ function setLang($lang) { // include the corresponding language file include_once(dirname(__FILE__).'/languages/'.$lang.'.php'); if ( !is_array($this->_strings) ) { trigger_error('language `'.$lang.'\' is not implemented',E_USER_ERROR); } $this->_lang = $lang; } /** @} */ // ######################################################################## // CAS SERVER CONFIG // ######################################################################## /** * @addtogroup internalConfig * @{ */ /** * a record to store information about the CAS server. * - $_server["version"]: the version of the CAS server * - $_server["hostname"]: the hostname of the CAS server * - $_server["port"]: the port the CAS server is running on * - $_server["uri"]: the base URI the CAS server is responding on * - $_server["base_url"]: the base URL of the CAS server * - $_server["login_url"]: the login URL of the CAS server * - $_server["service_validate_url"]: the service validating URL of the CAS server * - $_server["proxy_url"]: the proxy URL of the CAS server * - $_server["proxy_validate_url"]: the proxy validating URL of the CAS server * - $_server["logout_url"]: the logout URL of the CAS server * * $_server["version"], $_server["hostname"], $_server["port"] and $_server["uri"] * are written by CASClient::CASClient(), read by CASClient::getServerVersion(), * CASClient::getServerHostname(), CASClient::getServerPort() and CASClient::getServerURI(). * * The other fields are written and read by CASClient::getServerBaseURL(), * CASClient::getServerLoginURL(), CASClient::getServerServiceValidateURL(), * CASClient::getServerProxyValidateURL() and CASClient::getServerLogoutURL(). * * @hideinitializer * @private */ var $_server = array( 'version' => -1, 'hostname' => 'none', 'port' => -1, 'uri' => 'none' ); /** * This method is used to retrieve the version of the CAS server. * @return the version of the CAS server. * @private */ function getServerVersion() { return $this->_server['version']; } /** * This method is used to retrieve the hostname of the CAS server. * @return the hostname of the CAS server. * @private */ function getServerHostname() { return $this->_server['hostname']; } /** * This method is used to retrieve the port of the CAS server. * @return the port of the CAS server. * @private */ function getServerPort() { return $this->_server['port']; } /** * This method is used to retrieve the URI of the CAS server. * @return a URI. * @private */ function getServerURI() { return $this->_server['uri']; } /** * This method is used to retrieve the base URL of the CAS server. * @return a URL. * @private */ function getServerBaseURL() { // the URL is build only when needed if ( empty($this->_server['base_url']) ) { $this->_server['base_url'] = 'https://' .$this->getServerHostname() .':' .$this->getServerPort() .$this->getServerURI(); } return $this->_server['base_url']; } /** * This method is used to retrieve the login URL of the CAS server. * @param $gateway true to check authentication, false to force it * @return a URL. * @private */ function getServerLoginURL($gateway=false) { phpCAS::traceBegin(); // the URL is build only when needed if ( empty($this->_server['login_url']) ) { $this->_server['login_url'] = $this->getServerBaseURL(); $this->_server['login_url'] .= 'login?service=';// $this->_server['login_url'] .= preg_replace('/&/','%26',$this->getURL()); $this->_server['login_url'] .= urlencode($this->getURL()); if ($gateway) { $this->_server['login_url'] .= '&gateway=true'; } } phpCAS::traceEnd($this->_server['login_url']); return $this->_server['login_url']; } /** * This method sets the login URL of the CAS server. * @param $url the login URL * @private * @since 0.4.21 by Wyman Chan */ function setServerLoginURL($url) { return $this->_server['login_url'] = $url; } /** * This method is used to retrieve the service validating URL of the CAS server. * @return a URL. * @private */ function getServerServiceValidateURL() { // the URL is build only when needed if ( empty($this->_server['service_validate_url']) ) { switch ($this->getServerVersion()) { case CAS_VERSION_1_0: $this->_server['service_validate_url'] = $this->getServerBaseURL().'validate'; break; case CAS_VERSION_2_0: $this->_server['service_validate_url'] = $this->getServerBaseURL().'serviceValidate'; break; } }// return $this->_server['service_validate_url'].'?service='.preg_replace('/&/','%26',$this->getURL()); return $this->_server['service_validate_url'].'?service='.urlencode($this->getURL()); } /** * This method is used to retrieve the proxy validating URL of the CAS server. * @return a URL. * @private */ function getServerProxyValidateURL() { // the URL is build only when needed if ( empty($this->_server['proxy_validate_url']) ) { switch ($this->getServerVersion()) { case CAS_VERSION_1_0: $this->_server['proxy_validate_url'] = ''; break; case CAS_VERSION_2_0: $this->_server['proxy_validate_url'] = $this->getServerBaseURL().'proxyValidate'; break; } }// return $this->_server['proxy_validate_url'].'?service='.preg_replace('/&/','%26',$this->getURL()); return $this->_server['proxy_validate_url'].'?service='.urlencode($this->getURL()); } /** * This method is used to retrieve the proxy URL of the CAS server. * @return a URL. * @private */ function getServerProxyURL() { // the URL is build only when needed if ( empty($this->_server['proxy_url']) ) { switch ($this->getServerVersion()) { case CAS_VERSION_1_0: $this->_server['proxy_url'] = ''; break; case CAS_VERSION_2_0: $this->_server['proxy_url'] = $this->getServerBaseURL().'proxy'; break; } } return $this->_server['proxy_url']; } /** * This method is used to retrieve the logout URL of the CAS server. * @return a URL.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -