1#!/usr/local/bin/php
2<?php
3
4// PHP Weathermap 0.97a
5// Copyright Howard Jones, 2005-2009 howie@thingy.com
6// http://www.network-weathermap.com/
7// Released under the GNU Public License
8require_once '/usr/local/share/pear/Console/Getopt.php';
9
10require_once "/usr/local/share/phpweathermap/Weathermap.class.php";
11
12if (!module_checks()) { die ("Quitting: Module checks failed.\n"); }
13
14//    $weathermap_debugging=TRUE;
15
16$output="html";
17$configfile="/usr/local/share/phpweathermap/weathermap.conf";
18$htmlfile='';
19$imagefile='';
20$dumpafter=0;
21$dumpstats=0;
22$randomdata=0;
23$dumpconfig='';
24$defines=array();
25
26// **************************************************************************************
27// THIS IS THE ONE LINE IN HERE YOU MIGHT HAVE TO CHANGE!
28$rrdtool="/usr/local/bin/rrdtool";
29// (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		"uberdebug",
44		"stats",
45		"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
59if (PEAR::isError($ret)) { die ("Error in command line: " . $ret->getMessage() . "\n (try --help)\n"); }
60
61$gopts=$ret[0];
62
63$options_output = array();
64
65if (sizeof($gopts) > 0)
66{
67	foreach ($gopts as $o)
68	{
69		switch ($o[0])
70		{
71		case '--config':
72			$configfile=$o[1];
73			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			// $map->imageuri=$o[1];
85			$options_output['imageuri'] = $o[1];
86			break;
87
88		case '--dumpconfig':
89			//$map->dumpconfig=$o[1];
90			// $options_output['dumpconfig'] = $o[1];
91			$dumpconfig=$o[1];
92			break;
93
94		case '--randomdata':
95			$randomdata=1;
96			break;
97
98		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		case '--debug':
107			$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
115			// Set up the callback
116			assert_options(ASSERT_CALLBACK, 'my_assert_handler');
117
118			break;
119
120		case '--sizedebug':
121		case '--no-data':
122			//$map->sizedebug=TRUE;
123			$options_output['sizedebug'] = TRUE;
124			break;
125
126		case '--bulge':
127			//$map->widthmod=TRUE;
128			$options_output['widthmod'] = TRUE;
129			break;
130
131		case '--output':
132			$imagefile=$o[1];
133			break;
134
135        case '--define':
136            preg_match("/^([^=]+)=(.*)\s*$/",$o[1],$matches);
137			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            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
169						print " --define name=value      -  Define internal variables\n";
170						print "                             (equivalent to global SET in config file)\n\n";
171
172                        print " --no-data                -  skip the data-reading process (just a 'grey' map)\n";
173                        print " --randomdata            -  as above, but use random data\n";
174                        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// set this BEFORE we create the map object, so we get the debug output from Reset(), as well
188if(isset($options_output['debugging']) && $options_output['debugging'])
189{
190	$weathermap_debugging=TRUE;
191	debug("------------------------------------\n");
192	debug("Starting PHP-Weathermap run, with config: $configfile\n");
193	debug("------------------------------------\n");
194}
195
196$map=new Weathermap;
197$map->rrdtool = $rrdtool;
198$map->context="cli";
199
200// now stuff in all the others, that we got from getopts
201foreach ($options_output as $key=>$value)
202{
203	$map->$key = $value;
204	// $map->add_hint($key,$value);
205}
206
207$weathermap_map = $configfile;
208
209if ($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		if ($map->htmloutputfile != '') { $htmlfile = $map->htmloutputfile; }
221	}
222
223    // feed in any command-line defaults, so that they appear as if SET lines in the config
224
225    // XXX FIXME
226    foreach ($defines as $hintname=>$hint)
227    {
228        $map->add_hint($hintname,$hint);
229    }
230
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
238        if ( (isset($options_output['sizedebug']) && ! $options_output['sizedebug']) || (!isset($options_output['sizedebug'])) )
239	{
240		if ($randomdata == 1) { $map->RandomData(); }
241		else { $map->ReadData(); }
242	}
243
244	# exit();
245
246	if ($imagefile != '')
247	{
248		$map->DrawMap($imagefile);
249		$map->imagefile=$imagefile;
250	}
251
252	if ($htmlfile != '')
253	{
254		debug("Writing HTML to $htmlfile\n");
255
256		$fd=fopen($htmlfile, 'w');
257		fwrite($fd,
258			'<!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		fwrite($fd,'<meta http-equiv="refresh" content="300" /><title>' . $map->ProcessString($map->title, $map) . '</title></head><body>');
261
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			'<hr /><span id="byline">Network Map created with <a href="http://www.network-weathermap.com/?vs='
273			. $WEATHERMAP_VERSION . '">PHP Network Weathermap v' . $WEATHERMAP_VERSION
274			. '</a></span></body></html>');
275		fclose ($fd);
276	}
277
278	if ($dumpconfig != '')
279		$map->WriteConfig($dumpconfig);
280
281	if ($dumpstats != '')
282		$map->DumpStats();
283
284	if ($dumpafter == 1)
285		print_r ($map);
286
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}
295else { die ("\n\nCould not read Weathermap config file. No output produced. Maybe try --help?\n"); }
296
297
298
299function 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// vim:ts=4:sw=4:
310?>
311