Thread: CVS: phpweather/config make_config.php,1.9,1.10 pw_option.php,1.4,1.5...
Brought to you by:
iridium
From: Martin G. <gim...@us...> - 2003-07-01 10:15:39
|
Update of /cvsroot/phpweather/phpweather/config In directory sc8-pr-cvs1:/tmp/cvs-serv15345 Modified Files: make_config.php pw_option.php pw_option_integer.php pw_option_multi_select.php pw_option_select.php pw_option_text.php Log Message: Updated the Configuration Builder to match the new dependency classes and added the fetch_method option. Also, the generated PHP code for the defaults.php file now deals correctly with embedded single and double quotes in strings. Not that there's much need for these (except possibly in mark_begin and mark_end) but they're now properly escaped. Index: make_config.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/config/make_config.php,v retrieving revision 1.9 retrieving revision 1.10 diff -u -3 -r1.9 -r1.10 --- make_config.php 20 Apr 2003 13:12:43 -0000 1.9 +++ make_config.php 1 Jul 2003 10:15:36 -0000 1.10 @@ -21,8 +21,9 @@ require_once('pw_optiongroup.php'); /* A couple of dependencies: */ -require_once('pw_dependency.php'); +require_once('pw_dependency_equal.php'); require_once('pw_dependency_or.php'); +require_once('pw_dependency_and.php'); /* We have to strip slashes from the GPC variables. */ if (get_magic_quotes_gpc() == 1) { @@ -49,12 +50,16 @@ if (empty($HTTP_SESSION_VARS)) { /* 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'); - $adodb_dep = new pw_dependency('db_type', 'adodb'); - $adodb_ext_dep = new pw_dependency('db_adodb_ext', 'true'); + $sql_dep = new pw_dependency_or(new pw_dependency_equal('db_type', 'mysql'), + new pw_dependency_equal('db_type', 'pgsql')); + $dba_dep = new pw_dependency_equal('db_type', 'dba'); + $db_dep = new pw_dependency_or($sql_dep, $dba_dep); + + $fsockopen_dep = new pw_dependency_equal('fetch_method', 'fsockopen'); + $proxy_dep = new pw_dependency_equal('use_proxy', 'true'); + + $adodb_dep = new pw_dependency_equal('db_type', 'adodb'); + $adodb_ext_dep = new pw_dependency_equal('db_adodb_ext', 'true'); $not_empty_validator = new pw_validator_ereg("Sorry, the empty string '' cannot be " . "used here", '^.+$'); @@ -84,7 +89,7 @@ "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(), + false, array('1' => 'Errors only', '2' => 'Warnings only', '4' => 'Debug information only', @@ -97,13 +102,13 @@ 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'); + false, $icao_validator, 'EKYT'); $HTTP_SESSION_VARS['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(), + false, array('both_metric' => 'Metric first, then imperial', 'both_imperial' => 'Imperial first, then metric', 'only_metric' => 'Only metric', @@ -114,7 +119,7 @@ 'PHP Weather can produce textual output using ' . 'in several languages - please select your default ' . 'from the list.', - array(), + false, get_languages('text'), 'en'); @@ -125,32 +130,43 @@ "offset here. For example, if your times generated are 1 " . "hour too early (so METARs appear an hour older than they " . "are), set this option to be +1.", - array(), false, 0); + false, false, 0); + + $HTTP_SESSION_VARS['fetch_method'] = + new pw_option_select('fetch_method', + "PHP Weather can fetch the METAR reports from the NWS using " . + "one of two different methods: using either the file() or " . + "the fsockopen() function. You should start with file() and " . + "then change it to fsockopen() if you discover that file() " . + "has been disabled or you need to use a proxy server.", + false, + array('file' => 'Use the file() function', + 'fsockopen' => 'Use the fsockopen() function')); $HTTP_SESSION_VARS['use_proxy'] = new pw_option_boolean('use_proxy', "Set this option to 'Yes' to enable support for a " . "proxy server.", - array(), + $fsockopen_dep, array('false' => 'No', 'true' => 'Yes')); $HTTP_SESSION_VARS['proxy_host'] = new pw_option_text('proxy_host', "This is the hostname of the proxy server.", - array($proxy_dep), $host_validator); + $proxy_dep, $host_validator); $HTTP_SESSION_VARS['proxy_port'] = new pw_option_integer('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); + $proxy_dep, $port_validator, 3128); $HTTP_SESSION_VARS['db_type'] = new pw_option_select('db_type', 'PHP Weather can use several kinds of databases.', - array(), + false, array('null' => 'No database at all', 'mysql' => 'A MySQL database', 'pgsql' => 'A PostgreSQL database', @@ -162,7 +178,7 @@ "We need to know where we can find the ADOdb library. " . "The default is for a Debian GNU/Linux system, but " . "you'll probably have to change it.", - array($adodb_dep), + $adodb_dep, false, // we could try to validate the path... '/usr/share/adodb'); @@ -172,7 +188,7 @@ "wrapper library. You can get a list of drivers from " . '<a href="http://php.weblogs.com/ADODB_Manual#drivers">' . "the ADOdb Manual</a>.", - array($adodb_dep), + $adodb_dep, $not_empty_validator); $HTTP_SESSION_VARS['db_adodb_ext'] = @@ -181,7 +197,7 @@ "then set this option to 'Yes'. PHP might have been " . "compiled with support for MySQL, but the extension " . "isn't necessarily loaded.", - array($adodb_dep), + $adodb_dep, array('false' => 'No', 'true' => 'Yes')); $HTTP_SESSION_VARS['db_adodb_ext_name'] = @@ -191,7 +207,7 @@ "you're using MySQL, then the extension is called " . "'mysql', or if you're using PostgreSQL, then it's " . "called 'pgsql'.", - array($adodb_ext_dep), + $adodb_ext_dep, $not_empty_validator); $HTTP_SESSION_VARS['db_adodb_ext_file'] = @@ -201,7 +217,7 @@ "'mysql' extension with the MySQL database, then the " . "file to load is either 'mysql.so' on a Unix based " . "system, or 'mysql.dll' on a Windows based system.", - array($adodb_ext_dep), + $adodb_ext_dep, $not_empty_validator); $HTTP_SESSION_VARS['db_handler'] = @@ -209,7 +225,7 @@ "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), + $dba_dep, array('dbm' => 'dbm - The oldest (original) type of ' . 'Berkeley DB style databases', 'ndbm' => 'ndbm - a newer and more flexible type.', @@ -223,7 +239,7 @@ "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), + $db_dep, array('false' => 'No', 'true' => 'Yes')); $HTTP_SESSION_VARS['cache_timeout'] = @@ -235,14 +251,15 @@ "seconds (1 hour), but some stations make two " . "reports each hour, so you might want to lower this " . "number to perhaps 2400 or even 1800.", - array(new pw_dependency('always_use_db', 'false'), $db_dep), + new pw_dependency_and(new pw_dependency_equal('always_use_db', 'false'), + $db_dep), false, '3600'); $HTTP_SESSION_VARS['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), + $sql_dep, array('false' => 'No', 'true' => 'Yes')); $HTTP_SESSION_VARS['db_port'] = @@ -250,26 +267,26 @@ '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_empty); + $sql_dep, $port_validator_empty); $HTTP_SESSION_VARS['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); + $sql_dep, $host_validator); $HTTP_SESSION_VARS['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); + $sql_dep, $table_validator); $HTTP_SESSION_VARS['db_username'] = new pw_option_text('db_username', 'This is the username that PHP Weather will use ' . 'for accessing the database.', - array($sql_dep)); + $sql_dep); $HTTP_SESSION_VARS['db_password'] = new pw_option_text('db_password', @@ -277,26 +294,26 @@ '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)); + $sql_dep); $HTTP_SESSION_VARS['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'); + $db_dep, $table_validator, 'pw_metars'); $HTTP_SESSION_VARS['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'); + $db_dep, $table_validator, 'pw_stations'); $HTTP_SESSION_VARS['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'); + $dba_dep, $table_validator, 'pw_countries'); $HTTP_SESSION_VARS['mark_begin'] = @@ -306,14 +323,14 @@ '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>'); + false, false, '<b>'); $HTTP_SESSION_VARS['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>'); + false, false, '</b>'); $HTTP_SESSION_VARS['exclude'] = new pw_option_multi_select('exclude', @@ -322,7 +339,7 @@ '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(), + false, array('time' => 'Leave out the time part', 'wind' => 'Leave out the wind part', 'runway' => 'Leave out information about runways')); @@ -344,6 +361,7 @@ new pw_optiongroup('general_group', 'General Options', 'This is some general options for PHP Weather.', array('verbosity', 'icao', 'pref_units', 'language', 'offset', + 'fetch_method', 'use_proxy', 'proxy_host', 'proxy_port'), !empty($HTTP_POST_VARS['general_group_visible'])); Index: pw_option.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/config/pw_option.php,v retrieving revision 1.4 retrieving revision 1.5 diff -u -3 -r1.4 -r1.5 --- pw_option.php 28 Dec 2002 00:52:54 -0000 1.4 +++ pw_option.php 1 Jul 2003 10:15:36 -0000 1.5 @@ -31,15 +31,11 @@ var $description = ''; /** - * The dependencies of this option. + * The dependency of this option. * - * If this option depends on other options, then their names will be - * listed in this array. If this options doesn't have any - * dependencies, then the array will be empty. - * - * @var array + * @var object */ - var $dependencies = array(); + var $dependency = null; /** * The current value of the option. @@ -73,11 +69,11 @@ */ var $validator; - function pw_option($name, $description, $dependencies, + function pw_option($name, $description, $dependency = false, $validator = false, $default = false) { $this->name = $name; $this->description = $description; - $this->dependencies = $dependencies; + $this->dependency = $dependency; if ($validator) { $this->validator = $validator; } else { @@ -120,22 +116,14 @@ /** * Checks to see if this option is ready to be displayed. * - * When this method is called, the option will go through it's - * dependencies and make sure that they're all satisfied. You - * shouldn't use an option that isn't ready. - * - * @return boolean True if the option is ready to be displayed, - * false otherwise. + * @return boolean Returns true if the option is ready to be + * displayed, false otherwise. */ function is_ready() { - $ready = true; - foreach($this->dependencies as $dependency) { - if (!$dependency->check()) { - $ready = false; - break; - } - } - return $ready; + if (empty($this->dependency)) + return true; + else + return $this->dependency->check(); } /** @@ -155,7 +143,7 @@ * Updates the current value. * * @param array New values. This array should have the same - * structure as $_REQUEST which means that it should contain + * structure as $HTTP_POST_VARS which means that it should contain * option_name_value => value pairs where option_name is the name of * an option. */ @@ -166,6 +154,21 @@ } /** + * Escapes a string. + * + * The string can then be can be wrapped in single quotes and safely + * read back using PHP to get the original string. + * + * @param string The string that should be escaped. + * @return string The escaped string. + */ + function escape_str($str) { + $str = str_replace('\\', '\\\\', $str); + $str = str_replace('\'', '\\\'', $str); + return $str; + } + + /** * Returns the configuration. * * @return string A string suitable for inclusion in the @@ -176,7 +179,7 @@ if ($this->is_ready() && $this->is_valid() && $this->value != $this->default) { return "/* $this->name */\n\$this->properties['$this->name'] = " . - "'$this->value';\n\n"; + "'" . $this->escape_str($this->value) . "';\n\n"; } else { return ''; } Index: pw_option_integer.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/config/pw_option_integer.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -3 -r1.1 -r1.2 --- pw_option_integer.php 12 Apr 2002 22:01:49 -0000 1.1 +++ pw_option_integer.php 1 Jul 2003 10:15:36 -0000 1.2 @@ -1,12 +1,14 @@ <?php class pw_option_integer extends pw_option_text { - function pw_option_integer($name, $description, $dependencies, + function pw_option_integer($name, $description, $dependency = false, $validator = false, $default = false) { if (!$validator) { - $validator = new pw_validator_ereg("Sorry, '%s' is not an integer.", '^[-+]?[0-9]+$'); + $validator = new pw_validator_ereg("Sorry, '%s' is not an integer.", + '^[-+]?[0-9]+$'); } - $this->pw_option_text($name, $description, $dependencies, $validator, $default); + $this->pw_option_text($name, $description, $dependency, + $validator, $default); } function get_config() { Index: pw_option_multi_select.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/config/pw_option_multi_select.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -3 -r1.1 -r1.2 --- pw_option_multi_select.php 7 Apr 2002 18:38:20 -0000 1.1 +++ pw_option_multi_select.php 1 Jul 2003 10:15:36 -0000 1.2 @@ -4,8 +4,8 @@ var $choices = array(); - function pw_option_multi_select($name, $description, $dependencies, $choices) { - $this->pw_option($name, $description, $dependencies); + function pw_option_multi_select($name, $description, $dependency = false, $choices) { + $this->pw_option($name, $description, $dependency); $this->choices = $choices; } @@ -51,8 +51,12 @@ 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"; + /* Escape the value using the static pw_option::escape_str + * method as a callback */ + $escaped = array_map(array('pw_option', 'escape_str'), $this->value); + return "/* $this->name */\n" . + "\$this->properties['$this->name'] = array(\n" . + " '" . implode("',\n '", $escaped) . "'\n);\n"; } else { return ''; } Index: pw_option_select.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/config/pw_option_select.php,v retrieving revision 1.2 retrieving revision 1.3 diff -u -3 -r1.2 -r1.3 --- pw_option_select.php 28 Dec 2002 00:52:54 -0000 1.2 +++ pw_option_select.php 1 Jul 2003 10:15:36 -0000 1.3 @@ -4,12 +4,12 @@ var $choices = array(); - function pw_option_select($name, $description, $dependencies, + function pw_option_select($name, $description, $dependency = false, $choices, $default = false) { if ($default && isset($choices[$default])) { - $this->pw_option($name, $description, $dependencies, false, $default); + $this->pw_option($name, $description, $dependency, false, $default); } else { - $this->pw_option($name, $description, $dependencies, false, key($choices)); + $this->pw_option($name, $description, $dependency, false, key($choices)); } $this->choices = $choices; } @@ -19,9 +19,11 @@ } function show() { + if ($this->is_ready()) { - echo "<dt>Option <code>$this->name</code>: "; + 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="' . @@ -36,12 +38,12 @@ 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>'; + $this->value . '" is not among your choices.</p>'; } echo "\n</dd>\n"; } } - + } ?> Index: pw_option_text.php =================================================================== RCS file: /cvsroot/phpweather/phpweather/config/pw_option_text.php,v retrieving revision 1.3 retrieving revision 1.4 diff -u -3 -r1.3 -r1.4 --- pw_option_text.php 29 May 2002 20:42:56 -0000 1.3 +++ pw_option_text.php 1 Jul 2003 10:15:36 -0000 1.4 @@ -1,18 +1,20 @@ <?php class pw_option_text extends pw_option { - function pw_option_text($name, $description, $dependencies, + function pw_option_text($name, $description, $dependency, $validator = false, $default = false) { - $this->pw_option($name, $description, $dependencies, $validator, $default); + $this->pw_option($name, $description, $dependency, $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) . '" onkeyup="' . $this->validator->get_javascript($this->name) . "\" /></dt>\n"; + echo '<dd><p>' . $this->description . "</p>\n"; if ($this->is_valid()) { echo '<p id="' . $this->name . '"><font color="green">Input accepted.</font></p>'; |