Update of /cvsroot/phpweather/phpweather
In directory usw-pr-cvs1:/tmp/cvs-serv3465

Added Files:
        configurator.php 
Log Message:
This is kind of cool!

I've made a script that will generate a config-file, based on the
input from the user. The code is still rather rough, but it seams to
be working. I should use more functions etc...

Right now, it only generates the configuration. There's also some code
in it, that can generate the necessary tables in the database, and
populate it with data about the stations. This code should probably be
moved into the individual db-files.

The output is also rather sparse - if someone wants to spice it up,
then please do so!


--- NEW FILE ---
<html>
<head>
</head>
<body>

<h1>Configuration tool for PHP Weather</h1>

<?php

$properties['always_use_db'] = array(
  'type'   => 'boolean',
  'values' => array('false' => 'No',
                    'true'  => 'Yes'),
  'desc'   => "If you set this option to 'Yes', then PHP Weather " .
              "will always use the data it finds in the database, " .
              "even if it's too old. But if the data isn't there, " .
              "it will still fetch new data from the Internet.");

$properties['station'] = array(
  'type'  => 'text',
  'value' => 'EKYT',
  'desc'  => 'This will be the default station used by PHP ' .
             'Weather. You should enter a four-letter ICAO.');

$properties['db_hostname'] = array(
  'type'   => 'text',
  'value'  => '',
  'desc'   => 'This is the hostname that PHP Weather will use, ' .
              'if you choose to use a database-backend, that ' .
              'supports network connections.');

$properties['db_database'] = array(
  'type'  => 'text',
  'value' => '',
  'desc'  => 'This is the name of the database that PHP Weather ' .
             'should use.');

$properties['db_username'] = array(
  'type'  => 'text',
  'value' => '',
  'desc'  => 'This is the username that PHP Weather will use.');

$properties['db_password'] = array(
  'type'  => 'text',
  'value' => '',
  'desc'  => 'This is the password that PHP Weather will use when ' .
             'trying to make a connection to the database. If you ' .
             ' don\'t use a database that requires a password, ' . 
             'then just leave this field blank.');

$properties['db_pconnect'] = array(
  'type'   => 'boolean',
  'values' => array('false' => 'No',
                    'true'  => 'Yes'),
  'desc'   => 'If you want to make a persistent connection to the ' .
              'database, then set this option to \'Yes\'.');

$properties['db_port'] = array(
  'type'  => 'text',
  'value' => '',
  'desc'  => 'If you have to use a non-standard port when ' .
             'connecting to the database, then please specify it ' .
             'here. If not, then just leave this field blank.');

$properties['db_path'] = array(
  'type'  => 'text',
  'value' => 'PHPWEATHER_BASE_DIR',
  'desc'  => 'If you\'re using a text-style database, then use ' .
             'this option to select the directory you would like ' .
             'to use, for the database-files. If you wan\'t '.
             'PHP Weather to use it\'s base-directory, then leave ' .
             'this option at \'PHPWEATHER_BASE_DIR\'.');

$properties['db_metars'] = array(
  'type'  => 'text',
  'value' => 'metars.db',
  'desc'  => 'This is the name of the database that is used to cache ' .
             'METARs.');

$properties['db_stations'] = array(
  'type'  => 'text',
  'value' => 'stations.db',
  'desc'  => 'This is the name of the database that is used to store ' .
             'the names of the stations.');

$properties['db_type'] = array(
  'type'   => 'select',
  'values' => array('null'  => 'No database at all',
                    'mysql' => 'A MySQL database',
                    'dba'   => 'A Berkeley DB style database',
                    'pqsql' => 'A PostgreSQL database'),
  'desc'   => 'PHP Weather can use several different ' .
              'database-engines. Please choose one from the list.');

$properties['pref_units'] = array(
  'type'   => 'select',
  'values' => array('both_metric'   => 'Metric first, then imperical',
                    'both_imperial' => 'Imperial first, then metric',
                    'only_metric'   => 'Only metric',
                    'only_imperial' => 'Only imperial'),
  'desc'   => 'You can choose to display the data in several ' .
              'formats. Pleace choose one that fits your need.');

$properties['mark_begin'] = array(
  'type'  => 'text',
  'value' => '<b>',
  'desc'  => 'This string will be placed infront of all the ' .
             'changable parts of the output. If you don\'t want ' .
             ' this  to happen, then just use an empty string. ' .
             'Other good choices include \'&lt;i&gt;\', \'&lt;font ' .
             'color="red"&gt;, etc.');

$properties['mark_end'] = array(
  'type'  => 'text',
  'value' => '</b>',
  'desc'  => 'This string is places after all the changable parts. ' .
             'You should make sure, that it closes any tags ' .
             ' you\'ve opened in <code>mark_begin</code>');

$properties['exclude'] = array(
  'type'   => 'multi_select',
  'values' => array('time' => 'The time part',
                  'wind' => 'The wind part',
                  'runway' => 'Information about runways'),
  'desc'   => 'You can disable some of the output produced. If ' .
              'you\'re not interested in information about ' .
              ' runways-visibility, then select it in this list. ' .
              'You can select severel options at once by holding ' .
              'down Ctrl while clicking on the option in Netscape.');

$properties['language']      = array(
  'type'   => 'select',
  'values' => array('en' => 'English'),
  'desc'   => 'PHP Weather can produce output in several langauges ' .
              '- pleace select the default from the list.');

if ($action == 'make_config') {

  $timestamp = date ('dS of F, Y H:i:s');


  echo "<pre>\n";
  print_r($GLOBALS['HTTP_POST_VARS']);
  echo "</pre>\n";


  $config = "
/* This is a local configurations file for PHP Weather.
   It was generated on the $timestamp. */\n\n";

  while(list($option, $value) = each($GLOBALS['HTTP_POST_VARS'])) {
    if (!empty($value)) {
      if ($properties[$option]['type'] == 'boolean' &&
          $properties[$option]['value'] != $value) {
        $config .= "/* $option: */\n\$this->properties['$option'] = $value;\n";
      } elseif ($properties[$option]['type'] == 'text' &&
                $properties[$option]['value'] != $value) {
        $config .= "/* $option: */\n\$this->properties['$option'] = '$value';\n";
      } elseif ($properties[$option]['type'] == 'select') {
        list($default) = each($properties[$option]['values']);
        if ($default != $value) {
          $config .= "/* $option: */\n\$this->properties['$option'] = '$value';\n";
        }
      } elseif ($properties[$option]['type'] == 'multi_select') {
        /* $value is then an array */
        $config .= "/* $option: */\n\$this->properties['$option'] = array(\n";
        $config .= "  '$value[0]'";
        for ($i = 1; $i < count($value); $i++) {
          $config .= ",\n  '$value[$i]'";
        }
        $config .= "\n);\n";

      }
    }
  }


  /* We first check to see, if there's already a config-file. */
  if (file_exists('defaults.php')) {
    /* We make a backup-copy of the file */
    $new_filename = 'defaults.' . time() . '.php';
    if (@copy('defaults.php', $new_filename)) {
      $fp = @fopen('defaults.php', 'w');
    }
  } else {
    $fp = @fopen('defaults.php', 'w');
  }


  echo "<p>Here's the configuration file based on your answers:</p>

<pre>
$config
</pre>\n";

  if ($fp) {
    fputs($fp, $config);
    fclose($fp);
    echo "<p>The configuration was written to <code>defaults.php</code>. A backup was 
made to <code>$new_filename</code>.</p>\n";
  } else {
    echo "<p>A configuration file could <b>not</b> be written. You should copy the 
above configuration to a file, and manually save it as <code>default.php</code>.\n";
  }


  
} elseif ($action == 'dosql') {

  if ($db_engine == 'MySQL') {
    /* We try to make a connection to the MySQL database. */
    if (mysql_connect($db_hostname, $db_username, $db_password)) {
      echo "<p>Connection established to MySQL database at $hostname.</p>\n";
      $db_connection = 1;
    } else {
      echo "<p>Connection failed!</p>\n";
      $db_connection = 0;
    }
  } elseif ($db_engine == 'PostgreSQL') {
    /* We try to make a connection to the PostgreSQL database. */
    if ($conn = pg_connect($hostname, '', $db_name)) {
      echo "<p>Connection established to PostgreSQL database at $hostname.</p>\n";
      $db_connection = 1;
    } else {
      echo '<p>Connection failed! PostgreSQL said: ' . pg_errormessage() . "</p>\n";
      $db_connection = 0;
    }
  } elseif ($db_engine == 'dbm') {
    if (true) {
      echo "<p>Dbm-files created succesfully.</p>\n";
      $db_connection = 1;
    } else {
      echo "<p>Unable to create dbm-files.</p>\n";
      $db_connection = 0;
    }
  } else {
    /* No database engine - this is an error! */
    $db_connection = 0;
  }
  
  if(!$db_connection) {
    echo "<p>The script couldn't connect to the database, so <b>no</b> rows were 
inserted.</p>\n";
  } else {
    $fp = fopen('airports.csv', 'r');
    
    if ($db_engine == 'MySQL') {
      /* We use MySQL. */
      mysql_select_db($db_name);
      /* First we make a table for the METARs */
      mysql_query("DROP TABLE IF EXISTS $db_metars_table");
      mysql_query("
CREATE TABLE $db_metars_table (
   metar varchar(255) NOT NULL,
   timestamp timestamp(14),
   station varchar(4) NOT NULL,
   PRIMARY KEY (station),
   UNIQUE station (station)
)");
      echo "<p>Table $db_metars_table has been created.</p>\n";

      /* Then we make a table for the stations. */
      mysql_query("DROP TABLE IF EXISTS $db_stations_table");
      mysql_query("
CREATE TABLE $db_stations_table (
   icao varchar(4) NOT NULL,
   block_nr char(2) NOT NULL,
   station_nr char(3) NOT NULL,
   name varchar(255) NOT NULL,
   state char(2) NOT NULL,
   country varchar(64) NOT NULL,
   wmo_region tinyint(4) DEFAULT '0' NOT NULL,
   latitude varchar(9) NOT NULL,
   longitude varchar(10) NOT NULL,
   u_latitude varchar(9) NOT NULL,
   u_longitude varchar(10) NOT NULL,
   elevation smallint(6) DEFAULT '0' NOT NULL,
   u_elevation smallint(6) DEFAULT '0' NOT NULL,
   rbsn char(1) NOT NULL,
   PRIMARY KEY (icao),
   UNIQUE icao (icao),
   KEY country (country)
)");
      echo "<p>Table $db_stations_table has been created.</p>\n";
      echo "<p>The rows will now be inserted.</p>\n";
      while ($row = fgetcsv($fp, 1024, ';')) {
        for ($i = 0; $i < count($row); $i++) {
          $row[$i] = addslashes($row[$i]);
        }
        mysql_query("INSERT INTO $db_stations_table VALUES('$row[0]', '$row[1]', 
'$row[2]', '$row[3]', '$row[4]', '$row[5]', '$row[6]', '$row[7]', '$row[8]', 
'$row[9]', '$row[10]', '$row[11]', '$row[12]', '$row[13]')");
        $inserted_rows++;
      }
    } elseif ($db_engine == 'PostgreSQL') {
      /* We use PostgreSQL. */
      /* First we make a table for the METARs */
      pg_exec("
CREATE TABLE $db_metars_table (
   metar VARCHAR(255) NOT NULL,
   timestamp TIMESTAMP,
   station VARCHAR(4) PRIMARY KEY NOT NULL
)", $conn);
      echo "<p>Table $db_metars_table has been created.</p>\n";

      /* Then we make a table for the stations. */
      /* [...] */
      echo "<p>Table $db_stations_table has been created.</p>\n";
      echo "<p>The rows will now be inserted.</p>\n";
      while ($row = fgetcsv($fp, 1024, ';')) {
        for ($i = 0; $i < count($row); $i++) {
          $row[$i] = addslashes($row[$i]);
        }
        pg_exec("INSERT INTO $db_stations_table (icao, block_nr, station_nr, name, 
state, country, wmo_region, latitude, longitude, u_latitude, u_longitude, elevation, 
u_elevation, rbsn) VALUES('$row[0]', '$row[1]', '$row[2]', '$row[3]', '$row[4]', 
'$row[5]', '$row[6]', '$row[7]', '$row[8]', '$row[9]', '$row[10]', '$row[11]', 
'$row[12]', '$row[13]')", $conn);
        $inserted_rows++;
      }
    } elseif ($db_engine == 'dbm') {
      echo "<p>The rows will now be inserted.</p>\n";
      while ($row = fgetcsv($fp, 1024, ';')) {
        for ($i = 0; $i < count($row); $i++) {
          $row[$i] = addslashes($row[$i]);
        }
        dbminsert($icao-name,      $row[0], $row[3]);
        dbminsert($icao-country,   $row[0], $row[5]);
        $inserted_rows++;
      }
    }
    echo "<p>$inserted_rows rows were inserted.</p>\n";
  }

  /* We now print a configurationsfile: */

  $timestamp = date ('M/j-Y H:i:s');
  echo "<p>Here's a configurationfile based on your answers:</p>\n<hr>\n";
  echo "<pre>
/* Configurations file for PHP Weather. Generated on $timestamp.

   You should save this piece somewhere safe (since it contains your
   password. Then make another file called config.inc in the same
   directory as phpweather.inc. This file should then include the
   secret config-file like this:

   &lt;?php
   include(\"$DOCUMENT_ROOT/path/to/secret/place/config.inc\");
   ?&gt;

   This way you don't have to change anything when you upgrade to a
   new version. */

/* The type of database. */
\$db_engine         = '$db_engine';

/* Name of database. */
\$db_name           = '$db_name';

/* Username for database. */
\$db_username       = '$db_username';

/* Password for database. */
\$db_password       = '$db_password';

/* Name of table used for caching the METARs. */
\$db_metars_table   = '$db_metars_table';

/* Name of table used for airport information. */
\$db_stations_table = '$db_stations_table';

/* Hostname of the machine running the database. */
\$db_hostname       = '$db_hostname';

/* How much is the time off? If you're having problems with the time
   being off, then adjust it here. If all the times are 1 hour behind,
   then set it to 3600.*/
//\$time_shift        = $time_shift
</pre>\n<hr>\n";
  
} else {
  /* No action has been specified - we make a form. */


echo '
<form action="configurator.php" method="post">
<table border="2">
<input type="hidden" name="action" value="make_config">
';

while(list($property, $data) = each($properties)) {
  echo "<tr>\n  <td>\n    <p><b><code>$property</code></b></p>\n    
<p>$data[desc]</p>\n  </td>\n";
  switch($data['type']) {
  case 'boolean':
    echo "  <td>\n    <select name=\"$property\">\n";
    while(list($option, $label) = each($data['values'])) {
      echo "      <option value=\"$option\">$label</option>\n";
    }
    echo "    </select>\n  </td>\n";
    break;
  case 'text':
    echo "  <td>\n    <input type=\"text\" name=\"$property\" 
value=\"$data[value]\">\n  </td>\n";
    break;
  case 'select':
    echo "  <td>\n    <select name=\"$property\">\n";
    /* The first option is the default */
    list($option, $label) = each($data['values']);
    echo "      <option value=\"$option\" selected>$label</option>\n";
    while(list($option, $label) = each($data['values'])) {
      echo "      <option value=\"$option\">$label</option>\n";
    }
    echo "    </select>\n  </td>\n";
    break;
  case 'multi_select':
    echo "  <td>\n    <select multiple name=\"${property}[]\">\n";
    while(list($option, $label) = each($data['values'])) {
      echo "      <option value=\"$option\">$label</option>\n";
    }
    echo "    </select>\n  </td>\n";
    break;
  }
}

echo '
</table>
<p><input type="submit" value="Make config-file"> <input type="reset" 
value="Clear"></p>
</form>
';

}
?>
</body>
</html>  


_______________________________________________
PHPWeather-checkins mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/phpweather-checkins

Reply via email to