1 | 18 | simandl | #!/usr/bin/php |
2 | 1 | simandl | <?php |
3 | | | |
4 | 85 | simandl | // PHP Weathermap 0.97a |
5 | | | // Copyright Howard Jones, 2005-2009 howie@thingy.com |
6 | 1 | simandl | // http://www.network-weathermap.com/ |
7 | | | // Released under the GNU Public License |
8 | | | require_once 'Console/Getopt.php'; |
9 | | | |
10 | | | require_once "Weathermap.class.php"; |
11 | | | |
12 | | | if (!module_checks()) { die ("Quitting: Module checks failed.\n"); } |
13 | | | |
14 | 85 | simandl | // $weathermap_debugging=TRUE; |
15 | 1 | simandl | |
16 | | | $output="html"; |
17 | | | $configfile="weathermap.conf"; |
18 | | | $htmlfile=''; |
19 | | | $imagefile=''; |
20 | | | $dumpafter=0; |
21 | 85 | simandl | $dumpstats=0; |
22 | 1 | simandl | $randomdata=0; |
23 | | | $dumpconfig=''; |
24 | | | $defines=array(); |
25 | | | |
26 | | | // ************************************************************************************** |
27 | | | // THIS IS THE ONE LINE IN HERE YOU MIGHT HAVE TO CHANGE! |
28 | 85 | simandl | $rrdtool="/usr/bin/rrdtool"; |
29 | 1 | simandl | // (on Windows, use / instead of \ in pathnames - c:/rrdtool/bin/rrdtool.exe for example) |
30 | | | // ************************************************************************************** |
31 | | | |
32 | | | // initialize object |
33 | | | $cg=new Console_Getopt(); |
34 | | | $short_opts=''; |
35 | | | $long_opts=array |
36 | | | ( |
37 | | | "version", |
38 | | | "help", |
39 | | | "image-uri=", |
40 | | | "config=", |
41 | | | "output=", |
42 | | | "debug", |
43 | 85 | simandl | "uberdebug", |
44 | | | "stats", |
45 | 1 | simandl | "define=", |
46 | | | "no-data", |
47 | | | "randomdata", |
48 | | | "htmloutput=", |
49 | | | "dumpafter", |
50 | | | "bulge", |
51 | | | "sizedebug", |
52 | | | "dumpconfig=" |
53 | | | ); |
54 | | | |
55 | | | $args=$cg->readPHPArgv(); |
56 | | | |
57 | | | $ret=$cg->getopt($args, $short_opts, $long_opts); |
58 | | | |
59 | | | if (PEAR::isError($ret)) { die ("Error in command line: " . $ret->getMessage() . "\n (try --help)\n"); } |
60 | | | |
61 | | | $gopts=$ret[0]; |
62 | | | |
63 | 85 | simandl | $options_output = array(); |
64 | | | |
65 | 1 | simandl | if (sizeof($gopts) > 0) |
66 | | | { |
67 | | | foreach ($gopts as $o) |
68 | | | { |
69 | | | switch ($o[0]) |
70 | | | { |
71 | | | case '--config': |
72 | 85 | simandl | $configfile=$o[1]; |
73 | 1 | simandl | break; |
74 | | | |
75 | | | case '--htmloutput': |
76 | | | $htmlfile=$o[1]; |
77 | | | break; |
78 | | | |
79 | | | case '--dumpafter': |
80 | | | $dumpafter=1; |
81 | | | break; |
82 | | | |
83 | | | case '--image-uri': |
84 | 85 | simandl | // $map->imageuri=$o[1]; |
85 | | | $options_output['imageuri'] = $o[1]; |
86 | 1 | simandl | break; |
87 | | | |
88 | | | case '--dumpconfig': |
89 | 85 | simandl | //$map->dumpconfig=$o[1]; |
90 | | | // $options_output['dumpconfig'] = $o[1]; |
91 | | | $dumpconfig=$o[1]; |
92 | 1 | simandl | break; |
93 | | | |
94 | | | case '--randomdata': |
95 | | | $randomdata=1; |
96 | | | break; |
97 | | | |
98 | 85 | simandl | case '--stats': |
99 | | | $dumpstats=1; |
100 | | | break; |
101 | | | |
102 | | | case '--uberdebug': |
103 | | | // allow ALL trace messages (normally we block some of the chatty ones) |
104 | | | $weathermap_debug_suppress=array(); |
105 | | | // FALL THROUGH |
106 | 1 | simandl | case '--debug': |
107 | 85 | simandl | $options_output['debugging'] = TRUE; |
108 | | | $weathermap_debugging=TRUE; |
109 | | | |
110 | | | // enable assertion handling |
111 | | | assert_options(ASSERT_ACTIVE, 1); |
112 | | | assert_options(ASSERT_WARNING, 0); |
113 | | | assert_options(ASSERT_QUIET_EVAL, 1); |
114 | 1 | simandl | |
115 | 85 | simandl | // Set up the callback |
116 | | | assert_options(ASSERT_CALLBACK, 'my_assert_handler'); |
117 | | | |
118 | 1 | simandl | break; |
119 | | | |
120 | | | case '--sizedebug': |
121 | | | case '--no-data': |
122 | 85 | simandl | //$map->sizedebug=TRUE; |
123 | | | $options_output['sizedebug'] = TRUE; |
124 | 1 | simandl | break; |
125 | | | |
126 | | | case '--bulge': |
127 | 85 | simandl | //$map->widthmod=TRUE; |
128 | | | $options_output['widthmod'] = TRUE; |
129 | 1 | simandl | break; |
130 | | | |
131 | | | case '--output': |
132 | | | $imagefile=$o[1]; |
133 | | | break; |
134 | | | |
135 | | | case '--define': |
136 | | | preg_match("/^([^=]+)=(.*)\s*$/",$o[1],$matches); |
137 | 85 | simandl | if(isset($matches[2])) |
138 | | | { |
139 | | | $varname = $matches[1]; |
140 | | | $value = $matches[2]; |
141 | | | debug(">> $varname = '$value'\n"); |
142 | | | // save this for later, so that when the map object exists, it can be defined |
143 | | | $defines[$varname]=$value; |
144 | | | } |
145 | | | else |
146 | | | { |
147 | | | print "WARNING: --define format is: --define name=value\n"; |
148 | | | } |
149 | 1 | simandl | break; |
150 | | | |
151 | | | case '--version': |
152 | | | print 'PHP Network Weathermap v' . $WEATHERMAP_VERSION."\n"; |
153 | | | exit(); |
154 | | | break; |
155 | | | |
156 | | | case '--help': |
157 | | | print 'PHP Network Weathermap v' . $WEATHERMAP_VERSION."\n"; |
158 | | | print "Copyright Howard Jones, 2005-2007 howie@thingy.com\nReleased under the GNU Public License\nhttp://www.network-weathermap.com/\n\n"; |
159 | | | |
160 | | | print "Usage: php weathermap [options]\n\n"; |
161 | | | |
162 | | | print " --config {filename} - filename to read from. Default weathermap.conf\n"; |
163 | | | print " --output {filename} - filename to write image. Default weathermap.png\n"; |
164 | | | print " --htmloutput {filename} - filename to write HTML. Default weathermap.html\n\n"; |
165 | | | |
166 | | | print " --image-uri {uri} - URI to prefix <img> tags in HTML.\n"; |
167 | | | print " --bulge - Enable link-bulging mode. See manual.\n\n"; |
168 | 85 | simandl | |
169 | | | print " --define name=value - Define internal variables\n"; |
170 | | | print " (equivalent to global SET in config file)\n\n"; |
171 | 1 | simandl | |
172 | | | print " --no-data - skip the data-reading process (just a 'grey' map)\n"; |
173 | 13 | simandl | print " --randomdata - as above, but use random data\n"; |
174 | 1 | simandl | print " --debug - produce (LOTS) of debugging information during run\n"; |
175 | | | print " --dump-after - dump all internal PHP structures (HUGE)\n"; |
176 | | | print " --dumpconfig {filename} - filename to write a new config to (for testing)\n\n"; |
177 | | | |
178 | | | print " --help - show this help\n"; |
179 | | | print " --version - show version number\n\n"; |
180 | | | print "More info at http://www.network-weathermap.com/\n"; |
181 | | | exit(); |
182 | | | break; |
183 | | | } |
184 | | | } |
185 | | | } |
186 | | | |
187 | 85 | simandl | // set this BEFORE we create the map object, so we get the debug output from Reset(), as well |
188 | | | if(isset($options_output['debugging']) && $options_output['debugging']) |
189 | 1 | simandl | { |
190 | 85 | simandl | $weathermap_debugging=TRUE; |
191 | | | debug("------------------------------------\n"); |
192 | | | debug("Starting PHP-Weathermap run, with config: $configfile\n"); |
193 | | | debug("------------------------------------\n"); |
194 | 1 | simandl | } |
195 | | | |
196 | 85 | simandl | $map=new Weathermap; |
197 | | | $map->rrdtool = $rrdtool; |
198 | | | $map->context="cli"; |
199 | | | |
200 | | | // now stuff in all the others, that we got from getopts |
201 | | | foreach ($options_output as $key=>$value) |
202 | | | { |
203 | | | $map->$key = $value; |
204 | | | // $map->add_hint($key,$value); |
205 | | | } |
206 | | | |
207 | | | $weathermap_map = $configfile; |
208 | | | |
209 | 1 | simandl | if ($map->ReadConfig($configfile)) |
210 | | | { |
211 | | | // allow command-lines to override the config file, but provide a default if neither are present |
212 | | | if ($imagefile == '') |
213 | | | { |
214 | | | if ($map->imageoutputfile == '') { $imagefile="weathermap.png"; } |
215 | | | else { $imagefile=$map->imageoutputfile; } |
216 | | | } |
217 | | | |
218 | | | if ($htmlfile == '') |
219 | | | { |
220 | 85 | simandl | if ($map->htmloutputfile != '') { $htmlfile = $map->htmloutputfile; } |
221 | 1 | simandl | } |
222 | | | |
223 | | | // feed in any command-line defaults, so that they appear as if SET lines in the config |
224 | | | |
225 | 85 | simandl | // XXX FIXME |
226 | 1 | simandl | foreach ($defines as $hintname=>$hint) |
227 | | | { |
228 | | | $map->add_hint($hintname,$hint); |
229 | | | } |
230 | 85 | simandl | |
231 | | | // now stuff in all the others, that we got from getopts |
232 | | | foreach ($options_output as $key=>$value) |
233 | | | { |
234 | | | // $map->$key = $value; |
235 | | | $map->add_hint($key,$value); |
236 | | | } |
237 | 1 | simandl | |
238 | 85 | simandl | if ( (isset($options_output['sizedebug']) && ! $options_output['sizedebug']) || (!isset($options_output['sizedebug'])) ) |
239 | 1 | simandl | { |
240 | | | if ($randomdata == 1) { $map->RandomData(); } |
241 | | | else { $map->ReadData(); } |
242 | | | } |
243 | | | |
244 | 85 | simandl | # exit(); |
245 | | | |
246 | 1 | simandl | if ($imagefile != '') |
247 | | | { |
248 | | | $map->DrawMap($imagefile); |
249 | | | $map->imagefile=$imagefile; |
250 | | | } |
251 | | | |
252 | | | if ($htmlfile != '') |
253 | | | { |
254 | 85 | simandl | debug("Writing HTML to $htmlfile\n"); |
255 | | | |
256 | 1 | simandl | $fd=fopen($htmlfile, 'w'); |
257 | | | fwrite($fd, |
258 | 85 | simandl | '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head>'); |
259 | | | if($map->htmlstylesheet != '') fwrite($fd,'<link rel="stylesheet" type="text/css" href="'.$map->htmlstylesheet.'" />'); |
260 | 137 | simandl | fwrite($fd,'<meta http-equiv="refresh" content="10" /><title>' . $map->ProcessString($map->title, $map) . '</title></head><body>'); |
261 | 1 | simandl | |
262 | | | if ($map->htmlstyle == "overlib") |
263 | | | { |
264 | | | fwrite($fd, |
265 | | | "<div id=\"overDiv\" style=\"position:absolute; visibility:hidden; z-index:1000;\"></div>\n"); |
266 | | | fwrite($fd, |
267 | | | "<script type=\"text/javascript\" src=\"overlib.js\"><!-- overLIB (c) Erik Bosrup --></script> \n"); |
268 | | | } |
269 | | | |
270 | | | fwrite($fd, $map->MakeHTML()); |
271 | | | fwrite($fd, |
272 | 85 | simandl | '<hr /><span id="byline">Network Map created with <a href="http://www.network-weathermap.com/?vs=' |
273 | 1 | simandl | . $WEATHERMAP_VERSION . '">PHP Network Weathermap v' . $WEATHERMAP_VERSION |
274 | 85 | simandl | . '</a></span></body></html>'); |
275 | 1 | simandl | fclose ($fd); |
276 | | | } |
277 | | | |
278 | 85 | simandl | if ($dumpconfig != '') |
279 | | | $map->WriteConfig($dumpconfig); |
280 | 1 | simandl | |
281 | 85 | simandl | if ($dumpstats != '') |
282 | | | $map->DumpStats(); |
283 | | | |
284 | 1 | simandl | if ($dumpafter == 1) |
285 | | | print_r ($map); |
286 | 85 | simandl | |
287 | | | # print_r ($map->node_template_tree); |
288 | | | # print_r ($map->link_template_tree); |
289 | | | |
290 | | | # $map->cachefolder="editcache"; |
291 | | | # $map->CacheUpdate(1); |
292 | | | |
293 | | | |
294 | 1 | simandl | } |
295 | | | else { die ("\n\nCould not read Weathermap config file. No output produced. Maybe try --help?\n"); } |
296 | | | |
297 | 85 | simandl | |
298 | | | |
299 | | | function my_assert_handler($file, $line, $code) |
300 | | | { |
301 | | | echo "Assertion Failed: |
302 | | | File $file |
303 | | | Line $line |
304 | | | Code $code"; |
305 | | | debug_print_backtrace(); |
306 | | | exit(); |
307 | | | } |
308 | | | |
309 | 1 | simandl | // vim:ts=4:sw=4: |
310 | | | ?> |