HEX
Server: Microsoft-IIS/10.0
System: Windows NT ITPWINWEBSVR22 10.0 build 20348 (Windows Server 2022) AMD64
User: www.conferencesearch.co.uk (0)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: D:/web/castironradiatorcentre/changes prev/bak 2019-12-05/global.asa
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Option Explicit
</SCRIPT>

<!--#include file="config.asp"-->

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
' ============
' Version 2.02 (27/12/14)
' ============
' (SS,14/09/07) New version
' (SS,21/11/07) The Session_OnEnd had stopped working after Windows 2003 SP2. Worked after hotfix.
' Moved some code into Session_AddBackToStock
' (SS,07/07/11) modified for product options and logging
' (SS,28/07/11) added ability to get the database source name from config.asp, also added Option Explicit
' improved database handled, opening and closing handled only in ExecSQL and GetSQLValueAsString
' logging and delete options from stock only done if schema allows
' schema version is logged and also added session count
' (SS,22/04/14) Added LogEnabled condition to AddLog to only enable when LogEnabled is True in config.asp.
' (SS,27/12/14) Added "if select check..." to Sub Session_EmptyBasket to minimise "DELETE" queries in binary log for replication

Sub Application_OnStart
  Application("ConnectionString") = "DSN=" & GetDatabaseSourceName & ";" ' (SS,28/7/11) now gets name from GetDatabaseSourceName function in config.asp
  Application("SchemaVersion") = GetSchemaVersionFromSettings
  AddLog "Application Start"
  AddLog "Schema Version: " & GetSchemaVersion
  Application("SessionCount") = 0
End Sub

Sub Application_OnEnd
  AddLog "Application End"
End Sub

Sub Session_OnStart
  ' (SS,28/7/11) increment the session count
  Application.Lock
  Application("SessionCount") = Application("SessionCount") + 1
  Application.UnLock

  AddLog "Session Start (" & Application("SessionCount") & ")"

  ' (SS,2/8/12) changed timeout from 180 to 120 minutes
  Session.Timeout = 120 ' time out after two hours of non-use

  ' Set the server locale to UK, mainly to prevent date problems (day and month being reversed)
  Session.LCID = 2057
  ' make sure basket is empty on session start
  Session_EmptyBasket

  ' clear the session values

  ' following are used for continue shopping and search log
  Session("Search") = ""
  Session("Page") = ""
  Session("Category") = ""
  Session("Subcategory") = ""

  ' following are check out values accepted from user
  Session("Title") = ""
  Session("FirstName") = ""
  Session("Surname") = ""
  Session("Address1") = ""
  Session("Address2") = ""
  Session("Town") = ""
  Session("County") = ""
  Session("Postcode") = ""
  Session("Country") = ""
  Session("Telephone") = ""
  Session("Email") = ""
  Session("EmailConfirm") = ""
  Session("PurchaseOrderNo") = ""
  Session("Message") = ""

  ' (SS,16/7/04) delivery address same as invoice set to true
  Session("DeliveryAddressSameAsInvoice") = True

  ' when order created this holds the order number used
  Session("OrderNoPlaced") = ""

  ' (SS,11/5/04) used to save selected categories in menu
  Session("MenuCategory") = ""
  Session("MenuSubcategory") = ""

  ' (SS,18/5/04) to allow password protection during development stage '
  ' used in conjunction with Application("DevelopmentPassword") '
  Session("Authenticated") = False

  ' (SS,20/5/04) following used to keep track of which special offers to show next '
  Session("SpecialOfferCount") = 0

  ' (SS,3/8/04) account holder settings
  Session("AccountID") = ""  ' this is blank if account ID not entered and authenticated '
  Session("ContractID") = ""

  ' (SS,30/9/04) added referrer feature '
  Session("ReferrerAdded") = False

End Sub

Sub Session_OnEnd
  ' (SS,28/7/11) decrement the session count
  Application.Lock
  Application("SessionCount") = Application("SessionCount") - 1
  Application.UnLock  

  ' add items in order taken from stock back into stock
  ' (SS,21/11/07) OrderNo passed as parameter, more reliable using Session("OrderNoPlaced") in subroutine doesn't always return correct value
  Session_AddBackToStock(Session("OrderNoPlaced"))

  ' empty the basket when session has timed out or ended some other way
  Session_EmptyBasket
  AddLog "Session End (" & Application("SessionCount") & ")"
End Sub

Sub Session_EmptyBasket
  AddLog "EmptyBasket 1" ' (SS,27/12/14) added 1

  ' (SS,27/12/14) added checking count of existing records before deleting to prevent delete queries form being added to binary log for replication 
  ' unnecessarily when there are no records. Noticed the two delete queries below were very common in the log
  ' this routine is also run when session starts, therefore every session results in up to 4 delete queries even nothing added to basket
  If CInt(GetSQLValueAsString("SELECT COUNT(*) FROM shoppingbaskets WHERE SessionID = '" & Session.SessionID & "'")) > 0 Then
    AddLog "EmptyBasket 2" ' (SS,27/12/14)
    
    ' delete the shopping basket
    ' (SS,6/7/11) modified to now also delete options
    If IsSchemaVersionAtLeast153 Then
      AddLog "EmptyBasket 3" ' (SS,27/12/14)
      ExecSQL "DELETE t1, t2 FROM shoppingbaskets t1, shopping_basket_options t2 WHERE t1.ItemID = t2.ItemID AND t1.SessionID = '" & Session.SessionID & "'"
    End If
    
    ' (SS,6/7/11) following necessary because above only deletes record from shoppingbaskets if at least one option record exists, it also works in schemas prior to 1.53
    ExecSQL "DELETE FROM shoppingbaskets WHERE SessionID = '" & Session.SessionID & "'"
  End If
End Sub

Sub Session_AddBackToStock(AOrderNo)

  AddLog "AddBackToStock 1"

  ' (SS,28/5/07) added following to mark order as cancelled and add items back into stock
	' if order was placed (but not paid for) then mark as cancelled and add items back into stock
  If AOrderNo <> "" Then
    AddLog "AddBackToStock 2"

    ' (S,7/7/11) if order is in the ORDER PLACED status, then cancel and put products and options back into stock
    If GetSQLValueAsString("SELECT Status FROM orders WHERE OrderNo = " & AOrderNo) = "ORDER PLACED" Then    
      
      AddLog "AddBackToStock 3"
      
      ExecSQL "UPDATE orders SET Status = 'CANCELLED' WHERE OrderNo = " & AOrderNo
    
      Dim LSQL
      
      AddLog "AddBackToStock 4"
      
      ' (SS,7/7/11) put products back in stock, taken from TakeProductsFromStock
      ' replaces previous version which couldn't handle more then one product record with same code or id
      LSQL = "UPDATE products, " &_
        "(SELECT ProductID, SUM(Qty) AS Qty FROM orderdetails" &_
        " WHERE OrderNo = " & AOrderNo &_
        " GROUP BY ProductID" &_
        ") AS t2" &_
        " SET NumInStock = NumInStock + Qty" &_
        " WHERE products.ProductID = t2.ProductID AND NumInStock IS NOT NULL"             
      ExecSQL LSQL
    
      AddLog "AddBackToStock 5"
      
      ' (SS,6/7/11) put options back into stock
      ' SQL taken from TakeProductOptionsFromStock
      If IsSchemaVersionAtLeast153 Then
        LSQL = "UPDATE product_option_values, " &_
          "(SELECT order_detail_options.ProductOptionValueID, SUM(orderdetails.Qty) AS Qty" &_
          " FROM order_detail_options, orderdetails" &_
          " WHERE orderdetails.OrderDetailID = order_detail_options.OrderDetailID" &_
          " AND order_detail_options.OrderNo = " & AOrderNo & " AND order_detail_options.ProductOptionValueID <> 0" &_
          " GROUP BY ProductOptionValueID " &_
          ") AS t2 " &_
          " SET NumInStock = NumInStock + Qty " &_
          " WHERE product_option_values.ProductOptionValueID = t2.ProductOptionValueID AND NumInStock IS NOT NULL"        
        ExecSQL LSQL
      End If
    End If
    
    AddLog "AddBackToStock 6" ' (SS,6/7/11) 
    
	End If
  AddLog "AddBackToStock 7"
End Sub

' (SS,20/11/07) used for debugging
' (SS,6/7/11) modified to log session id and order no
' (SS,22/04/14) Added LogEnabled condition 
Sub AddLog(AMessage)
  ' only add to log if schema contains the log_cancelled_sessions table it was added on 1.53
  ' (SS,22/4/14) added LogEnabled 
  If IsSchemaVersionAtLeast153 And LogEnabled Then
    ExecSQL "INSERT INTO log_cancelled_sessions SET SessionID = '" & Session.SessionID & "', OrderNo = '" & Session("OrderNoPlaced") & "', Message = '" & AMessage & "'"
  End If
End Sub

' (SS,28/7/11)
Function GetSchemaVersionFromSettings
  GetSchemaVersionFromSettings = CDbl(GetSQLValueAsString("SELECT FieldValue FROM settings WHERE GroupName = 'Application' AND FieldName = 'SchemaVersion'"))
End Function

' (SS,28/7/11) returns schema version saved in Application collection
Function GetSchemaVersion
  GetSchemaVersion = Application("SchemaVersion")
End Function

' (SS,28/7/11)
Function IsSchemaVersionAtLeast153
  IsSchemaVersionAtLeast153 = GetSchemaVersion >= 1.53 
End Function

' (SS,28/7/11) executes given SQL, also opens and closes the connection
Sub ExecSQL(ASQL)
  Dim LoConn
  Set LoConn = Server.CreateObject("ADODB.Connection")
  LoConn.ConnectionString = Application("ConnectionString")
  LoConn.Open
  LoConn.Execute(ASQL)
  LoConn.Close
  Set LoConn = Nothing
End Sub

' (SS,6/7/11) special version for use in global.asa, opens and closes the connection
Function GetSQLValueAsString(ASQL)
  Const adCmdText = &H0001
  Dim LnRecs, LoRS, LoConn
  
  Set LoConn = Server.CreateObject("ADODB.Connection")
  LoConn.ConnectionString = Application("ConnectionString")
  LoConn.Open  
  
  Set LoRS = LoConn.Execute(ASQL, LnRecs, adCmdText)
  If LoRS.Eof Then
    GetSQLValueAsString = ""
  Else
    GetSQLValueAsString = LoRS.Fields.Item(0) ' first field
  End If
  
  LoRS.Close
  Set LoRS = Nothing
  
  LoConn.Close
  Set LoConn = Nothing    
End Function

</SCRIPT>