diff options
-rw-r--r-- | config.def.h | 2 | ||||
-rw-r--r-- | wiki.c | 54 |
2 files changed, 37 insertions, 19 deletions
diff --git a/config.def.h b/config.def.h index a83aa8e..ea4a312 100644 --- a/config.def.h +++ b/config.def.h @@ -1,6 +1,8 @@ static char *title = "a wiki"; static char *id = "anonymous"; +static char *loft = ".loft"; + static char *links[][2] = { { "home", "home" }, { "planer", "planer" }, @@ -3,6 +3,7 @@ #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <ctype.h> #include <glob.h> #include <libgen.h> #include <time.h> @@ -103,11 +104,10 @@ int unauthorized(int status) { int edit(char *path) { if(!writable(path)) return unauthorized(403); FILE *fp = fopen(path, "r"); - char *page = clean(path); fp ? head(path, path, "vis") : head(path, NULL, NULL); char buffer[sysconf(_SC_PAGESIZE)]; - printf("<form method='post' enctype='text/plain'>\n"); - printf("<p><textarea name=text rows=24 cols=72>"); + printf("<form method='post'>\n"); + printf("<p><textarea name=t rows=24 cols=72>"); if(fp) while(fgets(buffer, sizeof(buffer), fp)) printf("%s", buffer); @@ -119,38 +119,56 @@ int edit(char *path) { return 0; } -char *loft = ".loft"; -int store(char *raw, char *data, int len) { +int store(char *raw, int len) { char *dir = dirname(strdup(raw)); char *id = basename(strdup(raw)); if(chdir(dir)) return problem(404, "Not found"); char path[strlen(id) + strlen(loft) + 256]; time_t epoch = time(NULL); - snprintf(path, 512, "%s/%s.%d", loft, id, epoch); + snprintf(path, 512, "%s/%s.%d", loft, id, (int) epoch); FILE *fp = fopen(path, "w"); - if(!fp) return redirect(id); - fwrite(data + 5, len - 5, 1, fp); + if(!fp) return redirect("FFFF"); + + int pos = 0; + unsigned int decoded; + char buffer[3]; + for(int i = 0; i < len; i++) { + buffer[pos] = getchar(); + if(buffer[pos] == '+') buffer[pos] = ' '; + if(buffer[pos] == '&') { buffer[pos] = '\0'; break; } + if(pos == 2) { + if(buffer[0] == '%' && isxdigit(buffer[1]) && isxdigit(buffer[2])) { + sscanf(buffer, "%%%2x", &decoded); + fprintf(fp, "%c", decoded); + memset(buffer, 0, 3); + pos = 0; + } else { + fprintf(fp, "%c", buffer[0]); + memmove(buffer, &buffer[1], 2); + buffer[2] = 0; + } + } + else pos++; + } + fprintf(fp, "%.3s", buffer); fputc('\0', fp); fclose(fp); unlink(id); symlink(path, id); + return 0; } int post(char *path) { - char *header, *data; + char *header; int clen; header = getenv("CONTENT_LENGTH"); if(!header) return redirect(path); clen = atoi(header); - data = calloc(1, clen + 1); - fread(data, clen, 1, stdin); - if(strncmp(data, "text=", 5)) - return redirect(path); - - if(store(path, data, clen)) { - - } + while(clen > 1) + if(clen--, getchar() == 't') + if(clen--, getchar() == '=') + store(path, clen); return redirect(path); } @@ -169,7 +187,6 @@ int list(char *raw, int dir) { foot(0); return 0; } - head("treff", NULL, NULL); char *path; printf("<ul class='glob'>"); @@ -202,7 +219,6 @@ int view(char *path) { return list(path, 1); FILE *fp = fopen(path, "r"); if(!fp) return redirect("?e"); - char *page = clean(path); writable(path) ? head(path, "?e", "rediger") : head(path, NULL, NULL); marxup(fp, stdout, NULL); foot(1); |