~ubuntu-branches/ubuntu/natty/moodle/natty

« back to all changes in this revision

Viewing changes to auth/cas/CAS/CAS.php

  • Committer: Bazaar Package Importer
  • Author(s): Tomasz Muras
  • Date: 2010-10-30 12:19:28 UTC
  • mfrom: (1.1.12 upstream) (3.1.10 squeeze)
  • Revision ID: james.westby@ubuntu.com-20101030121928-qzobi6mctpnk4dif
Tags: 1.9.9.dfsg2-2
* Added Romanian translation
* Updated Japanese translation (closes: #596820)
* Backporting security fixes from Moodle 1.9.10 (closes: #601384)
   - Updated embedded CAS to 1.1.3
   - Added patch for MDL-24523:
     clean_text() not filtering text in markdown format
   - Added patch for MDL-24810 and upgraded customized HTML Purifier to 4.2.0 
   - Added patch for MDL-24258:
     students can delete their forum posts later than $CFG->maxeditingtime 
     under certain conditions
   - Added patch for MDL-23377:
     Can't delete quiz attempts in course without enrolled students

Show diffs side-by-side

added added

removed removed

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