Converting an absolute path into a url - coldfusion

I have a path: c:\home\example.com\wwwroot\img\
I need to convert it into a url: https://example.com/img/
The thing is that the path is dynamic and therefore the url is dynamic. I have the path in a variable. I am NOT on the page in the path, otherwise I would easily be able to use ExpandPath().
What I have is the root url which I get like this:
<cfif isDefined("cgi.SERVER_PORT_SECURE") and cgi.SERVER_PORT_SECURE eq 1>
<cfset http_sec = "https://">
<cfelse>
<cfset http_sec = "http://">
</cfif>
<cfset websiteurl = "#http_sec##cgi.http_host#">
This gives me the root: https://example.com
This where I am stuck.
So if the path is "c:\home\example.com\wwwroot\folder\sub-folder\"
I need to convert it into a url: https://example.com/folder/sub-folder/

How about:
<Cfset path = "c:\home\example.com\wwwroot\folder\sub-folder\">
<cfif isDefined("cgi.SERVER_PORT_SECURE") and cgi.SERVER_PORT_SECURE eq 1>
<cfset http_sec = "https://">
<cfelse>
<cfset http_sec = "http://">
</cfif>
<cfset weburl = replacenocase(path,'c:\home\','','one')>
<cfset weburl = rereplacenocase(weburl,'(.*?)(wwwroot\\)(.*)','\1\3','one')>
<cfset weburl = replace(weburl,'\','/','all')>
<cfset weburl = "#http_sec##weburl#">
<Cfdump var=#weburl#>
Basically you need to strip the "c:\home\" and "wwwroot\" from the absolute path and convert the \ into a /. This is just one way to do this.

You can use these variables...
Protocol = #getPageContext().getRequest().getScheme()#;
Domain = #cgi.server_name#;
Template = #cgi.script_name#;
Variables = #cgi.query_string#;
So for example:
<cfset path = "c:\home\example.com\wwwroot\folder\sub-folder\">
<!--- remove the site loc on disk --->
<cfset path = replaceNoCase(path, 'c:\home\example.com\wwwroot\', '', 'all')>
<!--- convert slashes --->
<cfset path = replace(path, '\', '/', 'all')>
<!--- put it all together --->
<cfset myURL = '#getPageContext().getRequest().getScheme()#://#cgi.server_name#/#path#?#cgi.query_string#'>

Related

I need to fix file names on PO invoice generator when we upload file

<cfif NOT DirectoryExists(ExpandPath(lv_upload_path))>
<cfdirectory action="create" directory="#ExpandPath(lv_upload_path)#">
</cfif>
<cfset lv_file_ext = listLast(FORM.filename_inv, ".") />
<!--- replace special chars in comapny name except space, then replace space with underscore --->
<cfset company_name = replaceSpecialChars(
textString=FORM.company_name,
replaceTheseChars="33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,58,59,60,61,62,63,64,91,92,93,94,95,96,123,124,125,126"
)>
<cfset company_name = Replace( company_name, "#Chr(32)#", "_", "All" )>
<cfset lv_file_name = company_name & "_Factura_" & replaceSpecialChars(FORM.in_supplier_inv) & "_" & DateFormat(FORM.in_when_inv, "dd.mm.yyyy") >
<cfset lv_file_name_out = lv_file_name & "." & replaceSpecialChars(lv_file_ext)>
<cffile action="upload" filefield="pickfiles_inv" destination="#ExpandPath(lv_upload_path & '/' & lv_file_name_out)#" nameconflict="MakeUnique">
<cfset lv_document_name = CFFILE.serverfile />
<cfset new_id = APPLICATION.po_management.f_savePOInvDocuments(
p_po_inv_id=URL.id,
p_supplier_number=FORM.in_supplier_inv,
p_when_created=FORM.in_when_inv,
p_amount=FORM.in_amount_inv,
p_document_name=lv_document_name
Im new so if I make incorrect question, just help with how make it more good in future.
When upload a file give error back
Element FILENAME_INV is undefined in FORM.
Use the following line instead:
<cfset lv_file_ext = listLast(FORM.pickfiles_inv, ".") />

How do you track templates calls in ColdFusion?

How do you track template path in ColdFusion?
I.E.
I have the following folder and file structure
index.cfm
<cfset ArrayAppend(request.Trace, '/')>
<cfdump var=#request.trace#>
foo
index.cfm
<cfset ArrayAppend(request.Trace, '/foo/')>
<cfinclude template='../'>
bar
index.cfm
ArrayAppend(request.Trace,'/foo/bar/')>
<cfinclude template='../'>
When I call foo/bar/index.cfm,
request.Trace equals:
'/foo/bar/'
'/foo/'
'/'
How could I do this without specifically declaring each folder name?
Have a look at:
expandPath(".")
getBaseTemplatePath()
getCurrentTemplatePath()
CGI.CF_TEMPLATE_PATH
CGI.PATH_TRANSLATED
CGI.SCRIPT_NAME
If you want the template stack trace, use this:
<cfset templateTrace = []>
<cfset tagTrace = createObject("java","java.lang.Exception").init().TagContext>
<cfloop array="#tagTrace#" index="tagInfo">
<cfset templateTrace.add(tagInfo.Template)>
</cfloop>
<cfdump var="#templateTrace#">
This will output all templates passed up to this call.
Not ideal but this worked for me.
<cfset currentFile = GetCurrentTemplatePath()>
<cfset currentDir = GetDirectoryFromPath(currentFile)>
<cfset webroot = expandPath("/")>
<cfset m_Trace = Replace(currentDir, webroot , '\')>
<cfset ArrayAppend (request.Trace, m_Trace )>

How to avoid evaluate()

How can I write the equivalent for
<cfset lang = Evaluate("SERVER.Locale.#LocaleName#.#SESSION.Locale#")>
without using Evaluate.
I tried something like
<cfset lang = SERVER[Locale][#LocaleName#][#SESSION.Locale#]>
after referring this, but it was not working.
UPDATE
<cfset localename = "test">
<cfset session.locale = "en">
<cfif Not IsDefined("SERVER.LOCALE")>
<cfset SERVER.Locale = StructNew()>
</cfif>
<cfif IsDefined("SERVER.Locale.#LocaleName#")>
<cfset StructDelete(SERVER.Locale, "#LocaleName#")>
</cfif>
<cfset "SERVER.Locale.#LocaleName#" = StructNew()>
<!---<cfset lang = Evaluate("SERVER.Locale.#LocaleName#.#SESSION.Locale#")>--->
<!---<cfset lang = SERVER["Locale.#LocaleName#.#SESSION.Locale#"]>--->
<cfset lang = SERVER.Locale[LocaleName][SESSION.Locale]>
<cfoutput>#lang#</cfoutput>
ERROR:
Element en is undefined in a CFML structure referenced as part of an expression.
This:
<cfset lang = Evaluate("SERVER.Locale.#LocaleName#.#SESSION.Locale#")>
Should be revised to this:
<cfset lang = SERVER.Locale[LocaleName][SESSION.Locale]>
You should not have the [Locale] in square brackets like that.
I think the error message (which you should always post with your question) probably actually explains that to you, dunnit?

Need to use the smtp settings of coldfusion 10

There is a new settings of Application.cfc in the coldfusion 10 which is:
this.smtpServersettings = as a structure
I can hard code the values here, but i am saving my mail settings in the database and i want to use those values here, is it possible or not to use the database settings here or not.
here is my Update:
<cfcomponent hint="File for the Website" output="false">
<cfsetting showdebugoutput="no">
<cfset this.name = "myProject1">
<cfset this.applicationTimeout = createTimeSpan(2,0,0,0)>
<cfset this.clientmanagement= "yes">
<cfset this.loginstorage = "session">
<cfset this.sessionmanagement = "yes">
<cfset this.sessiontimeout = CreateTimeSpan(0,0,40,0) />
<cfset this.setClientCookies = "yes">
<cfset this.scriptProtect = "all">
<cfset this.setDomainCookies = true>
<cfset this.customTagPaths = ExpandPath('customtags')>
<cfset this.datasource = {name="myDB"}>
<cfset this.smtpServersettings = {structur as username,password and mail settings}>
but that value needs to be through database, can you show me an example please
Yes, you should be able to set the value of the this.smtpServersettings variable from a database query. And for what it's worth, I think that setting as been available since ColdFusion 9.
All you would need to do is something like the following.
Run your query to get the values from the database
Create a new structure
Assign the three variables to that structure; server, username, and password
Assign the this.smtpServersettings variable to your new structure
Of course you will need to add some code to do something if/when your database query fails.
Update after the question was updated
Just do something like the following (pseudo code):
<cfcomponent hint="File for the Website" output="false">
<cfsetting showdebugoutput="no">
<cfset this.name = "myProject1">
<cfset this.applicationTimeout = createTimeSpan(2,0,0,0)>
<cfset this.clientmanagement= "yes">
<cfset this.loginstorage = "session">
<cfset this.sessionmanagement = "yes">
<cfset this.sessiontimeout = CreateTimeSpan(0,0,40,0) />
<cfset this.setClientCookies = "yes">
<cfset this.scriptProtect = "all">
<cfset this.setDomainCookies = true>
<cfset this.customTagPaths = ExpandPath('customtags')>
<cfset this.datasource = {name="myDB"}>
<!--- run your query here --->
<!--- check your query and do something appropriate if/when it fails --->
<cfset this.MyStructure = StructNew()>
<cfset this.MyStructure.server = this.MyQuery.ServerNameVariable>
<cfset this.MyStructure.username = this.MyQuery.UserNameVariable>
<cfset this.MyStructure.password = this.MyQuery.PasswordVariable>
<cfset this.smtpServersettings = this.MyStructure>
You also need to be aware that doing it this way will run that query on every page request. Make sure it runs fast.

ColdFusion 8 - Application.cfc interference

I've just had a strange issue with a client's website, my live domain was using my development domains Application settings. I've not had this issue before, and removing cflock around the Application.dsn (amongst other settings) resolved the issue.
As mentioned I have a live site *www.* and development site *dev.*, my development site is in a subfolder of the live site /dev/ and has it's own Application.cfc.
My first question is, if I have variables in my Application scope in the dev folder will this overwrite the Application scope variables in the folder above? Will it consider it the same scope? If so, then this might explain what the issue and if not then I am stumped.
My second question is, how should I correct cflock application scope variables in my application.cfc? Should I be doing this at all?
Here is my Application.cfc, advice would be greatly appreciated:
<cfcomponent output="true">
<cfimport taglib="taglib" prefix="func">
<!---
Check staging directory exists
--->
<cfset THIS.env = "staging">
<!---
Set application vars
--->
<cfset THIS.applicationTimeout = createTimeSpan(0,0,0,0)>
<cfset THIS.sessionManagement="Yes">
<cfset THIS.clientManagement = true>
<cfset THIS.clientStorage = "cookie">
<cfset THIS.loginStorage = "cookie">
<cfset THIS.setDomainCookies = false>
<cfset THIS.setClientCookies = true>
<cfset THIS.scriptProtect = true>
<cfset THIS.secureJSON = true> <!--- Added 12.06.13 --->
<!---
Check environment
Set application name
--->
<cfif THIS.env EQ "staging">
<cfset THIS.applicationName = "devenv">
<cfset THIS.dsn = "devenv">
<cfelse>
<cfset THIS.applicationName = "liveenv">
<cfset THIS.dsn = "liveenv">
</cfif>
<cfif #cgi.HTTP_HOST# NEQ "localhost">
<cfset THIS.dirpath = "http://#cgi.http_host#">
<cfset THIS.componentPath = "cfcs.">
<cfelse>
<cfset urlString = #mid(cgi.PATH_INFO, 2, 200)#>
<cfset THIS.localhostFolderName = #spanexcluding(urlString, "/")#>
<cfset THIS.dirpath = "http://localhost/#THIS.localhostFolderName#">
<cfset THIS.componentPath = "#THIS.localhostFolderName#.cfcs.">
</cfif>
<cfset THIS.name = THIS.applicationName>
<cfset THIS.sessiontimeout = createtimespan(0,0,20,0)>
<cfset THIS.setClientCookies = true>
<cfset THIS.visitor = true>
<cffunction name="onApplicationStart" returntype="void">
<cfset APPLICATION.name = THIS.applicationName>
<cfset APPLICATION.dsn = THIS.dsn>
<cfset APPLICATION.DSN = THIS.dsn>
<cfset APPLICATION.dirpath = THIS.dirpath>
<cfset APPLICATION.componentPath = THIS.componentPath>
<cfif #cgi.HTTP_HOST# EQ "localhost">
<cfset APPLICATION.localhostFolderName = THIS.localhostFolderName>
</cfif>
<!--- USED FOR PATHS AND URLS --->
<!--- Property image upload paths ---->
<cfset APPLICATION.paths = StructNew()>
<!---
Check environment
Set local root
--->
<cfif THIS.env EQ "staging">
<cfset APPLICATION.paths.localRoot = "c:\websites\foobar.co.uk\dev\">
<cfelse>
<cfset APPLICATION.paths.localRoot = "c:\websites\foobar.co.uk\">
</cfif>
<cfset APPLICATION.paths.logs = APPLICATION.paths.localRoot & "logs\">
<cfset APPLICATION.paths.logFile = APPLICATION.paths.logs & "site_log.txt">
<cfset APPLICATION.paths.property = StructNew()>
<cfset APPLICATION.paths.property.image = APPLICATION.paths.localRoot & "images\property\">
<cfset APPLICATION.paths.property.large = APPLICATION.paths.property.image & "large\">
<cfset APPLICATION.paths.property.thumb = APPLICATION.paths.property.image & "thumbs\">
<cfset APPLICATION.paths.property.cmsThumb = APPLICATION.paths.property.image & "thumbs\cms\">
<cfset APPLICATION.paths.property.pdf = APPLICATION.paths.localRoot & "pdf\">
<cfset APPLICATION.paths.property.pdfGenerated = APPLICATION.paths.property.pdf & "generated\">
<cfset APPLICATION.newsUploadPath = APPLICATION.paths.localRoot & "images\news\">
<cfset APPLICATION.articlesUploadPath = APPLICATION.paths.localRoot & "images\articles\">
<cfset APPLICATION.articlesThumbsDir = "../images/articles/thumbs/">
<cfset APPLICATION.articlesContentDir = "../images/articles/assets/">
<cfset APPLICATION.articlesAssetsDir = "../articles/assets/">
<!--- Site URLS ---->
<cfset APPLICATION.urls = StructNew()>
<cfset APPLICATION.urls.root = "http://" & CGI.server_name & "/">
<cfset APPLICATION.urls.com = "com">
<cfset APPLICATION.urls.tagLib = APPLICATION.urls.root & "taglib/">
<cfset APPLICATION.urls.cms.tagLib = "http://" & CGI.server_name & ":" & CGI.server_port & "/admin/tagLib/">
<cfset APPLICATION.RowsPerPage = 10>
<!--- Property URLS --->
<cfset APPLICATION.urls.property.pdf = APPLICATION.urls.root & "pdf/">
<cfset APPLICATION.urls.property.image = APPLICATION.urls.root & "images/property/">
<cfset APPLICATION.urls.property.large = APPLICATION.urls.root & "images/property/large/">
<cfset APPLICATION.urls.property.thumb = APPLICATION.urls.root & "images/property/thumbs/">
<cfset APPLICATION.urls.property.cmsThumb = APPLICATION.urls.root & "images/property/thumbs/cms/">
<cfset APPLICATION.urls.news.image = APPLICATION.urls.root & "images/news/">
<cfset APPLICATION.urls.articles.image = APPLICATION.urls.root & "images/articles/">
<cflock scope="Application" timeout="5" type="Exclusive">
<cfscript>
/* Commonly used objects and queries */
// DAOs
APPLICATION.propertyDAO = CreateObject("component", "cfcs.dataobjects.propertyDAO").init(APPLICATION.dsn);
APPLICATION.propertyImageDAO = CreateObject("component", "cfcs.dataobjects.property_imageDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyImageDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_imageDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyLocationDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_locationDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTypeDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_typeDAO").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTenureDAO = CreateObject("component", "cfcs.dataobjects.property_to_property_tenureDAO").init(APPLICATION.dsn);
APPLICATION.propertyGroupDAO = CreateObject("component", "cfcs.dataobjects.property_groupDAO").init(APPLICATION.dsn);
// Gateways
APPLICATION.propertyGateway = CreateObject("component", "cfcs.dataobjects.propertyGateway").init(APPLICATION.dsn);
APPLICATION.propertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_imageGateway").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_to_property_imageGateway").init(APPLICATION.dsn);
APPLICATION.propertyLocationGateway = CreateObject("component", "cfcs.dataobjects.property_locationGateway").init(APPLICATION.dsn);
APPLICATION.propertyImageGateway = CreateObject("component", "cfcs.dataobjects.property_imageGateway").init(APPLICATION.dsn);
APPLICATION.propertyTypeGateway = CreateObject("component", "cfcs.dataobjects.property_typeGateway").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTypeGateway = CreateObject("component", "cfcs.dataobjects.property_typeGateway").init(APPLICATION.dsn);
APPLICATION.propertyTenureGateway = CreateObject("component", "cfcs.dataobjects.property_tenureGateway").init(APPLICATION.dsn);
APPLICATION.propertyToPropertyTenureGateway = CreateObject("component", "cfcs.dataobjects.property_to_property_tenureGateway").init(APPLICATION.dsn);
APPLICATION.partnerGateway = CreateObject("component", "cfcs.dataobjects.partnerGateway").init(APPLICATION.dsn);
// Business Objects
APPLICATION.propertyBO = CreateObject("component", "cfcs.businessobjects.propertyBO").init(APPLICATION.dsn);
// Common queries
APPLICATION.qPartners = APPLICATION.partnerGateway.getAllRecords();
APPLICATION.qPropertyTypes = APPLICATION.propertyTypeGateway.getAllRecords();
APPLICATION.qPropertyTenures = APPLICATION.propertyTenureGateway.getAllRecords();
APPLICATION.qPropertyMinMaxSize = APPLICATION.propertyGateway.getMinMaxSize();
APPLICATION.qPropertyLocations = APPLICATION.propertyLocationGateway.getAllRecords();
</cfscript>
</cflock>
</cffunction>
<cffunction name="onSessionStart" returntype="void">
<cflock scope="Session" timeout="5" type="Exclusive">
<cfscript>
SESSION.propertySearchCriteria = CreateObject("component", "cfcs.beans.property_search_criteria").init();
SESSION.propertySearch = CreateObject("component", "cfcs.beans.property_search").init(SESSION.propertySearchCriteria);
</cfscript>
</cflock>
</cffunction>
Why do you change your application name based on the environment it's in? That makes no sense. The application is still the same app, irrespective of whether it's in prod, or you're staging it, or developing it.
Equally, same with the source code. You've got your dev site within your live website? How does this make any sense?
I dunno what's going on with your application variables, but they're bound to the application name (this.name), not where the Application.cfc is in the directory structure.
I suspect your zero applicationTimeout is not helping matters much here. This basically means your application never actually persists... it'll be timing out every request. This makes no sense either.
You do not need to <cflock> that code in onApplicationStart(). ColdFusion will only allow one request to run onApplicationStart(): all other requests will be queued until it's completed, by which time the application has started, so the queued requests won't even attempt to run it.
I think your problem is most likely causes by your poor application framework design, and this weird thing you're doing having dev and prod in the same source code tree (which defies all logic).
Sort your source code out, and the problem won't arise.
You can use conditional logic to set your application name.
if ( YouAreHappy and YouKnowIt ) {
This.name = "ClapYourHands";
}
else {
This.name = "StompYourFeet";
}
In real life, your if condition would be something that differented the live and test environments. cgi.http_host is a good candidate.