From 0b819c19fd42dea09b9ef638ebdba8c592ecddb8 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 28 Dec 2010 20:23:02 +0100 Subject: [PATCH] use pear config package to read config file --- README | 7 ++ Wrt3g.php | 54 ++++++----- Wrt3g/Config.php | 190 ++++++++++++++++++++++++++++++++++++++ scripts/linksys-wrt3g.php | 32 +++---- 4 files changed, 240 insertions(+), 43 deletions(-) create mode 100644 Wrt3g/Config.php diff --git a/README b/README index 1c9f260..741ddc1 100644 --- a/README +++ b/README @@ -9,3 +9,10 @@ The routers are also known under the names: - Wireless-G Router for 3G/UMTS Broadband - Linksys WRT54G3G-EU Wireless-G UMTS Router - WRT54G3G + + + +Dependencies +------------ +- PEAR Config package +- PEAR System_Folders package diff --git a/Wrt3g.php b/Wrt3g.php index ee48eca..6b12ac5 100644 --- a/Wrt3g.php +++ b/Wrt3g.php @@ -11,6 +11,7 @@ * @link http://cweiske.de/linksys-wrt3g-tools.htm */ require_once 'HTTP/Request2.php'; +require_once 'Wrt3g/Config.php'; require_once 'Wrt3g/HtmlParser.php'; require_once 'Wrt3g/RequestObserver.php'; @@ -25,27 +26,6 @@ require_once 'Wrt3g/RequestObserver.php'; */ class Wrt3g { - /** - * Router hostname/IP - * - * @var string - */ - public $host; - - /** - * Name of user with administration privileges - * - * @var string - */ - public $user; - - /** - * Password for $user - * - * @var string - */ - public $password; - /** * Logging verbosity * 0 - no debug logging @@ -65,6 +45,30 @@ class Wrt3g */ public $requestClass = 'HTTP_Request2'; + /** + * Configuration object + * + * @var Wrt3g_Config + */ + public $config; + + + + /** + * Loads the configuration from file and cmdline options + * + * @param array $options Command line options array + * + * @return void + * + * @see Wrt3g_Config + */ + public function loadConfig($options) + { + $this->config = new Wrt3g_Config($this); + $this->config->load($options); + } + /** @@ -76,9 +80,9 @@ class Wrt3g protected function getAuthBaseUrl() { return 'http://' - . $this->user - . ':' . $this->password - . '@' . $this->host; + . $this->config->user + . ':' . $this->config->password + . '@' . $this->config->host; } @@ -91,7 +95,7 @@ class Wrt3g */ protected function getAnonBaseUrl() { - return 'http://' . $this->host; + return 'http://' . $this->config->host; } diff --git a/Wrt3g/Config.php b/Wrt3g/Config.php new file mode 100644 index 0000000..f8b9649 --- /dev/null +++ b/Wrt3g/Config.php @@ -0,0 +1,190 @@ + + * @license AGPL v3 + * @link http://cweiske.de/linksys-wrt3g-tools.htm + */ +require_once 'Config.php'; +require_once 'System/Folders.php'; + +/** + * Loads and saves the router configuration data from a config file + * + * @category Tools + * @package linksys-wrt3g-tools + * @author Christian Weiske + * @license AGPL v3 + * @link http://cweiske.de/linksys-wrt3g-tools.htm + */ +class Wrt3g_Config +{ + /** + * Router hostname/IP + * + * @var string + */ + public $host; + + /** + * Name of user with administration privileges + * + * @var string + */ + public $user; + + /** + * Password for $user + * + * @var string + */ + public $password; + + /** + * Object with a "log" method. + * + * @var Wrt3g + */ + public $logger; + + + + /** + * Create new instance + * + * @param object $logger Object with log() method + */ + public function __construct($logger) + { + $this->logger = $logger; + } + + + /** + * Loads the router data from config file and takes the $options + * array into account when set, overwriting the config file values. + * + * Config values are loaded into class variables + * + * @param array $options Array of commandline options. Values used: + * - host - Hostname/IP + * - user - Username to log in with + * - password - Password for user + * + * @return void + */ + public function load($options = array()) + { + $file = $this->getConfigFilePath(); + if ($file) { + $this->loadFromFile($file); + } + $this->loadOptions($options); + + if (isset($options['save'])) { + $this->save($file); + } + } + + + + /** + * Loads the options array from command line into the class + * variables. + * + * @param array $options Array of commandline options. Values used: + * - host - Hostname/IP + * - user - Username to log in with + * - password - Password for user + * + * @return void + */ + protected function loadOptions($options) + { + foreach (array('host', 'user', 'password') as $variable) { + if (isset($options[$variable])) { + $this->$variable = $options[$variable]; + } + } + } + + + + /** + * Loads configuration from config file ("ini" style). + * The file does not need to exist. Config values are stored + * in the public class variables. + * + * @param string $file Config file path to load + * + * @return void + * + * @throws Exception When something went wrong + */ + protected function loadFromFile($file) + { + $this->logger->log('Trying to load config file ' . $file, 2); + $cfg = new Config(); + $root = $cfg->parseConfig($file, 'inifile'); + + if (PEAR::isError($root)) { + $this->logger->log( + 'Error while reading configuration: ' . $root->getMessage(), + 1 + ); + return; + } + $settings = $root->toArray(); + $this->loadOptions($settings['root']); + } + + + + /** + * Returns the full path to the configuration file, if it exists + * or not. + * + * @return string Config file path or NULL if not determinable + */ + public function getConfigFilePath() + { + $sf = new System_Folders(); + $home = $sf->getHome(); + if ($home === null) { + return null; + } + //works on linux only :) + return $home . '.config/linksys-wrt3g-tools'; + } + + + + /** + * Saves the config file + * + * @param string $file Config file path to save into + * + * @return void + */ + public function save($file) + { + $cfg = new Config(); + $cfg->parseConfig($file, 'inifile'); + //ignore read errors + + $root = $cfg->getRoot(); + $root->setDirective('host', $this->host); + $root->setDirective('user', $this->user); + $root->setDirective('password', $this->password); + + $this->logger->log('Saving config into file ' . $file, 2); + $cfg->writeConfig($file, 'inifile'); + } +} + +?> \ No newline at end of file diff --git a/scripts/linksys-wrt3g.php b/scripts/linksys-wrt3g.php index 219e081..f53d76a 100755 --- a/scripts/linksys-wrt3g.php +++ b/scripts/linksys-wrt3g.php @@ -14,18 +14,6 @@ require_once 'Wrt3g.php'; require_once 'Console/CommandLine.php'; -//default config options -$GLOBALS['linksys-wrt3g-tools'] = array( - 'host' => null, - 'user' => 'admin', - 'password' => null, -); - -$configFile = dirname(__FILE__) . '/../config.php'; -if (file_exists($configFile)) { - require_once $configFile; -} - $parser = new Console_CommandLine(); $parser->description = "Tool to control Linksys WRT3g routers"; $parser->version = '0.0.1';//FIXME: dynamic @@ -37,7 +25,7 @@ $parser->addOption( 'description' => 'IP/Hostname to connect to', 'help_name' => 'HOST', 'action' => 'StoreString', - 'default' => $GLOBALS['linksys-wrt3g-tools']['host'] + 'default' => null ) ); $parser->addOption( @@ -48,7 +36,7 @@ $parser->addOption( 'description' => 'Admin user name', 'help_name' => 'USER', 'action' => 'StoreString', - 'default' => $GLOBALS['linksys-wrt3g-tools']['user'] + 'default' => 'admin' ) ); $parser->addOption( @@ -59,7 +47,7 @@ $parser->addOption( 'description' => 'Password for admin user', 'help_name' => 'PASS', 'action' => 'StoreString', - 'default' => $GLOBALS['linksys-wrt3g-tools']['password'] + 'default' => null ) ); $parser->addOption( @@ -111,6 +99,12 @@ $parser->addCommand( 'description' => 'Reboot the router' ) ); +$stCmd = $parser->addCommand( + 'saveConfig', + array( + 'description' => 'Saves the router configuration into the config file' + ) +); try { $result = $parser->parse(); @@ -122,9 +116,7 @@ try { try { $router = new Wrt3g(); $router->verbosity = $result->options['verbosity']; - $router->host = $result->options['host']; - $router->user = $result->options['user']; - $router->password = $result->options['password']; + $router->loadConfig($result->options); if ($result->options['dummy']) { require_once 'Wrt3g/DummyRequest.php'; @@ -143,6 +135,10 @@ try { } break; + case 'saveConfig': + $router->config->save($router->config->getConfigFilePath()); + break; + case 'all': case 'card': case 'status': -- 2.30.2