diff options
Diffstat (limited to 'Wrt3g/Config.php')
| -rw-r--r-- | Wrt3g/Config.php | 190 |
1 files changed, 190 insertions, 0 deletions
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 @@ +<?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 |
