<commit_before><%@ page language="java"
	
	import="imcode.util.Utility,
	        org.apache.oro.text.perl.Perl5Util,
	        java.io.*"
	
%><%@taglib prefix="vel" uri="/WEB-INF/velocitytag.tld"%><vel:velocity><%

/* *******************************************************************
 *           SETTINGS                                                *
 ******************************************************************* */

String acceptedExtPattern         = "/\\.(HTML?|CSS|JS|VBS|TXT|INC|JSP|ASP|FRAG)+$/i" ;
String acceptedExtPatternReadonly = "/" +
	"(\\.(HTML?|CSS|JS|VBS|TXT|INC|JSP|ASP|FRAG)+$)" +
	"|(\\.LOG+)" +
	"/i" ;

String IMG_PATH   = request.getContextPath()+"/imcms/"+Utility.getLoggedOnUser( request ).getLanguageIso639_2()+"/images/admin/" ; // path to buttons (with trailing /)

/* *******************************************************************
 *           INIT                                                    *
 ******************************************************************* */

String file        = request.getParameter("file") ;
String thisPage = request.getContextPath() + request.getServletPath();
String sTemp;

String hdPath      = request.getParameter("hdPath") ;
String fileSrc     = request.getParameter("txtField") ;
boolean doSave     = (fileSrc != null) ? true : false ;
boolean isReadonly = (request.getParameter("readonly") != null) ? true : false ;

String theSearchString = (request.getParameter("searchString") != null) ? request.getParameter("searchString") : "" ;

/* Is editable file? */
Perl5Util re = new Perl5Util() ;

if (isReadonly) acceptedExtPattern = acceptedExtPatternReadonly ;

boolean isEditable = re.match(acceptedExtPattern, file) ;

/* reset file ? */

boolean resetOrg   = false ;
boolean resetSaved = false ;

if (request.getParameter("resetFile") != null) {
	if (request.getParameter("resetFile").equals("org")) {
		resetOrg   = true ;
	} else if (request.getParameter("resetFile").equals("saved")) {
		resetSaved = true ;
	}
}

/* edit template ? */

boolean isTempl  = (request.getParameter("template") != null) ? true : false ;
String templName = request.getParameter("templName") ;

/* split to "/path" and "filename.ext" */

File webRoot    = imcode.server.WebAppGlobalConstants.getInstance().getAbsoluteWebAppPath() ;

/* get full path */

File fn = new File(webRoot, file) ;

if (hdPath == null) {
	hdPath = fn.getCanonicalPath() ;
}

/* Check browser */

String uAgent = request.getHeader("USER-AGENT") ;
boolean hasDocumentAll  = re.match("/(MSIE 4|MSIE 5|MSIE 5\\.5|MSIE 6|MSIE 7)/i", uAgent) ;
boolean hasDocumentLayers  = (re.match("/Mozilla/i", uAgent) && !re.match("/Gecko/i", uAgent) && !re.match("/MSIE/i", uAgent)) ? true : false ;
boolean hasGetElementById = re.match("/Gecko/i", uAgent) ;
boolean isMac = re.match("/Mac/i", uAgent) ;

/* Special Character replacers */

 /* replace entities so they will render correctly.
    ie: &Aring; in code would have been  in the editfield.
    Now &amp;Aring; in code and &Aring; in editfield. Thereby saved correctly. */
String[] fuckedUpUmling = new String[] {
 "&", "&amp;"
} ;

 /* replace back. If any of them where in a script-block, they would have been rendered and saved wrongly.
    ie: &amp;nbsp; will be replaced with &nbsp; 
String[] fuckedUpUmlingBack = new String[] {
 "&amp;", "&#$1;"
} ;
*/
/* SAVE IT */

Writer fileOut  = null ;
boolean isSaved = false ;
String sError   = "" ;

if (doSave) {
	try {
		if (!fn.exists()) {
			sError = "<? install/htdocs/sv/jsp/FileAdmin_edit.jsp/1001/1 ?>" ;
		}
		session.setAttribute("fileSaved", fileSrc) ;
		/*for (int i = 0; i < fuckedUpUmlingBack.length; i = i + 2) {
			fileSrc = fileSrc.replaceAll(fuckedUpUmlingBack[i],fuckedUpUmlingBack[i+1]) ;
		}*/
		fileOut = new FileWriter(fn) ;
		fileOut.write(fileSrc) ;
		isSaved = true ;
	} catch (FileNotFoundException fnEx) {
		sError = "<? install/htdocs/sv/jsp/FileAdmin_edit.jsp/1001/2 ?>" ;
	} catch (IOException ioEx) {
		sError = "<? install/htdocs/sv/jsp/FileAdmin_edit.jsp/1001/3 ?>" ;
	} finally {
		if (fileOut != null) {
			fileOut.close() ;
		}
	}
}

/* if Is editable file - Read file and show it */

String fileLine = "" ;
String tempStr  = "";

if (isEditable && !doSave) {

	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fn))) ;

	fileSrc = "" ;
	while ((fileLine = br.readLine()) != null) {
		tempStr = fileLine + "\n" ;
		if (tempStr.length() > 0) {
			fileSrc += tempStr ;
		}
	}
	br.close() ;


} else if (!isEditable) {
	
	out.print("The file is not editable!") ;
	return ;
	
}

/*
  :: replace all "Special Characters", or they will be saved wrongly the second time they're saved.
     or they will mess up the page...
     Replace all < & > with &lt; / &gt;
*/

for (int i = 0; i < fuckedUpUmling.length; i = i + 2) {
	fileSrc = fileSrc.replaceAll(fuckedUpUmling[i],fuckedUpUmling[i+1]) ;
}<commit_msg>Bug 2891: Require Superadmin role and a POST to overwrite files.<commit_after><%@ page language="java"

	import="imcode.util.Utility,
	        org.apache.oro.text.perl.Perl5Util,
	        java.io.*,
            imcode.server.user.UserDomainObject"

%><%@taglib prefix="vel" uri="/WEB-INF/velocitytag.tld"%><vel:velocity><%

UserDomainObject user = Utility.getLoggedOnUser( request );
if (!user.isSuperAdmin()) {
    return ;
}

/* *******************************************************************
 *           SETTINGS                                                *
 ******************************************************************* */

String acceptedExtPattern         = "/\\.(HTML?|CSS|JS|VBS|TXT|INC|JSP|ASP|FRAG)+$/i" ;
String acceptedExtPatternReadonly = "/" +
	"(\\.(HTML?|CSS|JS|VBS|TXT|INC|JSP|ASP|FRAG)+$)" +
	"|(\\.LOG+)" +
	"/i" ;

    String IMG_PATH   = request.getContextPath()+"/imcms/"+user.getLanguageIso639_2()+"/images/admin/" ; // path to buttons (with trailing /)

/* *******************************************************************
 *           INIT                                                    *
 ******************************************************************* */

String file        = request.getParameter("file") ;
String thisPage = request.getContextPath() + request.getServletPath();
String sTemp;

String hdPath      = request.getParameter("hdPath") ;
String fileSrc     = request.getParameter("txtField") ;
boolean doSave     = "POST".equalsIgnoreCase(request.getMethod()) && fileSrc != null ;
boolean isReadonly = request.getParameter("readonly") != null ;

String theSearchString = (request.getParameter("searchString") != null) ? request.getParameter("searchString") : "" ;

/* Is editable file? */
Perl5Util re = new Perl5Util() ;

if (isReadonly) acceptedExtPattern = acceptedExtPatternReadonly ;

boolean isEditable = re.match(acceptedExtPattern, file) ;

/* reset file ? */

boolean resetOrg   = false ;
boolean resetSaved = false ;

if (request.getParameter("resetFile") != null) {
	if (request.getParameter("resetFile").equals("org")) {
		resetOrg   = true ;
	} else if (request.getParameter("resetFile").equals("saved")) {
		resetSaved = true ;
	}
}

/* edit template ? */

boolean isTempl  = (request.getParameter("template") != null) ? true : false ;
String templName = request.getParameter("templName") ;

/* split to "/path" and "filename.ext" */

File webRoot    = imcode.server.WebAppGlobalConstants.getInstance().getAbsoluteWebAppPath() ;

/* get full path */

File fn = new File(webRoot, file) ;

if (hdPath == null) {
	hdPath = fn.getCanonicalPath() ;
}

/* Check browser */

String uAgent = request.getHeader("USER-AGENT") ;
boolean hasDocumentAll  = re.match("/(MSIE 4|MSIE 5|MSIE 5\\.5|MSIE 6|MSIE 7)/i", uAgent) ;
boolean hasDocumentLayers  = (re.match("/Mozilla/i", uAgent) && !re.match("/Gecko/i", uAgent) && !re.match("/MSIE/i", uAgent)) ? true : false ;
boolean hasGetElementById = re.match("/Gecko/i", uAgent) ;
boolean isMac = re.match("/Mac/i", uAgent) ;

/* Special Character replacers */

 /* replace entities so they will render correctly.
    ie: &Aring; in code would have been  in the editfield.
    Now &amp;Aring; in code and &Aring; in editfield. Thereby saved correctly. */
String[] fuckedUpUmling = new String[] {
 "&", "&amp;"
} ;

 /* replace back. If any of them where in a script-block, they would have been rendered and saved wrongly.
    ie: &amp;nbsp; will be replaced with &nbsp;
String[] fuckedUpUmlingBack = new String[] {
 "&amp;", "&#$1;"
} ;
*/
/* SAVE IT */

Writer fileOut  = null ;
boolean isSaved = false ;
String sError   = "" ;

if (doSave) {
	try {
		if (!fn.exists()) {
			sError = "<? install/htdocs/sv/jsp/FileAdmin_edit.jsp/1001/1 ?>" ;
		}
		session.setAttribute("fileSaved", fileSrc) ;
		/*for (int i = 0; i < fuckedUpUmlingBack.length; i = i + 2) {
			fileSrc = fileSrc.replaceAll(fuckedUpUmlingBack[i],fuckedUpUmlingBack[i+1]) ;
		}*/
		fileOut = new FileWriter(fn) ;
		fileOut.write(fileSrc) ;
		isSaved = true ;
	} catch (FileNotFoundException fnEx) {
		sError = "<? install/htdocs/sv/jsp/FileAdmin_edit.jsp/1001/2 ?>" ;
	} catch (IOException ioEx) {
		sError = "<? install/htdocs/sv/jsp/FileAdmin_edit.jsp/1001/3 ?>" ;
	} finally {
		if (fileOut != null) {
			fileOut.close() ;
		}
	}
}

/* if Is editable file - Read file and show it */

String fileLine = "" ;
String tempStr  = "";

if (isEditable && !doSave) {

	BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fn))) ;

	fileSrc = "" ;
	while ((fileLine = br.readLine()) != null) {
		tempStr = fileLine + "\n" ;
		if (tempStr.length() > 0) {
			fileSrc += tempStr ;
		}
	}
	br.close() ;


} else if (!isEditable) {

	out.print("The file is not editable!") ;
	return ;

}

/*
  :: replace all "Special Characters", or they will be saved wrongly the second time they're saved.
     or they will mess up the page...
     Replace all < & > with &lt; / &gt;
*/

for (int i = 0; i < fuckedUpUmling.length; i = i + 2) {
	fileSrc = fileSrc.replaceAll(fuckedUpUmling[i],fuckedUpUmling[i+1]) ;
}
{'fraction_non_alphanumeric': 0.2091627488246474, 'fraction_numerical': 0.007602280684205261, 'mean_word_length': 3.5799358680714612, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4808020', 'n_tokens_mistral': 3453, 'n_tokens_neox': 3254, 'n_words': 735}