* @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()) { //system config file $this->loadFromFile('/etc/linksys-wrt3g-tools'); //user config file $file = $this->getConfigFilePath(); if ($file) { $this->loadFromFile($file); } //environment variables: we use lowercase options, which should // not collide with the uppercase env variables like USER $this->loadOptions($_SERVER); //commandline options $this->loadOptions($options); $this->logger->log( sprintf( "Configuration: %s:%s@%s", $this->user, $this->password, $this->host ), 2 ); $this->verify(); 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'); } /** * Verifies that all required options are set * * @return void * * @throw Exception When something is missing */ protected function verify() { if ($this->host == '') { throw new Exception('Hostname missing'); } } } ?>