CAS.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687
  1. <?php
  2. /*
  3. * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the ESUP-Portail consortium & the JA-SIG
  15. * Collaborative nor the names of its contributors may be used to endorse or
  16. * promote products derived from this software without specific prior
  17. * written permission.
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. //
  30. // hack by Vangelis Haniotakis to handle the absence of $_SERVER['REQUEST_URI'] in IIS
  31. //
  32. if (!$_SERVER['REQUEST_URI']) {
  33. $_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'].'?'.$_SERVER['QUERY_STRING'];
  34. }
  35. //
  36. // another one by Vangelis Haniotakis also to make phpCAS work with PHP5
  37. //
  38. if (version_compare(PHP_VERSION, '5', '>=')) {
  39. require_once __DIR__.'/CAS/domxml-php4-to-php5.php';
  40. }
  41. /**
  42. * @file CAS/CAS.php
  43. * Interface class of the phpCAS library
  44. *
  45. * @ingroup public
  46. */
  47. // ########################################################################
  48. // CONSTANTS
  49. // ########################################################################
  50. // ------------------------------------------------------------------------
  51. // CAS VERSIONS
  52. // ------------------------------------------------------------------------
  53. /**
  54. * phpCAS version. accessible for the user by phpCAS::getVersion().
  55. */
  56. define('PHPCAS_VERSION', '1.1.1');
  57. // ------------------------------------------------------------------------
  58. // CAS VERSIONS
  59. // ------------------------------------------------------------------------
  60. /**
  61. * @addtogroup public
  62. * @{
  63. */
  64. /**
  65. * CAS version 1.0
  66. */
  67. define("CAS_VERSION_1_0", '1.0');
  68. /*!
  69. * CAS version 2.0
  70. */
  71. define("CAS_VERSION_2_0", '2.0');
  72. // ------------------------------------------------------------------------
  73. // SAML defines
  74. // ------------------------------------------------------------------------
  75. /**
  76. * SAML protocol
  77. */
  78. define("SAML_VERSION_1_1", 'S1');
  79. /**
  80. * XML header for SAML POST
  81. */
  82. define("SAML_XML_HEADER", '<?xml version="1.0" encoding="UTF-8"?>');
  83. /**
  84. * SOAP envelope for SAML POST
  85. */
  86. define("SAML_SOAP_ENV",
  87. '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/>');
  88. /**
  89. * SOAP body for SAML POST
  90. */
  91. define("SAML_SOAP_BODY", '<SOAP-ENV:Body>');
  92. /**
  93. * SAMLP request
  94. */
  95. define("SAMLP_REQUEST",
  96. '<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">');
  97. define("SAMLP_REQUEST_CLOSE", '</samlp:Request>');
  98. /**
  99. * SAMLP artifact tag (for the ticket)
  100. */
  101. define("SAML_ASSERTION_ARTIFACT", '<samlp:AssertionArtifact>');
  102. /**
  103. * SAMLP close
  104. */
  105. define("SAML_ASSERTION_ARTIFACT_CLOSE", '</samlp:AssertionArtifact>');
  106. /**
  107. * SOAP body close
  108. */
  109. define("SAML_SOAP_BODY_CLOSE", '</SOAP-ENV:Body>');
  110. /**
  111. * SOAP envelope close
  112. */
  113. define("SAML_SOAP_ENV_CLOSE", '</SOAP-ENV:Envelope>');
  114. /**
  115. * SAML Attributes
  116. */
  117. define("SAML_ATTRIBUTES", 'SAMLATTRIBS');
  118. /** @} */
  119. /**
  120. * @addtogroup publicPGTStorage
  121. * @{
  122. */
  123. // ------------------------------------------------------------------------
  124. // FILE PGT STORAGE
  125. // ------------------------------------------------------------------------
  126. /**
  127. * Default path used when storing PGT's to file
  128. */
  129. define("CAS_PGT_STORAGE_FILE_DEFAULT_PATH", '/tmp');
  130. /**
  131. * phpCAS::setPGTStorageFile()'s 2nd parameter to write plain text files
  132. */
  133. define("CAS_PGT_STORAGE_FILE_FORMAT_PLAIN", 'plain');
  134. /**
  135. * phpCAS::setPGTStorageFile()'s 2nd parameter to write xml files
  136. */
  137. define("CAS_PGT_STORAGE_FILE_FORMAT_XML", 'xml');
  138. /**
  139. * Default format used when storing PGT's to file
  140. */
  141. define("CAS_PGT_STORAGE_FILE_DEFAULT_FORMAT", CAS_PGT_STORAGE_FILE_FORMAT_PLAIN);
  142. // ------------------------------------------------------------------------
  143. // DATABASE PGT STORAGE
  144. // ------------------------------------------------------------------------
  145. /**
  146. * default database type when storing PGT's to database
  147. */
  148. define("CAS_PGT_STORAGE_DB_DEFAULT_DATABASE_TYPE", 'mysql');
  149. /**
  150. * default host when storing PGT's to database
  151. */
  152. define("CAS_PGT_STORAGE_DB_DEFAULT_HOSTNAME", 'localhost');
  153. /**
  154. * default port when storing PGT's to database
  155. */
  156. define("CAS_PGT_STORAGE_DB_DEFAULT_PORT", '');
  157. /**
  158. * default database when storing PGT's to database
  159. */
  160. define("CAS_PGT_STORAGE_DB_DEFAULT_DATABASE", 'phpCAS');
  161. /**
  162. * default table when storing PGT's to database
  163. */
  164. define("CAS_PGT_STORAGE_DB_DEFAULT_TABLE", 'pgt');
  165. /** @} */
  166. // ------------------------------------------------------------------------
  167. // SERVICE ACCESS ERRORS
  168. // ------------------------------------------------------------------------
  169. /**
  170. * @addtogroup publicServices
  171. * @{
  172. */
  173. /**
  174. * phpCAS::service() error code on success
  175. */
  176. define("PHPCAS_SERVICE_OK", 0);
  177. /**
  178. * phpCAS::service() error code when the PT could not retrieve because
  179. * the CAS server did not respond.
  180. */
  181. define("PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE", 1);
  182. /**
  183. * phpCAS::service() error code when the PT could not retrieve because
  184. * the response of the CAS server was ill-formed.
  185. */
  186. define("PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE", 2);
  187. /**
  188. * phpCAS::service() error code when the PT could not retrieve because
  189. * the CAS server did not want to.
  190. */
  191. define("PHPCAS_SERVICE_PT_FAILURE", 3);
  192. /**
  193. * phpCAS::service() error code when the service was not available.
  194. */
  195. define("PHPCAS_SERVICE_NOT AVAILABLE", 4);
  196. /** @} */
  197. // ------------------------------------------------------------------------
  198. // LANGUAGES
  199. // ------------------------------------------------------------------------
  200. /**
  201. * @addtogroup publicLang
  202. * @{
  203. */
  204. define("PHPCAS_LANG_ENGLISH", 'english');
  205. define("PHPCAS_LANG_FRENCH", 'french');
  206. define("PHPCAS_LANG_GREEK", 'greek');
  207. define("PHPCAS_LANG_GERMAN", 'german');
  208. define("PHPCAS_LANG_JAPANESE", 'japanese');
  209. define("PHPCAS_LANG_SPANISH", 'spanish');
  210. define("PHPCAS_LANG_CATALAN", 'catalan');
  211. /** @} */
  212. /**
  213. * @addtogroup internalLang
  214. * @{
  215. */
  216. /**
  217. * phpCAS default language (when phpCAS::setLang() is not used)
  218. */
  219. define("PHPCAS_LANG_DEFAULT", PHPCAS_LANG_ENGLISH);
  220. /** @} */
  221. // ------------------------------------------------------------------------
  222. // DEBUG
  223. // ------------------------------------------------------------------------
  224. /**
  225. * @addtogroup publicDebug
  226. * @{
  227. */
  228. /**
  229. * The default directory for the debug file under Unix.
  230. */
  231. define('DEFAULT_DEBUG_DIR', '/tmp/');
  232. /** @} */
  233. // ------------------------------------------------------------------------
  234. // MISC
  235. // ------------------------------------------------------------------------
  236. /**
  237. * @addtogroup internalMisc
  238. * @{
  239. */
  240. /**
  241. * This global variable is used by the interface class phpCAS.
  242. *
  243. * @hideinitializer
  244. */
  245. $GLOBALS['PHPCAS_CLIENT'] = null;
  246. /**
  247. * This global variable is used to store where the initializer is called from
  248. * (to print a comprehensive error in case of multiple calls).
  249. *
  250. * @hideinitializer
  251. */
  252. $GLOBALS['PHPCAS_INIT_CALL'] = array(
  253. 'done' => false,
  254. 'file' => '?',
  255. 'line' => -1,
  256. 'method' => '?'
  257. );
  258. /**
  259. * This global variable is used to store where the method checking
  260. * the authentication is called from (to print comprehensive errors)
  261. *
  262. * @hideinitializer
  263. */
  264. $GLOBALS['PHPCAS_AUTH_CHECK_CALL'] = array(
  265. 'done' => false,
  266. 'file' => '?',
  267. 'line' => -1,
  268. 'method' => '?',
  269. 'result' => false
  270. );
  271. /**
  272. * This global variable is used to store phpCAS debug mode.
  273. *
  274. * @hideinitializer
  275. */
  276. $GLOBALS['PHPCAS_DEBUG'] = array(
  277. 'filename' => '/tmp/cas.log',
  278. 'indent' => 0,
  279. 'unique_id' => ''
  280. );
  281. /** @} */
  282. // ########################################################################
  283. // CLIENT CLASS
  284. // ########################################################################
  285. // include client class
  286. include_once __DIR__.'/CAS/client.php';
  287. // ########################################################################
  288. // INTERFACE CLASS
  289. // ########################################################################
  290. /**
  291. * @class phpCAS
  292. * The phpCAS class is a simple container for the phpCAS library. It provides CAS
  293. * authentication for web applications written in PHP.
  294. *
  295. * @ingroup public
  296. * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
  297. *
  298. * \internal All its methods access the same object ($PHPCAS_CLIENT, declared
  299. * at the end of CAS/client.php).
  300. */
  301. class phpCAS
  302. {
  303. // ########################################################################
  304. // INITIALIZATION
  305. // ########################################################################
  306. /**
  307. * @addtogroup publicInit
  308. * @{
  309. */
  310. /**
  311. * phpCAS client initializer.
  312. * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
  313. * called, only once, and before all other methods (except phpCAS::getVersion()
  314. * and phpCAS::setDebug()).
  315. *
  316. * @param $server_version the version of the CAS server
  317. * @param $server_hostname the hostname of the CAS server
  318. * @param $server_port the port the CAS server is running on
  319. * @param $server_uri the URI the CAS server is responding on
  320. * @param $start_session Have phpCAS start PHP sessions (default true)
  321. *
  322. * @return a newly created CASClient object
  323. */
  324. public function client($server_version, $server_hostname, $server_port, $server_uri, $start_session = true)
  325. {
  326. global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL;
  327. self::traceBegin();
  328. if (is_object($PHPCAS_CLIENT)) {
  329. self::error($PHPCAS_INIT_CALL['method'].'() has already been called (at '.$PHPCAS_INIT_CALL['file'].':'.$PHPCAS_INIT_CALL['line'].')');
  330. }
  331. if (gettype($server_version) != 'string') {
  332. self::error('type mismatched for parameter $server_version (should be `string\')');
  333. }
  334. if (gettype($server_hostname) != 'string') {
  335. self::error('type mismatched for parameter $server_hostname (should be `string\')');
  336. }
  337. if (gettype($server_port) != 'integer') {
  338. self::error('type mismatched for parameter $server_port (should be `integer\')');
  339. }
  340. if (gettype($server_uri) != 'string') {
  341. self::error('type mismatched for parameter $server_uri (should be `string\')');
  342. }
  343. // store where the initializer is called from
  344. $dbg = self::backtrace();
  345. $PHPCAS_INIT_CALL = array(
  346. 'done' => true,
  347. 'file' => $dbg[0]['file'],
  348. 'line' => $dbg[0]['line'],
  349. 'method' => __CLASS__.'::'.__FUNCTION__
  350. );
  351. // initialize the global object $PHPCAS_CLIENT
  352. $PHPCAS_CLIENT = new CASClient($server_version, false /*proxy*/
  353. , $server_hostname, $server_port, $server_uri, $start_session);
  354. self::traceEnd();
  355. }
  356. /**
  357. * phpCAS proxy initializer.
  358. * @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
  359. * called, only once, and before all other methods (except phpCAS::getVersion()
  360. * and phpCAS::setDebug()).
  361. *
  362. * @param $server_version the version of the CAS server
  363. * @param $server_hostname the hostname of the CAS server
  364. * @param $server_port the port the CAS server is running on
  365. * @param $server_uri the URI the CAS server is responding on
  366. * @param $start_session Have phpCAS start PHP sessions (default true)
  367. *
  368. * @return a newly created CASClient object
  369. */
  370. public function proxy($server_version, $server_hostname, $server_port, $server_uri, $start_session = true)
  371. {
  372. global $PHPCAS_CLIENT, $PHPCAS_INIT_CALL;
  373. self::traceBegin();
  374. if (is_object($PHPCAS_CLIENT)) {
  375. self::error($PHPCAS_INIT_CALL['method'].'() has already been called (at '.$PHPCAS_INIT_CALL['file'].':'.$PHPCAS_INIT_CALL['line'].')');
  376. }
  377. if (gettype($server_version) != 'string') {
  378. self::error('type mismatched for parameter $server_version (should be `string\')');
  379. }
  380. if (gettype($server_hostname) != 'string') {
  381. self::error('type mismatched for parameter $server_hostname (should be `string\')');
  382. }
  383. if (gettype($server_port) != 'integer') {
  384. self::error('type mismatched for parameter $server_port (should be `integer\')');
  385. }
  386. if (gettype($server_uri) != 'string') {
  387. self::error('type mismatched for parameter $server_uri (should be `string\')');
  388. }
  389. // store where the initialzer is called from
  390. $dbg = self::backtrace();
  391. $PHPCAS_INIT_CALL = array(
  392. 'done' => true,
  393. 'file' => $dbg[0]['file'],
  394. 'line' => $dbg[0]['line'],
  395. 'method' => __CLASS__.'::'.__FUNCTION__
  396. );
  397. // initialize the global object $PHPCAS_CLIENT
  398. $PHPCAS_CLIENT = new CASClient($server_version, true /*proxy*/
  399. , $server_hostname, $server_port, $server_uri, $start_session);
  400. self::traceEnd();
  401. }
  402. /** @} */
  403. // ########################################################################
  404. // DEBUGGING
  405. // ########################################################################
  406. /**
  407. * @addtogroup publicDebug
  408. * @{
  409. */
  410. /**
  411. * Set/unset debug mode
  412. *
  413. * @param $filename the name of the file used for logging, or FALSE to stop debugging.
  414. */
  415. public function setDebug($filename = '')
  416. {
  417. global $PHPCAS_DEBUG;
  418. if ($filename != false && gettype($filename) != 'string') {
  419. self::error('type mismatched for parameter $dbg (should be FALSE or the name of the log file)');
  420. }
  421. if (empty ($filename)) {
  422. if (preg_match('/^Win.*/', getenv('OS'))) {
  423. if (isset ($_ENV['TMP'])) {
  424. $debugDir = $_ENV['TMP'].'/';
  425. } else {
  426. if (isset ($_ENV['TEMP'])) {
  427. $debugDir = $_ENV['TEMP'].'/';
  428. } else {
  429. $debugDir = '';
  430. }
  431. }
  432. } else {
  433. $debugDir = DEFAULT_DEBUG_DIR;
  434. }
  435. $filename = $debugDir.'phpCAS.log';
  436. }
  437. if (empty ($PHPCAS_DEBUG['unique_id'])) {
  438. $PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))), 0, 4);
  439. }
  440. $PHPCAS_DEBUG['filename'] = $filename;
  441. self::trace('START ******************');
  442. }
  443. /** @} */
  444. /**
  445. * @addtogroup internalDebug
  446. * @{
  447. */
  448. /**
  449. * This method is a wrapper for debug_backtrace() that is not available
  450. * in all PHP versions (>= 4.3.0 only)
  451. */
  452. public function backtrace()
  453. {
  454. if (function_exists('debug_backtrace')) {
  455. return debug_backtrace();
  456. } else {
  457. // poor man's hack ... but it does work ...
  458. return array();
  459. }
  460. }
  461. /**
  462. * Logs a string in debug mode.
  463. *
  464. * @param $str the string to write
  465. *
  466. * @private
  467. */
  468. public function log($str)
  469. {
  470. $indent_str = ".";
  471. global $PHPCAS_DEBUG;
  472. if ($PHPCAS_DEBUG['filename']) {
  473. for ($i = 0; $i < $PHPCAS_DEBUG['indent']; $i++) {
  474. $indent_str .= '| ';
  475. }
  476. error_log($PHPCAS_DEBUG['unique_id'].' '.$indent_str.$str."\n", 3, $PHPCAS_DEBUG['filename']);
  477. }
  478. }
  479. /**
  480. * This method is used by interface methods to print an error and where the function
  481. * was originally called from.
  482. *
  483. * @param $msg the message to print
  484. *
  485. * @private
  486. */
  487. public function error($msg)
  488. {
  489. $dbg = self::backtrace();
  490. $function = '?';
  491. $file = '?';
  492. $line = '?';
  493. if (is_array($dbg)) {
  494. for ($i = 1; $i < sizeof($dbg); $i++) {
  495. if (is_array($dbg[$i])) {
  496. if ($dbg[$i]['class'] == __CLASS__) {
  497. $function = $dbg[$i]['function'];
  498. $file = $dbg[$i]['file'];
  499. $line = $dbg[$i]['line'];
  500. }
  501. }
  502. }
  503. }
  504. 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";
  505. self::trace($msg);
  506. self::traceExit();
  507. exit ();
  508. }
  509. /**
  510. * This method is used to log something in debug mode.
  511. */
  512. public function trace($str)
  513. {
  514. $dbg = self::backtrace();
  515. self::log($str.' ['.basename($dbg[1]['file']).':'.$dbg[1]['line'].']');
  516. }
  517. /**
  518. * This method is used to indicate the start of the execution of a function in debug mode.
  519. */
  520. public function traceBegin()
  521. {
  522. global $PHPCAS_DEBUG;
  523. $dbg = self::backtrace();
  524. $str = '=> ';
  525. if (!empty ($dbg[2]['class'])) {
  526. $str .= $dbg[2]['class'].'::';
  527. }
  528. $str .= $dbg[2]['function'].'(';
  529. if (is_array($dbg[2]['args'])) {
  530. foreach ($dbg[2]['args'] as $index => $arg) {
  531. if ($index != 0) {
  532. $str .= ', ';
  533. }
  534. $str .= str_replace("\n", "", var_export($arg, true));
  535. }
  536. }
  537. $str .= ') ['.basename($dbg[2]['file']).':'.$dbg[2]['line'].']';
  538. self::log($str);
  539. $PHPCAS_DEBUG['indent']++;
  540. }
  541. /**
  542. * This method is used to indicate the end of the execution of a function in debug mode.
  543. *
  544. * @param string $res The result of the function
  545. */
  546. public function traceEnd($res = '')
  547. {
  548. global $PHPCAS_DEBUG;
  549. $PHPCAS_DEBUG['indent']--;
  550. $dbg = self::backtrace();
  551. $str = '';
  552. $str .= '<= '.str_replace("\n", "", var_export($res, true));
  553. self::log($str);
  554. }
  555. /**
  556. * This method is used to indicate the end of the execution of the program
  557. */
  558. public function traceExit()
  559. {
  560. global $PHPCAS_DEBUG;
  561. self::log('exit()');
  562. while ($PHPCAS_DEBUG['indent'] > 0) {
  563. self::log('-');
  564. $PHPCAS_DEBUG['indent']--;
  565. }
  566. }
  567. /** @} */
  568. // ########################################################################
  569. // INTERNATIONALIZATION
  570. // ########################################################################
  571. /**
  572. * @addtogroup publicLang
  573. * @{
  574. */
  575. /**
  576. * This method is used to set the language used by phpCAS.
  577. * @note Can be called only once.
  578. *
  579. * @param $lang a string representing the language.
  580. *
  581. * @sa PHPCAS_LANG_FRENCH, PHPCAS_LANG_ENGLISH
  582. */
  583. public function setLang($lang)
  584. {
  585. global $PHPCAS_CLIENT;
  586. if (!is_object($PHPCAS_CLIENT)) {
  587. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  588. }
  589. if (gettype($lang) != 'string') {
  590. self::error('type mismatched for parameter $lang (should be `string\')');
  591. }
  592. $PHPCAS_CLIENT->setLang($lang);
  593. }
  594. /** @} */
  595. // ########################################################################
  596. // VERSION
  597. // ########################################################################
  598. /**
  599. * @addtogroup public
  600. * @{
  601. */
  602. /**
  603. * This method returns the phpCAS version.
  604. *
  605. * @return the phpCAS version.
  606. */
  607. public function getVersion()
  608. {
  609. return PHPCAS_VERSION;
  610. }
  611. /** @} */
  612. // ########################################################################
  613. // HTML OUTPUT
  614. // ########################################################################
  615. /**
  616. * @addtogroup publicOutput
  617. * @{
  618. */
  619. /**
  620. * This method sets the HTML header used for all outputs.
  621. *
  622. * @param $header the HTML header.
  623. */
  624. public function setHTMLHeader($header)
  625. {
  626. global $PHPCAS_CLIENT;
  627. if (!is_object($PHPCAS_CLIENT)) {
  628. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  629. }
  630. if (gettype($header) != 'string') {
  631. self::error('type mismatched for parameter $header (should be `string\')');
  632. }
  633. $PHPCAS_CLIENT->setHTMLHeader($header);
  634. }
  635. /**
  636. * This method sets the HTML footer used for all outputs.
  637. *
  638. * @param $footer the HTML footer.
  639. */
  640. public function setHTMLFooter($footer)
  641. {
  642. global $PHPCAS_CLIENT;
  643. if (!is_object($PHPCAS_CLIENT)) {
  644. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  645. }
  646. if (gettype($footer) != 'string') {
  647. self::error('type mismatched for parameter $footer (should be `string\')');
  648. }
  649. $PHPCAS_CLIENT->setHTMLFooter($footer);
  650. }
  651. /** @} */
  652. // ########################################################################
  653. // PGT STORAGE
  654. // ########################################################################
  655. /**
  656. * @addtogroup publicPGTStorage
  657. * @{
  658. */
  659. /**
  660. * This method is used to tell phpCAS to store the response of the
  661. * CAS server to PGT requests onto the filesystem.
  662. *
  663. * @param string $format the format used to store the PGT's (`plain' and `xml' allowed)
  664. * @param string $path the path where the PGT's should be stored
  665. */
  666. public function setPGTStorageFile($format = '', $path = '')
  667. {
  668. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  669. self::traceBegin();
  670. if (!is_object($PHPCAS_CLIENT)) {
  671. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  672. }
  673. if (!$PHPCAS_CLIENT->isProxy()) {
  674. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  675. }
  676. if ($PHPCAS_AUTH_CHECK_CALL['done']) {
  677. self::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'].')');
  678. }
  679. if (gettype($format) != 'string') {
  680. self::error('type mismatched for parameter $format (should be `string\')');
  681. }
  682. if (gettype($path) != 'string') {
  683. self::error('type mismatched for parameter $format (should be `string\')');
  684. }
  685. $PHPCAS_CLIENT->setPGTStorageFile($format, $path);
  686. self::traceEnd();
  687. }
  688. /**
  689. * This method is used to tell phpCAS to store the response of the
  690. * CAS server to PGT requests into a database.
  691. * @note The connection to the database is done only when needed.
  692. * As a consequence, bad parameters are detected only when
  693. * initializing PGT storage, except in debug mode.
  694. *
  695. * @param $user the user to access the data with
  696. * @param $password the user's password
  697. * @param $database_type the type of the database hosting the data
  698. * @param $hostname the server hosting the database
  699. * @param $port the port the server is listening on
  700. * @param $database the name of the database
  701. * @param $table the name of the table storing the data
  702. */
  703. public function setPGTStorageDB(
  704. $user,
  705. $password,
  706. $database_type = '',
  707. $hostname = '',
  708. $port = 0,
  709. $database = '',
  710. $table = ''
  711. ) {
  712. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  713. self::traceBegin();
  714. if (!is_object($PHPCAS_CLIENT)) {
  715. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  716. }
  717. if (!$PHPCAS_CLIENT->isProxy()) {
  718. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  719. }
  720. if ($PHPCAS_AUTH_CHECK_CALL['done']) {
  721. self::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'].')');
  722. }
  723. if (gettype($user) != 'string') {
  724. self::error('type mismatched for parameter $user (should be `string\')');
  725. }
  726. if (gettype($password) != 'string') {
  727. self::error('type mismatched for parameter $password (should be `string\')');
  728. }
  729. if (gettype($database_type) != 'string') {
  730. self::error('type mismatched for parameter $database_type (should be `string\')');
  731. }
  732. if (gettype($hostname) != 'string') {
  733. self::error('type mismatched for parameter $hostname (should be `string\')');
  734. }
  735. if (gettype($port) != 'integer') {
  736. self::error('type mismatched for parameter $port (should be `integer\')');
  737. }
  738. if (gettype($database) != 'string') {
  739. self::error('type mismatched for parameter $database (should be `string\')');
  740. }
  741. if (gettype($table) != 'string') {
  742. self::error('type mismatched for parameter $table (should be `string\')');
  743. }
  744. $PHPCAS_CLIENT->setPGTStorageDB($user, $password, $database_type, $hostname, $port, $database, $table);
  745. self::traceEnd();
  746. }
  747. /** @} */
  748. // ########################################################################
  749. // ACCESS TO EXTERNAL SERVICES
  750. // ########################################################################
  751. /**
  752. * @addtogroup publicServices
  753. * @{
  754. */
  755. /**
  756. * This method is used to access an HTTP[S] service.
  757. *
  758. * @param $url the service to access.
  759. * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on
  760. * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE,
  761. * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE.
  762. * @param $output the output of the service (also used to give an error
  763. * message on failure).
  764. *
  765. * @return TRUE on success, FALSE otherwise (in this later case, $err_code
  766. * gives the reason why it failed and $output contains an error message).
  767. */
  768. public function serviceWeb($url, & $err_code, & $output)
  769. {
  770. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  771. self::traceBegin();
  772. if (!is_object($PHPCAS_CLIENT)) {
  773. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  774. }
  775. if (!$PHPCAS_CLIENT->isProxy()) {
  776. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  777. }
  778. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  779. self::error('this method should only be called after the programmer is sure the user has been authenticated (by calling '.__CLASS__.'::checkAuthentication() or '.__CLASS__.'::forceAuthentication()');
  780. }
  781. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  782. self::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');
  783. }
  784. if (gettype($url) != 'string') {
  785. self::error('type mismatched for parameter $url (should be `string\')');
  786. }
  787. $res = $PHPCAS_CLIENT->serviceWeb($url, $err_code, $output);
  788. self::traceEnd($res);
  789. return $res;
  790. }
  791. /**
  792. * This method is used to access an IMAP/POP3/NNTP service.
  793. *
  794. * @param $url a string giving the URL of the service, including the mailing box
  795. * for IMAP URLs, as accepted by imap_open().
  796. * @param $service a string giving for CAS retrieve Proxy ticket
  797. * @param $flags options given to imap_open().
  798. * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on
  799. * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE,
  800. * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE.
  801. * @param $err_msg an error message on failure
  802. * @param $pt the Proxy Ticket (PT) retrieved from the CAS server to access the URL
  803. * on success, FALSE on error).
  804. *
  805. * @return an IMAP stream on success, FALSE otherwise (in this later case, $err_code
  806. * gives the reason why it failed and $err_msg contains an error message).
  807. */
  808. public function serviceMail($url, $service, $flags, & $err_code, & $err_msg, & $pt)
  809. {
  810. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  811. self::traceBegin();
  812. if (!is_object($PHPCAS_CLIENT)) {
  813. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  814. }
  815. if (!$PHPCAS_CLIENT->isProxy()) {
  816. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  817. }
  818. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  819. self::error('this method should only be called after the programmer is sure the user has been authenticated (by calling '.__CLASS__.'::checkAuthentication() or '.__CLASS__.'::forceAuthentication()');
  820. }
  821. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  822. self::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');
  823. }
  824. if (gettype($url) != 'string') {
  825. self::error('type mismatched for parameter $url (should be `string\')');
  826. }
  827. if (gettype($flags) != 'integer') {
  828. self::error('type mismatched for parameter $flags (should be `integer\')');
  829. }
  830. $res = $PHPCAS_CLIENT->serviceMail($url, $service, $flags, $err_code, $err_msg, $pt);
  831. self::traceEnd($res);
  832. return $res;
  833. }
  834. /** @} */
  835. // ########################################################################
  836. // AUTHENTICATION
  837. // ########################################################################
  838. /**
  839. * @addtogroup publicAuth
  840. * @{
  841. */
  842. /**
  843. * Set the times authentication will be cached before really accessing the CAS server in gateway mode:
  844. * - -1: check only once, and then never again (until you pree login)
  845. * - 0: always check
  846. * - n: check every "n" time
  847. *
  848. * @param $n an integer.
  849. */
  850. public function setCacheTimesForAuthRecheck($n)
  851. {
  852. global $PHPCAS_CLIENT;
  853. if (!is_object($PHPCAS_CLIENT)) {
  854. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  855. }
  856. if (gettype($n) != 'integer') {
  857. self::error('type mismatched for parameter $header (should be `string\')');
  858. }
  859. $PHPCAS_CLIENT->setCacheTimesForAuthRecheck($n);
  860. }
  861. /**
  862. * This method is called to check if the user is authenticated (use the gateway feature).
  863. * @return TRUE when the user is authenticated; otherwise FALSE.
  864. */
  865. public function checkAuthentication()
  866. {
  867. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  868. self::traceBegin();
  869. if (!is_object($PHPCAS_CLIENT)) {
  870. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  871. }
  872. $auth = $PHPCAS_CLIENT->checkAuthentication();
  873. // store where the authentication has been checked and the result
  874. $dbg = self::backtrace();
  875. $PHPCAS_AUTH_CHECK_CALL = array(
  876. 'done' => true,
  877. 'file' => $dbg[0]['file'],
  878. 'line' => $dbg[0]['line'],
  879. 'method' => __CLASS__.'::'.__FUNCTION__,
  880. 'result' => $auth
  881. );
  882. self::traceEnd($auth);
  883. return $auth;
  884. }
  885. /**
  886. * This method is called to force authentication if the user was not already
  887. * authenticated. If the user is not authenticated, halt by redirecting to
  888. * the CAS server.
  889. */
  890. public function forceAuthentication()
  891. {
  892. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  893. self::traceBegin();
  894. if (!is_object($PHPCAS_CLIENT)) {
  895. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  896. }
  897. $auth = $PHPCAS_CLIENT->forceAuthentication();
  898. // store where the authentication has been checked and the result
  899. $dbg = self::backtrace();
  900. $PHPCAS_AUTH_CHECK_CALL = array(
  901. 'done' => true,
  902. 'file' => $dbg[0]['file'],
  903. 'line' => $dbg[0]['line'],
  904. 'method' => __CLASS__.'::'.__FUNCTION__,
  905. 'result' => $auth
  906. );
  907. if (!$auth) {
  908. self::trace('user is not authenticated, redirecting to the CAS server');
  909. $PHPCAS_CLIENT->forceAuthentication();
  910. } else {
  911. self::trace('no need to authenticate (user `'.self::getUser().'\' is already authenticated)');
  912. }
  913. self::traceEnd();
  914. return $auth;
  915. }
  916. /**
  917. * This method is called to renew the authentication.
  918. **/
  919. public function renewAuthentication()
  920. {
  921. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  922. self::traceBegin();
  923. if (!is_object($PHPCAS_CLIENT)) {
  924. self::error('this method should not be called before'.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  925. }
  926. // store where the authentication has been checked and the result
  927. $dbg = self::backtrace();
  928. $PHPCAS_AUTH_CHECK_CALL = array(
  929. 'done' => true,
  930. 'file' => $dbg[0]['file'],
  931. 'line' => $dbg[0]['line'],
  932. 'method' => __CLASS__.'::'.__FUNCTION__,
  933. 'result' => $auth
  934. );
  935. $PHPCAS_CLIENT->renewAuthentication();
  936. self::traceEnd();
  937. }
  938. /**
  939. * This method has been left from version 0.4.1 for compatibility reasons.
  940. */
  941. public function authenticate()
  942. {
  943. self::error('this method is deprecated. You should use '.__CLASS__.'::forceAuthentication() instead');
  944. }
  945. /**
  946. * This method is called to check if the user is authenticated (previously or by
  947. * tickets given in the URL).
  948. *
  949. * @return TRUE when the user is authenticated.
  950. */
  951. public function isAuthenticated()
  952. {
  953. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  954. self::traceBegin();
  955. if (!is_object($PHPCAS_CLIENT)) {
  956. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  957. }
  958. // call the isAuthenticated method of the global $PHPCAS_CLIENT object
  959. $auth = $PHPCAS_CLIENT->isAuthenticated();
  960. // store where the authentication has been checked and the result
  961. $dbg = self::backtrace();
  962. $PHPCAS_AUTH_CHECK_CALL = array(
  963. 'done' => true,
  964. 'file' => $dbg[0]['file'],
  965. 'line' => $dbg[0]['line'],
  966. 'method' => __CLASS__.'::'.__FUNCTION__,
  967. 'result' => $auth
  968. );
  969. self::traceEnd($auth);
  970. return $auth;
  971. }
  972. /**
  973. * Checks whether authenticated based on $_SESSION. Useful to avoid
  974. * server calls.
  975. * @return true if authenticated, false otherwise.
  976. * @since 0.4.22 by Brendan Arnold
  977. */
  978. public function isSessionAuthenticated()
  979. {
  980. global $PHPCAS_CLIENT;
  981. if (!is_object($PHPCAS_CLIENT)) {
  982. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  983. }
  984. return ($PHPCAS_CLIENT->isSessionAuthenticated());
  985. }
  986. /**
  987. * This method returns the CAS user's login name.
  988. * @warning should not be called only after phpCAS::forceAuthentication()
  989. * or phpCAS::checkAuthentication().
  990. *
  991. * @return the login name of the authenticated user
  992. */
  993. public function getUser()
  994. {
  995. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  996. if (!is_object($PHPCAS_CLIENT)) {
  997. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  998. }
  999. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  1000. self::error('this method should only be called after '.__CLASS__.'::forceAuthentication() or '.__CLASS__.'::isAuthenticated()');
  1001. }
  1002. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  1003. self::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');
  1004. }
  1005. return $PHPCAS_CLIENT->getUser();
  1006. }
  1007. /**
  1008. * This method returns the CAS user's login name.
  1009. * @warning should not be called only after phpCAS::forceAuthentication()
  1010. * or phpCAS::checkAuthentication().
  1011. *
  1012. * @return the login name of the authenticated user
  1013. */
  1014. public function getAttributes()
  1015. {
  1016. global $PHPCAS_CLIENT, $PHPCAS_AUTH_CHECK_CALL;
  1017. if (!is_object($PHPCAS_CLIENT)) {
  1018. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  1019. }
  1020. if (!$PHPCAS_AUTH_CHECK_CALL['done']) {
  1021. self::error('this method should only be called after '.__CLASS__.'::forceAuthentication() or '.__CLASS__.'::isAuthenticated()');
  1022. }
  1023. if (!$PHPCAS_AUTH_CHECK_CALL['result']) {
  1024. self::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');
  1025. }
  1026. return $PHPCAS_CLIENT->getAttributes();
  1027. }
  1028. /**
  1029. * Handle logout requests.
  1030. */
  1031. public function handleLogoutRequests($check_client = true, $allowed_clients = false)
  1032. {
  1033. global $PHPCAS_CLIENT;
  1034. if (!is_object($PHPCAS_CLIENT)) {
  1035. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  1036. }
  1037. return ($PHPCAS_CLIENT->handleLogoutRequests($check_client, $allowed_clients));
  1038. }
  1039. /**
  1040. * This method returns the URL to be used to login.
  1041. * or phpCAS::isAuthenticated().
  1042. *
  1043. * @return the login name of the authenticated user
  1044. */
  1045. public function getServerLoginURL()
  1046. {
  1047. global $PHPCAS_CLIENT;
  1048. if (!is_object($PHPCAS_CLIENT)) {
  1049. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  1050. }
  1051. return $PHPCAS_CLIENT->getServerLoginURL();
  1052. }
  1053. /**
  1054. * Set the login URL of the CAS server.
  1055. * @param string $url the login URL
  1056. * @since 0.4.21 by Wyman Chan
  1057. */
  1058. public function setServerLoginURL($url = '')
  1059. {
  1060. global $PHPCAS_CLIENT;
  1061. self::traceBegin();
  1062. if (!is_object($PHPCAS_CLIENT)) {
  1063. self::error('this method should only be called after
  1064. ' . __CLASS__.'::client()');
  1065. }
  1066. if (gettype($url) != 'string') {
  1067. self::error('type mismatched for parameter $url (should be
  1068. `string\')');
  1069. }
  1070. $PHPCAS_CLIENT->setServerLoginURL($url);
  1071. self::traceEnd();
  1072. }
  1073. /**
  1074. * Set the serviceValidate URL of the CAS server.
  1075. * Used only in CAS 1.0 validations
  1076. * @param string $url the serviceValidate URL
  1077. * @since 1.1.0 by Joachim Fritschi
  1078. */
  1079. public function setServerServiceValidateURL($url = '')
  1080. {
  1081. global $PHPCAS_CLIENT;
  1082. self::traceBegin();
  1083. if (!is_object($PHPCAS_CLIENT)) {
  1084. self::error('this method should only be called after
  1085. ' . __CLASS__.'::client()');
  1086. }
  1087. if (gettype($url) != 'string') {
  1088. self::error('type mismatched for parameter $url (should be
  1089. `string\')');
  1090. }
  1091. $PHPCAS_CLIENT->setServerServiceValidateURL($url);
  1092. self::traceEnd();
  1093. }
  1094. /**
  1095. * Set the proxyValidate URL of the CAS server.
  1096. * Used for all CAS 2.0 validations
  1097. * @param string $url the proxyValidate URL
  1098. * @since 1.1.0 by Joachim Fritschi
  1099. */
  1100. public function setServerProxyValidateURL($url = '')
  1101. {
  1102. global $PHPCAS_CLIENT;
  1103. self::traceBegin();
  1104. if (!is_object($PHPCAS_CLIENT)) {
  1105. self::error('this method should only be called after
  1106. ' . __CLASS__.'::client()');
  1107. }
  1108. if (gettype($url) != 'string') {
  1109. self::error('type mismatched for parameter $url (should be
  1110. `string\')');
  1111. }
  1112. $PHPCAS_CLIENT->setServerProxyValidateURL($url);
  1113. self::traceEnd();
  1114. }
  1115. /**
  1116. * Set the samlValidate URL of the CAS server.
  1117. * @param string $url the samlValidate URL
  1118. * @since 1.1.0 by Joachim Fritschi
  1119. */
  1120. public function setServerSamlValidateURL($url = '')
  1121. {
  1122. global $PHPCAS_CLIENT;
  1123. self::traceBegin();
  1124. if (!is_object($PHPCAS_CLIENT)) {
  1125. self::error('this method should only be called after
  1126. ' . __CLASS__.'::client()');
  1127. }
  1128. if (gettype($url) != 'string') {
  1129. self::error('type mismatched for parameter $url (should be
  1130. `string\')');
  1131. }
  1132. $PHPCAS_CLIENT->setServerSamlValidateURL($url);
  1133. self::traceEnd();
  1134. }
  1135. /**
  1136. * This method returns the URL to be used to login.
  1137. * or phpCAS::isAuthenticated().
  1138. *
  1139. * @return the login name of the authenticated user
  1140. */
  1141. public function getServerLogoutURL()
  1142. {
  1143. global $PHPCAS_CLIENT;
  1144. if (!is_object($PHPCAS_CLIENT)) {
  1145. self::error('this method should not be called before '.__CLASS__.'::client() or '.__CLASS__.'::proxy()');
  1146. }
  1147. return $PHPCAS_CLIENT->getServerLogoutURL();
  1148. }
  1149. /**
  1150. * Set the logout URL of the CAS server.
  1151. * @param string $url the logout URL
  1152. * @since 0.4.21 by Wyman Chan
  1153. */
  1154. public function setServerLogoutURL($url = '')
  1155. {
  1156. global $PHPCAS_CLIENT;
  1157. self::traceBegin();
  1158. if (!is_object($PHPCAS_CLIENT)) {
  1159. self::error('this method should only be called after
  1160. ' . __CLASS__.'::client()');
  1161. }
  1162. if (gettype($url) != 'string') {
  1163. self::error('type mismatched for parameter $url (should be
  1164. `string\')');
  1165. }
  1166. $PHPCAS_CLIENT->setServerLogoutURL($url);
  1167. self::traceEnd();
  1168. }
  1169. /**
  1170. * This method is used to logout from CAS.
  1171. * @params string $params an array that contains the optional url and service parameters that will be passed to the CAS server
  1172. * @public
  1173. */
  1174. public function logout($params = "")
  1175. {
  1176. global $PHPCAS_CLIENT;
  1177. self::traceBegin();
  1178. if (!is_object($PHPCAS_CLIENT)) {
  1179. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1180. }
  1181. $parsedParams = array();
  1182. if ($params != "") {
  1183. if (is_string($params)) {
  1184. self::error('method `phpCAS::logout($url)\' is now deprecated, use `phpCAS::logoutWithUrl($url)\' instead');
  1185. }
  1186. if (!is_array($params)) {
  1187. self::error('type mismatched for parameter $params (should be `array\')');
  1188. }
  1189. foreach ($params as $key => $value) {
  1190. if ($key != "service" && $key != "url") {
  1191. self::error('only `url\' and `service\' parameters are allowed for method `phpCAS::logout($params)\'');
  1192. }
  1193. $parsedParams[$key] = $value;
  1194. }
  1195. }
  1196. $PHPCAS_CLIENT->logout($parsedParams);
  1197. // never reached
  1198. self::traceEnd();
  1199. }
  1200. /**
  1201. * This method is used to logout from CAS. Halts by redirecting to the CAS server.
  1202. * @param string $service a URL that will be transmitted to the CAS server
  1203. */
  1204. public function logoutWithRedirectService($service)
  1205. {
  1206. global $PHPCAS_CLIENT;
  1207. self::traceBegin();
  1208. if (!is_object($PHPCAS_CLIENT)) {
  1209. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1210. }
  1211. if (!is_string($service)) {
  1212. self::error('type mismatched for parameter $service (should be `string\')');
  1213. }
  1214. $PHPCAS_CLIENT->logout(array(
  1215. "service" => $service
  1216. ));
  1217. // never reached
  1218. self::traceEnd();
  1219. }
  1220. /**
  1221. * This method is used to logout from CAS. Halts by redirecting to the CAS server.
  1222. * @param string $url a URL that will be transmitted to the CAS server
  1223. */
  1224. public function logoutWithUrl($url)
  1225. {
  1226. global $PHPCAS_CLIENT;
  1227. self::traceBegin();
  1228. if (!is_object($PHPCAS_CLIENT)) {
  1229. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1230. }
  1231. if (!is_string($url)) {
  1232. self::error('type mismatched for parameter $url (should be `string\')');
  1233. }
  1234. $PHPCAS_CLIENT->logout(array(
  1235. "url" => $url
  1236. ));
  1237. // never reached
  1238. self::traceEnd();
  1239. }
  1240. /**
  1241. * This method is used to logout from CAS. Halts by redirecting to the CAS server.
  1242. * @param string $service a URL that will be transmitted to the CAS server
  1243. * @param string $url a URL that will be transmitted to the CAS server
  1244. */
  1245. public function logoutWithRedirectServiceAndUrl($service, $url)
  1246. {
  1247. global $PHPCAS_CLIENT;
  1248. self::traceBegin();
  1249. if (!is_object($PHPCAS_CLIENT)) {
  1250. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1251. }
  1252. if (!is_string($service)) {
  1253. self::error('type mismatched for parameter $service (should be `string\')');
  1254. }
  1255. if (!is_string($url)) {
  1256. self::error('type mismatched for parameter $url (should be `string\')');
  1257. }
  1258. $PHPCAS_CLIENT->logout(array(
  1259. "service" => $service,
  1260. "url" => $url
  1261. ));
  1262. // never reached
  1263. self::traceEnd();
  1264. }
  1265. /**
  1266. * Set the fixed URL that will be used by the CAS server to transmit the PGT.
  1267. * When this method is not called, a phpCAS script uses its own URL for the callback.
  1268. *
  1269. * @param string $url the URL
  1270. */
  1271. public function setFixedCallbackURL($url = '')
  1272. {
  1273. global $PHPCAS_CLIENT;
  1274. self::traceBegin();
  1275. if (!is_object($PHPCAS_CLIENT)) {
  1276. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  1277. }
  1278. if (!$PHPCAS_CLIENT->isProxy()) {
  1279. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  1280. }
  1281. if (gettype($url) != 'string') {
  1282. self::error('type mismatched for parameter $url (should be `string\')');
  1283. }
  1284. $PHPCAS_CLIENT->setCallbackURL($url);
  1285. self::traceEnd();
  1286. }
  1287. /**
  1288. * Set the fixed URL that will be set as the CAS service parameter. When this
  1289. * method is not called, a phpCAS script uses its own URL.
  1290. *
  1291. * @param string $url the URL
  1292. */
  1293. public function setFixedServiceURL($url)
  1294. {
  1295. global $PHPCAS_CLIENT;
  1296. self::traceBegin();
  1297. if (!is_object($PHPCAS_CLIENT)) {
  1298. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  1299. }
  1300. if (gettype($url) != 'string') {
  1301. self::error('type mismatched for parameter $url (should be `string\')');
  1302. }
  1303. $PHPCAS_CLIENT->setURL($url);
  1304. self::traceEnd();
  1305. }
  1306. /**
  1307. * Get the URL that is set as the CAS service parameter.
  1308. */
  1309. public function getServiceURL()
  1310. {
  1311. global $PHPCAS_CLIENT;
  1312. if (!is_object($PHPCAS_CLIENT)) {
  1313. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  1314. }
  1315. return ($PHPCAS_CLIENT->getURL());
  1316. }
  1317. /**
  1318. * Retrieve a Proxy Ticket from the CAS server.
  1319. */
  1320. public function retrievePT($target_service, & $err_code, & $err_msg)
  1321. {
  1322. global $PHPCAS_CLIENT;
  1323. if (!is_object($PHPCAS_CLIENT)) {
  1324. self::error('this method should only be called after '.__CLASS__.'::proxy()');
  1325. }
  1326. if (gettype($target_service) != 'string') {
  1327. self::error('type mismatched for parameter $target_service(should be `string\')');
  1328. }
  1329. return ($PHPCAS_CLIENT->retrievePT($target_service, $err_code, $err_msg));
  1330. }
  1331. /**
  1332. * Set the certificate of the CAS server.
  1333. *
  1334. * @param $cert the PEM certificate
  1335. */
  1336. public function setCasServerCert($cert)
  1337. {
  1338. global $PHPCAS_CLIENT;
  1339. self::traceBegin();
  1340. if (!is_object($PHPCAS_CLIENT)) {
  1341. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1342. }
  1343. if (gettype($cert) != 'string') {
  1344. self::error('type mismatched for parameter $cert (should be `string\')');
  1345. }
  1346. $PHPCAS_CLIENT->setCasServerCert($cert);
  1347. self::traceEnd();
  1348. }
  1349. /**
  1350. * Set the certificate of the CAS server CA.
  1351. *
  1352. * @param $cert the CA certificate
  1353. */
  1354. public function setCasServerCACert($cert)
  1355. {
  1356. global $PHPCAS_CLIENT;
  1357. self::traceBegin();
  1358. if (!is_object($PHPCAS_CLIENT)) {
  1359. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1360. }
  1361. if (gettype($cert) != 'string') {
  1362. self::error('type mismatched for parameter $cert (should be `string\')');
  1363. }
  1364. $PHPCAS_CLIENT->setCasServerCACert($cert);
  1365. self::traceEnd();
  1366. }
  1367. /**
  1368. * Set no SSL validation for the CAS server.
  1369. */
  1370. public function setNoCasServerValidation()
  1371. {
  1372. global $PHPCAS_CLIENT;
  1373. self::traceBegin();
  1374. if (!is_object($PHPCAS_CLIENT)) {
  1375. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1376. }
  1377. $PHPCAS_CLIENT->setNoCasServerValidation();
  1378. self::traceEnd();
  1379. }
  1380. /** @} */
  1381. /**
  1382. * Change CURL options.
  1383. * CURL is used to connect through HTTPS to CAS server
  1384. * @param string $key the option key
  1385. * @param string $value the value to set
  1386. */
  1387. public function setExtraCurlOption($key, $value)
  1388. {
  1389. global $PHPCAS_CLIENT;
  1390. self::traceBegin();
  1391. if (!is_object($PHPCAS_CLIENT)) {
  1392. self::error('this method should only be called after '.__CLASS__.'::client() or'.__CLASS__.'::proxy()');
  1393. }
  1394. $PHPCAS_CLIENT->setExtraCurlOption($key, $value);
  1395. self::traceEnd();
  1396. }
  1397. }
  1398. // ########################################################################
  1399. // DOCUMENTATION
  1400. // ########################################################################
  1401. // ########################################################################
  1402. // MAIN PAGE
  1403. /**
  1404. * @mainpage
  1405. *
  1406. * The following pages only show the source documentation.
  1407. *
  1408. */
  1409. // ########################################################################
  1410. // MODULES DEFINITION
  1411. /** @defgroup public User interface */
  1412. /** @defgroup publicInit Initialization
  1413. * @ingroup public
  1414. */
  1415. /** @defgroup publicAuth Authentication
  1416. * @ingroup public
  1417. */
  1418. /** @defgroup publicServices Access to external services
  1419. * @ingroup public
  1420. */
  1421. /** @defgroup publicConfig Configuration
  1422. * @ingroup public
  1423. */
  1424. /** @defgroup publicLang Internationalization
  1425. * @ingroup publicConfig
  1426. */
  1427. /** @defgroup publicOutput HTML output
  1428. * @ingroup publicConfig
  1429. */
  1430. /** @defgroup publicPGTStorage PGT storage
  1431. * @ingroup publicConfig
  1432. */
  1433. /** @defgroup publicDebug Debugging
  1434. * @ingroup public
  1435. */
  1436. /** @defgroup internal Implementation */
  1437. /** @defgroup internalAuthentication Authentication
  1438. * @ingroup internal
  1439. */
  1440. /** @defgroup internalBasic CAS Basic client features (CAS 1.0, Service Tickets)
  1441. * @ingroup internal
  1442. */
  1443. /** @defgroup internalProxy CAS Proxy features (CAS 2.0, Proxy Granting Tickets)
  1444. * @ingroup internal
  1445. */
  1446. /** @defgroup internalPGTStorage PGT storage
  1447. * @ingroup internalProxy
  1448. */
  1449. /** @defgroup internalPGTStorageDB PGT storage in a database
  1450. * @ingroup internalPGTStorage
  1451. */
  1452. /** @defgroup internalPGTStorageFile PGT storage on the filesystem
  1453. * @ingroup internalPGTStorage
  1454. */
  1455. /** @defgroup internalCallback Callback from the CAS server
  1456. * @ingroup internalProxy
  1457. */
  1458. /** @defgroup internalProxied CAS proxied client features (CAS 2.0, Proxy Tickets)
  1459. * @ingroup internal
  1460. */
  1461. /** @defgroup internalConfig Configuration
  1462. * @ingroup internal
  1463. */
  1464. /** @defgroup internalOutput HTML output
  1465. * @ingroup internalConfig
  1466. */
  1467. /** @defgroup internalLang Internationalization
  1468. * @ingroup internalConfig
  1469. *
  1470. * To add a new language:
  1471. * - 1. define a new constant PHPCAS_LANG_XXXXXX in CAS/CAS.php
  1472. * - 2. copy any file from CAS/languages to CAS/languages/XXXXXX.php
  1473. * - 3. Make the translations
  1474. */
  1475. /** @defgroup internalDebug Debugging
  1476. * @ingroup internal
  1477. */
  1478. /** @defgroup internalMisc Miscellaneous
  1479. * @ingroup internal
  1480. */
  1481. // ########################################################################
  1482. // EXAMPLES
  1483. /**
  1484. * @example example_simple.php
  1485. */
  1486. /**
  1487. * @example example_proxy.php
  1488. */
  1489. /**
  1490. * @example example_proxy2.php
  1491. */
  1492. /**
  1493. * @example example_lang.php
  1494. */
  1495. /**
  1496. * @example example_html.php
  1497. */
  1498. /**
  1499. * @example example_file.php
  1500. */
  1501. /**
  1502. * @example example_db.php
  1503. */
  1504. /**
  1505. * @example example_service.php
  1506. */
  1507. /**
  1508. * @example example_session_proxy.php
  1509. */
  1510. /**
  1511. * @example example_session_service.php
  1512. */
  1513. /**
  1514. * @example example_gateway.php
  1515. */
  1516. /**
  1517. * @example example_custom_urls.php
  1518. */