#!/usr/bin/perl use Modern::Perl qw(2014); use DBD::mysql; use JSON; use CGI::Carp qw(fatalsToBrowser); my @output; my $cgi = CGI->new; my %params = $cgi->Vars; my $params = \%params; my $json = JSON::XS->new(); $|=1; my $q; if ( ! $params->{'start'}) { $q = qq(select * from exhibit where timestamp > subtime(now(), "24:0:0.0")); $params->{'interval'} = qq(24:0:0.0); } else { $q = qq(select * from exhibit where timestamp >= '$params{'start'}' and timestamp <= '$params{'end'}'); } my $db = "breakins"; my $host = "localhost"; #"192.168.0.3"; my $user = "xxx"; my $password = "xxxxxxx"; my $dsn = "DBI:mysql:database=$db;host=$host;port=3306"; my $dbh = DBI->connect($dsn, $user, $password ); # the query to the db is determined above using times passed in by the web page # in this case, I do a bit of modification to individual rows as they are put in @output so data is in the specific json format required # also add fields to each row which I don't want to store in the database as they are redundant. my $sth = $dbh->prepare($q); $sth->execute; while ( my $row = $sth->fetchrow_hashref ){ $row->{'label'} = $row->{'timestamp'} . "-" . $row->{'IP'}; $row->{'display'} = 'true'; $row->{'recurring'} = 'false'; $row->{'color'} = 'FFEEBB'; $row->{'timestamp'} = iso_time($row->{'timestamp'}); push @output, $row; } $dbh->disconnect; # Make sure that the resultant JSON will print out all nice and pretty $json->pretty(1); #or condensed if this is set to 0 # $prettyJSON will contain the text string which is the Oh-So-Pretty version of $jsonStructure my $prettyJson = $json->encode(\@output); print $cgi->header(-type=>'application/json', -rel=>'exhibit-data'); print qq({\n"items":); print $prettyJson; print qq(}\n); sub getdate { my $ts = shift; my $arg = shift; my ($date, $time) = split('T', $ts); if ($arg eq 'd') { return $date; } else { return $time; } } sub iso_time { my $t = shift; my @tp = split(' ', $t); my $ret = join('T', @tp); return $ret; } =pod startDate: Date in which your activity starts. If it is a recurring event, then all dates within the startDate/endDate range may display the event. If your event is not recurring, make the startDate and endDate the same. endDate: The ending date for your event. startTime: The time your event starts. endTime: The time your event ends. display: Whether the calendar will display your event. Enter either "true" or "false". label: The label for your event. This will be displayed on the calendar within each box for each event. description: Your event description. recurring: If your event is recurring, list the days of the week it occurs on. Use "M", "T", "W", "R", "F", "S", and "Sn" for "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", and "Sunday", respectively. color: The background color of your event. Please enter a hex, rgb, or color name. =cut