ASP Classic blog framework - BrainOrdure
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

798 Zeilen
25KB

  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 Function GetEnvironmentValue(name)
  56. Dim shell, env, value
  57. value = ""
  58. On Error Resume Next
  59. Set shell = Server.CreateObject("WScript.Shell")
  60. Set env = shell.Environment("PROCESS")
  61. If Err.Number = 0 Then
  62. value = env(name)
  63. If Err.Number <> 0 Then
  64. Err.Clear
  65. value = ""
  66. End If
  67. Else
  68. Err.Clear
  69. End If
  70. On Error GoTo 0
  71. GetEnvironmentValue = Trim(CStr(value))
  72. End Function
  73. Public Function GetSecureSetting(key, envName)
  74. Dim value
  75. value = Trim(CStr(GetAppSetting(key)))
  76. If Len(value) > 0 And LCase(value) <> "nothing" Then
  77. GetSecureSetting = value
  78. Exit Function
  79. End If
  80. value = Trim(CStr(GetEnvironmentValue(envName)))
  81. If Len(value) > 0 Then
  82. GetSecureSetting = value
  83. Exit Function
  84. End If
  85. GetSecureSetting = ""
  86. End Function
  87. Public Function GetGenerationPromptTemplate()
  88. Dim prompt
  89. prompt = Trim(CStr(GetAppSetting("AbacusGenerationPrompt")))
  90. If Len(prompt) = 0 Or LCase(prompt) = "nothing" Then
  91. prompt = "You write clear, engaging blog post content for a classic ASP blog. Return only valid JSON with two keys: summary and body. Summary must be 1 to 2 sentences. Body must be 3 to 5 short paragraphs separated by blank lines. Do not use markdown fences, bullets, or code blocks." & vbCrLf & vbCrLf & _
  92. "Create blog content for this post title: {TITLE}" & vbCrLf & _
  93. "Existing summary: {SUMMARY}" & vbCrLf & _
  94. "Existing body: {BODY}" & vbCrLf & _
  95. "Keep the title unchanged. Make the content readable and helpful for a general audience."
  96. End If
  97. GetGenerationPromptTemplate = prompt
  98. End Function
  99. Public Function BuildGenerationPrompt(ByRef post)
  100. Dim template
  101. template = GetGenerationPromptTemplate()
  102. template = Replace(template, "{TITLE}", SafePromptText(post.Title))
  103. template = Replace(template, "{SUMMARY}", SafePromptText(post.Summary))
  104. template = Replace(template, "{BODY}", SafePromptText(post.Body))
  105. BuildGenerationPrompt = template
  106. End Function
  107. Public Function SafePromptText(ByVal value)
  108. If IsNull(value) Or IsEmpty(value) Then
  109. SafePromptText = ""
  110. Else
  111. SafePromptText = CStr(value)
  112. End If
  113. End Function
  114. Public Function UpdateAppSetting(key, value)
  115. Dim xml, nodes, node, appSettings, found
  116. Set xml = Server.CreateObject("Microsoft.XMLDOM")
  117. xml.async = False
  118. xml.preserveWhiteSpace = True
  119. xml.Load Server.MapPath("web.config")
  120. If xml.parseError.errorCode <> 0 Then
  121. Err.Raise 1, "UpdateAppSetting", "Unable to load web.config: " & xml.parseError.reason
  122. End If
  123. Set nodes = xml.selectNodes("//appSettings/add[@key='" & key & "']")
  124. found = False
  125. If Not (nodes Is Nothing) Then
  126. If nodes.Length > 0 Then
  127. Set node = nodes.Item(0)
  128. node.setAttribute "value", value
  129. found = True
  130. End If
  131. End If
  132. If Not found Then
  133. Set appSettings = xml.selectSingleNode("//appSettings")
  134. If appSettings Is Nothing Then
  135. Err.Raise 1, "UpdateAppSetting", "<appSettings> section not found in web.config."
  136. End If
  137. Set node = xml.createElement("add")
  138. node.setAttribute "key", key
  139. node.setAttribute "value", value
  140. appSettings.appendChild node
  141. End If
  142. xml.Save Server.MapPath("web.config")
  143. Application.Contents.Remove("AppSetting_" & key)
  144. UpdateAppSetting = True
  145. End Function
  146. Public Sub ShowServerVariables
  147. Dim varName, htmlTable
  148. htmlTable = "<table border='1' cellspacing='0' cellpadding='5'>"
  149. htmlTable = htmlTable & "<thead><tr><th>Variable Name</th><th>Value</th></tr></thead><tbody>"
  150. ' Loop through all server variables
  151. For Each varName In Request.ServerVariables
  152. htmlTable = htmlTable & "<tr>"
  153. htmlTable = htmlTable & "<td>" & Server.HTMLEncode(varName) & "</td>"
  154. htmlTable = htmlTable & "<td>" & Server.HTMLEncode(Request.ServerVariables(varName)) & "</td>"
  155. htmlTable = htmlTable & "</tr>"
  156. Next
  157. htmlTable = htmlTable & "</tbody></table>"
  158. ' Output the HTML table
  159. Response.Write(htmlTable)
  160. End Sub
  161. '------------------------------------------------------------------------------
  162. ' Utility: IIf Function for VBScript
  163. ' Usage: result = IIf(condition, trueValue, falseValue)
  164. '------------------------------------------------------------------------------
  165. Function IIf(condition, trueValue, falseValue)
  166. On Error Resume Next
  167. If CBool(condition) Then
  168. IIf = trueValue
  169. Else
  170. IIf = falseValue
  171. End If
  172. If Err.Number <> 0 Then
  173. ' Optional: handle or log error in conversion/evaluation
  174. Err.Clear
  175. End If
  176. On Error GoTo 0
  177. End Function
  178. '-----------------------------
  179. ' Utility: Generic Error Reporter
  180. '-----------------------------
  181. Public Sub ErrorCheck(context)
  182. If Err.Number <> 0 Then
  183. Dim errHtml
  184. errHtml = "<div style='padding:10px; border:2px solid red; background:#fdd; font-family:Verdana; font-size:12px;'>"
  185. errHtml = errHtml & "<strong>Error occurred" & IIf(Not IsEmpty(context) And context <> "", ": " & context, "") & "</strong><br />"
  186. errHtml = errHtml & "<em>Time:</em> " & Now() & "<br />"
  187. errHtml = errHtml & "<em>Number:</em> " & Err.Number & "<br />"
  188. errHtml = errHtml & "<em>Description:</em> " & Server.HTMLEncode(Err.Description) & "<br />"
  189. If Len(Err.Source) > 0 Then
  190. errHtml = errHtml & "<em>Source:</em> " & Server.HTMLEncode(Err.Source) & "<br />"
  191. End If
  192. errHtml = errHtml & "</div>"
  193. Response.Write errHtml
  194. Err.Clear
  195. End If
  196. End Sub
  197. '------------------------------------------------------------------------------
  198. ' Utility: TrimQueryParams
  199. ' Removes everything from the first "?" or "&" onward.
  200. ' Usage:
  201. ' CleanPath = TrimQueryParams(rawPath)
  202. '------------------------------------------------------------------------------
  203. Function TrimQueryParams(rawPath)
  204. Dim posQ, posA, cutPos
  205. ' find the first occurrences of "?" and "&"
  206. posQ = InStr(rawPath, "?")
  207. posA = InStr(rawPath, "&")
  208. ' determine the earliest cut position (>0)
  209. If posQ > 0 And posA > 0 Then
  210. cutPos = IIf(posQ < posA, posQ, posA)
  211. ElseIf posQ > 0 Then
  212. cutPos = posQ
  213. ElseIf posA > 0 Then
  214. cutPos = posA
  215. Else
  216. cutPos = 0
  217. End If
  218. ' if found, return up to just before that char
  219. If cutPos > 0 Then
  220. TrimQueryParams = Left(rawPath, cutPos - 1)
  221. Else
  222. TrimQueryParams = rawPath
  223. End If
  224. End Function
  225. Function DecodeUrlPath(ByVal rawPath)
  226. Dim current, previous
  227. current = Trim(CStr(rawPath))
  228. On Error Resume Next
  229. Do
  230. previous = current
  231. current = Server.URLDecode(current)
  232. If Err.Number <> 0 Then
  233. Err.Clear
  234. current = previous
  235. Exit Do
  236. End If
  237. Loop While current <> previous
  238. On Error GoTo 0
  239. current = Replace(current, "%252D", "-")
  240. current = Replace(current, "%252d", "-")
  241. current = Replace(current, "%2D", "-")
  242. current = Replace(current, "%2d", "-")
  243. current = Replace(current, "%25", "%")
  244. DecodeUrlPath = current
  245. End Function
  246. Sub Destroy(o)
  247. if isobject(o) then
  248. if not o is nothing then
  249. on error resume next
  250. o.close
  251. on error goto 0
  252. set o = nothing
  253. end if
  254. end if
  255. End Sub
  256. 'prepends indents
  257. Private Sub puti(v)
  258. put Spaces(m_indent) & v
  259. End Sub
  260. Sub put(v)
  261. Select Case typename(v)
  262. Case "LinkedList_Class" : response.write join(v.TO_Array, ", ")
  263. Case "DynamicArray_Class" : response.write JoinList(v)
  264. Case "Variant()" : response.write join(v, ", ")
  265. Case else : response.write v
  266. End Select
  267. End Sub
  268. Sub put_
  269. put "<br>"
  270. End Sub
  271. Sub putl(v)
  272. put v
  273. put_
  274. End Sub
  275. '---------------------------------------------------------------------------------------------------------------------
  276. 'Wrapper for Server.HTMLEncode() -- makes it easier on the eyes when reading the HTML code
  277. Function H(s)
  278. If Not IsEmpty(s) and Not IsNull(s) then
  279. H = Server.HTMLEncode(s)
  280. Else
  281. H = ""
  282. End If
  283. End Function
  284. '------------------------------------------------------------------------------
  285. ' Canonical application URL helpers
  286. ' - Categories use numeric IDs
  287. ' - Posts use slug permalinks for public links and numeric IDs for admin actions
  288. '------------------------------------------------------------------------------
  289. Function CategoryUrl(ByVal categoryId)
  290. CategoryUrl = "/categories/" & Server.URLEncode(CStr(categoryId))
  291. End Function
  292. Function CategoriesUrl()
  293. CategoriesUrl = "/categories"
  294. End Function
  295. Function CategoryNewUrl()
  296. CategoryNewUrl = "/categories/new"
  297. End Function
  298. Function CategoryEditUrl(ByVal categoryId)
  299. CategoryEditUrl = CategoryUrl(categoryId) & "/edit"
  300. End Function
  301. Function CategoryDeleteUrl(ByVal categoryId)
  302. CategoryDeleteUrl = CategoryUrl(categoryId) & "/delete"
  303. End Function
  304. Function PostUrl(ByVal slug)
  305. PostUrl = "/posts/" & Server.URLEncode(NormalizeSlug(slug))
  306. End Function
  307. Function PostPath(ByVal slug)
  308. PostPath = "/posts/" & NormalizeSlug(slug)
  309. End Function
  310. Function PostsUrl()
  311. PostsUrl = "/posts"
  312. End Function
  313. Function PostNewUrl()
  314. PostNewUrl = "/posts/new"
  315. End Function
  316. Function PostEditUrl(ByVal postId)
  317. PostEditUrl = "/posts/" & Server.URLEncode(CStr(postId)) & "/edit"
  318. End Function
  319. Function PostUpdateUrl(ByVal postId)
  320. PostUpdateUrl = "/posts/" & Server.URLEncode(CStr(postId))
  321. End Function
  322. Function PostDeleteUrl(ByVal postId)
  323. PostDeleteUrl = "/posts/" & Server.URLEncode(CStr(postId)) & "/delete"
  324. End Function
  325. Function AdminPostPublishUrl(ByVal postId)
  326. AdminPostPublishUrl = "/admin/posts/" & Server.URLEncode(CStr(postId)) & "/publish"
  327. End Function
  328. Function AdminPostUnpublishUrl(ByVal postId)
  329. AdminPostUnpublishUrl = "/admin/posts/" & Server.URLEncode(CStr(postId)) & "/unpublish"
  330. End Function
  331. Function AdminPostAIUrl(ByVal postId)
  332. AdminPostAIUrl = "/admin/posts/" & Server.URLEncode(CStr(postId)) & "/ai"
  333. End Function
  334. Function AdminCommentsUrl()
  335. AdminCommentsUrl = "/admin/comments"
  336. End Function
  337. Function AdminCommentApproveUrl(ByVal commentId)
  338. AdminCommentApproveUrl = "/admin/comments/" & Server.URLEncode(CStr(commentId)) & "/approve"
  339. End Function
  340. Function AdminCommentUnapproveUrl(ByVal commentId)
  341. AdminCommentUnapproveUrl = "/admin/comments/" & Server.URLEncode(CStr(commentId)) & "/unapprove"
  342. End Function
  343. Function AdminCommentDeleteUrl(ByVal commentId)
  344. AdminCommentDeleteUrl = "/admin/comments/" & Server.URLEncode(CStr(commentId)) & "/delete"
  345. End Function
  346. Function AdminAIPromptUrl()
  347. AdminAIPromptUrl = "/admin/ai-prompt"
  348. End Function
  349. Function AdminAIPromptUpdateUrl()
  350. AdminAIPromptUpdateUrl = "/admin/ai-prompt"
  351. End Function
  352. Function AdminUrl()
  353. AdminUrl = "/admin"
  354. End Function
  355. Function CommentsUrl()
  356. CommentsUrl = "/comments"
  357. End Function
  358. Function NormalizeSlug(ByVal slug)
  359. Dim current, previous
  360. current = Trim(CStr(slug))
  361. If Len(current) = 0 Then
  362. NormalizeSlug = ""
  363. Exit Function
  364. End If
  365. On Error Resume Next
  366. Do
  367. previous = current
  368. current = Server.URLDecode(current)
  369. If Err.Number <> 0 Then
  370. Err.Clear
  371. current = previous
  372. Exit Do
  373. End If
  374. Loop While current <> previous
  375. On Error GoTo 0
  376. current = Replace(current, "%252D", "-")
  377. current = Replace(current, "%252d", "-")
  378. current = Replace(current, "%2D", "-")
  379. current = Replace(current, "%2d", "-")
  380. current = Replace(current, "%25", "%")
  381. NormalizeSlug = current
  382. End Function
  383. '=======================================================================================================================
  384. ' Adapted from Tolerable library
  385. '=======================================================================================================================
  386. ' This subroutine allows us to ignore the difference
  387. ' between object and primitive assignments. This is
  388. ' essential for many parts of the engine.
  389. Public Sub Assign(ByRef var, ByVal val)
  390. If IsObject(val) Then
  391. Set var = val
  392. Else
  393. var = val
  394. End If
  395. End Sub
  396. ' This is similar to the ? : operator of other languages.
  397. ' Unfortunately, both the if_true and if_false "branches"
  398. ' will be evalauted before the condition is even checked. So,
  399. ' you'll only want to use this for simple expressions.
  400. Public Function Choice(ByVal cond, ByVal if_true, ByVal if_false)
  401. If cond Then
  402. Assign Choice, if_true
  403. Else
  404. Assign Choice, if_false
  405. End If
  406. End Function
  407. ' Allows single-quotes to be used in place of double-quotes.
  408. ' Basically, this is a cheap trick that can make it easier
  409. ' to specify Lambdas.
  410. Public Function Q(ByVal input)
  411. Q = Replace(input, "'", """")
  412. End Function
  413. Function SurroundString(inputVar)
  414. If VarType(inputVar) = vbString Then
  415. SurroundString = """" & inputVar & """"
  416. Else
  417. SurroundString = inputVar
  418. End If
  419. End Function
  420. Function SurroundStringInArray(arr)
  421. Dim i
  422. For i = LBound(arr) To UBound(arr)
  423. If IsString(arr(i)) Then
  424. arr(i) = """" & arr(i) & """"
  425. End If
  426. Next
  427. SurroundStringInArray = arr
  428. End Function
  429. '-----------------------------------------------------------------------------------------------------------------------
  430. 'Boolean type checkers
  431. 'Don't forget IsArray is built-in!
  432. Function IsString(value)
  433. IsString = Choice(typename(value) = "String", true, false)
  434. End Function
  435. Function IsDict(value)
  436. IsDict = Choice(typename(value) = "Dictionary", true, false)
  437. End Function
  438. Function IsRecordset(value)
  439. IsRecordset = Choice(typename(value) = "Recordset", true, false)
  440. End Function
  441. Function IsLinkedList(value)
  442. IsLinkedList = Choice(typename(value) = "LinkedList_Class", true, false)
  443. End Function
  444. Function IsArray(value)
  445. IsArray = Choice(typename(value) = "Variant()", true, false)
  446. End Function
  447. '--------------------------------------------------------------------
  448. ' Returns True when the named key is present in Session.Contents
  449. ' • Handles scalars (String, Integer, etc.), objects, Empty, and Null
  450. '--------------------------------------------------------------------
  451. Function SessionHasKey(keyName)
  452. 'Loop over the existing keys—Session.Contents is like a dictionary
  453. Dim k
  454. For Each k In Session.Contents
  455. If StrComp(k, keyName, vbTextCompare) = 0 Then
  456. SessionHasKey = True
  457. Exit Function
  458. End If
  459. Next
  460. SessionHasKey = False 'not found
  461. End Function
  462. Function RenderObjectsAsTable(arr,boolUseTabulator)
  463. Dim html, propNames, i, j, obj, val, pkName, isPk
  464. If IsEmpty(arr) Or Not IsArray(arr) Then
  465. RenderObjectsAsTable = "<!-- no data -->"
  466. Exit Function
  467. End If
  468. Set obj = arr(0)
  469. On Error Resume Next
  470. propNames = obj.Properties
  471. pkName = obj.PrimaryKey
  472. On Error GoTo 0
  473. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  474. RenderObjectsAsTable = "<!-- missing properties or primary key -->"
  475. Exit Function
  476. End If
  477. html = "<div class='table-wrapper'>" & vbCrLf
  478. html = html & "<table class='pobo-table' id='pobo-table'>" & vbCrLf
  479. html = html & " <thead><tr>" & vbCrLf
  480. For i = 0 To UBound(propNames)
  481. html = html & " <th>" & Server.HTMLEncode(propNames(i)) & "</th>" & vbCrLf
  482. Next
  483. html = html & " </tr></thead>" & vbCrLf
  484. html = html & " <tbody>" & vbCrLf
  485. For j = 0 To UBound(arr)
  486. Set obj = arr(j)
  487. html = html & " <tr>" & vbCrLf
  488. For i = 0 To UBound(propNames)
  489. val = GetDynamicProperty(obj, propNames(i))
  490. isPk = (StrComp(propNames(i), pkName, vbTextCompare) = 0)
  491. If IsNull(val) Or IsEmpty(val) Then
  492. val = "&nbsp;"
  493. ElseIf IsDate(val) Then
  494. val = FormatDateTime(val, vbShortDate)
  495. ElseIf VarType(val) = vbBoolean Then
  496. val = IIf(val, "True", "False")
  497. Else
  498. val = CStr(val)
  499. Dim maxLen : maxLen = CInt(GetAppSetting("TableCellMaxLength"))
  500. If maxLen <= 0 Then maxLen = 90
  501. If Len(val) > maxLen Then
  502. val = Left(val, maxLen - 3) & "..."
  503. End If
  504. val = Server.HTMLEncode(val)
  505. End If
  506. If isPk and boolUseTabulator = False Then
  507. val = "<a href=""" & obj.Tablename & "/edit/" & GetDynamicProperty(obj, pkName) & """ class=""table-link"">" & val & "</a>"
  508. End If
  509. html = html & " <td>" & val & "</td>" & vbCrLf
  510. Next
  511. html = html & " </tr>" & vbCrLf
  512. Next
  513. html = html & " </tbody>" & vbCrLf & "</table>" & vbCrLf & "</div>"
  514. RenderObjectsAsTable = html
  515. End Function
  516. Function RenderFormFromObject(obj)
  517. Dim html, propNames, i, name, val, inputType
  518. Dim pkName, tableName, checkedAttr
  519. On Error Resume Next
  520. propNames = obj.Properties
  521. pkName = obj.PrimaryKey
  522. tableName = obj.TableName
  523. On Error GoTo 0
  524. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  525. RenderFormFromObject = "<!-- Invalid object -->"
  526. Exit Function
  527. End If
  528. html = "<form method='post' action='/" & tableName & "/save' class='article-content'>" & vbCrLf
  529. For i = 0 To UBound(propNames)
  530. name = propNames(i)
  531. val = GetDynamicProperty(obj, name)
  532. ' Handle nulls
  533. If IsNull(val) Then val = ""
  534. ' Primary key → hidden input
  535. If StrComp(name, pkName, vbTextCompare) = 0 Then
  536. html = html & " <input type='hidden' name='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  537. 'Continue For
  538. End If
  539. html = html & " <div class='form-group'>" & vbCrLf
  540. html = html & " <label for='" & name & "'>" & name & "</label>" & vbCrLf
  541. Select Case True
  542. Case VarType(val) = vbBoolean
  543. checkedAttr = ""
  544. If val = True Then checkedAttr = " checked"
  545. html = html & " <input type='checkbox' class='form-check-input' name='" & name & "' id='" & name & "' value='true'" & checkedAttr & " />" & vbCrLf
  546. Case IsDate(val)
  547. html = html & " <input type='date' class='form-control' name='" & name & "' id='" & name & "' value='" & FormatDateForInput(val) & "' />" & vbCrLf
  548. Case IsNumeric(val)
  549. html = html & " <input type='number' class='form-control' name='" & name & "' id='" & name & "' value='" & val & "' />" & vbCrLf
  550. Case Len(val) > CInt(GetAppSetting("FormTextareaThreshold"))
  551. html = html & " <textarea class='form-control' name='" & name & "' id='" & name & "' rows='6'>" & Server.HTMLEncode(val) & "</textarea>" & vbCrLf
  552. Case Else
  553. html = html & " <input type='text' class='form-control' name='" & name & "' id='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  554. End Select
  555. html = html & " </div>" & vbCrLf
  556. Next
  557. html = html & " <button type='submit' class='btn btn-primary btn-lg'>Save</button>" & vbCrLf
  558. html = html & "</form>" & vbCrLf
  559. RenderFormFromObject = html
  560. End Function
  561. Function GetDynamicProperty(obj, propName)
  562. On Error Resume Next
  563. Dim result
  564. Execute "result = obj." & propName
  565. If Err.Number <> 0 Then
  566. result = ""
  567. Err.Clear
  568. End If
  569. GetDynamicProperty = result
  570. On Error GoTo 0
  571. End Function
  572. Function FormatDateForInput(val)
  573. If IsDate(val) Then
  574. Dim yyyy, mm, dd
  575. yyyy = Year(val)
  576. mm = Right("0" & Month(val), 2)
  577. dd = Right("0" & Day(val), 2)
  578. FormatDateForInput = yyyy & "-" & mm & "-" & dd
  579. Else
  580. FormatDateForInput = ""
  581. End If
  582. End Function
  583. '-------------------------------------------------------------
  584. ' Returns obj.<propName> for any public VBScript class property
  585. '-------------------------------------------------------------
  586. Function GetObjProp(o, pName)
  587. Dim tmp
  588. ' Build a tiny statement like: tmp = o.UserID
  589. Execute "tmp = o." & pName
  590. GetObjProp = tmp
  591. End Function
  592. Function GenerateSlug(title)
  593. Dim slug
  594. slug = LCase(title) ' Convert to lowercase
  595. slug = Replace(slug, "&", "and") ' Replace ampersands
  596. slug = Replace(slug, "'", "") ' Remove apostrophes
  597. slug = Replace(slug, """", "") ' Remove quotes
  598. slug = Replace(slug, "–", "-") ' Replace en dash
  599. slug = Replace(slug, "—", "-") ' Replace em dash
  600. slug = Replace(slug, "/", "-") ' Replace slashes
  601. slug = Replace(slug, "\", "-") ' Replace backslashes
  602. ' Remove all non-alphanumeric and non-hyphen/space characters
  603. Dim i, ch, clean
  604. clean = ""
  605. For i = 1 To Len(slug)
  606. ch = Mid(slug, i, 1)
  607. If (ch >= "a" And ch <= "z") Or (ch >= "0" And ch <= "9") Or ch = " " Or ch = "-" Then
  608. clean = clean & ch
  609. End If
  610. Next
  611. ' Replace multiple spaces or hyphens with single hyphen
  612. Do While InStr(clean, " ") > 0
  613. clean = Replace(clean, " ", " ")
  614. Loop
  615. clean = Replace(clean, " ", "-")
  616. Do While InStr(clean, "--") > 0
  617. clean = Replace(clean, "--", "-")
  618. Loop
  619. ' Trim leading/trailing hyphens
  620. Do While Left(clean, 1) = "-"
  621. clean = Mid(clean, 2)
  622. Loop
  623. Do While Right(clean, 1) = "-"
  624. clean = Left(clean, Len(clean) - 1)
  625. Loop
  626. GenerateSlug = clean
  627. End Function
  628. Function GetRawJsonFromRequest()
  629. Dim stream, rawJson
  630. Set stream = Server.CreateObject("ADODB.Stream")
  631. stream.Type = 1 ' adTypeBinary
  632. stream.Open
  633. stream.Write Request.BinaryRead(Request.TotalBytes)
  634. stream.Position = 0
  635. stream.Type = 2 ' adTypeText
  636. stream.Charset = "utf-8"
  637. rawJson = stream.ReadText
  638. stream.Close
  639. Set stream = Nothing
  640. GetRawJsonFromRequest = rawJson
  641. End Function
  642. Function Active(controllerName)
  643. On Error Resume Next
  644. If Replace(Lcase(router.Resolve(Request.ServerVariables("REQUEST_METHOD"), TrimQueryParams(Request.ServerVariables("HTTP_X_ORIGINAL_URL")))(0)),"controller","") = LCase(controllerName) Then
  645. Active = "active"
  646. Else
  647. Active = ""
  648. End If
  649. On Error GoTo 0
  650. End Function
  651. '====================================================================
  652. ' FormatDateForSql
  653. ' Converts a VBScript Date to a SQL Server-compatible string
  654. ' Output: 'YYYY-MM-DD HH:MM:SS'
  655. '====================================================================
  656. Function FormatDateForSql(vbDate)
  657. If IsNull(vbDate) Or vbDate = "" Then
  658. FormatDateForSql = "NULL"
  659. Exit Function
  660. End If
  661. ' Ensure vbDate is a valid date
  662. If Not IsDate(vbDate) Then
  663. Err.Raise vbObjectError + 1000, "FormatDateForSql", "Invalid date: " & vbDate
  664. End If
  665. Dim yyyy, mm, dd, hh, nn, ss
  666. yyyy = Year(vbDate)
  667. mm = Right("0" & Month(vbDate), 2)
  668. dd = Right("0" & Day(vbDate), 2)
  669. hh = Right("0" & Hour(vbDate), 2)
  670. nn = Right("0" & Minute(vbDate), 2)
  671. ss = Right("0" & Second(vbDate), 2)
  672. ' Construct SQL Server datetime literal
  673. FormatDateForSql = "'" & yyyy & "-" & mm & "-" & dd & " " & hh & ":" & nn & ":" & ss & "'"
  674. End Function
  675. %>

Powered by TurnKey Linux.