3 * Part of Linksys WRT3G tools
8 * @package linksys-wrt3g-tools
9 * @author Christian Weiske <cweiske@cweiske.de>
11 * @link http://cweiske.de/linksys-wrt3g-tools.htm
13 require_once 'Config.php';
14 require_once 'System/Folders.php';
17 * Loads and saves the router configuration data from a config file
20 * @package linksys-wrt3g-tools
21 * @author Christian Weiske <cweiske@cweiske.de>
23 * @link http://cweiske.de/linksys-wrt3g-tools.htm
35 * Name of user with administration privileges
49 * Object with a "log" method.
60 * @param object $logger Object with log() method
62 public function __construct($logger)
64 $this->logger = $logger;
69 * Loads the router data from config file and takes the $options
70 * array into account when set, overwriting the config file values.
72 * Config values are loaded into class variables
74 * @param array $options Array of commandline options. Values used:
75 * - host - Hostname/IP
76 * - user - Username to log in with
77 * - password - Password for user
81 public function load($options = array())
83 $file = $this->getConfigFilePath();
85 $this->loadFromFile($file);
87 $this->loadOptions($options);
89 if (isset($options['save'])) {
97 * Loads the options array from command line into the class
100 * @param array $options Array of commandline options. Values used:
101 * - host - Hostname/IP
102 * - user - Username to log in with
103 * - password - Password for user
107 protected function loadOptions($options)
109 foreach (array('host', 'user', 'password') as $variable) {
110 if (isset($options[$variable])) {
111 $this->$variable = $options[$variable];
119 * Loads configuration from config file ("ini" style).
120 * The file does not need to exist. Config values are stored
121 * in the public class variables.
123 * @param string $file Config file path to load
127 * @throws Exception When something went wrong
129 protected function loadFromFile($file)
131 $this->logger->log('Trying to load config file ' . $file, 2);
133 $root = $cfg->parseConfig($file, 'inifile');
135 if (PEAR::isError($root)) {
137 'Error while reading configuration: ' . $root->getMessage(),
142 $settings = $root->toArray();
143 $this->loadOptions($settings['root']);
149 * Returns the full path to the configuration file, if it exists
152 * @return string Config file path or NULL if not determinable
154 public function getConfigFilePath()
156 $sf = new System_Folders();
157 $home = $sf->getHome();
158 if ($home === null) {
161 //works on linux only :)
162 return $home . '.config/linksys-wrt3g-tools';
168 * Saves the config file
170 * @param string $file Config file path to save into
174 public function save($file)
177 $cfg->parseConfig($file, 'inifile');
180 $root = $cfg->getRoot();
181 $root->setDirective('host', $this->host);
182 $root->setDirective('user', $this->user);
183 $root->setDirective('password', $this->password);
185 $this->logger->log('Saving config into file ' . $file, 2);
186 $cfg->writeConfig($file, 'inifile');