Files
i2p.i2p/apps/routerconsole/java/src/net/i2p/router/web/CSSHelper.java

203 lines
6.9 KiB
Java
Raw Normal View History

package net.i2p.router.web;
import java.util.HashMap;
import java.util.Locale;
2011-09-02 17:24:14 +00:00
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.i2p.servlet.util.ServletUtil;
import net.i2p.util.RandomSource;
/**
* Copied and modded from I2PTunnel IndexBean (GPL)
* @author zzz
*/
public class CSSHelper extends HelperBase {
2011-09-02 17:24:14 +00:00
private static final Map<String, Boolean> _UACache = new ConcurrentHashMap<String, Boolean>();
2011-09-02 17:24:14 +00:00
public static final String PROP_UNIVERSAL_THEMING = "routerconsole.universal.theme";
public static final String PROP_THEME_NAME = "routerconsole.theme";
/** @since 0.9.33 moved from ConfigUIHelper */
public static final String PROP_THEME_PFX = PROP_THEME_NAME + '.';
2009-07-15 22:56:53 +00:00
public static final String DEFAULT_THEME = "light";
public static final String BASE_THEME_PATH = "/themes/console/";
private static final String FORCE = "classic";
2010-01-02 02:43:18 +00:00
public static final String PROP_REFRESH = "routerconsole.summaryRefresh";
public static final String DEFAULT_REFRESH = "15";
public static final int MIN_REFRESH = 3;
public static final String PROP_DISABLE_REFRESH = "routerconsole.summaryDisableRefresh";
2012-05-13 13:05:17 +00:00
private static final String PROP_XFRAME = "routerconsole.disableXFrame";
public static final String PROP_FORCE_MOBILE_CONSOLE = "routerconsole.forceMobileConsole";
/** @since 0.9.32 */
public static final String PROP_EMBED_APPS = "routerconsole.embedApps";
/** @since 0.9.36 */
public static final String PROP_DISABLE_OLD = "routerconsole.disableOldThemes";
public static final boolean DEFAULT_DISABLE_OLD = true;
private static final String _consoleNonce = Long.toString(RandomSource.getInstance().nextLong());
/**
* formerly stored in System.getProperty("router.consoleNonce")
* @since 0.9.4
*/
public static String getNonce() {
return _consoleNonce;
}
public String getTheme(String userAgent) {
String url = BASE_THEME_PATH;
if (userAgent != null && userAgent.contains("MSIE") && !userAgent.contains("Trident/6") &&
!_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD)) {
url += FORCE + "/";
} else {
// This is the first thing to use _context on most pages
if (_context == null)
throw new IllegalStateException("No contexts. This is usually because the router is either starting up or shutting down.");
String theme = _context.getProperty(PROP_THEME_NAME, DEFAULT_THEME);
// remap deprecated themes
if (theme.equals("midnight")) {
if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
theme = "dark";
} else if (theme.equals("classic")) {
if (_context.getProperty(PROP_DISABLE_OLD, DEFAULT_DISABLE_OLD))
theme = "light";
}
2009-07-15 22:56:53 +00:00
url += theme + "/";
}
return url;
}
/**
* Returns whether app embedding is enabled or disabled
* @since 0.9.32
*/
public boolean embedApps() {
return _context.getBooleanProperty(PROP_EMBED_APPS);
}
/**
* change default language for the router AND save it
* @param lang xx OR xx_XX OR xxx OR xxx_XX
*/
public void setLang(String lang) {
// Protected with nonce in css.jsi
if (lang != null && lang.length() >= 2 && lang.length() <= 6 &&
lang.replaceAll("[a-zA-Z_]", "").length() == 0) {
2015-03-16 15:44:45 +00:00
Map<String, String> m = new HashMap<String, String>(2);
int under = lang.indexOf('_');
if (under < 0) {
m.put(Messages.PROP_LANG, lang.toLowerCase(Locale.US));
m.put(Messages.PROP_COUNTRY, "");
_context.router().saveConfig(m, null);
} else if (under > 0 && lang.length() > under + 1) {
m.put(Messages.PROP_LANG, lang.substring(0, under).toLowerCase(Locale.US));
m.put(Messages.PROP_COUNTRY, lang.substring(under + 1).toUpperCase(Locale.US));
_context.router().saveConfig(m, null);
}
}
}
/**
* needed for conditional css loads for zh
* @return two-letter only, lower-case
*/
public String getLang() {
return Messages.getLanguage(_context);
}
2011-11-09 18:38:39 +00:00
/**
* Show / hide news on home page
* @param val if non-null, "1" to show, else hide
* @since 0.8.12
*/
public void setNews(String val) {
// Protected with nonce in css.jsi
if (val != null)
Big refactor of the router console update subsystem, in preparation for implementing out-of-console updaters like i2psnark. - Add new update interfaces in net.i2p.update - All update implementations moved to routerconsole update/ - Implement an UpdateManager that registers with the RouterContext - UpdateManager handles multiple types of things to update (router, plugins, news, ...) and methods of updating (HTTP, ...) - UpdateManager maintains list of installed, downloaded, and available versions of everything - Define Updaters that can check for a new version and/or download an item - Individual Updaters register with the UpdateManager obtained from I2PAppContext, identifying the type of update item and update method they can handle. - Updaters need only core libs, no router.jar or routerconsole access required. - All checks and updates are initiated via the UpdateManager. - All status on checks and updates in-progress or completed are obtained from the UpdateManager. No more use of System properties to broadcast update state. - All update and checker tasks are intantiated on demand and threaded; no more static references left over. - Split out the Runners and Checkers from the Handlers and make the inheritance more sane. - No more permanent NewsFetcher thread; run on the SimpleScheduler queue and thread a checker task only to fetch the news. - No more static NewsFetcher instance in routerconsole. All helper methods that are still required are moved to NewsHelper. The UpdateManager implements the policy for when to check and download. All requests go through the UpdateManager. For each update type, there's several parts: - The xxxUpdateHandler implements the Updater - The xxxUpdateChecker implements the UpdateTask for checking - The xxxUpdateRunner implements the UpdateTask for downloading New and moved classes: web/ update/ ---- ------- new ConsoleUpdateManager.java new PluginUpdateChecker.java from PluginUpdateChecker PluginUpdateChecker -> PluginUpdateHandler.java PluginUpdateHandler.java -> PluginUpdateRunner new UnsignedUpdateHandler.java UnsignedUpdateHandler -> UnsignedUpdateRunner.java new UnsignedUpdateChecker from NewsFetcher UpdateHandler.java remains new UpdateHandler.java new UpdateRunner.java from UpdateHandler move NewsHandler from NewsFetcher new NewsFetcher new NewsTimerTask new DummyHandler Initial checkin. Unfinished, untested, unpolished.
2012-06-18 22:09:45 +00:00
NewsHelper.showNews(_context, val.equals("1"));
2011-11-09 18:38:39 +00:00
}
2012-05-13 13:05:17 +00:00
/**
* Should we send X_Frame_Options=SAMEORIGIN
* Default true
* @since 0.9.1
*/
public boolean shouldSendXFrame() {
return !_context.getBooleanProperty(PROP_XFRAME);
}
2010-01-02 02:43:18 +00:00
/** change refresh and save it */
public void setRefresh(String r) {
try {
if (Integer.parseInt(r) < MIN_REFRESH)
2018-08-25 14:29:32 +00:00
r = Integer.toString(MIN_REFRESH);
_context.router().saveConfig(PROP_REFRESH, r);
} catch (RuntimeException e) {
}
2010-01-02 02:43:18 +00:00
}
/** @return refresh time in seconds, as a string */
public String getRefresh() {
String r = _context.getProperty(PROP_REFRESH, DEFAULT_REFRESH);
try {
if (Integer.parseInt(r) < MIN_REFRESH)
2018-08-25 14:29:32 +00:00
r = Integer.toString(MIN_REFRESH);
} catch (RuntimeException e) {
2018-08-25 14:29:32 +00:00
r = Integer.toString(MIN_REFRESH);
}
return r;
2010-01-02 02:43:18 +00:00
}
/**
* change disable refresh boolean and save it
* @since 0.9.1
*/
public void setDisableRefresh(String r) {
String disableRefresh = "false";
if ("0".equals(r))
disableRefresh = "true";
_context.router().saveConfig(PROP_DISABLE_REFRESH, disableRefresh);
}
/**
* @return true if refresh is disabled
* @since 0.9.1
*/
public boolean getDisableRefresh() {
return _context.getBooleanProperty(PROP_DISABLE_REFRESH);
}
/** translate the title and display consistently */
public String title(String s) {
StringBuilder buf = new StringBuilder(128);
buf.append("<title>")
.append(_t("I2P Router Console"))
.append(" - ")
.append(_t(s))
.append("</title>");
return buf.toString();
}
/**
* Should we allow a refreshing IFrame?
* @since 0.8.5
*/
public boolean allowIFrame(String ua) {
boolean forceMobileConsole = _context.getBooleanProperty(PROP_FORCE_MOBILE_CONSOLE);
if (forceMobileConsole)
return false;
2011-09-02 17:24:14 +00:00
if (ua == null)
return true;
Boolean brv = _UACache.get(ua);
if (brv != null)
return brv.booleanValue();
boolean rv = shouldAllowIFrame(ua);
_UACache.put(ua, Boolean.valueOf(rv));
return rv;
}
private static boolean shouldAllowIFrame(String ua) {
return !ServletUtil.isSmallBrowser(ua);
}
}