diff --git a/scripts/functions.inc.php b/scripts/functions.inc.php index 2db3a90471755d6cb9bc70f179c07f8ba5bdcf32..c5bc37aaefd1dd2c0f03ef4b9c51df39aabf1506 100644 --- a/scripts/functions.inc.php +++ b/scripts/functions.inc.php @@ -1,52 +1,19 @@ <?php -function getCAPData() +function getCAPData($config) { try { - $alerts = new UNL_Alert_RaveRSS('http://www.getrave.com/rss/unl/channel1'); - $filtered = new UNL_Alert_RecentPubDateFilter($alerts->getIterator()); + $url = isset($config['proxyUrl']) ? $config['proxyUrl'] :'http://www.getrave.com/rss/unl/channel1'; + $alerts = new UNL_Alert_RaveRSS($url); - $filtered->rewind(); - if ($filtered->valid()) { - $item = $filtered->current(); - $isTest = false; - if (stripos($item->description, 'test')) { - $isTest = true; - } - - $pubDate = (string) $item->children('dc', true)->date; - $pubDateTime = new DateTime($pubDate); - - $output = array( - 'identifier' => md5($pubDate), - 'sender' => 'police.unl.edu', - 'sent' => $pubDateTime->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/', - ); - } + if (!empty($config['recentTimeframe'])) { + $filtered = new UNL_Alert_RecentPubDateFilter($alerts->getIterator(), $config['recentTimeframe']); } else { - $output = array(); + $filtered = new UNL_Alert_RecentPubDateFilter($alerts->getIterator()); } + + $data = new UNL_Alert_RSSToCAP($filtered); + $output = $data->getData(); } catch (Exception $e) { $output = array(); } diff --git a/src/UNL/Alert/RSSToCAP.php b/src/UNL/Alert/RSSToCAP.php new file mode 100644 index 0000000000000000000000000000000000000000..0b244be90140e30b9f6088635a52cc9cdb6b5c78 --- /dev/null +++ b/src/UNL/Alert/RSSToCAP.php @@ -0,0 +1,58 @@ +<?php + +class UNL_Alert_RSSToCAP +{ + protected $iterator; + + public function __construct(Iterator $items) + { + $this->iterator = $items; + } + + public function getData() + { + $output = array(); + $this->iterator->rewind(); + + if ($this->iterator->valid()) { + $item = $this->iterator->current(); + $isTest = false; + if (stripos($item->description, 'test')) { + $isTest = true; + } + + $pubDate = (string) $item->children('dc', true)->date; + $pubDateTime = new DateTime($pubDate); + + $output = array( + 'identifier' => md5($pubDate), + 'sender' => 'police.unl.edu', + 'sent' => $pubDateTime->format(DateTime::ATOM), + 'status' => $isTest ? 'Test' : 'Actual', + 'msgType' => 'Alert', + 'scope' => 'Public', + ); + + if ($isTest) { + $output['note'] = 'This is only a test'; + } + + $output['info'] = array(); + + foreach ($this->iterator 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/', + ); + } + } + + return $output; + } +}