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.

565 line
19KB

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

Powered by TurnKey Linux.