Regex function to display hyperlinks properly on ColdFusion page - regex

I am trying to fix an issue with a certain [Coldfusion generated PAGE][1]. Please note, I have little knowledge of ColdFusion and how it works. So if you need more information, please let me know and I will try and track it down.
In the "How to Apply" section of the page there is meant to be included a URL for a certain website. However the page shows only the first part of the URL in clickable hyperlink format. Everything after the / is shown as plain text. Can you please suggest a solution ? Here is how the url looks on the page :
http://example.com/job-vacancies
Please note that the sections are extracted from the database and displayed dynamically in web pages. This image shows how the text looks in the database:
This is the content of job.cfm:
<cfset Application.controller.job()>
And this is the content of Application.cfm:
<cfapplication name="UK_AC_UEL_JOBSEARCH"
sessionmanagement="true"
setclientcookies="false"
setdomaincookies="false">
<cfscript>
function onRequestStart()
{
var formKeys = StructKeyList(Form);
var urlKeys = StructKeyList(URL);
var key = "";
var value = "";
var i = 0;
// ....
if (NOT StructKeyExists(Application, "controller"))
{
Application.controller = CreateObject("component", "uk.ac.uel.jobsearch.controller.Controller");
}
// ....
}
onRequestStart();
Update 1
I am still in the process of looking for the file that contains what actually renders the page in question. I was able to find Controller.cfc but it doesn't seem like it's directly responsible for rendering the page. After further searching, here is the code I believe renders that section of the page:
<div class="display-item last">
<div id="how-to-apply">
<h2>How to apply</h2>
<p>#formatWithLinks(Arguments.data.ApplicationProcedure)#</p>
<dl>
<dt>Closing date</dt>
<cfif Arguments.data.HasClosingDate>
<dd>#DateFormat(Arguments.data.ClosingDate, "D MMMM YYYY")#</dd>
<cfelse>
<dd>None: ongoing recruitment</dd>
</cfif>
</dl>
</div>
</div>
I am not sure what to change to tackle this issue. Any help would be greatly appreciated!

Hi guys just got to the bottom of this, the functions formatWithLinks is the key to solve this, it was found in the view.cfc file as #Leigh suggested ( thank you ) and here is the complete code of the function:
<cffunction name="formatWithLinks" output="false" returntype="string">
<cfargument name="input" />
<cfscript>
var output = Arguments.input;
/*var httpREWithProtocol = "(?x)
(
((https|http):\/\/) ## protocol
(
[a-zA-Z]{1}
([\w-]+\.)+
([a-zA-Z]{2,}) ## top level domain
) ## domain name
(:[\d]{1,5})? ## port number
((/?\w+/)+|/?)
(\w+\.[\w]{3,4})?
((\?\w+=\w+)?
(&\w+=\w+)*)?
)";*/
var httpREWithoutProtocol = "(?x)
(\s) ## Space requirement here means this won't match beginning of string
(
(
[a-zA-Z]{1}
([\w-]+\.)+
([a-zA-Z]{2,}) ## top level domain
) ## domain name
(:[\d]{1,5})? ## port number
((/?\w+/)+|/?)
(\w+\.[\w]{3,4})?
((\?\w+=\w+)?
(&\w+=\w+)*)?
)";
var httpREWithoutProtocolBeginningOfString = "(?x)
^ ## Match string here; I don't like doing it this way, but at least it works
(
(
[a-zA-Z]{1}
([\w-]+\.)+
([a-zA-Z]{2,}) ## top level domain
) ## domain name
(:[\d]{1,5})? ## port number
((/?\w+/)+|/?)
(\w+\.[\w]{3,4})?
((\?\w+=\w+)?
(&\w+=\w+)*)?
)";
var emailRE = "(?x)
(
[\S]+# ## user
[a-zA-Z]{1}
([\w-]+\.)+
([a-zA-Z]{2,})
)";
output = REReplace(output, "<[^>]*>", "", "all");
output = XMLFormat(output);
//output = REReplace(output, httpREWithProtocol, "\1", "all");
output = REReplaceNoCase(output,"(\bhttp://[a-z0-9\.\-_:~###%&/?+=]+)", "\1", "all");
output = REReplace(output, httpREWithoutProtocol, "\1\2", "all");
output = REReplace(output, httpREWithoutProtocolBeginningOfString, "\1", "all");
output = REReplace(output, emailRE, "\1", "all");
return output;
</cfscript>
</cffunction>
thank you everyone for trying to help. i greatly appreciate your efforts

The application.cfm is not where your problem is. Have a look in the cfm that actually renders the page. You'll probably find something like this...
http://example.com/#section#
Change that to...
http://example.com/#section#
And you should be golden. Note that everything between the hash tags is a dynamic ColdFusion variable. Your variable name will likely be very different.

Related

Extracting the Domain from the URL using ColdFusion

I am currently using CGI variables to extract the full URL and the hostname from the current URL:
<cfset currentURL = CGI.SERVER_NAME>
<cfset host = ListFirst(currentURL, ".")>
The previous code works as expected.
I would like to also extract the domain. I can't find a CGI variable for that surprisingly, and I have tried the following code but it does not seem to work:
<cfset domain = ListLast(currentURL, "#host#")>
But the domain variable is only showing 'om' instead of 'domain.com' when I output it. What am I doing wrong here?
This is one way to do it and it will handle domains with multiple subdomain parts (e.g., local.dev.mydomain.com).
<cfset currentURL = CGI.SERVER_NAME>
<cfset domainParts = listLen(currentURL, ".")>
<cfset domain = gettoken(currentURL,domainParts-1,".") & "." & gettoken(currentURL,domainParts,".") >
The full URL would be:
'#getPageContext().getRequest().getScheme()#://#cgi.server_name#/#cgi.script_name#?#cgi.query_string#'
That will include protocol, path, and url variables.
The domain name is simply #cgi.server_name#.

URL replacement for Javascript code in ColdFusion

I have inherited a external page where I have no control:
I have javascript sorting on that page: like http://www.exampledomain.com/javascript:void(1);
Now it has many links like this, the 1 you see is dynamic, what I want to is: convert this code to ColdFusion URL like http://www.exampledomain.com/sor=1&sort=asc & desc. The 1 should work as it is, like it should keep its value as it is 1,2,3,4 etc. I tried to do this with jQuery.
How can I alter these links in ColdFusion?
I tried to come up with some of Javascript solution but it did not work
$('#container').find('a').attr('href', function(i, old) {
var col = decodeURIComponent(old).match(/javascript:\s*sort\((.*?)\)/)[1];
return hrefcall+data+'&sortBy='+col;
Thanks
Your question is unclear, the source of the data is unknown and there are a few typoes.
This JQuery (1.9.1)
var ihref = "";
var col = 'somedata';
$("a").click(function (i) {
ihref = $(this).attr('href');
if (ihref.match(/javascript:\s*sort\(\d+\).*/i)) {
ihref = ihref.replace(/javascript:\s*sort\((\d+)\).*/i,"http://www.w3schools.com/sor=$1&sortBy=" + col);
$(this).attr('href',ihref);
alert("As a demonstration, you\'ll see the link is rewritten when a javascript:sort url is clicked.");
}
});
Will convert all javascript:void links as you wanted. I did change the dummy url to w3schools.com because the good folks who own w3 permit their site to be loaded in Iframes which is necessary to easily demonstrate that this code works.
Of course, JQuery only works when JS is enabled. Still, since you started with JQuery, I thought I might show you a working demonstration.
(The links don't actually work, because w3schools doesn't have pages at those points, but you can see in the status bar, the links are rewritten).
If you're retrieving the page via cfhttp.filecontent, you can do something like this
<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sort=asc","ALL")>
The ReReplaceNoCase() was tested against this sample code..
<cfset cfhttp = {} />
<cfset col = "somedata" />
<cfsavecontent variable="cfhttp.filecontent">
test - will not alter url<Br />
test - will alter url<Br />
test - will alter url<Br />
test - will not alter url<Br />
</cfsavecontent>
<cfset cfhttp.filecontent = ReReplaceNoCase(cfhttp.filecontent,"javascript:\s*sort\((\d+)\);?","http://www.w3schools.com/sor=\1&sortBy=#col#","ALL")>
<cfdump var="#cfhttp#">

How to access the URL parameter id in Coldfusion if it's after a hashtag #?

After trying for about an hour without success... (coldfusion8) a dummy question, but I*m stuck:
My URL (Jquery Mobile, no pushstate, that's why it looks like it is):
http://www.page.de/test/mem/search.cfm#/test/mem/search.cfm?id=9900000003869
If I output:
<cfdump output="e:\website\dump.txt" label="catch" var="#url#">
I get this:
catch - struct
ID: 9900000003869
But how do i access it... I'm trying forever, nothing works:
<cfdump output="e:\website\dump.txt" label="catch" var="#id#">
<cfdump output="e:\website\dump.txt" label="catch" var="#ID#">
<cfdump output="e:\website\dump.txt" label="catch" var="#url.id#">
<cfdump output="e:\website\dump.txt" label="catch" var="#url.ID#">
<cfdump output="e:\website\dump.txt" label="catch" var="#StructGetValue(url,"id")d#">
...
Thanks for helping!
Ok... This works:
URL = http://www.page.de/test/mem/search.cfm#/test/mem/search.cfm?id=9900000003869
<cfset objRequest = GetPageContext().GetRequest() />
<cfset strUrl = right( objRequest.GetRequestUrl().Append( "?" & objRequest.GetQueryString() ).ToString(), 13)>
Credit
If someone finds an easier, please post. I will check as answer.
You're trying to read this from a txt file?
Can you not simply use:
<cfdump label="catch" var="#url.id#" />
Does that work?
EDIT:
Could you try capturing and formatting what you need first then after, writing it to the file?
For example, try using:
<cfsavecontent variable="myFileContents">
<cfoutput>#url.id#</cfoutput>
</cfsavecontent>
<cffile action="Write" file="e:\website\dump.txt" output="#myFileContents#" />
I have not tested this code, but give it a go and see!
Might want to put a check on that URL variable too using isDefined()
Good luck.
Doing some research on fragment identifiers (which is a new term to me :( )prompted by Peter and Duncan's comments, I've found from wiki: http://en.wikipedia.org/wiki/Fragment_identifier
The fragment identifier functions differently than the rest of the
URI: namely, its processing is exclusively client-side with no
participation from the server — of course the server typically helps
to determine the MIME type, and the MIME type determines the
processing of fragments. When an agent (such as a Web browser)
requests a resource from a Web server, the agent sends the URI to
the server, but does not send the fragment. Instead, the agent waits
for the server to send the resource, and then the agent processes the
resource according to the document type and fragment value.
now, being your client IS sending the fragment and the url variable is accessible to you for some reason, using it is done by my original post to follow.
<cfoutput>
is generally how you output a variable or other evaluations to the screen.
<cfset myName = "Travis">
<cfoutput>Hello, my name is #myName#</cfoutput>
You can also access the variable by using it in a statement that doesn't output anywhere.
<cfset myFullName = myName & " Mak">
You can also use the variables in a query
<cfquery name = "qSomeQuery" datasource = "#application.dsn#">
select * from table where id = #url.id#
</cfquery>
However, that's the bad way to use it in a query, you should always use cfquery param.
<cfquery name = "qSomeQuery" datasource = "#application.dsn#">
select * from table where id = <cfqueryparam cfsqltype="cf_sql_integer" value="#url.id#">
</cfquery>
The problem you're having in testing the variable is due to incorrect syntax.
<cfif isDefined("url.id")> verses <cfif isDefined(url.id)> a more accurate test is <cfif structKeyExists(url, "id")>
For some reason my CF server truncates everything in the url after the # but yours doesn't seem to have this problem. As your cfdump states, you can see your url variables so "accessing" the url variable is as easy as using it: #url.id# or testing it <cfif isDefined("url.id")>

How to determine if a full name has a space in it?

I have a field that a user can input first and last name to fill out my form. Sometimes, users put on their first name and that results in empty fields in my database. PLEASE keep in mind that I cannot change this method completely because this form is part of a bigger project and it is being used by other websites of my company.
This is the part of the code that i need the validation around it. I already have a validation that ensures that the filed is not empty but I need on more to ensure that the field has two items in it separated by space.
<input name="fullname" class="fullname" type="text" value="#fullname#" maxlength="150"/>
<cfif fullname eq '' and check2 eq 'check2'>
<br /><span style="color:red">*you must enter your full name</span></cfif>
The check2 eq 'check2' is checking if the form was submitted already to ensure a user submitting their data twice.
I thought of using regular expressions to do that but unfortunately I am not very familiar with how to use regx in CF9 and the documentation online through me off a bit.
I was also thinking to use "Find" or "FindOneOF", any thoughts on that?
Also, I am trying to avoid using JQ,JS etc, so please try to keep your suggestions based on CF code IF possible.
Any help or different suggestions on how to tackle this issue will be very appreciated.
No regex is needed for this. A slightly simpler solution:
<cfset form.fullname = "Dave " />
<cfif listLen(form.fullname," ") GT 1> <!--- space-delimited list, no need for trimming or anything --->
<!--- name has more than one 'piece' -- is good --->
<cfelse>
<!--- name has only one 'piece' -- bad --->
</cfif>
You could do something like this for server side validation:
<cfscript>
TheString = "ronger ddd";
TheString = trim(TheString); // get rid of beginning and ending spaces
SpaceAt = reFind(" ", TheString); // find the index of a space
// no space found -- one word
if (SpaceAt == 0) {
FullNameHasSpace = false;
// at least one space was found -- more than one word
} else {
FullNameHasSpace = true;
}
</cfscript>
<cfoutput>
<input type="input" value="#TheString#">
<cfif FullNameHasSpace eq true>
<p>found space at position #SpaceAt#</p>
<p>Your data is good.</p>
<cfelse>
<p>Did not find a space.</p>
<p>Your data is bad.</p>
</cfif>
</cfoutput>

Coldfusion Parse URLs in text

Can anyone help with a function that will parse all urls into valid html links in a text string?
For example:
"Welcome to www.nerds4life.com. View our articles at nerds4life.com or at http://nerds4life.com or also http://www.nerds4life.com"
would become:
"Welcome to www.nerds4life.com. View our articles at nerds4life.com or at http://nerds4life.com or also http://www.nerds4life.com"
What would be the best way to approach this. Regex (and if so, how?) or loop through each word in the text (would think that's less efficient)
Thanks
Again... there may be a more elegant regex...
Certainly feel free to google for "good" regex's for finding URLs if this one falls short.
<cfset myText = "Welcome to www.nerds4life.com. View our articles at nerds4life.com or at http://nerds4life.com or also http://www.nerds4life.com or at https://foo.com or http://123.com" />
<cfset myNewText = rereplaceNoCase( myText, '((http(s)?://)?((www\.)?\w+\.\w{2,6}))', '\1', 'all' ) />
This will parse URL in string that starts with http or www and terminated by a space
<cfset myString = "Welcome to www.nerds4life.com. View our articles at nerds4life.com or at http://nerds4life.com or also http://www.nerds4life.com">
<cfset URLinString = rereplaceNoCase(myString, '(((http(s)?://)|(www))\.?(\S+))', '\1', 'all')>