GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CLI_Action::getAllParams()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
* Copyright (c) Xerox Corporation, Codendi Team, 2001-2007. All rights reserved
4
*
5
* 
6
*/
7
8
class CLI_Action {
9
    
10
    var $name;
11
    var $description;
12
    var $module;
13
    var $soap;
14
    var $params;
15
    var $soapCommand;
16
    function CLI_Action($name, $description) {
17
        $this->name              = $name;
18
        $this->soapCommand       = $name;
19
        $this->description       = $description;
20
        $this->params            = array();
21
        $this->addParam(array(
22
            'name'           => 'quiet',
23
            'description'    => '--quiet or -q      Quiet-mode. Suppress result output.',
24
            'parameters'     => array('q', 'quiet'),
25
            'value_required' => false,
26
            'soap'           => false,
27
        ));
28
        $this->addProjectParam();
29
    }
30
    function addProjectParam() {
31
        $this->addParam(array(
32
            'name'           => 'group_id',
33
            'description'    => '--project=<name>   Name of the project the item belongs to. If you specified the name of
34
      the working project when you logged in, this parameter is not needed.',
35
            'parameters'     => array('project'),
36
        ));
37
    }
38
    function getName() {
39
        return $this->name;
40
    }
41
    function getDescription() {
42
        return $this->description;
43
    }
44
    function setModule(&$module) {
45
        $this->module =& $module;
46
    }
47
    function setSoapCommand($commandName) {
48
        $this->soapCommand = $commandName;
49
    }
50
    function addParam($param) {
51
        $this->params[] = $param;
52
    }
53
    function getAllParams() {
54
        return $this->params;
55
    }
56
    function help() {
57
        $help = $this->getName() .":\n";
58
        $help .= $this->getDescription() ."\n\n";
59
        $help .= "Available parameters:\n";
60
        foreach($this->params as $param) {
61
            $help .= "   ". $param['description'] ."\n";
62
        }
63
        $help .= "\n   --help    Show this screen\n";
64
        return $help;
65
    }
66
    
67
    function soapResult($params, $soap_result, $fieldnames = array(), $loaded_params = array()) {
68
        if (!$loaded_params['others']['quiet']) $this->show_output($soap_result, $fieldnames);
69
    }
70
    function loadParams($params) {
71
        $all_params = array('soap' => array(), 'others' => array());
72
        foreach($this->params as $param) {
73
            $search_for = $param['name'];
74
            if (isset($param['parameters'])) {
75
                $search_for = $param['parameters'];
76
            }
77
            $type = 'soap';
78
            if (isset($param['soap']) && !$param['soap']) {
79
                $type = 'others';
80
            }
81
            $value_required = !isset($param['value_required']) || $param['value_required'];
82
            if (isset($param['method'])) {
83
                $t = call_user_func_array($param['method'], array(&$params));
84
            } else {
85
                $t = $this->module->getParameter($params, $search_for, $value_required);
86
            }
87
            if (!method_exists($this, 'validate_'. $param['name']) 
88
                || 
89
                call_user_func_array(array($this, 'validate_'. $param['name']), array(&$t))
90
            ) {
91
                $all_params[$type][$param['name']] = $t;
92
            }
93
        }
94
        return $all_params;
95
    }
96
    function confirmation($params) {
97
        return true;
98
    }
99
    function before_soapCall(&$loaded_params) {
100
    }
101
    function after_loadParams(&$loaded_params) {
102
    }
103
    function use_extra_params() {
104
        return true;
105
    }
106
    function soapCall($soap_params, $use_extra_params = true) {
107
        return $GLOBALS['soap']->call($this->soapCommand, $soap_params, $use_extra_params);
108
    }
109
    /*function soapError() {
110
        if (($error = $GLOBALS['soap']->getError())) {
111
            $GLOBALS['LOG']->add($GLOBALS['soap']->responseData);
112
            exit_error($error, $GLOBALS['soap']->faultcode);
113
        }
114
    }*/
115
116
    function execute($params) {
117
    	$soap_result = null;
118
        if ($this->module->getParameter($params, array('h', 'help'))) {
119
            echo $this->help();
120
        } else {
121
            $loaded_params = $this->loadParams($params);
122
            $this->after_loadParams($loaded_params);
123
            if ($this->confirmation($loaded_params)) {
124
                $this->before_soapCall($loaded_params);
125
                try {
126
                	$soap_result = $this->soapCall($loaded_params['soap'], $this->use_extra_params());
127
                } catch (SoapFault $fault) {
128
                    $GLOBALS['LOG']->add($GLOBALS['soap']->__getLastResponse());
129
                    exit_error($fault, $fault->getCode());
130
                }
131
                $this->soapResult($params, $soap_result, array(), $loaded_params);
132
            }
133
        }
134
        return $soap_result;
135
    }
136
    
137
    function validate_group_id(&$group_id) {
138
        $group_id = $this->get_working_group($group_id);
139
        return true;
140
    }
141
    
142
    function user_confirm($msg) {
143
        $sure = $this->module->get_user_input($msg. " (y/n): ");
144
        return strtolower($sure) == 'yes' || strtolower($sure) == 'y';
145
    }
146
    /**
147
     * show_output - Format a SOAP result in order to display it on the user's screen
148
     *
149
     * extracted from GForge Command-line Interface
150
     * contained in GForge.
151
     * Copyright 2005 GForge, LLC
152
     * http://gforge.org/
153
     *
154
     * @param    array    Result of a SOAP call
155
     * @param    array    Titles to assign to each field (optional).
156
     */
157
    function show_output($result, $fieldnames = array()) {
158
        // There are 3 types of output: a scalar (int, string), a vector or a matrix (table-like).
159
        // Try to guess which output is the best for $result
160
        if (is_object($result)) {
161
            
162
            $this->show_object($result, $fieldnames);
163
            
164
        } elseif(is_array($result)) {
165
            
166
            if (count($result) == 0) {
167
                echo "No results\n";
168
                return;
169
            }
170
            
171
            if (isset($result[0]) && is_array($result[0])) {
172
                $this->show_matrix($result, $fieldnames);
173
            } elseif (isset($result[0]) && is_object($result[0])) {
174
                $this->show_matrix($result, $fieldnames);
175
            } else {
176
                $this->show_vector($result, $fieldnames);
177
            }
178
        } else {
179
            $this->show_scalar($result, $fieldnames);
180
        }
181
    }
182
183
    /**
184
     * Check if the object returned by a SOAP call is valid
185
     * @see CLI_Action::show_object()
186
     *
187
     * @param array $row Result of a SOAP call
188
     *
189
     * @return Boolean
190
     */
191
    private function isIterable($row) {
192
        return (is_object($row) && get_class($row) === 'stdClass') || is_array($row);
193
    }
194
195
    function show_object($result, $fieldnames = array()) {
196
        $titles = array();
197
        $lengths = array();
198
        
199
        // this is for showing multidimensional arrays
200
        static $recursive_id = 1;
201
        
202
        foreach ($result as $colname => $value) {
203
            if (!isset($titles[$colname])) {
204
                if (!isset($fieldnames[$colname])) {
205
                    $titles[$colname] = $colname;
206
                } else {
207
                    $titles[$colname] = $fieldnames[$colname];
208
                }
209
            }
210
            
211
            if (!is_array($value)) {
212
                if (!isset($lengths[$colname]) || $lengths[$colname] < strlen($value)+2) {
213
                    $lengths[$colname] = max(strlen($value), strlen($titles[$colname]));
214
                    $lengths[$colname] += 2;
215
                }
216
            } else {
217
                $lengths[$colname] = strlen($titles[$colname]) + 2;
218
            }
219
        }
220
    
221
        // show the titles
222
        foreach ($titles as $colname => $title) {
223
            $length = $lengths[$colname];
224
            echo "+".str_repeat("-", $length);
225
        }
226
        echo "+\n";
227
        foreach ($titles as $colname => $title) {
228
            $length = $lengths[$colname];
229
            echo "|".$this->center_text($title, $length);
230
        }
231
        echo "|\n";
232
        foreach ($titles as $colname => $title) {
233
            $length = $lengths[$colname];
234
            echo "+".str_repeat("-", $length);
235
        }
236
        echo "+\n";
237
        
238
        $recursive_items = array();
239
        // now show the values
240
        foreach ($result as $colname => $value) {
241
            // recursively show the multi dimensional array
242
            if (is_array($value)) {
243
                if (array_key_exists($colname, $fieldnames)) $rec_titles = $fieldnames[$colname];
244
                else $rec_titles = array();
245
                $recursive_items[$recursive_id] = array("titles" => $rec_titles, "values" => $value);
246
                // show the reference # instead
247
                $value = "[".$recursive_id."]";
248
                $recursive_id++;
249
            }
250
            
251
            $length = $lengths[$colname];
252
            if (is_array($value)) continue;
253
            echo "| ".$value.str_repeat(" ", $length-strlen($value)-1);
254
        }
255
        echo "|\n";
256
        
257
        
258
        // show last line
259
        foreach ($titles as $colname => $title) {
260
            $length = $lengths[$colname];
261
            echo "+".str_repeat("-", $length);
262
        }
263
        echo "+\n";
264
        
265
        // now recursively show the multidimensional array
266
        foreach ($recursive_items as $id => $item) {
267
            echo "\n";
268
            echo "[".$id."]:\n";
269
            $this->show_output($item["values"], $item["titles"]);
270
        }
271
    }
272
    
273
    
274
    function show_objects($result, $fieldnames = array()) {
275
        $titles = array();
276
        $lengths = array();
277
        
278
        // this is for showing multidimensional arrays
279
        static $recursive_id = 1;
280
        
281
        foreach ($result as $row) {
282
            foreach ($row as $colname => $value) {
283
                if (!isset($titles[$colname])) {
284
                    if (!isset($fieldnames[$colname])) {
285
                        $titles[$colname] = $colname;
286
                    } else {
287
                        $titles[$colname] = $fieldnames[$colname];
288
                    }
289
                }
290
                
291
                if (!is_array($value)) {
292
                    if (!isset($lengths[$colname]) || $lengths[$colname] < strlen($value)+2) {
293
                        $lengths[$colname] = max(strlen($value), strlen($titles[$colname]));
294
                        $lengths[$colname] += 2;
295
                    }
296
                } else {
297
                    $lengths[$colname] = strlen($titles[$colname]) + 2;
298
                }
299
            }
300
        }
301
    
302
        // show the titles
303
        foreach ($titles as $colname => $title) {
304
            $length = $lengths[$colname];
305
            echo "+".str_repeat("-", $length);
306
        }
307
        echo "+\n";
308
        foreach ($titles as $colname => $title) {
309
            $length = $lengths[$colname];
310
            echo "|".$this->center_text($title, $length);
311
        }
312
        echo "|\n";
313
        foreach ($titles as $colname => $title) {
314
            $length = $lengths[$colname];
315
            echo "+".str_repeat("-", $length);
316
        }
317
        echo "+\n";
318
        
319
        $recursive_items = array();
320
        // now show the values
321
        foreach ($result as $row) {
322
            foreach ($row as $colname => $value) {
323
                // recursively show the multi dimensional array
324
                if (is_array($value)) {
325
                    if (array_key_exists($colname, $fieldnames)) $rec_titles = $fieldnames[$colname];
326
                    else $rec_titles = array();
327
                    $recursive_items[$recursive_id] = array("titles" => $rec_titles, "values" => $value);
328
                    // show the reference # instead
329
                    $value = "[".$recursive_id."]";
330
                    $recursive_id++;
331
                }
332
                
333
                $length = $lengths[$colname];
334
                if (is_array($value)) continue;
335
                echo "| ".$value.str_repeat(" ", $length-strlen($value)-1);
336
            }
337
            echo "|\n";
338
        }
339
        
340
        // show last line
341
        foreach ($titles as $colname => $title) {
342
            $length = $lengths[$colname];
343
            echo "+".str_repeat("-", $length);
344
        }
345
        echo "+\n";
346
        
347
        // now recursively show the multidimensional array
348
        foreach ($recursive_items as $id => $item) {
349
            echo "\n";
350
            echo "[".$id."]:\n";
351
            $this->show_output($item["values"], $item["titles"]);
352
        }
353
    }
354
    
355
    /**
356
     * extracted from GForge Command-line Interface
357
     * contained in GForge.
358
     * Copyright 2005 GForge, LLC
359
     * http://gforge.org/
360
     *
361
     */
362
    function show_scalar($result, $fieldnames = array()) {
363
        $title = (isset($fieldnames[0])) ? $fieldnames[0] : "Result";
364
        // convert to string (may be an int)
365
        $result = "$result";
366
        $length = max(strlen($result), strlen($title));
367
        $length = $length + 2;        // +2 for having spaces at the beginning and the end
368
    
369
        // show the title
370
        echo "+".str_repeat("-", $length)."+\n";
371
        echo "|".$this->center_text($title, $length)."|\n";
372
        echo "+".str_repeat("-", $length)."+\n";
373
        
374
        // show the item
375
        echo "| ".$result.str_repeat(" ", $length-strlen($result)-1)."|\n";
376
        echo "+".str_repeat("-", $length)."+\n";
377
    }
378
    
379
    /**
380
     * extracted from GForge Command-line Interface
381
     * contained in GForge.
382
     * Copyright 2005 GForge, LLC
383
     * http://gforge.org/
384
     *
385
     */
386
    function show_vector($result, $fieldnames = array()) {
387
        // There are two types of vector: those that are a set of items and those
388
        // that are just like a row in the database. For the second case, reuse the
389
        // show_matrix function considering the vector as a 1-row matrix
390
        if (!isset($result[0])) {
391
            // This happens when $result is an indexed array (i.e. $result["fieldname"]. In this
392
            // case consider $result as a 1 row matrix
393
            $foo_matrix = array();
394
            $foo_matrix[] = $result;
395
            $this->show_matrix($foo_matrix, $fieldnames);
396
            return;
397
        }
398
        
399
        $title = (isset($fieldnames[0])) ? $fieldnames[0] : "Result";
400
        
401
        $length = strlen($title);
402
        // get the maximum length for a single item
403
        foreach ($result as $item) {
404
            if (is_array($item)) continue;        // shouldn't happen
405
            $length = max(strlen($item), $length);
406
        }
407
        $length = $length + 2;        // +2 for having spaces at the beginning and the end
408
        
409
        // show the title
410
        echo "+".str_repeat("-", $length)."+\n";
411
        echo "|".$this->center_text($title, $length)."|\n";
412
        echo "+".str_repeat("-", $length)."+\n";
413
        
414
        // show each item
415
        foreach ($result as $item) {
416
            echo "| ".$item.str_repeat(" ", $length-strlen($item)-1)."|\n";
417
        }
418
        
419
        // show last line
420
        echo "+".str_repeat("-", $length)."+\n";
421
    }
422
    
423
    /**
424
     * extracted from GForge Command-line Interface
425
     * contained in GForge.
426
     * Copyright 2005 GForge, LLC
427
     * http://gforge.org/
428
     *
429
     */
430
    function show_matrix($result, $fieldnames = array()) {
431
        $titles = array();
432
        $lengths = array();
433
        
434
        // this is for showing multidimensional arrays
435
        static $recursive_id = 1;
436
        
437
        foreach ($result as $row) {
438
            if ($this->isIterable($row)) {
439
                foreach ($row as $colname => $value) {
440
                    if (!isset($titles[$colname])) {
441
                        if (!isset($fieldnames[$colname])) {
442
                            $titles[$colname] = $colname;
443
                        } else {
444
                            $titles[$colname] = $fieldnames[$colname];
445
                        }
446
                    }
447
                }
448
            }
449
        }
450
        // Multi-line string handling
451
        // A row containing a N-line cell will give N rows in the array $result
452
        $rowArrays = array();
453
        $result2 = array();
454
        foreach ($result as $i => $row) {
455
            if ($this->isIterable($row)) {
456
                foreach ($row as $colname => $value) {
457
                    if (is_array($value)) {
458
                        $lengths[$colname] = strlen($titles[$colname]) + 2;
459
                        $rowArrays[$i][0][$colname] = $value;
460
                    } else {
461
                        $lines = explode("\n", $value);
462
                    
463
                        foreach ($lines as $j => $line) {
464
                            if (!isset($lengths[$colname]) || $lengths[$colname] < strlen($line)+2) {
465
                                $lengths[$colname] = max(strlen($line), strlen($titles[$colname]));
466
                                $lengths[$colname] += 2;
467
                            }
468
469
                            if (!isset($rowArrays[$i][$j])) {
470
                                foreach ($titles as $colname2 => $v) {
471
                                    $rowArrays[$i][$j][$colname2] = '';
472
                                }
473
                            }
474
                            $rowArrays[$i][$j][$colname] = $line;
475
                        }
476
                    }
477
                }
478
                foreach ($rowArrays[$i] as $row) {
479
                    $result2[] = $row;
480
                }
481
            }
482
        }
483
        $result = $result2;
484
    
485
        // show the titles
486
        foreach ($titles as $colname => $title) {
487
            $length = $lengths[$colname];
488
            echo "+".str_repeat("-", $length);
489
        }
490
        echo "+\n";
491
        foreach ($titles as $colname => $title) {
492
            $length = $lengths[$colname];
493
            echo "|".$this->center_text($title, $length);
494
        }
495
        echo "|\n";
496
        foreach ($titles as $colname => $title) {
497
            $length = $lengths[$colname];
498
            echo "+".str_repeat("-", $length);
499
        }
500
        echo "+\n";
501
        
502
        $recursive_items = array();
503
        // now show the values
504
        foreach ($result as $row) {
505
            if ($this->isIterable($row)) {
506
                foreach ($row as $colname => $value) {
507
                    // recursively show the multi dimensional array
508
                    if (is_array($value)) {
509
                        if (array_key_exists($colname, $fieldnames)) $rec_titles = $fieldnames[$colname];
510
                        else $rec_titles = array();
511
                        $recursive_items[$recursive_id] = array("titles" => $rec_titles, "values" => $value);
512
                        // show the reference # instead
513
                        $value = "[".$recursive_id."]";
514
                        $recursive_id++;
515
                    }
516
                    
517
                    $length = $lengths[$colname];
518
                    if (is_array($value)) continue;
519
                    echo "| ".$value.str_repeat(" ", $length-strlen($value)-1);
520
                }
521
                echo "|\n";
522
            }
523
        }
524
        
525
        // show last line
526
        foreach ($titles as $colname => $title) {
527
            $length = $lengths[$colname];
528
            echo "+".str_repeat("-", $length);
529
        }
530
        echo "+\n";
531
        
532
        // now recursively show the multidimensional array
533
        foreach ($recursive_items as $id => $item) {
534
            echo "\n";
535
            echo "[".$id."]:\n";
536
            $this->show_output($item["values"], $item["titles"]);
537
        }
538
    }
539
    /**
540
     * center_text - Given a text and a length, returns a string of length $lenght with $text located in
541
     * the middle (the string is padded with whitespaces).
542
     *
543
     * extracted from GForge Command-line Interface
544
     * contained in GForge.
545
     * Copyright 2005 GForge, LLC
546
     * http://gforge.org/
547
     *
548
     * @param    string
549
     * @param    int
550
     */
551
    function center_text($text, $length) {
552
        if (strlen($text) >= $length) return $text;
553
        $delta = $length - strlen($text);
554
        $pad_left = floor($delta/2);
555
        $pad_right = $delta - $pad_left;
556
        return str_repeat(" ", $pad_left).$text.str_repeat(" ", $pad_right);
557
    }
558
    
559
    /**
560
     * get_group_id - Given a group UNIX name, returns the group ID or 0 if the group doesn't exists
561
     *
562
     * extracted from GForge Command-line Interface
563
     * contained in GForge.
564
     * Copyright 2005 GForge, LLC
565
     * http://gforge.org/
566
     *
567
     * @param    string    UNIX name of the project
568
     */
569
    function get_group_id($unix_group_name) {
570
        if (!isset($this->cached_group_id[$unix_group_name])) {
571
            try {
572
                
573
                $res = $GLOBALS['soap']->call("getGroupByName", array(/*"sessionKey" => $GLOBALS['soap']->session_string, */ "unix_group_name" => $unix_group_name));
574
                
575
                if (!is_object($res)) {        // An error here means that no group was found
576
                    $this->cached_group_id[$unix_group_name] = 0;
577
                } else {
578
                    $this->cached_group_id[$unix_group_name] = $res->group_id;
579
                }
580
                
581
             } catch (SoapFault $fault) {
582
                $this->cached_group_id[$unix_group_name] = 0;
583
            }
584
        }
585
        return $this->cached_group_id[$unix_group_name];
586
    }
587
    var $cached_group_id;
588
    
589
    /**
590
     * get_working_group - Return the ID of the group the user is currently working with. The name of the group can be defined
591
     * on the session or in the parameters.
592
     *
593
     * extracted from GForge Command-line Interface
594
     * contained in GForge.
595
     * Copyright 2005 GForge, LLC
596
     * http://gforge.org/
597
     *
598
     * @param array An array of parameters to look for the defined group. If the group isn't in the parameters, looks in the session
599
     * @param bool Specify if we should abort the program if the group isn't found
600
     */
601
    function get_working_group($group_name, $die=true) {
602
        if ($group_name) {
603
            $group_id = $this->get_group_id($group_name);
604
            if (!$group_id) {
605
                if ($die) exit_error("Invalid project \"".$group_name."\"");
606
                else return false;
607
            }
608
        } else {
609
            $group_id = $GLOBALS['soap']->getSessionGroupID();
610
            if (!$group_id) {
611
                if ($die) exit_error("You must specify a project using the --project=parameter");
612
                else return false;
613
            }
614
        }
615
        return $group_id;
616
    }
617
618
    /**
619
     * check_date - Check if a date entered by the user is correctly formatted and it is valid.
620
     * 
621
     * extracted from GForge Command-line Interface
622
     * contained in GForge.
623
     * Copyright 2005 GForge, LLC
624
     * http://gforge.org/
625
     *
626
     * @param    string    Date
627
     * @return    string    String with the error (if any)
628
     */
629
    function check_date($date) {
630
        $pieces = array();
631
        if (!preg_match("/^([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})\$/", $date, $pieces)) {
632
            return "Must be in format YYYY-MM-DD";
633
        }
634
        
635
        $year = intval($pieces[1]);
636
        $month = intval($pieces[2]);
637
        $day = intval($pieces[3]);
638
        
639
        if (!checkdate($month, $day, $year)) {
640
            return "Is not a valid date";
641
        }
642
        
643
        return "";
644
    }
645
    
646
    /**
647
     * convert_date - Convert a date entered by the user in format YYYY-MM-DD to a timestamp
648
     * to be inserted in the database.
649
     * 
650
     * extracted from GForge Command-line Interface
651
     * contained in GForge.
652
     * Copyright 2005 GForge, LLC
653
     * http://gforge.org/
654
     *
655
     * This function assumes the date has the correct format
656
     * @param    string    Date
657
     * @return    int
658
     */
659
    function convert_date($date) {
660
        $pieces = array();
661
        preg_match("/^([0-9]{2,4})-([0-9]{1,2})-([0-9]{1,2})\$/", $date, $pieces);
662
        
663
        $year = intval($pieces[1]);
664
        $month = intval($pieces[2]);
665
        $day = intval($pieces[3]);
666
        
667
        return mktime(0, 0, 0, $month, $day, $year);
668
    }
669
670
}
671
672
?>
673