1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
local orbit = require "orbit"
local toycms = ...
-- Admin interface
function admin(web, section_id)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/admin") }))
else
local params = {}
if tonumber(section_id) then
params.section = models.section:find(tonumber(section_id))
end
return admin_layout(web, render_admin(web, params))
end
end
toycms:dispatch_get(admin, "/admin", "/admin/(%d+)")
function login_get(web)
return admin_layout(web, render_login(web, web.input))
end
function login_post(web)
local login = web.input.login
local password = web.input.password
local user = models.user:find_by_login{ login }
if web:empty_param("link_to") then
web.input.link_to = web:link("/")
end
if user then
if password == user.password then
web:set_cookie("user_id", user.id)
web:set_cookie("password", user.password)
return web:redirect(web.input.link_to)
else
return web:redirect(web:link("/login", { login = login,
link_to = web.input.link_to,
not_match = "1" }))
end
else
return web:redirect(web:link("/login", { login = login,
link_to = web.input.link_to,
not_found = "1" }))
end
end
toycms:dispatch_get(login_get, "/login")
toycms:dispatch_post(login_post, "/login")
function add_user_get(web)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/adduser") }))
else
return admin_layout(web, render_add_user(web, web.input))
end
end
function add_user_post(web)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/adduser") }))
else
local errors = {}
if web:empty_param("login") then
errors.login = strings.blank_user
end
if web:empty_param("password1") then
errors.password = strings.blank_password
end
if web.input.password1 ~= web.input.password2 then
errors.password = strings.password_mismatch
end
if web:empty_param("name") then
errors.name = strings.blank_name
end
if not next(errors) then
local user = models.user:new()
user.login = web.input.login
user.password = web.input.password1
user.name = web.input.name
user:save()
return web:redirect(web:link("/admin"))
else
for k, v in pairs(errors) do web.input["error_" .. k] = v end
return web:redirect(web:link("/adduser", web.input))
end
end
end
toycms:dispatch_get(add_user_get, "/adduser")
toycms:dispatch_post(add_user_post, "/adduser")
function edit_section_get(web, section_id)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/editsection") }))
else
section_id = tonumber(section_id)
if section_id then
web.input.section = models.section:find_by_id{ section_id }
else
web.input.section = models.section:new()
end
return admin_layout(web, render_edit_section(web, web.input))
end
end
function edit_section_post(web, section_id)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/editsection") }))
else
section_id = tonumber(section_id)
local errors = {}
if web:empty_param("title") then
errors.title = strings.blank_title
end
if not next(errors) then
local section
if section_id then
section = models.section:find_by_id{ section_id }
else
section = models.section:new()
end
section.title = web.input.title
section.description = web.input.description
section.tag = web.input.tag
section:save()
cache:nuke()
return web:redirect(web:link("/editsection/" .. section.id))
else
for k, v in pairs(errors) do web.input["error_" .. k] = v end
if section_id then
return web:redirect(web:link("/editsection/" .. section_id, web.input))
else
return web:redirect(web:link("/editsection", web.input))
end
end
end
end
toycms:dispatch_get(edit_section_get, "/editsection", "/editsection/(%d+)")
toycms:dispatch_post(edit_section_post, "/editsection", "/editsection/(%d+)")
function delete_section(web, section_id)
if not check_user(web) then
return web:redirect(web:link("/login",
{ link_to = web:link("/editsection/" .. section_id) }))
else
section_id = tonumber(section_id)
local section = models.section:find(section_id)
if section then
section:delete()
cache:nuke()
return web:redirect(web:link("/admin"))
end
end
end
toycms:dispatch_post(delete_section, "/deletesection/(%d+)")
function delete_post(web, post_id)
if not check_user(web) then
return web:redirect(web:link("/login",
{ link_to = web:link("/editpost/" .. post_id) }))
else
post_id = tonumber(post_id)
local post = models.post:find(post_id)
if post then
post:delete()
cache:nuke()
return web:redirect(web:link("/admin"))
end
end
end
toycms:dispatch_post(delete_post, "/deletepost/(%d+)")
function edit_post_get(web, post_id)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/editpost") }))
else
post_id = tonumber(post_id)
if post_id then
web.input.post = models.post:find_by_id{ post_id }
else
web.input.post = models.post:new()
end
web.input.sections = models.section:find_all()
return admin_layout(web, render_edit_post(web, web.input))
end
end
function edit_post_post(web, post_id)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/editpost") }))
else
post_id = tonumber(post_id)
local errors = {}
if web:empty_param("title") then
errors.title = strings.blank_title
end
if not next(errors) then
local post
if post_id then
post = models.post:find_by_id{ post_id }
else
post = models.post:new()
end
post.title = web.input.title
post.abstract = web.input.abstract
post.image = web.input.image
if web:empty_param("published_at") then
post.published_at = nil
else
local day, month, year, hour, minute =
string.match(web.input.published_at,
"(%d+)-(%d+)-(%d+) (%d+):(%d+)")
post.published_at = os.time({ day = day, month = month,
year = year, hour = hour,
min = minute })
end
post.body = web.input.body
post.section_id = tonumber(web.input.section_id)
post.published = (not web:empty_param("published"))
post.external_url = web.input.external_url
post.in_home = (not web:empty_param("in_home"))
post.user_id = check_user(web).id
post.comment_status = web.input.comment_status
post:save()
cache:nuke()
return web:redirect(web:link("/editpost/" .. post.id))
else
for k, v in pairs(errors) do web.input["error_" .. k] = v end
if post_id then
return web:redirect(web:link("/editpost/" .. post_id, web.input))
else
return web:redirect(web:link("/editpost", web.input))
end
end
end
end
toycms:dispatch_get(edit_post_get, "/editpost", "/editpost/(%d+)")
toycms:dispatch_post(edit_post_post, "/editpost", "/editpost/(%d+)")
function manage_comments(web)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/comments") }))
else
local params = {}
local post_model = models.post
params.for_mod = models.comment:find_all_by_approved{ false,
order = "created_at desc", inject = { model = post_model,
fields = { "title" } } }
params.approved = models.comment:find_all_by_approved{ true,
order = "created_at desc", inject = { model = post_model,
fields = { "title" } } }
return admin_layout(web, render_manage_comments(web, params))
end
end
toycms:dispatch_get(manage_comments, "/comments")
function approve_comment(web, id)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/comments#" ..
id) }))
else
local comment = models.comment:find(tonumber(id))
if comment then
comment.approved = true
comment:save()
local post = models.post:find(comment.post_id)
post.n_comments = (post.n_comments or 0) + 1
post:save()
cache:invalidate("/", "/xml", "/section/" .. post.section_id,
"/section/" .. post.section_id .. "/xml",
"/post/" .. post.id, "/post/" .. post.id .. "/xml",
"/archive/" ..
string.format("%Y/%m", post.published_at))
return web:redirect(web:link("/comments#" .. id))
end
end
end
toycms:dispatch_post(approve_comment, "/comment/(%d+)/approve")
function delete_comment(web, id)
if not check_user(web) then
return web:redirect(web:link("/login", { link_to = web:link("/comments#" ..
id) }))
else
local comment = models.comment:find(tonumber(id))
if comment then
if comment.approved then
local post = models.post:find(comment.post_id)
post.n_comments = post.n_comments - 1
post:save()
cache:invalidate("/", "/xml", "/section/" .. post.section_id,
"/section/" .. post.section_id .. "/xml",
"/post/" .. post.id, "/post/" .. post.id .. "/xml",
"/archive/" ..
string.format("%Y/%m", post.published_at))
end
comment:delete()
return web:redirect(web:link("/comments"))
end
end
end
toycms:dispatch_post(delete_comment, "/comment/(%d+)/delete")
toycms:dispatch_static("/admin_style%.css")
local function error_message(msg)
return "<span style = \"color: red\">" .. msg .. "</span>"
end
function admin_layout(web, inner_html)
return html{
head{
title"ToyCMS Admin",
meta{ ["http-equiv"] = "Content-Type",
content = "text/html; charset=utf-8" },
link{ rel = 'stylesheet', type = 'text/css',
href = web:static_link('/admin_style.css'), media = 'screen' }
},
body{
div{ id = "container",
div{ id = "header", title = "sitename", "ToyCMS Admin" },
div{ id = "mainnav",
ul {
li{ a{ href = web:link("/admin"), strings.admin_home } },
li{ a{ href = web:link("/adduser"), strings.new_user } },
li{ a{ href = web:link("/editsection"), strings.new_section } },
li{ a{ href = web:link("/editpost"), strings.new_post } },
li{ a{ href = web:link("/comments"), strings.manage_comments } },
}
},
div{ id = "menu",
_admin_menu(web, args)
},
div{ id = "contents", inner_html },
div{ id = "footer", "Copyright 2007 Fabio Mascarenhas" }
}
}
}
end
function _admin_menu(web)
local res = {}
local user = check_user(web)
if user then
res[#res + 1] = ul{ li{ strings.logged_as,
(user.name or user.login) } }
res[#res + 1] = h3(strings.sections)
local section_list = {}
local sections = models.section:find_all()
for _, section in ipairs(sections) do
section_list[#section_list + 1] =
li{ a{ href=web:link("/admin/" .. section.id), section.title } }
end
res[#res + 1] = ul(table.concat(section_list,"\n"))
end
return table.concat(res, "\n")
end
function render_admin(web, params)
local section_list
local sections = models.section:find_all({ order = "id asc" })
if params.section then
local section = params.section
local res_section = {}
res_section[#res_section + 1] = "<div class=\"blogentry\">\n"
res_section[#res_section + 1] = h2(strings.section .. ": " ..
a{ href = web:link("/editsection/" .. section.id),
section.title })
local posts = models.post:find_all_by_section_id{ section.id,
order = "published_at desc" }
res_section[#res_section + 1] = "<p>"
for _, post in ipairs(posts) do
local in_home, published = "", ""
if post.in_home then in_home = " [HOME]" end
if post.published then published = " [P]" end
res_section[#res_section + 1] = a{ href =
web:link("/editpost/" .. post.id), post.title } .. in_home ..
published .. br()
end
res_section[#res_section + 1] = "</p>"
res_section[#res_section + 1] =
p{ a.button{ href = web:link("/editpost?section_id=" .. section.id),
button{ strings.new_post } } }
res_section[#res_section + 1] = "</div>\n"
section_list = table.concat(res_section, "\n")
elseif next(sections) then
local res_section = {}
for _, section in ipairs(sections) do
res_section[#res_section + 1] = "<div class=\"blogentry\">\n"
res_section[#res_section + 1] = h2(strings.section .. ": " ..
a{ href = web:link("/editsection/" .. section.id),
section.title })
local posts = models.post:find_all_by_section_id{ section.id,
order = "published_at desc" }
res_section[#res_section + 1] = "<p>"
for _, post in ipairs(posts) do
local in_home, published = "", ""
if post.in_home then in_home = " [HOME]" end
if post.published then published = " [P]" end
res_section[#res_section + 1] = a{ href =
web:link("/editpost/" .. post.id), post.title } .. in_home ..
published .. br()
end
res_section[#res_section + 1] = "</p>"
res_section[#res_section + 1] =
p{ a.button { href = web:link("/editpost?section_id=" .. section.id),
button{ strings.new_post } } }
res_section[#res_section + 1] = "</div>\n"
end
section_list = table.concat(res_section, "\n")
else
section_list = strings.no_sections
end
return div(section_list)
end
function render_login(web, params)
local res = {}
local err_msg = ""
if params.not_match then
err_msg = p{ error_message(strings.password_not_match) }
elseif params.not_found then
err_msg = p{ error_message(strings.user_not_found) }
end
res[#res + 1] = h2"Login"
res[#res + 1] = err_msg
res[#res + 1] = form{
method = "post",
action = web:link("/login"),
input{ type = "hidden", name = "link_to", value = params.link_to },
p{
strings.login, br(), input{ type = "text", name = "login",
value = params.login or "" },
br(), br(),
strings.password, br(), input{ type = "password", name = "password" },
br(), br(),
input{ type = "submit", value = strings.login_button }
}
}
return div(res)
end
function render_add_user(web, params)
local error_login, error_password, error_name = "", "", ""
if params.error_login then
error_login = error_message(params.error_login) .. br()
end
if params.error_password then
error_password = error_message(params.error_password) .. br()
end
if params.error_name then
error_name = error_message(params.error_name) .. br()
end
return div{
h2(strings.new_user),
form{
method = "post",
action = web:link("/adduser"),
p{
strings.login, br(), error_login, input{ type = "text",
name = "login", value = params.login }, br(), br(),
strings.password, br(), error_password, input{ type = "password",
name = "password1" }, br(),
input{ type = "password", name = "password2" }, br(), br(),
strings.name, br(), error_name, input{ type = "text",
name = "name", value = params.name }, br(), br(),
input{ type = "submit", value = strings.add }
}
},
}
end
function render_edit_section(web, params)
local error_title = ""
if params.error_title then
error_title = error_message(params.error_title) .. br()
end
local page_header, button_text
if not params.section.id then
page_header = strings.new_section
button_text = strings.add
else
page_header = strings.edit_section
button_text = strings.edit
end
local action
local delete
if params.section.id then
action = web:link("/editsection/" .. params.section.id)
delete = form{ method = "post", action = web:link("/deletesection/" ..
params.section.id),
input{ type = "submit", value = strings.delete } }
else
action = web:link("/editsection")
end
return div{
h2(page_header),
form{
method = "post",
action = action,
p{
strings.title, br(), error_title, input{ type = "text",
name = "title", value = params.title or params.section.title },
br(), br(),
strings.description, br(), textarea{ name = "description",
rows = "5", cols = "40", params.description or
params.section.description }, br(), br(),
strings.tag, br(), input{ type = "text", name = "tag",
value = params.tag or params.section.tag }, br(), br(),
input{ type = "submit", value = button_text }
}
}, delete
}
end
function render_edit_post(web, params)
local error_title = ""
if params.error_title then
error_title = error_message(params.error_title) .. br()
end
local page_header, button_text
if not params.post.id then
page_header = strings.new_post
button_text = strings.add
else
page_header = strings.edit_post
button_text = strings.edit
end
local action
local delete
if params.post.id then
action = web:link("/editpost/" .. params.post.id)
delete = form{ method = "post", action = web:link("/deletepost/" ..
params.post.id),
input{ type = "submit", value = strings.delete } }
else
action = web:link("/editpost")
end
local sections = {}
for _, section in pairs(params.sections) do
sections[#sections + 1] = option{ value = section.id,
selected = (section.id == (tonumber(params.section_id) or
params.post.section_id)) or nil,
section.title }
end
sections = "<select name=\"section_id\">" .. table.concat(sections, "\n") ..
"</select>"
local comment_status = {}
for status, text in pairs({ closed = strings.closed,
moderated = strings.moderated,
unmoderated = strings.unmoderated }) do
comment_status[#comment_status + 1] = option{ value = status,
selected = (status == (params.comment_status or
params.post.comment_status)) or nil, text }
end
local comment_status = "<select name=\"comment_status\">" ..
table.concat(comment_status, "\n") .. "</select>"
return div{
h2(page_header),
form{
method = "post",
action = action,
p{
strings.section, br(), sections, br(), br(),
strings.title, br(), error_title, input{ type = "text",
name = "title", value = params.title or params.post.title },
br(), br(),
strings.index_image, br(), error_title, input{ type = "text",
name = "image", value = params.image or params.post.image },
br(), br(),
strings.external_url, br(), input{ type = "text",
name = "external_url", value = params.external_url or
params.post.external_url },
br(), br(),
strings.abstract, br(), textarea{ name = "abstract",
rows = "5", cols = "40", params.abstract or
params.post.abstract }, br(), br(),
strings.body, br(), textarea{ name = "body",
rows = "15", cols = "80", params.body or
params.post.body }, br(), br(),
strings.comment_status, br(), comment_status, br(), br(),
strings.published_at, br(), input{ type = "text",
name = "published_at", value = params.published_at or
os.date("%d-%m-%Y %H:%M", params.post.published_at) }, br(), br(),
input{ type = "checkbox", name = "published", value = "1",
checked = params.published or params.post.published or nil },
strings.published, br(), br(),
input{ type = "checkbox", name = "in_home", value = "1",
checked = params.in_home or params.post.in_home or nil },
strings.in_home, br(), br(),
input{ type = "submit", value = button_text }
}
}, delete
}
end
function render_manage_comments(web, params)
local for_mod = {}
for _, comment in ipairs(params.for_mod) do
for_mod[#for_mod + 1] = div{
p{ id = comment.id, strong{ strings.comment_by, " ",
comment:make_link(), " ",
strings.on_post, " ", a{
href = web:link("/post/" .. comment.post_id), comment.post_title },
" ", strings.on, " ", time(comment.created_at), ":" } },
markdown(comment.body),
p{ form{ action = web:link("/comment/" .. comment.id .. "/approve"),
method = "post", input{ type = "submit", value = strings.approve }
}, form{ action = web:link("/comment/" .. comment.id .. "/delete"),
method = "post", input{ type = "submit", value = strings.delete }
}
},
}
end
local approved = {}
for _, comment in ipairs(params.approved) do
approved[#approved + 1] = div{
p{ id = comment.id, strong{ strings.comment_by, " ",
comment:make_link(), " ",
strings.on_post, " ", a{
href = web:link("/post/" .. comment.post_id), comment.post_title },
" ", strings.on, " ", time(comment.created_at), ":" } },
markdown(comment.body),
p{ form{ action = web:link("/comment/" .. comment.id .. "/delete"),
method = "post", input{ type = "submit", value = strings.delete }
} },
}
end
if #for_mod == 0 then for_mod = { p{ strings.no_comments } } end
if #approved == 0 then approved = { p{ strings.no_comments } } end
return div{
h2(strings.waiting_moderation),
table.concat(for_mod, "\n"),
h2(strings.published),
table.concat(approved, "\n")
}
end
orbit.htmlify(toycms, "_.+", "admin_layout", "render_.+")
|