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.

549 line
18KB

  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. '------------------------------------------------------------------------------
  135. ' Utility: NewGuid
  136. ' Generates a new 36-character GUID string (no surrounding braces), suitable
  137. ' for use as a one-time token, e.g. in a URL query string.
  138. '------------------------------------------------------------------------------
  139. Function NewGuid()
  140. Dim TL, raw
  141. Set TL = CreateObject("Scriptlet.TypeLib")
  142. raw = CStr(TL.Guid)
  143. Set TL = Nothing
  144. NewGuid = Mid(raw, 2, 36) ' strip the surrounding { }
  145. End Function
  146. Sub Destroy(o)
  147. if isobject(o) then
  148. if not o is nothing then
  149. on error resume next
  150. o.close
  151. on error goto 0
  152. set o = nothing
  153. end if
  154. end if
  155. End Sub
  156. 'prepends indents
  157. Private Sub puti(v)
  158. put Spaces(m_indent) & v
  159. End Sub
  160. Sub put(v)
  161. Select Case typename(v)
  162. Case "LinkedList_Class" : response.write join(v.TO_Array, ", ")
  163. Case "DynamicArray_Class" : response.write JoinList(v)
  164. Case "Variant()" : response.write join(v, ", ")
  165. Case else : response.write v
  166. End Select
  167. End Sub
  168. Sub put_
  169. put "<br>"
  170. End Sub
  171. Sub putl(v)
  172. put v
  173. put_
  174. End Sub
  175. '---------------------------------------------------------------------------------------------------------------------
  176. 'Wrapper for Server.HTMLEncode() -- makes it easier on the eyes when reading the HTML code
  177. Function H(s)
  178. If Not IsEmpty(s) and Not IsNull(s) then
  179. H = Server.HTMLEncode(s)
  180. Else
  181. H = ""
  182. End If
  183. End Function
  184. '=======================================================================================================================
  185. ' Adapted from Tolerable library
  186. '=======================================================================================================================
  187. ' This subroutine allows us to ignore the difference
  188. ' between object and primitive assignments. This is
  189. ' essential for many parts of the engine.
  190. Public Sub Assign(ByRef var, ByVal val)
  191. If IsObject(val) Then
  192. Set var = val
  193. Else
  194. var = val
  195. End If
  196. End Sub
  197. ' This is similar to the ? : operator of other languages.
  198. ' Unfortunately, both the if_true and if_false "branches"
  199. ' will be evalauted before the condition is even checked. So,
  200. ' you'll only want to use this for simple expressions.
  201. Public Function Choice(ByVal cond, ByVal if_true, ByVal if_false)
  202. If cond Then
  203. Assign Choice, if_true
  204. Else
  205. Assign Choice, if_false
  206. End If
  207. End Function
  208. ' Allows single-quotes to be used in place of double-quotes.
  209. ' Basically, this is a cheap trick that can make it easier
  210. ' to specify Lambdas.
  211. Public Function Q(ByVal input)
  212. Q = Replace(input, "'", """")
  213. End Function
  214. Function SurroundString(inputVar)
  215. If VarType(inputVar) = vbString Then
  216. SurroundString = """" & inputVar & """"
  217. Else
  218. SurroundString = inputVar
  219. End If
  220. End Function
  221. Function SurroundStringInArray(arr)
  222. Dim i
  223. For i = LBound(arr) To UBound(arr)
  224. If IsString(arr(i)) Then
  225. arr(i) = """" & arr(i) & """"
  226. End If
  227. Next
  228. SurroundStringInArray = arr
  229. End Function
  230. '-----------------------------------------------------------------------------------------------------------------------
  231. 'Boolean type checkers
  232. 'Don't forget IsArray is built-in!
  233. Function IsString(value)
  234. IsString = Choice(typename(value) = "String", true, false)
  235. End Function
  236. Function IsDict(value)
  237. IsDict = Choice(typename(value) = "Dictionary", true, false)
  238. End Function
  239. Function IsRecordset(value)
  240. IsRecordset = Choice(typename(value) = "Recordset", true, false)
  241. End Function
  242. Function IsLinkedList(value)
  243. IsLinkedList = Choice(typename(value) = "LinkedList_Class", true, false)
  244. End Function
  245. Function IsArray(value)
  246. IsArray = Choice(typename(value) = "Variant()", true, false)
  247. End Function
  248. '--------------------------------------------------------------------
  249. ' Returns True when the named key is present in Session.Contents
  250. ' • Handles scalars (String, Integer, etc.), objects, Empty, and Null
  251. '--------------------------------------------------------------------
  252. Function SessionHasKey(keyName)
  253. 'Loop over the existing keys—Session.Contents is like a dictionary
  254. Dim k
  255. For Each k In Session.Contents
  256. If StrComp(k, keyName, vbTextCompare) = 0 Then
  257. SessionHasKey = True
  258. Exit Function
  259. End If
  260. Next
  261. SessionHasKey = False 'not found
  262. End Function
  263. Function RenderObjectsAsTable(arr,boolUseTabulator)
  264. Dim html, propNames, i, j, obj, val, pkName, isPk
  265. If IsEmpty(arr) Or Not IsArray(arr) Then
  266. RenderObjectsAsTable = "<!-- no data -->"
  267. Exit Function
  268. End If
  269. Set obj = arr(0)
  270. On Error Resume Next
  271. propNames = obj.Properties
  272. pkName = obj.PrimaryKey
  273. On Error GoTo 0
  274. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  275. RenderObjectsAsTable = "<!-- missing properties or primary key -->"
  276. Exit Function
  277. End If
  278. html = "<div class='table-wrapper'>" & vbCrLf
  279. html = html & "<table class='pobo-table' id='pobo-table'>" & vbCrLf
  280. html = html & " <thead><tr>" & vbCrLf
  281. For i = 0 To UBound(propNames)
  282. html = html & " <th>" & Server.HTMLEncode(propNames(i)) & "</th>" & vbCrLf
  283. Next
  284. html = html & " </tr></thead>" & vbCrLf
  285. html = html & " <tbody>" & vbCrLf
  286. For j = 0 To UBound(arr)
  287. Set obj = arr(j)
  288. html = html & " <tr>" & vbCrLf
  289. For i = 0 To UBound(propNames)
  290. val = GetDynamicProperty(obj, propNames(i))
  291. isPk = (StrComp(propNames(i), pkName, vbTextCompare) = 0)
  292. If IsNull(val) Or IsEmpty(val) Then
  293. val = "&nbsp;"
  294. ElseIf IsDate(val) Then
  295. val = FormatDateTime(val, vbShortDate)
  296. ElseIf VarType(val) = vbBoolean Then
  297. val = IIf(val, "True", "False")
  298. Else
  299. val = CStr(val)
  300. Dim maxLen : maxLen = CInt(GetAppSetting("TableCellMaxLength"))
  301. If maxLen <= 0 Then maxLen = 90
  302. If Len(val) > maxLen Then
  303. val = Left(val, maxLen - 3) & "..."
  304. End If
  305. val = Server.HTMLEncode(val)
  306. End If
  307. If isPk and boolUseTabulator = False Then
  308. val = "<a href=""" & obj.Tablename & "/edit/" & GetDynamicProperty(obj, pkName) & """ class=""table-link"">" & val & "</a>"
  309. End If
  310. html = html & " <td>" & val & "</td>" & vbCrLf
  311. Next
  312. html = html & " </tr>" & vbCrLf
  313. Next
  314. html = html & " </tbody>" & vbCrLf & "</table>" & vbCrLf & "</div>"
  315. RenderObjectsAsTable = html
  316. End Function
  317. Function RenderFormFromObject(obj)
  318. Dim html, propNames, i, name, val, inputType
  319. Dim pkName, tableName, checkedAttr
  320. On Error Resume Next
  321. propNames = obj.Properties
  322. pkName = obj.PrimaryKey
  323. tableName = obj.TableName
  324. On Error GoTo 0
  325. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  326. RenderFormFromObject = "<!-- Invalid object -->"
  327. Exit Function
  328. End If
  329. html = "<form method='post' action='/" & tableName & "/save' class='article-content'>" & vbCrLf
  330. For i = 0 To UBound(propNames)
  331. name = propNames(i)
  332. val = GetDynamicProperty(obj, name)
  333. ' Handle nulls
  334. If IsNull(val) Then val = ""
  335. ' Primary key → hidden input
  336. If StrComp(name, pkName, vbTextCompare) = 0 Then
  337. html = html & " <input type='hidden' name='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  338. 'Continue For
  339. End If
  340. html = html & " <div class='form-group'>" & vbCrLf
  341. html = html & " <label for='" & name & "'>" & name & "</label>" & vbCrLf
  342. Select Case True
  343. Case VarType(val) = vbBoolean
  344. checkedAttr = ""
  345. If val = True Then checkedAttr = " checked"
  346. html = html & " <input type='checkbox' class='form-check-input' name='" & name & "' id='" & name & "' value='true'" & checkedAttr & " />" & vbCrLf
  347. Case IsDate(val)
  348. html = html & " <input type='date' class='form-control' name='" & name & "' id='" & name & "' value='" & FormatDateForInput(val) & "' />" & vbCrLf
  349. Case IsNumeric(val)
  350. html = html & " <input type='number' class='form-control' name='" & name & "' id='" & name & "' value='" & val & "' />" & vbCrLf
  351. Case Len(val) > CInt(GetAppSetting("FormTextareaThreshold"))
  352. html = html & " <textarea class='form-control' name='" & name & "' id='" & name & "' rows='6'>" & Server.HTMLEncode(val) & "</textarea>" & vbCrLf
  353. Case Else
  354. html = html & " <input type='text' class='form-control' name='" & name & "' id='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  355. End Select
  356. html = html & " </div>" & vbCrLf
  357. Next
  358. html = html & " <button type='submit' class='btn btn-primary btn-lg'>Save</button>" & vbCrLf
  359. html = html & "</form>" & vbCrLf
  360. RenderFormFromObject = html
  361. End Function
  362. Function GetDynamicProperty(obj, propName)
  363. On Error Resume Next
  364. Dim result
  365. Execute "result = obj." & propName
  366. If Err.Number <> 0 Then
  367. result = ""
  368. Err.Clear
  369. End If
  370. GetDynamicProperty = result
  371. On Error GoTo 0
  372. End Function
  373. Function FormatDateForInput(val)
  374. If IsDate(val) Then
  375. Dim yyyy, mm, dd
  376. yyyy = Year(val)
  377. mm = Right("0" & Month(val), 2)
  378. dd = Right("0" & Day(val), 2)
  379. FormatDateForInput = yyyy & "-" & mm & "-" & dd
  380. Else
  381. FormatDateForInput = ""
  382. End If
  383. End Function
  384. '-------------------------------------------------------------
  385. ' Returns obj.<propName> for any public VBScript class property
  386. '-------------------------------------------------------------
  387. Function GetObjProp(o, pName)
  388. Dim tmp
  389. ' Build a tiny statement like: tmp = o.UserID
  390. Execute "tmp = o." & pName
  391. GetObjProp = tmp
  392. End Function
  393. Function GenerateSlug(title)
  394. Dim slug
  395. slug = LCase(title) ' Convert to lowercase
  396. slug = Replace(slug, "&", "and") ' Replace ampersands
  397. slug = Replace(slug, "'", "") ' Remove apostrophes
  398. slug = Replace(slug, """", "") ' Remove quotes
  399. slug = Replace(slug, "–", "-") ' Replace en dash
  400. slug = Replace(slug, "—", "-") ' Replace em dash
  401. slug = Replace(slug, "/", "-") ' Replace slashes
  402. slug = Replace(slug, "\", "-") ' Replace backslashes
  403. ' Remove all non-alphanumeric and non-hyphen/space characters
  404. Dim i, ch, clean
  405. clean = ""
  406. For i = 1 To Len(slug)
  407. ch = Mid(slug, i, 1)
  408. If (ch >= "a" And ch <= "z") Or (ch >= "0" And ch <= "9") Or ch = " " Or ch = "-" Then
  409. clean = clean & ch
  410. End If
  411. Next
  412. ' Replace multiple spaces or hyphens with single hyphen
  413. Do While InStr(clean, " ") > 0
  414. clean = Replace(clean, " ", " ")
  415. Loop
  416. clean = Replace(clean, " ", "-")
  417. Do While InStr(clean, "--") > 0
  418. clean = Replace(clean, "--", "-")
  419. Loop
  420. ' Trim leading/trailing hyphens
  421. Do While Left(clean, 1) = "-"
  422. clean = Mid(clean, 2)
  423. Loop
  424. Do While Right(clean, 1) = "-"
  425. clean = Left(clean, Len(clean) - 1)
  426. Loop
  427. GenerateSlug = clean
  428. End Function
  429. Function GetRawJsonFromRequest()
  430. Dim stream, rawJson
  431. Set stream = Server.CreateObject("ADODB.Stream")
  432. stream.Type = 1 ' adTypeBinary
  433. stream.Open
  434. stream.Write Request.BinaryRead(Request.TotalBytes)
  435. stream.Position = 0
  436. stream.Type = 2 ' adTypeText
  437. stream.Charset = "utf-8"
  438. rawJson = stream.ReadText
  439. stream.Close
  440. Set stream = Nothing
  441. GetRawJsonFromRequest = rawJson
  442. End Function
  443. Function Active(controllerName)
  444. On Error Resume Next
  445. If Replace(Lcase(router.Resolve(Request.ServerVariables("REQUEST_METHOD"), TrimQueryParams(Request.ServerVariables("HTTP_X_ORIGINAL_URL")))(0)),"controller","") = LCase(controllerName) Then
  446. Active = "active"
  447. Else
  448. Active = ""
  449. End If
  450. On Error GoTo 0
  451. End Function
  452. '====================================================================
  453. ' FormatDateForSql
  454. ' Converts a VBScript Date to a SQL Server-compatible string
  455. ' Output: 'YYYY-MM-DD HH:MM:SS'
  456. '====================================================================
  457. Function FormatDateForSql(vbDate)
  458. If IsNull(vbDate) Or vbDate = "" Then
  459. FormatDateForSql = "NULL"
  460. Exit Function
  461. End If
  462. ' Ensure vbDate is a valid date
  463. If Not IsDate(vbDate) Then
  464. Err.Raise vbObjectError + 1000, "FormatDateForSql", "Invalid date: " & vbDate
  465. End If
  466. Dim yyyy, mm, dd, hh, nn, ss
  467. yyyy = Year(vbDate)
  468. mm = Right("0" & Month(vbDate), 2)
  469. dd = Right("0" & Day(vbDate), 2)
  470. hh = Right("0" & Hour(vbDate), 2)
  471. nn = Right("0" & Minute(vbDate), 2)
  472. ss = Right("0" & Second(vbDate), 2)
  473. ' Construct SQL Server datetime literal
  474. FormatDateForSql = "'" & yyyy & "-" & mm & "-" & dd & " " & hh & ":" & nn & ":" & ss & "'"
  475. End Function
  476. %>

Powered by TurnKey Linux.