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 $this->loadOptions($options);
94 if (isset($options['save'])) {
102 * Loads the options array from command line into the class
105 * @param array $options Array of commandline options. Values used:
106 * - host - Hostname/IP
107 * - user - Username to log in with
108 * - password - Password for user
112 protected function loadOptions($options)
114 foreach (array('host', 'user', 'password') as $variable) {
115 if (isset($options[$variable])) {
116 $this->$variable = $options[$variable];
124 * Loads configuration from config file ("ini" style).
125 * The file does not need to exist. Config values are stored
126 * in the public class variables.
128 * @param string $file Config file path to load
132 * @throws Exception When something went wrong
134 protected function loadFromFile($file)
136 $this->logger->log('Trying to load config file ' . $file, 2);
138 $root = $cfg->parseConfig($file, 'inifile');
140 if (PEAR::isError($root)) {
142 'Error while reading configuration: ' . $root->getMessage(),
147 $settings = $root->toArray();
148 $this->loadOptions($settings['root']);
154 * Returns the full path to the configuration file, if it exists
157 * @return string Config file path or NULL if not determinable
159 public function getConfigFilePath()
161 $sf = new System_Folders();
162 $home = $sf->getHome();
163 if ($home === null) {
166 //works on linux only :)
167 return $home . '.config/linksys-wrt3g-tools';
173 * Saves the config file
175 * @param string $file Config file path to save into
179 public function save($file)
182 $cfg->parseConfig($file, 'inifile');
185 $root = $cfg->getRoot();
186 $root->setDirective('host', $this->host);
187 $root->setDirective('user', $this->user);
188 $root->setDirective('password', $this->password);
190 $this->logger->log('Saving config into file ' . $file, 2);
191 $cfg->writeConfig($file, 'inifile');