]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/commandline.inc
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / scripts / commandline.inc
1 <?php
2 /*
3  * StatusNet - a distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // -*- mode: php -*-
21
22 # Abort if called from a web server
23
24 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
25     print "This script must be run from the command line\n";
26     exit();
27 }
28
29 define('GNUSOCIAL', true);
30 define('STATUSNET', true); //compatibility
31
32 define('GNUSOCIAL_CLI', true);  // to know we're in a CLI environment
33
34 // Set various flags so we don't time out on long-running processes
35
36 ini_set("max_execution_time", "0");
37 ini_set("max_input_time", "0");
38 set_time_limit(0);
39 mb_internal_encoding('UTF-8');
40 error_reporting(E_ALL & ~E_STRICT & ~E_DEPRECATED);
41
42 // Add extlib to our path so we can get Console_Getopt
43
44 $_extra_path = array(INSTALLDIR.'/extlib/');
45
46 set_include_path(implode(PATH_SEPARATOR, $_extra_path) . PATH_SEPARATOR . get_include_path());
47
48 require_once 'Console/Getopt.php';
49
50 // Note: $shortoptions and $longoptions should be pre-defined!
51
52 $_default_shortoptions = 'qvhc:s:p:';
53
54 $_default_longoptions = array('quiet', 'verbose', 'help', 'conf=', 'server=', 'path=');
55
56 if (isset($shortoptions)) {
57     $shortoptions .= $_default_shortoptions;
58 } else {
59     $shortoptions = $_default_shortoptions;
60 }
61
62 if (isset($longoptions)) {
63     $longoptions = array_merge($longoptions, $_default_longoptions);
64 } else {
65     $longoptions = $_default_longoptions;
66 }
67
68 $parser = new Console_Getopt();
69
70 $result = $parser->getopt($argv, $shortoptions, $longoptions);
71
72 if (PEAR::isError($result)) {
73     print $result->getMessage()."\n";
74     exit(1);
75 } else {
76     list($options, $args) = $result;
77 }
78
79 function show_help()
80 {
81     global $helptext;
82
83     $_default_help_text = <<<END_OF_DEFAULT
84       General options:
85
86     -q --quiet           Quiet (little output)
87     -v --verbose         Verbose (lots of output)
88     -c --conf=<filename> Use <filename> as config file
89     -s --server=<name>   Use <name> as server name
90     -p --path=<path>     Use <path> as path name
91     -h --help            Show this message and quit.
92
93 END_OF_DEFAULT;
94     if (isset($helptext)) {
95         print $helptext;
96     }
97     print $_default_help_text;
98     exit(0);
99 }
100
101 foreach ($options as $option) {
102
103     switch ($option[0]) {
104      case '--server':
105      case 's':
106         $server = $option[1];
107         break;
108
109      case '--path':
110      case 'p':
111         $path = $option[1];
112         break;
113
114      case '--conf':
115      case 'c':
116         $conffile = $option[1];
117         break;
118
119      case '--help':
120      case 'h':
121         show_help();
122     }
123 }
124
125 require_once INSTALLDIR . '/lib/common.php';
126
127 set_error_handler('common_error_handler');
128
129 // Set up the language infrastructure so we can localize anything that
130 // needs to be sent out to users, such as mail notifications.
131 common_init_language();
132
133 function _make_matches($opt, $alt)
134 {
135     $matches = array();
136
137     if (strlen($opt) > 1 && 0 != strncmp($opt, '--', 2)) {
138         $matches[] = '--'.$opt;
139     } else {
140         $matches[] = $opt;
141     }
142
143     if (!empty($alt)) {
144         if (strlen($alt) > 1 && 0 != strncmp($alt, '--', 2)) {
145             $matches[] = '--'.$alt;
146         } else {
147             $matches[] = $alt;
148         }
149     }
150
151     return $matches;
152 }
153
154 function have_option($opt, $alt=null)
155 {
156     global $options;
157
158     $matches = _make_matches($opt, $alt);
159
160     foreach ($options as $option) {
161         if (in_array($option[0], $matches)) {
162             return true;
163         }
164     }
165
166     return false;
167 }
168
169 function get_option_value($opt, $alt=null)
170 {
171     global $options;
172
173     $matches = _make_matches($opt, $alt);
174
175     foreach ($options as $option) {
176         if (in_array($option[0], $matches)) {
177             return $option[1];
178         }
179     }
180
181     return null;
182 }
183
184 class NoUserArgumentException extends Exception
185 {
186 }
187
188 function getUser()
189 {
190     $user = null;
191
192     if (have_option('i', 'id')) {
193         $id = get_option_value('i', 'id');
194         $user = User::getKV('id', $id);
195         if (empty($user)) {
196             throw new Exception("Can't find user with id '$id'.");
197         }
198     } else if (have_option('n', 'nickname')) {
199         $nickname = get_option_value('n', 'nickname');
200         $user = User::getKV('nickname', $nickname);
201         if (empty($user)) {
202             throw new Exception("Can't find user with nickname '$nickname'");
203         }
204     } else {
205         throw new NoUserArgumentException("No user argument specified.");
206     }
207
208     return $user;
209 }
210
211 /** "Printf not quiet" */
212
213 function printfnq()
214 {
215     if (have_option('q', 'quiet')) {
216         return null;
217     }
218
219     $cargs  = func_num_args();
220
221     if ($cargs == 0) {
222         return 0;
223     }
224
225     $args   = func_get_args();
226     $format = array_shift($args);
227
228     return vprintf($format, $args);
229 }
230
231 /** "Print when verbose" */
232
233 function printfv()
234 {
235     if (!have_option('v', 'verbose')) {
236         return null;
237     }
238
239     $cargs  = func_num_args();
240
241     if ($cargs == 0) {
242         return 0;
243     }
244
245     $args   = func_get_args();
246     $format = array_shift($args);
247
248     return vprintf($format, $args);
249 }