ASP Classic blog framework - BrainOrdure
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

817 行
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. Function RenderPostBody(ByVal body)
  285. Dim raw
  286. If IsNull(body) Or IsEmpty(body) Then
  287. RenderPostBody = ""
  288. Exit Function
  289. End If
  290. raw = CStr(body)
  291. If InStr(1, raw, "<", vbTextCompare) > 0 And InStr(1, raw, ">", vbTextCompare) > 0 Then
  292. RenderPostBody = raw
  293. Else
  294. raw = Server.HTMLEncode(raw)
  295. raw = Replace(raw, vbCrLf, "<br>")
  296. raw = Replace(raw, vbCr, "<br>")
  297. raw = Replace(raw, vbLf, "<br>")
  298. RenderPostBody = raw
  299. End If
  300. End Function
  301. '------------------------------------------------------------------------------
  302. ' Canonical application URL helpers
  303. ' - Categories use numeric IDs
  304. ' - Posts use slug permalinks for public links and numeric IDs for admin actions
  305. '------------------------------------------------------------------------------
  306. Function CategoryUrl(ByVal categoryId)
  307. CategoryUrl = "/categories/" & Server.URLEncode(CStr(categoryId))
  308. End Function
  309. Function CategoriesUrl()
  310. CategoriesUrl = "/categories"
  311. End Function
  312. Function CategoryNewUrl()
  313. CategoryNewUrl = "/categories/new"
  314. End Function
  315. Function CategoryEditUrl(ByVal categoryId)
  316. CategoryEditUrl = CategoryUrl(categoryId) & "/edit"
  317. End Function
  318. Function CategoryDeleteUrl(ByVal categoryId)
  319. CategoryDeleteUrl = CategoryUrl(categoryId) & "/delete"
  320. End Function
  321. Function PostUrl(ByVal slug)
  322. PostUrl = "/posts/" & Server.URLEncode(NormalizeSlug(slug))
  323. End Function
  324. Function PostPath(ByVal slug)
  325. PostPath = "/posts/" & NormalizeSlug(slug)
  326. End Function
  327. Function PostsUrl()
  328. PostsUrl = "/posts"
  329. End Function
  330. Function PostNewUrl()
  331. PostNewUrl = "/posts/new"
  332. End Function
  333. Function PostEditUrl(ByVal postId)
  334. PostEditUrl = "/posts/" & Server.URLEncode(CStr(postId)) & "/edit"
  335. End Function
  336. Function PostUpdateUrl(ByVal postId)
  337. PostUpdateUrl = "/posts/" & Server.URLEncode(CStr(postId))
  338. End Function
  339. Function PostDeleteUrl(ByVal postId)
  340. PostDeleteUrl = "/posts/" & Server.URLEncode(CStr(postId)) & "/delete"
  341. End Function
  342. Function AdminPostPublishUrl(ByVal postId)
  343. AdminPostPublishUrl = "/admin/posts/" & Server.URLEncode(CStr(postId)) & "/publish"
  344. End Function
  345. Function AdminPostUnpublishUrl(ByVal postId)
  346. AdminPostUnpublishUrl = "/admin/posts/" & Server.URLEncode(CStr(postId)) & "/unpublish"
  347. End Function
  348. Function AdminPostAIUrl(ByVal postId)
  349. AdminPostAIUrl = "/admin/posts/" & Server.URLEncode(CStr(postId)) & "/ai"
  350. End Function
  351. Function AdminCommentsUrl()
  352. AdminCommentsUrl = "/admin/comments"
  353. End Function
  354. Function AdminCommentApproveUrl(ByVal commentId)
  355. AdminCommentApproveUrl = "/admin/comments/" & Server.URLEncode(CStr(commentId)) & "/approve"
  356. End Function
  357. Function AdminCommentUnapproveUrl(ByVal commentId)
  358. AdminCommentUnapproveUrl = "/admin/comments/" & Server.URLEncode(CStr(commentId)) & "/unapprove"
  359. End Function
  360. Function AdminCommentDeleteUrl(ByVal commentId)
  361. AdminCommentDeleteUrl = "/admin/comments/" & Server.URLEncode(CStr(commentId)) & "/delete"
  362. End Function
  363. Function AdminAIPromptUrl()
  364. AdminAIPromptUrl = "/admin/ai-prompt"
  365. End Function
  366. Function AdminAIPromptUpdateUrl()
  367. AdminAIPromptUpdateUrl = "/admin/ai-prompt"
  368. End Function
  369. Function AdminUrl()
  370. AdminUrl = "/admin"
  371. End Function
  372. Function CommentsUrl()
  373. CommentsUrl = "/comments"
  374. End Function
  375. Function NormalizeSlug(ByVal slug)
  376. Dim current, previous
  377. current = Trim(CStr(slug))
  378. If Len(current) = 0 Then
  379. NormalizeSlug = ""
  380. Exit Function
  381. End If
  382. On Error Resume Next
  383. Do
  384. previous = current
  385. current = Server.URLDecode(current)
  386. If Err.Number <> 0 Then
  387. Err.Clear
  388. current = previous
  389. Exit Do
  390. End If
  391. Loop While current <> previous
  392. On Error GoTo 0
  393. current = Replace(current, "%252D", "-")
  394. current = Replace(current, "%252d", "-")
  395. current = Replace(current, "%2D", "-")
  396. current = Replace(current, "%2d", "-")
  397. current = Replace(current, "%25", "%")
  398. NormalizeSlug = current
  399. End Function
  400. '=======================================================================================================================
  401. ' Adapted from Tolerable library
  402. '=======================================================================================================================
  403. ' This subroutine allows us to ignore the difference
  404. ' between object and primitive assignments. This is
  405. ' essential for many parts of the engine.
  406. Public Sub Assign(ByRef var, ByVal val)
  407. If IsObject(val) Then
  408. Set var = val
  409. Else
  410. var = val
  411. End If
  412. End Sub
  413. ' This is similar to the ? : operator of other languages.
  414. ' Unfortunately, both the if_true and if_false "branches"
  415. ' will be evalauted before the condition is even checked. So,
  416. ' you'll only want to use this for simple expressions.
  417. Public Function Choice(ByVal cond, ByVal if_true, ByVal if_false)
  418. If cond Then
  419. Assign Choice, if_true
  420. Else
  421. Assign Choice, if_false
  422. End If
  423. End Function
  424. ' Allows single-quotes to be used in place of double-quotes.
  425. ' Basically, this is a cheap trick that can make it easier
  426. ' to specify Lambdas.
  427. Public Function Q(ByVal input)
  428. Q = Replace(input, "'", """")
  429. End Function
  430. Function SurroundString(inputVar)
  431. If VarType(inputVar) = vbString Then
  432. SurroundString = """" & inputVar & """"
  433. Else
  434. SurroundString = inputVar
  435. End If
  436. End Function
  437. Function SurroundStringInArray(arr)
  438. Dim i
  439. For i = LBound(arr) To UBound(arr)
  440. If IsString(arr(i)) Then
  441. arr(i) = """" & arr(i) & """"
  442. End If
  443. Next
  444. SurroundStringInArray = arr
  445. End Function
  446. '-----------------------------------------------------------------------------------------------------------------------
  447. 'Boolean type checkers
  448. 'Don't forget IsArray is built-in!
  449. Function IsString(value)
  450. IsString = Choice(typename(value) = "String", true, false)
  451. End Function
  452. Function IsDict(value)
  453. IsDict = Choice(typename(value) = "Dictionary", true, false)
  454. End Function
  455. Function IsRecordset(value)
  456. IsRecordset = Choice(typename(value) = "Recordset", true, false)
  457. End Function
  458. Function IsLinkedList(value)
  459. IsLinkedList = Choice(typename(value) = "LinkedList_Class", true, false)
  460. End Function
  461. Function IsArray(value)
  462. IsArray = Choice(typename(value) = "Variant()", true, false)
  463. End Function
  464. '--------------------------------------------------------------------
  465. ' Returns True when the named key is present in Session.Contents
  466. ' • Handles scalars (String, Integer, etc.), objects, Empty, and Null
  467. '--------------------------------------------------------------------
  468. Function SessionHasKey(keyName)
  469. 'Loop over the existing keys—Session.Contents is like a dictionary
  470. Dim k
  471. For Each k In Session.Contents
  472. If StrComp(k, keyName, vbTextCompare) = 0 Then
  473. SessionHasKey = True
  474. Exit Function
  475. End If
  476. Next
  477. SessionHasKey = False 'not found
  478. End Function
  479. Function RenderObjectsAsTable(arr,boolUseTabulator)
  480. Dim html, propNames, i, j, obj, val, pkName, isPk
  481. If IsEmpty(arr) Or Not IsArray(arr) Then
  482. RenderObjectsAsTable = "<!-- no data -->"
  483. Exit Function
  484. End If
  485. Set obj = arr(0)
  486. On Error Resume Next
  487. propNames = obj.Properties
  488. pkName = obj.PrimaryKey
  489. On Error GoTo 0
  490. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  491. RenderObjectsAsTable = "<!-- missing properties or primary key -->"
  492. Exit Function
  493. End If
  494. html = "<div class='table-wrapper'>" & vbCrLf
  495. html = html & "<table class='pobo-table' id='pobo-table'>" & vbCrLf
  496. html = html & " <thead><tr>" & vbCrLf
  497. For i = 0 To UBound(propNames)
  498. html = html & " <th>" & Server.HTMLEncode(propNames(i)) & "</th>" & vbCrLf
  499. Next
  500. html = html & " </tr></thead>" & vbCrLf
  501. html = html & " <tbody>" & vbCrLf
  502. For j = 0 To UBound(arr)
  503. Set obj = arr(j)
  504. html = html & " <tr>" & vbCrLf
  505. For i = 0 To UBound(propNames)
  506. val = GetDynamicProperty(obj, propNames(i))
  507. isPk = (StrComp(propNames(i), pkName, vbTextCompare) = 0)
  508. If IsNull(val) Or IsEmpty(val) Then
  509. val = "&nbsp;"
  510. ElseIf IsDate(val) Then
  511. val = FormatDateTime(val, vbShortDate)
  512. ElseIf VarType(val) = vbBoolean Then
  513. val = IIf(val, "True", "False")
  514. Else
  515. val = CStr(val)
  516. Dim maxLen : maxLen = CInt(GetAppSetting("TableCellMaxLength"))
  517. If maxLen <= 0 Then maxLen = 90
  518. If Len(val) > maxLen Then
  519. val = Left(val, maxLen - 3) & "..."
  520. End If
  521. val = Server.HTMLEncode(val)
  522. End If
  523. If isPk and boolUseTabulator = False Then
  524. val = "<a href=""" & obj.Tablename & "/edit/" & GetDynamicProperty(obj, pkName) & """ class=""table-link"">" & val & "</a>"
  525. End If
  526. html = html & " <td>" & val & "</td>" & vbCrLf
  527. Next
  528. html = html & " </tr>" & vbCrLf
  529. Next
  530. html = html & " </tbody>" & vbCrLf & "</table>" & vbCrLf & "</div>"
  531. RenderObjectsAsTable = html
  532. End Function
  533. Function RenderFormFromObject(obj)
  534. Dim html, propNames, i, name, val, inputType
  535. Dim pkName, tableName, checkedAttr
  536. On Error Resume Next
  537. propNames = obj.Properties
  538. pkName = obj.PrimaryKey
  539. tableName = obj.TableName
  540. On Error GoTo 0
  541. If IsEmpty(propNames) Or Len(pkName) = 0 Then
  542. RenderFormFromObject = "<!-- Invalid object -->"
  543. Exit Function
  544. End If
  545. html = "<form method='post' action='/" & tableName & "/save' class='article-content'>" & vbCrLf
  546. For i = 0 To UBound(propNames)
  547. name = propNames(i)
  548. val = GetDynamicProperty(obj, name)
  549. ' Handle nulls
  550. If IsNull(val) Then val = ""
  551. ' Primary key → hidden input
  552. If StrComp(name, pkName, vbTextCompare) = 0 Then
  553. html = html & " <input type='hidden' name='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  554. 'Continue For
  555. End If
  556. html = html & " <div class='form-group'>" & vbCrLf
  557. html = html & " <label for='" & name & "'>" & name & "</label>" & vbCrLf
  558. Select Case True
  559. Case VarType(val) = vbBoolean
  560. checkedAttr = ""
  561. If val = True Then checkedAttr = " checked"
  562. html = html & " <input type='checkbox' class='form-check-input' name='" & name & "' id='" & name & "' value='true'" & checkedAttr & " />" & vbCrLf
  563. Case IsDate(val)
  564. html = html & " <input type='date' class='form-control' name='" & name & "' id='" & name & "' value='" & FormatDateForInput(val) & "' />" & vbCrLf
  565. Case IsNumeric(val)
  566. html = html & " <input type='number' class='form-control' name='" & name & "' id='" & name & "' value='" & val & "' />" & vbCrLf
  567. Case Len(val) > CInt(GetAppSetting("FormTextareaThreshold"))
  568. html = html & " <textarea class='form-control' name='" & name & "' id='" & name & "' rows='6'>" & Server.HTMLEncode(val) & "</textarea>" & vbCrLf
  569. Case Else
  570. html = html & " <input type='text' class='form-control' name='" & name & "' id='" & name & "' value='" & Server.HTMLEncode(val) & "' />" & vbCrLf
  571. End Select
  572. html = html & " </div>" & vbCrLf
  573. Next
  574. html = html & " <button type='submit' class='btn btn-primary btn-lg'>Save</button>" & vbCrLf
  575. html = html & "</form>" & vbCrLf
  576. RenderFormFromObject = html
  577. End Function
  578. Function GetDynamicProperty(obj, propName)
  579. On Error Resume Next
  580. Dim result
  581. Execute "result = obj." & propName
  582. If Err.Number <> 0 Then
  583. result = ""
  584. Err.Clear
  585. End If
  586. GetDynamicProperty = result
  587. On Error GoTo 0
  588. End Function
  589. Function FormatDateForInput(val)
  590. If IsDate(val) Then
  591. Dim yyyy, mm, dd
  592. yyyy = Year(val)
  593. mm = Right("0" & Month(val), 2)
  594. dd = Right("0" & Day(val), 2)
  595. FormatDateForInput = yyyy & "-" & mm & "-" & dd
  596. Else
  597. FormatDateForInput = ""
  598. End If
  599. End Function
  600. '-------------------------------------------------------------
  601. ' Returns obj.<propName> for any public VBScript class property
  602. '-------------------------------------------------------------
  603. Function GetObjProp(o, pName)
  604. Dim tmp
  605. ' Build a tiny statement like: tmp = o.UserID
  606. Execute "tmp = o." & pName
  607. GetObjProp = tmp
  608. End Function
  609. Function GenerateSlug(title)
  610. Dim slug
  611. slug = LCase(title) ' Convert to lowercase
  612. slug = Replace(slug, "&", "and") ' Replace ampersands
  613. slug = Replace(slug, "'", "") ' Remove apostrophes
  614. slug = Replace(slug, """", "") ' Remove quotes
  615. slug = Replace(slug, "–", "-") ' Replace en dash
  616. slug = Replace(slug, "—", "-") ' Replace em dash
  617. slug = Replace(slug, "/", "-") ' Replace slashes
  618. slug = Replace(slug, "\", "-") ' Replace backslashes
  619. ' Remove all non-alphanumeric and non-hyphen/space characters
  620. Dim i, ch, clean
  621. clean = ""
  622. For i = 1 To Len(slug)
  623. ch = Mid(slug, i, 1)
  624. If (ch >= "a" And ch <= "z") Or (ch >= "0" And ch <= "9") Or ch = " " Or ch = "-" Then
  625. clean = clean & ch
  626. End If
  627. Next
  628. ' Replace multiple spaces or hyphens with single hyphen
  629. Do While InStr(clean, " ") > 0
  630. clean = Replace(clean, " ", " ")
  631. Loop
  632. clean = Replace(clean, " ", "-")
  633. Do While InStr(clean, "--") > 0
  634. clean = Replace(clean, "--", "-")
  635. Loop
  636. ' Trim leading/trailing hyphens
  637. Do While Left(clean, 1) = "-"
  638. clean = Mid(clean, 2)
  639. Loop
  640. Do While Right(clean, 1) = "-"
  641. clean = Left(clean, Len(clean) - 1)
  642. Loop
  643. GenerateSlug = clean
  644. End Function
  645. Function GetRawJsonFromRequest()
  646. Dim stream, rawJson
  647. Set stream = Server.CreateObject("ADODB.Stream")
  648. stream.Type = 1 ' adTypeBinary
  649. stream.Open
  650. stream.Write Request.BinaryRead(Request.TotalBytes)
  651. stream.Position = 0
  652. stream.Type = 2 ' adTypeText
  653. stream.Charset = "utf-8"
  654. rawJson = stream.ReadText
  655. stream.Close
  656. Set stream = Nothing
  657. GetRawJsonFromRequest = rawJson
  658. End Function
  659. Function Active(controllerName)
  660. On Error Resume Next
  661. If Replace(Lcase(router.Resolve(Request.ServerVariables("REQUEST_METHOD"), TrimQueryParams(Request.ServerVariables("HTTP_X_ORIGINAL_URL")))(0)),"controller","") = LCase(controllerName) Then
  662. Active = "active"
  663. Else
  664. Active = ""
  665. End If
  666. On Error GoTo 0
  667. End Function
  668. '====================================================================
  669. ' FormatDateForSql
  670. ' Converts a VBScript Date to a SQL Server-compatible string
  671. ' Output: 'YYYY-MM-DD HH:MM:SS'
  672. '====================================================================
  673. Function FormatDateForSql(vbDate)
  674. If IsNull(vbDate) Or vbDate = "" Then
  675. FormatDateForSql = "NULL"
  676. Exit Function
  677. End If
  678. ' Ensure vbDate is a valid date
  679. If Not IsDate(vbDate) Then
  680. Err.Raise vbObjectError + 1000, "FormatDateForSql", "Invalid date: " & vbDate
  681. End If
  682. Dim yyyy, mm, dd, hh, nn, ss
  683. yyyy = Year(vbDate)
  684. mm = Right("0" & Month(vbDate), 2)
  685. dd = Right("0" & Day(vbDate), 2)
  686. hh = Right("0" & Hour(vbDate), 2)
  687. nn = Right("0" & Minute(vbDate), 2)
  688. ss = Right("0" & Second(vbDate), 2)
  689. ' Construct SQL Server datetime literal
  690. FormatDateForSql = "'" & yyyy & "-" & mm & "-" & dd & " " & hh & ":" & nn & ":" & ss & "'"
  691. End Function
  692. %>

Powered by TurnKey Linux.