common.c (3303B)
1 #include <ctype.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "common.h" 6 #include "pages.h" 7 8 enum kcgi_err 9 linkpage(struct pagedata *pd, int page, char *names[PageMax]) 10 { 11 char *url; 12 char namebuf[32]; 13 enum kcgi_err status; 14 15 url = pd->pages[page]; 16 if (names[page]) { 17 strcpy(namebuf, names[page]); 18 } else { 19 strcpy(namebuf, url); 20 namebuf[0] = toupper(namebuf[0]); 21 } 22 23 if ((status = khtml_elem(&pd->html, KELEM_LI)) != KCGI_OK || 24 (status = htmllink(pd, url, namebuf)) != KCGI_OK || 25 (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) 26 return status; 27 return KCGI_OK; 28 } 29 30 enum kcgi_err 31 linkpages(struct pagedata *pd, int *pages, char *names[PageMax]) 32 { 33 unsigned int i; 34 enum kcgi_err status; 35 36 if ((status = khtml_elem(&pd->html, KELEM_UL)) != KCGI_OK) 37 return status; 38 39 for (i = 0; pages[i] >= 0; ++i) { 40 status = linkpage(pd, pages[i], names); 41 if (status != KCGI_OK) 42 return status; 43 } 44 45 return khtml_closeelem(&pd->html, 1); 46 } 47 48 enum kcgi_err 49 tk_header(struct pagedata *pd) 50 { 51 static char *pagenames[PageMax] = { 52 [PageIndex] = "Home" 53 }; 54 static int auth_a[] = { PageIndex, PageMain, PageArchive, -1 }; 55 static int auth_b[] = { PageAccount, PageLogout, -1 }; 56 static int unauth_a[] = { PageIndex, -1 }; 57 static int unauth_b[] = { PageLogin, -1 }; 58 59 enum kcgi_err status; 60 int *a, *b; 61 62 if (pd->user) { 63 a = auth_a; 64 b = auth_b; 65 } else { 66 a = unauth_a; 67 b = unauth_b; 68 } 69 70 if ((status = khtml_elem(&pd->html, KELEM_NAV)) != KCGI_OK || 71 (status = linkpages(pd, a, pagenames)) != KCGI_OK || 72 (status = linkpages(pd, b, pagenames)) != KCGI_OK) 73 return status; 74 return khtml_closeelem(&pd->html, 1); 75 } 76 77 enum kcgi_err 78 tk_startpage(struct pagedata *pd, struct pagetemplate *t, enum khttp code) 79 { 80 enum kcgi_err status; 81 82 if ((status = startpage(pd, t, code)) != KCGI_OK || 83 (status = tk_header(pd)) != KCGI_OK) 84 return status; 85 return KCGI_OK; 86 } 87 88 enum kcgi_err 89 tk_htmlerror(struct pagedata *pd, char *fmt, ...) 90 { 91 enum kcgi_err status; 92 char ebuf[128]; 93 va_list ap; 94 95 va_start(ap, fmt); 96 /* intentionally ignore truncated message */ 97 if (vsnprintf(ebuf, sizeof(ebuf), fmt, ap) < 0) 98 return KCGI_SYSTEM; 99 va_end(ap); 100 101 if ((status = khtml_attr(&pd->html, KELEM_P, 102 KATTR_CLASS, "error", KATTR__MAX)) != KCGI_OK || 103 (status = khtml_puts(&pd->html, "Error: ")) != KCGI_OK || 104 (status = khtml_puts(&pd->html, ebuf)) != KCGI_OK) 105 return status; 106 return khtml_closeelem(&pd->html, 1); 107 } 108 109 enum kcgi_err 110 tk_errorpage(struct pagedata *pd, enum khttp code) 111 { 112 static char *css[] = { "css/main.css", NULL }; 113 static struct pagetemplate template = { 114 "Unsuccessful", 115 .css = css 116 }; 117 118 enum kcgi_err status; 119 120 kutil_warn(&pd->req, pd->user ? pd->user->name : NULL, 121 "%s: %s", pd->req.fullpath, khttps[code]); 122 123 if ((status = tk_startpage(pd, &template, code)) != KCGI_OK || 124 (status = khtml_elem(&pd->html, KELEM_H1)) != KCGI_OK || 125 (status = khtml_printf(&pd->html, "%s", khttps[code])) != KCGI_OK || 126 (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) 127 return status; 128 return endpage(pd, &template); 129 } 130 131 enum kcgi_err 132 tk_prompt_login(struct pagedata *pd) 133 { 134 return redirect(pd, pd->pages[PageLogin], "Page requires authentication"); 135 }