jablonka.czprosek.czf

weathermap

Subversion Repositories:
[/] [editor.inc.php] - Blame information for rev 110

 

Line No. Rev Author Line
11simandl<?php
285simandl 
3/** editor.inc.php
4 *
5 * All the functions used by the editor.
6 */
7 
8/** @function fix_gpc_string
9 *
10 * Take a string (that we got from $_REQUEST) and make it back to how the
11 * user TYPED it, regardless of whether magic_quotes_gpc is turned on or off.
12 *
13 * @param string $input String to fix
14 *
15 * @returns string Fixed string
16 *
17 */
18function fix_gpc_string($input)
19{
20 if (true == function_exists('get_magic_quotes_gpc') && 1 == get_magic_quotes_gpc()) {
21 $input = stripslashes($input);
22 }
23 return ($input);
24}
25 
261simandlfunction show_editor_startpage()
27{
2885simandl global $mapdir, $WEATHERMAP_VERSION, $config_loaded, $cacti_found, $ignore_cacti,$configerror;
291simandl 
3085simandl $fromplug = false;
31 if (isset($_REQUEST['plug']) && (intval($_REQUEST['plug'])==1) ) {
32 $fromplug = true;
33 }
34 
351simandl $matches=0;
36 
3785simandl print '<html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" media="screen" href="editor.css" /><script type="text/javascript" src="editor-resources/jquery-latest.min.js"></script><script src="editor.js" type="text/javascript"></script><title>PHP Weathermap Editor ' . $WEATHERMAP_VERSION
381simandl . '</title></head><body>';
39 
40 print '<div id="nojs" class="alert"><b>WARNING</b> - ';
41 print 'Sorry, it\'s partly laziness on my part, but you really need JavaScript enabled and DOM support in your browser to use this editor. It\'s a visual tool, so accessibility is already an issue, if it is, and from a security viewpoint, you\'re already running my ';
42 print 'code on your <i>server</i> so either you trust it all having read it, or you\'re already screwed.<P>';
43 print 'If it\'s a major issue for you, please feel free to complain. It\'s mainly laziness as I said, and there could be a fallback (not so smooth) mode for non-javascript browsers if it was seen to be worthwhile (I would take a bit of convincing, because I don\'t see a benefit, personally).</div>';
4413simandl 
45 $errormessage = "";
4685simandl 
47 if ($configerror!='') {
48 $errormessage .= $configerror.'<p>';
49 }
5013simandl 
5185simandl if (! $cacti_found && !$ignore_cacti) {
5213simandl $errormessage .= '$cacti_base is not set correctly. Cacti integration will be disabled in the editor.';
5385simandl if ($config_loaded != 1) {
54 $errormessage .= " You might need to copy editor-config.php-dist to editor-config.php and edit it.";
55 }
5613simandl }
57 
5885simandl if ($errormessage != '') {
5913simandl print '<div class="alert" id="nocacti">'.$errormessage.'</div>';
60 }
611simandl 
62 print '<div id="withjs">';
63 print '<div id="dlgStart" class="dlgProperties" ><div class="dlgTitlebar">Welcome</div><div class="dlgBody">';
64 print 'Welcome to the PHP Weathermap '.$WEATHERMAP_VERSION.' editor.<p>';
6585simandl print '<div style="border: 3px dashed red; background: #055; padding: 5px; font-size: 90%;"><b>NOTE:</b> This editor is not finished! There are many features of ';
661simandl print 'Weathermap that you will be missing out on if you choose to use the editor only.';
67 print 'These include: curves, node offsets, font definitions, colour changing, per-node/per-link settings and image uploading. You CAN use the editor without damaging these features if you added them by hand, however.</div><p>';
6813simandl 
691simandl print 'Do you want to:<p>';
70 print 'Create A New Map:<br>';
71 print '<form method="GET">';
72 print 'Named: <input type="text" name="mapname" size="20">';
73 
74 print '<input name="action" type="hidden" value="newmap">';
7585simandl print '<input name="plug" type="hidden" value="'.$fromplug.'">';
76 
771simandl print '<input type="submit" value="Create">';
78 print '</form>';
79 
80 $titles = array();
81 
8285simandl $errorstring="";
83 
84 if (is_dir($mapdir)) {
851simandl $n=0;
86 $dh=opendir($mapdir);
87 
8885simandl if ($dh) {
89 while (false !== ($file = readdir($dh))) {
901simandl $realfile=$mapdir . DIRECTORY_SEPARATOR . $file;
9185simandl $note = "";
921simandl 
9385simandl if ( (is_file($realfile)) && (is_readable($realfile)) && (!preg_match("/^\./",$file) ) ) {
94 if (!is_writable($realfile)) {
95 $note .= "(read-only)";
96 }
971simandl $title='(no title)';
98 $fd=fopen($realfile, "r");
9985simandl if ($fd) {
100 while (!feof($fd)) {
10113simandl $buffer=fgets($fd, 4096);
102 
10385simandl if (preg_match("/^\s*TITLE\s+(.*)/i", $buffer, $matches)) {
104 $title=$matches[1];
105 }
10613simandl }
107 
108 fclose ($fd);
109 $titles[$file] = $title;
11085simandl $notes[$file] = $note;
11113simandl $n++;
1121simandl }
113 }
114 }
115 
116 closedir ($dh);
11785simandl } else {
118 $errorstring = "Can't open mapdir to read.";
119 }
1201simandl 
121 ksort($titles);
122 
12385simandl if ($n == 0) {
124 $errorstring = "No files in mapdir";
125 }
126 } else {
127 $errorstring = "NO DIRECTORY named $mapdir";
128 }
129 
130 
131 print 'OR<br />Create A New Map as a copy of an existing map:<br>';
132 print '<form method="GET">';
133 print 'Named: <input type="text" name="mapname" size="20"> based on ';
134 
135 print '<input name="action" type="hidden" value="newmapcopy">';
136 print '<input name="plug" type="hidden" value="'.$fromplug.'">';
137 print '<select name="sourcemap">';
138 
139 if ($errorstring == '') {
140 foreach ($titles as $file=>$title) {
141 $nicefile = htmlspecialchars($file);
142 print "<option value=\"$nicefile\">$nicefile</option>\n";
143 }
144 } else {
145 print '<option value="">'.$errorstring.'</option>';
146 }
147 
148 print '</select>';
149 print '<input type="submit" value="Create Copy">';
150 print '</form>';
151 print 'OR<br />';
152 print 'Open An Existing Map (looking in ' . $mapdir . '):<ul class="filelist">';
153 
154 if ($errorstring == '') {
155 foreach ($titles as $file=>$title) {
1561simandl $title = $titles[$file];
15785simandl $note = $notes[$file];
158 $nicefile = htmlspecialchars($file);
159 print "<li>$note<a href=\"?mapname=$nicefile&plug=$fromplug\">$nicefile</a> - <span class=\"comment\">$title</span></li>\n";
1601simandl }
16185simandl } else {
162 print '<li>'.$errorstring.'</li>';
1631simandl }
164 
16585simandl print "</ul>";
1661simandl 
167 print "</div>"; // dlgbody
168 print '<div class="dlgHelp" id="start_help">PHP Weathermap ' . $WEATHERMAP_VERSION
16985simandl . ' Copyright &copy; 2005-2010 Howard Jones - howie@thingy.com<br />The current version should always be <a href="http://www.network-weathermap.com/">available here</a>, along with other related software. PHP Weathermap is licensed under the GNU Public License, version 2. See COPYING for details. This distribution also includes the Overlib library by Erik Bosrup.</div>';
1701simandl 
171 print "</div>"; // dlgStart
172 print "</div>"; // withjs
173 print "</body></html>";
174}
175 
176function snap($coord, $gridsnap = 0)
177{
17885simandl if ($gridsnap == 0) {
179 return ($coord);
180 } else {
181 return ($coord - ($coord % $gridsnap));
182 }
1831simandl}
184 
185// Following function is based on code taken from here:
186// http://uk2.php.net/manual/en/security.globals.php
187//
188// It extracts a set of named variables into the global namespace,
18985simandl// validating them as they go. Returns true or false depending on if
1901simandl// validation fails. If it does fail, then nothing is added to the
191// global namespace.
192//
19385simandlfunction extract_with_validation($array, $paramarray, $prefix = "", $debug = false)
1941simandl{
19585simandl $all_present=true;
196 $candidates=array( );
1971simandl 
19885simandl if ($debug) {
1991simandl print '<pre>';
20085simandl print_r ($paramarray);
2011simandl print_r ($array);
20285simandl }
2031simandl 
20485simandl foreach ($paramarray as $var) {
2051simandl $varname=$var[0];
206 $vartype=$var[1];
207 $varreqd=$var[2];
208 
20985simandl if ($varreqd == 'req' && !array_key_exists($varname, $array)) {
210 $all_present=false;
211 }
2121simandl 
21385simandl if (array_key_exists($varname, $array)) {
2141simandl $varvalue=$array[$varname];
215 
21685simandl if ($debug) {
2171simandl print "Checking $varname...";
21885simandl }
2191simandl 
220 $waspresent=$all_present;
221 
222 switch ($vartype)
223 {
224 case 'int':
22585simandl if (!preg_match('/^\-*\d+$/', $varvalue)) {
226 $all_present=false;
227 }
2281simandl 
229 break;
230 
231 case 'float':
23285simandl if (!preg_match('/^\d+\.\d+$/', $varvalue)) {
233 $all_present=false;
234 }
2351simandl 
236 break;
237 
238 case 'yesno':
23985simandl if (!preg_match('/^(y|n|yes|no)$/i', $varvalue)) {
240 $all_present=false;
241 }
2421simandl 
243 break;
244 
245 case 'sqldate':
24685simandl if (!preg_match('/^\d\d\d\d\-\d\d\-\d\d$/i', $varvalue)) {
247 $all_present=false;
248 }
2491simandl 
250 break;
251 
252 case 'any':
253 // we don't care at all
254 break;
255 
256 case 'ip':
25785simandl if (!preg_match( '/^((\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)(?:\.(\d|[1-9]\d|2[0-4]\d|25[0-5]|1\d\d)){3})$/', $varvalue)) {
258 $all_present=false;
259 }
2601simandl 
261 break;
262 
263 case 'alpha':
26485simandl if (!preg_match('/^[A-Za-z]+$/', $varvalue)) {
265 $all_present=false;
266 }
2671simandl 
268 break;
269 
270 case 'alphanum':
27185simandl if (!preg_match('/^[A-Za-z0-9]+$/', $varvalue)) {
272 $all_present=false;
273 }
2741simandl 
275 break;
276 
277 case 'bandwidth':
27885simandl if (!preg_match('/^\d+\.?\d*[KMGT]*$/i', $varvalue)) {
279 $all_present=false;
280 }
2811simandl 
282 break;
283 
284 default:
285 // an unknown type counts as an error, really
28685simandl $all_present=false;
2871simandl 
288 break;
289 }
290 
29185simandl if ($debug && $waspresent != $all_present) {
292 print "Failed on $varname.";
293 }
2941simandl 
29585simandl if ($all_present) {
2961simandl $candidates["{$prefix}{$varname}"]=$varvalue;
297 $candidates["{$prefix}{$varname}_slashes"]=addslashes($varvalue);
298 $candidates["{$prefix}{$varname}_url"]=urlencode($varvalue);
299 $candidates["{$prefix}{$varname}_html"]=htmlspecialchars($varvalue);
300 $candidates["{$prefix}{$varname}_url_html"]=htmlspecialchars(urlencode($varvalue));
301 }
30285simandl } else {
303 if ($debug) {
3041simandl print "Skipping $varname\n";
30585simandl }
3061simandl }
307 }
308 
30985simandl if ($debug) {
3101simandl print_r ($candidates);
31185simandl }
3121simandl 
31385simandl if ($all_present) {
314 foreach ($candidates as $key => $value) {
315 $GLOBALS[$key]=$value;
316 }
3171simandl }
318 
31985simandl if ($debug) {
3201simandl print '</pre>';
32185simandl }
3221simandl 
323 return ($all_present);
324}
325 
326function get_imagelist($imagedir)
327{
328 $imagelist = array();
329 
33085simandl if (is_dir($imagedir)) {
3311simandl $n=0;
332 $dh=opendir($imagedir);
333 
33485simandl if ($dh) {
335 while ($file=readdir($dh)) {
3361simandl $realfile=$imagedir . DIRECTORY_SEPARATOR . $file;
337 $uri = $imagedir . "/" . $file;
338 
33985simandl if (is_file($realfile) && ( preg_match('/\.(gif|jpg|png)$/i',$file) )) {
3401simandl $imagelist[] = $uri;
341 $n++;
342 }
343 }
344 
345 closedir ($dh);
346 }
347 }
348 return ($imagelist);
349}
35013simandl 
351function handle_inheritance(&$map, &$inheritables)
352{
35385simandl foreach ($inheritables as $inheritable) {
35413simandl $fieldname = $inheritable[1];
355 $formname = $inheritable[2];
356 
357 $new = $_REQUEST[$formname];
358 
35985simandl $old = ($inheritable[0]=='node' ? $map->nodes['DEFAULT']->$fieldname : $map->links['DEFAULT']->$fieldname);
36013simandl 
36185simandl if ($old != $new) {
362 if ($inheritable[0]=='node') {
363 $map->nodes['DEFAULT']->$fieldname = $new;
364 foreach ($map->nodes as $node) {
365 if ($node->name != ":: DEFAULT ::" && $node->$fieldname == $old) {
36613simandl $map->nodes[$node->name]->$fieldname = $new;
367 }
368 }
369 }
370 
37185simandl if ($inheritable[0]=='link') {
372 $map->links['DEFAULT']->$fieldname = $new;
373 foreach ($map->links as $link) {
374 
375 if ($link->name != ":: DEFAULT ::" && $link->$fieldname == $old) {
37613simandl $map->links[$link->name]->$fieldname = $new;
377 }
378 }
379 }
380 }
381 }
382}
383 
384function get_fontlist(&$map,$name,$current)
385{
38685simandl $output = '<select class="fontcombo" name="'.$name.'">';
387 
388 ksort($map->fonts);
389 
390 foreach ($map->fonts as $fontnumber => $font) {
391 $output .= '<option ';
392 if ($current == $fontnumber) {
393 $output .= 'SELECTED';
394 }
395 $output .= ' value="'.$fontnumber.'">'.$fontnumber.' ('.$font->type.')</option>';
396 }
397 
398 $output .= "</select>";
399 
400 return($output);
40113simandl}
402 
40385simandlfunction editor_log($str)
404{
405 // $f = fopen("editor.log","a");
406 // fputs($f, $str);
407 // fclose($f);
408}
409 
4101simandl// vim:ts=4:sw=4:
411?>

Powered by WebSVN 2.2.1