| [5] | 1 | <!--- |
|---|
| 2 | Name : utils |
|---|
| 3 | Author : Raymond Camden |
|---|
| 4 | Created : A long time ago in a galaxy far, far away |
|---|
| 5 | Last Updated : January 14, 2008 |
|---|
| 6 | History : Reset history for version 4.0 |
|---|
| 7 | : Added this header, and moved coloredCode in (rkc 9/22/05) |
|---|
| 8 | : Added Mail (rkc 7/7/06) |
|---|
| 9 | : Added getResource (rkc 8/20/06) |
|---|
| 10 | : fix in the comment area, not from me, but I forget who did it (rkc 1/18/08) |
|---|
| 11 | Purpose : Utilities |
|---|
| 12 | ---> |
|---|
| 13 | <cfcomponent displayName="Utils" output="false" hint="Utility functions for the Blog"> |
|---|
| 14 | |
|---|
| 15 | <!--- |
|---|
| 16 | Copyright for coloredCode function. Also note that Jeff Coughlin made some mods to this as well. |
|---|
| 17 | ============================================================= |
|---|
| 18 | Utility: ColdFusion ColoredCode v3.2 |
|---|
| 19 | Author: Dain Anderson |
|---|
| 20 | Email: webmaster@cfcomet.com |
|---|
| 21 | Revised: June 7, 2001 |
|---|
| 22 | Download: http://www.cfcomet.com/cfcomet/utilities/ |
|---|
| 23 | ============================================================= |
|---|
| 24 | ---> |
|---|
| 25 | <cffunction name="coloredCode" output="false" returnType="string" access="public" |
|---|
| 26 | hint="Colors code"> |
|---|
| 27 | <cfargument name="dataString" type="string" required="true"> |
|---|
| 28 | <cfargument name="class" type="string" required="true"> |
|---|
| 29 | |
|---|
| 30 | <cfset var data = trim(arguments.dataString) /> |
|---|
| 31 | <cfset var eof = 0> |
|---|
| 32 | <cfset var bof = 1> |
|---|
| 33 | <cfset var match = ""> |
|---|
| 34 | <cfset var orig = ""> |
|---|
| 35 | <cfset var chunk = ""> |
|---|
| 36 | |
|---|
| 37 | <cfscript> |
|---|
| 38 | /* Convert special characters so they do not get interpreted literally; italicize and boldface */ |
|---|
| 39 | data = REReplaceNoCase(data, '&([[:alpha:]]{2,});', '«strong»«em»&\1;«/em»«/strong»', 'ALL'); |
|---|
| 40 | |
|---|
| 41 | /* Convert many standalone (not within quotes) numbers to blue, ie. myValue = 0 */ |
|---|
| 42 | data = REReplaceNoCase(data, "(gt|lt|eq|is|,|\(|\))([[:space:]]?[0-9]{1,})", "\1«span style='color: ##0000ff'»\2«/span»", "ALL"); |
|---|
| 43 | |
|---|
| 44 | /* Convert normal tags to navy blue */ |
|---|
| 45 | 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"); |
|---|
| 46 | |
|---|
| 47 | /* Convert all table-related tags to teal */ |
|---|
| 48 | data = REReplaceNoCase(data, "<(/?)(t(a|r|d|b|f|h)([^>]*)|c(ap|ol)([^>]*))>", "«span style='color: ##008080'»<\1\2>«/span»", "ALL"); |
|---|
| 49 | |
|---|
| 50 | /* Convert all form-related tags to orange */ |
|---|
| 51 | data = REReplaceNoCase(data, "<(/?)((bu|f(i|or)|i(n|s)|l(a|e)|se|op|te)([^>]*))>", "«span style='color: ##ff8000'»<\1\2>«/span»", "ALL"); |
|---|
| 52 | |
|---|
| 53 | /* Convert all tags starting with 'a' to green, since the others aren't used much and we get a speed gain */ |
|---|
| 54 | data = REReplaceNoCase(data, "<(/?)(a[^>]*)>", "«span style='color: ##008000'»<\1\2>«/span»", "ALL"); |
|---|
| 55 | |
|---|
| 56 | /* Convert all image and style tags to purple */ |
|---|
| 57 | data = REReplaceNoCase(data, "<(/?)((im[^>]*)|(sty[^>]*))>", "«span style='color: ##800080'»<\1\2>«/span»", "ALL"); |
|---|
| 58 | |
|---|
| 59 | /* Convert all ColdFusion, SCRIPT and WDDX tags to maroon */ |
|---|
| 60 | data = REReplaceNoCase(data, "<(/?)((cf[^>]*)|(sc[^>]*)|(wddx[^>]*))>", "«span style='color: ##800000'»<\1\2>«/span»", "ALL"); |
|---|
| 61 | |
|---|
| 62 | /* Convert all HTML and ColdFusion comments to gray */ |
|---|
| 63 | /* The next 10 lines of code can be replaced with the commented-out line following them, if you do care whether HTML and CFML |
|---|
| 64 | comments contain colored markup. */ |
|---|
| 65 | |
|---|
| 66 | while(NOT EOF) { |
|---|
| 67 | Match = REFindNoCase("<!--" & "-?([^-]*)-?-->", data, BOF, True); |
|---|
| 68 | if (Match.pos[1]) { |
|---|
| 69 | Orig = Mid(data, Match.pos[1], Match.len[1]); |
|---|
| 70 | Chunk = REReplaceNoCase(Orig, "«(/?[^»]*)»", "", "ALL"); |
|---|
| 71 | BOF = ((Match.pos[1] + Len(Chunk)) + 38); // 38 is the length of the SPAN tags in the next line |
|---|
| 72 | data = Replace(data, Orig, "«span style='color: ##808080'»«em»#Chunk#«/em»«/span»"); |
|---|
| 73 | } else EOF = 1; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | /* Convert all inline "//" comments to gray (revised) */ |
|---|
| 77 | data = REReplaceNoCase(data, "([^:/]\/{2,2})([^\n]+)($|[\n])", "«span style='color: ##808080'»«em»\1\2«/em»«/span»", "ALL"); |
|---|
| 78 | |
|---|
| 79 | /* Convert all multi-line script comments to gray */ |
|---|
| 80 | data = REReplaceNoCase(data, "(\/\*[^\*]*\*\/)", "«span style='color: ##808080'»«em»\1«/em»«/span»", "ALL"); |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | /* Convert all quoted values to blue */ |
|---|
| 84 | data = REReplaceNoCase(data, """([^""]*)""", "«span style=""color: ##0000ff""»""\1""«/span»", "all"); |
|---|
| 85 | |
|---|
| 86 | /* Convert left containers to their ASCII equivalent */ |
|---|
| 87 | data = REReplaceNoCase(data, "<", "<", "ALL"); |
|---|
| 88 | |
|---|
| 89 | /* Convert right containers to their ASCII equivalent */ |
|---|
| 90 | data = REReplaceNoCase(data, ">", ">", "ALL"); |
|---|
| 91 | |
|---|
| 92 | /* Revert all pseudo-containers back to their real values to be interpreted literally (revised) */ |
|---|
| 93 | data = REReplaceNoCase(data, "«([^»]*)»", "<\1>", "ALL"); |
|---|
| 94 | |
|---|
| 95 | /* ***New Feature*** Convert all FILE and UNC paths to active links (i.e, file:///, \\server\, c:\myfile.cfm) */ |
|---|
| 96 | data = REReplaceNoCase(data, "(((file:///)|([a-z]:\\)|(\\\\[[:alpha:]]))+(\.?[[:alnum:]\/=^@*|:~`+$%?_##& -])+)", "<a target=""_blank"" href=""\1"">\1</a>", "ALL"); |
|---|
| 97 | |
|---|
| 98 | /* Convert all URLs to active links (revised) */ |
|---|
| 99 | data = REReplaceNoCase(data, "([[:alnum:]]*://[[:alnum:]\@-]*(\.[[:alnum:]][[:alnum:]-]*[[:alnum:]]\.)?[[:alnum:]]{2,}(\.?[[:alnum:]\/=^@*|:~`+$%?_##&-])+)", "<a target=""_blank"" href=""\1"">\1</a>", "ALL"); |
|---|
| 100 | |
|---|
| 101 | /* Convert all email addresses to active mailto's (revised) */ |
|---|
| 102 | data = REReplaceNoCase(data, "(([[:alnum:]][[:alnum:]_.-]*)?[[:alnum:]]@[[:alnum:]][[:alnum:].-]*\.[[:alpha:]]{2,})", "<a href=""mailto:\1"">\1</a>", "ALL"); |
|---|
| 103 | </cfscript> |
|---|
| 104 | |
|---|
| 105 | <!--- mod by ray ---> |
|---|
| 106 | <!--- change line breaks at end to <br /> ---> |
|---|
| 107 | <cfset data = replace(data,chr(13),"<br />","all") /> |
|---|
| 108 | <!--- replace tab with 3 spaces ---> |
|---|
| 109 | <cfset data = replace(data,chr(9)," ","all") /> |
|---|
| 110 | <cfset data = "<div class=""#arguments.class#"">" & data & "</div>" /> |
|---|
| 111 | |
|---|
| 112 | <cfreturn data> |
|---|
| 113 | </cffunction> |
|---|
| 114 | |
|---|
| 115 | <cffunction name="configParam" output="false" returnType="string" access="public" |
|---|
| 116 | hint="Basically says, try to get name.key in ini file, and if not, default to default.key. Also support %default% expansion, which just means replace with default value"> |
|---|
| 117 | <cfargument name="iniFile" type="string" required="true"> |
|---|
| 118 | <cfargument name="name" type="string" required="true"> |
|---|
| 119 | <cfargument name="key" type="string" required="true"> |
|---|
| 120 | |
|---|
| 121 | <cfset var result = getProfileString(inifile,name,key)> |
|---|
| 122 | |
|---|
| 123 | <cfif result eq ""> |
|---|
| 124 | <cfset result = getProfileString(inifile,"Default",key)> |
|---|
| 125 | </cfif> |
|---|
| 126 | |
|---|
| 127 | <cfreturn result> |
|---|
| 128 | |
|---|
| 129 | </cffunction> |
|---|
| 130 | |
|---|
| 131 | <cffunction name="getResource" access="public" returnType="string" output="false" |
|---|
| 132 | hint="A utility wrapper for RB"> |
|---|
| 133 | <cfargument name="str" type="string" required="true"> |
|---|
| 134 | <cfreturn application.resourceBundle.getResource(arguments.str)> |
|---|
| 135 | </cffunction> |
|---|
| 136 | |
|---|
| 137 | <cffunction name="mail" access="public" returnType="void" output="false" |
|---|
| 138 | hint="This function handles the funky auth mail versus non auth mail. All mail ops for app should eventually go through this."> |
|---|
| 139 | <cfargument name="to" type="string" required="true"> |
|---|
| 140 | <cfargument name="from" type="string" required="true"> |
|---|
| 141 | <cfargument name="subject" type="string" required="true"> |
|---|
| 142 | <cfargument name="body" type="string" required="true"> |
|---|
| 143 | <cfargument name="cc" type="string" required="false" default=""> |
|---|
| 144 | <cfargument name="bcc" type="string" required="false" default=""> |
|---|
| 145 | <cfargument name="type" type="string" required="false" default="text"> |
|---|
| 146 | <cfargument name="mailserver" type="string" required="false" default=""> |
|---|
| 147 | <cfargument name="mailusername" type="string" required="false" default=""> |
|---|
| 148 | <cfargument name="mailpassword" type="string" required="false" default=""> |
|---|
| 149 | |
|---|
| 150 | <cfif arguments.mailserver is ""> |
|---|
| 151 | <cfmail to="#arguments.to#" from="#arguments.from#" cc="#arguments.cc#" bcc="#arguments.bcc#" subject="#arguments.subject#" type="#arguments.type#">#arguments.body#</cfmail> |
|---|
| 152 | <cfelse> |
|---|
| 153 | <cfmail to="#arguments.to#" from="#arguments.from#" cc="#arguments.cc#" bcc="#arguments.bcc#" subject="#arguments.subject#" type="#arguments.type#" |
|---|
| 154 | server="#arguments.mailserver#" username="#arguments.mailusername#" password="#arguments.mailpassword#">#arguments.body#</cfmail> |
|---|
| 155 | </cfif> |
|---|
| 156 | |
|---|
| 157 | </cffunction> |
|---|
| 158 | |
|---|
| 159 | <cffunction name="throw" access="public" returnType="void" output="false" |
|---|
| 160 | hint="Throws errors."> |
|---|
| 161 | <cfargument name="message" type="string" required="false" default=""> |
|---|
| 162 | <cfthrow type="blog.cfc" message="#arguments.message#"> |
|---|
| 163 | |
|---|
| 164 | </cffunction> |
|---|
| 165 | |
|---|
| 166 | <cffunction name="htmlToPlainText" access="public" returnType="string" output="false" |
|---|
| 167 | hint="Sanitizes a string from having HTML by replacing common entites and remove the others."> |
|---|
| 168 | <cfargument name="input" type="string" required="true"> |
|---|
| 169 | |
|---|
| 170 | <!---// remove html tags (do this first to avoid issues after replacing < & >) //---> |
|---|
| 171 | <cfset arguments.input = reReplace(arguments.input, "<[^>]+>", "", "all") /> |
|---|
| 172 | <!---// replace the ellipse entity with three periods //---> |
|---|
| 173 | <cfset arguments.input = replace(arguments.input, "…", "...", "all") /> |
|---|
| 174 | <!---// replace the em-dashes with two dashes //---> |
|---|
| 175 | <cfset arguments.input = reReplace(arguments.input, "(—)|(&##8212;)|(&##151;)", "--", "all") /> |
|---|
| 176 | <!---// replace the < entity with the real character //---> |
|---|
| 177 | <cfset arguments.input = replace(arguments.input, "<", "<", "all") /> |
|---|
| 178 | <!---// replace the > entity with the real character //---> |
|---|
| 179 | <cfset arguments.input = replace(arguments.input, ">", ">", "all") /> |
|---|
| 180 | <!---// replace the & entity with the real character //---> |
|---|
| 181 | <cfset arguments.input = replace(arguments.input, "&", "&", "all") /> |
|---|
| 182 | <!---// remove all other html entities //---> |
|---|
| 183 | <cfset arguments.input = reReplace(arguments.input, "(&##[0-9]+;)|(&[A-Za-z]+;)", "", "all") /> |
|---|
| 184 | |
|---|
| 185 | <cfreturn arguments.input /> |
|---|
| 186 | </cffunction> |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | <cffunction name="fixUrl" access="public" returntype="string" output="false" hint="Ensures a URL is properly formatted."> |
|---|
| 190 | <cfargument name="url" type="string" required="true" /> |
|---|
| 191 | |
|---|
| 192 | <cfreturn reReplace(arguments.url, "\/{2,}", "/", "all")> |
|---|
| 193 | </cffunction> |
|---|
| 194 | </cfcomponent> |
|---|