boolean EDIT_MODE = false;
boolean PREVIEW_MODE = false;
LogFactory.getLog(this.getClass()).debug("CMS Filter URI = " + uri);
PermissionAPI permissionAPI = APILocator.getPermissionAPI();
if (session != null) {
// struts crappy messages have to be retrived from session
if (session.getAttribute(Globals.ERROR_KEY) != null) {
request.setAttribute(Globals.ERROR_KEY, session.getAttribute(Globals.ERROR_KEY));
session.removeAttribute(Globals.ERROR_KEY);
}
if (session.getAttribute(Globals.MESSAGE_KEY) != null) {
request.setAttribute(Globals.MESSAGE_KEY, session.getAttribute(Globals.MESSAGE_KEY));
session.removeAttribute(Globals.MESSAGE_KEY);
}
// set the preview mode
ADMIN_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.ADMIN_MODE_SESSION) != null);
PREVIEW_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION) != null && ADMIN_MODE);
EDIT_MODE = (session.getAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION) != null && ADMIN_MODE);
if (request.getParameter("livePage") != null && request.getParameter("livePage").equals("1")) {
PREVIEW_MODE = false;
EDIT_MODE = false;
session.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
session.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
LogFactory.getLog(this.getClass()).debug("CMS FILTER Cleaning PREVIEW_MODE_SESSION LIVE!!!!");
}
if (request.getParameter("previewPage") != null && request.getParameter("previewPage").equals("1")) {
PREVIEW_MODE = false;
EDIT_MODE = true;
session.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, null);
session.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, "true");
request.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, "true");
LogFactory.getLog(this.getClass()).debug("CMS FILTER Cleaning EDIT_MODE_SESSION PREVIEW!!!!");
}
if (request.getParameter("previewPage") != null && request.getParameter("previewPage").equals("2")) {
PREVIEW_MODE = true;
EDIT_MODE = false;
session.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, "true");
request.setAttribute(com.dotmarketing.util.WebKeys.PREVIEW_MODE_SESSION, "true");
session.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
request.setAttribute(com.dotmarketing.util.WebKeys.EDIT_MODE_SESSION, null);
LogFactory.getLog(this.getClass()).debug("CMS FILTER Cleaning PREVIEW_MODE_SESSION PREVIEW!!!!");
}
}
/*
* Getting host object form the session
*/
HostWebAPI hostWebAPI = WebAPILocator.getHostWebAPI();
Host host;
try {
host = hostWebAPI.getCurrentHost(request);
} catch (PortalException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
} catch (SystemException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
} catch (DotDataException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
} catch (DotSecurityException e) {
Logger.error(this, "Unable to retrieve current request host for URI " + uri);
throw new ServletException(e.getMessage(), e);
}
/*
* If someone is trying to go right to an asset without going through
* the cms, give them a 404
*/
if (UtilMethods.isSet(ASSET_PATH) && uri.startsWith(ASSET_PATH)) {
response.sendError(403, "Forbidden");
return;
}
String pointer = null;
boolean isVanityURL = UtilMethods.isSet(VirtualLinksCache.getPathFromCache(host.getHostname() + ":" + uri));
if (!isVanityURL)
isVanityURL = UtilMethods.isSet(VirtualLinksCache.getPathFromCache(uri));
if(!uri.equals(pointer) && !uri.endsWith("/")
&& ! RegEX.contains(uri, folderPathRegEx)
&& uri.indexOf("/dotCMS/") == -1
&& !isVanityURL ) {
Enumeration enm = req.getParameterNames();
StringBuffer params = new StringBuffer("");
for (; enm.hasMoreElements(); ) {
String name = (String)enm.nextElement();
params.append(name + "=" + req.getParameter(name));
if(enm.hasMoreElements())
params.append(StringPool.AMPERSAND);
}
response.sendRedirect(uri + "/" + (params.length() > 0 ? "?" + params : ""));
return;
}
//Verify if the request is for a specific language
Long languageId;
if ( !UtilMethods.isSet( req.getParameter( "language_id" ) ) ) {
languageId = APILocator.getLanguageAPI().getDefaultLanguage().getId();
} else {
languageId = Long.parseLong( req.getParameter( "language_id" ) );
}
/* if edit mode */
if (PREVIEW_MODE || EDIT_MODE) {
try {
pointer = WorkingCache.getPathFromCache(uri, host);
if(!UtilMethods.isSet(pointer)){//DOTCMS-7062
pointer = LiveCache.getPathFromCache(uri, host);
}
if (!UtilMethods.isSet(pointer)
&& !uri.equals("/")
&& (uri.endsWith(dotExtension)
|| InodeUtils.isSet(APILocator
.getFolderAPI().findFolderByPath(uri, host,APILocator.getUserAPI().getSystemUser(),false)
.getInode()))) {
String url = uri;
if (!uri.endsWith(dotExtension)) {
url += "index" + dotExtension;
}
request.getRequestDispatcher("/html/portlet/ext/htmlpages/page_not_found_404.jsp?url=" + url + "&hostId=" + host.getIdentifier()).forward(
req, res);
return;
}
LogFactory.getLog(this.getClass()).debug("CMS preview pointer = " + uri + ":" + pointer);
} catch (Exception e) {
Logger.debug(this.getClass(), "Can't find pointer " + uri);
}
/* if live mode */
} else {
try {
pointer = LiveCache.getPathFromCache( uri, host, languageId );
} catch (Exception e) {
Logger.debug(this.getClass(), "Can't find pointer " + uri);
try {
if(WebAPILocator.getUserWebAPI().isLoggedToBackend(request)){
response.setHeader( "Pragma", "no-cache" );
response.setHeader( "Cache-Control", "no-cache" );
response.setDateHeader( "Expires", 0 );
response.sendError(404);
return;
}
} catch (Exception e1) {
Logger.debug(this.getClass(), "Can't find pointer " + uri);
}
}
// If the cache hits the db the connection needs to be manually
// closed
try {
HibernateUtil.closeSession();
} catch (DotHibernateException e) {
Logger.error(CMSFilter.class, e.getMessage(), e);
}
LogFactory.getLog(this.getClass()).debug("CMS live pointer = " + uri + ":" + pointer);
}
/*
* Checking if host is active
*/
boolean hostlive;
try {
hostlive = APILocator.getVersionableAPI().hasLiveVersion(host);
} catch (Exception e1) {
throw new ServletException(e1);
}
if(!ADMIN_MODE && !hostlive) {
//Checking if it has a maintenance virtual link
pointer = (String) VirtualLinksCache.getPathFromCache(host.getHostname() + ":/cmsMaintenancePage");
if(pointer == null) {
try {
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, LanguageUtil.get(company.getCompanyId(), company.getLocale(), "server-unavailable-error-message"));
} catch (LanguageException e) {
Logger.error(CMSFilter.class, e.getMessage(), e);
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}
return;
}
}
// if absolute link somewhere else
if (UtilMethods.isSet(pointer) && (pointer.startsWith(httpProtocol) || pointer.startsWith(httpsProtocol))) {
response.sendRedirect(pointer);
return;
}
// virtual links only after other links
if (!UtilMethods.isSet(pointer)) {
if (uri.endsWith("/"))
uri = uri.substring(0, uri.length() - 1);
pointer = VirtualLinksCache.getPathFromCache(host.getHostname() + ":" + uri);
if (!UtilMethods.isSet(pointer)) {
pointer = VirtualLinksCache.getPathFromCache(uri);
}
if (UtilMethods.isSet(pointer)) { // is it a virtual link?
LogFactory.getLog(this.getClass()).debug("CMS found virtual link pointer = " + uri + ":" + pointer);
boolean external = false;
String auxPointer = pointer;
if(auxPointer.indexOf("http://") != -1 || auxPointer.indexOf("https://") != -1)
{
try {
User systemUser = APILocator.getUserAPI().getSystemUser();
auxPointer = auxPointer.replace("https://","");
auxPointer = auxPointer.replace("http://","");
int startIndex = 0;
int endIndex = auxPointer.indexOf("/");
if(startIndex < endIndex)
{
String localHostName = auxPointer.substring(startIndex,endIndex);
Host localHost = hostWebAPI.findByName(localHostName, systemUser, false);
if(localHost ==null || !InodeUtils.isSet(localHost.getInode())){
external=true;
}
}
else
{
external = true;
}
} catch (DotSecurityException e) {
Logger.error(this, "Unable to retrieve host were the virtual link " + uri + " is pointing.", e);
throw new ServletException(e.getMessage(), e);
} catch (DotDataException e) {
Logger.error(this, "Unable to retrieve host were the virtual link " + uri + " is pointing.", e);
throw new ServletException(e.getMessage(), e);
}
}
if (!external) {
String ext = Config.getStringProperty("VELOCITY_PAGE_EXTENSION");
if (!pointer.contains("." + ext + "?")) {
boolean isDotPage = true;
if(!pointer.contains("." + ext) && !pointer.endsWith("/") && pointer.contains("/")){
if(pointer.substring(pointer.lastIndexOf("/")).contains(".")){
uri = pointer;
try {
pointer = LiveCache.getPathFromCache(uri, host);
} catch (Exception e) {
Logger.debug(this.getClass(), "Can't find pointer " + uri);
}
isDotPage = false;
}
}
if(isDotPage){
if (pointer.contains("?") && !pointer.contains("#")) {
int index = pointer.indexOf('?');
String indexPage = "index." + ext;
if ((0 < index) && (pointer.charAt(index-1) != '/'))
indexPage = "/" + indexPage;
pointer = pointer.substring(0, index) + indexPage + pointer.substring(index);
} else {
if(pointer.endsWith("/")){
pointer = pointer.substring(0, pointer.lastIndexOf("/"));
}
if(!pointer.startsWith("/") && !(pointer.indexOf("http://") != -1 || pointer.indexOf("https://")!=-1)){
pointer = "/" + pointer;
}
String endSlash = pointer.substring(pointer.lastIndexOf("/"));
if (!pointer.endsWith("." + ext) && !endSlash.contains("#")) {
if (!pointer.endsWith("/"))
pointer += "/";
pointer += "index." + ext;
}else if(endSlash.contains("#") && !(pointer.indexOf("http://") != -1 || pointer.indexOf("https://")!=-1)){
String reqUrl = request.getRequestURL().toString();
pointer = reqUrl.replaceAll(uri.endsWith("/")?uri:uri+"/", pointer);
}
}
}
}else if(pointer.contains("#") && !(pointer.indexOf("http://") != -1 || pointer.indexOf("https://")!=-1)){
String endSlash = pointer.substring(pointer.lastIndexOf("/"));
if(endSlash.contains("#")){
String reqUrl = request.getRequestURL().toString();
pointer = reqUrl.replaceAll(uri.endsWith("/")?uri:uri+"/", pointer);
}
}
}
/*
* Apply Rules to pointer
*/
JBossRulesUtils.checkObjectRulesFromXML(request);
}
}
if (UtilMethods.isSet(pointer) && (pointer.startsWith(httpProtocol) || pointer.startsWith(httpsProtocol))) {
response.sendRedirect(pointer);
return;
}
if (UtilMethods.isSet(pointer)) {
if (!endInTheVelocityPageExtension(pointer)) {
// Validate the permission
User user = null;
try {
if (session != null)
user = (com.liferay.portal.model.User) session.getAttribute(com.dotmarketing.util.WebKeys.CMS_USER);
} catch (Exception nsue) {
Logger.warn(this, "Exception trying to getUser: " + nsue.getMessage(), nsue);
}
if(user==null) {
try {
user = com.liferay.portal.util.PortalUtil.getUser(request);
} catch (Exception nsue) {
Logger.warn(this, "Exception trying to getUser: " + nsue.getMessage(), nsue);
}
}
boolean signedIn = false;
if (user != null) {
signedIn = true;
}
Identifier ident = null;
try {
ident =APILocator.getIdentifierAPI().find(host,uri);
/**
* Build a fake proxy file object so we
* can get inheritable permissions on it
* without having to hit cache or db
*/
boolean canRead = false;
if(ident.getAssetType().equals("contentlet")){
try{
ContentletVersionInfo cinfo = APILocator.getVersionableAPI().getContentletVersionInfo( ident.getId(), languageId );
//If we did not find a version with for given language lets try with the default language
if ( !UtilMethods.isSet( cinfo.getIdentifier() ) && !languageId.equals( APILocator.getLanguageAPI().getDefaultLanguage().getId() ) ) {
languageId = APILocator.getLanguageAPI().getDefaultLanguage().getId();
cinfo = APILocator.getVersionableAPI().getContentletVersionInfo( ident.getId(), languageId );
}
Contentlet proxy = new Contentlet();
if(UtilMethods.isSet(cinfo.getLiveInode()))
proxy = APILocator.getContentletAPI().find(cinfo.getLiveInode(), user, true);
else if(WebAPILocator.getUserWebAPI().isLoggedToBackend(request))
proxy = APILocator.getContentletAPI().find(cinfo.getWorkingInode(), user, true);
canRead = UtilMethods.isSet(proxy.getInode());
}catch(Exception e){
Logger.warn(this, "Unable to find file asset contentlet with identifier " + ident.getId(), e);
}
}else{
com.dotmarketing.portlets.files.model.File f = new com.dotmarketing.portlets.files.model.File();
(f).setIdentifier(ident.getInode());
canRead = permissionAPI.doesUserHavePermission(f, PermissionAPI.PERMISSION_READ, user, true);
f = null;
}
if (!canRead) {