From 2e651ee3fd5db5b8b5046b1ae0dd9fb848d3df6a Mon Sep 17 00:00:00 2001 From: Kevin Abel <kevin.abel.0@gmail.com> Date: Wed, 11 Jun 2014 10:33:03 -0500 Subject: [PATCH] Convert the scripts to use the new ArrayObject for generating CAP Alert --- scripts/exportCAPAlert.php | 61 +++---------------------------------- scripts/exportJSONAlert.php | 20 +++++------- scripts/functions.inc.php | 52 +++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 69 deletions(-) create mode 100644 scripts/functions.inc.php diff --git a/scripts/exportCAPAlert.php b/scripts/exportCAPAlert.php index d792395..e211999 100644 --- a/scripts/exportCAPAlert.php +++ b/scripts/exportCAPAlert.php @@ -1,61 +1,8 @@ <?php require_once dirname(__FILE__).'/../config.inc.php'; +require_once 'functions.inc.php'; -function exportCAPAlert() -{ +$output = getCAPData(); +file_put_contents(dirname(__FILE__).'/../www/xml/unlcap.xml', $output->toXML('alert', 'urn:oasis:names:tc:emergency:cap:1.2')); - $output = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?> -<alert xmlns:cap="http://www.incident.com/cap/1.0"> - <identifier>University of Nebraska-Lincoln '.date('Y-m-d\TH:i:sO').'</identifier> - <sender>police.unl.edu</sender> - <sent>'.date('Y-m-d\TH:i:sO').'</sent> - <status>Actual</status> - <msgType>Alert</msgType> - <scope>Public</scope> - <note> - Current Watches, Warnings and Advisories for UNL Issued by University Police - </note> - <references> - http://www.unl.edu/ - </references>'; - try { - // Get the RSS feed - $alerts = new UNL_Alert_TwitterAlert(); - - // Create the filter - $filtered = new UNL_Alert_RecentPubDateFilter($alerts); - foreach ($filtered as $item) { - $effective = DateTime::createFromFormat('U', strtotime($item->created_at)); - $effective->setTimezone(new DateTimeZone(date_default_timezone_get())); - $output .= ' - <info> - <category>Safety</category> - <event>University of Nebraska-Lincoln Alert</event> - <urgency>Immediate</urgency> - <severity>Extreme</severity> - <certainty>Likely</certainty> - <headline>UNL ALERT</headline> - <description>'.$item->text.'</description> - <effective>'.date('Y-m-d\TH:i:sO', $effective->getTimestamp()).'</effective> - <web>http://www.unl.edu/</web> - <parameter> - <valueName>id</valueName> - <value>'.md5($item->id).'</value> - </parameter> - <area> - <areaDesc>Lincoln (Nebraska)</areaDesc> - <geocode>031111</geocode> - </area> - </info>'; - } - } catch (Exception $e) { - echo $e; - } - $output .= PHP_EOL.'</alert>'.PHP_EOL; - return $output; -} -$xmlCAP = exportCAPAlert(); - -file_put_contents(dirname(__FILE__).'/../www/xml/unlcap.xml', $xmlCAP); - -require dirname(__FILE__).'/exportJSONAlert.php'; +include dirname(__FILE__).'/exportJSONAlert.php'; diff --git a/scripts/exportJSONAlert.php b/scripts/exportJSONAlert.php index 5d259cc..aabc05f 100644 --- a/scripts/exportJSONAlert.php +++ b/scripts/exportJSONAlert.php @@ -1,22 +1,18 @@ <?php -require_once dirname(__FILE__).'/../src/xml2json/xml2json.php'; +require_once dirname(__FILE__).'/../config.inc.php'; +require_once 'functions.inc.php'; -$file = dirname(__FILE__).'/../www/xml/unlcap.xml'; +if (!isset($output)) { + $output = getCAPData(); +} -//Read the XML alert info from the file. -file_exists($file) or die('Could not find file '); -$xmlStringContents = file_get_contents($file); -$jsonContents = ''; -$nl = "\n"; -// Convert it to JSON now. -// xml2json simply takes a String containing XML contents as input. -$jsonContents = xml2json::transformXmlStringToJson($xmlStringContents); +$jsonContents = $output->toJSON('alert'); -$jsonContents = 'unlAlerts.data = '.$jsonContents.$nl; +$jsonContents = 'unlAlerts.data = ' . $jsonContents . "\n"; $jsonContents .= 'try { unlAlerts.server.init(); -} catch(e) {}'.$nl; +} catch(e) {}'."\n"; file_put_contents(dirname(__FILE__).'/../www/json/unlcap.js', $jsonContents); diff --git a/scripts/functions.inc.php b/scripts/functions.inc.php new file mode 100644 index 0000000..ec9c5b8 --- /dev/null +++ b/scripts/functions.inc.php @@ -0,0 +1,52 @@ +<?php + +function getCAPData() +{ + try { + $alerts = new UNL_Alert_RaveRSS('http://www.getrave.com/rss/unl/channel1'); + $filtered = new UNL_Alert_RecentPubDateFilter($alerts->getIterator()); + + $filtered->rewind(); + if ($filtered->valid()) { + $item = $filtered->current(); + $isTest = false; + if (stripos($item->description, 'test')) { + $isTest = true; + } + + $output = array( + 'identifier' => md5((string) $item->children('dc', true)->date), + 'sender' => 'police.unl.edu', + 'sent' => (new DateTime((string) $item->children('dc', true)->date))->format(DateTime::ATOM), + 'status' => $isTest ? 'Test' : 'Actual', + 'msgType' => 'Alert', + 'scope' => 'Public', + ); + + if ($isTest) { + $output['note'] = 'This is only a test'; + } + + $output['info'] = array(); + + foreach ($filtered as $item) { + $output['info'][] = array( + 'category' => 'Safety', + 'event' => 'UNL Alert', + 'ugency' => 'Immediate', + 'severity' => 'Extreme', + 'certainty' => 'Observed', + 'headline' => (string) $item->title, + 'description' => (string) $item->description, + 'web' => 'http://www.unl.edu/', + ); + } + } else { + $output = array(); + } + } catch (Exception $e) { + $output = array(); + } + + return new UNL_Alert_ArrayObject($output); +} -- GitLab