Tom Pfeiffer Author URI: http://www.volcanodiscovery.com License: GNU General Public License, version 2 */ /* Copyright 2012 Tom Pfeiffer (email : info [dot] volcanodiscovery [dot] com) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ class AdvancedEarthquakeMonitor extends WP_Widget { private $timesCalled=0; function AdvancedEarthquakeMonitor() { $widget_ops = array('classname' => 'widget_AdvancedEarthquakeMonitor', 'description' => __( 'Show list and map of earthquakes'),'name'=>'Advanced Earthquake Monitor' ); parent::WP_Widget('AdvancedEarthquakeMonitor', __('AdvancedEarthquakeMonitor'), $widget_ops); } function form( $instance ) { $this->timesCalled++; $instance = wp_parse_args( (array) $instance, array( 'region' => '0', 'lat1' => '', 'lat2' => '', 'lon1' => '', 'lon2' => '', 'minmag' => '', 'noearthquakes' => 'No Earthquakes', 'dateformat' => 'D H:i:s (T)', 'displayformat' => '{time} {mag},{loc} - {details}', 'showonmaptext' => 'Show on map', 'detaillinktext' => 'Details', 'showtitle' => true , 'customtitle' => '', 'show' => 20, 'trim' => 24, 'linkable' => true , 'sourcelinkable' => true , 'newwindow' => true , 'liststyle' => '.advancedearthquakemonitor-class0{font-size:90%} .advancedearthquakemonitor-class1{font-size:90%} .advancedearthquakemonitor-class2{} .advancedearthquakemonitor-class3{} .advancedearthquakemonitor-class4{} .advancedearthquakemonitor-class5{font-weight:bold} .advancedearthquakemonitor-class6{font-weight:bold;color:red} .advancedearthquakemonitor-class7{font-size:110%;font-weight:bold;color:red} .advancedearthquakemonitor-class8{font-size:120%;font-weight:bold;color:red} .advancedearthquakemonitor-class9{font-size:130%;font-weight:bold;color:red}', )); $noearthquakes = esc_attr($instance['noearthquakes']); $dateformat = esc_attr($instance['dateformat']); $showonmaptext = esc_attr($instance['showonmaptext']); $detaillinktext = esc_attr($instance['detaillinktext']); $region = intval($instance['region']); $lat1 = esc_attr($instance['lat1']); $lat2 = esc_attr($instance['lat2']); $lon1 = esc_attr($instance['lon1']); $lon2 = esc_attr($instance['lon2']); $trim = absint($instance['trim']); $minmag = esc_attr($instance['minmag']); $showtitle = (bool) $instance['showtitle']; $customtitle = esc_attr($instance['customtitle']); $linkable = (bool) $instance['linkable']; $sourcelinkable = (bool) $instance['sourcelinkable']; $newwindow = (bool) $instance['newwindow']; $displayformat = esc_attr($instance['displayformat']); $liststyle = esc_attr($instance['liststyle']); $show = absint($instance['show']); if ( $show < 1 || 100 < $show ) { $show = 20; } /* JS show hide script */ $js = ' function aem_sh(el) { if (document.getElementById(el).style.display == "none") {document.getElementById(el).style.display = "";} else {document.getElementById(el).style.display = "none";}; if (document.getElementById(el+"_1") !=null) {if (document.getElementById(el+"_1").style.display == "none") {document.getElementById(el+"_1").style.display ="";} else {document.getElementById(el+"_1").style.display = "none";};} } function populateSelector(id,arr,curId) { if (typeof(arr)=="undefined") return; var sel=document.getElementById(id); for(i=0;itimesCalled==1) echo (''); if ($this->timesCalled==1) echo (''); /* Show Title */ echo "

"; /* Text for Custom Title */ //echo "

"; echo "

"; echo '

Basic configuration (show/hide)

'; echo '
'; /* Earthquake count */ echo "

"; echo "

"; /* min mag */ echo "

"; echo "

"; /* select specific predefined region */ echo "

"; echo '

"; echo ' '; /* Lon 1-2 */ echo "

West/East limits (values from -180 to 180)"; echo " /

"; /* Lat 1-2 */ echo "

South/North limits (-90 to 90)"; echo " /

"; // echo '

Want to show the earthquake list on a map - look at the VolcanoWidget plugin!

'; echo '
'; echo '

Labels (show/hide)

'; echo ''; echo '

Advanced configuration (show/hide)

'; echo ''; echo '

Formatting (show/hide)

'; echo ''; } function update( $new_instance, $old_instance ) { $instance = $old_instance; $instance['noearthquakes'] = trim( strip_tags( stripslashes( $new_instance['noearthquakes'] ) ) ); $instance['dateformat'] = trim ($new_instance['dateformat']); $instance['customtitle'] = trim( strip_tags( stripslashes( $new_instance['customtitle'] ) ) ); $instance['show'] = absint($new_instance['show']); $instance['trim'] = absint($new_instance['trim']); $instance['showtitle'] = isset($new_instance['showtitle']); $instance['showonmaptext'] = trim( strip_tags( stripslashes( $new_instance['showonmaptext'] ) ) ); $instance['detaillinktext'] = trim( strip_tags( stripslashes( $new_instance['detaillinktext'] ) ) ); $instance['linkable'] = isset($new_instance['linkable']); $instance['sourcelinkable'] = isset($new_instance['sourcelinkable']); $instance['newwindow'] = isset($new_instance['newwindow']); $instance['displayformat'] = trim( stripslashes( $new_instance['displayformat'] ) ); $instance['liststyle'] = trim( stripslashes( $new_instance['liststyle'] ) ); $instance['minmag'] = $new_instance['minmag']; $instance['region'] = $new_instance['region']; $instance['lat1'] = $new_instance['lat1']; $instance['lat2'] = $new_instance['lat2']; $instance['lon1'] = $new_instance['lon1']; $instance['lon2'] = $new_instance['lon2']; return $instance; } function checkphpversion() { if(!version_compare(PHP_VERSION, '5.2.1', '>=')) { $out = '
'; $out .= '

AdvancedEarthquakeMonitor plugin requires PHP5.2.1 or higher. Your server is running '.phpversion().'.

'; $out .= '
'; echo $out; } return; } function findBetween($s1,$s2,&$str) { if ($s1=='') $pos1=0; else $pos1=strpos($str,$s1); if ($pos1===false) return false; $pos2=strpos($str,$s2,$pos1+strlen($s1)); if ($pos2===false) return false; $val = substr($str,$pos1+strlen($s1),$pos2-$pos1-strlen($s1)); $str = substr($str,$pos2+strlen($s2)); return $val; } function getQuakes($max=20,$minmag='',$lat1='',$lat2='',$lon1='',$lon2='') { $url = 'https://earthquakes.volcanodiscovery.com/widget/data/q-table'; if ($lat1 && $lat2 && $lon1 && $lon2) $url .= '-'.$lat1.'_'.$lat2.'_'.$lon1.'_'.$lon2; $url .= '.tbl'; $content = file_get_contents($url); $quakes = array(); $day = $this->findBetween('','',$content); $year = substr($day,-4); $parts = explode('findBetween('','',$part); $quake['timeUTC'] = substr($quake['timeUTC'],4,-12).$year.' '.substr($quake['timeUTC'],-12); $quake['magnitude'] = trim($this->findBetween('M','/',$part)); if ($quake['magnitude']<$minmag) continue; $quake['depth'] = trim($this->findBetween(' ',' -',$part)); $quake['link'] = trim($this->findBetween('href="','"',$part)); $quake['location'] = trim($this->findBetween('','<',$part)); $quake['location'] = rtrim($quake['location'],'[- '); $quake['lat'] = trim($this->findBetween('showQuake(',',',$part)); $quake['lon'] = trim($this->findBetween('',',',$part)); $quake['sourcelink'] = trim($this->findBetween('href="','"',$part)); $quake['source'] = trim($this->findBetween('>','<',$part)); $quakes[]=$quake; // perhaps the year changes as we go down... $day = $this->findBetween('','',$part); if ($day) $year = substr($day,-4); $count++; if ($count<$max) continue; break; } return $quakes; } function widget($args, $instance) { date_default_timezone_set ('UTC'); extract( $args ); $this->timesCalled ++; // to chec how many times this is used echo $before_widget; if ($instance['showtitle']) { if ($instance['customtitle'] <> '') { echo "{$before_title}".$instance["customtitle"]."{$after_title}"; } else { echo "{$before_title}".$this->maintitle."{$after_title}"; } } if ($this->timesCalled==1) { $preHTML = ' '; } else $preHTML = ''; $quakes = $this->getQuakes(absint($instance['show']),$instance['minmag'],$instance['lat1'],$instance['lat2'],$instance['lon1'],$instance['lon2']); //echo (count($quakes).' earthquakes'); if ($quakes !== FALSE) { echo $preHTML; // styles echo ' '; echo ""; } else echo "Feed error in Earthquakedata"; echo $after_widget; } } add_action('admin_notices', array('AdvancedEarthquakeMonitor','checkphpversion')); add_action( 'widgets_init', 'widget_AdvancedEarthquakeMonitor_widget_init' ); function widget_AdvancedEarthquakeMonitor_widget_init() { register_widget('AdvancedEarthquakeMonitor'); } ?>