Thread: CVS: phpweather/config index.php,NONE,1.1 pw_dependency.php,NONE,1.1 ...
Brought to you by:
iridium
From: Martin G. <gim...@us...> - 2002-04-07 18:38:23
|
Update of /cvsroot/phpweather/phpweather/config In directory usw-pr-cvs1:/tmp/cvs-serv8657 Added Files: index.php pw_dependency.php pw_dependency_or.php pw_option.php pw_option_boolean.php pw_option_multi_select.php pw_option_select.php pw_option_text.php pw_optiongroup.php pw_validator.php pw_validator_ereg.php pw_validator_range.php reset_session.php Log Message: This is a new and improved configurator - a script that can be used to generate a defaults.php file for PHP Weather. The new thing is, that the options now depend on each other - you cannot configure the database password if you're using a DBA database, because that kind of database doesn't use a password. The options are also validated so that the default ICAO really is a string of four letters or digits. I've dropped the idea about having the script save the configuration. It only worked when the webserver had write-permission to the directory (which was rare) and it caused some security-problems. There's also no code for generating the databases. I think it would be better if we made a separate script for that. Perhaps we should couple this with the code that updates the METARs from cycle-files. The classes still lack documentation... --- NEW FILE --- <?php error_reporting(E_ALL); /* Include a couple of validators: */ include('pw_validator.php'); include('pw_validator_ereg.php'); include('pw_validator_range.php'); /* Include some options: */ include('pw_option.php'); include('pw_option_text.php'); include('pw_option_select.php'); include('pw_option_multi_select.php'); include('pw_option_boolean.php'); /* We want to group the options: */ include('pw_optiongroup.php'); /* A couple of dependencies: */ include('pw_dependency.php'); include('pw_dependency_or.php'); /* We have to strip slashes from the GPC variables. */ if (get_magic_quotes_gpc() == 1) { function recursive_stripslashes(&$array) { $keys = array_keys($array); foreach($keys as $key) { if (is_array($array[$key])) { recursive_stripslashes($array[$key]); } else { $array[$key] = stripslashes($array[$key]); } } } recursive_stripslashes($_REQUEST); } /* Start the session. */ session_start(); /* If $options isn't registered, then we should make the variable and * register it: */ if (!session_is_registered('options')) { /* Common dependencies: */ $db_dep = new pw_dependency_or('db_type', array('mysql', 'pgsql', 'dba')); $sql_dep = new pw_dependency_or('db_type', array('mysql', 'pgsql')); $dba_dep = new pw_dependency('db_type', 'dba'); $proxy_dep = new pw_dependency('use_proxy', 'true'); $port_validator = new pw_validator_range("Sorry, '%s' is not a valid port-number because " . "is't outside the range 1-65536", 1, 65536, true); /* This just catches the most obvious errors. */ $table_validator = new pw_validator_ereg("Sorry, '%s' is not a valid name.", '^[^./]+$'); $icao_validator = new pw_validator_ereg("Sorry, '%s' is not a valid ICAO.", '^[a-zA-Z0-9]{4}$'); /* This just catches the most obvious errors. */ $host_validator = new pw_validator_ereg("Sorry, '%s' is not a valid hostname.", '^[^/#?~]+$'); /* Next comes all the options: */ $options['verbosity'] = new pw_option_select('verbosity', "The setting of this variable controls the amount of " . "errors, warnings, and debug-information PHP Weather " . "will print. It is suggested that you always include " . "errors in the output and perhaps also warnings.", array(), array('1' => 'Errors only', '2' => 'Warnings only', '4' => 'Debug information only', '3' => 'Errors + warnings', '5' => 'Errors + debug information', '6' => 'Warnings + debug information', '7' => 'Everything') ); $options['icao'] = new pw_option_text('icao', 'This will be the default station used by PHP Weather. ' . 'You should enter a valid four-letter ICAO.', array(), $icao_validator, 'EKYT' ); $options['pref_units'] = new pw_option_select('pref_units', 'You may choose to display the data in several ' . 'formats. Please choose one that fits your need.', array(), array('both_metric' => 'Metric first, then imperial', 'both_imperial' => 'Imperial first, then metric', 'only_metric' => 'Only metric', 'only_imperial' => 'Only imperial')); $options['language'] = new pw_option_select('language', 'PHP Weather can produce output in several languages ' . '- please select your default from the list.', array(), array('en' => 'English', 'da' => 'Danish', 'hu' => 'Hungarian', 'no' => 'Norwegian')); $options['use_proxy'] = new pw_option_boolean('use_proxy', "Set this option to 'Yes' to enable support for a " . "proxy server.", array(), array('false' => 'No', 'true' => 'Yes')); $options['proxy_host'] = new pw_option_text('proxy_host', "This is the hostname of the proxy server.", array($proxy_dep), $host_validator); $options['proxy_port'] = new pw_option_text('proxy_port', "This is the port number of the proxy server. The " . "default is what is used by the Squid proxy server. " . "Another common port number is '8080'", array($proxy_dep), $port_validator, '3128'); $options['db_type'] = new pw_option_select('db_type', 'PHP Weather can use several kinds of databases.', array(), array('null' => 'No database at all', 'mysql' => 'A MySQL database', 'pgsql' => 'A PostgreSQL database', 'dba' => 'A DBA database')); $options['db_handler'] = new pw_option_select('db_handler', "If you've chosen to use a Berkeley DB style database " . "through the PHP database abstraction layer (DBA), then " . "please select the handler you would like to use.", array($dba_dep), array('dbm' => 'dbm - The oldest (original) type of ' . 'Berkeley DB style databases', 'ndbm' => 'ndbm - a newer and more flexible type.', 'gdbm' => 'gdbm - The GNU database manager', 'db2' => 'db2 - Sleepycat Softwares DB2', 'db3' => 'db3 - Sleepycat Softwares DB3')); $options['always_use_db'] = new pw_option_boolean('always_use_db', "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.", array($db_dep), array('false' => 'No', 'true' => 'Yes')); $options['db_pconnect'] = new pw_option_boolean('db_pconnect', "If you want to make a persistent connection to the " . "database, then set this option to 'Yes'.", array($sql_dep), array('false' => 'No', 'true' => 'Yes')); $options['db_port'] = new pw_option_text('db_port', '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.', array($sql_dep), $port_validator); $options['db_hostname'] = new pw_option_text('db_hostname', 'This is the hostname that PHP Weather will use, ' . 'if you choose to use a database-backend, that ' . 'supports network connections.', array($sql_dep), $host_validator); $options['db_database'] = new pw_option_text('db_database', 'This is the name of the database that PHP Weather ' . 'should use.', array($sql_dep), $table_validator); $options['db_username'] = new pw_option_text('db_username', 'This is the username that PHP Weather will use ' . 'for accessing the database.', array($sql_dep)); $options['db_password'] = new pw_option_text('db_password', 'This is the password that PHP Weather will use when ' . 'trying to make a connection to the database. Please ' . "remember to protect the file after you've stored the " . "password in it.", array($sql_dep)); $options['db_metars'] = new pw_option_text('db_metars', 'This is the name of the table that is used ' . 'to cache the METARs.', array($db_dep), $table_validator, 'pw_metars'); $options['db_stations'] = new pw_option_text('db_stations', 'This is the name of the database/table that is used ' . 'to store the names of the stations.', array($db_dep), $table_validator, 'pw_stations'); $options['db_countries'] = new pw_option_text('db_countries', 'This is the name of the database that is used to ' . 'store the names of the countries together with ' . 'country-codes.', array($dba_dep), $table_validator, 'pw_countries'); $options['mark_begin'] = new pw_option_text('mark_begin', 'This string will be placed in front 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 <code><i></code>, <code><font " . 'color="red"></code>, etc.', array(), false, '<b>'); $options['mark_end'] = new pw_option_text('mark_end', 'This string is placed after all the changable parts. ' . 'You should make sure that it closes any tags ' . "you've opened in <code>mark_begin</code>.", array(), false, '</b>'); $options['exclude'] = new pw_option_multi_select('exclude', '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 several options at once in Netscape ' . 'by holding down Ctrl while clicking on the option.', array(), array('time' => 'Leave out the time part', 'wind' => 'Leave out the wind part', 'runway' => 'Leave out information about runways')); /* Finally - we register all the options with the session. */ session_register('options'); } /* The options should now be ready - they might come from a restored * session, or they might just have been created. */ /* We update the options with the latest information. We have to be carefull when we do this, because we have to operate directly on the options in $options and not on a copy. */ $keys = array_keys($options); foreach ($keys as $option) { $options[$option]->update_value($_REQUEST); } /* Grouping */ $general_group = new pw_optiongroup('General Options', 'This is some general options for PHP Weather.', array('verbosity', 'icao', 'pref_units', 'language', 'use_proxy', 'proxy_host', 'proxy_port')); $db_group = new pw_optiongroup('Database Options', 'These options deal with the database. PHP Weather ' . 'can use a database for caching the METARs it ' . 'retrieves. This is very nice, since it takes at ' . 'least a second or two to fetch a METAR from the ' . 'National Weather Service.', array('db_type', 'db_handler', 'db_pconnect', 'always_use_db', 'db_hostname', 'db_port', 'db_database', 'db_username', 'db_password', 'db_metars', 'db_stations', 'db_countries')); $rendering_group = new pw_optiongroup('Rendering Options', 'You can customize the looks of PHP Weather using these options.', array('mark_begin', 'mark_end', 'exclude')); /* We can now generate a configuration file with the options selected so far: */ $timestamp = date ('dS of F, Y H:i:s'); $config = "<?php /* This is a local configuration file for PHP Weather. It was generated on the $timestamp. */\n" . $general_group->get_config() . $db_group->get_config() . $rendering_group->get_config() . "\n?>\n"; if (!empty($_REQUEST['download'])) { header('Content-type: application/octet-stream'); header('Content-Disposition: attachment; filename="defaults.php"'); echo $config; exit(); } ?> <html> <head> <title>Configuration Builder for PHP Weather</title> <style type="text/css"> <!-- BODY { font-family: Verdana, Tahoma, Geneva, Arial, Helvetica, sans-serif; font-size: small; } DT { font-weight: bold } P { margin-top: 0.5em; margin-bottom: 0.5em; } --> </style> </head> <body> <img src="../icons/phpweather-long-white.gif" alt="PHP Weather" align="right"> <h1>Configuration Builder for PHP Weather</h1> <p>This is the Configuretor shipped with PHP Weather.</p> <p>Change the options below - when you're done, then press one of the 'Update Options' buttons. Depending on your choices, more options might appear. Continue to change the options until they all say <span style="color: green">Input accepted.</span></p> <form action="<? echo $PHP_SELF . '?' . SID ?>" method="POST"> <p>You can <input type="submit" name="download" value="Download the Configuration"> or <input type="reset" value="Reset Everything" onclick="document.location='reset_session.php'"></p> <dl> <?php $general_group->show(); $db_group->show(); $rendering_group->show(); ?> </dl> <p><input type="submit" value="Update options"></p> <p>This is a configuration file bases on your answers above:</p> <?php highlight_string($config); ?> <p>You should copy the above configuration to a file called <code>defaults.php</code> in the root directory of your PHP Weather installation. It's very important that the lines with <code><font color="#0000CC"><?php</font></code> and <code><font color="#0000CC">?></font></code> are the very first and very last, respectively. There should be no blank lines outside these two tags.</p> <p>You can also <input type="submit" name="download" value="Download the Configuration"> or <input type="reset" value="Reset Everything" onclick="document.location='reset_session.php'"></p> </form> </body> </html> --- NEW FILE --- <?php class pw_dependency { var $option; var $dep; function pw_dependency($option, $d) { $this->option = $option; $this->dep = $d; } function check() { return ($GLOBALS['options'][$this->option]->get_value() == $this->dep); } } ?> --- NEW FILE --- <?php class pw_dependency_or extends pw_dependency { function pw_dependency_or($option, $d) { $this->pw_dependency($option, $d); } function check() { $value = $GLOBALS['options'][$this->option]->get_value(); foreach ($this->dep as $d) { if ($value == $d) return true; } return false; } } ?> --- NEW FILE --- <?php class pw_option { var $name = ''; var $description = ''; var $dependencies = array(); var $value = ''; var $default = ''; var $validator; function pw_option($name, $description, $dependencies, $validator = false, $default = false) { $this->name = $name; $this->description = $description; $this->dependencies = $dependencies; if ($validator) { $this->validator = $validator; } else { $this->validator = new pw_validator('Please correct your input'); } if ($default) { $this->default = $default; $this->update_value(array($name . '_value' => $default)); } } function get_name() { return $this->name; } function get_description() { return $this->description; } function get_value() { return $this->value; } function is_ready() { $ready = true; foreach($this->dependencies as $dependency) { if (!$dependency->check()) { $ready = false; break; } } return $ready; } function is_valid() { return $this->validator->validate($this->value); } function update_value($values) { if (isset($values[$this->name . '_value'])) { $this->value = $values[$this->name . '_value']; } } function get_config() { if ($this->is_ready() && $this->is_valid() && $this->value != $this->default) { return "/* $this->name */\n\$this->properties['$this->name'] = " . "'$this->value';\n\n"; } else { return ''; } } } ?> --- NEW FILE --- <?php class pw_option_boolean extends pw_option_select { function get_config() { if ($this->is_ready() && $this->value != $this->default) { return "/* $this->name */\n\$this->properties['$this->name'] = " . "$this->value;\n\n"; } else { return ''; } } } ?> --- NEW FILE --- <?php class pw_option_multi_select extends pw_option { var $choices = array(); function pw_option_multi_select($name, $description, $dependencies, $choices) { $this->pw_option($name, $description, $dependencies); $this->choices = $choices; } function is_valid() { foreach ($this->value as $value) { if (!in_array($value, array_keys($this->choices))) { return false; } } return true; } function update_value($values) { if (isset($values[$this->name . '_value'])) { $this->value = $values[$this->name . '_value']; } else { $this->value = array(); } } function show() { if ($this->is_ready()) { echo "<dt>Option <code>$this->name</code>: " . '<select name="' . $this->name . '_value[]" multiple="multiple">'; foreach ($this->choices as $choice => $label) { if (in_array($choice, $this->value)) { echo '<option selected="selected" value="' . htmlentities($choice) . "\">$label</option>\n"; } else { echo '<option value="' . htmlentities($choice) . "\">$label</option>\n"; } } echo "</select>\n</dt>\n"; echo '<dd><p>' . $this->description . "</p>\n"; if ($this->is_valid()) { echo '<p style="color: green">Input accepted.</p>'; } else { echo '<p style="color: red">Please correct your input.</p>'; } echo "</dd>\n"; } } function get_config() { if ($this->is_ready() && $this->is_valid() && !empty($this->value)) { return "/* $this->name */\n\$this->properties['$this->name'] = array(\n" . " '" . implode("',\n '", $this->value) . "'\n);\n"; } else { return ''; } } } /* class multi_select */ ?> --- NEW FILE --- <?php class pw_option_select extends pw_option { var $choices = array(); function pw_option_select($name, $description, $dependencies, $choices) { $this->pw_option($name, $description, $dependencies, false, key($choices)); $this->choices = $choices; } function is_valid() { return in_array($this->value, array_keys($this->choices)); } function show() { if ($this->is_ready()) { echo "<dt>Option <code>$this->name</code>: "; echo '<select name="' . $this->name . '_value">'; foreach ($this->choices as $choice => $label) { if ($choice == $this->value) { echo '<option selected="selected" value="' . htmlentities($choice) . "\">$label</option>\n"; } else { echo '<option value="' . htmlentities($choice) . "\">$label</option>\n"; } } echo "</select>\n</dt>\n"; echo '<dd><p>' . $this->description . "</p>\n"; if ($this->is_valid()) { echo '<p style="color: green">Input accepted.</p>'; } else { echo '<p style="color: red">Please correct your input: "' . $this->value . '" is not amount your choices.</p>'; } echo "\n</dd>\n"; } } } ?> --- NEW FILE --- <?php class pw_option_text extends pw_option { function pw_option_text($name, $description, $dependencies, $validator = false, $default = false) { $this->pw_option($name, $description, $dependencies, $validator, $default); } function show() { if ($this->is_ready()) { echo "<dt>Option <code>$this->name</code>: "; echo '<input type="text" name="' . $this->name . '_value" value="' . htmlentities($this->value) . "\"></dt>\n"; echo '<dd><p>' . $this->description . "</p>\n"; if ($this->is_valid()) { echo '<p style="color: green">Input accepted.</p>'; } else { echo '<p style="color: red">' . $this->validator->get_error() . '</p>'; } echo "\n</dd>\n"; } } } ?> --- NEW FILE --- <?php class pw_optiongroup { var $title; var $description; var $options; function pw_optiongroup($title, $description, $options) { $this->title = $title; $this->description = $description; $this->options = $options; } function show() { echo "<dt>$this->title <input type=\"submit\" value=\"Update options\"></dt>\n"; echo "<dd><p>$this->description</p>\n"; echo "<dl>\n"; foreach($this->options as $option) { $GLOBALS['options'][$option]->show(); } echo "</dl>\n"; echo "</dd>\n"; } function get_config() { $config = ''; foreach($this->options as $option) { $config .= $GLOBALS['options'][$option]->get_config(); } $stars = "\n/*" . str_repeat('*', 64) . "*/\n"; $top = $stars . '/*' . str_pad($this->title, 64, ' ', STR_PAD_BOTH) . "*/" . $stars; if ($config == '') { return $top . '/* ' . str_pad('All options are at their default values.', 63) . "*/\n"; } else { return $top . '/* ' . str_pad('The following options have been changed:', 63) . "*/\n" . "\n$config\n"; } } } ?> --- NEW FILE --- <?php class pw_validator { var $error; var $value; function pw_validator($error) { $this->error = $error; } function validate($value) { $this->value = $value; return true; } function get_error() { return sprintf($this->error, $this->value); } } ?> --- NEW FILE --- <? class pw_validator_ereg extends pw_validator { var $regex; function pw_validator_ereg($error, $regex) { $this->pw_validator($error); $this->regex = $regex; } function validate($value) { $this->value = $value; return ereg($this->regex, $value); } } ?> --- NEW FILE --- <? class pw_validator_range extends pw_validator { var $low; var $high; var $optional; function pw_validator_range($error, $low, $high, $optional = false) { $this->pw_validator($error); $this->low = $low; $this->high = $high; $this->optional = $optional; } function validate($value) { $this->value = $value; if ($this->optional && empty($value)) return true; return (ereg('^[0-9]+$', $value) && $this->low <= $value && $value <= $this->high); } } ?> --- NEW FILE --- <?php /* This script destroys a session. We start by initializing the * session, then we unset any vaiables associated with it, and then * destroy it. After that is done, we redirect to the index.php in the * current directory. */ session_start(); session_unset(); session_destroy(); header('Location: http://' . $_SERVER['HTTP_HOST'] . dirname($PHP_SELF) . '/index.php'); exit(); ?> |