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())
84 $this->loadFromFile('/etc/linksys-wrt3g-tools');
87 $file = $this->getConfigFilePath();
89 $this->loadFromFile($file);
91 //environment variables: we use lowercase options, which should
92 // not collide with the uppercase env variables like USER
93 $this->loadOptions($_SERVER);
95 $this->loadOptions($options);
99 "Configuration: %s:%s@%s",
100 $this->user, $this->password, $this->host
107 if (isset($options['save'])) {
115 * Loads the options array from command line into the class
118 * @param array $options Array of commandline options. Values used:
119 * - host - Hostname/IP
120 * - user - Username to log in with
121 * - password - Password for user
125 protected function loadOptions($options)
127 foreach (array('host', 'user', 'password') as $variable) {
128 if (isset($options[$variable])) {
129 $this->$variable = $options[$variable];
137 * Loads configuration from config file ("ini" style).
138 * The file does not need to exist. Config values are stored
139 * in the public class variables.
141 * @param string $file Config file path to load
145 * @throws Exception When something went wrong
147 protected function loadFromFile($file)
149 $this->logger->log('Trying to load config file ' . $file, 2);
151 $root = $cfg->parseConfig($file, 'inifile');
153 if (PEAR::isError($root)) {
155 'Error while reading configuration: ' . $root->getMessage(),
160 $settings = $root->toArray();
161 $this->loadOptions($settings['root']);
167 * Returns the full path to the configuration file, if it exists
170 * @return string Config file path or NULL if not determinable
172 public function getConfigFilePath()
174 $sf = new System_Folders();
175 $home = $sf->getHome();
176 if ($home === null) {
179 //works on linux only :)
180 return $home . '.config/linksys-wrt3g-tools';
186 * Saves the config file
188 * @param string $file Config file path to save into
192 public function save($file)
195 $cfg->parseConfig($file, 'inifile');
198 $root = $cfg->getRoot();
199 $root->setDirective('host', $this->host);
200 $root->setDirective('user', $this->user);
201 $root->setDirective('password', $this->password);
203 $this->logger->log('Saving config into file ' . $file, 2);
204 $cfg->writeConfig($file, 'inifile');
210 * Verifies that all required options are set
214 * @throw Exception When something is missing
216 protected function verify()
218 if ($this->host == '') {
219 throw new Exception('Hostname missing');