Thread: CVS: phpweather-1.x phpweather.inc,1.4,1.5
Brought to you by:
iridium
From: Martin G. <gim...@us...> - 2002-05-17 22:12:15
|
Update of /cvsroot/phpweather/phpweather-1.x In directory usw-pr-cvs1:/tmp/cvs-serv850 Modified Files: phpweather.inc Log Message: Removed configuration-variables. I also indented some of the code and broke some long lines. I haven't tested the PostgreSQL code - it's taken from the PHP manual so I hope it works... Index: phpweather.inc =================================================================== RCS file: /cvsroot/phpweather/phpweather-1.x/phpweather.inc,v retrieving revision 1.4 retrieving revision 1.5 diff -u -3 -r1.4 -r1.5 --- phpweather.inc 17 May 2002 15:52:43 -0000 1.4 +++ phpweather.inc 17 May 2002 22:12:13 -0000 1.5 @@ -1,9 +1,10 @@ <?php -/* Copyright (c) 2000, 2001 Martin Geisler <gim...@gi...>. +/* Copyright (c) 2000-2002 Martin Geisler <gim...@gi...>. Licensed under the GPL, see the file COPYING. -Also see - http://www.gimpster.com/php/phpweather/ or +See + + http://www.phpweather.net/ and http://www.sourceforge.net/projects/phpweather for updates and further instructions on how to use PHP Weather. @@ -12,98 +13,50 @@ /* This stores the version number in the variable $version. */ $version = '1.60-CVS'; -/* Global variables. You control PHP Weather by setting these. - * - * This $useXXX variables control the databaseformat PHP Weather uses. - * Set them all to 0 to turn database-support off. Then PHP Weather - * will retrieve a new METAR everytime the script is called. It takes - * about one second to retrieve a METAR, so you probably want to use a - * database. - * - * If $useDBM is set to 1, the program will create the appropriate - * files, but the owner will be set to the user running the webserver, - * usually nobody. This will fail if that user does not have write - * permission to the current directory. - */ - -/* set to 1 to use a MySQL database */ -$useMySQL = 0; - -/* set to 1 to use a DBM database */ -$useDBM = 0; - -/* set to 1 to use a PostgreSQL database */ -$usePSQL = 0; +require('config-dist.inc'); -/* set to 1 to use a Oracle 8 database */ -$useOCI = 0; - -/* set to 1 to use XML */ -$useXML = 0; - -/* This lets you configure a proxy. If you want to use a proxy, set - the next variable to 1. */ -$useProxy = 0; -/* Then set this variable to the ip-number or hostname of the proxy. */ -$proxy_host = 'proxy.your-isp.com'; -/* Finally, change the port, if necessary. The popular Squid proxy - defaults to port 3128, but port 80 or 8080 is also commonly used. */ -$proxy_port = 3128; - -$dbmMetar = 0; -$dbmTimestamp = 0; +if (file_exists('config.inc')) include('config.inc'); if ($useMySQL) { - /* Here's a good place to connect to the MySQL database. You can do - this by using the code below, but if you do, then please make - sure that others can't see this file! */ - $db_hostname = 'localhost'; - $db_name = 'database_name'; - $db_username = 'your_username'; - $db_password = 'your_secret'; - - if (mysql_pconnect($db_hostname, $db_username, $db_password)) { - mysql_select_db($db_name); - } else { - echo "<p>Unable to connect to MySQL database!</p>"; - $useMySQL = 0; /* turn off so rest of program won't use */ - } - + /* Make a connection to the MySQL database: */ + if (mysql_pconnect($db_hostname, $db_username, $db_password)) { + mysql_select_db($db_name); + } else { + echo "<p>Unable to connect to MySQL database!</p>"; + $useMySQL = 0; /* turn off so rest of program won't use */ + } + } elseif ($useDBM) { + /* Open the DBM databases: */ $dbmMetar = dbmopen ("metar", "c"); $dbmTimestamp = dbmopen ("metarTimestamp", "c"); - if (!$dbmMetar || !dbmTimestamp) { + if (!$dbmMetar || !$dbmTimestamp) { echo "<p>Unable to open DBM files!</p>"; $useDBM = 0; /* turn off so rest of program won't use */ - } + } + } elseif ($usePSQL) { - $db_name = 'database_name'; - $conn = pg_Connect("", "", "", "", $db_name); + /* Make a connection to the PostgreSQL database: */ + $conn = pg_Connect("host=$db_hostname dbname=$db_name port=5432 " . + "user=$db_username password=$db_password "); if (!$conn) { echo "<p>Unable to connect to PostgreSQL database!</p>"; $usePSQL = 0; } + } elseif ($useOCI) { - $conn = OCILogon ("ced","weah","svil"); + /* Make a connection to the Oracle 8 database: */ + $conn = OCILogon ($db_username, $db_password, $db_name); if (!$conn) { echo "<p>Unable to connect to Oracle 8 database!</p>"; $useOCI = 0; } + } elseif ($useXML) { - // Configure XML + /* Read the XML file: */ $XMLFile = 'cache.xml'; - /* - The file must exist for this to work. So save the following three - lines in the file $XMLFile, and make sure that the webserver can - read and write to it: - - <?xml version="1.0" encoding="UTF-8"?> - <metardata> - </metardata> - */ - - // Check for existance of the file + // Check for existence of the file: if (!file_exists($XMLFile) || !is_readable($XMLFile) || !is_writable($XMLFile)){ @@ -111,10 +64,10 @@ $useXML = 0; // Turn off so we do not try to use later } else { $XMLMetar = array (); - // Read XML Here ParseXML ($XMLParser, $XMLFile); } + } @@ -171,22 +124,27 @@ if ($windunit == 'KT') { /* The windspeed measured in knots: */ $knots = number_format($value); - /* The windspeed measured in meters per second, rounded to one decimal place: */ + /* The windspeed measured in meters per second, rounded to one + * decimal place: */ $meterspersec = number_format($value * 0.51444, 1); - /* The windspeed measured in miles per hour, rounded to one decimal place: */ + /* The windspeed measured in miles per hour, rounded to one + * decimal place: */ $milesperhour = number_format($value * 1.1507695060844667, 1); } elseif ($windunit == 'MPS') { /* The windspeed measured in meters per second: */ $meterspersec = number_format($value); - /* The windspeed measured in knots, rounded to one decimal place: */ + /* The windspeed measured in knots, rounded to one decimal + * place: */ $knots = number_format($value / 0.51444, 1); - /* The windspeed measured in miles per hour, rounded to one decimal place: */ + /* The windspeed measured in miles per hour, rounded to one + * decimal place: */ $milesperhour = number_format($value / 0.51444 * 1.1507695060844667, 1); } elseif ($windunit == 'KMH') { /* The windspeed measured in kilometers per hour: */ $meterspersec = number_format($value * 1000 / 3600, 1); $knots = number_format($value * 1000 / 3600 / 0.51444, 1); - /* The windspeed measured in miles per hour, rounded to one decimal place: */ + /* The windspeed measured in miles per hour, rounded to one + * decimal place: */ $milesperhour = number_format($knots * 1.1507695060844667, 1); } } @@ -470,7 +428,8 @@ if (isset($metar)) { /* found station */ if ($always_use_cache || $timestamp > time() - 3600) { - /* We have asked explicit for a cached metar, or the metar is still fresh. */ + /* We have asked explicit for a cached metar, or the metar is + * still fresh. */ return $metar; } else { /* We looked in the cache, but the metar was too old. */ @@ -554,23 +513,23 @@ if ($date_unixtime < (time() - 3300)) { /* The timestamp in the metar is more than 55 minutes old. We - adjust the timestamp, so that we won't try to fetch a new - METAR within the next 5 minutes. After 5 minutes, the - timestamp will again be more than 1 hour old. */ + * adjust the timestamp, so that we won't try to fetch a new + * METAR within the next 5 minutes. After 5 minutes, the + * timestamp will again be more than 1 hour old. */ $date_unixtime = time() - 3300; } } else { /* If we end up here, it means that there was no file, we then set - the metar to and empty string. We set the date to time() - 3000 - to give the server 10 minutes of peace. If the file is - unavailable, we don't want to stress the server. */ + * the metar to and empty string. We set the date to time() - 3000 + * to give the server 10 minutes of peace. If the file is + * unavailable, we don't want to stress the server. */ $metar = ''; $date_unixtime = time() - 3000; } /* It might seam strange, that we make a local date, but MySQL - expects a local when we insert the METAR. */ + * expects a local when we insert the METAR. */ $date = date('Y/m/d H:i', $date_unixtime); if ($useMySQL) { @@ -655,7 +614,7 @@ if (ereg('RMK|TEMPO|BECMG', $part)) { /* The rest of the METAR is either a remark or temporary - information. We skip the rest of the METAR. */ + * information. We skip the rest of the METAR. */ $decoded_metar['remarks'] .= ' ' . $part; break; } elseif ($part == 'METAR') { @@ -683,7 +642,8 @@ * add/subtract some hours to $regs[2], e.g. if all your times * are 960 minutes off (16 hours) then add 16 to $regs[2]. */ - $decoded_metar['time'] = gmmktime($regs[2], $regs[3], 0, gmdate('m'), $regs[1], gmdate('Y')); + $decoded_metar['time'] = gmmktime($regs[2], $regs[3], 0, + gmdate('m'), $regs[1], gmdate('Y')); } elseif (ereg('(AUTO|COR|RTD|CC[A-Z]|RR[A-Z])', $part, $regs)) { /* * Report Modifier: AUTO, COR, CCx or RRx @@ -814,35 +774,37 @@ $decoded_metar['runway_vis_ft'] = $prefix . number_format($regs[2]); $decoded_metar['runway_vis_meter'] = $prefix . number_format($regs[2] * 0.3048); } - } elseif (ereg('^(-|\+|VC)?(TS|SH|FZ|BL|DR|MI|BC|PR|RA|DZ|SN|SG|GR|GS|PE|IC|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)+$', $part)) { + } elseif (ereg('^(-|\+|VC)?(TS|SH|FZ|BL|DR|MI|BC|PR|RA|DZ|SN|SG|GR|' . + 'GS|PE|IC|UP|BR|FG|FU|VA|DU|SA|HZ|PY|PO|SQ|FC|SS|DS)+$', + $part)) { /* * Current weather-group */ if ($part[0] == '-') { - /* A light phenomenon */ - $decoded_metar['weather'] .= $strings['light']; - $part = substr($part, 1); + /* A light phenomenon */ + $decoded_metar['weather'] .= $strings['light']; + $part = substr($part, 1); } elseif ($part[0] == '+') { - /* A heavy phenomenon */ - $decoded_metar['weather'] .= $strings['heavy']; - $part = substr($part, 1); + /* A heavy phenomenon */ + $decoded_metar['weather'] .= $strings['heavy']; + $part = substr($part, 1); } elseif ($part[0].$part[1] == 'VC') { - /* Proximity Qualifier */ - $decoded_metar['weather'] .= $strings['nearby']; - $part = substr($part, 2); + /* Proximity Qualifier */ + $decoded_metar['weather'] .= $strings['nearby']; + $part = substr($part, 2); } else { /* no intensity code => moderate phenomenon */ $decoded_metar['weather'] .= $strings['moderate']; } while ($bite = substr($part, 0, 2)) { - /* Now we take the first two letters and determine what they - mean. We append this to the variable so that we gradually - build up a phrase. */ - $decoded_metar['weather'] .= $weather_array[$bite]; - /* Here we chop off the two first letters, so that we can take - a new bite at top of the while-loop. */ - $part = substr($part, 2); + /* Now we take the first two letters and determine what they + mean. We append this to the variable so that we gradually + build up a phrase. */ + $decoded_metar['weather'] .= $weather_array[$bite]; + /* Here we chop off the two first letters, so that we can take + a new bite at top of the while-loop. */ + $part = substr($part, 2); } } elseif (ereg('(SKC|CLR)', $part, $regs)) { /* @@ -852,35 +814,42 @@ */ $cloud_layers++; /* Again we have to translate the code-characters to a - meaningful string. */ - $decoded_metar['cloud_layer'. $cloud_layers.'_condition'] = $cloud_condition_array[$regs[1]]; - $decoded_metar['cloud_layer'.$cloud_layers.'_coverage'] = $cloud_coverage_array[$regs[1]]; - } elseif (ereg('^(VV|FEW|SCT|BKN|OVC)([0-9]{3})(CB|TCU)?$', $part, $regs)) { + meaningful string. */ + $decoded_metar['cloud_layer'. $cloud_layers.'_condition'] = + $cloud_condition_array[$regs[1]]; + $decoded_metar['cloud_layer'.$cloud_layers.'_coverage'] = + $cloud_coverage_array[$regs[1]]; + } elseif (ereg('^(VV|FEW|SCT|BKN|OVC)([0-9]{3})(CB|TCU)?$', + $part, $regs)) { /* We have found (another) a cloud-layer-group. There can be up - to three of these groups, so we store them as cloud_layer1, - cloud_layer2 and cloud_layer3. */ + to three of these groups, so we store them as cloud_layer1, + cloud_layer2 and cloud_layer3. */ $cloud_layers++; - /* Again we have to translate the code-characters to a meaningful string. */ + /* Again we have to translate the code-characters to a + meaningful string. */ if ($regs[1] == 'OVC') { - $clouds_str_temp = ''; + $clouds_str_temp = ''; } else { - $clouds_str_temp = $strings['clouds']; + $clouds_str_temp = $strings['clouds']; } if ($regs[3] == 'CB') { - /* cumulonimbus (CB) clouds were observed. */ - $decoded_metar['cloud_layer'.$cloud_layers.'_condition'] = - $cloud_condition_array[$regs[1]] . $strings['clouds_cb']; + /* cumulonimbus (CB) clouds were observed. */ + $decoded_metar['cloud_layer'.$cloud_layers.'_condition'] = + $cloud_condition_array[$regs[1]] . $strings['clouds_cb']; } elseif ($regs[3] == 'TCU') { - /* towering cumulus (TCU) clouds were observed. */ - $decoded_metar['cloud_layer'.$cloud_layers.'_condition'] = - $cloud_condition_array[$regs[1]] . $strings['clouds_tcu']; + /* towering cumulus (TCU) clouds were observed. */ + $decoded_metar['cloud_layer'.$cloud_layers.'_condition'] = + $cloud_condition_array[$regs[1]] . $strings['clouds_tcu']; } else { - $decoded_metar['cloud_layer'.$cloud_layers.'_condition'] = - $cloud_condition_array[$regs[1]] . $clouds_str_temp; + $decoded_metar['cloud_layer'.$cloud_layers.'_condition'] = + $cloud_condition_array[$regs[1]] . $clouds_str_temp; } - $decoded_metar['cloud_layer'.$cloud_layers.'_coverage'] = $cloud_coverage[$regs[1]]; - $decoded_metar['cloud_layer'.$cloud_layers.'_altitude_ft'] = $regs[2] *100; - $decoded_metar['cloud_layer'.$cloud_layers.'_altitude_m'] = round($regs[2] * 30.48); + $decoded_metar['cloud_layer'.$cloud_layers.'_coverage'] = + $cloud_coverage[$regs[1]]; + $decoded_metar['cloud_layer'.$cloud_layers.'_altitude_ft'] = + $regs[2] *100; + $decoded_metar['cloud_layer'.$cloud_layers.'_altitude_m'] = + round($regs[2] * 30.48); } elseif (ereg('^(M?[0-9]{2})/(M?[0-9]{2})?$', $part, $regs)) { /* * Temperature/Dew Point Group @@ -888,7 +857,8 @@ */ $decoded_metar['temp_c'] = number_format(strtr($regs[1], 'M', '-')); $decoded_metar['dew_c'] = number_format(strtr($regs[2], 'M', '-')); - /* The temperature and dew-point measured in Fahrenheit, rounded to the nearest degree. */ + /* The temperature and dew-point measured in Fahrenheit, rounded + to the nearest degree. */ $decoded_metar['temp_f'] = round(strtr($regs[1], 'M', '-') * (9/5) + 32); $decoded_metar['dew_f'] = round(strtr($regs[2], 'M', '-') * (9/5) + 32); } elseif(ereg('A([0-9]{4})', $part, $regs)) { @@ -1025,8 +995,6 @@ */ global $useMySQL, $useDBM, $usePSQL, $conn, $dbmMetar, $dbmTimestamp; - - // XML Globals I did this seperate so you could see it. global $useXML, $XMLMetar, $XMLParser, $XMLFile; if ($useMySQL) { @@ -1041,8 +1009,8 @@ $num=pg_numrows($result); if (pg_numrows($result)) { for ($i = 0; $i < $num; $i++) { - list($station) = pg_fetch_row($result, $i); - fetch_metar($station, 0); + list($station) = pg_fetch_row($result, $i); + fetch_metar($station, 0); } } } elseif ($useDBM) { @@ -1058,34 +1026,33 @@ $nrows = OCIFetchStatement($stmt,$results); if ( $nrows > 0 ) { for ($i = 0; $i < $nrows; $i++) { - list($station) = $results[i]; - fetch_metar($station, 0); + list($station) = $results[i]; + fetch_metar($station, 0); } } } elseif ($useXML) { // File Update here // Parse the XML ParseXML ($XMLParser, $XMLFile); - + reset ($XMLMetar); - while (list ($station) = each ($XMLMetar)) - { + while (list ($station) = each ($XMLMetar)) { fetch_metar ($station, 0); } } } /* - XML Handling Tools -*/ + * XML Handling Tools + */ function ParseXML($XMLParser, $XMLFile){ // Open the file for reading $FileHandle = fopen($XMLFile, "r"); - + // Suck the information out $XMLData = fread($FileHandle, filesize($XMLFile)); - + // Close the file fclose($FileHandle); @@ -1134,8 +1101,7 @@ fwrite($FileHandle, $Head, strlen ($Head)); // Walk the stations and write them to the file - while (list ($Station, $Info) = each ($XMLMetar)) - { + while (list ($Station, $Info) = each ($XMLMetar)) { // Check for actual station value in array! if (!empty($Station)) { $CacheString = "\n\t<cache station=\"" . $Info['station'] . |