Sorcerer's IsleCode QueryParam Scanner / files

  1<!--- qpscanner v0.8 | (c) Peter Boughton | License: GPLv3 | Website: https://www.sorcerersisle.com/software/qpscanner --->
  2<cfcomponent output=false >
  3
  4	<cffunction name="init" returntype="any" output=false access="public">
  5		<cfargument name="ConfigDirectory" type="String"/>
  6
  7		<cfset This.ConfigDirectory = Arguments.ConfigDirectory/>
  8
  9		<cfreturn This/>
 10	</cffunction>
 11
 12
 13	<cffunction name="read" returntype="any" output=false access="public">
 14		<cfargument name="ConfigId"  type="String" required_ />
 15		<cfargument name="Format"    type="String" default="default" />
 16		<cfargument name="Overrides" type="Struct" optional />
 17		<cfset var Setting = QueryNew("id,label,type,options,value,hint,status") />
 18		<cfset var Sections    = -1 />
 19		<cfset var SectionList = -1 />
 20		<cfset var CurSection  = -1 />
 21		<cfset var CurSetting  = -1 />
 22		<cfset var X           = -1 />
 23		<cfset var Result      = -1 />
 24		<cfset var RootConfigFile = This.ConfigDirectory&'/../config.ini' />
 25		<cfset var ThisConfigFile = lcase( REreplace( Arguments.ConfigId , '\W+' , '' , 'all' ) ) />
 26		<cfset ThisConfigFile = This.ConfigDirectory & '/#ThisConfigFile#.ini' />
 27
 28		<cfif FileExists( ThisConfigFile )>
 29
 30			<cfset Sections    = getProfileSections( RootConfigFile ) />
 31			<cfset SectionList = getProfileString( RootConfigFile ,'Config' , 'keys' ) />
 32
 33			<cfloop index="CurSection" list=#SectionList# >
 34				<cfset X = QueryAddRow(Setting)/>
 35				<cfset Setting['Id'][X] = CurSection />
 36				<cfloop index="CurSetting" list=#Sections[CurSection]# >
 37					<cfset Setting[CurSetting][X] = getProfileString( RootConfigFile , CurSection , CurSetting )/>
 38				</cfloop>
 39			</cfloop>
 40
 41			<cfloop query="Setting">
 42				<cfset Setting['Value'][CurrentRow] = getProfileString( ThisConfigFile , 'Settings' , Id )/>
 43			</cfloop>
 44
 45			<cfswitch expression=#Arguments.Format# >
 46				<cfcase value="Struct">
 47					<cfset Result = StructNew()/>
 48					<cfloop query="Setting">
 49						<cfset Result[Id] = Value />
 50					</cfloop>
 51
 52					<cfif StructKeyExists(Arguments,'Overrides')>
 53						<cfset overrideDefaults(Result,Arguments.Overrides) />
 54
 55						<cfset expandValues(Result) />
 56					</cfif>
 57
 58					<cfreturn Result />
 59				</cfcase>
 60				<cfdefaultcase>
 61					<cfreturn Setting />
 62				</cfdefaultcase>
 63			</cfswitch>
 64		<cfelse>
 65			<cfthrow
 66				message = "Invalid Value '#Arguments.ConfigId#' for Argument ConfigId."
 67				detail  = "Cannot find configuration file at '#ConfigFile#'."
 68				type    = "qpscanner.Settings.Read.InvalidArgument.ConfigId"
 69			/>
 70		</cfif>
 71	</cffunction>
 72
 73
 74	<cffunction name="overrideDefaults" returntype="void" output=false access="private">
 75		<cfargument name="ScanSettings" type="Struct" required_ />
 76		<cfargument name="Overrides"    type="Struct" required_ />
 77
 78		<cfloop item="local.Setting" collection=#Arguments.ScanSettings# >
 79			<cfif StructKeyExists( Arguments.Overrides , Setting )>
 80				<cfset Arguments.ScanSettings[Setting] = Arguments.Overrides[Setting] />
 81			</cfif>
 82		</cfloop>
 83	</cffunction>
 84
 85
 86	<cffunction name="expandValues" returntype="void" output=false access="private">
 87		<cfargument name="ScanSettings" type="Struct" required_ />
 88
 89		<cfif StructKeyExists(Arguments.ScanSettings,'RequestTimeout')
 90			AND NOT isNumeric(Arguments.ScanSettings.RequestTimeout)
 91			>
 92			<cfset Arguments.ScanSettings.RequestTimeout = -1 />
 93		</cfif>
 94
 95		<cfif Arguments.ScanSettings.StartingDir EQ 'auto'>
 96			<cfset Arguments.ScanSettings.StartingDir = findHomeDirectory() />
 97		</cfif>
 98
 99		<cfset Arguments.ScanSettings.StartingDir = normalizePath(Arguments.ScanSettings.StartingDir) />
100
101	</cffunction>
102
103
104	<cffunction name="findHomeDirectory" returntype="String" output=false access="public">
105		<cfset var CurDir = -1/>
106		<cfset var DirList = "{home-directory},/,." />
107
108		<cfloop index="CurDir" list=#DirList# >
109			<cfif DirectoryExists( expandPath(CurDir) )>
110				<cfreturn normalizePath( expandPath( CurDir ) ) />
111			</cfif>
112		</cfloop>
113	</cffunction>
114
115
116	<cffunction name="normalizePath" returntype="String" output=false access="private">
117		<cfargument name="Path" type="String" required_ />
118
119		<cfset var Result = Arguments.Path.replaceAll('[\\/]+','/') />
120
121		<cfif len(Result) AND Result.endsWith('/') AND NOT Result.endsWith(':/') >
122			<cfset Result = Left(Result,len(Result)-1) />
123		</cfif>
124
125		<cfreturn Result />
126	</cffunction>
127
128
129</cfcomponent>