* @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';
*/
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
*/
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);
+ }
+
/**
protected function getAuthBaseUrl()
{
return 'http://'
- . $this->user
- . ':' . $this->password
- . '@' . $this->host;
+ . $this->config->user
+ . ':' . $this->config->password
+ . '@' . $this->config->host;
}
*/
protected function getAnonBaseUrl()
{
- return 'http://' . $this->host;
+ return 'http://' . $this->config->host;
}
--- /dev/null
+<?php
+/**
+ * Part of Linksys WRT3G tools
+ *
+ * PHP version 5
+ *
+ * @category Tools
+ * @package linksys-wrt3g-tools
+ * @author Christian Weiske <cweiske@cweiske.de>
+ * @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 <cweiske@cweiske.de>
+ * @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
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
'description' => 'IP/Hostname to connect to',
'help_name' => 'HOST',
'action' => 'StoreString',
- 'default' => $GLOBALS['linksys-wrt3g-tools']['host']
+ 'default' => null
)
);
$parser->addOption(
'description' => 'Admin user name',
'help_name' => 'USER',
'action' => 'StoreString',
- 'default' => $GLOBALS['linksys-wrt3g-tools']['user']
+ 'default' => 'admin'
)
);
$parser->addOption(
'description' => 'Password for admin user',
'help_name' => 'PASS',
'action' => 'StoreString',
- 'default' => $GLOBALS['linksys-wrt3g-tools']['password']
+ 'default' => null
)
);
$parser->addOption(
'description' => 'Reboot the router'
)
);
+$stCmd = $parser->addCommand(
+ 'saveConfig',
+ array(
+ 'description' => 'Saves the router configuration into the config file'
+ )
+);
try {
$result = $parser->parse();
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';
}
break;
+ case 'saveConfig':
+ $router->config->save($router->config->getConfigFilePath());
+ break;
+
case 'all':
case 'card':
case 'status':