use pear config package to read config file
authorChristian Weiske <cweiske@cweiske.de>
Tue, 28 Dec 2010 19:23:02 +0000 (20:23 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 28 Dec 2010 19:23:02 +0000 (20:23 +0100)
README
Wrt3g.php
Wrt3g/Config.php [new file with mode: 0644]
scripts/linksys-wrt3g.php

diff --git a/README b/README
index 1c9f2607a239733856d17acbfd74f2d82bf5a32f..741ddc1a199f9dba5669834758dbd72966ac65c7 100644 (file)
--- 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
index ee48eca8437c4db7c73773ca767b6c3896eff902..6b12ac5c7b7fac167876c8d1fc1c8c645ea7547d 100644 (file)
--- 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 (file)
index 0000000..f8b9649
--- /dev/null
@@ -0,0 +1,190 @@
+<?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
index 219e081c1c87be924830d8e756b32e1c4a622dac..f53d76a819364642d83f1918cc9ea1e60b30894b 100755 (executable)
 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':