ASP Classic blog framework - BrainOrdure
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.

348 lines
12KB

  1. <%
  2. Class AdminController_Class
  3. Private m_useLayout
  4. Private m_title
  5. Private Sub Class_Initialize()
  6. m_useLayout = True
  7. m_title = "Admin"
  8. End Sub
  9. Public Property Get useLayout
  10. useLayout = m_useLayout
  11. End Property
  12. Public Property Let useLayout(v)
  13. m_useLayout = v
  14. End Property
  15. Public Property Get Title
  16. Title = m_title
  17. End Property
  18. Public Property Let Title(v)
  19. m_title = v
  20. End Property
  21. '---------------------------------------------------------------
  22. ' Action: Index
  23. '---------------------------------------------------------------
  24. Public Sub Index()
  25. m_title = "Admin Dashboard"
  26. %>
  27. <!--#include file="../views/Admin/index.asp" -->
  28. <%
  29. End Sub
  30. '---------------------------------------------------------------
  31. ' Action: Posts
  32. '---------------------------------------------------------------
  33. Public Sub Posts()
  34. m_title = "Manage Posts"
  35. Dim posts
  36. Set posts = PostsRepository().FindAllWhere(Empty, "CreatedDate DESC", 0, 0)
  37. %>
  38. <!--#include file="../views/Admin/posts.asp" -->
  39. <%
  40. End Sub
  41. '---------------------------------------------------------------
  42. ' Action: Categories
  43. '---------------------------------------------------------------
  44. Public Sub Categories()
  45. m_title = "Manage Categories"
  46. Dim categories
  47. Set categories = CategoriesRepository().FindAll
  48. %>
  49. <!--#include file="../views/Admin/categories.asp" -->
  50. <%
  51. End Sub
  52. '---------------------------------------------------------------
  53. ' Action: Comments
  54. '---------------------------------------------------------------
  55. Public Sub Comments()
  56. m_title = "Manage Comments"
  57. Dim comments
  58. Set comments = CommentsRepository().Find(Empty, "CreatedDate")
  59. %>
  60. <!--#include file="../views/Admin/comments.asp" -->
  61. <%
  62. End Sub
  63. '---------------------------------------------------------------
  64. ' Action: AIPrompt
  65. '---------------------------------------------------------------
  66. Public Sub AIPrompt()
  67. m_title = "AI Prompt Settings"
  68. Dim promptTemplate : promptTemplate = GetGenerationPromptTemplate()
  69. %>
  70. <!--#include file="../views/Admin/ai-prompt.asp" -->
  71. <%
  72. End Sub
  73. '---------------------------------------------------------------
  74. ' Action: UpdateAIPrompt
  75. '---------------------------------------------------------------
  76. Public Sub UpdateAIPrompt()
  77. Dim promptTemplate : promptTemplate = Trim(Request.Form("PromptTemplate"))
  78. If Len(promptTemplate) = 0 Then
  79. Flash().AddError "Prompt template cannot be empty."
  80. Response.Redirect "/admin/ai-prompt"
  81. Exit Sub
  82. End If
  83. On Error Resume Next
  84. UpdateAppSetting "AbacusGenerationPrompt", promptTemplate
  85. If Err.Number <> 0 Then
  86. Flash().AddError "Unable to save prompt template: " & Err.Description
  87. Err.Clear
  88. On Error GoTo 0
  89. Response.Redirect "/admin/ai-prompt"
  90. Exit Sub
  91. End If
  92. On Error GoTo 0
  93. Flash().Success = "AI prompt template saved."
  94. Response.Redirect "/admin/ai-prompt"
  95. End Sub
  96. '---------------------------------------------------------------
  97. ' Action: PublishPost
  98. '---------------------------------------------------------------
  99. Public Sub PublishPost(ByVal id)
  100. Dim post
  101. On Error Resume Next
  102. Set post = PostsRepository().FindByID(id)
  103. If Err.Number <> 0 Then
  104. Err.Clear
  105. On Error GoTo 0
  106. Flash().AddError "Post not found."
  107. Response.Redirect "/admin/posts"
  108. Exit Sub
  109. End If
  110. On Error GoTo 0
  111. post.IsPublished = 1
  112. If Not IsDate(post.PublishedDate) Or CDate(post.PublishedDate) <= #1/1/1970# Then
  113. post.PublishedDate = Now()
  114. End If
  115. post.UpdatedDate = Now()
  116. PostsRepository().Update post
  117. Flash().Success = "Post published."
  118. Response.Redirect "/admin/posts"
  119. End Sub
  120. '---------------------------------------------------------------
  121. ' Action: UnpublishPost
  122. '---------------------------------------------------------------
  123. Public Sub UnpublishPost(ByVal id)
  124. Dim post
  125. On Error Resume Next
  126. Set post = PostsRepository().FindByID(id)
  127. If Err.Number <> 0 Then
  128. Err.Clear
  129. On Error GoTo 0
  130. Flash().AddError "Post not found."
  131. Response.Redirect "/admin/posts"
  132. Exit Sub
  133. End If
  134. On Error GoTo 0
  135. post.IsPublished = 0
  136. post.UpdatedDate = Now()
  137. PostsRepository().Update post
  138. Flash().Success = "Post unpublished."
  139. Response.Redirect "/admin/posts"
  140. End Sub
  141. '---------------------------------------------------------------
  142. ' Action: ApproveComment
  143. '---------------------------------------------------------------
  144. Public Sub ApproveComment(ByVal id)
  145. UpdateCommentApproval id, 1, "Comment approved."
  146. End Sub
  147. '---------------------------------------------------------------
  148. ' Action: UnapproveComment
  149. '---------------------------------------------------------------
  150. Public Sub UnapproveComment(ByVal id)
  151. UpdateCommentApproval id, 0, "Comment unapproved."
  152. End Sub
  153. '---------------------------------------------------------------
  154. ' Action: DeleteComment
  155. '---------------------------------------------------------------
  156. Public Sub DeleteComment(ByVal id)
  157. Dim comment
  158. On Error Resume Next
  159. Set comment = CommentsRepository().FindByID(id)
  160. If Err.Number <> 0 Then
  161. Err.Clear
  162. On Error GoTo 0
  163. Flash().AddError "Comment not found."
  164. Response.Redirect "/admin/comments"
  165. Exit Sub
  166. End If
  167. On Error GoTo 0
  168. CommentsRepository().Delete id
  169. Flash().Success = "Comment deleted."
  170. Response.Redirect "/admin/comments"
  171. End Sub
  172. '---------------------------------------------------------------
  173. ' Action: GenerateAIContent
  174. '---------------------------------------------------------------
  175. Public Sub GenerateAIContent(ByVal id)
  176. Dim post
  177. On Error Resume Next
  178. Set post = PostsRepository().FindByID(id)
  179. If Err.Number <> 0 Then
  180. Err.Clear
  181. On Error GoTo 0
  182. Flash().AddError "Post not found."
  183. Response.Redirect "/admin/posts"
  184. Exit Sub
  185. End If
  186. On Error GoTo 0
  187. Dim generatedSummary, generatedBody
  188. On Error Resume Next
  189. GeneratePostContentFromAI post, generatedSummary, generatedBody
  190. If Err.Number <> 0 Then
  191. Flash().AddError "AI content generation failed: " & Err.Description
  192. Err.Clear
  193. On Error GoTo 0
  194. Response.Redirect PostEditUrl(post.PostID)
  195. Exit Sub
  196. End If
  197. On Error GoTo 0
  198. If Len(Trim(generatedSummary)) = 0 Or Len(Trim(generatedBody)) = 0 Then
  199. Flash().AddError "AI content generation returned empty content."
  200. Response.Redirect PostEditUrl(post.PostID)
  201. Exit Sub
  202. End If
  203. post.Summary = generatedSummary
  204. post.Body = generatedBody
  205. post.UpdatedDate = Now()
  206. PostsRepository().Update post
  207. Flash().Success = "AI content generated."
  208. Response.Redirect PostEditUrl(post.PostID)
  209. End Sub
  210. Private Sub GeneratePostContentFromAI(ByRef post, ByRef generatedSummary, ByRef generatedBody)
  211. Dim apiKey : apiKey = Trim(CStr(GetSecureSetting("AbacusApiKey", "ABACUS_API_KEY")))
  212. If Len(apiKey) = 0 Then
  213. Err.Raise 1, "AdminController.GeneratePostContentFromAI", "Abacus API key is not configured."
  214. End If
  215. Dim baseUrl : baseUrl = Trim(CStr(GetAppSetting("AbacusApiBaseUrl")))
  216. If Len(baseUrl) = 0 Or LCase(baseUrl) = "nothing" Then baseUrl = "https://routellm.abacus.ai/v1"
  217. If Right(baseUrl, 1) = "/" Then baseUrl = Left(baseUrl, Len(baseUrl) - 1)
  218. Dim modelName : modelName = Trim(CStr(GetAppSetting("AbacusModel")))
  219. If Len(modelName) = 0 Or LCase(modelName) = "nothing" Then modelName = "route-llm"
  220. Dim systemPrompt, userPrompt, payload, responseText, parsed, choices, choice, message, content, contentJson
  221. systemPrompt = "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."
  222. userPrompt = BuildGenerationPrompt(post)
  223. payload = "{""model"":""" & JsonEscape(modelName) & """,""messages"":[{""role"":""system"",""content"":""" & JsonEscape(systemPrompt) & """},{""role"":""user"",""content"":""" & JsonEscape(userPrompt) & """}],""temperature"":0.7}"
  224. responseText = HttpPostJson(baseUrl & "/chat/completions", apiKey, payload)
  225. Set parsed = json()
  226. parsed.loadJSON responseText
  227. If Not parsed.data.Exists("choices") Then
  228. Err.Raise 1, "AdminController.GeneratePostContentFromAI", "Abacus API response did not include choices."
  229. End If
  230. Set choices = parsed.data.Item("choices")
  231. If choices.Count = 0 Then
  232. Err.Raise 1, "AdminController.GeneratePostContentFromAI", "Abacus API returned no choices."
  233. End If
  234. Set choice = choices.Item(0)
  235. If Not choice.Exists("message") Then
  236. Err.Raise 1, "AdminController.GeneratePostContentFromAI", "Abacus API response did not include a message."
  237. End If
  238. Set message = choice.Item("message")
  239. If Not message.Exists("content") Then
  240. Err.Raise 1, "AdminController.GeneratePostContentFromAI", "Abacus API response did not include content."
  241. End If
  242. content = Trim(CStr(message.Item("content")))
  243. contentJson = ExtractJsonObject(content)
  244. Set parsed = json()
  245. parsed.loadJSON contentJson
  246. If parsed.data.Exists("summary") Then generatedSummary = Trim(CStr(parsed.data.Item("summary")))
  247. If parsed.data.Exists("body") Then generatedBody = Trim(CStr(parsed.data.Item("body")))
  248. End Sub
  249. Private Sub UpdateCommentApproval(ByVal id, ByVal isApproved, ByVal successMessage)
  250. Dim comment
  251. On Error Resume Next
  252. Set comment = CommentsRepository().FindByID(id)
  253. If Err.Number <> 0 Then
  254. Err.Clear
  255. On Error GoTo 0
  256. Flash().AddError "Comment not found."
  257. Response.Redirect "/admin/comments"
  258. Exit Sub
  259. End If
  260. On Error GoTo 0
  261. comment.IsApproved = isApproved
  262. CommentsRepository().Update comment
  263. Flash().Success = successMessage
  264. Response.Redirect "/admin/comments"
  265. End Sub
  266. Private Function HttpPostJson(ByVal url, ByVal apiKey, ByVal payload)
  267. Dim http
  268. Set http = Server.CreateObject("Msxml2.ServerXMLHTTP.6.0")
  269. http.setTimeouts 10000, 10000, 10000, 30000
  270. http.open "POST", url, False
  271. http.setRequestHeader "Content-Type", "application/json"
  272. http.setRequestHeader "Authorization", "Bearer " & apiKey
  273. http.send payload
  274. If http.status < 200 Or http.status >= 300 Then
  275. Err.Raise IIf(http.status > 0, http.status, 1), "AdminController.HttpPostJson", "Abacus API returned HTTP " & http.status & ": " & Left(CStr(http.responseText), 500)
  276. End If
  277. HttpPostJson = http.responseText
  278. End Function
  279. Private Function ExtractJsonObject(ByVal text)
  280. Dim startPos, endPos
  281. startPos = InStr(text, "{")
  282. endPos = InStrRev(text, "}")
  283. If startPos > 0 And endPos > startPos Then
  284. ExtractJsonObject = Mid(text, startPos, endPos - startPos + 1)
  285. Else
  286. ExtractJsonObject = text
  287. End If
  288. End Function
  289. End Class
  290. Dim AdminController_Class__Singleton
  291. Function AdminController()
  292. If IsEmpty(AdminController_Class__Singleton) Then
  293. Set AdminController_Class__Singleton = New AdminController_Class
  294. End If
  295. Set AdminController = AdminController_Class__Singleton
  296. End Function
  297. %>

Powered by TurnKey Linux.