<commit_before><cfif not isdefined("form.input") or form.input eq "" >
 <cflocation statuscode="400" url="graph.cfm" /> <!--- Empty inputs are "Bad Request"'s --->
</cfif>

<cfparam name="form.x" type="numeric" default="5" />
<cfparam name="form.y" type="numeric" default="5" />
<cfparam name="form.method" type="string" default="circo" />

<!--- Generate a temporary file in the local tmp directory, and write our form submission (DOT code, hopefully) to it --->
<cfset currentDir = getdirectoryFromPath(getcurrentTemplatePath()) />
<cfset TempFile = GetTempFile("#currentDir#/tmp/", "dot") />
<cffile action="write" file="#TempFile#" output="#form.input#" addnewline="false" />

<!--- Figure out the graph output and error file locations --->
<cfset Output = "#currentDir#/tmp/#getfileFromPath(TempFile)#.png" />
<cfset Error = "#currentDir#/tmp/#getfileFromPath(TempFile)#.error" />
<cfset quickResults = ""/>

<!--- The ListContains check, makes sure the given layout method is valid, preventing form tampering from executing arbitrary code --->
<cfif ListContainsNoCase("dot,neato,twopi,circo,fdp,sfdp",form.method) gt 0 >
<!--- CFExecute is always pretty scary, especially given how the arguments are passed, In this case each input is validated though:
 - #TempFile# is generated by this page
 - #x# and #y# are guaranteed to be numeric by cfparam
 - #form.method# is tested to ensure it's within our list of acceptable parameters
 - #Output# and #Error# are generated by this page --->
 <cfexecute name="/usr/bin/dot" arguments="#TempFile# -Tpng -Gsize=#x#,#y# -Glayout=#form.method# -o#Output#" errorFile="#Error#" timeout="5" />
 <cfelse>
 <cfabort />
</cfif>
 
<!--- If the output generated successfully, show it, otherwise show the error --->
<cfoutput>
 <cfif FileExists("#Output#") >
  <cflocation statuscode="200" url="http://#cgi.server_name#/graph.cfm?result=#getfileFromPath(TempFile)#&x=#form.x#&y=#form.y#&method=#form.method#" />
 <cfelse>
  <cflocation statuscode="500" url="http://#cgi.server_name#/graph.cfm?error=#getfileFromPath(TempFile)#" />
</cfif>
</cfoutput><commit_msg>Mention malicious DOT scripts and their risk<commit_after><cfif not isdefined("form.input") or form.input eq "" >
 <cflocation statuscode="400" url="graph.cfm" /> <!--- Empty inputs are "Bad Request"'s --->
</cfif>

<cfparam name="form.x" type="numeric" default="5" />
<cfparam name="form.y" type="numeric" default="5" />
<cfparam name="form.method" type="string" default="circo" />

<!--- Generate a temporary file in the local tmp directory, and write our form submission (DOT code, hopefully) to it --->
<cfset currentDir = getdirectoryFromPath(getcurrentTemplatePath()) />
<cfset TempFile = GetTempFile("#currentDir#/tmp/", "dot") />
<cffile action="write" file="#TempFile#" output="#form.input#" addnewline="false" />

<!--- Figure out the graph output and error file locations --->
<cfset Output = "#currentDir#/tmp/#getfileFromPath(TempFile)#.png" />
<cfset Error = "#currentDir#/tmp/#getfileFromPath(TempFile)#.error" />
<cfset quickResults = ""/>

<!--- The ListContains check, makes sure the given layout method is valid, preventing form tampering from executing arbitrary code --->
<cfif ListContainsNoCase("dot,neato,twopi,circo,fdp,sfdp",form.method) gt 0 >
<!--- CFExecute is always pretty scary, especially given how the arguments are passed, In this case each input is validated though:
 - #TempFile# is generated by this page
 - #x# and #y# are guaranteed to be numeric by cfparam
 - #form.method# is tested to ensure it's within our list of acceptable parameters
 - #Output# and #Error# are generated by this page --->
<!--- The only other concern here, is a malicious DOT script, which could potentially expose files on the system by including them
 as custom shapes for example, Since DOT has no way to restrict language features on the command line, make sure this app is sandboxed,
 your JSP servers security policies and system permissions are the only way to mitigate this danger. !--->
 <cfexecute name="/usr/bin/dot" arguments="#TempFile# -Tpng -Gsize=#x#,#y# -Glayout=#form.method# -o#Output#" errorFile="#Error#" timeout="5" />
 <cfelse>
 <cfabort />
</cfif>
 
<!--- If the output generated successfully, show it, otherwise show the error --->
<cfoutput>
 <cfif FileExists("#Output#") >
  <cflocation statuscode="200" url="http://#cgi.server_name#/graph.cfm?result=#getfileFromPath(TempFile)#&x=#form.x#&y=#form.y#&method=#form.method#" />
 <cfelse>
  <cflocation statuscode="500" url="http://#cgi.server_name#/graph.cfm?error=#getfileFromPath(TempFile)#" />
</cfif>
</cfoutput>
{'fraction_non_alphanumeric': 0.16322986759279356, 'fraction_numerical': 0.005643585847623182, 'mean_word_length': 5.039318479685452, 'pattern_counts': {'":': 0, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3779713', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1426, 'n_words': 442}