|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 /* 00004 * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * * Redistributions of source code must retain the above copyright notice, 00011 * this list of conditions and the following disclaimer. 00012 * * Redistributions in binary form must reproduce the above copyright notice, 00013 * this list of conditions and the following disclaimer in the documentation 00014 * and/or other materials provided with the distribution. 00015 * * Neither the name of the ESUP-Portail consortium & the JA-SIG 00016 * Collaborative nor the names of its contributors may be used to endorse or 00017 * promote products derived from this software without specific prior 00018 * written permission. 00019 00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00021 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00022 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00023 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 00024 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00025 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00026 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 00027 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 */ 00031 00032 // 00033 // hack by Vangelis Haniotakis to handle the absence of $_SERVER['REQUEST_URI'] in IIS 00034 // 00035 if (!isset($_SERVER['REQUEST_URI'])) { 00036 $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING']; 00037 } 00038 00039 // 00040 // another one by Vangelis Haniotakis also to make phpCAS work with PHP5 00041 // 00042 if (version_compare(PHP_VERSION, '5', '>=') && !(function_exists('domxml_new_doc'))) { 00043 require_once (dirname(__FILE__) . '/CAS/domxml-php4-to-php5.php'); 00044 } 00045 00053 // ######################################################################## 00054 // CONSTANTS 00055 // ######################################################################## 00056 00057 // ------------------------------------------------------------------------ 00058 // CAS VERSIONS 00059 // ------------------------------------------------------------------------ 00060 00064 define('PHPCAS_VERSION', '1.1.3'); 00065 00066 // ------------------------------------------------------------------------ 00067 // CAS VERSIONS 00068 // ------------------------------------------------------------------------ 00077 define("CAS_VERSION_1_0", '1.0'); 00081 define("CAS_VERSION_2_0", '2.0'); 00082 00083 // ------------------------------------------------------------------------ 00084 // SAML defines 00085 // ------------------------------------------------------------------------ 00086 00090 define("SAML_VERSION_1_1", 'S1'); 00091 00095 define("SAML_XML_HEADER", '<?xml version="1.0" encoding="UTF-8"?>'); 00096 00100 define("SAML_SOAP_ENV", '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/>'); 00101 00105 define("SAML_SOAP_BODY", '<SOAP-ENV:Body>'); 00106 00110 define("SAMLP_REQUEST", '<samlp:Request xmlns:samlp="urn:oasis:names:tc:SAML:1.0:protocol" MajorVersion="1" MinorVersion="1" RequestID="_192.168.16.51.1024506224022" IssueInstant="2002-06-19T17:03:44.022Z">'); 00111 define("SAMLP_REQUEST_CLOSE", '</samlp:Request>'); 00112 00116 define("SAML_ASSERTION_ARTIFACT", '<samlp:AssertionArtifact>'); 00117 00121 define("SAML_ASSERTION_ARTIFACT_CLOSE", '</samlp:AssertionArtifact>'); 00122 00126 define("SAML_SOAP_BODY_CLOSE", '</SOAP-ENV:Body>'); 00127 00131 define("SAML_SOAP_ENV_CLOSE", '</SOAP-ENV:Envelope>'); 00132 00136 define("SAML_ATTRIBUTES", 'SAMLATTRIBS'); 00137 00143 // ------------------------------------------------------------------------ 00144 // FILE PGT STORAGE 00145 // ------------------------------------------------------------------------ 00149 define("CAS_PGT_STORAGE_FILE_DEFAULT_PATH", '/tmp'); 00153 define("CAS_PGT_STORAGE_FILE_FORMAT_PLAIN", 'plain'); 00157 define("CAS_PGT_STORAGE_FILE_FORMAT_XML", 'xml'); 00161 define("CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT", CAS_PGT_STORAGE_FILE_FORMAT_PLAIN); 00163 // ------------------------------------------------------------------------ 00164 // SERVICE ACCESS ERRORS 00165 // ------------------------------------------------------------------------ 00174 define("PHPCAS_SERVICE_OK", 0); 00179 define("PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE", 1); 00184 define("PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE", 2); 00189 define("PHPCAS_SERVICE_PT_FAILURE", 3); 00193 define("PHPCAS_SERVICE_NOT AVAILABLE", 4); 00194 00196 // ------------------------------------------------------------------------ 00197 // LANGUAGES 00198 // ------------------------------------------------------------------------ 00204 define("PHPCAS_LANG_ENGLISH", 'english'); 00205 define("PHPCAS_LANG_FRENCH", 'french'); 00206 define("PHPCAS_LANG_GREEK", 'greek'); 00207 define("PHPCAS_LANG_GERMAN", 'german'); 00208 define("PHPCAS_LANG_JAPANESE", 'japanese'); 00209 define("PHPCAS_LANG_SPANISH", 'spanish'); 00210 define("PHPCAS_LANG_CATALAN", 'catalan'); 00211 00222 define("PHPCAS_LANG_DEFAULT", PHPCAS_LANG_ENGLISH); 00223 00225 // ------------------------------------------------------------------------ 00226 // DEBUG 00227 // ------------------------------------------------------------------------ 00236 define('DEFAULT_DEBUG_DIR', '/tmp/'); 00237 00239 // ------------------------------------------------------------------------ 00240 // MISC 00241 // ------------------------------------------------------------------------ 00252 $GLOBALS['PHPCAS_CLIENT'] = null; 00253 00260 $GLOBALS['PHPCAS_INIT_CALL'] = array ( 00261 'done' => FALSE, 00262 'file' => '?', 00263 'line' => -1, 00264 'method' => '?' 00265 ); 00266 00273 $GLOBALS['PHPCAS_AUTH_CHECK_CALL'] = array ( 00274 'done' => FALSE, 00275 'file' => '?', 00276 'line' => -1, 00277 'method' => '?', 00278 'result' => FALSE 00279 ); 00280 00286 $GLOBALS['PHPCAS_DEBUG'] = array ( 00287 'filename' => FALSE, 00288 'indent' => 0, 00289 'unique_id' => '' 00290 ); 00291 00294 // ######################################################################## 00295 // CLIENT CLASS 00296 // ######################################################################## 00297 00298 // include client class 00299 include_once (dirname(__FILE__) . '/CAS/client.php'); 00300 00301 // ######################################################################## 00302 // INTERFACE CLASS 00303 // ######################################################################## 00304 00317 class phpCAS { 00318 00319 // ######################################################################## 00320 // INITIALIZATION 00321 // ######################################################################## 00322 00342 function client($server_version, $server_hostname, $server_port, $server_uri, $start_session = true) { 00343 global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL; 00344 00345 phpCAS :: traceBegin(); 00346 if (is_object($PHPCAS_CLIENT)) { 00347 phpCAS :: error($PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . $PHPCAS_INIT_CALL['file'] . ':' . $PHPCAS_INIT_CALL['line'] . ')'); 00348 } 00349 if (gettype($server_version) != 'string') { 00350 phpCAS :: error('type mismatched for parameter $server_version (should be `string\')'); 00351 } 00352 if (gettype($server_hostname) != 'string') { 00353 phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')'); 00354 } 00355 if (gettype($server_port) != 'integer') { 00356 phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')'); 00357 } 00358 if (gettype($server_uri) != 'string') { 00359 phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')'); 00360 } 00361 00362 // store where the initializer is called from 00363 $dbg = phpCAS :: backtrace(); 00364 $PHPCAS_INIT_CALL = array ( 00365 'done' => TRUE, 00366 'file' => $dbg[0]['file'], 00367 'line' => $dbg[0]['line'], 00368 'method' => __CLASS__ . '::' . __FUNCTION__ 00369 ); 00370 00371 // initialize the global object $PHPCAS_CLIENT 00372 $PHPCAS_CLIENT = new CASClient($server_version, FALSE /*proxy*/ 00373 , $server_hostname, $server_port, $server_uri, $start_session); 00374 phpCAS :: traceEnd(); 00375 } 00376 00391 function proxy($server_version, $server_hostname, $server_port, $server_uri, $start_session = true) { 00392 global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL; 00393 00394 phpCAS :: traceBegin(); 00395 if (is_object($PHPCAS_CLIENT)) { 00396 phpCAS :: error($PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . $PHPCAS_INIT_CALL['file'] . ':' . $PHPCAS_INIT_CALL['line'] . ')'); 00397 } 00398 if (gettype($server_version) != 'string') { 00399 phpCAS :: error('type mismatched for parameter $server_version (should be `string\')'); 00400 } 00401 if (gettype($server_hostname) != 'string') { 00402 phpCAS :: error('type mismatched for parameter $server_hostname (should be `string\')'); 00403 } 00404 if (gettype($server_port) != 'integer') { 00405 phpCAS :: error('type mismatched for parameter $server_port (should be `integer\')'); 00406 } 00407 if (gettype($server_uri) != 'string') { 00408 phpCAS :: error('type mismatched for parameter $server_uri (should be `string\')'); 00409 } 00410 00411 // store where the initialzer is called from 00412 $dbg = phpCAS :: backtrace(); 00413 $PHPCAS_INIT_CALL = array ( 00414 'done' => TRUE, 00415 'file' => $dbg[0]['file'], 00416 'line' => $dbg[0]['line'], 00417 'method' => __CLASS__ . '::' . __FUNCTION__ 00418 ); 00419 00420 // initialize the global object $PHPCAS_CLIENT 00421 $PHPCAS_CLIENT = new CASClient($server_version, TRUE /*proxy*/ 00422 , $server_hostname, $server_port, $server_uri, $start_session); 00423 phpCAS :: traceEnd(); 00424 } 00425 00427 // ######################################################################## 00428 // DEBUGGING 00429 // ######################################################################## 00430 00441 function setDebug($filename = '') { 00442 global $PHPCAS_DEBUG; 00443 00444 if ($filename != FALSE && gettype($filename) != 'string') { 00445 phpCAS :: error('type mismatched for parameter $dbg (should be FALSE or the name of the log file)'); 00446 } 00447 00448 if (empty ($filename)) { 00449 if (preg_match('/^Win.*/', getenv('OS'))) { 00450 if (isset ($_ENV['TMP'])) { 00451 $debugDir = $_ENV['TMP'] . '/'; 00452 } else 00453 if (isset ($_ENV['TEMP'])) { 00454 $debugDir = $_ENV['TEMP'] . '/'; 00455 } else { 00456 $debugDir = ''; 00457 } 00458 } else { 00459 $debugDir = DEFAULT_DEBUG_DIR; 00460 } 00461 $filename = $debugDir . 'phpCAS.log'; 00462 } 00463 00464 if (empty ($PHPCAS_DEBUG['unique_id'])) { 00465 $PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))), 0, 4); 00466 } 00467 00468 $PHPCAS_DEBUG['filename'] = $filename; 00469 00470 phpCAS :: trace('START phpCAS-' . PHPCAS_VERSION . ' ******************'); 00471 } 00472 00483 function backtrace() { 00484 if (function_exists('debug_backtrace')) { 00485 return debug_backtrace(); 00486 } else { 00487 // poor man's hack ... but it does work ... 00488 return array (); 00489 } 00490 } 00491 00499 function log($str) { 00500 $indent_str = "."; 00501 global $PHPCAS_DEBUG; 00502 00503 if ($PHPCAS_DEBUG['filename']) { 00504 for ($i = 0; $i < $PHPCAS_DEBUG['indent']; $i++) { 00505 $indent_str .= '| '; 00506 } 00507 error_log($PHPCAS_DEBUG['unique_id'] . ' ' . $indent_str . $str . "\n", 3, $PHPCAS_DEBUG['filename']); 00508 } 00509 00510 } 00511 00520 function error($msg) { 00521 $dbg = phpCAS :: backtrace(); 00522 $function = '?'; 00523 $file = '?'; 00524 $line = '?'; 00525 if (is_array($dbg)) { 00526 for ($i = 1; $i < sizeof($dbg); $i++) { 00527 if (is_array($dbg[$i])) { 00528 if ($dbg[$i]['class'] == __CLASS__) { 00529 $function = $dbg[$i]['function']; 00530 $file = $dbg[$i]['file']; 00531 $line = $dbg[$i]['line']; 00532 } 00533 } 00534 } 00535 } 00536 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"; 00537 phpCAS :: trace($msg); 00538 phpCAS :: traceExit(); 00539 exit (); 00540 } 00541 00545 function trace($str) { 00546 $dbg = phpCAS :: backtrace(); 00547 phpCAS :: log($str . ' [' . basename($dbg[1]['file']) . ':' . $dbg[1]['line'] . ']'); 00548 } 00549 00553 function traceBegin() { 00554 global $PHPCAS_DEBUG; 00555 00556 $dbg = phpCAS :: backtrace(); 00557 $str = '=> '; 00558 if (!empty ($dbg[2]['class'])) { 00559 $str .= $dbg[2]['class'] . '::'; 00560 } 00561 $str .= $dbg[2]['function'] . '('; 00562 if (is_array($dbg[2]['args'])) { 00563 foreach ($dbg[2]['args'] as $index => $arg) { 00564 if ($index != 0) { 00565 $str .= ', '; 00566 } 00567 $str .= str_replace("\n", "", var_export($arg, TRUE)); 00568 } 00569 } 00570 $str .= ') [' . basename($dbg[2]['file']) . ':' . $dbg[2]['line'] . ']'; 00571 phpCAS :: log($str); 00572 $PHPCAS_DEBUG['indent']++; 00573 } 00574 00580 function traceEnd($res = '') { 00581 global $PHPCAS_DEBUG; 00582 00583 $PHPCAS_DEBUG['indent']--; 00584 $dbg = phpCAS :: backtrace(); 00585 $str = ''; 00586 $str .= '<= ' . str_replace("\n", "", var_export($res, TRUE)); 00587 phpCAS :: log($str); 00588 } 00589 00593 function traceExit() { 00594 global $PHPCAS_DEBUG; 00595 00596 phpCAS :: log('exit()'); 00597 while ($PHPCAS_DEBUG['indent'] > 0) { 00598 phpCAS :: log('-'); 00599 $PHPCAS_DEBUG['indent']--; 00600 } 00601 } 00602 00604 // ######################################################################## 00605 // INTERNATIONALIZATION 00606 // ######################################################################## 00620 function setLang($lang) { 00621 global $PHPCAS_CLIENT; 00622 if (!is_object($PHPCAS_CLIENT)) { 00623 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00624 } 00625 if (gettype($lang) != 'string') { 00626 phpCAS :: error('type mismatched for parameter $lang (should be `string\')'); 00627 } 00628 $PHPCAS_CLIENT->setLang($lang); 00629 } 00630 00632 // ######################################################################## 00633 // VERSION 00634 // ######################################################################## 00645 function getVersion() { 00646 return PHPCAS_VERSION; 00647 } 00648 00650 // ######################################################################## 00651 // HTML OUTPUT 00652 // ######################################################################## 00663 function setHTMLHeader($header) { 00664 global $PHPCAS_CLIENT; 00665 if (!is_object($PHPCAS_CLIENT)) { 00666 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00667 } 00668 if (gettype($header) != 'string') { 00669 phpCAS :: error('type mismatched for parameter $header (should be `string\')'); 00670 } 00671 $PHPCAS_CLIENT->setHTMLHeader($header); 00672 } 00673 00679 function setHTMLFooter($footer) { 00680 global $PHPCAS_CLIENT; 00681 if (!is_object($PHPCAS_CLIENT)) { 00682 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00683 } 00684 if (gettype($footer) != 'string') { 00685 phpCAS :: error('type mismatched for parameter $footer (should be `string\')'); 00686 } 00687 $PHPCAS_CLIENT->setHTMLFooter($footer); 00688 } 00689 00691 // ######################################################################## 00692 // PGT STORAGE 00693 // ######################################################################## 00706 function setPGTStorageFile($format = '', $path = '') { 00707 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 00708 00709 phpCAS :: traceBegin(); 00710 if (!is_object($PHPCAS_CLIENT)) { 00711 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 00712 } 00713 if (!$PHPCAS_CLIENT->isProxy()) { 00714 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 00715 } 00716 if ($PHPCAS_AUTH_CHECK_CALL['done']) { 00717 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'] . ')'); 00718 } 00719 if (gettype($format) != 'string') { 00720 phpCAS :: error('type mismatched for parameter $format (should be `string\')'); 00721 } 00722 if (gettype($path) != 'string') { 00723 phpCAS :: error('type mismatched for parameter $format (should be `string\')'); 00724 } 00725 $PHPCAS_CLIENT->setPGTStorageFile($format, $path); 00726 phpCAS :: traceEnd(); 00727 } 00728 00729 00731 // ######################################################################## 00732 // ACCESS TO EXTERNAL SERVICES 00733 // ######################################################################## 00752 function serviceWeb($url, & $err_code, & $output) { 00753 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 00754 00755 phpCAS :: traceBegin(); 00756 if (!is_object($PHPCAS_CLIENT)) { 00757 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 00758 } 00759 if (!$PHPCAS_CLIENT->isProxy()) { 00760 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 00761 } 00762 if (!$PHPCAS_AUTH_CHECK_CALL['done']) { 00763 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()'); 00764 } 00765 if (!$PHPCAS_AUTH_CHECK_CALL['result']) { 00766 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'); 00767 } 00768 if (gettype($url) != 'string') { 00769 phpCAS :: error('type mismatched for parameter $url (should be `string\')'); 00770 } 00771 00772 $res = $PHPCAS_CLIENT->serviceWeb($url, $err_code, $output); 00773 00774 phpCAS :: traceEnd($res); 00775 return $res; 00776 } 00777 00795 function serviceMail($url, $service, $flags, & $err_code, & $err_msg, & $pt) { 00796 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 00797 00798 phpCAS :: traceBegin(); 00799 if (!is_object($PHPCAS_CLIENT)) { 00800 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 00801 } 00802 if (!$PHPCAS_CLIENT->isProxy()) { 00803 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 00804 } 00805 if (!$PHPCAS_AUTH_CHECK_CALL['done']) { 00806 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()'); 00807 } 00808 if (!$PHPCAS_AUTH_CHECK_CALL['result']) { 00809 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'); 00810 } 00811 if (gettype($url) != 'string') { 00812 phpCAS :: error('type mismatched for parameter $url (should be `string\')'); 00813 } 00814 00815 if (gettype($flags) != 'integer') { 00816 phpCAS :: error('type mismatched for parameter $flags (should be `integer\')'); 00817 } 00818 00819 $res = $PHPCAS_CLIENT->serviceMail($url, $service, $flags, $err_code, $err_msg, $pt); 00820 00821 phpCAS :: traceEnd($res); 00822 return $res; 00823 } 00824 00826 // ######################################################################## 00827 // AUTHENTICATION 00828 // ######################################################################## 00842 function setCacheTimesForAuthRecheck($n) { 00843 global $PHPCAS_CLIENT; 00844 if (!is_object($PHPCAS_CLIENT)) { 00845 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00846 } 00847 if (gettype($n) != 'integer') { 00848 phpCAS :: error('type mismatched for parameter $header (should be `string\')'); 00849 } 00850 $PHPCAS_CLIENT->setCacheTimesForAuthRecheck($n); 00851 } 00852 00857 function checkAuthentication() { 00858 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 00859 00860 phpCAS :: traceBegin(); 00861 if (!is_object($PHPCAS_CLIENT)) { 00862 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00863 } 00864 00865 $auth = $PHPCAS_CLIENT->checkAuthentication(); 00866 00867 // store where the authentication has been checked and the result 00868 $dbg = phpCAS :: backtrace(); 00869 $PHPCAS_AUTH_CHECK_CALL = array ( 00870 'done' => TRUE, 00871 'file' => $dbg[0]['file'], 00872 'line' => $dbg[0]['line'], 00873 'method' => __CLASS__ . '::' . __FUNCTION__, 00874 'result' => $auth 00875 ); 00876 phpCAS :: traceEnd($auth); 00877 return $auth; 00878 } 00879 00885 function forceAuthentication() { 00886 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 00887 00888 phpCAS :: traceBegin(); 00889 if (!is_object($PHPCAS_CLIENT)) { 00890 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00891 } 00892 00893 $auth = $PHPCAS_CLIENT->forceAuthentication(); 00894 00895 // store where the authentication has been checked and the result 00896 $dbg = phpCAS :: backtrace(); 00897 $PHPCAS_AUTH_CHECK_CALL = array ( 00898 'done' => TRUE, 00899 'file' => $dbg[0]['file'], 00900 'line' => $dbg[0]['line'], 00901 'method' => __CLASS__ . '::' . __FUNCTION__, 00902 'result' => $auth 00903 ); 00904 00905 if (!$auth) { 00906 phpCAS :: trace('user is not authenticated, redirecting to the CAS server'); 00907 $PHPCAS_CLIENT->forceAuthentication(); 00908 } else { 00909 phpCAS :: trace('no need to authenticate (user `' . phpCAS :: getUser() . '\' is already authenticated)'); 00910 } 00911 00912 phpCAS :: traceEnd(); 00913 return $auth; 00914 } 00915 00919 function renewAuthentication() { 00920 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 00921 00922 phpCAS :: traceBegin(); 00923 if (!is_object($PHPCAS_CLIENT)) { 00924 phpCAS :: error('this method should not be called before' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00925 } 00926 00927 // store where the authentication has been checked and the result 00928 $dbg = phpCAS :: backtrace(); 00929 $PHPCAS_AUTH_CHECK_CALL = array ( 00930 'done' => TRUE, 00931 'file' => $dbg[0]['file'], 00932 'line' => $dbg[0]['line'], 00933 'method' => __CLASS__ . '::' . __FUNCTION__, 00934 'result' => $auth 00935 ); 00936 00937 $PHPCAS_CLIENT->renewAuthentication(); 00938 phpCAS :: traceEnd(); 00939 } 00940 00944 function authenticate() { 00945 phpCAS :: error('this method is deprecated. You should use ' . __CLASS__ . '::forceAuthentication() instead'); 00946 } 00947 00954 function isAuthenticated() { 00955 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 00956 00957 phpCAS :: traceBegin(); 00958 if (!is_object($PHPCAS_CLIENT)) { 00959 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00960 } 00961 00962 // call the isAuthenticated method of the global $PHPCAS_CLIENT object 00963 $auth = $PHPCAS_CLIENT->isAuthenticated(); 00964 00965 // store where the authentication has been checked and the result 00966 $dbg = phpCAS :: backtrace(); 00967 $PHPCAS_AUTH_CHECK_CALL = array ( 00968 'done' => TRUE, 00969 'file' => $dbg[0]['file'], 00970 'line' => $dbg[0]['line'], 00971 'method' => __CLASS__ . '::' . __FUNCTION__, 00972 'result' => $auth 00973 ); 00974 phpCAS :: traceEnd($auth); 00975 return $auth; 00976 } 00977 00984 function isSessionAuthenticated() { 00985 global $PHPCAS_CLIENT; 00986 if (!is_object($PHPCAS_CLIENT)) { 00987 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 00988 } 00989 return ($PHPCAS_CLIENT->isSessionAuthenticated()); 00990 } 00991 00999 function getUser() { 01000 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 01001 if (!is_object($PHPCAS_CLIENT)) { 01002 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 01003 } 01004 if (!$PHPCAS_AUTH_CHECK_CALL['done']) { 01005 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()'); 01006 } 01007 if (!$PHPCAS_AUTH_CHECK_CALL['result']) { 01008 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'); 01009 } 01010 return $PHPCAS_CLIENT->getUser(); 01011 } 01012 01020 function getAttributes() { 01021 global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL; 01022 if (!is_object($PHPCAS_CLIENT)) { 01023 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 01024 } 01025 if (!$PHPCAS_AUTH_CHECK_CALL['done']) { 01026 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::forceAuthentication() or ' . __CLASS__ . '::isAuthenticated()'); 01027 } 01028 if (!$PHPCAS_AUTH_CHECK_CALL['result']) { 01029 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'); 01030 } 01031 return $PHPCAS_CLIENT->getAttributes(); 01032 } 01036 function handleLogoutRequests($check_client = true, $allowed_clients = false) { 01037 global $PHPCAS_CLIENT; 01038 if (!is_object($PHPCAS_CLIENT)) { 01039 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 01040 } 01041 return ($PHPCAS_CLIENT->handleLogoutRequests($check_client, $allowed_clients)); 01042 } 01043 01050 function getServerLoginURL() { 01051 global $PHPCAS_CLIENT; 01052 if (!is_object($PHPCAS_CLIENT)) { 01053 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 01054 } 01055 return $PHPCAS_CLIENT->getServerLoginURL(); 01056 } 01057 01063 function setServerLoginURL($url = '') { 01064 global $PHPCAS_CLIENT; 01065 phpCAS :: traceBegin(); 01066 if (!is_object($PHPCAS_CLIENT)) { 01067 phpCAS :: error('this method should only be called after 01068 ' . __CLASS__ . '::client()'); 01069 } 01070 if (gettype($url) != 'string') { 01071 phpCAS :: error('type mismatched for parameter $url (should be 01072 `string\')'); 01073 } 01074 $PHPCAS_CLIENT->setServerLoginURL($url); 01075 phpCAS :: traceEnd(); 01076 } 01077 01084 function setServerServiceValidateURL($url = '') { 01085 global $PHPCAS_CLIENT; 01086 phpCAS :: traceBegin(); 01087 if (!is_object($PHPCAS_CLIENT)) { 01088 phpCAS :: error('this method should only be called after 01089 ' . __CLASS__ . '::client()'); 01090 } 01091 if (gettype($url) != 'string') { 01092 phpCAS :: error('type mismatched for parameter $url (should be 01093 `string\')'); 01094 } 01095 $PHPCAS_CLIENT->setServerServiceValidateURL($url); 01096 phpCAS :: traceEnd(); 01097 } 01098 01105 function setServerProxyValidateURL($url = '') { 01106 global $PHPCAS_CLIENT; 01107 phpCAS :: traceBegin(); 01108 if (!is_object($PHPCAS_CLIENT)) { 01109 phpCAS :: error('this method should only be called after 01110 ' . __CLASS__ . '::client()'); 01111 } 01112 if (gettype($url) != 'string') { 01113 phpCAS :: error('type mismatched for parameter $url (should be 01114 `string\')'); 01115 } 01116 $PHPCAS_CLIENT->setServerProxyValidateURL($url); 01117 phpCAS :: traceEnd(); 01118 } 01119 01125 function setServerSamlValidateURL($url = '') { 01126 global $PHPCAS_CLIENT; 01127 phpCAS :: traceBegin(); 01128 if (!is_object($PHPCAS_CLIENT)) { 01129 phpCAS :: error('this method should only be called after 01130 ' . __CLASS__ . '::client()'); 01131 } 01132 if (gettype($url) != 'string') { 01133 phpCAS :: error('type mismatched for parameter $url (should be 01134 `string\')'); 01135 } 01136 $PHPCAS_CLIENT->setServerSamlValidateURL($url); 01137 phpCAS :: traceEnd(); 01138 } 01139 01146 function getServerLogoutURL() { 01147 global $PHPCAS_CLIENT; 01148 if (!is_object($PHPCAS_CLIENT)) { 01149 phpCAS :: error('this method should not be called before ' . __CLASS__ . '::client() or ' . __CLASS__ . '::proxy()'); 01150 } 01151 return $PHPCAS_CLIENT->getServerLogoutURL(); 01152 } 01153 01159 function setServerLogoutURL($url = '') { 01160 global $PHPCAS_CLIENT; 01161 phpCAS :: traceBegin(); 01162 if (!is_object($PHPCAS_CLIENT)) { 01163 phpCAS :: error('this method should only be called after 01164 ' . __CLASS__ . '::client()'); 01165 } 01166 if (gettype($url) != 'string') { 01167 phpCAS :: error('type mismatched for parameter $url (should be 01168 `string\')'); 01169 } 01170 $PHPCAS_CLIENT->setServerLogoutURL($url); 01171 phpCAS :: traceEnd(); 01172 } 01173 01179 function logout($params = "") { 01180 global $PHPCAS_CLIENT; 01181 phpCAS :: traceBegin(); 01182 if (!is_object($PHPCAS_CLIENT)) { 01183 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01184 } 01185 $parsedParams = array (); 01186 if ($params != "") { 01187 if (is_string($params)) { 01188 phpCAS :: error('method `phpCAS::logout($url)\' is now deprecated, use `phpCAS::logoutWithUrl($url)\' instead'); 01189 } 01190 if (!is_array($params)) { 01191 phpCAS :: error('type mismatched for parameter $params (should be `array\')'); 01192 } 01193 foreach ($params as $key => $value) { 01194 if ($key != "service" && $key != "url") { 01195 phpCAS :: error('only `url\' and `service\' parameters are allowed for method `phpCAS::logout($params)\''); 01196 } 01197 $parsedParams[$key] = $value; 01198 } 01199 } 01200 $PHPCAS_CLIENT->logout($parsedParams); 01201 // never reached 01202 phpCAS :: traceEnd(); 01203 } 01204 01209 function logoutWithRedirectService($service) { 01210 global $PHPCAS_CLIENT; 01211 phpCAS :: traceBegin(); 01212 if (!is_object($PHPCAS_CLIENT)) { 01213 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01214 } 01215 if (!is_string($service)) { 01216 phpCAS :: error('type mismatched for parameter $service (should be `string\')'); 01217 } 01218 $PHPCAS_CLIENT->logout(array ( 01219 "service" => $service 01220 )); 01221 // never reached 01222 phpCAS :: traceEnd(); 01223 } 01224 01229 function logoutWithUrl($url) { 01230 global $PHPCAS_CLIENT; 01231 phpCAS :: traceBegin(); 01232 if (!is_object($PHPCAS_CLIENT)) { 01233 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01234 } 01235 if (!is_string($url)) { 01236 phpCAS :: error('type mismatched for parameter $url (should be `string\')'); 01237 } 01238 $PHPCAS_CLIENT->logout(array ( 01239 "url" => $url 01240 )); 01241 // never reached 01242 phpCAS :: traceEnd(); 01243 } 01244 01250 function logoutWithRedirectServiceAndUrl($service, $url) { 01251 global $PHPCAS_CLIENT; 01252 phpCAS :: traceBegin(); 01253 if (!is_object($PHPCAS_CLIENT)) { 01254 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01255 } 01256 if (!is_string($service)) { 01257 phpCAS :: error('type mismatched for parameter $service (should be `string\')'); 01258 } 01259 if (!is_string($url)) { 01260 phpCAS :: error('type mismatched for parameter $url (should be `string\')'); 01261 } 01262 $PHPCAS_CLIENT->logout(array ( 01263 "service" => $service, 01264 "url" => $url 01265 )); 01266 // never reached 01267 phpCAS :: traceEnd(); 01268 } 01269 01276 function setFixedCallbackURL($url = '') { 01277 global $PHPCAS_CLIENT; 01278 phpCAS :: traceBegin(); 01279 if (!is_object($PHPCAS_CLIENT)) { 01280 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 01281 } 01282 if (!$PHPCAS_CLIENT->isProxy()) { 01283 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 01284 } 01285 if (gettype($url) != 'string') { 01286 phpCAS :: error('type mismatched for parameter $url (should be `string\')'); 01287 } 01288 $PHPCAS_CLIENT->setCallbackURL($url); 01289 phpCAS :: traceEnd(); 01290 } 01291 01298 function setFixedServiceURL($url) { 01299 global $PHPCAS_CLIENT; 01300 phpCAS :: traceBegin(); 01301 if (!is_object($PHPCAS_CLIENT)) { 01302 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 01303 } 01304 if (gettype($url) != 'string') { 01305 phpCAS :: error('type mismatched for parameter $url (should be `string\')'); 01306 } 01307 $PHPCAS_CLIENT->setURL($url); 01308 phpCAS :: traceEnd(); 01309 } 01310 01314 function getServiceURL() { 01315 global $PHPCAS_CLIENT; 01316 if (!is_object($PHPCAS_CLIENT)) { 01317 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 01318 } 01319 return ($PHPCAS_CLIENT->getURL()); 01320 } 01321 01325 function retrievePT($target_service, & $err_code, & $err_msg) { 01326 global $PHPCAS_CLIENT; 01327 if (!is_object($PHPCAS_CLIENT)) { 01328 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::proxy()'); 01329 } 01330 if (gettype($target_service) != 'string') { 01331 phpCAS :: error('type mismatched for parameter $target_service(should be `string\')'); 01332 } 01333 return ($PHPCAS_CLIENT->retrievePT($target_service, $err_code, $err_msg)); 01334 } 01335 01341 function setCasServerCert($cert) { 01342 global $PHPCAS_CLIENT; 01343 phpCAS :: traceBegin(); 01344 if (!is_object($PHPCAS_CLIENT)) { 01345 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01346 } 01347 if (gettype($cert) != 'string') { 01348 phpCAS :: error('type mismatched for parameter $cert (should be `string\')'); 01349 } 01350 $PHPCAS_CLIENT->setCasServerCert($cert); 01351 phpCAS :: traceEnd(); 01352 } 01353 01359 function setCasServerCACert($cert) { 01360 global $PHPCAS_CLIENT; 01361 phpCAS :: traceBegin(); 01362 if (!is_object($PHPCAS_CLIENT)) { 01363 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01364 } 01365 if (gettype($cert) != 'string') { 01366 phpCAS :: error('type mismatched for parameter $cert (should be `string\')'); 01367 } 01368 $PHPCAS_CLIENT->setCasServerCACert($cert); 01369 phpCAS :: traceEnd(); 01370 } 01371 01375 function setNoCasServerValidation() { 01376 global $PHPCAS_CLIENT; 01377 phpCAS :: traceBegin(); 01378 if (!is_object($PHPCAS_CLIENT)) { 01379 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01380 } 01381 $PHPCAS_CLIENT->setNoCasServerValidation(); 01382 phpCAS :: traceEnd(); 01383 } 01384 01393 function setExtraCurlOption($key, $value) { 01394 global $PHPCAS_CLIENT; 01395 phpCAS :: traceBegin(); 01396 if (!is_object($PHPCAS_CLIENT)) { 01397 phpCAS :: error('this method should only be called after ' . __CLASS__ . '::client() or' . __CLASS__ . '::proxy()'); 01398 } 01399 $PHPCAS_CLIENT->setExtraCurlOption($key, $value); 01400 phpCAS :: traceEnd(); 01401 } 01402 01403 } 01404 01405 // ######################################################################## 01406 // DOCUMENTATION 01407 // ######################################################################## 01408 01409 // ######################################################################## 01410 // MAIN PAGE 01411 01419 // ######################################################################## 01420 // MODULES DEFINITION 01421 01492 // ######################################################################## 01493 // EXAMPLES 01494 01531 ?>