root/trunk/website/forums/cfcs/utils.cfc @ 40

Revision 5, 10.8 kB (checked in by DanWilson, 17 years ago)

Initial Commit Of ModelGlue? Website (upgrade to blogcfc 511)

Line 
1<cfcomponent displayName="Utils" hint="Set of common methods.">
2        <!---
3        /**
4         * This function takes URLs in a text string and turns them into links.
5         * Version 2 by Lucas Sherwood, lucas@thebitbucket.net.
6         * Version 3 Updated to allow for ;
7         *
8         * @param string         Text to parse. (Required)
9         * @param target         Optional target for links. Defaults to "". (Optional)
10         * @param paragraph      Optionally add paragraphFormat to returned string. (Optional)
11         * @return Returns a string.
12         * @author Joel Mueller (jmueller@swiftk.com)
13         * @version 3, August 11, 2004
14         */
15        --->
16        <cffunction name="activeURL" access="public" returnType="string" output="false">
17        <cfargument name="string" type="string" required="true">
18        <cfscript>
19        var nextMatch = 1;
20        var objMatch = "";
21        var outstring = "";
22        var thisURL = "";
23        var thisLink = "";
24        var     target = IIf(arrayLen(arguments) gte 2, "arguments[2]", DE(""));
25        var paragraph = IIf(arrayLen(arguments) gte 3, "arguments[3]", DE("false"));
26       
27        do {
28                objMatch = REFindNoCase("(((https?:|ftp:|gopher:)\/\/)|(www\.|ftp\.))[-[:alnum:]\?%,\.\/&##!;@:=\+~_]+[A-Za-z0-9\/]", string, nextMatch, true);
29                if (objMatch.pos[1] GT nextMatch OR objMatch.pos[1] EQ nextMatch) {
30                        outString = outString & Mid(String, nextMatch, objMatch.pos[1] - nextMatch);
31                } else {
32                        outString = outString & Mid(String, nextMatch, Len(string));
33                }
34                nextMatch = objMatch.pos[1] + objMatch.len[1];
35                if (ArrayLen(objMatch.pos) GT 1) {
36                        // If the preceding character is an @, assume this is an e-mail address
37                        // (for addresses like admin@ftp.cdrom.com)
38                        if (Compare(Mid(String, Max(objMatch.pos[1] - 1, 1), 1), "@") NEQ 0) {
39                                thisURL = Mid(String, objMatch.pos[1], objMatch.len[1]);
40                                thisLink = "<A HREF=""";
41                                switch (LCase(Mid(String, objMatch.pos[2], objMatch.len[2]))) {
42                                        case "www.": {
43                                                thisLink = thisLink & "http://";
44                                                break;
45                                        }
46                                        case "ftp.": {
47                                                thisLink = thisLink & "ftp://";
48                                                break;
49                                        }
50                                }
51                                thisLink = thisLink & thisURL & """";
52                                if (Len(Target) GT 0) {
53                                        thisLink = thisLink & " TARGET=""" & Target & """";
54                                }
55                                thisLink = thisLink & ">" & thisURL & "</A>";
56                                outString = outString & thisLink;
57                                // String = Replace(String, thisURL, thisLink);
58                                // nextMatch = nextMatch + Len(thisURL);
59                        } else {
60                                outString = outString & Mid(String, objMatch.pos[1], objMatch.len[1]);
61                        }
62                }
63        } while (nextMatch GT 0);
64               
65        // Now turn e-mail addresses into mailto: links.
66        outString = REReplace(outString, "([[:alnum:]_\.\-]+@([[:alnum:]_\.\-]+\.)+[[:alpha:]]{2,4})", "<A HREF=""mailto:\1"">\1</A>", "ALL");
67               
68        if (paragraph) {
69                outString = ParagraphFormat(outString);
70        }
71        return outString;
72        </cfscript>
73        </cffunction>
74
75<!---
76Copyright for coloredCode function. Also note that Jeff Coughlin made some mods to this as well.
77=============================================================
78        Utility:        ColdFusion ColoredCode v3.2
79        Author:         Dain Anderson
80        Email:          webmaster@cfcomet.com
81        Revised:        June 7, 2001
82        Download:       http://www.cfcomet.com/cfcomet/utilities/
83=============================================================
84--->
85        <cffunction name="coloredCode" output="false" returnType="string" access="public"
86                           hint="Colors code">
87                <cfargument name="dataString" type="string" required="true">
88                <cfargument name="class" type="string" required="true">
89
90                <cfset var data = trim(arguments.dataString) />
91                <cfset var eof = 0>
92                <cfset var bof = 1>
93                <cfset var match = "">
94                <cfset var orig = "">
95                <cfset var chunk = "">
96
97                <cfscript>
98                /* Convert special characters so they do not get interpreted literally; italicize and boldface */
99                data = REReplaceNoCase(data, '&([[:alpha:]]{2,});', '«strong»«em»&amp;\1;«/em»«/strong»', 'ALL');
100       
101                /* Convert many standalone (not within quotes) numbers to blue, ie. myValue = 0 */
102                data = REReplaceNoCase(data, "(gt|lt|eq|is|,|\(|\))([[:space:]]?[0-9]{1,})", "\1«span style='color: ##0000ff'»\2«/span»", "ALL");
103       
104                /* Convert normal tags to navy blue */
105                data = REReplaceNoCase(data, "<(/?)((!d|b|c(e|i|od|om)|d|e|f(r|o)|h|i|k|l|m|n|o|p|q|r|s|t(e|i|t)|u|v|w|x)[^>]*)>", "«span style='color: ##000080'»<\1\2>«/span»", "ALL");
106       
107                /* Convert all table-related tags to teal */
108                data = REReplaceNoCase(data, "<(/?)(t(a|r|d|b|f|h)([^>]*)|c(ap|ol)([^>]*))>", "«span style='color: ##008080'»<\1\2>«/span»", "ALL");
109       
110                /* Convert all form-related tags to orange */
111                data = REReplaceNoCase(data, "<(/?)((bu|f(i|or)|i(n|s)|l(a|e)|se|op|te)([^>]*))>", "«span style='color: ##ff8000'»<\1\2>«/span»", "ALL");
112       
113                /* Convert all tags starting with 'a' to green, since the others aren't used much and we get a speed gain */
114                data = REReplaceNoCase(data, "<(/?)(a[^>]*)>", "«span style='color: ##008000'»<\1\2>«/span»", "ALL");
115       
116                /* Convert all image and style tags to purple */
117                data = REReplaceNoCase(data, "<(/?)((im[^>]*)|(sty[^>]*))>", "«span style='color: ##800080'»<\1\2>«/span»", "ALL");
118       
119                /* Convert all ColdFusion, SCRIPT and WDDX tags to maroon */
120                data = REReplaceNoCase(data, "<(/?)((cf[^>]*)|(sc[^>]*)|(wddx[^>]*))>", "«span style='color: ##800000'»<\1\2>«/span»", "ALL");
121       
122                /* Convert all inline "//" comments to gray (revised) */
123                data = REReplaceNoCase(data, "([^:/]\/{2,2})([^[:cntrl:]]+)($|[[:cntrl:]])", "«span style='color: ##808080'»«em»\1\2«/em»«/span»", "ALL");
124       
125                /* Convert all multi-line script comments to gray */
126                data = REReplaceNoCase(data, "(\/\*[^\*]*\*\/)", "«span style='color: ##808080'»«em»\1«/em»«/span»", "ALL");
127       
128                /* Convert all HTML and ColdFusion comments to gray */ 
129                /* The next 10 lines of code can be replaced with the commented-out line following them, if you do care whether HTML and CFML
130                   comments contain colored markup. */
131
132                while(NOT EOF) {
133                        Match = REFindNoCase("<!--" & "-?([^-]*)-?-->", data, BOF, True);
134                        if (Match.pos[1]) {
135                                Orig = Mid(data, Match.pos[1], Match.len[1]);
136                                Chunk = REReplaceNoCase(Orig, "«(/?[^»]*)»", "", "ALL");
137                                BOF = ((Match.pos[1] + Len(Chunk)) + 38); // 38 is the length of the SPAN tags in the next line
138                                data = Replace(data, Orig, "«span style='color: ##808080'»«em»#Chunk#«/em»«/span»");
139                        } else EOF = 1;
140                }
141
142
143                /* Convert all quoted values to blue */
144                data = REReplaceNoCase(data, """([^""]*)""", "«span style=""color: ##0000ff""»""\1""«/span»", "all");
145
146                /* Convert left containers to their ASCII equivalent */
147                data = REReplaceNoCase(data, "<", "&lt;", "ALL");
148
149                /* Convert right containers to their ASCII equivalent */
150                data = REReplaceNoCase(data, ">", "&gt;", "ALL");
151
152                /* Revert all pseudo-containers back to their real values to be interpreted literally (revised) */
153                data = REReplaceNoCase(data, "«([^»]*)»", "<\1>", "ALL");
154
155                /* ***New Feature*** Convert all FILE and UNC paths to active links (i.e, file:///, \\server\, c:\myfile.cfm) */
156                data = REReplaceNoCase(data, "(((file:///)|([a-z]:\\)|(\\\\[[:alpha:]]))+(\.?[[:alnum:]\/=^@*|:~`+$%?_##& -])+)", "<a target=""_blank"" href=""\1"">\1</a>", "ALL");
157
158                /* Convert all URLs to active links (revised) */
159                data = REReplaceNoCase(data, "([[:alnum:]]*://[[:alnum:]\@-]*(\.[[:alnum:]][[:alnum:]-]*[[:alnum:]]\.)?[[:alnum:]]{2,}(\.?[[:alnum:]\/=^@*|:~`+$%?_##&-])+)", "<a target=""_blank"" href=""\1"">\1</a>", "ALL");
160
161                /* Convert all email addresses to active mailto's (revised) */
162                data = REReplaceNoCase(data, "(([[:alnum:]][[:alnum:]_.-]*)?[[:alnum:]]@[[:alnum:]][[:alnum:].-]*\.[[:alpha:]]{2,})", "<a href=""mailto:\1"">\1</a>", "ALL");
163                </cfscript>
164
165                <!--- mod by ray --->
166                <!--- change line breaks at end to <br /> --->
167                <cfset data = replace(data,chr(13),"<br />","all") />
168                <!--- replace tab with 3 spaces --->
169                <cfset data = replace(data,chr(9),"&nbsp;&nbsp;&nbsp;","all") />
170                <cfset data = "<div class=""#arguments.class#"">" & data &  "</div>" />
171               
172                <cfreturn data>
173        </cffunction>
174       
175        <cffunction name="logSearch" returnType="void" output="false" access="public" hint="Logs a search request">
176                <cfargument name="searchTerms" type="string" required="true">
177                <cfargument name="dsn" type="string" required="true">
178                <cfargument name="tableprefix" type="string" required="true">
179               
180                <cfquery datasource="#arguments.dsn#">
181                        insert into #arguments.tableprefix#search_log(searchterms, datesearched)
182                        values(<cfqueryparam cfsqltype="cf_sql_varchar" value="#left(arguments.searchTerms, 255)#">,
183                               <cfqueryparam cfsqltype="cf_sql_timestamp" value="#now()#">)
184                </cfquery>
185               
186        </cffunction>
187       
188        <cffunction name="isTheUserInAnyRole" access="public" returnType="boolean" output="false"
189                                hint="isUserInRole only does AND checks. This method allows for OR checks.">
190               
191                <cfargument name="rolelist" type="string" required="true">
192                <cfset var role = "">
193               
194                <cfloop index="role" list="#rolelist#">
195                        <cfif isUserInRole(role)>
196                                <cfreturn true>
197                        </cfif>
198                </cfloop>
199               
200                <cfreturn false>
201               
202        </cffunction>
203       
204        <!---
205        /**
206         * An &quot;enhanced&quot; version of ParagraphFormat.
207         * Added replacement of tab with nonbreaking space char, idea by Mark R Andrachek.
208         * Rewrite and multiOS support by Nathan Dintenfas.
209         *
210         * @param string         The string to format. (Required)
211         * @return Returns a string.
212         * @author Ben Forta (ben@forta.com)
213         * @version 3, June 26, 2002
214         */
215        --->
216        <cffunction name="paragraphFormat2" access="public" returnType="string" output="false">
217                <cfargument name="str" type="string" required="true">
218                <cfscript>
219                //first make Windows style into Unix style
220                str = replace(str,chr(13)&chr(10),chr(10),"ALL");
221                //now make Macintosh style into Unix style
222                str = replace(str,chr(13),chr(10),"ALL");
223                //now fix tabs
224                str = replace(str,chr(9),"&nbsp;&nbsp;&nbsp;","ALL");
225                //now return the text formatted in HTML
226                return replace(str,chr(10),"<br />","ALL");
227                </cfscript>
228        </cffunction>
229       
230        <cffunction name="queryToStruct" access="public" returnType="struct" output="false"
231                                hint="Transforms a query to a struct.">
232                <cfargument name="theQuery" type="query" required="true">
233                <cfset var s = structNew()>
234                <cfset var q ="">
235               
236                <cfloop index="q" list="#theQuery.columnList#">
237                        <cfset s[q] = theQuery[q][1]>
238                </cfloop>
239               
240                <cfreturn s>
241               
242        </cffunction>
243       
244        <cffunction name="searchSafe" access="public" returnType="string" output="false"
245                                hint="Removes any non a-z, 0-9 characters.">
246                <cfargument name="string" type="string" required="true">
247               
248                <cfreturn reReplace(arguments.string,"[^a-zA-Z0-9[:space:]]+","","all")>
249        </cffunction>
250       
251        <cffunction name="throw" access="public" returnType="void" output="false"
252                                hint="Handles exception throwing.">
253                               
254                <cfargument name="type" type="string" required="true">         
255                <cfargument name="message" type="string" required="true">
256               
257                <cfthrow type="#arguments.type#" message="#arguments.message#">
258               
259        </cffunction>
260
261</cfcomponent>
Note: See TracBrowser for help on using the browser.