PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/global/internal.cfm

http://cfwheels.googlecode.com/
ColdFusion | 954 lines | 915 code | 39 blank | 0 comment | 79 complexity | a0b7a56d31ee1edb5ed29faef9b32980 MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cffunction name="$compactOutput" returntype="string" access="public" output="false">
  2. <cfargument name="output" type="string" required="true">
  3. <cfscript>
  4. var loc = {};
  5. if (!StructKeyExists(application.wheels.vendor, "compactor"))
  6. {
  7. loc.filePaths = [];
  8. loc.filePaths[1] = ExpandPath("wheels/vendor/compactor/compactor.jar");
  9. loc.javaLoader = CreateObject("component", "#application.wheels.wheelsComponentPath#.vendor.javaloader.JavaLoader").init(loc.filePaths);
  10. application.wheels.vendor.compactor = loc.javaLoader.create("com.mindprod.compactor.Compactor");
  11. }
  12. </cfscript>
  13. <cfreturn application.wheels.vendor.compactor.compactString(arguments.output, "") />
  14. </cffunction>
  15. <cffunction name="$htmlFormat" returntype="string" access="public" output="false">
  16. <cfargument name="string" type="string" required="true" />
  17. <cfscript>
  18. var loc = {};
  19. if (!StructKeyExists(application.wheels.vendor, "stringEscapeUtils"))
  20. {
  21. loc.filePaths = [];
  22. loc.filePaths[1] = ExpandPath("wheels/vendor/commons-lang/commons-lang-2.5.jar");
  23. loc.javaLoader = CreateObject("component", "#application.wheels.wheelsComponentPath#.vendor.javaloader.JavaLoader").init(loc.filePaths);
  24. application.wheels.vendor.stringEscapeUtils = loc.javaLoader.create("org.apache.commons.lang.StringEscapeUtils");
  25. }
  26. </cfscript>
  27. <cfreturn application.wheels.vendor.stringEscapeUtils.escapeHtml(arguments.string) />
  28. </cffunction>
  29. <cffunction name="$initializeRequestScope" returntype="void" access="public" output="false">
  30. <cfscript>
  31. if (!StructKeyExists(request, "wheels"))
  32. {
  33. request.wheels = {};
  34. request.wheels.vendor = {};
  35. request.wheels.routes = {};
  36. request.wheels.params = {};
  37. request.wheels.cache = {};
  38. // create a structure to track the transaction status for all adapters
  39. request.wheels.transactions = {};
  40. // store cache info for output in debug area
  41. request.wheels.cacheCounts = {};
  42. request.wheels.cacheCounts.hits = 0;
  43. request.wheels.cacheCounts.misses = 0;
  44. request.wheels.cacheCounts.culls = 0;
  45. }
  46. </cfscript>
  47. </cffunction>
  48. <cffunction name="$toXml" returntype="xml" access="public" output="false">
  49. <cfargument name="data" type="any" required="true">
  50. <cfscript>
  51. // only instantiate the toXml object once per request
  52. if (!StructKeyExists(request.wheels.vendor, "toXml"))
  53. request.wheels.vendor.toXml = $createObjectFromRoot(path="#application.wheels.wheelsComponentPath#.vendor.toXml", fileName="toXML", method="init");
  54. </cfscript>
  55. <cfreturn request.wheels.vendor.toXml.toXml(arguments.data) />
  56. </cffunction>
  57. <cffunction name="$convertToString" returntype="string" access="public" output="false">
  58. <cfargument name="value" type="Any" required="true">
  59. <cfscript>
  60. if (IsBinary(arguments.value))
  61. return ToString(arguments.value);
  62. else if (IsDate(arguments.value))
  63. return CreateDateTime(year(arguments.value), month(arguments.value), day(arguments.value), hour(arguments.value), minute(arguments.value), second(arguments.value));
  64. </cfscript>
  65. <cfreturn arguments.value>
  66. </cffunction>
  67. <cffunction name="$listClean" returntype="any" access="public" output="false" hint="removes whitespace between list elements. optional argument to return the list as an array.">
  68. <cfargument name="list" type="string" required="true">
  69. <cfargument name="delim" type="string" required="false" default=",">
  70. <cfargument name="returnAs" type="string" required="false" default="string">
  71. <cfargument name="defaultValue" type="any" required="false" default="">
  72. <cfscript>
  73. var loc = {};
  74. loc.list = ListToArray(arguments.list, arguments.delim);
  75. loc.iEnd = ArrayLen(loc.list);
  76. for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
  77. {
  78. loc.list[loc.i] = Trim(loc.list[loc.i]);
  79. }
  80. switch (arguments.returnAs)
  81. {
  82. case "array":
  83. {// already an array so just break out
  84. break;
  85. }
  86. case "struct":
  87. {
  88. loc.s = {};
  89. for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
  90. {
  91. loc.s[loc.list[loc.i]] = arguments.defaultValue;
  92. }
  93. loc.list = loc.s;
  94. break;
  95. }
  96. default:
  97. {// create a list using the supplied delimeter
  98. loc.list = ArrayToList(loc.list, arguments.delim);
  99. break;
  100. }
  101. }
  102. </cfscript>
  103. <cfreturn loc.list>
  104. </cffunction>
  105. <cffunction name="$simpleHashedKey" returntype="string" access="public" output="false" hint="Same as $hashedKey but cannot handle binary data in queries.">
  106. <cfargument name="value" type="any" required="true">
  107. <cfscript>
  108. var returnValue = "";
  109. returnValue = SerializeJSON(arguments.value);
  110. // remove the characters that indicate array or struct so that we can sort it as a list below
  111. returnValue = ReplaceList(returnValue, "{,},[,]", ",,,");
  112. returnValue = ListSort(returnValue, "text");
  113. return returnValue;
  114. </cfscript>
  115. </cffunction>
  116. <cffunction name="$hashedKey" returntype="string" access="public" output="false" hint="Creates a unique string based on any arguments passed in (used as a key for caching mostly).">
  117. <cfscript>
  118. var loc = {};
  119. loc.returnValue = "";
  120. // we need to make sure we are looping through the passed in arguments in the same order everytime
  121. loc.values = [];
  122. loc.keyList = ListSort(StructKeyList(arguments), "textnocase", "asc");
  123. loc.iEnd = ListLen(loc.keyList);
  124. for (loc.i = 1; loc.i <= loc.iEnd; loc.i++)
  125. ArrayAppend(loc.values, arguments[ListGetAt(loc.keyList, loc.i)]);
  126. if (!ArrayIsEmpty(loc.values))
  127. {
  128. // this might fail if a query contains binary data so in those rare cases we fall back on using cfwddx (which is a little bit slower which is why we don't use it all the time)
  129. try
  130. {
  131. loc.returnValue = $simpleHashedKey(loc.values);
  132. }
  133. catch (Any e)
  134. {
  135. loc.returnValue = $wddx(input=loc.values);
  136. }
  137. }
  138. return Hash(loc.returnValue);
  139. </cfscript>
  140. </cffunction>
  141. <cffunction name="$timeSpanForCache" returntype="any" access="public" output="false">
  142. <cfargument name="cache" type="any" required="true">
  143. <cfargument name="defaultCacheTime" type="numeric" required="false" default="#application.wheels.defaultCacheTime#">
  144. <cfargument name="cacheDatePart" type="string" required="false" default="#application.wheels.cacheDatePart#">
  145. <cfscript>
  146. var loc = {};
  147. loc.cache = arguments.defaultCacheTime;
  148. if (IsNumeric(arguments.cache))
  149. loc.cache = arguments.cache;
  150. loc.list = "0,0,0,0";
  151. loc.dateParts = "d,h,n,s";
  152. loc.iEnd = ListLen(loc.dateParts);
  153. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  154. if (arguments.cacheDatePart == ListGetAt(loc.dateParts, loc.i))
  155. loc.list = ListSetAt(loc.list, loc.i, loc.cache);
  156. return CreateTimeSpan(ListGetAt(loc.list, 1),ListGetAt(loc.list, 2),ListGetAt(loc.list, 3),ListGetAt(loc.list, 4));
  157. </cfscript>
  158. </cffunction>
  159. <cffunction name="$combineArguments" returntype="void" access="public" output="false">
  160. <cfargument name="args" type="struct" required="true">
  161. <cfargument name="combine" type="string" required="true">
  162. <cfargument name="required" type="boolean" required="false" default="false">
  163. <cfargument name="extendedInfo" type="string" required="false" default="">
  164. <cfscript>
  165. if (StructKeyExists(arguments.args, ListGetAt(arguments.combine, 2)))
  166. {
  167. arguments.args[ListGetAt(arguments.combine, 1)] = arguments.args[ListGetAt(arguments.combine, 2)];
  168. StructDelete(arguments.args, ListGetAt(arguments.combine, 2));
  169. }
  170. if (arguments.required && application.wheels.showErrorInformation)
  171. if (!StructKeyExists(arguments.args, ListGetAt(arguments.combine, 2)) && !Len(arguments.args[ListGetAt(arguments.combine, 1)]))
  172. $throw(type="Wheels.IncorrectArguments", message="The `#ListGetAt(arguments.combine, 2)#` or `#ListGetAt(arguments.combine, 1)#` argument is required but was not passed in.", extendedInfo="#arguments.extendedInfo#");
  173. </cfscript>
  174. </cffunction>
  175. <!--- helper method to recursively map a structure to build mapping paths and retrieve its values so you can have your way with a deeply nested structure --->
  176. <cffunction name="$mapStruct" returntype="void" access="public" output="false" mixin="dispatch">
  177. <cfargument name="map" type="struct" required="true" />
  178. <cfargument name="struct" type="struct" required="true" />
  179. <cfargument name="path" type="string" required="false" default="" />
  180. <cfscript>
  181. var loc = {};
  182. for (loc.item in arguments.struct)
  183. {
  184. if (IsStruct(arguments.struct[loc.item])) // go further down the rabit hole
  185. {
  186. $mapStruct(map=arguments.map, struct=arguments.struct[loc.item], path="#arguments.path#[#loc.item#]");
  187. }
  188. else // map our position and value
  189. {
  190. arguments.map["#arguments.path#[#loc.item#]"] = {};
  191. arguments.map["#arguments.path#[#loc.item#]"].value = arguments.struct[loc.item];
  192. }
  193. }
  194. </cfscript>
  195. </cffunction>
  196. <cffunction name="$structKeysExist" returntype="boolean" access="public" output="false" hint="Check to see if all keys in the list exist for the structure and have length.">
  197. <cfargument name="struct" type="struct" required="true" />
  198. <cfargument name="keys" type="string" required="false" default="" />
  199. <cfscript>
  200. var loc = {};
  201. loc.returnValue = true;
  202. loc.iEnd = ListLen(arguments.keys);
  203. for (loc.i = 1; loc.i lte loc.iEnd; loc.i++)
  204. {
  205. if (!StructKeyExists(arguments.struct, ListGetAt(arguments.keys, loc.i)) || (IsSimpleValue(arguments.struct[ListGetAt(arguments.keys, loc.i)]) && !Len(arguments.struct[ListGetAt(arguments.keys, loc.i)])))
  206. {
  207. loc.returnValue = false;
  208. break;
  209. }
  210. }
  211. </cfscript>
  212. <cfreturn loc.returnValue />
  213. </cffunction>
  214. <cffunction name="$cgiScope" returntype="struct" access="public" output="false" hint="This copies all the variables Wheels needs from the CGI scope to the request scope.">
  215. <cfargument name="keys" type="string" required="false" default="request_method,http_x_requested_with,http_referer,server_name,path_info,script_name,query_string,remote_addr,server_port,server_port_secure,server_protocol,http_host,http_accept,content_type">
  216. <cfscript>
  217. var loc = {};
  218. loc.returnValue = {};
  219. loc.iEnd = ListLen(arguments.keys);
  220. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  221. loc.returnValue[ListGetAt(arguments.keys, loc.i)] = cgi[ListGetAt(arguments.keys, loc.i)];
  222. </cfscript>
  223. <cfreturn loc.returnValue>
  224. </cffunction>
  225. <cffunction name="$dollarify" returntype="struct" access="public" output="false">
  226. <cfargument name="input" type="struct" required="true">
  227. <cfargument name="on" type="string" required="true">
  228. <cfscript>
  229. var loc = {};
  230. for (loc.key in arguments.input)
  231. {
  232. if (ListFindNoCase(arguments.on, loc.key))
  233. {
  234. arguments.input["$"&loc.key] = arguments.input[loc.key];
  235. StructDelete(arguments.input, loc.key);
  236. }
  237. }
  238. </cfscript>
  239. <cfreturn arguments.input>
  240. </cffunction>
  241. <cffunction name="$abortInvalidRequest" returntype="void" access="public" output="false">
  242. <cfscript>
  243. var applicationPath = Replace(GetCurrentTemplatePath(), "\", "/", "all");
  244. var callingPath = Replace(GetBaseTemplatePath(), "\", "/", "all");
  245. if (ListLen(callingPath, "/") GT ListLen(applicationPath, "/") || GetFileFromPath(callingPath) == "root.cfm")
  246. {
  247. $header(statusCode="404", statusText="Not Found");
  248. $includeAndOutput(template="#application.wheels.eventPath#/onmissingtemplate.cfm");
  249. $abort();
  250. }
  251. </cfscript>
  252. </cffunction>
  253. <cffunction name="$URLEncode" returntype="string" access="public" output="false">
  254. <cfargument name="param" type="string" required="false" default="">
  255. <cfscript>
  256. var returnValue = "";
  257. returnValue = URLEncodedFormat(arguments.param);
  258. returnValue = ReplaceList(returnValue, "%24,%2D,%5F,%2E,%2B,%21,%2A,%27,%28,%29", "$,-,_,.,+,!,*,',(,)"); // these characters are safe so set them back to their original values.
  259. </cfscript>
  260. <cfreturn returnValue>
  261. </cffunction>
  262. <cffunction name="$routeVariables" returntype="string" access="public" output="false">
  263. <cfscript>
  264. var loc = {};
  265. loc.route = $findRoute(argumentCollection=arguments);
  266. loc.returnValue = loc.route.variables;
  267. </cfscript>
  268. <cfreturn loc.returnValue>
  269. </cffunction>
  270. <cffunction name="$findRoute" returntype="struct" access="public" output="false">
  271. <cfscript>
  272. var loc = {};
  273. // throw an error if a route with this name has not been set by developer in the config/routes.cfm file
  274. if (application.wheels.showErrorInformation && !StructKeyExists(application.wheels.namedRoutePositions, arguments.route))
  275. $throw(type="Wheels.RouteNotFound", message="Could not find the `#arguments.route#` route.", extendedInfo="Create a new route in `config/routes.cfm` with the name `#arguments.route#`.");
  276. loc.routePos = application.wheels.namedRoutePositions[arguments.route];
  277. if (ArrayLen(loc.routePos) gt 1)
  278. {
  279. // get our routes - we cache them in the request.wheels.routes scope to save time on subsequent calls to $findRoute
  280. if (StructKeyExists(request.wheels.routes, arguments.route))
  281. {
  282. loc.routeArray = request.wheels.routes[arguments.route];
  283. }
  284. else
  285. {
  286. loc.routeArray = [];
  287. for (loc.i = 1; loc.i lte ArrayLen(loc.routePos); loc.i++)
  288. loc.routeArray[loc.i] = application.wheels.routes[loc.routePos[loc.i]];
  289. request.wheels.routes[arguments.route] = loc.routeArray;
  290. }
  291. loc.foundRoute = false;
  292. while (!loc.foundRoute) // need to use a while loop here so we don't loop through all of the routes
  293. {
  294. if (application.wheels.showErrorInformation && !ArrayLen(loc.routePos))
  295. $throw(type="Wheels.RouteMatchNotFound", message="Could not find a match for the `#arguments.route#` route.");
  296. // we always try to find the route on the first position because we are cleaning the array everytime we don't find a match
  297. loc.returnValue = loc.routeArray[1];
  298. loc.foundRoute = true;
  299. for (loc.i = 1; loc.i lte ListLen(loc.returnValue.variables); loc.i++)
  300. {
  301. loc.variable = ListGetAt(loc.returnValue.variables, loc.i);
  302. if (!StructKeyExists(arguments, loc.variable) || !Len(arguments[loc.variable]))
  303. {
  304. loc.foundRoute = false;
  305. break;
  306. }
  307. }
  308. // clean the array of all routes that contain the variable that failed
  309. if (!loc.foundRoute)
  310. for (loc.i = ArrayLen(loc.routeArray); loc.i gte 1; loc.i--)
  311. if (ListFindNoCase(loc.routeArray[loc.i].variables, loc.variable))
  312. ArrayDeleteAt(loc.routeArray, loc.i);
  313. }
  314. }
  315. else
  316. {
  317. loc.returnValue = application.wheels.routes[loc.routePos[1]];
  318. }
  319. </cfscript>
  320. <cfreturn loc.returnValue>
  321. </cffunction>
  322. <cffunction name="$cachedModelClassExists" returntype="any" access="public" output="false">
  323. <cfargument name="name" type="string" required="true">
  324. <cfscript>
  325. var returnValue = false;
  326. if (StructKeyExists(application.wheels.models, arguments.name))
  327. returnValue = application.wheels.models[arguments.name];
  328. </cfscript>
  329. <cfreturn returnValue>
  330. </cffunction>
  331. <cffunction name="$constructParams" returntype="string" access="public" output="false">
  332. <cfargument name="params" type="string" required="true">
  333. <cfargument name="$URLRewriting" type="string" required="false" default="#application.wheels.URLRewriting#">
  334. <cfscript>
  335. var loc = {};
  336. arguments.params = Replace(arguments.params, "&amp;", "&", "all"); // change to using ampersand so we can use it as a list delim below and so we don't "double replace" the ampersand below
  337. // when rewriting is off we will already have "?controller=" etc in the url so we have to continue with an ampersand
  338. if (arguments.$URLRewriting == "Off")
  339. loc.delim = "&";
  340. else
  341. loc.delim = "?";
  342. loc.returnValue = "";
  343. loc.iEnd = ListLen(arguments.params, "&");
  344. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  345. {
  346. loc.temp = listToArray(ListGetAt(arguments.params, loc.i, "&"), "=");
  347. loc.returnValue = loc.returnValue & loc.delim & loc.temp[1] & "=";
  348. loc.delim = "&";
  349. if (ArrayLen(loc.temp) == 2)
  350. {
  351. loc.param = $URLEncode(loc.temp[2]);
  352. if (application.wheels.obfuscateUrls && !ListFindNoCase("cfid,cftoken", loc.temp[1]))
  353. loc.param = obfuscateParam(loc.param);
  354. loc.returnValue = loc.returnValue & loc.param;
  355. }
  356. }
  357. </cfscript>
  358. <cfreturn loc.returnValue>
  359. </cffunction>
  360. <cffunction name="$args" returntype="any" access="public" output="false">
  361. <cfargument name="args" type="struct" required="true">
  362. <cfargument name="name" type="string" required="true">
  363. <cfargument name="reserved" type="string" required="false" default="">
  364. <cfargument name="combine" type="string" required="false" default="">
  365. <cfargument name="cachable" type="boolean" required="false" default="false">
  366. <cfscript>
  367. var loc = {};
  368. // if function result caching is enabled globally, the calling function is cachable and we're not coming from a recursive call we return the result from the cache (setting the cache first when necessary)
  369. if (application.wheels.cacheFunctions && arguments.cachable && !StructKeyExists(arguments.args, "$recursive"))
  370. {
  371. // create a unique key based on the arguments passed in to the calling function
  372. // we use the simple version of the $hashedKey function here for performance reasons (we know that we'll never have binary query data passed in anyway so we don't need to deal with that)
  373. loc.functionHash = $simpleHashedKey(arguments.args);
  374. // if the function result is not already in the cache we'll call the function and place the result in the cache
  375. loc.functionResult = $getFromCache(key=loc.functionHash);
  376. if (IsBoolean(loc.functionResult) && !loc.functionResult)
  377. {
  378. arguments.args.$recursive = true;
  379. loc.functionResult = $invoke(method=arguments.name, invokeArgs=arguments.args);
  380. $addToCache(key=loc.functionHash, value=loc.functionResult);
  381. }
  382. return loc.functionResult;
  383. }
  384. if (Len(arguments.combine))
  385. {
  386. loc.iEnd = ListLen(arguments.combine);
  387. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  388. {
  389. loc.item = ListGetAt(arguments.combine, loc.i);
  390. loc.first = ListGetAt(loc.item, 1, "/");
  391. loc.second = ListGetAt(loc.item, 2, "/");
  392. loc.required = false;
  393. if (ListLen(loc.item, "/") > 2)
  394. {
  395. loc.required = true;
  396. }
  397. $combineArguments(args=arguments.args, combine="#loc.first#,#loc.second#", required=loc.required);
  398. }
  399. }
  400. if (application.wheels.showErrorInformation)
  401. {
  402. if (ListLen(arguments.reserved))
  403. {
  404. loc.iEnd = ListLen(arguments.reserved);
  405. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  406. {
  407. loc.item = ListGetAt(arguments.reserved, loc.i);
  408. if (StructKeyExists(arguments.args, loc.item))
  409. $throw(type="Wheels.IncorrectArguments", message="The `#loc.item#` argument cannot be passed in since it will be set automatically by Wheels.");
  410. }
  411. }
  412. }
  413. if (StructKeyExists(application.wheels.functions, arguments.name))
  414. StructAppend(arguments.args, application.wheels.functions[arguments.name], false);
  415. </cfscript>
  416. </cffunction>
  417. <cffunction name="$createObjectFromRoot" returntype="any" access="public" output="false">
  418. <cfargument name="path" type="string" required="true">
  419. <cfargument name="fileName" type="string" required="true">
  420. <cfargument name="method" type="string" required="true">
  421. <cfscript>
  422. var returnValue = "";
  423. arguments.returnVariable = "returnValue";
  424. arguments.component = ListChangeDelims(arguments.path, ".", "/") & "." & ListChangeDelims(arguments.fileName, ".", "/");
  425. arguments.argumentCollection = Duplicate(arguments);
  426. StructDelete(arguments, "path");
  427. StructDelete(arguments, "fileName");
  428. </cfscript>
  429. <cfinclude template="../../root.cfm">
  430. <cfreturn returnValue>
  431. </cffunction>
  432. <cffunction name="$debugPoint" returntype="void" access="public" output="false">
  433. <cfargument name="name" type="string" required="true">
  434. <cfscript>
  435. var loc = {};
  436. if (!StructKeyExists(request.wheels, "execution"))
  437. request.wheels.execution = {};
  438. loc.iEnd = ListLen(arguments.name);
  439. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  440. {
  441. loc.item = ListGetAt(arguments.name, loc.i);
  442. if (StructKeyExists(request.wheels.execution, loc.item))
  443. request.wheels.execution[loc.item] = GetTickCount() - request.wheels.execution[loc.item];
  444. else
  445. request.wheels.execution[loc.item] = GetTickCount();
  446. }
  447. </cfscript>
  448. </cffunction>
  449. <cffunction name="$cachedControllerClassExists" returntype="any" access="public" output="false">
  450. <cfargument name="name" type="string" required="true">
  451. <cfscript>
  452. var returnValue = false;
  453. if (StructKeyExists(application.wheels.controllers, arguments.name))
  454. returnValue = application.wheels.controllers[arguments.name];
  455. </cfscript>
  456. <cfreturn returnValue>
  457. </cffunction>
  458. <cffunction name="$fileExistsNoCase" returntype="boolean" access="public" output="false">
  459. <cfargument name="absolutePath" type="string" required="true">
  460. <cfscript>
  461. var loc = {};
  462. // break up the full path string in the path name only and the file name only
  463. loc.path = GetDirectoryFromPath(arguments.absolutePath);
  464. loc.file = Replace(arguments.absolutePath, loc.path, "");
  465. // get all existing files in the directory and place them in a list
  466. loc.dirInfo = $directory(directory=loc.path);
  467. loc.fileList = ValueList(loc.dirInfo.name);
  468. // loop through the file list and return true if the file exists regardless of case (the == operator is case insensitive)
  469. loc.iEnd = ListLen(loc.fileList);
  470. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  471. if (ListGetAt(loc.fileList, loc.i) == loc.file)
  472. return true;
  473. // the file wasn't found in the directory so we return false
  474. return false;
  475. </cfscript>
  476. </cffunction>
  477. <cffunction name="$objectFileName" returntype="string" access="public" output="false">
  478. <cfargument name="name" type="string" required="true">
  479. <cfargument name="objectPath" type="string" required="true">
  480. <cfargument name="type" type="string" required="true" hint="Can be either `controller` or `model`." />
  481. <cfscript>
  482. var loc = {};
  483. loc.objectFileExists = false;
  484. // if the name contains the delimiter let's capitalize the last element and append it back to the list
  485. if (ListLen(arguments.name, "/") gt 1)
  486. arguments.name = ListInsertAt(arguments.name, ListLen(arguments.name, "/"), capitalize(ListLast(arguments.name, "/")), "/");
  487. else
  488. arguments.name = capitalize(arguments.name);
  489. // we are going to store the full controller path in the existing / non-existing lists so we can have controllers in multiple places
  490. loc.fullObjectPath = arguments.objectPath & "/" & arguments.name;
  491. if (!ListFindNoCase(application.wheels.existingObjectFiles, loc.fullObjectPath) && !ListFindNoCase(application.wheels.nonExistingObjectFiles, loc.fullObjectPath))
  492. {
  493. if (FileExists(ExpandPath("#loc.fullObjectPath#.cfc")))
  494. loc.objectFileExists = true;
  495. if (application.wheels.cacheFileChecking)
  496. {
  497. if (loc.objectFileExists)
  498. application.wheels.existingObjectFiles = ListAppend(application.wheels.existingObjectFiles, loc.fullObjectPath);
  499. else
  500. application.wheels.nonExistingObjectFiles = ListAppend(application.wheels.nonExistingObjectFiles, loc.fullObjectPath);
  501. }
  502. }
  503. if (ListFindNoCase(application.wheels.existingObjectFiles, loc.fullObjectPath) || loc.objectFileExists)
  504. loc.returnValue = arguments.name;
  505. else
  506. loc.returnValue = capitalize(arguments.type);
  507. </cfscript>
  508. <cfreturn loc.returnValue>
  509. </cffunction>
  510. <cffunction name="$createControllerClass" returntype="any" access="public" output="false">
  511. <cfargument name="name" type="string" required="true">
  512. <cfargument name="controllerPaths" type="string" required="false" default="#application.wheels.controllerPath#">
  513. <cfargument name="type" type="string" required="false" default="controller" />
  514. <cfscript>
  515. var loc = {};
  516. // let's allow for multiple controller paths so that plugins can contain controllers
  517. // the last path is the one we will instantiate the base controller on if the controller is not found on any of the paths
  518. for (loc.i = 1; loc.i lte ListLen(arguments.controllerPaths); loc.i++)
  519. {
  520. loc.controllerPath = ListGetAt(arguments.controllerPaths, loc.i);
  521. loc.fileName = $objectFileName(name=arguments.name, objectPath=loc.controllerPath, type=arguments.type);
  522. if (loc.fileName != "Controller" || loc.i == ListLen(arguments.controllerPaths))
  523. {
  524. application.wheels.controllers[arguments.name] = $createObjectFromRoot(path=loc.controllerPath, fileName=loc.fileName, method="$initControllerClass", name=arguments.name);
  525. loc.returnValue = application.wheels.controllers[arguments.name];
  526. break;
  527. }
  528. }
  529. </cfscript>
  530. <cfreturn loc.returnValue>
  531. </cffunction>
  532. <cffunction name="$addToCache" returntype="void" access="public" output="false">
  533. <cfset application.wheels.cache.add(argumentCollection=arguments)>
  534. </cffunction>
  535. <cffunction name="$getFromCache" returntype="any" access="public" output="false">
  536. <cfscript>
  537. var cacheItem = application.wheels.cache.get(argumentCollection=arguments);
  538. if (application.wheels.showDebugInformation)
  539. {
  540. if (IsSimpleValue(cacheItem) && IsBoolean(cacheItem) && !cacheItem)
  541. {
  542. request.wheels.cacheCounts.misses = request.wheels.cacheCounts.misses + 1;
  543. }
  544. else
  545. {
  546. request.wheels.cacheCounts.hits = request.wheels.cacheCounts.hits + 1;
  547. }
  548. }
  549. </cfscript>
  550. <cfreturn application.wheels.cache.get(argumentCollection=arguments)>
  551. </cffunction>
  552. <cffunction name="$removeFromCache" returntype="void" access="public" output="false">
  553. <cfset application.wheels.cache.remove(argumentCollection=arguments)>
  554. </cffunction>
  555. <cffunction name="$cacheCount" returntype="numeric" access="public" output="false">
  556. <cfreturn application.wheels.cache.count(argumentCollection=arguments)>
  557. </cffunction>
  558. <cffunction name="$clearCache" returntype="void" access="public" output="false">
  559. <cfreturn application.wheels.cache.clear(argumentCollection=arguments)>
  560. </cffunction>
  561. <cffunction name="$createModelClass" returntype="any" access="public" output="false">
  562. <cfargument name="name" type="string" required="true">
  563. <cfargument name="modelPaths" type="string" required="false" default="#application.wheels.modelPath#">
  564. <cfargument name="type" type="string" required="false" default="model" />
  565. <cfscript>
  566. var loc = {};
  567. // let's allow for multiple controller paths so that plugins can contain controllers
  568. // the last path is the one we will instantiate the base controller on if the controller is not found on any of the paths
  569. for (loc.i = 1; loc.i lte ListLen(arguments.modelPaths); loc.i++)
  570. {
  571. loc.modelPath = ListGetAt(arguments.modelPaths, loc.i);
  572. loc.fileName = $objectFileName(name=arguments.name, objectPath=loc.modelPath, type=arguments.type);
  573. if (loc.fileName != arguments.type || loc.i == ListLen(arguments.modelPaths))
  574. {
  575. application.wheels.models[arguments.name] = $createObjectFromRoot(path=loc.modelPath, fileName=loc.fileName, method="$initModelClass", name=arguments.name);
  576. loc.returnValue = application.wheels.models[arguments.name];
  577. break;
  578. }
  579. }
  580. </cfscript>
  581. <cfreturn loc.returnValue>
  582. </cffunction>
  583. <!---
  584. Used to announce to the developer that a feature they are using will be removed at some point.
  585. DOES NOT work in production mode.
  586. To use call $deprecated() from within the method you want to deprecate. You may pass an optional
  587. custom message if desired. The method will return a structure containing the message and information
  588. about where the deprecation occurrs like the called method, line number, template name and shows the
  589. code that called the deprcated method.
  590. Example:
  591. Original foo()
  592. <cffunction name="foo" returntype="any" access="public" output="false">
  593. <cfargument name="baz" type="numeric" required="true">
  594. <cfreturn baz++>
  595. </cffunction>
  596. Should now call bar() instead and marking foo() as deprecated
  597. <cffunction name="foo" returntype="any" access="public" output="false">
  598. <cfargument name="baz" type="numeric" required="true">
  599. <cfset $deprecated("Calling foo is now deprecated, use bar() instead.")>
  600. <cfreturn bar(argumentscollection=arguments)>
  601. </cffunction>
  602. <cffunction name="bar" returntype="any" access="public" output="false">
  603. <cfargument name="baz" type="numeric" required="true">
  604. <cfreturn ++baz>
  605. </cffunction>
  606. --->
  607. <cffunction name="$deprecated" returntype="struct" access="public" output="false">
  608. <!--- a message to display instead of the default one. --->
  609. <cfargument name="message" type="string" required="false" default="You are using deprecated behavior which will be removed from the next major or minor release.">
  610. <!--- should you announce the deprecation. only used when writing tests. --->
  611. <cfargument name="announce" type="boolean" required="false" default="true">
  612. <cfset var loc = {}>
  613. <cfset loc.ret = {}>
  614. <cfset loc.tagcontext = []>
  615. <cfif not application.wheels.showDebugInformation>
  616. <cfreturn loc.ret>
  617. </cfif>
  618. <!--- set return value --->
  619. <cfset loc.data = []>
  620. <cfset loc.ret = {message=arguments.message, line="", method="", template="", data=loc.data}>
  621. <!---
  622. create an exception so we can get the TagContext and display what file and line number the
  623. deprecated method is being called in
  624. --->
  625. <cftry>
  626. <cfthrow type="Expression">
  627. <cfcatch type="any">
  628. <cfset loc.exception = cfcatch>
  629. </cfcatch>
  630. </cftry>
  631. <cfif StructKeyExists(loc.exception, "tagcontext")>
  632. <cfset loc.tagcontext = loc.exception.tagcontext>
  633. </cfif>
  634. <!---
  635. TagContext is an array. The first element of the array will always be the context for this
  636. method announcing the deprecation. The second element will be the deprecated function that
  637. is being called. We need to look at the third element of the array to get the method that
  638. is calling the method marked for deprecation.
  639. --->
  640. <cfif isArray(loc.tagcontext) and arraylen(loc.tagcontext) gte 3 and isStruct(loc.tagcontext[3])>
  641. <!--- grab and parse the information from the tagcontext. --->
  642. <cfset loc.context = loc.tagcontext[3]>
  643. <!--- the line number --->
  644. <cfset loc.ret.line = loc.context.line>
  645. <!--- the deprecated method that was called --->
  646. <cfif StructKeyExists(server, "railo")>
  647. <cfset loc.ret.method = rereplacenocase(loc.context.codePrintPlain, '.*\<cffunction name="([^"]*)">.*', "\1")>
  648. <cfelse>
  649. <cfset loc.ret.method = rereplacenocase(loc.context.raw_trace, ".*\$func([^\.]*)\..*", "\1")>
  650. </cfif>
  651. <!--- the user template where the method called occurred --->
  652. <cfset loc.ret.template = loc.context.template>
  653. <!--- try to get the code --->
  654. <cfif len(loc.ret.template) and FileExists(loc.ret.template)>
  655. <!--- grab a one line radius from where the deprecation occurred. --->
  656. <cfset loc.startAt = loc.ret.line - 1>
  657. <cfif loc.startAt lte 0>
  658. <cfset loc.startAt = loc.ret.line>
  659. </cfif>
  660. <cfset loc.stopAt = loc.ret.line + 1>
  661. <cfset loc.counter = 1>
  662. <cfloop file="#loc.ret.template#" index="loc.i">
  663. <cfif loc.counter gte loc.startAt and loc.counter lte loc.stopAt>
  664. <cfset arrayappend(loc.ret.data, loc.i)>
  665. </cfif>
  666. <cfif loc.counter gt loc.stopAt>
  667. <cfbreak>
  668. </cfif>
  669. <cfset loc.counter++>
  670. </cfloop>
  671. </cfif>
  672. <!--- change template name from full to relative path. --->
  673. <cfset loc.ret.template = listchangedelims(removechars(loc.ret.template, 1, len(expandpath(application.wheels.webpath))), "/", "\/")>
  674. </cfif>
  675. <!--- append --->
  676. <cfif arguments.announce>
  677. <cfset arrayappend(request.wheels.deprecation, loc.ret)>
  678. </cfif>
  679. <cfreturn loc.ret>
  680. </cffunction>
  681. <cffunction name="$loadRoutes" returntype="void" access="public" output="false">
  682. <cfscript>
  683. // clear out the route info
  684. ArrayClear(application.wheels.routes);
  685. StructClear(application.wheels.namedRoutePositions);
  686. // load developer routes first
  687. $include(template="#application.wheels.configPath#/routes.cfm");
  688. // add the wheels default routes at the end if requested
  689. if (application.wheels.loadDefaultRoutes)
  690. addDefaultRoutes();
  691. // set lookup info for the named routes
  692. $setNamedRoutePositions();
  693. </cfscript>
  694. </cffunction>
  695. <cffunction name="$setNamedRoutePositions" returntype="void" access="public" output="false">
  696. <cfscript>
  697. var loc = {};
  698. loc.iEnd = ArrayLen(application.wheels.routes);
  699. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  700. {
  701. loc.route = application.wheels.routes[loc.i];
  702. if (StructKeyExists(loc.route, "name") && len(loc.route.name))
  703. {
  704. if (!StructKeyExists(application.wheels.namedRoutePositions, loc.route.name))
  705. application.wheels.namedRoutePositions[loc.route.name] = ArrayNew(1);
  706. ArrayAppend(application.wheels.namedRoutePositions[loc.route.name], loc.i);
  707. }
  708. }
  709. </cfscript>
  710. </cffunction>
  711. <cffunction name="$clearModelInitializationCache">
  712. <cfset StructClear(application.wheels.models)>
  713. </cffunction>
  714. <cffunction name="$clearControllerInitializationCache">
  715. <cfset StructClear(application.wheels.controllers)>
  716. </cffunction>
  717. <cffunction name="$loadPlugins" returntype="void" access="public" output="false">
  718. <cfscript>
  719. var loc = {};
  720. application.wheels.plugins = {};
  721. application.wheels.incompatiblePlugins = "";
  722. application.wheels.mixableComponents = "application,dispatch,controller,model,cache,base,connection,microsoftsqlserver,mysql,oracle,postgresql,h2";
  723. application.wheels.mixins = {};
  724. application.wheels.dependantPlugins = "";
  725. loc.pluginFolder = GetDirectoryFromPath(GetBaseTemplatePath()) & "plugins";
  726. // get a list of plugin files and folders
  727. loc.pluginFolders = $directory(directory=loc.pluginFolder, type="dir");
  728. loc.pluginFiles = $directory(directory=loc.pluginFolder, filter="*.zip", type="file", sort="name DESC");
  729. // delete plugin directories if no corresponding plugin zip file exists
  730. if (application.wheels.deletePluginDirectories)
  731. {
  732. loc.iEnd = loc.pluginFolders.recordCount;
  733. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  734. {
  735. loc.name = loc.pluginFolders["name"][loc.i];
  736. loc.directory = loc.pluginFolders["directory"][loc.i];
  737. if (Left(loc.name, 1) != "." && !ListContainsNoCase(ValueList(loc.pluginFiles.name), loc.name & "-"))
  738. {
  739. loc.directory = loc.directory & "/" & loc.name;
  740. $directory(action="delete", directory=loc.directory, recurse=true);
  741. }
  742. }
  743. }
  744. // create directory and unzip code for the most recent version of each plugin
  745. if (loc.pluginFiles.recordCount)
  746. {
  747. loc.iEnd = loc.pluginFiles.recordCount;
  748. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  749. {
  750. loc.name = loc.pluginFiles["name"][loc.i];
  751. loc.pluginName = ListFirst(loc.name, "-");
  752. if (!StructKeyExists(application.wheels.plugins, loc.pluginName))
  753. {
  754. loc.pluginVersion = Replace(ListLast(loc.name, "-"), ".zip", "", "one");
  755. loc.thisPluginFile = loc.pluginFolder & "/" & loc.name;
  756. loc.thisPluginFolder = loc.pluginFolder & "/" & LCase(loc.pluginName);
  757. if (!DirectoryExists(loc.thisPluginFolder))
  758. $directory(action="create", directory=loc.thisPluginFolder);
  759. // unzip the plugin to its directory unless the developer has told us not to
  760. // we don't use the overwrite attribute on cfzip since it's been reported that it updates the date on the files on railo
  761. if (application.wheels.overwritePlugins)
  762. $zip(action="unzip", destination=loc.thisPluginFolder, file=loc.thisPluginFile, overwrite=true);
  763. loc.fileName = LCase(loc.pluginName) & "." & loc.pluginName;
  764. loc.plugin = $createObjectFromRoot(path=application.wheels.pluginComponentPath, fileName=loc.fileName, method="init");
  765. loc.plugin.pluginVersion = loc.pluginVersion;
  766. if (!StructKeyExists(loc.plugin, "version") || ListFind(loc.plugin.version, SpanExcluding(application.wheels.version, " ")) || application.wheels.loadIncompatiblePlugins)
  767. {
  768. application.wheels.plugins[loc.pluginName] = loc.plugin;
  769. if (StructKeyExists(loc.plugin, "version") && !ListFind(loc.plugin.version, SpanExcluding(application.wheels.version, " ")))
  770. application.wheels.incompatiblePlugins = ListAppend(application.wheels.incompatiblePlugins, loc.pluginName);
  771. }
  772. }
  773. }
  774. // store plugin injection information in application scope so we don't have to run this code on each injection
  775. loc.iEnd = ListLen(application.wheels.mixableComponents);
  776. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  777. {
  778. application.wheels.mixins[ListGetAt(application.wheels.mixableComponents, loc.i)] = {};
  779. }
  780. loc.iList = StructKeyList(application.wheels.plugins);
  781. loc.iEnd = ListLen(loc.iList);
  782. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  783. {
  784. loc.iItem = ListGetAt(loc.iList, loc.i);
  785. loc.pluginMeta = GetMetaData(application.wheels.plugins[loc.iItem]); // grab meta data of the plugin
  786. if (!StructKeyExists(loc.pluginMeta, "environment") || ListFindNoCase(loc.pluginMeta.environment, application.wheels.environment))
  787. {
  788. loc.pluginMixins = "global"; // by default and for backwards compatibility, we inject all methods into all objects
  789. if (StructKeyExists(loc.pluginMeta, "mixin"))
  790. loc.pluginMixins = loc.pluginMeta["mixin"]; // if the component has a default mixin value, assign that value
  791. // loop through all plugin methods and enter injection info accordingly (based on the mixin value on the method or the default one set on the entire component)
  792. loc.jList = StructKeyList(application.wheels.plugins[loc.iItem]);
  793. loc.jEnd = ListLen(loc.jList);
  794. for (loc.j=1; loc.j <= loc.jEnd; loc.j++)
  795. {
  796. loc.jItem = ListGetAt(loc.jList, loc.j);
  797. if (IsCustomFunction(application.wheels.plugins[loc.iItem][loc.jItem]) && loc.jItem != "init")
  798. {
  799. loc.methodMeta = GetMetaData(application.wheels.plugins[loc.iItem][loc.jItem]);
  800. loc.methodMixins = loc.pluginMixins;
  801. if (StructKeyExists(loc.methodMeta, "mixin"))
  802. loc.methodMixins = loc.methodMeta["mixin"];
  803. // mixin all methods except those marked as none
  804. if (loc.methodMixins != "none")
  805. {
  806. loc.kEnd = ListLen(application.wheels.mixableComponents);
  807. for (loc.k=1; loc.k <= loc.kEnd; loc.k++)
  808. {
  809. loc.kItem = ListGetAt(application.wheels.mixableComponents, loc.k);
  810. if (loc.methodMixins == "global" || ListFindNoCase(loc.methodMixins, loc.kItem))
  811. application.wheels.mixins[loc.kItem][loc.jItem] = application.wheels.plugins[loc.iItem][loc.jItem];
  812. }
  813. }
  814. }
  815. }
  816. }
  817. }
  818. // look for plugins that are incompatible with each other
  819. loc.addedFunctions = "";
  820. for (loc.key in application.wheels.plugins)
  821. {
  822. for (loc.keyTwo in application.wheels.plugins[loc.key])
  823. {
  824. if (!ListFindNoCase("init,version,pluginVersion", loc.keyTwo))
  825. {
  826. if (ListFindNoCase(loc.addedFunctions, loc.keyTwo))
  827. $throw(type="Wheels.IncompatiblePlugin", message="#loc.key# is incompatible with a previously installed plugin.", extendedInfo="Make sure none of the plugins you have installed override the same Wheels functions.");
  828. else
  829. loc.addedFunctions = ListAppend(loc.addedFunctions, loc.keyTwo);
  830. }
  831. }
  832. }
  833. // look for plugins that depend on other plugins that are not installed
  834. for (loc.key in application.wheels.plugins)
  835. {
  836. loc.pluginInfo = GetMetaData(application.wheels.plugins[loc.key]);
  837. if (StructKeyExists(loc.pluginInfo, "dependency"))
  838. {
  839. loc.iEnd = ListLen(loc.pluginInfo.dependency);
  840. for (loc.i=1; loc.i <= loc.iEnd; loc.i++)
  841. {
  842. loc.iItem = ListGetAt(loc.pluginInfo.dependency, loc.i);
  843. if (!StructKeyExists(application.wheels.plugins, loc.iItem))
  844. application.wheels.dependantPlugins = ListAppend(application.wheels.dependantPlugins, Reverse(SpanExcluding(Reverse(loc.pluginInfo.name), ".")) & "|" & loc.iItem);
  845. }
  846. }
  847. }
  848. }
  849. </cfscript>
  850. </cffunction>
  851. <cffunction name="$checkMinimumVersion" access="public" returntype="boolean" output="false">
  852. <cfargument name="version" type="string" required="true">
  853. <cfargument name="minversion" type="string" required="true">
  854. <cfscript>
  855. var loc = {};
  856. // remove periods and commas from the version and minimum version
  857. arguments.version = ListChangeDelims(arguments.version, "", ".,");
  858. arguments.minversion = ListChangeDelims(arguments.minversion, "", ".,");
  859. // make version and minversion the same length pad zeros to the end
  860. loc.a = max(len(arguments.version), len(arguments.minversion));
  861. arguments.version = arguments.version & RepeatString("0", loc.a - len(arguments.version));
  862. arguments.minversion = arguments.minversion & RepeatString("0", loc.a - len(arguments.minversion));
  863. // make sure the version is an integer
  864. if (IsNumeric(arguments.version) && IsNumeric(arguments.minversion) && arguments.version >= arguments.minversion)
  865. {
  866. return true;
  867. }
  868. return false;
  869. </cfscript>
  870. <cfreturn >
  871. </cffunction>