forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.php
1453 lines (1295 loc) · 32.6 KB
/
options.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Options logic for vip-go-ci.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Read settings from a options file in the
* repository, but only allow certain options
* to be configured.
*
* @param array $options Options array for the program.
* @param string $repo_options_file_name Name of options file name.
* @param array $options_overwritable Array of options that can be overwritten.
*
* @return bool False when options could not be read or set,
* true when read and option(s) potentially changed.
*/
function vipgoci_options_read_repo_file(
array &$options,
string $repo_options_file_name,
array $options_overwritable
) :bool {
$options['repo-options-set'] = array();
if ( false === $options['repo-options'] ) {
vipgoci_log(
'Skipping possibly overwriting options ' .
'using data from repository settings file ' .
'as this is disabled via command-line options',
array(
'repo-options' => $options['repo-options'],
)
);
return false;
}
vipgoci_log(
'Reading options from repository, overwriting ' .
'already set option values if applicable',
array(
'repo_owner' => $options['repo-owner'],
'repo_name' => $options['repo-name'],
'commit' => $options['commit'],
'filename' => $repo_options_file_name,
'options_overwritable' => $options_overwritable,
'repo_options_allowed' => $options['repo-options-allowed'],
)
);
/*
* Try to read options-file from
* repository, bail out of that fails.
*/
$repo_options_file_contents = vipgoci_gitrepo_fetch_committed_file(
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$options['commit'],
$repo_options_file_name,
$options['local-git-repo']
);
if ( false === $repo_options_file_contents ) {
vipgoci_log(
'No options found in repository settings-file, nothing further to do',
array(
'filename' => $repo_options_file_name,
)
);
return false;
}
$repo_options_arr = json_decode(
$repo_options_file_contents,
true
);
if ( null === $repo_options_arr ) {
vipgoci_log(
'Options not parsable, nothing further to do',
array(
'filename'
=> $repo_options_file_name,
'repo_options_arr'
=> $repo_options_arr,
'repo_options_file_contents'
=> $repo_options_file_contents,
'repo_options_allowed'
=> $options['repo-options-allowed'],
)
);
return false;
}
/*
* Actually set/overwrite values. Keep account of what we set
* and to what value, log it at the end.
*/
$options_read = array();
foreach (
$options_overwritable as
$option_overwritable_name => $option_overwritable_conf
) {
/*
* Detect possible issues with
* the arguments given, or value not defined
* in the options-file.
*/
if (
( ! isset(
$repo_options_arr[ $option_overwritable_name ]
) )
||
( ! isset(
$option_overwritable_conf['type']
) )
) {
continue;
}
/*
* We require the 'valid_values' parameter to
* be in place for all options, except ones
* dealing with arrays.
*/
if (
( 'array' !== $option_overwritable_conf['type'] ) &&
( ! isset( $option_overwritable_conf['valid_values'] ) )
) {
continue;
}
/*
* If not specified, we do not append
* by default.
*/
if ( ! isset(
$option_overwritable_conf['append']
) ) {
$option_overwritable_conf['append'] = false;
}
/*
* Limit which options are configurable via repository
* options file. Skip the current option if not found
* the list of allowed options.
*/
if ( ! in_array(
$option_overwritable_name,
$options['repo-options-allowed'],
true
) ) {
vipgoci_log(
'Found option to be configured that cannot ' .
'be configured via repository ' .
'options file, skipping',
array(
'option_overwritable_name'
=> $option_overwritable_name,
'option_overwritable_conf'
=> $option_overwritable_conf,
'repo_options_arr[' . $option_overwritable_name . ']'
=> $repo_options_arr[ $option_overwritable_name ],
'repo_options_allowed'
=> $options['repo-options-allowed'],
)
);
continue;
}
$do_skip = false;
if ( 'integer' === $option_overwritable_conf['type'] ) {
if ( ! is_numeric(
$repo_options_arr[ $option_overwritable_name ]
) ) {
$do_skip = true;
} elseif ( ! in_array(
$repo_options_arr[ $option_overwritable_name ],
$option_overwritable_conf['valid_values'],
true
) ) {
$do_skip = true;
}
} elseif ( 'boolean' === $option_overwritable_conf['type'] ) {
if ( ! is_bool(
$repo_options_arr[ $option_overwritable_name ]
) ) {
$do_skip = true;
} elseif ( ! in_array(
$repo_options_arr[ $option_overwritable_name ],
$option_overwritable_conf['valid_values'],
true
) ) {
$do_skip = true;
}
} elseif ( 'array' === $option_overwritable_conf['type'] ) {
if ( ! is_array(
$repo_options_arr[ $option_overwritable_name ]
) ) {
$do_skip = true;
}
} else {
$do_skip = true;
}
if ( true === $do_skip ) {
vipgoci_log(
'Found invalid value for option in option-file, or invalid arguments passed internally',
array(
'option_overwritable_name'
=> $option_overwritable_name,
'option_overwritable_conf'
=> $option_overwritable_conf,
'repo_options_arr[' . $option_overwritable_name . ']'
=> $repo_options_arr[ $option_overwritable_name ],
)
);
continue;
}
if (
( 'array' === $option_overwritable_conf['type'] ) &&
( true === $option_overwritable_conf['append'] )
) {
$options[ $option_overwritable_name ] = array_merge(
$options[ $option_overwritable_name ],
$repo_options_arr[ $option_overwritable_name ],
);
} else {
$options[ $option_overwritable_name ] =
$repo_options_arr[ $option_overwritable_name ];
}
$options_read[ $option_overwritable_name ] =
$repo_options_arr[ $option_overwritable_name ];
}
vipgoci_log(
'Set, overwrote or appended the following options',
$options_read
);
$options['repo-options-set'] = $options_read;
return true;
}
/**
* Read from repository files which folders are to
* be skipped from PHPCS scanning, PHP Linting and
* WPScan API scanning, if configured to do so, and
* join with any folders specified on the command line.
*
* @param array $options Options array for the program.
*
* @return void
*/
function vipgoci_options_read_repo_skip_files(
array &$options
) :void {
foreach (
array( 'phpcs', 'lint', 'wpscan-api' ) as $scan_type
) {
/*
* If not configured to read
* from options files, skip.
*/
if ( true !== $options[ $scan_type . '-skip-folders-in-repo-options-file' ] ) {
vipgoci_log(
'Not reading from repository files which ' .
$scan_type . ' folders can be skipped, as not ' .
'configured to do so',
array(
$scan_type . '_skip_folders_in_repo_options_file' =>
$options[ $scan_type . '-skip-folders-in-repo-options-file' ],
)
);
continue;
}
vipgoci_log(
'Reading from repository files which folders can ' .
'be skipped from ' . $scan_type . ' scanning',
array(
$scan_type . '_skip_folders_in_repo_options_file' =>
$options[ $scan_type . '-skip-folders-in-repo-options-file' ],
)
);
$scan_type_file_str = str_replace(
'-',
'_',
$scan_type
);
$type_options_file_name = '.vipgoci_' . $scan_type_file_str . '_skip_folders';
$type_options_file_contents =
vipgoci_gitrepo_fetch_committed_file(
$options['repo-owner'],
$options['repo-name'],
$options['token'],
$options['commit'],
$type_options_file_name,
$options['local-git-repo']
);
if ( empty(
$type_options_file_contents
) ) {
vipgoci_log(
'No folders skippable found in repository for ' . $scan_type . ', so skipping',
array(
'type_options_file_name' => $type_options_file_name,
)
);
continue;
}
/*
* Options files can use
* new-lines as separators
* between items. Here we
* emulate the behaviour
* found on the command-line,
* which is to use commas.
*/
$type_options_file_contents = str_replace(
"\n",
',',
$type_options_file_contents
);
/*
* Create temporary options
* to manage folders to be
* skipped, set with value
* read from repository options
* file.
*/
$tmp_options = array(
'tmp-skip-folders' => $type_options_file_contents,
);
/*
* Use standard function to
* handle what we read.
*/
vipgoci_option_skip_folder_handle(
$tmp_options,
'tmp-skip-folders'
);
/*
* Remove any possible empty
* lines, etc.
*/
$tmp_options['tmp-skip-folders'] = array_filter(
$tmp_options['tmp-skip-folders'],
'strlen'
);
/*
* If the parsed result is
* an array, and it is not empty,
* join it with any existing
* folders.
*/
if (
( is_array( $tmp_options['tmp-skip-folders'] ) ) &&
( count( $tmp_options['tmp-skip-folders'] ) > 0 )
) {
$log_msg = 'Merging folders found in configuration ' .
'file with possible other folders';
$options[ $scan_type . '-skip-folders' ] = array_merge(
$options[ $scan_type . '-skip-folders' ],
$tmp_options['tmp-skip-folders']
);
} else {
$log_msg = 'No folders found to merge with options, skipping';
}
$log_msg_details = array(
'scan_type' => $scan_type,
'file_options_' . $scan_type . '_skip_folders' => $tmp_options['tmp-skip-folders'],
'current_' . $scan_type . '_skip_folders' => $options[ $scan_type . '-skip-folders' ],
);
vipgoci_log(
$log_msg,
$log_msg_details
);
unset( $tmp_options );
}
}
/**
* Read options from environmental variables
* as specified on the command-line. Does not overwrite
* options already specified on the command-line even if
* the environment specifies them.
*
* @param array $options Array of options.
* @param array $options_recognized Array of recognized options by the program.
*
* @return void
*/
function vipgoci_options_read_env(
array &$options,
array $options_recognized
) :void {
if ( ! isset( $options['env-options'] ) ) {
return;
}
vipgoci_log(
'Attempting to read configuration from environmental variables',
array(
'env-options' => $options['env-options'],
'options-recognized' => $options_recognized,
)
);
$options_configured = array();
foreach ( $options['env-options'] as $option_unparsed ) {
/*
* Try to parse option from the command-line
* to figure out which environmental variable to use
* for which option.
*/
$option_parsed = explode(
'=',
$option_unparsed,
2 // Max one '='; any extra will be preserved in the option-env-var.
);
if ( count( $option_parsed ) !== 2 ) {
vipgoci_log(
'Invalid option provided via environment, skipping',
array(
'option_parsed' => $option_parsed,
)
);
continue;
}
$option_name = $option_parsed[0];
$option_env_var = $option_parsed[1];
unset( $option_parsed );
/*
* If option-name or env-var is too short, skip.
*/
if (
( strlen(
$option_name
) < 1 )
||
( strlen(
$option_env_var
) < 1 )
) {
vipgoci_log(
'Skipping option name/var-name from environment as it is too short',
array(
'option_name' => $option_name,
'option_env_var' => $option_env_var,
)
);
continue;
}
/*
* If not a recognized option, skip.
*/
if ( ! in_array(
$option_name . ':',
$options_recognized,
true
) ) {
vipgoci_log(
'Skipping option from environment as it is not recognized',
array(
'option_name' => $option_name,
'option_env_var' => $option_env_var,
'options_recognized' => $options_recognized,
)
);
continue;
}
/*
* Check for invalid options.
*/
if ( in_array(
$option_name,
array( 'env-options' ), // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
false
) ) {
vipgoci_log(
'Skipping option from environment as it is not allowed',
array(
'option_name' => $option_name,
'option_env_var' => $option_env_var,
)
);
continue;
}
/*
* Already configured and not
* configured by us in an earlier
* round? Then skip.
*/
if (
( isset( $options[ $option_name ] ) ) &&
( ! isset( $options_configured[ $option_name ] ) )
) {
vipgoci_log(
'Skipping option from environment as it is already configured',
array(
'option_name' => $option_name,
'option_env_var' => $option_env_var,
)
);
continue;
}
$option_env_var_value = getenv(
$option_env_var,
true
);
if ( false === $option_env_var_value ) {
vipgoci_log(
'Skipping option from environment as the variable is not defined',
array(
'option_name' => $option_name,
'option_env_var' => $option_env_var,
'option_env_var_value' => $option_env_var_value,
)
);
continue;
}
/*
* All checks passed.
* Actually set value.
*/
$options[ $option_name ] = $option_env_var_value;
$options_configured[ $option_name ] = $option_env_var_value;
}
vipgoci_log(
'Read and set options from environment',
array(
/*
* Note: Do not print out the actual
* values, so not to expose them in logs.
*/
'options_configured_keys' => array_keys(
$options_configured
),
)
);
}
/**
* Replace any sensitive option value from
* a given option array with something
* that can be printed safely and return
* a new array.
*
* @param null|array $options Options array when sanitizing, else null.
* @param array $options_add_to_sensitive If set, will add to list of sensitive option names.
*
* @return mixed Nothing is returned when sensitive options are added, else array when cleaning.
*/
function vipgoci_options_sensitive_clean(
null|array $options,
array $options_add_to_sensitive = array()
) :array {
static $sensitive_options = array();
if ( ! empty( $options_add_to_sensitive ) ) {
$sensitive_options = array_merge(
$sensitive_options,
$options_add_to_sensitive
);
return $sensitive_options;
}
$options_clean = $options;
foreach (
$options_clean as
$option_key => $option_value
) {
if ( ! in_array(
$option_key,
$sensitive_options,
true
) ) {
continue;
}
if ( ! isset( $options_clean[ $option_key ] ) ) {
continue;
}
$options_clean[ $option_key ] = '***';
}
return $options_clean;
}
/**
* Handle boolean parameters given on the command-line.
*
* Will set a default value for the given parameter name,
* if no value is set. Will then proceed to check if the
* value given is a boolean and will then convert the value
* to a boolean-type, and finally set it in $options.
*
* @param array $options Options array for the program.
* @param string $parameter_name Parameter name.
* @param string $default_value Default value.
*
* @return void
*/
function vipgoci_option_bool_handle(
array &$options,
string $parameter_name,
string $default_value
) :void {
/* If no default is given, set it */
if ( ! isset( $options[ $parameter_name ] ) ) {
$options[ $parameter_name ] = $default_value;
}
/* Check if the given value is a false or true */
if (
( 'false' !== $options[ $parameter_name ] ) &&
( 'true' !== $options[ $parameter_name ] )
) {
vipgoci_sysexit(
'Parameter --' . $parameter_name .
' has to be either false or true',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
/* Convert the given value to a boolean type value */
if ( 'false' === $options[ $parameter_name ] ) {
$options[ $parameter_name ] = false;
} else {
$options[ $parameter_name ] = true;
}
}
/**
* Handle integer parameters given on the command-line.
*
* Will set a default value for the given parameter name,
* if no value is set. Will then proceed to check if the
* value given is an integer-value, then forcibly convert
* it to integer-value to make sure it is of that type,
* then check if it is in a list of allowable values.
* If any of these fail, it will exit the program with an error.
*
* @param array $options Options array for the program.
* @param string $parameter_name Parameter name to process.
* @param int $default_value Default value for parameter.
* @param null|array $allowed_values Values allowed for parameter.
*
* @return void
*/
function vipgoci_option_integer_handle(
array &$options,
string $parameter_name,
int $default_value,
null|array $allowed_values = null
) :void {
/* If no value is set, set the default value */
if ( ! isset( $options[ $parameter_name ] ) ) {
$options[ $parameter_name ] = $default_value;
}
/* Make sure it is a numeric */
if ( ! is_numeric( $options[ $parameter_name ] ) ) {
vipgoci_sysexit(
'Usage: Parameter --' . $parameter_name . ' is not ' .
'an integer-value.',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
/* Forcibly convert to integer-value */
$options[ $parameter_name ] =
(int) $options[ $parameter_name ];
/*
* Check if value is in range
*/
if (
( null !== $allowed_values ) &&
( ! in_array(
$options[ $parameter_name ],
$allowed_values,
true
) )
) {
vipgoci_sysexit(
'Parameter --' . $parameter_name . ' is out ' .
'of allowable range.',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
}
/**
* Handle array-like option parameters given on the command line
*
* Parses the parameter, turns it into a real array,
* makes sure forbidden values are not contained in it.
* Does not return the result, but rather alters
* $options directly.
*
* Allows for array-item separator to be specified.
*
* @param array $options Options array for the program.
* @param string $option_name Option name to process.
* @param array $default_value Default value if nothing is set.
* @param string|array|null $forbidden_value Values not permissible to use.
* @param string $array_separator String separator between values.
* @param bool $strlower_option_value If to convert values to lower case.
* If set to true, forbidden values are set
* to lower case as well before validation.
*
* @return void
*/
function vipgoci_option_array_handle(
array &$options,
string $option_name,
array $default_value = array(),
array|null $forbidden_value = null,
string $array_separator = ',',
bool $strlower_option_value = true
) :void {
if ( ! isset( $options[ $option_name ] ) ) {
$options[ $option_name ] = $default_value;
} elseif ( is_array(
$options[ $option_name ]
) ) {
/*
* Option is already an array
* which can happen when an
* option is specified twice.
*/
vipgoci_sysexit(
'Parameter --' .
$option_name . ' ' .
'is an array -- ' .
'should be a string. ' .
'Is it specified twice?',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
} else {
if ( true === $strlower_option_value ) {
$options[ $option_name ] = strtolower(
$options[ $option_name ]
);
}
$options[ $option_name ] = explode(
$array_separator,
$options[ $option_name ]
);
/*
* Check for special case when an empty string is provided.
* Treat as if parameter was not provided.
*/
if (
( 1 === count( $options[ $option_name ] ) ) &&
( '' === $options[ $option_name ][0] )
) {
$options[ $option_name ] = $default_value;
}
if ( null !== $forbidden_value ) {
if ( is_string( $forbidden_value ) ) {
$forbidden_value = array( $forbidden_value );
}
if ( true === $strlower_option_value ) {
// Transform to lower case.
$forbidden_value = array_map(
'strtolower',
$forbidden_value
);
}
if ( ! empty(
array_intersect(
$forbidden_value,
$options[ $option_name ],
)
) ) {
vipgoci_sysexit(
'Parameter --' . $option_name . ' ' .
'can not contain \'' .
'"' . implode( ',', $forbidden_value ) . '"' .
'\' as one of the values',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
}
}
}
/**
* Handle parameter that expects the value
* of it to be a file. Allow a default value
* to be set if none is set.
*
* @param array $options Options array for the program.
* @param string $option_name Option name to process.
* @param string|null $default_value Path to file, or null.
*
* @return void
*/
function vipgoci_option_file_handle(
array &$options,
string $option_name,
string|null $default_value = null
) :void {
if (
( ! isset( $options[ $option_name ] ) ) &&
( null !== $default_value )
) {
$options[ $option_name ] = $default_value;
} elseif (
( ! isset( $options[ $option_name ] ) ) ||
( ! is_file( $options[ $option_name ] ) )
) {
vipgoci_sysexit(
'Parameter --' . $option_name .
' has to be a valid path',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
}
/**
* Handle parameter that we expect to be a URL.
*
* If the parameter is not empty, and is not really
* a URL (not starting with http:// or https://),
* exit with error. If empty, sets a default.
*
* @param array $options Options array for the program.
* @param string $option_name Option name to process.
* @param string|null $default_value Default value to set if empty, or null.
*
* @return void
*
* @codeCoverageIgnore
*/
function vipgoci_option_url_handle(
array &$options,
string $option_name,
string|null $default_value
) :void {
/*
* If not set, assume default value.
*/
if (
( ! isset( $options[ $option_name ] ) ) ||
( empty( $options[ $option_name ] ) )
) {
$options[ $option_name ] = $default_value;
}
/*
* If not default value, check if it looks like an URL,
* and if so, use it, but if not, exit with error.
*/
if ( $default_value !== $options[ $option_name ] ) {
$options[ $option_name ] = trim(
$options[ $option_name ]
);
if (
( 0 !== strpos(
$options[ $option_name ],
'http://'
) )
&&
( 0 !== strpos(
$options[ $option_name ],
'https://'
) )
) {
vipgoci_sysexit(
'Option --' . $option_name . ' should ' .
'be an URL',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
}
/*
* Ensure the URL is HTTPS if we are to check this
* and if the option value is a string. This is so
* that null can be passed.
*/
if (
( isset( $options['enforce-https-urls'] ) ) &&
( true === $options['enforce-https-urls'] ) &&
( is_string( $options[ $option_name ] ) ) &&
( 0 !== strpos( $options[ $option_name ], 'https://' ) )
) {
vipgoci_sysexit(
'Option --' . $option_name . ' should ' .
'be an URL starting with https://',
array(),
VIPGOCI_EXIT_USAGE_ERROR
);
}
}
/**
* Handle parameter that we expect to contain team slugs.
*
* Will check if the teams are valid, removing invalid ones
* and reconstructing the option afterwards.
*
* @param array $options Options array.
* @param string $option_name Option name to process.
*
* @return void Nothing is returned.
*/
function vipgoci_option_teams_handle(
array &$options,
string $option_name
): void {
if (
( ! isset( $options[ $option_name ] ) ) ||
( ! is_array( $options[ $option_name ] ) )
) {
$options[ $option_name ] = array();
}