-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathutilities.php
624 lines (542 loc) · 18.1 KB
/
utilities.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
<?php
/**
* Misc. utility functions.
*
* @author Ryan Gentner
* @author Jeffrey T. Palmer <jtpalmer@buffalo.edu>
*/
namespace xd_utilities;
use Exception;
use UnexpectedValueException;
/**
* Global INI data.
*
* @var array|null
*/
$iniData = null;
/**
* Parse the configuration file information for the requested section
* and option. Note that the configuration information is cached the
* first time that this function is called unless the $cache is set to
* false.
*
* @param $section Desired configuration section
* @param $option Desired option within the section
* @param $useCachedOptions Cache the parsed options file after the
* first call to this function. Set to true by default. Setting
* this to false will cause the file to be parsed again.
*
* @throws Exception if the section or option is not provided
* @throws Exception if the configuration file could not be parsed
* @throws Exception if the requested section is not found in the file
* @throws Exception if the requested option is not found in the section
*
* @return mixed The requested configuration option.
*/
function getConfiguration($section, $option, $useCachedOptions = true)
{
$sectionData = getConfigurationSection($section, $useCachedOptions);
if (empty($option)) {
$msg = "Configuration option not specified";
throw new Exception($msg);
}
if (!array_key_exists($option, $sectionData)) {
$msg = "Option '$option' does not exist in section '$section'";
throw new Exception($msg);
}
return $sectionData[$option];
}
/**
* Gets a base of a URL from the configuration.
*
* This function guarantees that the returned result ends with a slash.
*
* @see getConfiguration
*
* @param $section Desired configuration section
* @param $option Desired option within the section
* @param $useCachedOptions Cache the parsed options file after the
* first call to this function. Set to true by default. Setting
* this to false will cause the file to be parsed again.
*
* @throws Exception if the section or option is not provided
* @throws Exception if the configuration file could not be parsed
* @throws Exception if the requested section is not found in the file
* @throws Exception if the requested option is not found in the section
*
* @return string The requested configuration option.
*/
function getConfigurationUrlBase($section, $option, $useCachedOptions = true)
{
$configValue = getConfiguration($section, $option, $useCachedOptions);
return ensure_string_ends_with($configValue, '/');
}
/**
* Same as getConfiguration however it returns the whole section as
* an associative array.
*
* @param $section Desired configuration section
* @param $option Desired option within the section
* @param $useCachedOptions Cache the parsed options file after the
* first call to this function. Set to true by default. Setting
* this to false will cause the file to be parsed again.
*
* @throws Exception if the section or option is not provided
* @throws Exception if the configuration file could not be parsed
* @throws Exception if the requested section is not found in the file
*
* @return array The requested configuration section.
*/
function getConfigurationSection($section, $useCachedOptions = true)
{
global $iniData;
if (empty($section)) {
$msg = "Configuration section not specified";
throw new Exception($msg);
}
if (null === $iniData || !$useCachedOptions) {
$iniData = loadConfiguration();
}
// Verifying that the section exist.
if (!array_key_exists($section, $iniData)) {
$msg = "Undefined configuration section: '$section'";
throw new Exception($msg);
}
return $iniData[$section];
}
/**
* Load the configuration data.
*
* @return array
*/
function loadConfiguration()
{
$iniFile = CONFIG_PORTAL_SETTINGS;
$iniDir = preg_replace('/\\.ini$/', '.d', $iniFile);
if (!is_readable($iniFile)) {
$msg = "Could not read settings file: " . $iniFile;
throw new Exception($msg);
}
// Parse the main config file.
$iniData = parse_ini_file($iniFile, true);
if ($iniData === false) {
$msg = "Failed to parse settings file: " . $iniFile;
throw new Exception($msg);
}
// Merge partial config files.
$filePaths = glob("$iniDir/*.ini");
sort($filePaths);
foreach ($filePaths as $partialFile) {
if (!is_readable($partialFile)) {
$msg = "Could not read settings file: " . $partialFile;
throw new Exception($msg);
}
$partialData = parse_ini_file($partialFile, true);
if ($partialData === false) {
$msg = "Failed to parse settings file: " . $partialFile;
throw new Exception($msg);
}
foreach ($partialData as $sectionName => $sectionData) {
foreach ($sectionData as $key => $value) {
$iniData[$sectionName][$key] = $value;
}
}
}
return $iniData;
}
/**
* Clear cached configuration data.
*/
function clearConfigurationCache()
{
global $iniData;
$iniData = null;
}
/**
* Quote an entity.
*/
function quote($entity)
{
return "'$entity'";
}
/**
* Remove an element from an array.
*/
function remove_element_by_value(&$array, $value)
{
$index = array_search($value, $array);
if (!is_bool($index)) {
unset($array[$index]);
}
}
/**
* Check if a string begins with another string.
*
* @param string $string The string to check.
* @param string $search The string that may or may not be at the
* beginning of the other string.
*
* @return bool
*/
function string_begins_with($string, $search)
{
return (strncmp($string, $search, strlen($search)) == 0);
}
/**
* Check if a string ends with another string.
*
* Based on: http://stackoverflow.com/a/834355
*
* @param string $string The string to check.
* @param string $search The string that may or may not be at the
* end of the other string.
* @return bool True if $search is at the end of $string.
* Otherwise, false.
*/
function string_ends_with($string, $search)
{
$searchLength = strlen($search);
if ($searchLength === 0) {
return true;
}
return substr($string, -$searchLength) === $search;
}
/**
* Ensure a string ends with another string.
*
* If the given string ends with the given ending, it will be returned as is.
* Otherwise, the given string will be returned with the given ending
* appended on.
*
* @param string $string The string to check and possibly augment.
* @param string $ending The ending string to check for.
* @return string A string that ends with the given ending.
*/
function ensure_string_ends_with($string, $ending)
{
if (!string_ends_with($string, $ending)) {
$string .= $ending;
}
return $string;
}
/**
* Delete an element of an array.
*/
function array_delete_by_key(&$array, $delete_key, $use_old_keys = false)
{
unset($array[$delete_key]);
if (!$use_old_keys) {
$array = array_values($array);
}
return true;
}
/**
* Remove a key in an array and return its value or a default if not present.
*
* @param array $a The array to remove the key from.
* @param mixed $key The key to remove from the array.
* @param mixed $default (Optional) The default to return if the key is
* not present. (Defaults to null.)
* @return mixed The value for the key or the given default if the
* key was not present.
*/
function array_extract(array &$a, $key, $default = null) {
$value = array_get($a, $key, $default);
unset($a[$key]);
return $value;
}
/**
* Look up a key in an array and return its value or a default if not present.
*
* @param array $a The array to look up the key in.
* @param mixed $key The key to look up in the array.
* @param mixed $default (Optional) The default to return if the key is
* not present. (Defaults to null.)
* @return mixed The value for the key or the given default if the
* key was not present.
*/
function array_get(array $a, $key, $default = null) {
if (!array_key_exists($key, $a)) {
return $default;
}
return $a[$key];
}
/**
* Replace a key's value in an array and return its old value or a default if not present.
*
* @param array $a The array in which the key's value will be replaced.
* @param mixed $key The key for the value being replaced.
* @param mixed $newValue The new value to insert into the array.
* @param mixed $default (Optional) The default to return if the key was
* not present. (Defaults to null.)
* @return mixed The old value for the key or the given default if the
* key was not present.
*/
function array_replace_key_value(array &$a, $key, $newValue, $default = null) {
$oldValue = array_get($a, $key, $default);
$a[$key] = $newValue;
return $oldValue;
}
/**
* Locates a value for a parameter ($param) in a string ($haystack) with
* the format /param1=value/param2=value/.…
* or param1=value¶m2=value&…
*
* If no match is found, an empty string is returned
*/
function getParameterIn($param, $haystack)
{
$num_matches = preg_match("/$param=(.+)/", $haystack, $matches);
$param_value = '';
if ($num_matches > 0) {
$frags = explode('&', str_replace('/', '&', $matches[1]));
$param_value = $frags[0];
}
return $param_value;
}
/**
* Create an XML error message
*
* @param $dom Document object model that the error will be inserted into
* @param $nodeRoot Root of the error node
* @param $code Error code
* @param $message Error message
*
* @returns true if successful
*/
function generateError($dom, $nodeRoot, $code, $message)
{
\xd_domdocument\createElement($dom, $nodeRoot, "code", $code);
\xd_domdocument\createElement($dom, $nodeRoot, "reason", $message);
return true;
}
/**
* Print a message, then "delete" it.
*/
function printAndDelete($message)
{
$message_length = strlen($message);
print $message;
print str_repeat(chr(8), $message_length);
return $message_length;
}
/**
* Check for a center logo.
*
* @param bool $apply_css If true output CSS for the logo.
*
* @return bool
*/
function checkForCenterLogo($apply_css = true)
{
$use_center_logo = false;
try {
$logo = getConfiguration('general', 'center_logo');
$logo_width = getConfiguration('general', 'center_logo_width');
$logo_width = intval($logo_width);
if (strlen($logo) > 0 && $logo[0] !== '/') {
$logo = __DIR__ . '/' . $logo;
}
if (file_exists($logo)) {
$use_center_logo = true;
$img_data = base64_encode(file_get_contents($logo));
}
} catch(\Exception $e) {
}
if ($use_center_logo == true && $apply_css == true) {
print <<<EOF
<style type="text/css">
.custom_center_logo {
height: 25px;
width: {$logo_width}px;
background: url(data:image/png;base64,$img_data) right no-repeat;
}
</style>
EOF;
}
return $use_center_logo;
}
/**
* A temporary shim function to use while our supported PHP version is < 5.4.8 because 5.3
* incorrectly returns NULL in the following case:
*
* filter_var(false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)
*
* See
* https://bugs.php.net/bug.php?id=49510 and
* http://php.net/manual/en/function.filter-var.php and
* http://stackoverflow.com/questions/9132274/php-validation-booleans-using-filter-var
*
* @param mixed $value to be filtered
* @param int $filter the type of filter to apply
* @param mixed $options the options to be supplied to the filter
* @return bool|mixed false if the value is logically false, else the results of
* \filter_var($value, $filter, $options)
*/
function filter_var($value, $filter = FILTER_DEFAULT, $options = null)
{
return ( FILTER_VALIDATE_BOOLEAN == $filter && false === $value
? false
: \filter_var($value, $filter, $options) );
}
/**
* If the specified path is not already fully qualified (e.g., /var/tmp) then prepend the
* specified base path to the path.
*
* @param $path A string containing a path
* @param $base_bath A string containing the base path to be prepended to relative paths
*
* @return A fully qualified path, with the base path prepended to a relative path
*/
function qualify_path($path, $base_path)
{
if ( 0 !== strpos($path, DIRECTORY_SEPARATOR) && null !== $base_path && "" != $base_path ) {
$path = $base_path . DIRECTORY_SEPARATOR . $path;
}
return $path;
}
/**
* Resolve instances of current (.) and parent (..) directory references as well as "//"
* to a fully qualified path without these references. For example,
* /var/www/share/tools/etl/../../../etc/etl.json resolves to /var/www/etc/etl/etl. Only
* fully qualified paths are resolved as relative paths may not be able to be fully
* resolved (e.g., ../../../etc/etl.json cannot properly be resolved on it's own). This is
* useful for making logs more human readable.
*
* PHP provides realpath() but this returns FALSE if the file does not yet exist which may
* cause issues in a dynamic environment.
*/
function resolve_path($path)
{
// If we don't limit to filly qualified paths then relative paths such as "../../foo"
// are not properly resolved.
if ( 0 !== strpos($path, DIRECTORY_SEPARATOR) ) {
return $path;
}
$parts = explode(DIRECTORY_SEPARATOR, str_replace('//', '/', $path));
$resolved = array();
foreach ($parts as $part) {
if ( '.' == $part ) {
continue;
}
if ( '..' == $part ) {
array_pop($resolved);
continue;
}
$resolved[] = $part;
}
return implode(DIRECTORY_SEPARATOR, $resolved);
} // resolve_path()
/**
* Verify that an object contains all of the properties specified in the $propertyList
*
* @param stdClass $obj The object to examine
* @param array $propertyList The list of required properties
* @param array $missing Optional reference to an array that will contain a list of the
* missing properties.
*
* @return TRUE if the object contains all of the required properties, FALSE otherwise.
*/
function verify_required_object_properties($obj, array $propertyList, array &$missing = null)
{
if ( ! is_object($obj) ) {
throw new Exception(sprintf("First argument must be an object, %s given", gettype($obj)));
}
$missing = array();
foreach ( $propertyList as $p ) {
if ( ! isset($obj->$p) ) {
$missing[] = $p;
}
}
return 0 == count($missing);
} // verify_required_object_properties()
/**
* Verify the types of object properties, optionally skipping properties that are not
* present in the object. Property types must match the PHP is_*() methods (e.g.,
* is_int(), is_object(), is_string()) and will generate a warning message a function
* corresponding to the specified type does not exist.
*
* @param stdClass $obj The object to examine
* @param array $typeList An associative array where the keys are property names and
* the values are property types.
* @param array $messages Optional reference to an array that will contain a list of
* messages regarding the property types.
* @param boolean $skipMissingProperties If set to FALSE, properties that are not present in
* the object generate an error. If set to TRUE missing properties are silently skipped,
*
* @return TRUE if all properties were present and their type checks passed, FALSE
* otherwise.
*/
function verify_object_property_types(
$obj,
array $propertyList,
array &$messages = null,
$skipMissingProperties = false
) {
if ( ! is_object($obj) ) {
throw new Exception(sprintf("First argument must be an object, %s given", gettype($obj)));
}
$messages = array();
foreach ( $propertyList as $p => $type ) {
if ( ! isset($obj->$p) ) {
if ( ! $skipMissingProperties ) {
$messages[] = sprintf("missing property '%s'", $p);
}
continue;
}
$func = 'is_' . $type;
if ( ! function_exists($func) ) {
$messages[] = sprintf("Unsupported type %s given for property '%s'", $type, $p);
} elseif ( ! $func($obj->$p) ) {
$messages[] = sprintf("'%s' must be a %s, %s given", $p, $type, gettype($obj->$p));
}
}
return ( 0 == count($messages) );
} // verify_object_property_types()
/**
* If CAPTCHA settings are correct, validate a captcha
*/
function verify_captcha(){
$captchaSiteKey = '';
$captchaSecret = '';
try {
$captchaSiteKey = getConfiguration('mailer', 'captcha_public_key');
$captchaSecret = getConfiguration('mailer', 'captcha_private_key');
}
catch(exception $e){
}
if ('' !== $captchaSiteKey && '' !== $captchaSecret && !isset($_SESSION['xdUser'])) {
if (!isset($_POST['g-recaptcha-response'])){
\xd_response\presentError('Recaptcha information not specified');
}
$recaptcha = new \ReCaptcha\ReCaptcha($captchaSecret);
$resp = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER["REMOTE_ADDR"]);
if (!$resp->isSuccess()) {
$errors = $resp->getErrorCodes();
\xd_response\presentError('You must enter the words in the Recaptcha box properly.' . print_r($errors, 1));
}
}
}
/**
* Create a temporary directory.
*
* PHP does not have the equivalent of "mktemp -d".
*
* @param $prefix string The prefix of the generated directory.
*
* @return string The path to the temporary directory.
*/
function createTemporaryDirectory($prefix = 'xdmod-tmp-dir-')
{
$tmpDir = tempnam(sys_get_temp_dir(), $prefix);
if ($tmpDir === false) {
throw new UnexpectedValueException("Failed to create temporary file");
}
if (!unlink($tmpDir)) {
throw new UnexpectedValueException("Failed to remove file '$tmpDir'");
}
if (!mkdir($tmpDir, 0700)) {
throw new UnexpectedValueException("Failed to create directory '$tmpDir'");
}
return $tmpDir;
}