/*
The contents of this file are subject to the Mozilla Public License Version 1.1
(the "License"); you may not use this file except in compliance with the
License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is comprised of the ADF directory
The Initial Developer of the Original Code is
PaperThin, Inc. Copyright (c) 2009-2019.
All Rights Reserved.
By downloading, modifying, distributing, using and/or accessing any files
in this directory, you agree to the terms and conditions of the applicable
end user license agreement.
*/
/* *************************************************************** */
/*
Author:
PaperThin, Inc.
Name:
scriptsService_2_0.cfc
Summary:
Scripts Service functions for the ADF Library
Version:
2.0
History:
2016-03-11 - GAC - Created
2017-08-24 - GAC - Updated checkResourceURL() and checkRemoteResourceURL() methods
- Added the missing component metadata properties
*/
component displayname="scriptsService_2_0" extends="scriptsService_1_1" hint="Scripts functions for the ADF Library" output="no"
{
property name="version" value="2_0_4";
property name="type" value="singleton";
property name="wikiTitle" value="ScriptsService_2_0";
/*
resourceAPI.save(id, name, category, earlyLoadSourceArray, lateLoadSourceArray, description, installInstructions, aliasList, redistributable);
sourceArray: [{LoadTagType, SourceURL}]}
LoadTagType: 1=StyleSheet 2=JavaScript
History:
2017-03-27 - GAC - Updated to better handle argument validation from the CMD API since the wrapper function adds some additional arguments.
*/
public numeric function registerResource
(
required string name,
required string category,
required array earlyLoadSourceArray,
required array lateLoadSourceArray,
required string description,
required string installInstructions,
string aliasList="",
boolean redistributable=0,
boolean updateExisting=0,
boolean silent=0
)
{
var resSpecs = "";
var action = "registered";
var resourceAPI = Server.CommonSpot.ObjectFactory.getObject("Resource");
var msg = "";
arguments.id = 0;
if (structKeyExists(Request.Site.CS_Resources.Resources_by_name, arguments.name))
{
resSpecs = Request.Site.CS_Resources.Resources_by_name[arguments.name];
if (resSpecs.name != arguments.name) // registered version is an alias, can't update it
{
msg = "Alias with this name already exists, skipped: #arguments.name#";
if ( !arguments.silent )
writeOutput(msg & "
");
return 0;
}
else if (arguments.updateExisting == 0)
{
msg = "Resource already exists, skipped: #arguments.name#";
if ( !arguments.silent )
writeOutput(msg & "
");
return 0;
}
else
{
arguments.id = resSpecs.id;
action = "updated";
}
}
arguments.earlyLoadSourceArray = getResourceArray(arguments.earlyLoadSourceArray);
arguments.lateLoadSourceArray = getResourceArray(arguments.lateLoadSourceArray);
msg = "Resource #action#: #arguments.name#";
if ( !arguments.silent )
writeOutput(msg & "
");
// Check Resource URLs
arguments.earlyLoadSourceArray = checkResourceURL(resourceArray=arguments.earlyLoadSourceArray, silent=arguments.silent);
arguments.lateLoadSourceArray = checkResourceURL(resourceArray=arguments.lateLoadSourceArray, silent=arguments.silent);
// returns NUMERIC ID on NEW/UPDATE
// - Updated to better handle argument validation from the CMD API
if ( ArrayLen(arguments.earlyLoadSourceArray) OR ArrayLen(arguments.lateLoadSourceArray) )
{
return resourceAPI.save(id=arguments.id,
name=arguments.name,
category=arguments.category,
earlyLoadSourceArray=arguments.earlyLoadSourceArray,
lateLoadSourceArray=arguments.lateLoadSourceArray,
description=arguments.description,
installInstructions=arguments.installInstructions,
aliasList=arguments.aliasList,
redistributable=arguments.redistributable
);
}
else
return 0;
}
private array function getResourceArray(resourceSpecsArray)
{
var arr = Request.TypeFactory.newObjectInstance("ResourceLoadStruct_Array");
var count = arrayLen(arguments.resourceSpecsArray);
var i = 0;
for (i = 1; i <= count; i++)
arrayAppend(arr, getResourceStruct(argumentCollection=arguments.resourceSpecsArray[i]));
return arr;
}
private struct function getResourceStruct(loadTagType, sourceURL, canCombine, canMinify)
{
var res = Request.TypeFactory.newObjectInstance("ResourceLoadStruct");
if (!structKeyExists(arguments, "canCombine"))
arguments.canCombine = (left(arguments.sourceURL, 4) == "http") ? 0 : 1;
if (!structKeyExists(arguments, "canMinify"))
arguments.canMinify =
(
!arguments.canCombine
|| findNoCase(".min", arguments.sourceURL)
|| findNoCase("_min", arguments.sourceURL)
|| findNoCase("-pack", arguments.sourceURL)
|| findNoCase(".pack", arguments.sourceURL)
) ? 0 : 1;
res.loadTagType = arguments.loadTagType;
res.sourceURL = arguments.sourceURL;
res.canCombine = arguments.canCombine;
res.canMinify = arguments.canMinify;
return res;
}
private array function checkResourceURL(required array resourceArray, boolean silent=0)
{
var retArray = ArrayNew(1);
var i = 1;
var msg = "";
var srcURL = ";
var srcPath = ";
//writedump(var=arguments.resourceArray,expand=false);
for ( i=1; i LTE ArrayLen(arguments.resourceArray); i=i+1 ) {
msg = "";
if ( StructKeyExists(arguments.resourceArray[i],"sourceURL") )
{
srcURL = arguments.resourceArray[i].sourceURL;
// Check Local
if ( left(srcURL,4) != "http" AND left(srcURL,2) != "//" )
{
srcPath = ExpandPath(ListFirst(srcURL,"?"));
// CEHCK the Local Resource Path
if ( !FileExists(srcPath) )
msg = "Error: #srcURL# does not exist!";
}
else
{
// CHECK the Remote Resource URL
if ( !checkRemoteResourceURL(srcURL) )
msg = "Error: The remote resource #srcURL# was not available. Please verify it is a valid URL!";
}
}
else
msg = "Error: Resource has no sourceURL!";
if ( !arguments.silent && LEN(TRIM(msg)) )
{
writeOutput(msg & "
");
writeOutput(" -- Skipping resource registration. Please verify resource exists!" & "
");
}
else
arrayAppend(retArray,arguments.resourceArray[i]);
} // resourceArray loop
return retArray;
}
private boolean function checkRemoteResourceURL(required string sourceURL)
{
var retVal = false;
var retResult = '';
var httpService = '';
var cfmlEngineType = server.coldfusion.productname;
var cfmlEngineMajorVer = ListFirst(SERVER.ColdFusion.ProductVersion,",");
if ( FindNoCase(cfmlEngineType,'ColdFusion Server') )
{
if ( cfmlEngineMajorVer GTE 11 )
{
cfhttp(method="GET", charset="utf-8", url="#arguments.sourceURL#", result="retResult");
}
else
{
httpService = new http(method = "GET", charset = "utf-8", url = "#arguments.sourceURL#");
retResult = httpService.send().getPrefix();
}
}
else
{
cfhttp(method="GET", charset="utf-8", url="#arguments.sourceURL#", result="retResult");
}
if ( StructKeyExists(retResult,"statuscode") AND Find("200",retResult.statuscode) )
retVal = true;
return retVal;
}
}