1 /*
2         +-----------------------------------------------------------------------------+
3         | ILIAS open source                                                           |
4         +-----------------------------------------------------------------------------+
5         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
6         |                                                                             |
7         | This program is free software; you can redistribute it and/or               |
8         | modify it under the terms of the GNU General Public License                 |
9         | as published by the Free Software Foundation; either version 2              |
10         | of the License, or (at your option) any later version.                      |
11         |                                                                             |
12         | This program is distributed in the hope that it will be useful,             |
13         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
14         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
15         | GNU General Public License for more details.                                |
16         |                                                                             |
17         | You should have received a copy of the GNU General Public License           |
18         | along with this program; if not, write to the Free Software                 |
19         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
20         +-----------------------------------------------------------------------------+
21 */
22 
23 package de.ilias.services.settings;
24 
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.io.InputStreamReader;
31 import java.io.StringReader;
32 import java.util.prefs.Preferences;
33 
34 import org.apache.logging.log4j.LogManager;
35 import org.apache.logging.log4j.Logger;
36 import org.ini4j.Ini;
37 import org.ini4j.IniPreferences;
38 
39 /**
40  * Parser for ini files.
41  * Stores ini values in ServerSettings and ClientSettings
42  *
43  * @author Stefan Meyer <smeyer.ilias@gmx.de>
44  * @version $Id$
45  */
46 public class IniFileParser {
47 
48 	Logger logger = LogManager.getLogger(IniFileParser.class);
49 
50 	/**
51 	 *
52 	 */
IniFileParser()53 	public IniFileParser() {
54 
55 	}
56 
57 	/**
58 	 *
59 	 * @param path
60 	 * @throws ConfigurationException
61 	 */
parseServerSettings(String path, boolean parseClientSettings)62 	public void parseServerSettings(String path, boolean parseClientSettings) throws ConfigurationException {
63 
64 		Ini prefs;
65 		ServerSettings serverSettings = ServerSettings.getInstance();
66 		ClientSettings clientSettings;
67 		try {
68 
69 			prefs = new Ini(new FileReader(path));
70 			for(Ini.Section section : prefs.values()) {
71 
72 				if(section.getName().equals("Server")) {
73 					if(section.containsKey("IpAddress"))
74 						serverSettings.setHost(purgeString(section.get("IpAddress")));
75 					if(section.containsKey("Port"))
76 						serverSettings.setPort(purgeString(section.get("Port")));
77 					if(section.containsKey("IndexPath"))
78 						serverSettings.setIndexPath(purgeString(section.get("IndexPath")));
79 					if(section.containsKey("LogFile"))
80 						serverSettings.setLogFile(purgeString(section.get("LogFile")));
81 					if(section.containsKey("LogLevel"))
82 						serverSettings.setLogLevel(purgeString(section.get("LogLevel")));
83 					//serverSettings.initLogManager();
84 					if(section.containsKey("NumThreads"))
85 						serverSettings.setThreadNumber(purgeString(section.get("NumThreads")));
86 					if(section.containsKey("RAMBufferSize"))
87 						serverSettings.setRAMSize(purgeString(section.get("RAMBufferSize")));
88 					if(section.containsKey("IndexMaxFileSizeMB"))
89 						serverSettings.setMaxFileSizeMB(purgeString(section.get("IndexMaxFileSizeMB")));
90 				}
91 				if(section.getName().startsWith("Client") && parseClientSettings) {
92 					if(section.containsKey("ClientId")) {
93 						String client = purgeString(section.get("ClientId"));
94 						String nic;
95 						if(section.containsKey("NicId"))
96 							nic = purgeString(section.get("NicId"));
97 						else
98 							nic = "0";
99 						clientSettings = ClientSettings.getInstance(client, nic);
100 						if(section.containsKey("IliasIniPath")) {
101 							clientSettings.setIliasIniFile(purgeString(section.get("IliasIniPath")));
102 
103 							// Now parse the ilias.ini file
104 							parseClientData(clientSettings);
105 						}
106 					}
107 					else {
108 						logger.error("No ClientId given for section: " + section.getName());
109 						throw new ConfigurationException("No ClientId given for section: " + section.getName());
110 					}
111 				}
112 			}
113 
114 		}
115 		catch (ConfigurationException e) {
116 			logger.error("Cannot parse server settings: " + e.getMessage());
117 			throw new ConfigurationException(e);
118 		}
119 		catch (IOException e) {
120 			logger.error("Cannot parse server settings: " + e.getMessage());
121 			throw new ConfigurationException(e);
122 		}
123 	}
124 
125 	/**
126 	 * @param clientSettings
127 	 * @throws ConfigurationException
128 	 */
parseClientData(ClientSettings clientSettings)129 	public void parseClientData(ClientSettings clientSettings) throws ConfigurationException {
130 
131 		Preferences prefs;
132 		try {
133 			// parse ilias.ini.php
134 			prefs = new IniPreferences(convertIniFile(clientSettings.getIliasIniFile()));
135 			clientSettings.setDataDirectory(purgeString(prefs.node("clients").get("datadir",""),true));
136 			clientSettings.setAbsolutePath(purgeString(prefs.node("server").get("absolute_path",""),true));
137 
138 			String dataName = purgeString(prefs.node("clients").get("path", ""),true);
139 			String iniFileName = purgeString(prefs.node("clients").get("inifile",""),true);
140 
141 			clientSettings.setClientIniFile(clientSettings.getAbsolutePath().getCanonicalPath() +
142 					System.getProperty("file.separator") +
143 					dataName + System.getProperty("file.separator") +
144 					clientSettings.getClient() + System.getProperty("file.separator") +
145 					iniFileName);
146 			clientSettings.setIndexPath(ServerSettings.getInstance().getIndexPath() +
147 					System.getProperty("file.separator") +
148 					clientSettings.getClientKey());
149 			// now parse client.ini.php
150 			prefs = new IniPreferences(convertIniFile(clientSettings.getClientIniFile()));
151 
152 			clientSettings.setDbType(purgeString(prefs.node("db").get("type",""),true));
153 			clientSettings.setDbHost(purgeString(prefs.node("db").get("host",""),true));
154 			clientSettings.setDbPort(purgeString(prefs.node("db").get("port",""),true));
155 			clientSettings.setDbUser(purgeString(prefs.node("db").get("user",""),true));
156 			clientSettings.setDbPass(purgeString(prefs.node("db").get("pass",""),true));
157 			clientSettings.setDbName(purgeString(prefs.node("db").get("name",""),true));
158 
159 			logger.debug("Client ID: " + clientSettings.getClient());
160 			logger.debug("DB Type: " + clientSettings.getDbType());
161 			logger.debug("DB Host: " +clientSettings.getDbHost());
162 			logger.debug("DB Port: " + clientSettings.getDbPort());
163 			logger.debug("DB Name: " +clientSettings.getDbName());
164 			logger.debug("DB User: " +clientSettings.getDbUser());
165 			logger.debug("DB Pass: " +clientSettings.getDbPass());
166 
167 		}
168 		catch (IOException e) {
169 			logger.error("Caught IOException when trying to parse client data: " + e.getMessage());
170 			throw new ConfigurationException(e);
171 		}
172 		catch (ConfigurationException e) {
173 			logger.error("Caught ConfigurationException when trying to parse client data.");
174 			throw e;
175 		}
176 
177 	}
178 
179 	/**
180 	 *
181 	 * @param dirty
182 	 * @param replaceQuotes
183 	 * @return
184 	 */
purgeString(String dirty,boolean replaceQuotes)185 	public String purgeString(String dirty,boolean replaceQuotes) {
186 
187 		if(replaceQuotes) {
188 			return dirty.replace('"',' ').trim();
189 		}
190 		else {
191 			return dirty.trim();
192 		}
193 	}
194 
195 	/**
196 	 *
197 	 * @param dirty
198 	 * @return
199 	 */
purgeString(String dirty)200 	public String purgeString(String dirty) {
201 
202 		return purgeString(dirty,false);
203 	}
204 
205 	/**
206 	 *
207 	 * @return
208 	 * @throws ConfigurationException
209 	 */
convertIniFile(File iniFile)210 	private StringReader convertIniFile(File iniFile) throws ConfigurationException {
211 
212 		try {
213 			String output;
214 			InputStreamReader reader = new InputStreamReader(new FileInputStream(iniFile));
215 
216 			int c;
217 			StringBuilder builder = new StringBuilder();
218 
219 			while((c = reader.read())!=-1){
220 				builder.append((char)c);
221 		    }
222 			output = builder.toString();
223 			output = output.replaceFirst("<\\?php /\\*","");
224 			output = output.replaceFirst("\\*/ \\?>","");
225 			return new StringReader(output);
226 		}
227 		catch (FileNotFoundException e) {
228 			logger.fatal("Cannot find ini file: " + e.getMessage());
229 			throw new ConfigurationException(e);
230 		}
231 		catch (IOException e) {
232 			logger.error("Caught IOException when trying to convert ini file: " + e.getMessage());
233 			throw new ConfigurationException(e);
234 		}
235 	}
236 
237 
238 }
239