forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclilib.php
544 lines (517 loc) · 14 KB
/
clilib.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
<?php //$Id$
/**
* CLI support functions
* @author Dilan
*/
//include PEAR Console libraries
set_include_path($CFG->libdir . PATH_SEPARATOR . $CFG->libdir . '/pear/');
require_once('Console/Getopt.php');
/**
* Check the validity of the language
* return true or false
*
* @param string $lang (short code for language)
* @return true/false
*/
function valid_language($lang) {
global $DEFAULT;
$langdir = dir($DEFAULT['dirroot'].'/install/lang');
$i=0;
$validllangs = array();
while (false !== ($file=$langdir->read())) {
if ($file[0] != '.' ) {
$validllangs[$i++]=$file;
}
}
if (in_array($lang,$validllangs)) {
return true;
} else {
return false;
}
}
//========================================================================================//
/**
* Read from array of language strings and return a array of string elements in which
* both values and keys are set to input array's key
*
* @param array $lang string elements
* @return array of string element
*/
function get_short_codes ($lang = array()) {
$short_codes = array();
foreach ($lang as $key => $value) {
$short_codes[$key] = $key;
}
return $short_codes;
}
//========================================================================================//
/**
* Check value for valid yes/no argument
* Return true or false
*
* @param string $value
* @return true/false
*/
function valid_yes_no($value){
$valid=array('yes','y','n','no');
$value=strtolower($value);
if (in_array($value,$valid)) {
if ($value[0]=='y') {
return true;
} else if ($value[0]=='n') {
return true;
}
} else {
return false;
}
}
//========================================================================================//
/**
* Can value have a valid integer in the given range
* Return true or false
* @link valid_param()
*
*
* @param mixedtype $value
* @param int $start
* @param int $end
* @return true/false
*/
function valid_int_range($value,$start,$end) {
if (valid_param($value,PARAM_INT)) {
if ($value < $end && $value > $start) {
return true;
} else {
return false;
}
}
}
/**
* Take a value and and check it with the given set of values
* If value if found in the set return true. False otherwise
*
* @param mixed type $value
* @param array $set of valid elements
* @return boolean
*/
function valid_element($value,$set) {
if(!empty($set)) {
//convert all the elements from set to lower case
foreach ($set as $key=>$opt) {
$set[$key]=strtolower($opt);
}
$value=strtolower($value);
if (in_array($value,$set)) {
return true;
} else {
return false;
}
}
}
/**
* Take a value and Type of the value
* If value match the type return true, false otherwise
* uses {@link clean_param()} in moodlelib.php
* @param mixedtype $value
* @param int $type
* @return boolean
*/
function valid_param($value,$type){
$clean_val = clean_param($value,$type);
if ($clean_val == $value) {
return true;
}else {
return false;
}
}
//========================================================================================//
/**
* Creat long arument list for PEAR method invocation using LONGOPTOIN array
*
* @param long option array $long_opt
* @return PEAR method compatible long option array
*/
function create_long_options($long_opt) {
$opt=array();
$i=0;
if (is_array($long_opt)) {
foreach ($long_opt as $key=>$value) {
if ($value == CLI_VAL_REQ) {
$opt[$i++]=$key.'=';
} else if ($value == CLI_VAL_OPT) {
$opt[$i++]=$key.'==';
}
}
}
return $opt;
}
//========================================================================================//
/**
* This funtion return an array of options with option as key containing the value of
* respective option
*
* @param array of option arguments as defined by PEAR GetOpt calss $opt
* @return return a options arguments with options as keys and values as respective value for key
*/
function get_options($opt=array()) {
global $LONG_OPTIONS;
$ret_arr=array();
//get the options from the defined list of arguments
if (!empty($opt[0]) && is_array($opt[0])) {
foreach ($opt[0] as $key=>$value) {
if (substr($value[0],0,2)=='--') { //if the argument is a long option
$input_option=substr($value[0],2);
} else if (substr($value[0],0,1)=='-'){ //if the argument is a short option
$input_option=substr($value[0],1);
}
//check with valid set of options
if (in_array($input_option,$LONG_OPTIONS)) {
$ret_arr[$input_option]=$value[1];
}
}
}
//return array
return $ret_arr;
}
//========================================================================================//
//=========================================================================//
/**
* Validate options values
*
* @param array $options
*/
function validate_option_values($options){
$values=array();
$i=0;
foreach ($options as $val) {
$values[$i++]=$val;
}
if (isset($values['lang'])) {
if (!valid_language($values['lang'])) {
console_write_error('invalidvalueforlanguage');
console_write( "\n", '', false);
}
}
if (isset($values['webdir'])) {
/**
* @todo check valid directory path
*/
}
if (isset($values['webaddr'])) {
/**
* @todo check valid http url
*/
}
if (isset($values['moodledir'])) {
/**
* @todo check valid directory path
*/
}
if (isset($values['datadir'])) {
/**
* @todo check valid directory path
*/
}
if (isset($values['dbtype'])) {
$dbtypes=array('mysql','oci8po','postgres7','mssql','mssql_n','odbc_mssql');
if (!in_array($values['dbtype'],$dbtypes)) {
console_write_error('invaliddbtype');
}
}
if (isset($values['dbhost'])) {
/**
* @todo check host?
*/
}
if (isset($values['dbname'])) {
/**
* @todo check name for valid ones if required
*/
}
if (isset($values['dbuser'])) {
/**
* @todo check validity of db user if required
*/
}
if (isset($values['dbpass'])) {
/**
* @todo check validity of database password if required
*/
}
if (isset($values['prefix'])) {
/**
* @todo check for valid prefix
*/
}
if (isset($values['sitefullname'])) {
/**
* @todo check for valid fullname for site
*/
}
if (isset($values['siteshortname'])) {
/**
* @todo check for valid short name for site
*/
}
if (isset($values['sitesummary'])) {
/**
* @todo check for valid summary
*/
}
if (isset($values['sitenewsitems'])) {
/**
* @todo check for valid news items
*/
}
if (isset($values['adminfirstname'])) {
/**
* @todo check for valid admin first name
*/
}
if (isset($values['adminlastname'])) {
/**
* @todo check for valid last name
*/
}
if (isset($values['adminusername'])) {
/**
* @todo check for valid username
*/
}
if (isset($values['adminpassword'])) {
/**
* @todo check for valid password
*/
}
if (isset($values['adminemail'])) {
/**
* @todo check for valid email
*/
}
if (isset($values['verbose'])) {
if(!valid_int_range($values['verbose'],CLI_NO,CLI_FULL)){
console_write_error('invalidverbosevalue');
}
}
if (isset($values['interactivelevel'])) {
if(!valid_int_range($values['verbose'],CLI_NO,CLI_FULL)){
console_write_error('invalidinteractivevalue');
}
}
if (isset($values['help'])) {
/**
* @todo nothing really
*/
}
}
//=========================================================================//
/**
* Read a mixed type
*
* @param stream $from
* @param int $size
* @return mixed type
*/
function read($from=STDIN,$size=1024) {
$input= trim(fread($from,$size));
return $input;
}
/**
* Read an integer
*
* @return integer
*/
function read_int() {
$input=read();
if (valid_param($input,PARAM_INT)) {
return $input;
} else {
console_write_error('invalidint');
console_write( "\n", '', false);
}
}
//=========================================================================//
/**
* Read and integer value within range
*
* @param int $start
* @param int $end
* @return int
*/
function read_int_range($start,$end) {
$input=read_int();
if (valid_int_range($input,$start,$end)) {
return $input;
} else {
console_write_error('invalidintrange');
console_write( "\n", '', false);
}
}
//=========================================================================//
/**
* Read yes/no argument
*
* @return string yes/no
*/
function read_yes_no() {
$input=strtolower(read());
if (valid_yes_no($input)) {
if ($input[0]=='y') {
return 'yes';
} else if($input[0]=='n') {
return 'no';
}
} else {
console_write_error('invalidyesno');
console_write( "\n", '', false);
}
}
//=========================================================================//
/**
* Read a boolean parameter from the input
*
* @return boolean
*/
function read_boolean(){
$input=read_yes_no();
return clean_param($input,PARAM_BOOL);
}
//=========================================================================//
/**
* Reading an element from a given set
*
* @param mixed type array $set
* @return mixed type
*/
function read_element($set=array()) {
$input=read();
if (valid_element($input,$set)) {
return $input;
} else {
console_write_error('invalidsetelement');
console_write( "\n", '', false);
}
}
//=========================================================================//
function read_url() {
$input = read();
$localhost = false;
if ( strpos($input,'localhost') !== false) {
$input = str_replace('localhost','127.0.0.1',$input);
$localhost=true;
}
if (valid_param($input,PARAM_URL)) {
if ($localhost) {
return str_replace('127.0.0.1','localhost',$input);
} else {
return $input;
}
} else {
console_write_error('invalidurl');
}
}
//=========================================================================//
/**
* Enter description here...
*
* @return string
*/
function read_dir() {
$input = read();
return $input;
}
//===========================================================================//
/**
* Print compatibility message to standard out, and errors to standard error
*
* @param boolean $success
* @param string $testtext
* @param string $errormessage
* @param boolean $caution
* @param boolean $silent
* @return boolean
*/
function check_compatibility($success, $testtext,$errormessage,$caution=false,$silent=false) {
if ($success) {
if (!$silent) {
console_write(get_string('pass', 'install'),'',false);
}
} else {
if ($caution) {
if (!$silent) {
console_write(get_string('caution', 'install'),'',false);
}
} else {
console_write(get_string('fail', 'install'),'',false);
console_write_error($errormessage,'',false);
}
}
if (!$silent) {
console_write("\t\t",'',false);
console_write($testtext,'',false);
console_write("\n",'',false);
}
return $success;
}
//==========================================================================//
/**
* Print environment status to standard out
*
* @param array $env, of type object
*/
function print_environment_status($env = array()) {
console_write( get_string('name') . "\t\t\t" . get_string('info') . "\t" . get_string('status') . "\n\r", '', false);
//console_write("Status\t\tInfo\t\tPart\n\r",'',false);
foreach ( $env as $object) {
console_write($object->part,'',false);
console_write("\t\t",'',false);
if (!empty($object->info)) {
console_write( $object->info, '', false);
} else {
console_write( "\t", '', false);
}
console_write( "\t\t", '', false);
if ($object->status == 1 ) {
console_write('ok','',false);
} else {
console_write('fail','',false);
}
console_write("\n\r",'',false);
}
}
/**
* Print environment status to standard out
*
* @param array $env, of type object
*/
function print_environment_status_detailed($env = array()) {
console_write("Status\t\tLevel\t\tCurrent ver\tRequired ver\t\tPart\t\tInfo\n\r",'',false);
foreach ( $env as $object) {
if ($object->status == 1 ) {
console_write('ok ','',false);
} else if ($object->errorcode != 0) {
console_write('fail ','',false);
} else {
console_write('----','',false);
}
console_write("\t\t",'',false);
console_write($object->level,'',false);
console_write("\t\t",'',false);
console_write($object->current_version,'',false);
console_write("\t",'',false);
console_write($object->needed_version,'',false);
console_write("\t\t",'',false);
console_write($object->part,'',false);
console_write("\t\t",'',false);
console_write($object->info,'',false);
console_write("\n\r",'',false);
}
}
/**
* Print a new line in the standard output
*
*/
function print_newline() {
console_write( "\n", '', false);
}
?>