File: Shell.php

package info (click to toggle)
postfixadmin 4.0.1%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,888 kB
  • sloc: php: 12,256; perl: 1,156; sh: 717; python: 142; xml: 63; sql: 3; makefile: 2
file content (276 lines) | stat: -rw-r--r-- 7,334 bytes parent folder | download
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
<?php

/**
 * Base class for Shells
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
 * Copyright 2005-2008, Cake Software Foundation, Inc.
 *                                                              1785 E. Sahara Avenue, Suite 490-204
 *                                                              Las Vegas, Nevada 89104
 * Modified for Postfixadmin by Valkum
 *
 * Copyright 2010
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright           Copyright 2005-2008, Cake Software Foundation, Inc.
 * @link                                http://postfixadmin.sourceforge.net/ Postfixadmin on Sourceforge
 * @package                     postfixadmin
 * @subpackage          -
 * @since                       -
 * @version                     $Revision$
 * @modifiedby          $LastChangedBy$
 * @lastmodified        $Date$
 * @license                     http://www.opensource.org/licenses/mit-license.php The MIT License
 */

class Shell
{
    /**
     * An instance of the ShellDispatcher object that loaded this script
     *
     * @var object
     * @access public
     */
    public $Dispatch;

    /**
     * If true, the script will ask for permission to perform actions.
     *
     * @var boolean
     * @access public
     */
    public $interactive = true;
    /**
     * Contains command switches parsed from the command line.
     *
     * @var array
     * @access public
     */
    public $params = array();
    /**
     * Contains arguments parsed from the command line.
     *
     * @var array
     * @access public
     */
    public $args = array();

    /**
     * The file name of the shell that was invoked.
     *
     * @var string
     * @access public
     */
    public $shell;

    /**
     * The class name of the shell that was invoked.
     *
     * @var string
     * @access public
     */
    public $className;

    /**
     * The command called if public methods are available.
     *
     * @var string
     * @access public
     */

    public $command;
    /**
     * The name of the shell in camelized.
     *
     * @var string
     * @access public
     */
    public $name;

    /**
     * @param string
     */
    public $handler_to_use;

    public $new;
    /**
     *  Constructs this Shell instance.
     *
     */
    public function __construct(&$dispatch)
    {
        $vars = array('params', 'args', 'shell', 'shellCommand' => 'command');
        foreach ($vars as $key => $var) {
            if (is_string($key)) {
                $this->{$var} = & $dispatch->{$key};
            } else {
                $this->{$var} = & $dispatch->{$var};
            }
        }

        $this->className = get_class($this);

        if ($this->name == null) {
            $this->name = str_replace(array('shell', 'Shell', 'task', 'Task'), '', $this->className);
        }

        $this->Dispatch = & $dispatch;
    }

    /**
     * Starts up the the Shell
     * allows for checking and configuring prior to command or main execution
     * can be overriden in subclasses
     *
     * @access public
     */
    public function startup()
    {
        if (empty($this->params['q'])) {
            $this->_welcome();
        }
    }
    /**
     * Displays a header for the shell
     *
     * @access protected
     */
    public function _welcome()
    {
        $this->out("\nWelcome to Postfixadmin-CLI v" . $this->Dispatch->version);
        $this->hr();
    }

    /**
     * Prompts the user for input, and returns it.
     *
     * @param string $prompt Prompt text.
     * @param mixed $options Array or string of options.
     * @param string $default Default input value.
     * @return string either the default value, or the user-provided input.
     */
    public function in($prompt, $options = null, $default = '')
    {
        if (!$this->interactive) {
            return $default;
        }
        if ($prompt != '') {
            $this->out("");
        }
        $in = $this->Dispatch->getInput($prompt, $options, $default);

        if ($options && is_string($options)) {
            if (strpos($options, ',')) {
                $options = explode(',', $options);
            } elseif (strpos($options, '/')) {
                $options = explode('/', $options);
            } else {
                $options = array($options);
            }
        }
        if (is_array($options)) {
            while ($in == '' || ($in && (!in_array(strtolower($in), $options) && !in_array(strtoupper($in), $options)) && !in_array($in, $options))) {
                $this->err("Invalid input"); # TODO: make translateable
                $in = $this->Dispatch->getInput($prompt, $options, $default);
            }
        }

        return $in;
    }
    /**
     * Outputs to the stdout filehandle.
     *
     * @param string|array $string String to output.
     * @param boolean $newline If true, the outputs gets an added newline.
     */
    public function out($string, $newline = true)
    {
        if (is_array($string)) {
            $str = '';
            foreach ($string as $message) {
                $str .= $message ."\n";
            }
            $string = $str;
        }
        return $this->Dispatch->stdout($string, $newline);
    }
    /**
     * Outputs to the stderr filehandle.
     *
     * @param string|array $string Error text to output.
     * @access public
     */
    public function err($string)
    {
        if (is_array($string)) {
            $str = '';
            foreach ($string as $message) {
                $str .= $message ."\n";
            }
            $string = $str;
        }
        return $this->Dispatch->stderr($string."\n");
    }
    /**
     * Outputs a series of minus characters to the standard output, acts as a visual separator.
     *
     * @param boolean $newline If true, the outputs gets an added newline.
     * @access public
     */
    public function hr($newline = false)
    {
        if ($newline) {
            $this->out("\n");
        }
        $this->out('---------------------------------------------------------------');
        if ($newline) {
            $this->out("\n");
        }
    }
    /**
     * Displays a formatted error message and exits the application
     *
     * @param string $title Title of the error message
     * @param string $msg Error message
     * @access public
     */
    public function error($title, $msg)
    {
        $out  = "$title\n";
        $out .= "$msg\n";
        $out .= "\n";
        $this->err($out);
        $this->_stop(1);
    }
    /**
     * Outputs usage text on the standard output. Implement it in subclasses.
     *
     * @access public
     */
    public function help()
    {
        if ($this->command != null) {
            $this->err("Unknown {$this->name} command '$this->command'.\nFor usage, try 'postfixadmin-cli {$this->shell} help'.\n\n");
        } else {
            $this->Dispatch->help();
        }
    }
    /**
     * Stop execution of the current script
     *
     * @param $status see http://php.net/exit for values
     * @return void
     * @access public
     */
    public function _stop($status = 0)
    {
        exit($status);
    }
}