This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.php
executable file
·614 lines (501 loc) · 16.5 KB
/
graph.php
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
/*
* AMP Data Display Interface
*
* Graph display page
*/
require("amplib.php");
/*********************************************************************/
/**** Initialiase the AMP display system ****/
initialise();
/**** Page Specific Setup ****/
/* Page Settings Stored in Session */
if ( isset($_REQUEST["reset"]) ) {
/* Resetting, page needs default settings */
unset($_SESSION["page_settings"]);
session_unregister("page_settings");
$page_settings = array();
$_SESSION["page_settings"] = $page_settings;
} else if(isset($_POST["do"])) {
/* Extract settings out of session and store new ones */
$page_settings = $_SESSION["page_settings"];
if ( isset($_POST["form"]) ) {
$form = $_POST["form"];
$prefix = $_POST["form_prefix"];
$page_settings[$form] = array();
foreach ( $_POST as $key=>$value ) {
if ( strncmp($key, $prefix, strlen($prefix)) != 0 ) {
continue;
}
$page_settings[$form][$key] = $value;
}
}
$_SESSION["page_settings"] = $page_settings;
} else {
/* no form being submitted, use settings as they are */
global $page_settings;
if ( isset($_SESSION["page_settings"]) ) {
$page_settings = $_SESSION["page_settings"];
} else {
$_SESSION["page_settings"] = $page_settings;
}
}
/* Long names for src/dst */
$siteDb = dbconnect($GLOBALS["sitesDB"]);
if($siteDb) {
$srcName = getLongSiteName($siteDb, $src);
$dstName = getLongSiteName($siteDb, $dst);
dbclose($siteDb);
} else {
// if we can't open the db, just use the shortnames
$srcName = $src;
$dstName = $dst;
}
/* Date for page to display data from */
$timeZone = get_preference(PREF_GLOBAL, GP_TIMEZONE, PREF_GLOBAL);
$res = timeInZone($timeZone);
$timeInZone = $res->time;
unset($res);
// we can't use any functions that deal in localtime unless we set
// ourselves to be in the correct timezone
putenv("TZ=$timeZone");
/*
* If date isn't set, make it todays date.
* Using mktime we can find the UTC timestamp at the beginning of the day
*/
if(isset($_REQUEST["date"]) && $_REQUEST["date"] != "") {
$parts = explode("-", $_REQUEST["date"]);
$startTimeSec = mktime(0, 0, 0, $parts[1], $parts[2], $parts[0], -1);
} else {
$parts = getdate(strtotime("now"));
$startTimeSec = mktime(0, 0, 0,
$parts{'mon'}, $parts{'mday'}, $parts{'year'}, -1);
}
/*********************************************************************/
/* Time frame and range of graphs being shown */
$pageClass = PREF_SHORTTERM;
if(isset($_REQUEST["rge"]))
{
$tmp = explode("-", $_REQUEST["rge"]);
$duration = $tmp[0];
$xAxisType = $tmp[1];
// anything longer than a day is considered longterm
switch($xAxisType)
{
case "day":
$pageClass = PREF_SHORTTERM;
$secOfData = 86400;
break;
case "week":
$pageClass = PREF_LONGTERM;
$secOfData = 604800;
// if this isn't sunday localtime then take off as many days
// as we need to find the start of the week (day 0)
$wdayLocal = (int)date("w", $startTimeSec);
$startTimeSec -= $wdayLocal * 86400;
$periodStart = getdate($startTimeSec);
break;
case "month":
$pageClass = PREF_LONGTERM;
$secOfData = 86400 * date("t", $startTimeSec);
// if this isn't day 1 of the month localtime, then take off as
// many days as we need to find the start of the month
$mdayLocal = (int)date("j", $startTimeSec);
$startTimeSec -= ($mdayLocal -1 ) * 86400;
$periodStart = getdate($startTimeSec);
break;
default:
// for now, just use a day
$pageClass = PREF_SHORTTERM;
$secOfData = 86400;
break;
};
// if we've changed the start time based on the period we are interested in,
// then we need to update the start timestamp (UTC)
if(isset($periodStart[0]))
$startTimeSec = $periodStart[0];
}
else
{
$duration = 1;
$pageClass = PREF_SHORTTERM;
$xAxisType = "day";
$secOfData = 86400;
}
// how many of what period are we looking at each page?
$navigate = $duration . " " . $xAxisType;
if($duration > 1)
$navigate .= "s";
/**************************************************************/
/* store all the bits of the url that we want to use elsewhere */
// if graph is set, then we care about the options for it.
// if its not set, then the only options we can really use are the
// ones in page_settings, so just leave the optstring here blank
if(isset($_REQUEST["graph"])) {
$graphstring = "&graph=" . urlencode($_REQUEST["graph"]);
$optstring = "";
} else {
$graphstring = "";
if(isset($_REQUEST["opts"]))
$optstring = "&opts=" . urlencode($_REQUEST["opts"]);
else
$optstring = "";
}
if(isset($_REQUEST["ymax"]))
$ystring = "&ymax=" . urlencode($_REQUEST["ymax"]);
else
$ystring = "";
// this has to have a value or it breaks the hidden range input
// i dont think i thought this through very well
if(isset($_REQUEST["rge"]))
$rgestring = "&rge=" . urlencode($_REQUEST["rge"]);
else
$rgestring = "&rge=1-day";
/***********************************************************/
/* Build the list of display items for the page */
$sources = expandSites($src);
$destinations = expandSites($dst);
foreach($sources as $source) {
foreach($destinations as $destination) {
if ( $source == $destination )
continue;
initialise_display_items($source, $destination, $startTimeSec);
}
}
/**** HTML From Here ON ****/
templateTop();
echo "<script type='text/javascript' language='javascript'>";
echo "var options = new Array();";
echo "</script>";
//Opera insists on not reloading the image from the server, so this is needed
//to force it to do so
if ( strstr($_SERVER["HTTP_USER_AGENT"], "Opera") ) {
if ( !isset($_GET["reload"]) ) {
echo "<script type=\"text/javascript\">\n";
if ( !empty($_GET) ) {
$addText = "&reload=1";
} else {
$addText = "?reload=1";
}
echo "location.replace(location.href + \"$addText\")\n";
echo "</script>";
} else {
unset($_GET["reload"]);
}
}
/*********************************************************************/
/* Page heading */
echo '<h2 class="dataheading">'.htmlspecialchars($srcName).' to '.htmlspecialchars($dstName).'</h2>'."\n";
/*********************************************************************/
/* Navigate by source and destination */
echo "<div><b>View</b> ";
echo "<a href=\"src.php\">other sources</a> ";
echo '| <a href="src.php?src='.urldecode($src).'">other ' .
'destinations for '.htmlspecialchars($src).'</a>';
/* Add reverse link if available */
$dstr = "&date=" . date("Y-m-d", $startTimeSec);
displayReverseLink($src, $dst, $dstr,
$graphstring . $rgestring . $ystring, $optstring);
echo "</div>";
/**********************************************************************/
/* select time frame shown */
$ranges = array(
"day" => "Day(s)",
"week" => "Week(s)",
"month" => "Month(s)",
//"year" => "Year(s)",
);
if(isset($_REQUEST["graph"]))
{
$id = 'updatelink';
$updateFunction = "updateTime";
}
else
{
// nothing else has a name like this, so while horribly overused
// in places, this works as an id for an input field/link
$id = 'rge';
$updateFunction = "updateAllTimes";
}
/* display time period select options */
echo "Show ";
echo "<input type='text' size=1 maxlength=1 id='duration' " .
"value=$duration onchange='$updateFunction(\"$id\")'/> ";
echo "<select id='range' onchange='$updateFunction(\"$id\")'>";
foreach($ranges as $value=>$display)
{
echo '<option value="'.htmlspecialchars($value).'"';
if($xAxisType == $value)
echo " SELECTED";
echo '>'.htmlspecialchars($display);
}
echo "</select>";
echo " worth of data: ";
// remove any mention of rge from the url we use in this form
// so that it can be set properly later
$formurl = preg_replace("/&rge=[\w-]*/", "", $page_name);
echo "<form action=\"$formurl\" method=\"post\" class=\"graphoptions\">\n";
echo "<input type='submit' class='graphoptionssubmit' " .
"value='Refresh Graph >>' >\n";
// 'do' needs to be set to get our page_settings to carry over?
echo "<input type='hidden' name='do' value='yes'>\n";
echo "<input type='hidden' name='rge' value='" .
(isset($_REQUEST["rge"])?($_REQUEST["rge"]):"1-day") . "' id='rge' />\n";
echo "</form>";
echo "<br />";
/*********************************************************************/
/* Navigate by date */
echo "<br />";
echo "<div>\n";
/* go LEFT */
echo '<a href="graph.php?src='.urlencode($src).'&dst='.urlencode($dst) .
$graphstring . $optstring . $ystring . $rgestring .
"&date=" . date("Y-m-d", strtotime("-$navigate", $startTimeSec)) .
"\"><<" .
" go back " . htmlspecialchars($navigate) . "</a>";
/* go RIGHT */
// if the current time now is less than the end of the period being
// viewed there can be no more data to the right
if (($startTimeSec + $secOfData) > time()) {
/* Use CSS to hide the link rather than not outputting it so that
* centering of the title looks ok, when there are some graphs with
* the link and some without
*/
$s = " style=\"visibility: hidden;\"";
} else {
echo " || ";
$s = "";
}
echo '<a href="graph.php?src='.urlencode($src).'&dst='.urlencode($dst) .
$graphstring . $optstring . $ystring . $rgestring .
"&date=" . date("Y-m-d", strtotime("+$navigate", $startTimeSec)) .
"\"$s>" .
" go forward " . htmlspecialchars($navigate) .
" >></a>";
echo "</div>\n";
/********************************************************************/
/*
* loop over all the different types of graphs, and display n of each
* graph as asked for by the time period select options
*/
$pageStart = $startTimeSec;
$c=0;
foreach ( $display_items as $key=>$ditem ) {
$startTimeSec = $pageStart;
// if graphtype is set in the url, then allow this to override any
// preferences the user may have saved already, otherwise we just
// draw what is in their preferences
if(isset($_REQUEST["graph"]))
{
// if graphtype is set, display only this graph
if($key != $_REQUEST["graph"])
continue;
}
else
{
/* Check if this object is to be displayed */
$object = get_display_object($ditem->displayObject);
$d = is_item_displayed($ditem->name, $object->displayPref, $pageClass);
if ( ! $d ) {
continue;
}
}
/******************************************************************/
for ( $displaycount=0; $displaycount<$duration; $displaycount++ ) {
$imagemap = "";
// convert timestamp into localtime for display
$graphDate = strftime("%Y-%m-%d", $startTimeSec);
if($xAxisType == "week") {
/* display links to daily pages */
for ( $day=6; $day>=0; --$day ) {
if ( ($startTimeSec + ($day * 86400)) > $timeInZone ) {
continue;
}
/* TODO: Check that data for the day is actually available */
// convert timestamp into localtime for link targets
$link = "graph.php?src=$src&dst=$dst&date=" .
strftime("%Y-%m-%d", $startTimeSec + ($day * 86400)) .
$graphstring . $optstring . $ystring;
$links[$day] = $link;
} //create link for each day
unset($day);
$imagemap = printWeeklyMap($graphDate, $links);
} else if($xAxisType == "month") {
/* display links to daily pages */
for ( $day=date("t", $startTimeSec); $day>=0; --$day ) {
if ( ($startTimeSec + ($day * 86400)) > $timeInZone ) {
continue;
}
/* TODO: Check that data for the day is actually available */
// convert timestamp into localtime for link targets
$link = "graph.php?src=$src&dst=$dst&date=" .
strftime("%Y-%m-%d", $startTimeSec + ($day * 86400)) .
$graphstring . $optstring . $ystring;
$links[$day] = $link;
} //create link for each day
unset($day);
$imagemap = printMonthlyMap($graphDate, $links);
}
/******************************************************************/
/* Display it */
item_display($ditem->name, $startTimeSec, $secOfData, $graphDate,
$xAxisType, $pageClass, $imagemap, $displaycount);
// go back one period to draw the next graph
$startTimeSec = strtotime("-1 $xAxisType", $startTimeSec);
// update secOfData ony if we are changing months
if($xAxisType == "month")
$secOfData = 86400 * date("t", $startTimeSec);
}
$c++;
}
if ( $c == 0 ) {
graphError("No Graphs Selected!");
}
/* Finish System, Close HTML */
endPage();
?>
<script type="text/javascript" language="javascript">
//var options = new Array();
function updateAllTimes(which)
{
//alert(which);
var links = document.getElementsByName(which);
var rangeInput = document.getElementById("range");
var durationInput = document.getElementById("duration");
var range_string;
// work out our new range
range_string = durationInput.value + "-" + rangeInput.value;
// update all the hidden_rge input fields with this value
for(var i=0; i<links.length; i++) {
links[i].value = range_string;
}
}
function updateTime(which)
{
<?
/*
if(isset($_REQUEST["graph"]))
echo " var link = document.getElementById(which);\n";
else
echo " var link = document.getElementsByName(which);\n";
*/
?>
var link = document.getElementById(which);
var rangeInput = document.getElementById("range");
var durationInput = document.getElementById("duration");
var range_string;
var old_url;
// work out our new range
range_string = durationInput.value + "-" + rangeInput.value;
// grab the parts of the old url so we can keep
// everything else the same
old_url = link.href;
parts = old_url.split("\&");
link.href = parts[0];
// put all the parts back together, but change the option
// value to the new one
for(i=1; i<parts.length; i++)
{
if(parts[i].substr(0, 3) == "rge")
link.href += "&rge=" + range_string;
else
link.href += "&" + parts[i];
}
// update the link by the time options too
var hidden_link = document.getElementById("rge");
hidden_link.value = range_string;
}
function updateOptions(which, option, value)
{
//var link = document.getElementById("updatelink");
var link = document.getElementById(which);
var hidden_opts = document.getElementById("hidden_" + which);
var old_url;
var base;
var parts;
var newopt;
var option_string;
var i;
// set the changed value in our array
//options[option] = value;
options[which][option] = value;
// calculate the binary value of all the options
// could do this with one step seeing as we know
// only one option has changed, and which one it is
//base = options.length-1;
base = 3;
newopt = 65;
option_string = "";
for(i=0; i<options[which].length; i++)
{
if(options[which][i] == true)
{
//alert("increasing by" + Math.pow(2, base-i));
newopt += Math.pow(2, base-i);
//alert(newopt);
}
// every 4 bits we turn into a character
if( (i+1) % 4 == 0)
{
//alert("building char");
option_string += String.fromCharCode(newopt);
newopt = 65;
base += 4;
}
}
// if we haven't just made a character, use up the last of
// the options value to make a final one
if( (i) % 4 != 0)
{
//alert("building char2");
option_string += String.fromCharCode(newopt);
}
// grab the parts of the old url so we can keep
// everything else the same
old_url = link.href;
parts = old_url.split("\&");
link.href = parts[0];
// put all the parts back together, but change the option
// value to the new one
for(i=1; i<parts.length; i++)
{
if(parts[i].substr(0, 4) == "opts")
link.href += "&opts=" + option_string;
else
link.href += "&" + parts[i];
}
if(hidden_opts != null)
hidden_opts.value = option_string;
}
function updateTextfields(which, option, value)
{
var link = document.getElementById(which);
var hidden_opts = document.getElementById("hidden_" + which + "_ymax");
var old_url;
var parts;
// lets just hard code this for now cause its shorter
option = "ymax";
// grab the parts of the old url so we can keep
// everything else the same
old_url = link.href;
parts = old_url.split("\&");
link.href = parts[0];
// put all the parts back together, but change the option
// value to the new one
for(i=1; i<parts.length; i++)
{
if(parts[i].substr(0, option.length) == option)
link.href += "&" + option + "=" + value;
else
link.href += "&" + parts[i];
}
if(hidden_opts != null)
hidden_opts.value = value;
}
</script>
<?
// Emacs Control
// Local Variables:
// eval: (c++-mode)
// vim:set sw=2 ts=2 sts=2 et:
?>