Skip to content
Snippets Groups Projects
Commit 2e651ee3 authored by Kevin Abel's avatar Kevin Abel
Browse files

Convert the scripts to use the new ArrayObject for generating CAP Alert

parent 45f66d7d
Branches main
No related tags found
1 merge request!1Implement RAVE RSS feed a source of proxied alerts
<?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';
<?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);
<?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);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment