ASP Classic blog framework - BrainOrdure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 line
7.3KB

  1. <!--#include file="../app/models/autoload_models.asp" -->
  2. <!--#include file="../app/repositories/autoload_repositories.asp" -->
  3. <!--#include file="../app/Controllers/autoload_controllers.asp" -->
  4. <%
  5. ' Set cache expiration from configuration
  6. Dim cacheYear : cacheYear = GetAppSetting("CacheExpirationYear")
  7. If cacheYear = "nothing" Then cacheYear = "2030"
  8. Response.ExpiresAbsolute = "01/01/" & cacheYear
  9. Response.AddHeader "pragma", "no-cache"
  10. Response.AddHeader "cache-control", "private, no-cache, must-revalidate"
  11. '=======================================================================================================================
  12. ' MVC Dispatcher
  13. '=======================================================================================================================
  14. Class MVC_Dispatcher_Class
  15. dim CurrentController
  16. Public Property Get ControllerName
  17. ControllerName = CurrentController
  18. end Property
  19. '---------------------------------------------------------------------------------------------------------------------
  20. ' Convenience method to resolve route and dispatch in one call
  21. ' method: HTTP method (GET, POST, etc.)
  22. ' path: Request path (already cleaned of query params)
  23. '---------------------------------------------------------------------------------------------------------------------
  24. Public Sub DispatchRequest(method, path)
  25. Dim routeArray
  26. routeArray = router.Resolve(method, path)
  27. Dispatch routeArray
  28. End Sub
  29. '---------------------------------------------------------------------------------------------------------------------
  30. ' Main dispatch method - executes a resolved route
  31. ' RouteArray: Array(controller, action, params) from router.Resolve()
  32. '---------------------------------------------------------------------------------------------------------------------
  33. Public Sub Dispatch(RouteArray)
  34. On Error Resume Next
  35. Dim controllerName, actionName, hasParams, paramsArray
  36. controllerName = RouteArray(0)
  37. actionName = RouteArray(1)
  38. ' Security: Validate controller and action names
  39. If Not ControllerRegistry.IsValidControllerFormat(controllerName) Then
  40. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  41. Response.Write "<strong>Security Error:</strong> Invalid controller name format."
  42. Response.Write "</div>"
  43. Exit Sub
  44. End If
  45. If Not ControllerRegistry.IsValidActionFormat(actionName) Then
  46. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  47. Response.Write "<strong>Security Error:</strong> Invalid action name format."
  48. Response.Write "</div>"
  49. Exit Sub
  50. End If
  51. ' Security: Check controller whitelist
  52. If Not ControllerRegistry.IsValidController(controllerName) Then
  53. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  54. Response.Write "<strong>Security Error:</strong> Controller '" & Server.HTMLEncode(controllerName) & "' is not registered."
  55. Response.Write "</div>"
  56. Exit Sub
  57. End If
  58. ' Initialize current controller
  59. Dim controllerAssignment : controllerAssignment = "Set CurrentController = " & controllerName & "()"
  60. Execute controllerAssignment
  61. ' Check if layout should be used
  62. hasParams = (UBound(RouteArray) >= 2)
  63. If eval(controllerName & ".useLayout") Then
  64. %> <!-- #include file="../app/views/Shared/Header.asp" --> <%
  65. End If
  66. ' Prepare parameters
  67. If hasParams Then
  68. paramsArray = SurroundStringInArray(RouteArray(2))
  69. Else
  70. paramsArray = Empty
  71. End If
  72. ' Execute controller action
  73. ExecuteControllerAction controllerName, actionName, paramsArray
  74. ' Include footer if layout is used
  75. If eval(controllerName & ".useLayout") Then
  76. %> <!-- #include file="../app/views/Shared/Footer.asp" --> <%
  77. End If
  78. On Error GoTo 0
  79. End Sub
  80. ' Helper method to execute controller actions (eliminates code duplication)
  81. Private Sub ExecuteControllerAction(controllerName, actionName, paramsArray)
  82. On Error Resume Next
  83. Dim callString
  84. ' Build the call string based on whether we have parameters
  85. If Not IsEmpty(paramsArray) And IsArray(paramsArray) And UBound(paramsArray) >= 0 Then
  86. callString = "Call " & controllerName & "." & actionName & "(" & Join(paramsArray, ",") & ")"
  87. Else
  88. callString = "Call " & controllerName & "." & actionName & "()"
  89. End If
  90. ' Execute the action
  91. Execute callString
  92. ' Handle errors
  93. If Err.Number <> 0 Then
  94. HandleDispatchError actionName, Err.Description, Err.Number
  95. Err.Clear
  96. End If
  97. On Error GoTo 0
  98. End Sub
  99. ' Centralized error handling for dispatch errors
  100. Private Sub HandleDispatchError(actionName, errorDesc, errorNum)
  101. Dim isDevelopment
  102. isDevelopment = (LCase(GetAppSetting("Environment")) = "development")
  103. If isDevelopment Then
  104. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  105. Response.Write "<strong>Controller Action Error</strong><br>"
  106. Response.Write "Action: <code>" & Server.HTMLEncode(actionName) & "</code><br>"
  107. Response.Write "Error: " & Server.HTMLEncode(errorDesc) & "<br>"
  108. Response.Write "Error Number: " & errorNum
  109. Response.Write "</div>"
  110. Else
  111. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  112. Response.Write "<strong>An error occurred</strong><br>"
  113. Response.Write "Please contact the system administrator if the problem persists."
  114. Response.Write "</div>"
  115. End If
  116. End Sub
  117. Public Sub RequirePost
  118. If Request.Form.Count = 0 Then MVC.RedirectToExt "NotValid","",empty:End If
  119. End Sub
  120. ' Shortcut for RedirectToActionExt that does not require passing a parameters argument.
  121. Public Sub RedirectToAction(ByVal action_name)
  122. RedirectToActionExt action_name, empty
  123. End Sub
  124. Public Sub RedirectTo(controller_name, action_name)
  125. RedirectToExt controller_name, action_name, empty
  126. End Sub
  127. ' Redirects the browser to the specified action on the specified controller with the specified querystring parameters.
  128. ' params is a KVArray of querystring parameters.
  129. Public Sub RedirectToExt(controller_name, action_name, params)
  130. Response.Redirect Routes.UrlTo(controller_name, action_name, params)
  131. End Sub
  132. Public Sub RedirectToActionExt(ByVal action_name, ByVal params)
  133. RedirectToExt ControllerName, action_name, params
  134. End Sub
  135. End Class
  136. dim MVC_Dispatcher_Class__Singleton
  137. Function MVC()
  138. if IsEmpty(MVC_Dispatcher_Class__Singleton) then
  139. set MVC_Dispatcher_Class__Singleton = new MVC_Dispatcher_Class
  140. end if
  141. set MVC = MVC_Dispatcher_Class__Singleton
  142. End Function
  143. %>

Powered by TurnKey Linux.