Files
payg/Application.cfc
T

365 lines
12 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<cfcomponent
displayname="Application"
output="true"
hint="Handle the application.">
<!--- Set up the application. --->
<cfset this.Name = "PAYG" />
<cfset this.applicationTimeout = createTimeSpan( 0, 1, 0, 0 ) />
<cfset this.sessionmanagement="Yes"/>
<cfset this.clientmanagement="No"/>
<cfset this.sessiontimeout=CreateTimeSpan(0, 0, 120, 0)/>
<cfset this.setclientcookies="No"/>
<cfset this.mappings = structNew() />
<cfset this.mappings["/mod"] = getDirectoryFromPath(getCurrentTemplatePath()) & "mod/" />
<cfset this.customTagPaths = expandPath(getDirectoryFromPath(getCurrentTemplatePath())&'mod')/>
<cfset this.datasource = "dwh"/>
<cfset this.defaultdatasource = this.datasource/>
<cfset request.DS = "#this.datasource#">
<!--- кажется, нужно инициализировать датасорцы в псевдоконструкторе - onRequest не получается --->
<cfset this.datasources["#this.datasource#"]=getDS("#this.datasource#","ds_#this.datasource#")/>
<cfset this.datasources["billing-vc"]=getDS("billing-vc","ds_billing_vc")/>
<cfset this.datasources["billing-s3"]=getDS("billing-s3","ds_billing_s3")/>
<!--- но датасорцы таинственно прочухались с прошлым пушем, без переноса инициализации в псевдоконструктор --->
<!--- а потом опять... --->
<cfset this.datasources["inmem"] = {
class: "org.hsqldb.jdbcDriver",
// Внимание наjdbc:hsqldb:mem:
connectionString: "jdbc:hsqldb:mem:lucee_mem;shutdown=false",
username: "sa",
password: "",
// Защита от "висяка" и исчезновения базы:
// заставляем Lucee держать минимум 1 коннект открытым всегда
connectionLimit: 5,
connectionMin: 1
}
/><!--- inmemory DB --->
<cfset COOKIEENCKEY = "PREVED!Y2"/>
<!--- Define the page request properties. --->
<cfsetting
requesttimeout="45"
showdebugoutput="false"
enablecfoutputonly="false"
/>
<cffunction
name="OnApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="Fires when the application is first created.">
<cfreturn true />
</cffunction>
<cffunction
name="OnRequest"
access="public"
returntype="void"
output="true"
hint="Fires after pre page processing is complete.">
<cfargument name="template" type="string" required="true"/>
<cfset request.startTickCount=getTickCount()/>
<cfset setEncoding("FORM", "UTF-8")>
<cfset setEncoding("URL", "UTF-8")>
<cfset request.PERMISSION_NONE=0/>
<cfset request.PERMISSION_READ=1/>
<cfset request.PERMISSION_WRITE=2/>
<!--- https://www.jenkins.io/doc/book/system-administration/authenticating-scripted-clients/
The API token is available in your personal configuration page. Click your name on the top right corner on every page, then click "Configure" to see your API token. (The URL $root/me/configure is a good shortcut.) You can also change your API token from here.
--->
<cfset request.UNDEFINED_USR_ID=-1/>
<!--- request.ROOT_USR_ID=1 not used --->
<cfset request.ANONYMOUS_USR_ID=2/>
<cfset request.GUEST_USR_ID=3/>
<!--- global settings --->
<cfset request.RECORDS_PER_PAGE=500/>
<cfset request.APP_VERSION="0.00.059"/>
<cfset request.STAND=getStand()/>
<cflock scope="application" type="readonly" timeout=3>
<cfset request.APP_NAME=this.Name/>
</cflock>
<!--- <cfdump var=#GetApplicationSettings()#/> --->
<!--- неуклюжий выверт для локальной отладки --->
<!--- <cfif !structKeyExists(GetApplicationSettings().datasources,"#this.datasource#")>
<cfset this.datasources["#this.datasource#"]=getDS("#this.datasource#","ds_#this.datasource#")/>
</cfif>
<cfif !structKeyExists(this.datasources,"billing-vc")>
<cfset this.datasources["billing-vc"]=getDS("billing-vc","ds_billing_vc")/>
</cfif>
<cfif !structKeyExists(this.datasources,"billing-s3")>
<cfset this.datasources["billing-s3"]=getDS("billing-s3","ds_billing_s3")/>
</cfif> --->
<!--- <cfset this.datasources["#this.datasource#"]=getDS("#this.datasource#","ds_#this.datasource#")/>
<cfset this.datasources["billing-vc"]=getDS("billing-vc","ds_billing_vc")/>
<cfset this.datasources["billing-s3"]=getDS("billing-s3","ds_billing_s3")/> --->
<!--- <cfdump var=#this.datasources#/> --->
<!--- application constants --->
<!---<cfset request.SMTP_SERVER="172.16.16.16"/>
<cfset request.MAIL_FROM_FOR_NOTIFICATION="Pipeline tracker <pipeline@mail.ru>"/>
<cfset request.SUBJECT_PREFIX="[PIPELINE TRACKER] "/>--->
<!---<cfset request.BASE_URL="https://smtest.dtln.local/pipeline/"/>*** сделать нормально. Внимание! При наличии прокси CGI.SERVER_NAME может быть заменено --->
<cfset local = {} />
<cfset local.basePath = getDirectoryFromPath(
getCurrentTemplatePath()
) />
<cfset local.targetPath = getDirectoryFromPath(
expandPath( arguments.template )
) />
<cfset local.requestDepth = (
listLen( local.targetPath, "\/" ) -
listLen( local.basePath, "\/" )
) />
<cfset request.webRoot = repeatString(
"../",
local.requestDepth
) />
<!---
While we wouldn't normally do this for every page
request (it would normally be cached in the
application initialization), I'm going to calculate
the site URL based on the web root.
--->
<cfset request.siteUrl = (
IIF(
(CGI.server_port_secure <!--- CGI.https EQ "On" does not work with apache+tomcat and nginx+tomcat --->),
DE( "https://" ),
DE( "http://" )
) &
cgi.http_host &
reReplace(
getDirectoryFromPath( arguments.template ), "([^\\/]+[\\/]){#local.requestDepth#}$",
"",
"one"
)
) />
<cfset request.thisPage=Replace(ReplaceNoCase(expandPath(ARGUMENTS.template), local.basePath, ""), "\", "/")/>
<cfinclude template="inc/functions.cfm"/>
<cfcookie name="CFID" value="#session.CFID#">
<cfcookie name="CFTOKEN" value="#session.CFTOKEN#">
<!--- check saved login --->
<!---
если в сессии есть usr_id, ничего делать не нужно
если его нет (то есть сессия новая или свежесброшенная),
- проверить куки.
Дополнительное условие: если происходит процесс установки-сброса кук,
(в процессе установки-сброса определена переменная session.save_login) - не вмешиваемся
--->
<!--- <cfset request.usr_id=#request.UNDEFINED_USR_ID#/> --->
<cflock scope="session" type="exclusive" timeout="3">
<!---<cfset session.usr_id=1/>***--->
<cfif NOT structKeyExists(session,"usr_id") OR (structKeyExists(session,"usr_id") AND NOT isNumeric(session.usr_id))>
<cfset session.usr_id=#request.UNDEFINED_USR_ID#>
</cfif>
<cfif session.usr_id EQ request.UNDEFINED_USR_ID>
<cfif NOT structKeyExists(session, "save_login")>
<cfif structKeyExists(cookie, "portalUser")>
<cftry>
<cfset login = Decrypt(cookie.portalUser,COOKIEENCKEY)>
<cfoutput>#login#</cfoutput>
<cfquery name="qGetCredentials" datasource="#request.DS#">
select usr_id from usr where login='#login#'
</cfquery>
<cfset session.usr_id = qGetCredentials.usr_id>
<m:settings action="create"/>
<cfcatch type="Any">
<cfoutput>#cfcatch.message#</cfoutput>
<cfset session.usr_id = request.UNDEFINED_USR_ID>
</cfcatch>
</cftry>
</cfif>
</cfif>
</cfif>
<!--- <cfdump var=#request.usr_id#/> --->
<cfif session.usr_id EQ request.UNDEFINED_USR_ID>
<cfset session.usr_id = request.ANONYMOUS_USR_ID>
</cfif>
<cfset request.usr_id=session.usr_id/>
<!--- <cfdump var=#request.usr_id#/> --->
<cfif structKeyExists(session, "username")>
<cfset request.username=#session.username#/>
<cfelse>
<cfset request.username=""/>
</cfif>
<cfparam name="language" default=""/>
<cfif language EQ "ru" OR language EQ "en">
<cfset session.language=language/>
<cfelse>
<cfparam name="session.language" default="ru"/>
</cfif>
<cfset request.language=session.language/>
</cflock><!--- session exclusive --->
<cfset request.usr_is_admin=0/>
<!--- <cfparam name="URL.token" default=""/>
<cfif URL.token EQ '9b15d2ab-f044-49dc-88cb-3f6301a55e42'>
<cfset request.usr_id=5/>
</cfif> --->
<!--- ----------------------- --->
<!--- --------------------------- --->
<cfif request.usr_id GT 0 > <!---AND request.usr_id NEQ request.ANONYMOUS_USR_ID в данном случае закрыли анонимный доступ --->
<cfinclude template="#ARGUMENTS.template#"/>
<cfelse>
<cfset currentDir = (
lCase(
replace(
getDirectoryFromPath(
replaceNoCase(
expandPath(
ARGUMENTS.template
),
getDirectoryFromPath(
getCurrentTemplatePath()
),
""
)
), "\", "/", "ALL"
)
)
)/>
<cfif currentDir EQ "saml/"><!--- note traling slash --->
<cfinclude template="#ARGUMENTS.template#"/>
<cfelse>
<cfinclude template="login.cfm" />
</cfif>
</cfif>
<cfreturn />
</cffunction>
<cffunction
name="OnRequestEnd"
access="public"
returntype="void"
output="true"
hint="Fires after the page processing is complete.">
<!--- Attention! Before CF9, OnrequestEnd is not executed in case of redirect. That is why we use session.save_login --->
<cfif structKeyExists(session, "save_login")>
<cfif session.save_login EQ "">
<!--- unset (expire) cookie --->
<cfcookie expires="-1" name="portalUser" value="">
<cfelse>
<!--- set persistent cookie with no expiration --->
<cfcookie expires="NEVER" name="portalUser" value="#encrypt(session.save_login,COOKIEENCKEY)#">
</cfif>
<cfset structDelete(session, "save_login")>
</cfif>
<cfreturn />
</cffunction>
<cffunction
name="getStand"
access="private"
returntype="string"
output="true"><!--- *** duplicated in svc-api --->
<cftry>
<cfquery name=qConfig>
select value as stand from config
where name='STAND'
</cfquery>
<cfreturn qConfig.stand/>
<cfcatch type="ANY">
<!--- do nothing, default will be returned --->
</cfcatch>
</cftry>
<cfreturn ""/>
</cffunction>
<cffunction
name="getDS"
access="private"
returntype="struct"
output="true"
hint="Configure data source from environment variables. Convention: data source name is an environment varialble prefix">
<cfargument name="dsname" type="string" required="true"/>
<cfargument name="prefix" type="string" default=#dsname#/>
<cfset system = createObject("java", "java.lang.System")/>
<cfset var ds={}/>
<cfloop list="class,connectionString,database,driver,dbdriver,host,port,type,url,username,password,bundleName,bundleVersion,connectionLimit,liveTimeout,validate" item="field"><!--- driver vs dbdriver --->
<cfset var value=system.getEnv("#arguments.prefix#_#field#")/>
<cfif isDefined("value") AND len(value)>
<cfset structInsert(ds,field,value)/>
</cfif>
</cfloop>
<cfif structIsEmpty(ds)>
<cfthrow type="application" message="Datasource not configured" detail="Datasource not defined in the environment. Expected prefix is #arguments.prefix#"/>
</cfif>
<!--- test datasource (just to get exception if invalid) --->
<cftry>
<cfquery name="qTestDs" datasource=#ds#>
select 1;
</cfquery>
<cfcatch type="any">
<!--- <cfdump var=#ds#/><cfabort/> --->
<cfrethrow/>
</cfcatch>
</cftry>
<cfreturn ds/>
</cffunction>
</cfcomponent>