剥离URL中的文件名

时间:2015-05-11 18:45:43

标签: coldfusion-8 stripping

我正在处理我已经工作了一个星期左右的脚本的最终版本。除了最后一个 finalScriptPath 变量,我将所有内容都按照自己的喜好工作,该变量映射到我的表格列 scriptPath ,这是为了将页面名称从URL路径中删除所以像" www.example.com/products/2015/example.cfm这样的路径只会呈现"产品\ 2015"文件夹结构。请参阅随附的片段。Sample of code result以下是我的代码:

!---Function named EndRequestFunc for testing safely on CF Server--->
<!---Rename function to OnRequestStart and place in Application.cfm file before running live--->
<cffunction name="EndRequestFunc" access="public" returnType="string">
<!---Variable declared and set to empty--->
<cfset referer_path_and_file = "">
<cfset referer_path = "">
<cfset referer_file_name = "">
<cfset script_path_and_file = "">
<cfset script_path = "">
<cfset script_file_name = "">

<cfif cgi.HTTP_REFERER neq ''>
  <!--- all of this will fail if there is no referer, for instance, if they bookmark the page --->
  <!--- cgi.HTTP_REFERER may contain URL parameters, so let's strip those --->
  <cfset referer_path_and_file = ListFirst(CGI.HTTP_REFERER, "?")>
  <!--- now let's get just the path, stripping out the web server info --->
  <cfset referer_path = ListDeleteAt(CGI.HTTP_REFERER, ListLen(CGI.HTTP_REFERER, "/"), "/")>
  <cfset referer_path = ReplaceNoCase(referer_path, "https", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "http", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://machine1.fss.com", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://www_dev.fss.com", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://www.fss.com", "", "All")>
  <cfset referer_path = ReplaceNoCase(referer_path, "://10.11.2.60/", "", "All")>
  <cfset referer_path = referer_path & "/">
  <cfset referer_path = ReplaceNoCase(referer_path, "/", "\", "All")>
  <!--- now let's remove everything but the file name --->
  <cfset referer_file_name = ListLast(referer_path_and_file, "/")>
  <!--- and that leaves us with these variables set --->
 <!--- referer_path_and_file = "#referer_path_and_file#"<br />
  referer_path = "#referer_path#"<br />
  referer_file_name = "#referer_file_name#"<br />
  <br />--->
</cfif>

<!--- cgi.SCRIPT_NAME does not include URL parameters --->
<cfset script_path_and_file = ListFirst(CGI.SCRIPT_NAME, "\")>
<!--- now let's get just the path, stripping out the web server info --->
<cfset script_path = GetDirectoryFromPath(GetCurrentTemplatePath())>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www_dev", "", "All")>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www", "", "All")>
<!--- now let's remove everything but the file name --->
<cfset script_file_name = ListLast(script_path_and_file, "/")>
<!--- and that leaves us with these variables set--->
<!---script_path_and_file = "#script_path_and_file#"<br />
script_path = "#script_path#"<br />
script_file_name = "#script_file_name#"<br />--->
<!---<!---CFC Test Query--->
   <cfquery name="qryTest" datasource="Fss_Dev" >
INSERT INTO tblCFMPageRequest(pageName)
     VALUES
           ('MyPage.cfm')
   </cfquery>--->

<!---Directory Stripping And Modifier Block Goes Here--->
<!---Set CGI System Variables--->
<cfset currentHeader = CGI.HTTP_REFERER >
<cfset currentScriptPage = CGI.SCRIPT_NAME > 
<!---Set currentScriptPage as command line directory string and delcare new variable "reverseScriptPage"--->
<cfset reverseScriptPage = ReReplace(#currentScriptPage#, "/", "\","ALL")>
<!---Set reverseScriptPage value as newly format command line directory structure--->
<cfset newScriptPage = ListSetAt(#reverseScriptPage#, 1, "#reverseScriptPage#") >
<cfset lastScriptPage = ListFirst(#newScriptPage#, "/") > <!---Added this 5/11/2015--->
 <cfset  finalScriptPage = ListSetAt(#lastScriptPage#, 1, "#lastScriptPage#") >
<cfset finalScriptPath = ReReplace(#finalScriptPage#, "/", "\","ALL") >


<!---CGI.HTTP_HEADER Formatting Block--->
<!---This block retrieves the http header value, then strips http formatting--->
<!---currentHeader variable truncated by ListLast then assigned to variable headerFileName--->
<cfset headerFileName = ListLast(#currentHeader#,"/")>
<!---currentHeader URL string has "/" replaced with "\" characters then set as variable reverseHeaderURL--->
<cfset reverseHeaderURL = ReReplace(#currentHeader#, "/", "\","ALL")>
<!---reverseHeaderURL is stripped of all known http type formattings and IP formattings--->
<cfif cgi.HTTP_REFERER neq ''>
<cfset reverseHeaderURL = ListDeleteAt(cgi.HTTP_REFERER, ListLen(cgi.HTTP_REFERER, "/"), "/")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "https", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "http", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://machine1.fss.com", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://www_dev.fss.com", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://www.fss.com", "", "All") >
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://machine1.fss.com", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, "://10.11.2.60/", "", "All")>
  <cfset reverseHeaderURL = ReplaceNoCase(reverseHeaderURL, ":\\dev.fss.com\", "", "All")> <!---Added this line 5/11/2015--->
  <!---The end result is then formatted to C:\\ command line format and stored in new variable newHeaderURL--->
 <cfset newHeaderURL = ListSetAt(#reverseHeaderURL#, 1, "#reverseHeaderURL#") >
<cfset finalHeaderURL = ReReplace(#newHeaderURL#, "/", "\","ALL")>
</cfif>

   <!---Queries Table To Get Requested Record--->
    <cfquery name="qryGetPageRecord" datasource="Fss_Dev" dbname="Fss_Dev">
    SELECT referrerPage
    FROM tblCFMPageRequest
    WHERE referrerPage = '#referer_file_name#' AND scriptName = '#script_file_name#'
    </cfquery>
    <!---Conditional Check for record count equal to 0--->
    <cfif qryGetPageRecord.recordCount eq 0>
    <!---<cfset httpReferer = ListLast('http://www.fss.com/index.cfm?uid=testValue',"/")>
    <cfset ListFirst(httpReferer,"?")>--->
    <!---variable declared as scriptName and set equal to CGI.SCRIPT_NAME --->
    <!---Value truncated to actual file name through function ListLast() --->
    <cfquery name="setNewRecord" datasource="Fss_Dev" dbname="Fss_Dev">
    INSERT INTO tblCFMPageRequest (referrerPage, referrerPath, scriptName, scriptPath)
    VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#referer_file_name#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#finalHeaderURL#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#script_file_name#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#finalScriptPath#">)
  </cfquery>
    </cfif>
</cffunction>

1 个答案:

答案 0 :(得分:0)

经过一些试验后发现我的解决方案,特别是使用代码块时出错。下面是解决文件名的解决方案,只留下路径。

<cfif cgi.PATH_INFO neq ''>
  <cfset script_path_and_file = ListFirst(CGI.PATH_INFO, "?")>
 <cfset script_path = ListDeleteAt(CGI.PATH_INFO, ListLen(CGI.PATH_INFO, "/"), "/")>
  <cfset script_path = ReplaceNoCase(script_path, "https", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "http", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://machine1.fss.com", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://www_dev.fss.com", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://www.fss.com", "", "All")>
  <cfset script_path = ReplaceNoCase(script_path, "://10.11.2.60/", "", "All")>
  <cfset script_path = script_path & "/">
  <cfset script_path = ReplaceNoCase(script_path, "/", "\", "All")>
<cfset script_file_name = ListLast(script_path_and_file, "/") >
</cfif>

<cfset script_path = GetDirectoryFromPath(GetCurrentTemplatePath())>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www_dev", "", "All")>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\Clients\fss.com\www", "", "All")>
<cfset script_path = ReplaceNoCase(script_path, "C:\inetpub\wwwroot\", "", "All")>
<cfset script_file_name = ListLast(script_path_and_file, "/")>

    <cfquery name="setNewRecord" datasource="Fss_Main_Dev" dbname="Fss_Dev">
    INSERT INTO tblCFMPageRequest (referrerPage, referrerPath, scriptName, scriptPath)
    VALUES (<cfqueryparam cfsqltype="cf_sql_varchar" value="#referer_file_name#">,
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#finalHeaderURL#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#script_file_name#">, 
            <cfqueryparam cfsqltype="cf_sql_varchar" value="#script_path#">)
  </cfquery>

</cffunction>