-
Notifications
You must be signed in to change notification settings - Fork 396
/
CAS.php
2083 lines (1846 loc) · 63.8 KB
/
CAS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Licensed to Jasig under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* Jasig licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
* Interface class of the phpCAS library
* PHP Version 7
*
* @file CAS/CAS.php
* @category Authentication
* @package PhpCAS
* @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
* @author Olivier Berger <olivier.berger@it-sudparis.eu>
* @author Brett Bieber <brett.bieber@gmail.com>
* @author Joachim Fritschi <jfritschi@freenet.de>
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
* @ingroup public
*/
use Psr\Log\LoggerInterface;
//
// hack by Vangelis Haniotakis to handle the absence of $_SERVER['REQUEST_URI']
// in IIS
//
if (!isset($_SERVER['REQUEST_URI']) && isset($_SERVER['SCRIPT_NAME']) && isset($_SERVER['QUERY_STRING'])) {
$_SERVER['REQUEST_URI'] = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
}
// ########################################################################
// CONSTANTS
// ########################################################################
// ------------------------------------------------------------------------
// CAS VERSIONS
// ------------------------------------------------------------------------
/**
* phpCAS version. accessible for the user by phpCAS::getVersion().
*/
define('PHPCAS_VERSION', '1.6.1+');
/**
* @addtogroup public
* @{
*/
/**
* phpCAS supported protocols. accessible for the user by phpCAS::getSupportedProtocols().
*/
/**
* CAS version 1.0
*/
define("CAS_VERSION_1_0", '1.0');
/*!
* CAS version 2.0
*/
define("CAS_VERSION_2_0", '2.0');
/**
* CAS version 3.0
*/
define("CAS_VERSION_3_0", '3.0');
// ------------------------------------------------------------------------
// SAML defines
// ------------------------------------------------------------------------
/**
* SAML protocol
*/
define("SAML_VERSION_1_1", 'S1');
/**
* XML header for SAML POST
*/
define("SAML_XML_HEADER", '<?xml version="1.0" encoding="UTF-8"?>');
/**
* SOAP envelope for SAML POST
*/
define("SAML_SOAP_ENV", '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/>');
/**
* SOAP body for SAML POST
*/
define("SAML_SOAP_BODY", '<SOAP-ENV:Body>');
/**
* SAMLP request
*/
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">');
define("SAMLP_REQUEST_CLOSE", '</samlp:Request>');
/**
* SAMLP artifact tag (for the ticket)
*/
define("SAML_ASSERTION_ARTIFACT", '<samlp:AssertionArtifact>');
/**
* SAMLP close
*/
define("SAML_ASSERTION_ARTIFACT_CLOSE", '</samlp:AssertionArtifact>');
/**
* SOAP body close
*/
define("SAML_SOAP_BODY_CLOSE", '</SOAP-ENV:Body>');
/**
* SOAP envelope close
*/
define("SAML_SOAP_ENV_CLOSE", '</SOAP-ENV:Envelope>');
/**
* SAML Attributes
*/
define("SAML_ATTRIBUTES", 'SAMLATTRIBS');
/** @} */
/**
* @addtogroup publicPGTStorage
* @{
*/
// ------------------------------------------------------------------------
// FILE PGT STORAGE
// ------------------------------------------------------------------------
/**
* Default path used when storing PGT's to file
*/
define("CAS_PGT_STORAGE_FILE_DEFAULT_PATH", session_save_path());
/** @} */
// ------------------------------------------------------------------------
// SERVICE ACCESS ERRORS
// ------------------------------------------------------------------------
/**
* @addtogroup publicServices
* @{
*/
/**
* phpCAS::service() error code on success
*/
define("PHPCAS_SERVICE_OK", 0);
/**
* phpCAS::service() error code when the PT could not retrieve because
* the CAS server did not respond.
*/
define("PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE", 1);
/**
* phpCAS::service() error code when the PT could not retrieve because
* the response of the CAS server was ill-formed.
*/
define("PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE", 2);
/**
* phpCAS::service() error code when the PT could not retrieve because
* the CAS server did not want to.
*/
define("PHPCAS_SERVICE_PT_FAILURE", 3);
/**
* phpCAS::service() error code when the service was not available.
*/
define("PHPCAS_SERVICE_NOT_AVAILABLE", 4);
// ------------------------------------------------------------------------
// SERVICE TYPES
// ------------------------------------------------------------------------
/**
* phpCAS::getProxiedService() type for HTTP GET
*/
define("PHPCAS_PROXIED_SERVICE_HTTP_GET", 'CAS_ProxiedService_Http_Get');
/**
* phpCAS::getProxiedService() type for HTTP POST
*/
define("PHPCAS_PROXIED_SERVICE_HTTP_POST", 'CAS_ProxiedService_Http_Post');
/**
* phpCAS::getProxiedService() type for IMAP
*/
define("PHPCAS_PROXIED_SERVICE_IMAP", 'CAS_ProxiedService_Imap');
/** @} */
// ------------------------------------------------------------------------
// LANGUAGES
// ------------------------------------------------------------------------
/**
* @addtogroup publicLang
* @{
*/
define("PHPCAS_LANG_ENGLISH", 'CAS_Languages_English');
define("PHPCAS_LANG_FRENCH", 'CAS_Languages_French');
define("PHPCAS_LANG_GREEK", 'CAS_Languages_Greek');
define("PHPCAS_LANG_GERMAN", 'CAS_Languages_German');
define("PHPCAS_LANG_JAPANESE", 'CAS_Languages_Japanese');
define("PHPCAS_LANG_SPANISH", 'CAS_Languages_Spanish');
define("PHPCAS_LANG_CATALAN", 'CAS_Languages_Catalan');
define("PHPCAS_LANG_CHINESE_SIMPLIFIED", 'CAS_Languages_ChineseSimplified');
define("PHPCAS_LANG_GALEGO", 'CAS_Languages_Galego');
define("PHPCAS_LANG_PORTUGUESE", 'CAS_Languages_Portuguese');
/** @} */
/**
* @addtogroup internalLang
* @{
*/
/**
* phpCAS default language (when phpCAS::setLang() is not used)
*/
define("PHPCAS_LANG_DEFAULT", PHPCAS_LANG_ENGLISH);
/** @} */
// ------------------------------------------------------------------------
// DEBUG
// ------------------------------------------------------------------------
/**
* @addtogroup publicDebug
* @{
*/
/**
* The default directory for the debug file under Unix.
* @return string directory for the debug file
*/
function gettmpdir() {
if (!empty($_ENV['TMP'])) { return realpath($_ENV['TMP']); }
if (!empty($_ENV['TMPDIR'])) { return realpath( $_ENV['TMPDIR']); }
if (!empty($_ENV['TEMP'])) { return realpath( $_ENV['TEMP']); }
return "/tmp";
}
define('DEFAULT_DEBUG_DIR', gettmpdir()."/");
/** @} */
// include the class autoloader
require_once __DIR__ . '/CAS/Autoload.php';
/**
* The phpCAS class is a simple container for the phpCAS library. It provides CAS
* authentication for web applications written in PHP.
*
* @ingroup public
* @class phpCAS
* @category Authentication
* @package PhpCAS
* @author Pascal Aubry <pascal.aubry@univ-rennes1.fr>
* @author Olivier Berger <olivier.berger@it-sudparis.eu>
* @author Brett Bieber <brett.bieber@gmail.com>
* @author Joachim Fritschi <jfritschi@freenet.de>
* @author Adam Franco <afranco@middlebury.edu>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://wiki.jasig.org/display/CASC/phpCAS
*/
class phpCAS
{
/**
* This variable is used by the interface class phpCAS.
*
* @var CAS_Client
* @hideinitializer
*/
private static $_PHPCAS_CLIENT;
/**
* @var array
* This variable is used to store where the initializer is called from
* (to print a comprehensive error in case of multiple calls).
*
* @hideinitializer
*/
private static $_PHPCAS_INIT_CALL;
/**
* @var array
* This variable is used to store phpCAS debug mode.
*
* @hideinitializer
*/
private static $_PHPCAS_DEBUG;
/**
* This variable is used to enable verbose mode
* This prevents debug info to be show to the user. Since it's a security
* feature the default is false
*
* @hideinitializer
*/
private static $_PHPCAS_VERBOSE = false;
// ########################################################################
// INITIALIZATION
// ########################################################################
/**
* @addtogroup publicInit
* @{
*/
/**
* phpCAS client initializer.
*
* @param string $server_version the version of the CAS server
* @param string $server_hostname the hostname of the CAS server
* @param int $server_port the port the CAS server is running on
* @param string $server_uri the URI the CAS server is responding on
* @param string|string[]|CAS_ServiceBaseUrl_Interface
* $service_base_url the base URL (protocol, host and the
* optional port) of the CAS client; pass
* in an array to use auto discovery with
* an allowlist; pass in
* CAS_ServiceBaseUrl_Interface for custom
* behavior. Added in 1.6.0. Similar to
* serverName config in other CAS clients.
* @param bool $changeSessionID Allow phpCAS to change the session_id
* (Single Sign Out/handleLogoutRequests
* is based on that change)
* @param \SessionHandlerInterface $sessionHandler the session handler
*
* @return void a newly created CAS_Client object
* @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
* called, only once, and before all other methods (except phpCAS::getVersion()
* and phpCAS::setDebug()).
*/
public static function client($server_version, $server_hostname,
$server_port, $server_uri, $service_base_url,
$changeSessionID = true, \SessionHandlerInterface $sessionHandler = null
) {
phpCAS :: traceBegin();
if (is_object(self::$_PHPCAS_CLIENT)) {
phpCAS :: error(self::$_PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . self::$_PHPCAS_INIT_CALL['file'] . ':' . self::$_PHPCAS_INIT_CALL['line'] . ')');
}
// store where the initializer is called from
$dbg = debug_backtrace();
self::$_PHPCAS_INIT_CALL = array (
'done' => true,
'file' => $dbg[0]['file'],
'line' => $dbg[0]['line'],
'method' => __CLASS__ . '::' . __FUNCTION__
);
// initialize the object $_PHPCAS_CLIENT
try {
self::$_PHPCAS_CLIENT = new CAS_Client(
$server_version, false, $server_hostname, $server_port, $server_uri, $service_base_url,
$changeSessionID, $sessionHandler
);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
phpCAS :: traceEnd();
}
/**
* phpCAS proxy initializer.
*
* @param string $server_version the version of the CAS server
* @param string $server_hostname the hostname of the CAS server
* @param string $server_port the port the CAS server is running on
* @param string $server_uri the URI the CAS server is responding on
* @param string|string[]|CAS_ServiceBaseUrl_Interface
* $service_base_url the base URL (protocol, host and the
* optional port) of the CAS client; pass
* in an array to use auto discovery with
* an allowlist; pass in
* CAS_ServiceBaseUrl_Interface for custom
* behavior. Added in 1.6.0. Similar to
* serverName config in other CAS clients.
* @param bool $changeSessionID Allow phpCAS to change the session_id
* (Single Sign Out/handleLogoutRequests
* is based on that change)
* @param \SessionHandlerInterface $sessionHandler the session handler
*
* @return void a newly created CAS_Client object
* @note Only one of the phpCAS::client() and phpCAS::proxy functions should be
* called, only once, and before all other methods (except phpCAS::getVersion()
* and phpCAS::setDebug()).
*/
public static function proxy($server_version, $server_hostname,
$server_port, $server_uri, $service_base_url,
$changeSessionID = true, \SessionHandlerInterface $sessionHandler = null
) {
phpCAS :: traceBegin();
if (is_object(self::$_PHPCAS_CLIENT)) {
phpCAS :: error(self::$_PHPCAS_INIT_CALL['method'] . '() has already been called (at ' . self::$_PHPCAS_INIT_CALL['file'] . ':' . self::$_PHPCAS_INIT_CALL['line'] . ')');
}
// store where the initializer is called from
$dbg = debug_backtrace();
self::$_PHPCAS_INIT_CALL = array (
'done' => true,
'file' => $dbg[0]['file'],
'line' => $dbg[0]['line'],
'method' => __CLASS__ . '::' . __FUNCTION__
);
// initialize the object $_PHPCAS_CLIENT
try {
self::$_PHPCAS_CLIENT = new CAS_Client(
$server_version, true, $server_hostname, $server_port, $server_uri, $service_base_url,
$changeSessionID, $sessionHandler
);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
phpCAS :: traceEnd();
}
/**
* Answer whether or not the client or proxy has been initialized
*
* @return bool
*/
public static function isInitialized ()
{
return (is_object(self::$_PHPCAS_CLIENT));
}
/** @} */
// ########################################################################
// DEBUGGING
// ########################################################################
/**
* @addtogroup publicDebug
* @{
*/
/**
* Set/unset PSR-3 logger
*
* @param LoggerInterface $logger the PSR-3 logger used for logging, or
* null to stop logging.
*
* @return void
*/
public static function setLogger($logger = null)
{
if (empty(self::$_PHPCAS_DEBUG['unique_id'])) {
self::$_PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))), 0, 4);
}
self::$_PHPCAS_DEBUG['logger'] = $logger;
self::$_PHPCAS_DEBUG['indent'] = 0;
phpCAS :: trace('START ('.date("Y-m-d H:i:s").') phpCAS-' . PHPCAS_VERSION . ' ******************');
}
/**
* Set/unset debug mode
*
* @param string $filename the name of the file used for logging, or false
* to stop debugging.
*
* @return void
*
* @deprecated
*/
public static function setDebug($filename = '')
{
trigger_error('phpCAS::setDebug() is deprecated in favor of phpCAS::setLogger().', E_USER_DEPRECATED);
if ($filename != false && gettype($filename) != 'string') {
phpCAS :: error('type mismatched for parameter $dbg (should be false or the name of the log file)');
}
if ($filename === false) {
self::$_PHPCAS_DEBUG['filename'] = false;
} else {
if (empty ($filename)) {
if (preg_match('/^Win.*/', getenv('OS'))) {
if (isset ($_ENV['TMP'])) {
$debugDir = $_ENV['TMP'] . '/';
} else {
$debugDir = '';
}
} else {
$debugDir = DEFAULT_DEBUG_DIR;
}
$filename = $debugDir . 'phpCAS.log';
}
if (empty (self::$_PHPCAS_DEBUG['unique_id'])) {
self::$_PHPCAS_DEBUG['unique_id'] = substr(strtoupper(md5(uniqid(''))), 0, 4);
}
self::$_PHPCAS_DEBUG['filename'] = $filename;
self::$_PHPCAS_DEBUG['indent'] = 0;
phpCAS :: trace('START ('.date("Y-m-d H:i:s").') phpCAS-' . PHPCAS_VERSION . ' ******************');
}
}
/**
* Enable verbose errors messages in the website output
* This is a security relevant since internal status info may leak an may
* help an attacker. Default is therefore false
*
* @param bool $verbose enable verbose output
*
* @return void
*/
public static function setVerbose($verbose)
{
if ($verbose === true) {
self::$_PHPCAS_VERBOSE = true;
} else {
self::$_PHPCAS_VERBOSE = false;
}
}
/**
* Show is verbose mode is on
*
* @return bool verbose
*/
public static function getVerbose()
{
return self::$_PHPCAS_VERBOSE;
}
/**
* Logs a string in debug mode.
*
* @param string $str the string to write
*
* @return void
* @private
*/
public static function log($str)
{
$indent_str = ".";
if (isset(self::$_PHPCAS_DEBUG['logger']) || !empty(self::$_PHPCAS_DEBUG['filename'])) {
for ($i = 0; $i < self::$_PHPCAS_DEBUG['indent']; $i++) {
$indent_str .= '| ';
}
// allow for multiline output with proper identing. Useful for
// dumping cas answers etc.
$str2 = str_replace("\n", "\n" . self::$_PHPCAS_DEBUG['unique_id'] . ' ' . $indent_str, $str);
$str3 = self::$_PHPCAS_DEBUG['unique_id'] . ' ' . $indent_str . $str2;
if (isset(self::$_PHPCAS_DEBUG['logger'])) {
self::$_PHPCAS_DEBUG['logger']->info($str3);
}
if (!empty(self::$_PHPCAS_DEBUG['filename'])) {
// Check if file exists and modify file permissions to be only
// readable by the webserver
if (!file_exists(self::$_PHPCAS_DEBUG['filename'])) {
touch(self::$_PHPCAS_DEBUG['filename']);
// Chmod will fail on windows
@chmod(self::$_PHPCAS_DEBUG['filename'], 0600);
}
error_log($str3 . "\n", 3, self::$_PHPCAS_DEBUG['filename']);
}
}
}
/**
* This method is used by interface methods to print an error and where the
* function was originally called from.
*
* @param string $msg the message to print
*
* @return void
* @private
*/
public static function error($msg)
{
phpCAS :: traceBegin();
$dbg = debug_backtrace();
$function = '?';
$file = '?';
$line = '?';
if (is_array($dbg)) {
for ($i = 1; $i < sizeof($dbg); $i++) {
if (is_array($dbg[$i]) && isset($dbg[$i]['class']) ) {
if ($dbg[$i]['class'] == __CLASS__) {
$function = $dbg[$i]['function'];
$file = $dbg[$i]['file'];
$line = $dbg[$i]['line'];
}
}
}
}
if (self::$_PHPCAS_VERBOSE) {
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";
}
phpCAS :: trace($msg . ' in ' . $file . 'on line ' . $line );
phpCAS :: traceEnd();
throw new CAS_GracefullTerminationException(__CLASS__ . "::" . $function . '(): ' . $msg);
}
/**
* This method is used to log something in debug mode.
*
* @param string $str string to log
*
* @return void
*/
public static function trace($str)
{
$dbg = debug_backtrace();
phpCAS :: log($str . ' [' . basename($dbg[0]['file']) . ':' . $dbg[0]['line'] . ']');
}
/**
* This method is used to indicate the start of the execution of a function
* in debug mode.
*
* @return void
*/
public static function traceBegin()
{
$dbg = debug_backtrace();
$str = '=> ';
if (!empty ($dbg[1]['class'])) {
$str .= $dbg[1]['class'] . '::';
}
$str .= $dbg[1]['function'] . '(';
if (is_array($dbg[1]['args'])) {
foreach ($dbg[1]['args'] as $index => $arg) {
if ($index != 0) {
$str .= ', ';
}
if (is_object($arg)) {
$str .= get_class($arg);
} else {
$str .= str_replace(array("\r\n", "\n", "\r"), "", var_export($arg, true));
}
}
}
if (isset($dbg[1]['file'])) {
$file = basename($dbg[1]['file']);
} else {
$file = 'unknown_file';
}
if (isset($dbg[1]['line'])) {
$line = $dbg[1]['line'];
} else {
$line = 'unknown_line';
}
$str .= ') [' . $file . ':' . $line . ']';
phpCAS :: log($str);
if (!isset(self::$_PHPCAS_DEBUG['indent'])) {
self::$_PHPCAS_DEBUG['indent'] = 0;
} else {
self::$_PHPCAS_DEBUG['indent']++;
}
}
/**
* This method is used to indicate the end of the execution of a function in
* debug mode.
*
* @param mixed $res the result of the function
*
* @return void
*/
public static function traceEnd($res = '')
{
if (empty(self::$_PHPCAS_DEBUG['indent'])) {
self::$_PHPCAS_DEBUG['indent'] = 0;
} else {
self::$_PHPCAS_DEBUG['indent']--;
}
$str = '';
if (is_object($res)) {
$str .= '<= ' . get_class($res);
} else {
$str .= '<= ' . str_replace(array("\r\n", "\n", "\r"), "", var_export($res, true));
}
phpCAS :: log($str);
}
/**
* This method is used to indicate the end of the execution of the program
*
* @return void
*/
public static function traceExit()
{
phpCAS :: log('exit()');
while (self::$_PHPCAS_DEBUG['indent'] > 0) {
phpCAS :: log('-');
self::$_PHPCAS_DEBUG['indent']--;
}
}
/** @} */
// ########################################################################
// INTERNATIONALIZATION
// ########################################################################
/**
* @addtogroup publicLang
* @{
*/
/**
* This method is used to set the language used by phpCAS.
*
* @param string $lang string representing the language.
*
* @return void
*
* @sa PHPCAS_LANG_FRENCH, PHPCAS_LANG_ENGLISH
* @note Can be called only once.
*/
public static function setLang($lang)
{
phpCAS::_validateClientExists();
try {
self::$_PHPCAS_CLIENT->setLang($lang);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
}
/** @} */
// ########################################################################
// VERSION
// ########################################################################
/**
* @addtogroup public
* @{
*/
/**
* This method returns the phpCAS version.
*
* @return string the phpCAS version.
*/
public static function getVersion()
{
return PHPCAS_VERSION;
}
/**
* This method returns supported protocols.
*
* @return array an array of all supported protocols. Use internal protocol name as array key.
*/
public static function getSupportedProtocols()
{
$supportedProtocols = array();
$supportedProtocols[CAS_VERSION_1_0] = 'CAS 1.0';
$supportedProtocols[CAS_VERSION_2_0] = 'CAS 2.0';
$supportedProtocols[CAS_VERSION_3_0] = 'CAS 3.0';
$supportedProtocols[SAML_VERSION_1_1] = 'SAML 1.1';
return $supportedProtocols;
}
/** @} */
// ########################################################################
// HTML OUTPUT
// ########################################################################
/**
* @addtogroup publicOutput
* @{
*/
/**
* This method sets the HTML header used for all outputs.
*
* @param string $header the HTML header.
*
* @return void
*/
public static function setHTMLHeader($header)
{
phpCAS::_validateClientExists();
try {
self::$_PHPCAS_CLIENT->setHTMLHeader($header);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
}
/**
* This method sets the HTML footer used for all outputs.
*
* @param string $footer the HTML footer.
*
* @return void
*/
public static function setHTMLFooter($footer)
{
phpCAS::_validateClientExists();
try {
self::$_PHPCAS_CLIENT->setHTMLFooter($footer);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
}
/** @} */
// ########################################################################
// PGT STORAGE
// ########################################################################
/**
* @addtogroup publicPGTStorage
* @{
*/
/**
* This method can be used to set a custom PGT storage object.
*
* @param CAS_PGTStorage_AbstractStorage $storage a PGT storage object that inherits from the
* CAS_PGTStorage_AbstractStorage class
*
* @return void
*/
public static function setPGTStorage($storage)
{
phpCAS :: traceBegin();
phpCAS::_validateProxyExists();
try {
self::$_PHPCAS_CLIENT->setPGTStorage($storage);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
phpCAS :: traceEnd();
}
/**
* This method is used to tell phpCAS to store the response of the
* CAS server to PGT requests in a database.
*
* @param string $dsn_or_pdo a dsn string to use for creating a PDO
* object or a PDO object
* @param string $username the username to use when connecting to the
* database
* @param string $password the password to use when connecting to the
* database
* @param string $table the table to use for storing and retrieving
* PGT's
* @param string $driver_options any driver options to use when connecting
* to the database
*
* @return void
*/
public static function setPGTStorageDb($dsn_or_pdo, $username='',
$password='', $table='', $driver_options=null
) {
phpCAS :: traceBegin();
phpCAS::_validateProxyExists();
try {
self::$_PHPCAS_CLIENT->setPGTStorageDb($dsn_or_pdo, $username, $password, $table, $driver_options);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
phpCAS :: traceEnd();
}
/**
* This method is used to tell phpCAS to store the response of the
* CAS server to PGT requests onto the filesystem.
*
* @param string $path the path where the PGT's should be stored
*
* @return void
*/
public static function setPGTStorageFile($path = '')
{
phpCAS :: traceBegin();
phpCAS::_validateProxyExists();
try {
self::$_PHPCAS_CLIENT->setPGTStorageFile($path);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
phpCAS :: traceEnd();
}
/** @} */
// ########################################################################
// ACCESS TO EXTERNAL SERVICES
// ########################################################################
/**
* @addtogroup publicServices
* @{
*/
/**
* Answer a proxy-authenticated service handler.
*
* @param string $type The service type. One of
* PHPCAS_PROXIED_SERVICE_HTTP_GET; PHPCAS_PROXIED_SERVICE_HTTP_POST;
* PHPCAS_PROXIED_SERVICE_IMAP
*
* @return CAS_ProxiedService
* @throws InvalidArgumentException If the service type is unknown.
*/
public static function getProxiedService ($type)
{
phpCAS :: traceBegin();
phpCAS::_validateProxyExists();
try {
$res = self::$_PHPCAS_CLIENT->getProxiedService($type);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
phpCAS :: traceEnd();
return $res;
}
/**
* Initialize a proxied-service handler with the proxy-ticket it should use.
*
* @param CAS_ProxiedService $proxiedService Proxied Service Handler
*
* @return void
* @throws CAS_ProxyTicketException If there is a proxy-ticket failure.
* The code of the Exception will be one of:
* PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE
* PHPCAS_SERVICE_PT_FAILURE
*/
public static function initializeProxiedService (CAS_ProxiedService $proxiedService)
{
phpCAS::_validateProxyExists();
try {
self::$_PHPCAS_CLIENT->initializeProxiedService($proxiedService);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
}
/**
* This method is used to access an HTTP[S] service.
*
* @param string $url the service to access.
* @param int &$err_code an error code Possible values are
* PHPCAS_SERVICE_OK (on success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE,
* PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE, PHPCAS_SERVICE_PT_FAILURE,
* PHPCAS_SERVICE_NOT_AVAILABLE.
* @param string &$output the output of the service (also used to give an
* error message on failure).
*
* @return bool true on success, false otherwise (in this later case,
* $err_code gives the reason why it failed and $output contains an error
* message).
*/
public static function serviceWeb($url, & $err_code, & $output)
{
phpCAS :: traceBegin();
phpCAS::_validateProxyExists();
try {
$res = self::$_PHPCAS_CLIENT->serviceWeb($url, $err_code, $output);
} catch (Exception $e) {
phpCAS :: error(get_class($e) . ': ' . $e->getMessage());
}
phpCAS :: traceEnd($res);
return $res;
}
/**
* This method is used to access an IMAP/POP3/NNTP service.
*
* @param string $url a string giving the URL of the service,
* including the mailing box for IMAP URLs, as accepted by imap_open().
* @param string $service a string giving for CAS retrieve Proxy ticket