CVS: phpweather data_retrieval.php,1.24,1.25 index.php,1.35,1.36 pw_u...
Brought to you by:
iridium
|
From: Martin G. <gim...@us...> - 2002-08-28 10:10:05
|
Update of /cvsroot/phpweather/phpweather
In directory usw-pr-cvs1:/tmp/cvs-serv16372
Modified Files:
data_retrieval.php index.php pw_utilities.php
Log Message:
More good stuff from Ondrej Jombik <ne...@po...>... The index.php
page has been rewritten to provide the same functionality with less
and more clear code.
I've updated data_rettrieval.php to deal with the changed lookup_icao
method in the database backends.
Index: data_retrieval.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/data_retrieval.php,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -3 -r1.24 -r1.25
--- data_retrieval.php 27 Aug 2002 23:26:25 -0000 1.24
+++ data_retrieval.php 28 Aug 2002 10:10:00 -0000 1.25
@@ -26,11 +26,12 @@
var $metar;
/**
- * The location of the station, eg. something like 'Aalborg, Denmark'.
+ * Data associated with the current ICAO.
*
- * @var string
+ * @var array The array has three entries: name, country,
+ * and country code of the ICAO.
*/
- var $location;
+ var $icao_data;
/**
* Constructor
@@ -77,9 +78,9 @@
$new_icao = addslashes($new_icao);
if ($new_icao != $this->get_icao()) {
$this->properties['icao'] = strtoupper($new_icao);
- $this->location = '';
- $this->metar = '';
- $this->decoded_metar = '';
+ $this->icao_data = false;
+ $this->metar = false;
+ $this->decoded_metar = false;
}
}
@@ -121,7 +122,7 @@
if ($new_metar != $this->get_metar()) {
$this->debug('Loading a METAR manually.');
$this->properties['icao'] = strtoupper(substr($new_metar,0,4));
- $this->location = '';
+ $this->icao_data = $this->db->lookup_icao($this->get_icao());
$this->metar = $new_metar;
$this->decoded_metar = $this->decode_metar();
}
@@ -185,6 +186,7 @@
}
}
+
/**
* Fetches a METAR from the Internet.
*
@@ -198,7 +200,6 @@
* @access public
* @return string The raw METAR.
*/
-
function get_metar_from_web($new_station) {
$metar = '';
$icao = $this->get_icao();
@@ -310,18 +311,112 @@
return $metar;
}
+
+ /**
+ * Returns the location of the current station.
+ *
+ * The location is the name of the station followed by the name of
+ * the country. If the ICAO cannot be found in the database, then
+ * the ICAO is just returned again.
+ *
+ * @return string The location of the station.
+ */
function get_location() {
- if (!empty($this->location)) {
- $this->debug('get_location(): Using old location: ' . $this->location);
- return $this->location;
+ if (!empty($this->icao_data)) {
+ $location = $this->icao_data[0] . ', ' . $this->icao_data[1];
+ $this->debug("get_location(): Using old location: $location");
+ return $location;
} elseif ($this->db->connect()) {
$this->debug('get_location(): Looking for location in the database');
- return $this->location = $this->db->lookup_icao($this->get_icao());
+ $this->icao_data = $this->db->lookup_icao($this->get_icao());
+ if (empty($this->icao_data)) {
+ return $this->get_icao(); // ICAO not found in database.
+ } else {
+ return $this->icao_data[0] . ', ' . $this->icao_data[1];
+ }
+ } else {
+ return $this->get_icao();
+ }
+ }
+
+
+ /**
+ * Returns the name of the station for the current ICAO.
+ *
+ * @return string The name of the station or false if the ICAO
+ * wasn't found in the database.
+ * @access public
+ */
+ function get_name() {
+ if (!empty($this->icao_data)) {
+ $this->debug('get_name(): Using old station name: ' . $this->icao_data[0]);
+ return $this->icao_data[0];
+ } elseif ($this->db->connect()) {
+ $this->debug('get_name(): Looking for station name in the database');
+ $this->icao_data = $this->db->lookup_icao($this->get_icao());
+ if (empty($this->icao_data)) {
+ return false; // ICAO not found in database.
+ } else {
+ return $this->icao_data[0];
+ }
} else {
return false;
}
}
+
+ /**
+ * Returns the name of the country for the current ICAO.
+ *
+ * @return string The name of the country or false if the ICAO
+ * wasn't found in the database.
+ * @access public
+ */
+ function get_country() {
+ if (!empty($this->icao_data)) {
+ $this->debug('get_country(): Using old country name: ' . $this->icao_data[1]);
+ return $this->icao_data[1];
+ } elseif ($this->db->connect()) {
+ $this->debug('get_country(): Looking for country name in the database');
+ $this->icao_data = $this->db->lookup_icao($this->get_icao());
+ if (empty($this->icao_data)) {
+ return false; // ICAO not found in database.
+ } else {
+ return $this->icao_data[1];
+ }
+ } else {
+ return false;
+ }
+ }
+
+
+ /**
+ * Returns contry code specified ICAO.
+ *
+ * @return string country code (cc)
+ * @author Ondrej Jombik <ne...@po...>
+ * @access public
+ */
+ function get_country_code() {
+ if (!empty($this->icao_data)) {
+ $this->debug('get_country_code(): Using old country code (cc): ' .
+ $this->icao_data[2]);
+ return $this->icao_data[2];
+ } elseif ($this->db->connect()) {
+ $this->debug('get_country(): Looking for country code (cc) in the database');
+ $this->icao_data = $this->db->lookup_icao($this->get_icao());
+ if (empty($this->icao_data)) {
+ return false; // ICAO not found in database.
+ } else {
+ return $this->icao_data[2];
+ }
+ } else {
+ return false;
+ }
+ }
+
+
+
}
?>
Index: index.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/index.php,v
retrieving revision 1.35
retrieving revision 1.36
diff -u -3 -r1.35 -r1.36
--- index.php 26 Aug 2002 21:48:23 -0000 1.35
+++ index.php 28 Aug 2002 10:10:01 -0000 1.36
@@ -32,66 +32,88 @@
';
-if (empty($action)) {
- /* No action - we display a form from which the user can select a
- country. */
-
- $output .= '<form action="index.php" method="get">' . "\n" .
- '<p><input type="hidden" name="action" value="show_stations" /> ' .
- get_countries_select($weather, '') .
- ' <input type="submit" />' . "</p>\n</form>\n";
-
-} elseif ($action == 'show_stations' && !empty($cc)) {
- /* A country has just been selected - we make a form with all
- stations in that country. */
+/* We should protect ourself against nasty guys passing quoting
+ * characters to CGI variables. We also want to allow lowercase
+ * icao/cc specification. It should improve direct linking to our
+ * weather website. Someone should directly write
+ * ``http://example.com/weather.php?icao=lztt'' if he or she knows
+ * particular code for airport (icao). */
- if (empty($icao)) $icao = '';
- if (empty($language)) $language = 'en';
+if (empty($cc)) {
+ $cc = '';
+} else {
+ $cc = stripslashes($cc);
+}
+
+if (empty($icao)) {
+ $icao = '';
+} else {
+ $icao = stripslashes($icao);
+}
+
+$languages = $weather->get_languages('text');
+
+if (empty($language) || !in_array($language, array_keys($languages))) {
+ $language = 'en';
+} else {
+ $language = stripslashes($language);
+}
+
+if ($icao != '') {
+ $weather->set_icao($icao);
+ /* icao was passed, we resolve country code */
+ $cc_temp = $weather->get_country_code();
+ if ($cc_temp === false) {
+ /* turn icao off, it is not valid */
+ $icao = '';
+ } else {
+ /* use resolved country code */
+ $cc = $cc_temp;
+ }
+}
+
+/* Always display country selection */
+$output .= '<form action="index.php" method="get">' . "\n"
+ .'<p>' . get_countries_select($weather, $cc)
+ .' <input type="submit" value="'
+ .($cc == '' ? 'Submit country' : 'Change country').'" />'
+ .'<input type="hidden" name="language" value="'.htmlspecialchars($language).'"> '
+ . "</p>\n</form>\n";
+
+if (! empty($cc)) {
+ /* Show stations selection for particular country ($cc). */
+ $output .= '<form action="index.php" method="get">' . "\n<p>"
+ . get_stations_select($weather, $cc, $icao)
+ . get_languages_select($weather, $language)
+ . '<input type="submit" value="Submit location">'
+ . "</p>\n</form>\n";
+}
+
+if (! empty($icao)) {
+ /* We should only display the current weather if we have station ($icao) */
+ include(PHPWEATHER_BASE_DIR . "/output/pw_text_$language.php");
+ $type = 'pw_text_' . $language;
+ $text = new $type($weather);
- $output .= '<form action="index.php" method="get">' . "\n<p>" .
- '<input type="hidden" name="action" value="show_weather" /> ' .
- get_countries_select($weather, $cc) .
- get_stations_select($weather, $cc, $icao) .
- get_languages_select($weather, $language) .
- '<input type="submit" />' . "</p>\n</form>\n";
-
-} elseif ($action == 'show_weather' && !empty($language)) {
- /* A station has just been selected - we print the weather. */
- $output .= '<form action="index.php" method="get">' . "\n<p>" .
- '<input type="hidden" name="action" value="show_weather" /> ' .
- get_countries_select($weather, $cc) .
- get_stations_select($weather, $cc, $icao) .
- get_languages_select($weather, $language) .
- '<input type="submit" />' . "</p>\n</form>\n";
+ include(PHPWEATHER_BASE_DIR . "/output/pw_images.php");
+ $icons = new pw_images($weather);
- if ($cc == $old_cc) {
- /* We should only display the current weather is the country isn't
- changed */
- $weather->set_icao($icao);
- include(PHPWEATHER_BASE_DIR . "/output/pw_text_$language.php");
- $type = 'pw_text_' . $language;
- $text = new $type($weather);
-
- include(PHPWEATHER_BASE_DIR . "/output/pw_images.php");
- $icons = new pw_images($weather);
-
- $output .= '<p>This is the current weather in ' .
- $weather->get_location() . ":</p>\n<blockquote>\n" .
- $text->print_pretty() . "\n</blockquote>\n" .
- "<p>The matching icons are:</p>\n<blockquote>\n" .
- '<img src="' . $icons->get_sky_image() .
- '" height="50" width="80" border="1" alt="Current weather in ' .
- $weather->get_location() . '" /> ' .
- '<img src="' . $icons->get_winddir_image() .
- '" height="40" width="40" border="1" alt="Current wind in ' .
- $weather->get_location() . '" /> ' .
- '<img src="' . $icons->get_temp_image() .
- '" height="50" width="20" border="1" alt="Current temperature in ' .
- $weather->get_location() . '" />' .
- "\n</blockquote>\n" .
- "<p>The raw METAR is <code>" .
- $weather->get_metar() . "</code></p>\n";
- }
+ $output .= '<p>This is the current weather in ' .
+ $weather->get_location() . ":</p>\n<blockquote>\n" .
+ $text->print_pretty() . "\n</blockquote>\n" .
+ "<p>The matching icons are:</p>\n<blockquote>\n" .
+ '<img src="' . $icons->get_sky_image() .
+ '" height="50" width="80" border="1" alt="Current weather in ' .
+ $weather->get_location() . '" /> ' .
+ '<img src="' . $icons->get_winddir_image() .
+ '" height="40" width="40" border="1" alt="Current wind in ' .
+ $weather->get_location() . '" /> ' .
+ '<img src="' . $icons->get_temp_image() .
+ '" height="50" width="20" border="1" alt="Current temperature in ' .
+ $weather->get_location() . '" />' .
+ "\n</blockquote>\n" .
+ "<p>The raw METAR is <code>" .
+ $weather->get_metar() . "</code></p>\n";
}
if (empty($text)) {
@@ -105,6 +127,7 @@
$end_time = explode(' ', microtime());
$diff = ($end_time[0] + $end_time[1]) - ($start_time[0] + $start_time[1]);
+
?>
<p>PHP Weather comes with some documentation that you'll probably want
Index: pw_utilities.php
===================================================================
RCS file: /cvsroot/phpweather/phpweather/pw_utilities.php,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -3 -r1.4 -r1.5
--- pw_utilities.php 28 Mar 2002 15:49:09 -0000 1.4
+++ pw_utilities.php 28 Aug 2002 10:10:01 -0000 1.5
@@ -15,7 +15,6 @@
}
}
$output .= "\n</select>\n";
- $output .= '<input type="hidden" name="old_cc" value="' . $old_cc . '" />';
return $output;
}
@@ -35,8 +34,6 @@
}
}
$output .= "\n</select>\n";
- $output .= '<input type="hidden" name="old_icao" value="' .
- $old_icao . '" />';
return $output;
}
@@ -55,10 +52,8 @@
}
}
$output .= "\n</select>\n";
- $output .= '<input type="hidden" name="old_language" value="' .
- $old_language . '" />';
return $output;
}
-?>
\ No newline at end of file
+?>
|