initial commit

This commit is contained in:
Siwat Sirichai 2025-06-08 16:22:20 +07:00
commit 252dac3143
1516 changed files with 694271 additions and 0 deletions

View file

@ -0,0 +1,379 @@
Imports Microsoft.VisualBasic
Imports System.IO
Imports BaseClasses.Data
Imports Ciloci.Flee
Imports BaseClasses.Utils
Namespace Persons.Data
''' <summary>
''' The BaseFormulaEvaluator class evaluates a formula passed to the Evaluate function.
''' </summary>
''' <remarks></remarks>
Public Class BaseFormulaEvaluator
''' <summary>
''' Evaluator class that actually evaluates the formula.
''' This is available as a public property, so additional options
''' can be added to the evaluator from the calling functions.
''' </summary>
Protected _evaluator As ExpressionContext
Public ReadOnly Property Evaluator() As ExpressionContext
Get
Return _evaluator
End Get
End Property
''' <summary>
''' The Variables collection allows the passing of the variables to the Evaluator
''' </summary>
Public ReadOnly Property Variables() As Ciloci.Flee.VariableCollection
Get
Return Evaluator.Variables
End Get
End Property
''' <summary>
''' DataSource object from which each of the variables are
''' determined. This allows direct referencing of the
''' fields in the DataSource. For example, if the DataSource
''' is an Order_Details record, then the formula can use something like:
''' = UnitPrice * Quantity * (1 - Discount)
''' to calculate the Extended Price.
''' </summary>
Private _dataSource As BaseRecord = Nothing
Public Property DataSource() As BaseRecord
Get
Return _dataSource
End Get
Set(ByVal value As BaseRecord)
_dataSource = value
End Set
End Property
''' <summary>
''' Create a new evaluator and prepare for evaluation.
''' </summary>
Public Sub New(Optional ByVal mainObj As Object = Nothing)
' pass mainObj to contructor so that formula like = mainObj.<functionName> can be evaluated
If mainObj Is Nothing Then
_evaluator = New ExpressionContext()
Else
_evaluator = New ExpressionContext(mainObj)
End If
' The order of adding types is important. First we add our own
' formula functions, followed by the generic types.
Evaluator.Imports.AddType(GetType(BaseFormulaUtils))
' ADVANCED. For advanced usage, generic types can also be imported into
' the formula evaluator. This is done by adding types of some generic types
' such as Math, DateTime, Convert, and String. For example, if you add the
' Convert type, you can then use Convert.ToDecimal("string"). The second
' parameter to the AddType is the namespace that will be used in the formula.
' These functions expect a certain type. For example, Math functions expect
' a Double for the most part. If you pass a string, they will throw an exception.
' As such, we have written separate functions in FormulaUtils that are more
' loosely-typed than the standard libraries available here.
' Examples:
Evaluator.Imports.AddType(GetType(Math), "Math")
Evaluator.Imports.AddType(GetType(DateTime), "DateTime")
Evaluator.Imports.AddType(GetType(Convert), "Convert")
Evaluator.Imports.AddType(GetType(String), "String")
' We want a loosely-typed evaluation language - so do not
' type-check any variables.
Evaluator.Options.Checked = False
' Our policy is to always treat real numbers as Decimal instead of
' Double or Single to make it consistent across the entire
' evaluator.
Evaluator.Options.RealLiteralDataType = RealLiteralDataType.Decimal
' The variable event handler handles the variables based on the DataSource.
AddHandler _evaluator.Variables.ResolveVariableType, AddressOf variables_ResolveVariableType
AddHandler _evaluator.Variables.ResolveVariableValue, AddressOf variables_ResolveVariableValue
End Sub
''' <summary>
''' Evaluate the expression passed in as the string
''' </summary>
''' <param name="expression">The input whose absolute value is to be found.</param>
''' <returns>The result of the evaluation. Can we be any data type including string, datetime, decimal, etc.</returns>
Public Overridable Function Evaluate(ByVal expression As String) As Object
If IsNothing(expression) Then Return Nothing
If expression = "" Then Return ""
' Strip of the = in the front of the forumula - the Expression evaluator
' does not need it. Also, make sure to trim the result to remove any
' spaces in the front and the back.
expression = expression.TrimStart(New Char() {"="c, " "c}).Trim()
' If there are any exceptions when parsing or evaluating, they are
' thrown so that the end user can see the error. As such, there is no
' Try-Catch block here.
Try
Dim eDynamic As IDynamicExpression = _evaluator.CompileDynamic(expression)
Return eDynamic.Evaluate()
Catch ex As Exception
Return "ERROR: " & ex.Message
End Try
End Function
''' <summary>
''' Return the type of the given variable if it exists in the data source.
''' If the Return Type is Nothing, the evaluator assumes this is an invalid
''' variable and tries other methods to get its value.
''' </summary>
''' <param name="sender">The sender that sent this event.</param>
''' <param name="e">The event argument. Set e.VariableType.</param>
Protected Sub variables_ResolveVariableType(ByVal sender As Object, ByVal e As ResolveVariableTypeEventArgs)
Dim col As BaseColumn
' Returning Nothing indicates that we do not recognize this variable.
e.VariableType = Nothing
' If no DataSource was set, we do not have variables that we can use
' directly.
If IsNothing(DataSource) Then Return
Try
' Find a column in the datasource using a variable name.
col = DataSource.TableAccess.TableDefinition.ColumnList.GetByCodeName(e.VariableName)
If IsNothing(col) Then
' if the variable name ended with "DefaultValue", remmove it and then try to get the column name again.
If BaseClasses.Utils.InvariantLCase(e.VariableName).EndsWith("defaultvalue") Then
col = DataSource.TableAccess.TableDefinition.ColumnList.GetByCodeName(e.VariableName.Substring(0, e.VariableName.Length - 12))
End If
End If
If IsNothing(col) Then
Return
End If
Select Case col.ColumnType
Case BaseColumn.ColumnTypes.Number, _
BaseColumn.ColumnTypes.Percentage, _
BaseColumn.ColumnTypes.Star
' By default, all our internal data types use Decimal.
' This may result in problems when using the Math library
' but it reduces the problem by allowing us to loosely-type
' all data source properties.
e.VariableType = GetType(Decimal)
Case BaseColumn.ColumnTypes.Currency
' Convert currency into decimal to allow easier use in formulas
e.VariableType = GetType(Decimal)
Case BaseColumn.ColumnTypes.Boolean
' Boolean data types are maintained as Boolean and not
' converted to Integer as the Binary data type is.
e.VariableType = GetType(Boolean)
Case BaseColumn.ColumnTypes.Credit_Card_Date, _
BaseColumn.ColumnTypes.Date
' Use DateTme even for Credit Card Date.
e.VariableType = GetType(DateTime)
Case BaseColumn.ColumnTypes.Country, _
BaseColumn.ColumnTypes.Credit_Card_Number, _
BaseColumn.ColumnTypes.Email, _
BaseColumn.ColumnTypes.Password, _
BaseColumn.ColumnTypes.String, _
BaseColumn.ColumnTypes.Unique_Identifier, _
BaseColumn.ColumnTypes.USA_Phone_Number, _
BaseColumn.ColumnTypes.USA_State, _
BaseColumn.ColumnTypes.USA_Zip_Code, _
BaseColumn.ColumnTypes.Very_Large_String, _
BaseColumn.ColumnTypes.Web_Url
' For the purpose of formula's, all of the above field types
' are treated as strings.
e.VariableType = GetType(String)
Case BaseColumn.ColumnTypes.Binary, _
BaseColumn.ColumnTypes.File, _
BaseColumn.ColumnTypes.Image
' For the purpose of formula's we ignore BLOB fields since they
' cannot be used in any calculations or string functions.
e.VariableType = Nothing
Case Else
' Unknown data type.
e.VariableType = Nothing
End Select
Catch ex As Exception
' Ignore the error in case we cannot find the variable or its type - simply say that
' the Variable Type is Nothing - implying that we do not recognize this variable.
End Try
End Sub
''' <summary>
''' Return the value of the given variable if it exists in the data source
''' </summary>
''' <param name="sender">The input whose absolute value is to be found.</param>
''' <param name="e">The input whose absolute value is to be found.</param>
Protected Sub variables_ResolveVariableValue(ByVal sender As Object, ByVal e As ResolveVariableValueEventArgs)
Dim col As BaseColumn
' Default value is Nothing
e.VariableValue = Nothing
' If no DataSource was set, we do not have variables that we can use
' directly. We should not get here since the request for Type should have
' caught this.
If IsNothing(DataSource) Then Return
Try
' Find a column in the datasource using a variable name.
col = DataSource.TableAccess.TableDefinition.ColumnList.GetByCodeName(e.VariableName)
If IsNothing(col) Then
' if the variable name ended with "DefaultValue", remmove it and then try to get the column name again.
If BaseClasses.Utils.InvariantLCase(e.VariableName).EndsWith("defaultvalue") Then
col = DataSource.TableAccess.TableDefinition.ColumnList.GetByCodeName(e.VariableName.Substring(0, e.VariableName.Length - 12))
Select Case col.ColumnType
Case BaseColumn.ColumnTypes.Number, _
BaseColumn.ColumnTypes.Percentage, _
BaseColumn.ColumnTypes.Star
' The Number and Percentage values are saved as Single. So we first
' retrieve the Single value and then convert to Decimal. Our policy is
' always to return Decimal (never to return Single or Double) to be constent
' and avoid type conversion in the evaluator.
e.VariableValue = BaseFormulaUtils.ParseDecimal(col.DefaultValue)
Case BaseColumn.ColumnTypes.Currency
e.VariableValue = Me.DataSource.GetValue(col).ToDecimal
Case BaseColumn.ColumnTypes.Boolean
e.VariableValue = col.DefaultValue
Case BaseColumn.ColumnTypes.Credit_Card_Date, _
BaseColumn.ColumnTypes.Date
e.VariableValue = BaseFormulaUtils.ParseDate(col.DefaultValue)
Case BaseColumn.ColumnTypes.Country, _
BaseColumn.ColumnTypes.Credit_Card_Number, _
BaseColumn.ColumnTypes.Email, _
BaseColumn.ColumnTypes.Password, _
BaseColumn.ColumnTypes.String, _
BaseColumn.ColumnTypes.Unique_Identifier, _
BaseColumn.ColumnTypes.USA_Phone_Number, _
BaseColumn.ColumnTypes.USA_State, _
BaseColumn.ColumnTypes.USA_Zip_Code, _
BaseColumn.ColumnTypes.Very_Large_String, _
BaseColumn.ColumnTypes.Web_Url
e.VariableValue = col.DefaultValue
Case BaseColumn.ColumnTypes.File, _
BaseColumn.ColumnTypes.Image
' Can't do anything here.
e.VariableValue = Nothing
Case Else
e.VariableValue = Nothing
End Select
End If
Else
Select Case col.ColumnType
Case BaseColumn.ColumnTypes.Number, _
BaseColumn.ColumnTypes.Percentage, _
BaseColumn.ColumnTypes.Star
' The Number and Percentage values are saved as Single. So we first
' retrieve the Single value and then convert to Decimal. Our policy is
' always to return Decimal (never to return Single or Double) to be constent
' and avoid type conversion in the evaluator.
e.VariableValue = Decimal.Parse(Me.DataSource.GetValue(col).ToDouble().ToString())
Case BaseColumn.ColumnTypes.Currency
e.VariableValue = Me.DataSource.GetValue(col).ToDecimal
Case BaseColumn.ColumnTypes.Boolean
e.VariableValue = Me.DataSource.GetValue(col).ToBoolean
Case BaseColumn.ColumnTypes.Credit_Card_Date, _
BaseColumn.ColumnTypes.Date
e.VariableValue = Me.DataSource.GetValue(col).ToDateTime
Case BaseColumn.ColumnTypes.Country, _
BaseColumn.ColumnTypes.Credit_Card_Number, _
BaseColumn.ColumnTypes.Email, _
BaseColumn.ColumnTypes.Password, _
BaseColumn.ColumnTypes.String, _
BaseColumn.ColumnTypes.Unique_Identifier, _
BaseColumn.ColumnTypes.USA_Phone_Number, _
BaseColumn.ColumnTypes.USA_State, _
BaseColumn.ColumnTypes.USA_Zip_Code, _
BaseColumn.ColumnTypes.Very_Large_String, _
BaseColumn.ColumnTypes.Web_Url
e.VariableValue = Me.DataSource.GetValue(col).ToString
Case BaseColumn.ColumnTypes.File, _
BaseColumn.ColumnTypes.Image
' Can't do anything here.
e.VariableValue = Nothing
Case Else
e.VariableValue = Nothing
End Select
End If
Catch ex As Exception
' Ignore the error in case we cannot find the variable or its type - simply say that
' the Variable Type is Nothing - implying that we do not recognize this variable.
End Try
End Sub
'This method returns true or false value stating whether to apply GlobalWhereClause to a particular page or not.
'Use this method to exclude pages from applying Global Where Clauses sepcified in Batch Meister Wizard.
Public Shared Function ShouldApplyGlobalWhereClause(ByVal globalWhereClauseFormula As String) As Boolean
If System.Web.HttpContext.Current Is Nothing Then
Return False
End If
'Comment out the following code if you want to apply GlobalWhereClause to SignIn and SignOut pages
If Not (BaseClasses.Configuration.ApplicationSettings.Current.AuthenticationType = BaseClasses.Configuration.SecurityConstants.None) Then
If Not BaseClasses.Configuration.ApplicationSettings.Current.SecurityDisabled Then
If globalWhereClauseFormula.ToLower().Contains("userid()") Or globalWhereClauseFormula.ToLower().Contains("username()") Or _
globalWhereClauseFormula.ToLower().Contains("roles()") Then
If (System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToLower().Contains(BaseClasses.Configuration.ApplicationSettings.Current.SignInPageUrl.ToString().ToLower())) Then
Return False
End If
If (System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToLower().Contains(BaseClasses.Configuration.ApplicationSettings.Current.SignedOutPageUrl.ToString().ToLower())) Then
Return False
End If
If (System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToLower().Contains(BaseClasses.Configuration.ApplicationSettings.Current.ForgotUserPageUrl.ToString().ToLower())) Then
Return False
End If
If (System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToLower().Contains(BaseClasses.Configuration.ApplicationSettings.Current.SendUserInfoEmailUrl.ToString().ToLower())) Then
Return False
End If
End If
End If
End If
Return True
End Function
End Class
End Namespace

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,280 @@
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Imports System.Web
Imports BaseClasses.Resources
'SymmCrypto is a wrapper of System.Security.Cryptography.SymmetricAlgorithm classes
'and simplifies the interface. It supports customized SymmetricAlgorithm as well.
'Original Code from Frank Fang
'Revised by Jerome Howard to remove Bad Data errors, create seperate CryptoIV and
'use the maximum legal keysize for each encryption algorithm
Namespace Persons.Data
Public Class Crypto
'256 Bit IV Key that is truncated when a smaller keys are required
Private bytIV() As Byte = _
{78, 90, 23, 7, 54, 109, 34, 231, 90, 66, 109, 185, 228, 143, 89, 77, 190, 89, 103, 148, 54, 4, 98, 67, 243, 162, 68, 201, 73, 59, 184, 52}
'Supported .Net intrinsic SymmetricAlgorithm classes.
Public Enum Providers
DES
RC2
Rijndael
End Enum
Private _CryptoService As SymmetricAlgorithm
Public Sub New()
' Use the default provider of DES.
Me.New(Crypto.Providers.DES)
End Sub
'Constructor for using an intrinsic .Net SymmetricAlgorithm class.
Public Sub New(ByVal NetSelected As Providers)
Select Case NetSelected
Case Providers.DES
_CryptoService = New DESCryptoServiceProvider
Case Providers.RC2
_CryptoService = New RC2CryptoServiceProvider
Case Providers.Rijndael
_CryptoService = New RijndaelManaged
End Select
End Sub
'Constructor for using a customized SymmetricAlgorithm class.
Public Sub New(ByVal ServiceProvider As SymmetricAlgorithm)
_CryptoService = ServiceProvider
End Sub
'Depending on the legal key size limitations of a specific CryptoService provider
'and length of the private key provided, padding the secret key with a character
'or triming it to meet the legal size of the algorithm.
Private Function GetLegalKey(ByVal Key As String) As Byte()
'key sizes are in bits
Dim sTemp As String
Dim dValue As Integer
If (_CryptoService.LegalKeySizes.Length > 0) Then
Dim maxSize As Integer = _CryptoService.LegalKeySizes(0).MaxSize
dValue = CInt(maxSize / 8)
If Key.Length * 8 > maxSize Then
sTemp = Key.Substring(0, dValue)
Else
Dim moreSize As Integer = _CryptoService.LegalKeySizes(0).MinSize
Do While (Key.Length * 8 > moreSize)
moreSize += _CryptoService.LegalKeySizes(0).SkipSize
Loop
sTemp = Key.PadRight(dValue, "X".Chars(0))
End If
Else
sTemp = Key
End If
'Ensure that the IV Block size is also correct for the specific CryptoService provider
If (_CryptoService.LegalBlockSizes.Length > 0) Then
Dim maxSize As Integer = _CryptoService.LegalBlockSizes(0).MaxSize
dValue = CInt(maxSize / 8)
ReDim Preserve bytIV(sTemp.Length - 1)
If sTemp.Length * 8 > maxSize Then
ReDim Preserve bytIV(dValue - 1)
End If
End If
'convert the secret key to byte array
Return ASCIIEncoding.ASCII.GetBytes(sTemp)
End Function
Public Overridable Function Encrypt(ByVal Source As String, Optional ByVal includeSession As Boolean = True) As String
If (Source Is Nothing) Then
Return ""
End If
Dim s As String = ""
Try
If includeSession Then
s = Me.Encrypt(Source, Me.GetCryptoKey(), False)
s = System.Web.HttpUtility.UrlEncode(s)
Else
s = Me.Encrypt(Source, Me.GetCryptoKeyWithoutSessionVariable(), False)
End If
Catch ex As Exception
' In case of error, just return the source itself without encryption.
s = Source
End Try
Return s
End Function
Public Function Encrypt(ByVal Source As String, ByVal Key As String, Optional ByVal Encoded As Boolean = True) As String
Return Me.Encrypt(Source, Key, System.Text.Encoding.ASCII, Encoded)
End Function
Public Function Encrypt(ByVal Source As String, ByVal Key As String, ByVal Encoding As System.Text.Encoding, Optional ByVal Encoded As Boolean = True) As String
If Source Is Nothing OrElse Source.Trim.Length = 0 Then
Return Source
End If
If Encoded Then
Source = System.Web.HttpUtility.UrlEncode(Source)
End If
Dim bytIn As Byte()
If Encoding.EncodingName = System.Text.Encoding.ASCII.EncodingName Then
bytIn = System.Text.ASCIIEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage).GetBytes(Source)
ElseIf Encoding.EncodingName = System.Text.Encoding.Unicode.EncodingName Then
bytIn = System.Text.UnicodeEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage).GetBytes(Source)
Else
bytIn = System.Text.ASCIIEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage).GetBytes(Source)
End If
'= System.Text.ASCIIEncoding.ASCII.GetBytes(Source)
Dim ms As MemoryStream = New MemoryStream
'set the keys
_CryptoService.Key = GetLegalKey(Key)
_CryptoService.IV = bytIV
'create an Encryptor from the Provider Service instance
Dim encrypto As ICryptoTransform = _CryptoService.CreateEncryptor()
'create Crypto Stream that transforms a stream using the encryption
Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Write)
'write out encrypted content into MemoryStream
cs.Write(bytIn, 0, bytIn.Length)
cs.FlushFinalBlock()
cs.Close()
Dim bytOut() As Byte = ms.ToArray()
ms.Close()
Return Convert.ToBase64String(bytOut) 'convert into Base64 so that the result can be used in xml
End Function
Public Overridable Function Decrypt(ByVal Source As String, Optional ByVal includeSession As Boolean = True) As String
Dim s As String = ""
Try
' First try decrypting as is.
If includeSession Then
s = Me.Decrypt(Source, Me.GetCryptoKey(), False)
s = System.Web.HttpUtility.UrlDecode(s)
Else
s = Me.Decrypt(Source, Me.GetCryptoKeyWithoutSessionVariable(), False)
End If
Catch ex As Exception
Try
' If the first try of decrypting does not work, then
' URL decode it and try again. This is to ensure that if
' the encrypted key is passed through the URL, it needs to
' be decoded first.
Source = System.Web.HttpUtility.UrlDecode(Source)
s = Me.Decrypt(Source, GetCryptoKey(), False)
Catch ex1 As Exception
' In case of error, throw new Exception: do not allow to access encrypted page with non-encrypted URL
Throw New System.UriFormatException(RU.GetErrMsg(RU.ErrRes.GetRecords))
End Try
End Try
If ((Source IsNot Nothing AndAlso Source.Trim() <> "") AndAlso (s Is Nothing OrElse s.Trim() = "")) Then
Throw New System.UriFormatException(RU.GetErrMsg(RU.ErrRes.GetRecords))
End If
Return s
End Function
Public Function Decrypt(ByVal Source As String, ByVal Key As String, Optional ByVal Encoded As Boolean = True) As String
Return Me.Decrypt(Source, Key, System.Text.Encoding.ASCII, Encoded)
End Function
Public Function Decrypt(ByVal Source As String, ByVal Key As String, ByVal Encoding As System.Text.Encoding, Optional ByVal Encoded As Boolean = True) As String
If Source Is Nothing OrElse Source.Trim.Length = 0 Then
Return Source
End If
'convert from Base64 to binary
Dim bytIn As Byte()
Dim ms As MemoryStream
Try
bytIn = System.Convert.FromBase64String(Source)
ms = New MemoryStream(bytIn)
Catch ex As Exception
bytIn = System.Convert.FromBase64String(Source.Replace(" ", "+"))
ms = New MemoryStream(bytIn)
End Try
Dim bytKey() As Byte = GetLegalKey(Key)
Dim bytTemp(bytIn.Length) As Byte
'set the private key
_CryptoService.Key = bytKey
_CryptoService.IV = bytIV
'create a Decryptor from the Provider Service instance
Dim encrypto As ICryptoTransform = _CryptoService.CreateDecryptor()
'create Crypto Stream that transforms a stream using the decryption
Dim cs As CryptoStream = New CryptoStream(ms, encrypto, CryptoStreamMode.Read)
Dim output As String = ""
Try
'read out the result from the Crypto Stream
Dim sr As StreamReader
If Encoding.EncodingName = System.Text.Encoding.ASCII.EncodingName Then
sr = New StreamReader(cs, System.Text.ASCIIEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage))
ElseIf Encoding.EncodingName = System.Text.Encoding.Unicode.EncodingName Then
sr = New StreamReader(cs, System.Text.UnicodeEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage))
Else
sr = New StreamReader(cs, System.Text.ASCIIEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage))
End If
output = sr.ReadToEnd
sr.Close()
ms.Close()
cs.Close()
Catch ex As Exception
bytIn = System.Convert.FromBase64String(Source.Replace(" ", "+"))
ms = New MemoryStream(bytIn)
bytKey = GetLegalKey(Key)
'set the private key
_CryptoService.Key = bytKey
_CryptoService.IV = bytIV
encrypto = _CryptoService.CreateDecryptor()
Try
'create Crypto Stream that transforms a stream using the decryption
cs = New CryptoStream(ms, encrypto, CryptoStreamMode.Read)
Dim sr As StreamReader
If Encoding.EncodingName = System.Text.Encoding.ASCII.EncodingName Then
sr = New StreamReader(cs, System.Text.ASCIIEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage))
ElseIf Encoding.EncodingName = System.Text.Encoding.Unicode.EncodingName Then
sr = New StreamReader(cs, System.Text.UnicodeEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage))
Else
sr = New StreamReader(cs, System.Text.ASCIIEncoding.GetEncoding(System.Globalization.CultureInfo.CurrentUICulture.TextInfo.ANSICodePage))
End If
output = sr.ReadToEnd
sr.Close()
ms.Close()
cs.Close()
Catch
End Try
End Try
If Encoded Then
output = System.Web.HttpUtility.UrlDecode(output)
End If
Return output
End Function
' The URLEncryptionKey is specified in the web.config. The rightmost six characters of the current
' Session Id are concatenated with the URLEncryptionKey to provide added protection. You can change
' this to anything you like by changing this function for the application.
' This function is private and not overridable because each page cannot have its own key - it must
' be common across the entire application.
Private Function GetCryptoKey() As String
Return Left(System.Web.HttpContext.Current.Session.SessionID, 6) & BaseClasses.Configuration.ApplicationSettings.Current.URLEncryptionKey & Right(System.Web.HttpContext.Current.Session.SessionID, 6)
End Function
Private Function GetCryptoKeyWithoutSessionVariable() As String
Return Left("ffx4ypamvvcs4knwbxxehl45", 6) & BaseClasses.Configuration.ApplicationSettings.Current.URLEncryptionKey & Right("ffx4ypamvvcs4knwbxxehl45", 6)
End Function
End Class
End Namespace

View file

@ -0,0 +1,80 @@
Imports BaseClasses
Imports BaseClasses.Data
Imports System.Text.RegularExpressions
''' <summary>
''' The DatabaseObjects class contains a set of functions that provide
''' access to the database by using table or field names. The class
''' allows conversion of names into the proper table object and
''' retrieval of values for field name.
''' </summary>
''' <remarks></remarks>
Public Class DatabaseObjects
Private Const ASSEMBLY_NAME As String = "App_Code"
Private Const BUSINESS_NAMESPACE As String = "Persons.Business"
''' <summary>
''' Returns the BaseTable object for the given table name.
''' Determines if this is a Table, View or Query - and then
''' calls GetType to retrieve and return the object.
''' </summary>
''' <param name="tableName">tableName whose object is desired</param>
''' <returns>A BaseTable object for the given table name.</returns>
Public Shared Function GetTableObject(ByVal tableName As String) As BaseTable
Dim expandedTableName As String = String.Empty
Dim TYPE_FORMAT As String = "{0}.{1}{2},{3}"
Dim rgx As Regex = new Regex("[^a-zA-Z0-9]")
tableName = rgx.Replace(tableName, "_")
' First see if it is a table.
Try
expandedTableName = String.Format(TYPE_FORMAT, BUSINESS_NAMESPACE, tableName, "Table", ASSEMBLY_NAME)
Type.GetType(expandedTableName, True, True)
Catch
' It was not really a table name - so reset and try again with a view or a query.
expandedTableName = String.Empty
End Try
' Check if it is a view.
If expandedTableName = String.Empty Then
Try
expandedTableName = String.Format(TYPE_FORMAT, BUSINESS_NAMESPACE, tableName, "View", ASSEMBLY_NAME)
Type.GetType(expandedTableName, True, True)
Catch
' It was not really a view name - so reset and try again with a query.
expandedTableName = String.Empty
End Try
End If
' Check if it is a query.
If expandedTableName = String.Empty Then
Try
expandedTableName = String.Format(TYPE_FORMAT, BUSINESS_NAMESPACE, tableName, "Query", ASSEMBLY_NAME)
Type.GetType(expandedTableName, True, True)
Catch
' Still no luck.
expandedTableName = String.Empty
End Try
End If
If expandedTableName <> String.Empty Then
' OK, looks like we found an object.
Try
Dim t As BaseTable = BaseTable.CreateInstance(expandedTableName)
Return t
Catch
' Ignore, fall through and return Nothing
End Try
End If
' Could not find a table.
Return Nothing
End Function
End Class