Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

536 lignes
17KB

  1. <%
  2. Function QuoteValue(val)
  3. if IsWrappedInSingleQuotes(val) then
  4. QuoteValue = val
  5. Exit Function
  6. end if
  7. Select Case VarType(val)
  8. Case vbString
  9. QuoteValue = "'" & Replace(val, "'", "''") & "'"
  10. Case vbDate
  11. if conn.Provider = "Microsoft.Jet.OLEDB.4.0" or conn.Provider = "Microsoft.ACE.OLEDB.12.0" then
  12. QuoteValue = "#" & FormatDateTime(val, 0) & "#"
  13. else
  14. ' SQL Server
  15. QuoteValue = "'" & FormatDateTime(val, 0) & "'"
  16. end if
  17. Case vbNull, vbEmpty
  18. QuoteValue = "Null"
  19. Case vbBoolean
  20. ' Return boolean values without quotes
  21. QuoteValue = "'" & CStr(val) & "'"
  22. Case Else
  23. If IsNumeric(val) Then
  24. QuoteValue = CLng(val)
  25. Else
  26. QuoteValue = CStr(val)
  27. End If
  28. End Select
  29. End Function
  30. Public Function GetAppSetting(key)
  31. Dim cacheKey, xml, nodes, node, i
  32. cacheKey = "AppSetting_" & key
  33. ' Check Application cache first for performance
  34. If Not IsEmpty(Application(cacheKey)) Then
  35. GetAppSetting = Application(cacheKey)
  36. Exit Function
  37. End If
  38. ' Load from web.config only if not cached
  39. Set xml = Server.CreateObject("Microsoft.XMLDOM")
  40. xml.Load(Server.MapPath("web.config"))
  41. Set nodes = xml.selectNodes("//appSettings/add")
  42. For i = 0 To nodes.Length - 1
  43. Set node = nodes.Item(i)
  44. If node.getAttribute("key") = key Then
  45. GetAppSetting = node.getAttribute("value")
  46. ' Cache the value for subsequent requests
  47. Application.Lock
  48. Application(cacheKey) = GetAppSetting
  49. Application.Unlock
  50. Exit Function
  51. End If
  52. Next
  53. GetAppSetting = "nothing"
  54. End Function
  55. Public Sub ShowServerVariables
  56. Dim varName, htmlTable
  57. htmlTable = "<table border='1' cellspacing='0' cellpadding='5'>"
  58. htmlTable = htmlTable & "<thead><tr><th>Variable Name</th><th>Value</th></tr></thead><tbody>"
  59. ' Loop through all server variables
  60. For Each varName In Request.ServerVariables
  61. htmlTable = htmlTable & "<tr>"
  62. htmlTable = htmlTable & "<td>" & Server.HTMLEncode(varName) & "</td>"
  63. htmlTable = htmlTable & "<td>" & Server.HTMLEncode(Request.ServerVariables(varName)) & "</td>"
  64. htmlTable = htmlTable & "</tr>"
  65. Next
  66. htmlTable = htmlTable & "</tbody></table>"
  67. ' Output the HTML table
  68. Response.Write(htmlTable)
  69. End Sub
  70. '------------------------------------------------------------------------------
  71. ' Utility: IIf Function for VBScript
  72. ' Usage: result = IIf(condition, trueValue, falseValue)
  73. '------------------------------------------------------------------------------
  74. Function IIf(condition, trueValue, falseValue)
  75. On Error Resume Next
  76. If CBool(condition) Then
  77. IIf = trueValue
  78. Else
  79. IIf = falseValue
  80. End If
  81. If Err.Number <> 0 Then
  82. ' Optional: handle or log error in conversion/evaluation
  83. Err.Clear
  84. End If
  85. On Error GoTo 0
  86. End Function
  87. '-----------------------------
  88. ' Utility: Generic Error Reporter
  89. '-----------------------------
  90. Public Sub ErrorCheck(context)
  91. If Err.Number <> 0 Then
  92. Dim errHtml
  93. errHtml = "<div style='padding:10px; border:2px solid red; background:#fdd; font-family:Verdana; font-size:12px;'>"
  94. errHtml = errHtml & "<strong>Error occurred" & IIf(Not IsEmpty(context) And context <> "", ": " & context, "") & "</strong><br />"
  95. errHtml = errHtml & "<em>Time:</em> " & Now() & "<br />"
  96. errHtml = errHtml & "<em>Number:</em> " & Err.Number & "<br />"
  97. errHtml = errHtml & "<em>Description:</em> " & Server.HTMLEncode(Err.Description) & "<br />"
  98. If Len(Err.Source) > 0 Then
  99. errHtml = errHtml & "<em>Source:</em> " & Server.HTMLEncode(Err.Source) & "<br />"
  100. End If
  101. errHtml = errHtml & "</div>"
  102. Response.Write errHtml
  103. Err.Clear
  104. End If
  105. End Sub
  106. '------------------------------------------------------------------------------
  107. ' Utility: TrimQueryParams
  108. ' Removes everything from the first "?" or "&" onward.
  109. ' Usage:
  110. ' CleanPath = TrimQueryParams(rawPath)
  111. '------------------------------------------------------------------------------
  112. Function TrimQueryParams(rawPath)
  113. Dim posQ, posA, cutPos
  114. ' find the first occurrences of "?" and "&"
  115. posQ = InStr(rawPath, "?")
  116. posA = InStr(rawPath, "&")
  117. ' determine the earliest cut position (>0)
  118. If posQ > 0 And posA > 0 Then
  119. cutPos = IIf(posQ < posA, posQ, posA)
  120. ElseIf posQ > 0 Then
  121. cutPos = posQ
  122. ElseIf posA > 0 Then
  123. cutPos = posA
  124. Else
  125. cutPos = 0
  126. End If
  127. ' if found, return up to just before that char
  128. If cutPos > 0 Then
  129. TrimQueryParams = Left(rawPath, cutPos - 1)
  130. Else
  131. TrimQueryParams = rawPath
  132. End If
  133. End Function
  134. Sub Destroy(o)
  135. if isobject(o) then
  136. if not o is nothing then
  137. on error resume next
  138. o.close
  139. on error goto 0
  140. set o = nothing
  141. end if
  142. end if
  143. End Sub
  144. 'prepends indents
  145. Private Sub puti(v)
  146. put Spaces(m_indent) & v
  147. End Sub
  148. Sub put(v)
  149. Select Case typename(v)
  150. Case "LinkedList_Class" : response.write join(v.TO_Array, ", ")
  151. Case "DynamicArray_Class" : response.write JoinList(v)
  152. Case "Variant()" : response.write join(v, ", ")
  153. Case else : response.write v
  154. End Select
  155. End Sub
  156. Sub put_
  157. put "<br>"
  158. End Sub
  159. Sub putl(v)
  160. put v
  161. put_
  162. End Sub
  163. '---------------------------------------------------------------------------------------------------------------------
  164. 'Wrapper for Server.HTMLEncode() -- makes it easier on the eyes when reading the HTML code
  165. Function H(s)
  166. If Not IsEmpty(s) and Not IsNull(s) then
  167. H = Server.HTMLEncode(s)
  168. Else
  169. H = ""
  170. End If
  171. End Function
  172. '=======================================================================================================================
  173. ' Adapted from Tolerable library
  174. '=======================================================================================================================
  175. ' This subroutine allows us to ignore the difference
  176. ' between object and primitive assignments. This is
  177. ' essential for many parts of the engine.
  178. Public Sub Assign(ByRef var, ByVal val)
  179. If IsObject(val) Then
  180. Set var = val
  181. Else
  182. var = val
  183. End If
  184. End Sub
  185. ' This is similar to the ? : operator of other languages.
  186. ' Unfortunately, both the if_true and if_false "branches"
  187. ' will be evalauted before the condition is even checked. So,
  188. ' you'll only want to use this for simple expressions.
  189. Public Function Choice(ByVal cond, ByVal if_true, ByVal if_false)
  190. If cond Then
  191. Assign Choice, if_true
  192. Else
  193. Assign Choice, if_false
  194. End If
  195. End Function
  196. ' Allows single-quotes to be used in place of double-quotes.
  197. ' Basically, this is a cheap trick that can make it easier
  198. ' to specify Lambdas.
  199. Public Function Q(ByVal input)
  200. Q = Replace(input, "'", """")
  201. End Function
  202. Function SurroundString(inputVar)
  203. If VarType(inputVar) = vbString Then
  204. SurroundString = """" & inputVar & """"
  205. Else
  206. SurroundString = inputVar
  207. End If
  208. End Function
  209. Function SurroundStringInArray(arr)
  210. Dim i
  211. For i = LBound(arr) To UBound(arr)
  212. If IsString(arr(i)) Then
  213. arr(i) = """" & arr(i) & """"
  214. End If
  215. Next
  216. SurroundStringInArray = arr
  217. End Function
  218. '-----------------------------------------------------------------------------------------------------------------------
  219. 'Boolean type checkers
  220. 'Don't forget IsArray is built-in!
  221. Function IsString(value)
  222. IsString = Choice(typename(value) = "String", true, false)
  223. End Function
  224. Function IsDict(value)
  225. IsDict = Choice(typename(value) = "Dictionary", true, false)
  226. End Function
  227. Function IsRecordset(value)
  228. IsRecordset = Choice(typename(value) = "Recordset", true, false)
  229. End Function
  230. Function IsLinkedList(value)
  231. IsLinkedList = Choice(typename(value) = "LinkedList_Class", true, false)
  232. End Function
  233. Function IsArray(value)
  234. IsArray = Choice(typename(value) = "Variant()", true, false)
  235. End Function
  236. '--------------------------------------------------------------------
  237. ' Returns True when the named key is present in Session.Contents
  238. ' • Handles scalars (String, Integer, etc.), objects, Empty, and Null
  239. '--------------------------------------------------------------------
  240. Function SessionHasKey(keyName)
  241. 'Loop over the existing keys—Session.Contents is like a dictionary
  242. Dim k
  243. For Each k In Session.Contents
  244. If StrComp(k, keyName, vbTextCompare) = 0 Then
  245. SessionHasKey = True
  246. Exit Function
  247. End If
  248. Next
  249. SessionHasKey = False 'not found
  250. End Function
  251. Function RenderObjectsAsTable(arr,boolUseTabulator)
  252. Dim html, propNames, i, j, obj, val, pkName, isPk
  253. If IsEmpty(arr) Or Not IsArray(arr) Then
  254. RenderObjectsAsTable = "<!-- no data -->"
  255. Exit Function
  256. End If
  257. Set obj = arr(0)
  258. On Error Resume Next
  259. propNames = obj.Properties
  260. pkName = obj.PrimaryKey
  261. On Error GoTo 0
  262. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  263. RenderObjectsAsTable = "<!-- missing properties or primary key -->"
  264. Exit Function
  265. End If
  266. html = "<div class='table-wrapper'>" & vbCrLf
  267. html = html & "<table class='pobo-table' id='pobo-table'>" & vbCrLf
  268. html = html & " <thead><tr>" & vbCrLf
  269. For i = 0 To UBound(propNames)
  270. html = html & " <th>" & Server.HTMLEncode(propNames(i)) & "</th>" & vbCrLf
  271. Next
  272. html = html & " </tr></thead>" & vbCrLf
  273. html = html & " <tbody>" & vbCrLf
  274. For j = 0 To UBound(arr)
  275. Set obj = arr(j)
  276. html = html & " <tr>" & vbCrLf
  277. For i = 0 To UBound(propNames)
  278. val = GetDynamicProperty(obj, propNames(i))
  279. isPk = (StrComp(propNames(i), pkName, vbTextCompare) = 0)
  280. If IsNull(val) Or IsEmpty(val) Then
  281. val = "&nbsp;"
  282. ElseIf IsDate(val) Then
  283. val = FormatDateTime(val, vbShortDate)
  284. ElseIf VarType(val) = vbBoolean Then
  285. val = IIf(val, "True", "False")
  286. Else
  287. val = CStr(val)
  288. Dim maxLen : maxLen = CInt(GetAppSetting("TableCellMaxLength"))
  289. If maxLen <= 0 Then maxLen = 90
  290. If Len(val) > maxLen Then
  291. val = Left(val, maxLen - 3) & "..."
  292. End If
  293. val = Server.HTMLEncode(val)
  294. End If
  295. If isPk and boolUseTabulator = False Then
  296. val = "<a href=""" & obj.Tablename & "/edit/" & GetDynamicProperty(obj, pkName) & """ class=""table-link"">" & val & "</a>"
  297. End If
  298. html = html & " <td>" & val & "</td>" & vbCrLf
  299. Next
  300. html = html & " </tr>" & vbCrLf
  301. Next
  302. html = html & " </tbody>" & vbCrLf & "</table>" & vbCrLf & "</div>"
  303. RenderObjectsAsTable = html
  304. End Function
  305. Function RenderFormFromObject(obj)
  306. Dim html, propNames, i, name, val, inputType
  307. Dim pkName, tableName, checkedAttr
  308. On Error Resume Next
  309. propNames = obj.Properties
  310. pkName = obj.PrimaryKey
  311. tableName = obj.TableName
  312. On Error GoTo 0
  313. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  314. RenderFormFromObject = "<!-- Invalid object -->"
  315. Exit Function
  316. End If
  317. html = "<form method='post' action='/" & tableName & "/save' class='article-content'>" & vbCrLf
  318. For i = 0 To UBound(propNames)
  319. name = propNames(i)
  320. val = GetDynamicProperty(obj, name)
  321. ' Handle nulls
  322. If IsNull(val) Then val = ""
  323. ' Primary key → hidden input
  324. If StrComp(name, pkName, vbTextCompare) = 0 Then
  325. html = html & " <input type='hidden' name='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  326. 'Continue For
  327. End If
  328. html = html & " <div class='form-group'>" & vbCrLf
  329. html = html & " <label for='" & name & "'>" & name & "</label>" & vbCrLf
  330. Select Case True
  331. Case VarType(val) = vbBoolean
  332. checkedAttr = ""
  333. If val = True Then checkedAttr = " checked"
  334. html = html & " <input type='checkbox' class='form-check-input' name='" & name & "' id='" & name & "' value='true'" & checkedAttr & " />" & vbCrLf
  335. Case IsDate(val)
  336. html = html & " <input type='date' class='form-control' name='" & name & "' id='" & name & "' value='" & FormatDateForInput(val) & "' />" & vbCrLf
  337. Case IsNumeric(val)
  338. html = html & " <input type='number' class='form-control' name='" & name & "' id='" & name & "' value='" & val & "' />" & vbCrLf
  339. Case Len(val) > CInt(GetAppSetting("FormTextareaThreshold"))
  340. html = html & " <textarea class='form-control' name='" & name & "' id='" & name & "' rows='6'>" & Server.HTMLEncode(val) & "</textarea>" & vbCrLf
  341. Case Else
  342. html = html & " <input type='text' class='form-control' name='" & name & "' id='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  343. End Select
  344. html = html & " </div>" & vbCrLf
  345. Next
  346. html = html & " <button type='submit' class='btn btn-primary btn-lg'>Save</button>" & vbCrLf
  347. html = html & "</form>" & vbCrLf
  348. RenderFormFromObject = html
  349. End Function
  350. Function GetDynamicProperty(obj, propName)
  351. On Error Resume Next
  352. Dim result
  353. Execute "result = obj." & propName
  354. If Err.Number <> 0 Then
  355. result = ""
  356. Err.Clear
  357. End If
  358. GetDynamicProperty = result
  359. On Error GoTo 0
  360. End Function
  361. Function FormatDateForInput(val)
  362. If IsDate(val) Then
  363. Dim yyyy, mm, dd
  364. yyyy = Year(val)
  365. mm = Right("0" & Month(val), 2)
  366. dd = Right("0" & Day(val), 2)
  367. FormatDateForInput = yyyy & "-" & mm & "-" & dd
  368. Else
  369. FormatDateForInput = ""
  370. End If
  371. End Function
  372. '-------------------------------------------------------------
  373. ' Returns obj.<propName> for any public VBScript class property
  374. '-------------------------------------------------------------
  375. Function GetObjProp(o, pName)
  376. Dim tmp
  377. ' Build a tiny statement like: tmp = o.UserID
  378. Execute "tmp = o." & pName
  379. GetObjProp = tmp
  380. End Function
  381. Function GenerateSlug(title)
  382. Dim slug
  383. slug = LCase(title) ' Convert to lowercase
  384. slug = Replace(slug, "&", "and") ' Replace ampersands
  385. slug = Replace(slug, "'", "") ' Remove apostrophes
  386. slug = Replace(slug, """", "") ' Remove quotes
  387. slug = Replace(slug, "–", "-") ' Replace en dash
  388. slug = Replace(slug, "—", "-") ' Replace em dash
  389. slug = Replace(slug, "/", "-") ' Replace slashes
  390. slug = Replace(slug, "\", "-") ' Replace backslashes
  391. ' Remove all non-alphanumeric and non-hyphen/space characters
  392. Dim i, ch, clean
  393. clean = ""
  394. For i = 1 To Len(slug)
  395. ch = Mid(slug, i, 1)
  396. If (ch >= "a" And ch <= "z") Or (ch >= "0" And ch <= "9") Or ch = " " Or ch = "-" Then
  397. clean = clean & ch
  398. End If
  399. Next
  400. ' Replace multiple spaces or hyphens with single hyphen
  401. Do While InStr(clean, " ") > 0
  402. clean = Replace(clean, " ", " ")
  403. Loop
  404. clean = Replace(clean, " ", "-")
  405. Do While InStr(clean, "--") > 0
  406. clean = Replace(clean, "--", "-")
  407. Loop
  408. ' Trim leading/trailing hyphens
  409. Do While Left(clean, 1) = "-"
  410. clean = Mid(clean, 2)
  411. Loop
  412. Do While Right(clean, 1) = "-"
  413. clean = Left(clean, Len(clean) - 1)
  414. Loop
  415. GenerateSlug = clean
  416. End Function
  417. Function GetRawJsonFromRequest()
  418. Dim stream, rawJson
  419. Set stream = Server.CreateObject("ADODB.Stream")
  420. stream.Type = 1 ' adTypeBinary
  421. stream.Open
  422. stream.Write Request.BinaryRead(Request.TotalBytes)
  423. stream.Position = 0
  424. stream.Type = 2 ' adTypeText
  425. stream.Charset = "utf-8"
  426. rawJson = stream.ReadText
  427. stream.Close
  428. Set stream = Nothing
  429. GetRawJsonFromRequest = rawJson
  430. End Function
  431. Function Active(controllerName)
  432. On Error Resume Next
  433. If Replace(Lcase(router.Resolve(Request.ServerVariables("REQUEST_METHOD"), TrimQueryParams(Request.ServerVariables("HTTP_X_ORIGINAL_URL")))(0)),"controller","") = LCase(controllerName) Then
  434. Active = "active"
  435. Else
  436. Active = ""
  437. End If
  438. On Error GoTo 0
  439. End Function
  440. '====================================================================
  441. ' FormatDateForSql
  442. ' Converts a VBScript Date to a SQL Server-compatible string
  443. ' Output: 'YYYY-MM-DD HH:MM:SS'
  444. '====================================================================
  445. Function FormatDateForSql(vbDate)
  446. If IsNull(vbDate) Or vbDate = "" Then
  447. FormatDateForSql = "NULL"
  448. Exit Function
  449. End If
  450. ' Ensure vbDate is a valid date
  451. If Not IsDate(vbDate) Then
  452. Err.Raise vbObjectError + 1000, "FormatDateForSql", "Invalid date: " & vbDate
  453. End If
  454. Dim yyyy, mm, dd, hh, nn, ss
  455. yyyy = Year(vbDate)
  456. mm = Right("0" & Month(vbDate), 2)
  457. dd = Right("0" & Day(vbDate), 2)
  458. hh = Right("0" & Hour(vbDate), 2)
  459. nn = Right("0" & Minute(vbDate), 2)
  460. ss = Right("0" & Second(vbDate), 2)
  461. ' Construct SQL Server datetime literal
  462. FormatDateForSql = "'" & yyyy & "-" & mm & "-" & dd & " " & hh & ":" & nn & ":" & ss & "'"
  463. End Function
  464. %>

Powered by TurnKey Linux.