ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Utilities.php
Go to the documentation of this file.
1<?php
2
3
13{
14
18 public static $logMask = 0;
19
20
24 public static function getSelfHost()
25 {
26 return \SimpleSAML\Utils\HTTP::getSelfHost();
27 }
28
29
33 public static function selfURLhost()
34 {
35 return \SimpleSAML\Utils\HTTP::getSelfURLHost();
36 }
37
38
42 public static function isHTTPS()
43 {
44 return \SimpleSAML\Utils\HTTP::isHTTPS();
45 }
46
47
52 public static function selfURLNoQuery()
53 {
54 return \SimpleSAML\Utils\HTTP::getSelfURLNoQuery();
55 }
56
57
62 public static function getSelfHostWithPath()
63 {
64 return \SimpleSAML\Utils\HTTP::getSelfHostWithPath();
65 }
66
67
72 public static function getFirstPathElement($trailingslash = true)
73 {
74 return \SimpleSAML\Utils\HTTP::getFirstPathElement($trailingslash);
75 }
76
77
81 public static function selfURL()
82 {
83 return \SimpleSAML\Utils\HTTP::getSelfURL();
84 }
85
86
90 public static function getBaseURL()
91 {
93 }
94
95
99 public static function addURLparameter($url, $parameters)
100 {
101 return \SimpleSAML\Utils\HTTP::addURLParameters($url, $parameters);
102 }
103
104
108 public static function checkURLAllowed($url, array $trustedSites = null)
109 {
110 return \SimpleSAML\Utils\HTTP::checkURLAllowed($url, $trustedSites);
111 }
112
113
117 public static function parseStateID($stateId)
118 {
120 }
121
122
126 public static function checkDateConditions($start = null, $end = null)
127 {
128 $currentTime = time();
129
130 if (!empty($start)) {
132 // Allow for a 10 minute difference in Time
133 if (($startTime < 0) || (($startTime - 600) > $currentTime)) {
134 return false;
135 }
136 }
137 if (!empty($end)) {
139 if (($endTime < 0) || ($endTime <= $currentTime)) {
140 return false;
141 }
142 }
143 return true;
144 }
145
146
150 public static function generateID()
151 {
153 }
154
155
160 public static function generateTimestamp($instant = null)
161 {
163 }
164
165
169 public static function parseDuration($duration, $timestamp = null)
170 {
172 }
173
174
178 public static function fatalError($trackId = 'na', $errorCode = null, Exception $e = null)
179 {
180 throw new SimpleSAML_Error_Error($errorCode, $e);
181 }
182
183
187 public static function ipCIDRcheck($cidr, $ip = null)
188 {
189 return SimpleSAML\Utils\Net::ipCIDRcheck($cidr, $ip);
190 }
191
192
193 private static function _doRedirect($url, $parameters = array())
194 {
195 assert(is_string($url));
196 assert(!empty($url));
197 assert(is_array($parameters));
198
199 if (!empty($parameters)) {
200 $url = self::addURLparameter($url, $parameters);
201 }
202
203 /* Set the HTTP result code. This is either 303 See Other or
204 * 302 Found. HTTP 303 See Other is sent if the HTTP version
205 * is HTTP/1.1 and the request type was a POST request.
206 */
207 if ($_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.1' &&
208 $_SERVER['REQUEST_METHOD'] === 'POST'
209 ) {
210 $code = 303;
211 } else {
212 $code = 302;
213 }
214
215 if (strlen($url) > 2048) {
216 SimpleSAML\Logger::warning('Redirecting to a URL longer than 2048 bytes.');
217 }
218
219 // Set the location header
220 header('Location: '.$url, true, $code);
221
222 // Disable caching of this response
223 header('Pragma: no-cache');
224 header('Cache-Control: no-cache, must-revalidate');
225
226 // Show a minimal web page with a clickable link to the URL
227 echo '<?xml version="1.0" encoding="UTF-8"?>'."\n";
228 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'.
229 ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n";
230 echo '<html xmlns="http://www.w3.org/1999/xhtml">';
231 echo '<head>
232 <meta http-equiv="content-type" content="text/html; charset=utf-8">
233 <title>Redirect</title>
234 </head>';
235 echo '<body>';
236 echo '<h1>Redirect</h1>';
237 echo '<p>';
238 echo 'You were redirected to: ';
239 echo '<a id="redirlink" href="'.
240 htmlspecialchars($url).'">'.htmlspecialchars($url).'</a>';
241 echo '<script type="text/javascript">document.getElementById("redirlink").focus();</script>';
242 echo '</p>';
243 echo '</body>';
244 echo '</html>';
245
246 // End script execution
247 exit;
248 }
249
250
255 public static function redirect($url, $parameters = array(), $allowed_redirect_hosts = null)
256 {
257 assert(is_string($url));
258 assert(strlen($url) > 0);
259 assert(is_array($parameters));
260
261 if ($allowed_redirect_hosts !== null) {
262 $url = self::checkURLAllowed($url, $allowed_redirect_hosts);
263 } else {
265 }
266 self::_doRedirect($url, $parameters);
267 }
268
269
274 public static function redirectTrustedURL($url, $parameters = array())
275 {
277 }
278
279
284 public static function redirectUntrustedURL($url, $parameters = array())
285 {
287 }
288
289
293 public static function transposeArray($in)
294 {
296 }
297
298
303 public static function isDOMElementOfType(DOMNode $element, $name, $nsURI)
304 {
305 return SimpleSAML\Utils\XML::isDOMNodeOfType($element, $name, $nsURI);
306 }
307
308
312 public static function getDOMChildren(DOMElement $element, $localName, $namespaceURI)
313 {
314 return SimpleSAML\Utils\XML::getDOMChildren($element, $localName, $namespaceURI);
315 }
316
317
321 public static function getDOMText($element)
322 {
323 return SimpleSAML\Utils\XML::getDOMText($element);
324 }
325
326
331 public static function getAcceptLanguage()
332 {
333 return \SimpleSAML\Utils\HTTP::getAcceptLanguage();
334 }
335
336
340 public static function validateXML($xml, $schema)
341 {
342 $result = \SimpleSAML\Utils\XML::isValid($xml, $schema);
343 return ($result === true) ? '' : $result;
344 }
345
346
350 public static function validateXMLDocument($message, $type)
351 {
353 }
354
355
359 public static function generateRandomBytes($length)
360 {
361 assert(is_int($length));
362
363 return openssl_random_pseudo_bytes($length);
364 }
365
366
370 public static function stringToHex($bytes)
371 {
372 $ret = '';
373 for ($i = 0; $i < strlen($bytes); $i++) {
374 $ret .= sprintf('%02x', ord($bytes[$i]));
375 }
376 return $ret;
377 }
378
379
383 public static function resolvePath($path, $base = null)
384 {
385 return \SimpleSAML\Utils\System::resolvePath($path, $base);
386 }
387
388
392 public static function resolveURL($url, $base = null)
393 {
394 return \SimpleSAML\Utils\HTTP::resolveURL($url, $base);
395 }
396
397
401 public static function normalizeURL($url)
402 {
403 return \SimpleSAML\Utils\HTTP::normalizeURL($url);
404 }
405
406
410 public static function parseQueryString($query_string)
411 {
412 return \SimpleSAML\Utils\HTTP::parseQueryString($query_string);
413 }
414
415
420 public static function parseAttributes($attributes)
421 {
423 }
424
425
429 public static function getSecretSalt()
430 {
432 }
433
434
438 public static function getLastError()
439 {
440
441 if (!function_exists('error_get_last')) {
442 return '[Cannot get error message]';
443 }
444
445 $error = error_get_last();
446 if ($error === null) {
447 return '[No error message found]';
448 }
449
450 return $error['message'];
451 }
452
453
457 public static function resolveCert($path)
458 {
459 return \SimpleSAML\Utils\Config::getCertPath($path);
460 }
461
462
466 public static function loadPublicKey(SimpleSAML_Configuration $metadata, $required = false, $prefix = '')
467 {
468 return SimpleSAML\Utils\Crypto::loadPublicKey($metadata, $required, $prefix);
469 }
470
471
475 public static function loadPrivateKey(SimpleSAML_Configuration $metadata, $required = false, $prefix = '')
476 {
477 return SimpleSAML\Utils\Crypto::loadPrivateKey($metadata, $required, $prefix);
478 }
479
480
484 public static function formatDOMElement(DOMElement $root, $indentBase = '')
485 {
486 SimpleSAML\Utils\XML::formatDOMElement($root, $indentBase);
487 }
488
489
493 public static function formatXMLString($xml, $indentBase = '')
494 {
495 return SimpleSAML\Utils\XML::formatXMLString($xml, $indentBase);
496 }
497
498
502 public static function arrayize($data, $index = 0)
503 {
505 }
506
507
511 public static function isAdmin()
512 {
514 }
515
516
520 public static function getAdminLoginURL($returnTo = null)
521 {
523 }
524
525
529 public static function requireAdmin()
530 {
532 }
533
534
538 public static function postRedirect($destination, $post)
539 {
541 }
542
543
549 {
550 return \SimpleSAML\Utils\HTTP::getPOSTRedirectURL($destination, $post);
551 }
552
553
559 {
560 assert(is_string($destination));
561 assert(is_array($post));
562
564 $postData = array(
565 'post' => $post,
566 'url' => $destination,
567 );
568
570 $session->setData('core_postdatalink', $postId, $postData);
571
572 $redirInfo = base64_encode(SimpleSAML\Utils\Crypto::aesEncrypt($session->getSessionId().':'.$postId));
573
574 $url = SimpleSAML\Module::getModuleURL('core/postredirect.php', array('RedirInfo' => $redirInfo));
575 $url = preg_replace("#^https:#", "http:", $url);
576
577 return $url;
578 }
579
580
584 public static function validateCA($certificate, $caFile)
585 {
587 }
588
589
593 public static function initTimezone()
594 {
596 }
597
598
602 public static function writeFile($filename, $data, $mode = 0600)
603 {
605 }
606
607
611 public static function getTempDir()
612 {
614 }
615
616
620 public static function maskErrors($mask)
621 {
623 }
624
625
629 public static function popErrorMask()
630 {
632 }
633
634
639 public static function getDefaultEndpoint(array $endpoints, array $bindings = null)
640 {
641 return \SimpleSAML\Utils\Config\Metadata::getDefaultEndpoint($endpoints, $bindings);
642 }
643
644
649 public static function checkCookie($retryURL = null)
650 {
652 }
653
654
658 public static function debugMessage($message, $type)
659 {
661 }
662
663
667 public static function fetch($path, $context = array(), $getHeaders = false)
668 {
669 return \SimpleSAML\Utils\HTTP::fetch($path, $context, $getHeaders);
670 }
671
672
676 public static function aesEncrypt($clear)
677 {
679 }
680
681
685 public static function aesDecrypt($encData)
686 {
688 }
689
690
694 public static function isWindowsOS()
695 {
697 }
698
699
703 public static function setCookie($name, $value, array $params = null, $throw = true)
704 {
706 }
707}
$result
if(php_sapi_name() !='cli') $in
Definition: Utf8Test.php:37
$metadata['__DYNAMIC:1__']
$path
Definition: aliased.php:25
if(!isset($_REQUEST['ReturnTo'])) $returnTo
Definition: authpage.php:16
exit
Definition: backend.php:16
$filename
Definition: buildRTE.php:89
foreach($mandatory_scripts as $file) $timestamp
Definition: buildRTE.php:81
An exception for terminatinating execution or to throw for unit testing.
static xsDateTimeToTimestamp($time)
This function converts a SAML2 timestamp on the form yyyy-mm-ddThh:mm:ss(.s+)?Z to a UNIX timestamp.
Definition: Utils.php:721
static warning($string)
Definition: Logger.php:177
static popErrorMask()
Pop an error mask.
Definition: Logger.php:322
static maskErrors($mask)
Disable error reporting for the given log levels.
Definition: Logger.php:304
static getModuleURL($resource, array $parameters=array())
Get absolute URL to a specified module resource.
Definition: Module.php:220
static arrayize($data, $index=0)
Put a non-array variable into an array.
Definition: Arrays.php:24
static transpose($array)
This function transposes a two-dimensional array, so that $a['k1']['k2'] becomes $a['k2']['k1'].
Definition: Arrays.php:39
static normalizeAttributesArray($attributes)
Validate and normalize an array with attributes.
Definition: Attributes.php:79
static isAdmin()
Check whether the current user is admin.
Definition: Auth.php:42
static getAdminLoginURL($returnTo=null)
Retrieve a admin login URL.
Definition: Auth.php:22
static requireAdmin()
Require admin access to the current page.
Definition: Auth.php:60
static getSecretSalt()
Retrieve the secret salt.
Definition: Config.php:49
static loadPublicKey(\SimpleSAML_Configuration $metadata, $required=false, $prefix='')
Get public key or certificate from metadata.
Definition: Crypto.php:265
static aesEncrypt($data)
Encrypt data using AES-256-CBC and the system-wide secret salt as key.
Definition: Crypto.php:146
static loadPrivateKey(\SimpleSAML_Configuration $metadata, $required=false, $prefix='', $full_path=false)
Load a private key from metadata.
Definition: Crypto.php:195
static aesDecrypt($ciphertext)
Decrypt data using AES-256-CBC and the system-wide secret salt as key.
Definition: Crypto.php:81
static submitPOSTData($destination, $data)
Submit a POST form to a specific destination.
Definition: HTTP.php:1202
static checkSessionCookie($retryURL=null)
Check for session cookie, and show missing-cookie page if it is missing.
Definition: HTTP.php:286
static redirectUntrustedURL($url, $parameters=array())
This function redirects to the specified URL after performing the appropriate security checks on it.
Definition: HTTP.php:991
static setCookie($name, $value, $params=null, $throw=true)
Set a cookie.
Definition: HTTP.php:1104
static redirectTrustedURL($url, $parameters=array())
This function redirects to the specified URL without performing any security checks.
Definition: HTTP.php:959
static ipCIDRcheck($cidr, $ip=null)
Check whether an IP address is part of a CIDR.
Definition: Net.php:26
static generateID()
Generate a random identifier, ID_LENGTH bytes long.
Definition: Random.php:26
static writeFile($filename, $data, $mode=0600)
Atomically write a file.
Definition: System.php:183
static getOS()
This function returns the Operating System we are running on.
Definition: System.php:29
static getTempDir()
This function retrieves the path to a directory where temporary files can be saved.
Definition: System.php:70
static generateTimestamp($instant=null)
This function generates a timestamp on the form used by the SAML protocols.
Definition: Time.php:31
static initTimezone()
Initialize the timezone.
Definition: Time.php:51
static parseDuration($duration, $timestamp=null)
Interpret a ISO8601 duration value relative to a given timestamp.
Definition: Time.php:91
static isDOMNodeOfType(\DOMNode $element, $name, $nsURI)
This function checks if the DOMElement has the correct localName and namespaceURI.
Definition: XML.php:357
static checkSAMLMessage($message, $type)
This function performs some sanity checks on XML documents, and optionally validates them against the...
Definition: XML.php:35
static debugSAMLMessage($message, $type)
Helper function to log SAML messages that we send or receive.
Definition: XML.php:94
static formatXMLString($xml, $indentBase='')
Format an XML string.
Definition: XML.php:246
static validateCertificate($certificate, $caFile)
Validate the certificate used to sign the XML against a CA file.
Definition: Validator.php:419
static parseStateID($stateId)
Get the ID and (optionally) a URL embedded in a StateID, in the form 'id:url'.
Definition: State.php:410
static getSessionFromRequest()
Retrieves the current session.
Definition: Session.php:241
static setCookie($name, $value, array $params=null, $throw=true)
Definition: Utilities.php:703
static fetch($path, $context=array(), $getHeaders=false)
Definition: Utilities.php:667
static parseDuration($duration, $timestamp=null)
Definition: Utilities.php:169
static redirectUntrustedURL($url, $parameters=array())
Definition: Utilities.php:284
static aesEncrypt($clear)
Definition: Utilities.php:676
static stringToHex($bytes)
Definition: Utilities.php:370
static maskErrors($mask)
Definition: Utilities.php:620
static addURLparameter($url, $parameters)
Definition: Utilities.php:99
static checkCookie($retryURL=null)
Definition: Utilities.php:649
static generateRandomBytes($length)
Definition: Utilities.php:359
static getSecretSalt()
Definition: Utilities.php:429
static postRedirect($destination, $post)
Definition: Utilities.php:538
static resolvePath($path, $base=null)
Definition: Utilities.php:383
static checkURLAllowed($url, array $trustedSites=null)
Definition: Utilities.php:108
static loadPrivateKey(SimpleSAML_Configuration $metadata, $required=false, $prefix='')
Definition: Utilities.php:475
static getDOMText($element)
Definition: Utilities.php:321
static isDOMElementOfType(DOMNode $element, $name, $nsURI)
Definition: Utilities.php:303
static selfURLhost()
Definition: Utilities.php:33
static getDefaultEndpoint(array $endpoints, array $bindings=null)
Definition: Utilities.php:639
static ipCIDRcheck($cidr, $ip=null)
Definition: Utilities.php:187
static fatalError($trackId='na', $errorCode=null, Exception $e=null)
Definition: Utilities.php:178
static arrayize($data, $index=0)
Definition: Utilities.php:502
static getFirstPathElement($trailingslash=true)
Definition: Utilities.php:72
static getSelfHost()
Definition: Utilities.php:24
static parseQueryString($query_string)
Definition: Utilities.php:410
static parseAttributes($attributes)
Definition: Utilities.php:420
static validateXMLDocument($message, $type)
Definition: Utilities.php:350
static redirectTrustedURL($url, $parameters=array())
Definition: Utilities.php:274
static generateTimestamp($instant=null)
Definition: Utilities.php:160
static formatDOMElement(DOMElement $root, $indentBase='')
Definition: Utilities.php:484
static resolveURL($url, $base=null)
Definition: Utilities.php:392
static getAdminLoginURL($returnTo=null)
Definition: Utilities.php:520
static debugMessage($message, $type)
Definition: Utilities.php:658
static loadPublicKey(SimpleSAML_Configuration $metadata, $required=false, $prefix='')
Definition: Utilities.php:466
static redirect($url, $parameters=array(), $allowed_redirect_hosts=null)
Definition: Utilities.php:255
static resolveCert($path)
Definition: Utilities.php:457
static transposeArray($in)
Definition: Utilities.php:293
static writeFile($filename, $data, $mode=0600)
Definition: Utilities.php:602
static _doRedirect($url, $parameters=array())
Definition: Utilities.php:193
static getAcceptLanguage()
Definition: Utilities.php:331
static validateCA($certificate, $caFile)
Definition: Utilities.php:584
static checkDateConditions($start=null, $end=null)
Definition: Utilities.php:126
static validateXML($xml, $schema)
Definition: Utilities.php:340
static getSelfHostWithPath()
Definition: Utilities.php:62
static formatXMLString($xml, $indentBase='')
Definition: Utilities.php:493
static aesDecrypt($encData)
Definition: Utilities.php:685
static createHttpPostRedirectLink($destination, $post)
Definition: Utilities.php:558
static getDOMChildren(DOMElement $element, $localName, $namespaceURI)
Definition: Utilities.php:312
static createPostRedirectLink($destination, $post)
Definition: Utilities.php:548
static parseStateID($stateId)
Definition: Utilities.php:117
static selfURLNoQuery()
Definition: Utilities.php:52
static normalizeURL($url)
Definition: Utilities.php:401
$i
Definition: disco.tpl.php:19
$mask
Definition: example_042.php:90
$code
Definition: example_050.php:99
if(@file_exists(dirname(__FILE__).'/lang/eng.php')) $certificate
Definition: example_052.php:77
if(array_key_exists('yes', $_REQUEST)) $attributes
Definition: getconsent.php:85
$base
Definition: index.php:4
$index
Definition: metadata.php:60
$stateId
Definition: saml2-acs.php:76
catch(Exception $e) $message
$destination
$bindings
Attribute-related utility methods.
$ret
Definition: parser.php:6
$post
Definition: post.php:34
if($session===NULL) $postData
$type
$session
$url
$root
Definition: sabredav.php:45
getBaseURL($t, $type='get', $key=null, $value=null)
Definition: showstats.php:145
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$start
Definition: bench.php:8
$data
Definition: bench.php:6
$context
Definition: webdav.php:25
$errorCode