From: Stig Bakken Date: Fri, 26 Oct 2001 10:07:43 +0000 (+0000) Subject: * PEAR_Config class to maintain system and per-user configuration for X-Git-Tag: POST_PARAMETER_PARSING_API~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f197cf36301af5f09dd4d755b9961b718ef232d0;p=php * PEAR_Config class to maintain system and per-user configuration for PEAR installations * PEAR_Remote class to communicate with the backend server (xmlrpc) * change pear script's option parsing to "-d foo=bar" style * added -c/-C (user/system config file) and -s/-S (store user/system config) options --- diff --git a/pear/Makefile.in b/pear/Makefile.in index 5ce5e3aa0a..9d8cf36208 100644 --- a/pear/Makefile.in +++ b/pear/Makefile.in @@ -100,6 +100,7 @@ PEAR_FILES = \ Numbers/Roman.php \ PEAR.php \ PEAR/Common.php \ + PEAR/Config.php \ PEAR/Installer.php \ PEAR/Packager.php \ PEAR/Uploader.php \ diff --git a/pear/PEAR/Config.php b/pear/PEAR/Config.php new file mode 100644 index 0000000000..6c36e9b981 --- /dev/null +++ b/pear/PEAR/Config.php @@ -0,0 +1,213 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ + +require_once 'PEAR.php'; + +/** + * This is a class for storing simple configuration values keeping + * track of which are system-defined (defaulted) and which are + * user-defined. By default, only used-defined settings are stored + * back to the user's configuration file. + * + * Configuration member is a simple associative array. Used keys: + * + * master_server which server to query for mirror lists etc. + * server which server/mirror we're currently using + * username PEAR username + * password PEAR password (stored base64-encoded) + * php_dir Where to install .php files + * ext_dir Directory to install compiled libs in + * doc_dir Directory to install documentation in + * + */ +class PEAR_Config extends PEAR +{ + // {{{ properties + + var $defaults_file = ''; + + var $config_file = ''; + + var $configuration = array(); + + var $defaulted = array(); + + // }}} + + // {{{ PEAR_Config([file], [defaults_file]) + + function PEAR_Config($file = '', $defaults_file = '') + { + $this->PEAR(); + $this->config_file = $file; + $this->defaults_file = $defaults_file; + if ($file && file_exists($file)) { + $this->readConfigFile($file); + } + if ($defaults_file && file_exists($defaults_file)) { + $this->mergeConfigFile($defaults_file, false, true); + } + } + + // }}} + + // {{{ readConfigFile([file], [defaults]) + + function readConfigFile($file = null, $defaults = false) + { + if ($file === null) { + $file = $this->config_file; + } + $fp = @fopen($file, "r"); + if (!$fp) { + return $this->raiseError($php_errormsg); + } + $contents = fread($fp, filesize($file)); + $data = unserialize($contents); + if ($data === false) { + return $this->raiseError("PEAR_Config::readConfigFile: bad data"); + } + $this->configuration = $data; + if ($defaults) { + foreach ($data as $key => $value) { + $this->defaulted[$key] = true; + } + } + } + + // }}} + // {{{ mergeConfigFile(file, [override], [defaults]) + + function mergeConfigFile($file, $override = true, $defaults = false) + { + $fp = @fopen($file, "r"); + if (!$fp) { + return $this->raiseError($php_errormsg); + } + $contents = fread($fp, filesize($file)); + $data = unserialize($contents); + if ($data === false) { + return $this->raiseError("PEAR_Config::mergeConfigFile: bad data"); + } + foreach ($data as $key => $value) { + if (isset($this->configuration[$key]) && !$override) { + continue; + } + if ($defaults) { + $this->defaulted[$key] = true; + } + $this->configuration[$key] = $value; + } + } + + // }}} + // {{{ writeConfigFile([file], [what_keys]) + + function writeConfigFile($file = null, $what_keys = 'userdefined') + { + print "storing $what_keys keys\n"; + if ($what_keys == 'both') { + $this->writeConfigFile($file, 'userdefined'); + $this->writeConfigFile($file, 'default'); + } + if ($file === null) { + if ($what_keys == 'default') { + $file = $this->defaults_file; + } else { + $file = $this->config_file; + } + } + if ($what_keys == 'default') { + $keys_to_store = array_intersect(array_keys($this->configuration), + array_keys($this->defaulted)); + } elseif ($what_keys == 'all') { + $keys_to_store = array_keys($this->configuration); + } else { // user-defined keys + $keys_to_store = array_diff(array_keys($this->configuration), + array_keys($this->defaulted)); + } + $data = array(); + foreach ($keys_to_store as $key) { + $data[$key] = $this->configuration[$key]; + } + $fp = @fopen($file, "w"); + if (!$fp) { + return $this->raiseError("PEAR_Config::writeConfigFile fopen failed"); + } + if (!@fwrite($fp, serialize($data))) { + return $this->raiseError("PEAR_Config::writeConfigFile serialize failed"); + } + return true; + } + + // }}} + // {{{ get(key) + + function get($key) + { + return @$this->configuration[$key]; + } + + // }}} + // {{{ set(key, value, [default]) + + function set($key, $value, $default = false) + { + $this->configuration[$key] = $value; + if ($default) { + $this->defaulted[$key] = true; + } elseif (isset($this->defaulted[$key])) { + unset($this->defaulted[$key]); + } + } + + // }}} + // {{{ toDefault(key) + + function toDefault($key) + { + if (file_exists($this->defaults_file)) { + // re-reads the defaults file each time, but hey it works + unset($this->configuration[$key]); + $this->mergeConfigFile($this->defaults_file, false, true); + } + } + + // }}} + // {{{ isDefaulted(key) + + function isDefaulted($key) + { + return isset($this->defaulted[$key]); + } + + // }}} + // {{{ isDefined(key) + + function isDefined($key) + { + return isset($this->configuration[$key]); + } + + // }}} +} + +?> \ No newline at end of file diff --git a/pear/PEAR/Remote.php b/pear/PEAR/Remote.php new file mode 100644 index 0000000000..b813215176 --- /dev/null +++ b/pear/PEAR/Remote.php @@ -0,0 +1,78 @@ + | +// | | +// +----------------------------------------------------------------------+ +// +// $Id$ + +require_once 'PEAR.php'; + +/** + * This is a class for doing remote operations against the central + * PEAR database. + */ +class PEAR_Remote extends PEAR +{ + // {{{ properties + + var $config_object = null; + + // }}} + + // {{{ PEAR_Remote(config_object) + + function PEAR_Remote($config_object) + { + $this->PEAR(); + $this->config_object = $config_object; + } + + // }}} + + // {{{ call(method, [args...]) + + function call($method) + { + if (!extension_loaded("xmlrpc")) { + return $this->raiseError("xmlrpc support not loaded"); + } + $params = array_slice(func_get_args(), 1); + $request = xmlrpc_encode_request($method, $params); + print "

Request:

\n"; + print "

";
+        print htmlspecialchars($request);
+        print "
"; + $fp = @fsockopen($config->get("master_server"), 80); + if (!$fp) { + return $this->raiseError("PEAR_Remote::call: connect failed"); + } + $len = strlen($request); + fwrite($fp, ("POST /xmlrpc.php HTTP/1.0\r\n". + "Content-type: text/xml\r\n". + "Content-length: $len\r\n". + "\r\n$request")); + print "Response:\n"; + while ($line = fgets($fp, 2048)) { + print $line; + } + // XXX UNFINISHED + } + + // }}} +} + +?> \ No newline at end of file diff --git a/pear/scripts/pear.in b/pear/scripts/pear.in index 08adc72573..c5f7ec642b 100644 --- a/pear/scripts/pear.in +++ b/pear/scripts/pear.in @@ -19,52 +19,90 @@ // +----------------------------------------------------------------------+ // require_once 'PEAR.php'; +require_once 'PEAR/Config.php'; require_once 'Console/Getopt.php'; error_reporting(E_ALL ^ E_NOTICE); -$options = Console_Getopt::getopt($argv, "h?v:e:p:d:"); +// {{{ config file and option parsing + +$options = Console_Getopt::getopt($argv, "c:C:d:D:h?sS"); if (PEAR::isError($options)) { usage($options); } +if (OS_WINDOWS) { + $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pearsys.ini'; + $pear_user_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.ini'; +} else { + $pear_default_config = PHP_SYSCONFDIR.DIRECTORY_SEPARATOR.'pear.conf'; + $pear_user_config = $HTTP_ENV_VARS['HOME'].DIRECTORY_SEPARATOR.'.pearrc'; +} + $opts = $options[0]; + +foreach ($opts as $opt) { + switch ($opt[0]) { + case 'c': + $pear_user_config = $opt[1]; + break; + case 'C': + $pear_default_config = $opt[1]; + break; + } +} + +$config = new PEAR_Config($pear_user_config, $pear_default_config); +$store_user_config = false; +$store_default_config = false; + foreach ($opts as $opt) { $param = $opt[1]; switch ($opt[0]) { - case 'v': - $verbose = $param; + case 'd': + list($key, $value) = explode('=', $param); + $config->set($key, $value); break; - case 'e': - if ($param{0} != DIRECTORY_SEPARATOR) { - usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n")); - } - $ext_dir = $param; + case 'D': + list($key, $value) = explode('=', $param); + $config->set($key, $value, true); break; - case 'p': - if ($param{0} != DIRECTORY_SEPARATOR) { - usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n")); - } - $script_dir = $param; + case 's': + $store_user_config = true; break; - case 'd': - if ($param{0} != DIRECTORY_SEPARATOR) { - usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n")); - } - $doc_dir = $param; + case 'S': + $store_default_config = true; break; } } -$verbose = (isset($verbose)) ? $verbose : 1; -$script_dir = (isset($script_dir)) ? $script_dir : PEAR_INSTALL_DIR; -$ext_dir = (isset($ext_dir)) ? $ext_dir : PEAR_EXTENSION_DIR; -$doc_dir = (isset($doc_dir)) ? $doc_dir : ''; +$fallback_config = array( + 'php_dir' => PEAR_INSTALL_DIR, + 'ext_dir' => PEAR_EXTENSION_DIR, + 'doc_dir' => '', + 'verbose' => true, +); + +foreach ($fallback_config as $key => $value) { + if (!$config->isDefined($key)) { + $config->set($key, $value); + } +} + +$verbose = $config->get("verbose"); +$script_dir = $config->get("php_dir"); +$ext_dir = $config->get("ext_dir"); +$doc_dir = $config->get("doc_dir"); + +// }}} + +PEAR::setErrorHandling(PEAR_ERROR_PRINT, "pear: %s\n"); -PEAR::setErrorHandling(PEAR_ERROR_PRINT); $command = $options[1][1]; switch ($command) { - case 'install': + // {{{ install + + case 'install': { include_once 'PEAR/Installer.php'; $package = $options[1][2]; $installer =& new PEAR_Installer($script_dir, $ext_dir, $doc_dir); @@ -75,7 +113,12 @@ switch ($command) { print "install ok\n"; } break; - case 'package': + } + + // }}} + // {{{ package + + case 'package': { include_once 'PEAR/Packager.php'; $pkginfofile = $options[1][2]; $packager =& new PEAR_Packager($script_dir, $ext_dir, $doc_dir); @@ -86,19 +129,37 @@ switch ($command) { print "package ok\n"; } break; - default: - usage(); + } + + // }}} + // {{{ list-packages + + case 'list-packages': { + include_once 'PEAR/Remote.php'; + $remote = new PEAR_Remote($config); + } + + // }}} + default: { + if (!$store_default_config && !$store_user_config) { + usage(); + } break; + } } -function usage($obj = null) +// {{{ usage() + +function usage($error = null) { $stderr = fopen('php://stderr', 'w'); - if ($obj !== null) { - fputs($stderr, $obj->getMessage()); + if (PEAR::isError($obj)) { + fputs($stderr, $error->getMessage()); + } elseif ($obj !== null) { + fputs($stderr, $error); } fputs($stderr, - "Usage: pear [-v n] [-h] [-p ] [-e ] [-d ] command \n". + "Usage: pear [options] command \n". "Options:\n". " -v set verbosity level to (0-2, default 1)\n". " -p set script install dir (absolute path)\n". @@ -113,4 +174,6 @@ function usage($obj = null) exit; } +// }}} + ?> diff --git a/pear/scripts/pearwin.php b/pear/scripts/pearwin.php index 2d75b3766a..2e56d4f77f 100644 --- a/pear/scripts/pearwin.php +++ b/pear/scripts/pearwin.php @@ -38,19 +38,19 @@ foreach ($opts as $opt) { break; case 'e': if ($param{0} != getenv('DIRECTORY_SEPARATOR')) { - usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n")); + usage (new PEAR_Error("no absolute path (eg. /usr/lib/php)\n")); } $ext_dir = $param; break; case 'p': if ($param{0} != getenv('DIRECTORY_SEPARATOR')) { - usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n")); + usage (new PEAR_Error("no absolute path (eg. /usr/lib/php)\n")); } $script_dir = $param; break; case 'd': if ($param{0} != getenv('DIRECTORY_SEPARATOR')) { - usage (new PEAR_Error("no absolute path (ej. /usr/lib/php)\n")); + usage (new PEAR_Error("no absolute path (eg. /usr/lib/php)\n")); } $doc_dir = $param; break;