-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.wfl
More file actions
1266 lines (1203 loc) · 49.9 KB
/
Copy pathmain.wfl
File metadata and controls
1266 lines (1203 loc) · 49.9 KB
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ===========================================================================
// Scriptorium — a small WordPress-style CMS written in WFL.
//
// Run it from the repository root (template + asset paths resolve relative to
// the working directory):
//
// wfl main.wfl
//
// It opens a SQLite database (scriptorium.db, auto-created), serves the public
// site + a design-system-styled admin panel, and — on first run — seeds an
// admin account and prints its one-time password.
//
// The whole app layers on one include: render -> auth -> db -> util, plus the
// vendored Scribe template engine. Because WFL's `header`, `path`, and `method`
// request values are only available in the top-level request loop, the loop
// reads them once and passes them into the handler actions below.
//
// All mutable state (the database and uploaded media) lives under one data
// directory so a container/CI deploy can mount a single volume and never lose
// content on redeploy. The paths below default to the legacy layout and are
// re-pointed at boot when `data_dir` is set in .wflcfg. See issue #5.
// ===========================================================================
include from "app/render.wfl"
// Mutable-state paths, declared up front so the asset/media actions can read
// them. Boot (further down) reassigns these from `data_dir` when configured.
store db_path as "scriptorium.db"
store uploads_dir as "static/uploads"
// ---------------------------------------------------------------------------
// Response helpers (respond works inside an action when `req` is a parameter).
// ---------------------------------------------------------------------------
define action called send_html with parameters req and body_html:
respond to req with body_html and content_type "text/html"
return yes
end action
define action called send_html_status with parameters req and body_html and code:
respond to req with body_html and status code and content_type "text/html"
return yes
end action
define action called send_text_status with parameters req and body_text and code:
respond to req with body_text and status code and content_type "text/plain"
return yes
end action
// 303 See Other redirect (POST -> GET), path-only Location.
define action called send_redirect with parameters req and location:
create map redirect_headers:
"Location" is location
end map
respond to req with "" and status 303 and headers redirect_headers
return yes
end action
// Redirect and set a cookie in the same response (login / logout).
define action called send_redirect_cookie with parameters req and location and cookie_value:
create map redirect_headers:
"Location" is location
"Set-Cookie" is cookie_value
end map
respond to req with "" and status 303 and headers redirect_headers
return yes
end action
// Send HTML and set a cookie in the same response (the login form's CSRF cookie).
define action called send_html_cookie with parameters req and body_html and cookie_value:
create map extra_headers:
"Set-Cookie" is cookie_value
end map
respond to req with body_html and content_type "text/html" and headers extra_headers
return yes
end action
define action called send_forbidden with parameters req:
respond to req with "403 — you don't have access to that." and status 403 and content_type "text/plain"
return yes
end action
define action called send_csrf_rejected with parameters req:
respond to req with "403 — invalid or missing CSRF token. Go back, reload the page, and try again." and status 403 and content_type "text/plain"
return yes
end action
// Build an optional flash map from a message ("" means no flash).
define action called make_flash with parameters kind and message:
check if message is equal to "":
return nothing
end check
create map fm:
"kind" is kind
"message" is message
end map
return fm
end action
// ---------------------------------------------------------------------------
// Static assets — GET /assets/* -> files under ./static/
// ---------------------------------------------------------------------------
define action called serve_asset with parameters req and req_path:
check if contains of req_path and "..":
call send_text_status with req and "Forbidden" and 403
return yes
end check
store rel as substring of req_path and 8 and ((length of req_path) minus 8)
store filepath as "static/" with rel
// Uploaded media may live outside the app tree (see data_dir), so route
// /assets/uploads/* to the configured uploads directory rather than static/.
check if (indexof of rel and "uploads/") is equal to 0:
store upload_rel as substring of rel and 8 and ((length of rel) minus 8)
change filepath to uploads_dir with "/" with upload_rel
end check
check if file exists at filepath:
open file at filepath for reading binary as asset_file
store asset_bytes as read binary from asset_file
close file asset_file
respond to req with asset_bytes and content_type (mime_type of filepath)
otherwise:
call send_text_status with req and "Not found" and 404
end check
return yes
end action
// ---------------------------------------------------------------------------
// Public site
// ---------------------------------------------------------------------------
define action called handle_404 with parameters db and req and user:
store site_map as site_context of db and user
create map ctx:
"site" is site_map
end map
store html as render_public of "notfound.html" and ctx
call send_html_status with req and html and 404
return yes
end action
define action called handle_home with parameters db and req and user and page_num:
store per_page as to_int of (setting_get of db and "posts_per_page" and "5") and 5
check if per_page is less than 1:
change per_page to 5
end check
store safe_page as page_num
check if safe_page is less than 1:
change safe_page to 1
end check
store offset_val as (safe_page minus 1) times per_page
store posts_rows as post_list_published of db and per_page and offset_val
store total as post_count_published of db
store site_map as site_context of db and user
store has_more as (safe_page times per_page) is less than total
store has_prev as safe_page is greater than 1
store prev_page as safe_page minus 1
check if prev_page is less than or equal to 1:
store prev_url as "/"
otherwise:
store prev_url as "/blog/page/" with prev_page
end check
create map pag:
"has_more" is has_more
"has_prev" is has_prev
"next_url" is "/blog/page/" with (safe_page plus 1)
"prev_url" is prev_url
end map
create map ctx:
"site" is site_map
"posts" is posts_rows
"pagination" is pag
end map
store html as render_public of "home.html" and ctx
call send_html with req and html
return yes
end action
define action called handle_post with parameters db and req and user and slug:
store post_row as post_by_slug of db and slug
check if post_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if post_row["status"] is not equal to "published":
call handle_404 with db and req and user
return yes
end check
store site_map as site_context of db and user
create map ctx:
"site" is site_map
"post" is post_row
end map
store html as render_public of "post.html" and ctx
call send_html with req and html
return yes
end action
define action called handle_page with parameters db and req and user and slug:
store page_row as page_by_slug of db and slug
check if page_row is nothing:
call handle_404 with db and req and user
return yes
end check
store site_map as site_context of db and user
create map ctx:
"site" is site_map
"page" is page_row
end map
store html as render_public of "page.html" and ctx
call send_html with req and html
return yes
end action
define action called dispatch_public with parameters db and req and req_path and user:
check if req_path is equal to "/":
call handle_home with db and req and user and 1
return yes
end check
check if (req_path is equal to "/blog") or (req_path is equal to "/blog/"):
call handle_home with db and req and user and 1
return yes
end check
store pg as path_params of req_path and "/blog/page/:n"
check if pg is not nothing:
store n as to_int of pg["n"] and 1
call handle_home with db and req and user and n
return yes
end check
store ps as path_params of req_path and "/post/:slug"
check if ps is not nothing:
call handle_post with db and req and user and ps["slug"]
return yes
end check
store pp as path_params of req_path and "/page/:slug"
check if pp is not nothing:
call handle_page with db and req and user and pp["slug"]
return yes
end check
call handle_404 with db and req and user
return yes
end action
// ---------------------------------------------------------------------------
// Admin — authentication
//
// The login form is protected with a double-submit CSRF cookie (there is no
// session yet to bind a token to): rendering the form mints a random token,
// sets it as a short-lived cookie, and embeds it as a hidden field; the POST
// must present both, matching, compared in constant time. After login, every
// admin form instead carries the session-bound token (site.csrf / csrf_ok).
// ---------------------------------------------------------------------------
define action called render_login with parameters db and req and form_username and flash_msg:
store site_map as site_context of db and nothing
store login_tok as generate_csrf_token
create map login_form:
"username" is form_username
end map
create map ctx:
"site" is site_map
"form" is login_form
"csrf" is login_tok
"flash" is make_flash of "error" and flash_msg
end map
store html as render_admin of "login.html" and ctx
store csrf_cookie as "csrf=" with login_tok with "; HttpOnly; Path=/admin/login; SameSite=Lax; Max-Age=600"
call send_html_cookie with req and html and csrf_cookie
return yes
end action
define action called handle_login_form with parameters db and req and user:
check if user is not nothing:
call send_redirect with req and "/admin"
return yes
end check
call render_login with db and req and "" and ""
return yes
end action
// Extract the pre-login CSRF token from the Cookie header ("" when absent).
define action called login_csrf_from_cookie with parameters cookie_header:
check if cookie_header is nothing:
return ""
end check
store cookies as parse_cookies of cookie_header
return field_or of cookies and "csrf" and ""
end action
define action called handle_login_post with parameters db and req and req_body and cookie_hdr and req_ip:
// Rate limit: after 10 failed attempts from one address in 15 minutes,
// stop checking credentials at all until the window moves on.
store recent_failures as login_attempts_recent of db and req_ip
check if recent_failures is greater than or equal to 10:
call send_text_status with req and "429 — too many login attempts from your address. Wait 15 minutes and try again." and 429
return yes
end check
store f as parse_form_urlencoded of req_body
// Double-submit CSRF check (cookie token vs form token, constant time).
store cookie_tok as login_csrf_from_cookie of cookie_hdr
store form_tok as field_or of f and "csrf_token" and ""
store csrf_valid as no
check if (cookie_tok is not equal to "") and (form_tok is not equal to ""):
change csrf_valid to constant_time_equals of cookie_tok and form_tok
end check
check if csrf_valid is not equal to yes:
call render_login with db and req and "" and "That form had expired — please sign in again."
return yes
end check
store username as field_or of f and "username" and ""
store password as field_or of f and "password" and ""
store user as login_check of db and username and password
check if user is nothing:
store recorded as login_attempt_record of db and req_ip
call render_login with db and req and username and "Wrong username or password."
return yes
end check
store cleared as login_attempts_clear of db and req_ip
store sid as session_start of db and user["id"]
call send_redirect_cookie with req and "/admin" and (session_cookie of sid)
return yes
end action
define action called handle_logout with parameters db and req and cookie_hdr:
store gone as logout_by_cookie of db and cookie_hdr
store cleared_cookie as "sid=; HttpOnly; Path=/; SameSite=Lax; Max-Age=0"
call send_redirect_cookie with req and "/admin/login" and cleared_cookie
return yes
end action
// ---------------------------------------------------------------------------
// Admin — dashboard
// ---------------------------------------------------------------------------
define action called handle_dashboard with parameters db and req and user:
store site_map as site_context of db and user
create map stats:
"posts" is posts_total of db
"pages" is pages_total of db
"users" is user_count of db
end map
check if (is_admin of user) is equal to yes:
store recent as post_list_all of db
otherwise:
store recent as post_list_by_author of db and user["id"]
end check
create map ctx:
"site" is site_map
"stats" is stats
"posts" is recent
end map
store html as render_admin of "dashboard.html" and ctx
call send_html with req and html
return yes
end action
// ---------------------------------------------------------------------------
// Admin — posts
// ---------------------------------------------------------------------------
define action called render_post_form with parameters db and req and user and post_map and form_title and action_url and flash_msg:
store site_map as site_context of db and user
create map ctx:
"site" is site_map
"post" is post_map
"form_title" is form_title
"action_url" is action_url
"flash" is make_flash of "error" and flash_msg
end map
store html as render_admin of "post_form.html" and ctx
call send_html with req and html
return yes
end action
define action called empty_content_map:
create map blank:
"id" is ""
"title" is ""
"slug" is ""
"body_markdown" is ""
"status" is "draft"
end map
return blank
end action
define action called handle_posts_list with parameters db and req and user:
store site_map as site_context of db and user
check if (is_admin of user) is equal to yes:
store rows as post_list_all of db
otherwise:
store rows as post_list_by_author of db and user["id"]
end check
create map ctx:
"site" is site_map
"posts" is rows
end map
store html as render_admin of "posts_list.html" and ctx
call send_html with req and html
return yes
end action
define action called handle_post_new with parameters db and req and user:
call render_post_form with db and req and user and (empty_content_map) and "New post" and "/admin/posts" and ""
return yes
end action
define action called handle_post_create with parameters db and req and req_body and user:
store f as parse_form_urlencoded of req_body
store the_title as field_or of f and "title" and ""
store the_slug as field_or of f and "slug" and ""
store the_body as field_or of f and "body_markdown" and ""
store the_status as field_or of f and "status" and "draft"
check if (trim of the_title) is equal to "":
create map entered:
"id" is ""
"title" is the_title
"slug" is the_slug
"body_markdown" is the_body
"status" is the_status
end map
call render_post_form with db and req and user and entered and "New post" and "/admin/posts" and "Title is required."
return yes
end check
check if the_slug is equal to "":
change the_slug to slugify of the_title
otherwise:
change the_slug to slugify of the_slug
end check
check if the_slug is equal to "":
change the_slug to "post"
end check
store ok as yes
try:
store ins as post_create of db and the_slug and the_title and the_body and the_status and user["id"]
when error:
change ok to no
end try
check if ok is equal to no:
create map entered2:
"id" is ""
"title" is the_title
"slug" is the_slug
"body_markdown" is the_body
"status" is the_status
end map
call render_post_form with db and req and user and entered2 and "New post" and "/admin/posts" and "That slug is already taken — pick another."
return yes
end check
call send_redirect with req and "/admin/posts"
return yes
end action
define action called handle_post_edit with parameters db and req and user and id_text:
store the_id as to_int of id_text and 0
store post_row as post_by_id of db and the_id
check if post_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if (can_edit of user and post_row["author_id"]) is equal to yes:
store action_url as "/admin/posts/" with the_id
call render_post_form with db and req and user and post_row and "Edit post" and action_url and ""
otherwise:
call send_forbidden with req
end check
return yes
end action
define action called handle_post_update with parameters db and req and req_body and user and id_text:
store the_id as to_int of id_text and 0
store post_row as post_by_id of db and the_id
check if post_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if (can_edit of user and post_row["author_id"]) is equal to yes:
store f as parse_form_urlencoded of req_body
store the_title as field_or of f and "title" and ""
store the_slug as field_or of f and "slug" and ""
store the_body as field_or of f and "body_markdown" and ""
store the_status as field_or of f and "status" and "draft"
check if the_slug is equal to "":
change the_slug to slugify of the_title
otherwise:
change the_slug to slugify of the_slug
end check
store upd as post_update of db and the_id and the_slug and the_title and the_body and the_status
call send_redirect with req and "/admin/posts"
otherwise:
call send_forbidden with req
end check
return yes
end action
define action called handle_post_delete with parameters db and req and user and id_text:
store the_id as to_int of id_text and 0
store post_row as post_by_id of db and the_id
check if post_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if (can_edit of user and post_row["author_id"]) is equal to yes:
store del as post_delete of db and the_id
call send_redirect with req and "/admin/posts"
otherwise:
call send_forbidden with req
end check
return yes
end action
// ---------------------------------------------------------------------------
// Admin — pages (same shape as posts)
// ---------------------------------------------------------------------------
define action called render_page_form with parameters db and req and user and page_map and form_title and action_url and flash_msg:
store site_map as site_context of db and user
create map ctx:
"site" is site_map
"page" is page_map
"form_title" is form_title
"action_url" is action_url
"flash" is make_flash of "error" and flash_msg
end map
store html as render_admin of "page_form.html" and ctx
call send_html with req and html
return yes
end action
define action called handle_pages_list with parameters db and req and user:
store site_map as site_context of db and user
check if (is_admin of user) is equal to yes:
store rows as page_list_all of db
otherwise:
store rows as page_list_by_author of db and user["id"]
end check
create map ctx:
"site" is site_map
"pages" is rows
end map
store html as render_admin of "pages_list.html" and ctx
call send_html with req and html
return yes
end action
define action called handle_page_new with parameters db and req and user:
call render_page_form with db and req and user and (empty_content_map) and "New page" and "/admin/pages" and ""
return yes
end action
define action called handle_page_create with parameters db and req and req_body and user:
store f as parse_form_urlencoded of req_body
store the_title as field_or of f and "title" and ""
store the_slug as field_or of f and "slug" and ""
store the_body as field_or of f and "body_markdown" and ""
store the_status as field_or of f and "status" and "draft"
check if (trim of the_title) is equal to "":
create map entered:
"id" is ""
"title" is the_title
"slug" is the_slug
"body_markdown" is the_body
"status" is the_status
end map
call render_page_form with db and req and user and entered and "New page" and "/admin/pages" and "Title is required."
return yes
end check
check if the_slug is equal to "":
change the_slug to slugify of the_title
otherwise:
change the_slug to slugify of the_slug
end check
check if the_slug is equal to "":
change the_slug to "page"
end check
store ok as yes
try:
store ins as page_create of db and the_slug and the_title and the_body and the_status and user["id"]
when error:
change ok to no
end try
check if ok is equal to no:
create map entered2:
"id" is ""
"title" is the_title
"slug" is the_slug
"body_markdown" is the_body
"status" is the_status
end map
call render_page_form with db and req and user and entered2 and "New page" and "/admin/pages" and "That slug is already taken — pick another."
return yes
end check
call send_redirect with req and "/admin/pages"
return yes
end action
define action called handle_page_edit with parameters db and req and user and id_text:
store the_id as to_int of id_text and 0
store page_row as page_by_id of db and the_id
check if page_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if (can_edit of user and page_row["author_id"]) is equal to yes:
store action_url as "/admin/pages/" with the_id
call render_page_form with db and req and user and page_row and "Edit page" and action_url and ""
otherwise:
call send_forbidden with req
end check
return yes
end action
define action called handle_page_update with parameters db and req and req_body and user and id_text:
store the_id as to_int of id_text and 0
store page_row as page_by_id of db and the_id
check if page_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if (can_edit of user and page_row["author_id"]) is equal to yes:
store f as parse_form_urlencoded of req_body
store the_title as field_or of f and "title" and ""
store the_slug as field_or of f and "slug" and ""
store the_body as field_or of f and "body_markdown" and ""
store the_status as field_or of f and "status" and "draft"
check if the_slug is equal to "":
change the_slug to slugify of the_title
otherwise:
change the_slug to slugify of the_slug
end check
store upd as page_update of db and the_id and the_slug and the_title and the_body and the_status
call send_redirect with req and "/admin/pages"
otherwise:
call send_forbidden with req
end check
return yes
end action
define action called handle_page_delete with parameters db and req and user and id_text:
store the_id as to_int of id_text and 0
store page_row as page_by_id of db and the_id
check if page_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if (can_edit of user and page_row["author_id"]) is equal to yes:
store del as page_delete of db and the_id
call send_redirect with req and "/admin/pages"
otherwise:
call send_forbidden with req
end check
return yes
end action
// ---------------------------------------------------------------------------
// Admin — users (admin only)
// ---------------------------------------------------------------------------
define action called render_user_form with parameters db and req and user and edit_user and form_title and action_url and can_delete and flash_msg:
store site_map as site_context of db and user
create map ctx:
"site" is site_map
"edit_user" is edit_user
"form_title" is form_title
"action_url" is action_url
"can_delete" is can_delete
"flash" is make_flash of "error" and flash_msg
end map
store html as render_admin of "user_form.html" and ctx
call send_html with req and html
return yes
end action
define action called blank_user_map:
create map blank:
"id" is ""
"username" is ""
"role" is "author"
end map
return blank
end action
define action called handle_users_list with parameters db and req and user:
store site_map as site_context of db and user
store rows as user_list of db
create map ctx:
"site" is site_map
"users" is rows
end map
store html as render_admin of "users_list.html" and ctx
call send_html with req and html
return yes
end action
define action called handle_user_new with parameters db and req and user:
call render_user_form with db and req and user and (blank_user_map) and "New user" and "/admin/users" and no and ""
return yes
end action
define action called handle_user_create with parameters db and req and req_body and user:
store f as parse_form_urlencoded of req_body
store username as field_or of f and "username" and ""
store password as field_or of f and "password" and ""
store role_name as field_or of f and "role" and "author"
check if role_name is not equal to "admin":
change role_name to "author"
end check
store problem as ""
check if username is equal to "":
change problem to "Username is required."
end check
check if (problem is equal to "") and (password is equal to ""):
change problem to "Password is required for a new user."
end check
check if problem is not equal to "":
create map entered:
"id" is ""
"username" is username
"role" is role_name
end map
call render_user_form with db and req and user and entered and "New user" and "/admin/users" and no and problem
return yes
end check
store ok as yes
try:
store ph as hash_password of password
store ins as user_create of db and username and ph and role_name
when error:
change ok to no
end try
check if ok is equal to no:
create map entered2:
"id" is ""
"username" is username
"role" is role_name
end map
call render_user_form with db and req and user and entered2 and "New user" and "/admin/users" and no and "That username is already taken."
return yes
end check
call send_redirect with req and "/admin/users"
return yes
end action
define action called handle_user_edit with parameters db and req and user and id_text:
store the_id as to_int of id_text and 0
store edit_user as user_by_id of db and the_id
check if edit_user is nothing:
call send_text_status with req and "User not found" and 404
return yes
end check
store action_url as "/admin/users/" with the_id
store can_delete as (user["id"] is not equal to the_id)
call render_user_form with db and req and user and edit_user and "Edit user" and action_url and can_delete and ""
return yes
end action
define action called handle_user_update with parameters db and req and req_body and user and id_text:
store the_id as to_int of id_text and 0
store edit_user as user_by_id of db and the_id
check if edit_user is nothing:
call send_text_status with req and "User not found" and 404
return yes
end check
store f as parse_form_urlencoded of req_body
store role_name as field_or of f and "role" and "author"
check if role_name is not equal to "admin":
change role_name to "author"
end check
store password as field_or of f and "password" and ""
store role_upd as user_set_role of db and the_id and role_name
check if password is not equal to "":
store ph as hash_password of password
store pw_upd as user_set_password of db and the_id and ph
end check
call send_redirect with req and "/admin/users"
return yes
end action
define action called handle_user_delete with parameters db and req and user and id_text:
store the_id as to_int of id_text and 0
check if user["id"] is equal to the_id:
call send_text_status with req and "You cannot delete your own account." and 400
return yes
end check
store del as user_delete of db and the_id
call send_redirect with req and "/admin/users"
return yes
end action
define action called dispatch_users with parameters db and req and req_method and req_path and req_body and user:
check if req_path is equal to "/admin/users":
check if req_method is equal to "POST":
call handle_user_create with db and req and req_body and user
otherwise:
call handle_users_list with db and req and user
end check
return yes
end check
check if req_path is equal to "/admin/users/new":
call handle_user_new with db and req and user
return yes
end check
store ue as path_params of req_path and "/admin/users/:id/edit"
check if ue is not nothing:
call handle_user_edit with db and req and user and ue["id"]
return yes
end check
store ud as path_params of req_path and "/admin/users/:id/delete"
check if ud is not nothing:
check if req_method is equal to "POST":
call handle_user_delete with db and req and user and ud["id"]
otherwise:
call send_text_status with req and "Method not allowed" and 405
end check
return yes
end check
store uu as path_params of req_path and "/admin/users/:id"
check if uu is not nothing:
check if req_method is equal to "POST":
call handle_user_update with db and req and req_body and user and uu["id"]
otherwise:
call send_text_status with req and "Method not allowed" and 405
end check
return yes
end check
call send_text_status with req and "Not found" and 404
return yes
end action
// ---------------------------------------------------------------------------
// Admin — media library (uploads live under static/uploads/, so they are
// served by the existing /assets/* route as /assets/uploads/<filename>).
// ---------------------------------------------------------------------------
define action called handle_media_list with parameters db and req and user and flash_msg:
store site_map as site_context of db and user
// Seed with a list literal before assigning the query result: an action's
// return type is `Any` at the call site, and WFL >= 26.7.48 rejects `Any` as
// a for-each collection. Declaring the list first pins the type — the same
// shape handle_media_upload already uses for `parts`. See issue #9.
store rows as []
change rows to media_list_all of db
store items as []
for each media_row in rows:
create map item:
"id" is media_row["id"]
"filename" is media_row["filename"]
"original_name" is media_row["original_name"]
"content_type" is media_row["content_type"]
"size_kb" is round of (media_row["size"] divided by 1024)
"url" is "/assets/uploads/" with media_row["filename"]
"uploader_name" is media_row["uploader_name"]
"created_at" is media_row["created_at"]
"can_delete" is can_edit of user and media_row["uploader_id"]
end map
push with items and item
end for
create map ctx:
"site" is site_map
"media" is items
"flash" is make_flash of "error" and flash_msg
end map
store html as render_admin of "media.html" and ctx
call send_html with req and html
return yes
end action
// POST /admin/media/upload — a multipart/form-data body (the one admin form
// that is not form-urlencoded, so it validates its CSRF part itself).
define action called handle_media_upload with parameters db and req and user:
store upload_content_type as header "Content-Type" of req
store raw_bytes as body_bytes of req
store parts as []
store parse_failed as no
try:
change parts to parse_multipart of raw_bytes and upload_content_type
when error:
change parse_failed to yes
end try
check if parse_failed is equal to yes:
call handle_media_list with db and req and user and "That didn't look like a file upload — try again."
return yes
end check
store submitted_tok as ""
store file_part as nothing
for each part in parts:
store part_name as field_or of part and "name" and ""
check if part_name is equal to "csrf_token":
change submitted_tok to field_or of part and "content" and ""
end check
check if part_name is equal to "file":
change file_part to part
end check
end for
check if (csrf_ok of user and submitted_tok) is not equal to yes:
call send_csrf_rejected with req
return yes
end check
check if file_part is nothing:
call handle_media_list with db and req and user and "Choose a file to upload."
return yes
end check
store orig_name as field_or of file_part and "filename" and ""
check if orig_name is equal to "":
call handle_media_list with db and req and user and "Choose a file to upload."
return yes
end check
store ext as file_ext of orig_name
store ext_ok as no
check if (ext is equal to "png") or (ext is equal to "jpg") or (ext is equal to "jpeg") or (ext is equal to "gif") or (ext is equal to "webp"):
change ext_ok to yes
end check
check if ext_ok is equal to no:
call handle_media_list with db and req and user and "Only png, jpg, jpeg, gif, and webp images can be uploaded."
return yes
end check
store file_bytes as file_part["content_bytes"]
check if (length of file_bytes) is equal to 0:
call handle_media_list with db and req and user and "That file was empty."
return yes
end check
// Stored name: slug of the original name + random suffix, so uploads never
// collide and never carry a path or unexpected characters.
store base_name as slugify of (file_stem of orig_name)
check if base_name is equal to "":
change base_name to "upload"
end check
store stored_name as base_name with "-" with (secure_random_bytes of 4) with "." with ext
store stored_path as uploads_dir with "/" with stored_name
open file at stored_path for writing binary as upload_file
write binary file_bytes into upload_file
close file upload_file
// If the metadata insert fails, remove the just-written file so nothing
// stays publicly reachable without a library record.
store insert_ok as yes
try:
store created as media_create of db and stored_name and orig_name and (mime_type of stored_name) and (length of file_bytes) and user["id"]
when error:
change insert_ok to no
end try
check if insert_ok is equal to no:
try:
delete file at stored_path
when error:
display "Media upload: failed to remove orphaned file " with stored_path
end try
call handle_media_list with db and req and user and "Saving the upload failed — try again."
return yes
end check
call send_redirect with req and "/admin/media"
return yes
end action
define action called handle_media_delete with parameters db and req and user and id_text:
store the_id as to_int of id_text and 0
store media_row as media_by_id of db and the_id
check if media_row is nothing:
call handle_404 with db and req and user
return yes
end check
check if (can_edit of user and media_row["uploader_id"]) is equal to yes:
// Remove the DB row first: a leftover file with no record is merely
// orphaned bytes, but a record pointing at a deleted file would keep
// rendering a broken entry in the library.
store del as media_delete of db and the_id
store stored_path as uploads_dir with "/" with media_row["filename"]
try:
check if file exists at stored_path:
delete file at stored_path
end check
when error:
display "Media delete: failed to remove file " with stored_path
end try
call send_redirect with req and "/admin/media"
otherwise:
call send_forbidden with req
end check
return yes
end action
// ---------------------------------------------------------------------------
// Admin — settings (admin only)
// ---------------------------------------------------------------------------
define action called handle_settings with parameters db and req and user:
store site_map as site_context of db and user
create map settings_map:
"site_title" is setting_get of db and "site_title" and "Scriptorium"
"site_tagline" is setting_get of db and "site_tagline" and ""
"posts_per_page" is setting_get of db and "posts_per_page" and "5"
end map
create map ctx:
"site" is site_map
"settings" is settings_map
end map
store html as render_admin of "settings.html" and ctx
call send_html with req and html
return yes
end action
define action called handle_settings_save with parameters db and req and req_body and user:
store f as parse_form_urlencoded of req_body
store t as setting_set of db and "site_title" and (field_or of f and "site_title" and "Scriptorium")
store g as setting_set of db and "site_tagline" and (field_or of f and "site_tagline" and "")
store ppp as to_int of (field_or of f and "posts_per_page" and "5") and 5
check if ppp is less than 1:
change ppp to 5
end check
store p as setting_set of db and "posts_per_page" and ("" with ppp)
call send_redirect with req and "/admin/settings"
return yes