aboutsummaryrefslogtreecommitdiff
path: root/wiki.c
blob: 035eac87e5beee73d9c06aeaf6960923d13535b7 (plain)
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
#define _POSIX_C_SOURCE 200809L

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glob.h>
#include <libgen.h>
#include <unistd.h>
#include <sys/stat.h>

#include <marxup.h>

static char *wikititle = "a wiki";
static char *id = "anonymous";
static char *links[][2] = {
  { "home", "home" },
  { "planer", "planer" },
  0
};
static char *(*authenticate)() = NULL;

static char *script;
static char *base;

static const char *badchars = "\\?%:|\"<> ";

int writable(char *path) {
  struct stat s;
  if(!stat(path, &s)) {
    if(S_ISREG(s.st_mode))
      return !access(path, W_OK);
    else if(S_ISDIR(s.st_mode))
      return 0;
  }
  char *dir = dirname(strdup(path));
  if(!stat(dir, &s)) {
    if(S_ISDIR(s.st_mode))
      return !access(dir, W_OK);
  }
  return 0;
}

void head(char *title, char *link, char *what) {
  printf("Content-Type: text/html; charset=utf-8\n\n");
  printf("<!doctype html>\n");
  printf("<title>%s ยท %s</title>\n", wikititle, title);
  printf("<link rel='stylesheet' href='%s/wiki.css'>\n", base);
  if(link && what)
    printf("<h1><a data-text='%s' href='%s' >%s</a></h1>", what, link, title);
  else 
    printf("<h1>%s</h1>", title);
}

void foot(int file) {
  printf("<hr>\n");
  printf("<p>");
  for(int i = 0; *links[i]; i++)
    printf("<a href='%s/%s'>%s</a> ", script, links[i][1], links[i][0]);
  if(!file) return;
  printf("<span style='float: right'>");
  printf("%s ", id);
  printf("<a href='?t'>text/plain</a>");
  printf("</span>\n");
}

char *clean(char *p) {
  char *dup = strdup(basename(p));
  char *x = dup;
  while(*x) {
    if(*x == '-')
      *x = ' ';
    x++;
  }
  return dup;
}

int legit(char *page) {
  if(!page) return 0;
  if(!strcmp(page, "/")) return 0;
  if(strstr(page, "..")) return 0;
  if(strcspn(page, badchars) < strlen(page)) return 0;
  return 1;
}

int redirect(char *path) {
  printf("Status: 303\n");
  if(*path == '?')
    printf("Location: %s\n\n", path);
  else
    printf("Location: %s/%s\n\n", script, path);
  return 0;
}

int unauthorized(int status) {
  printf("Status: %d\n", status);
  if(status == 401)
    printf("WWW-Authenticate: Basic realm='Kya'\n");
  printf("Content-Type: text/plain; charset=utf-8\n\n");
  printf("No.\n");
  return 1;
}

int edit(char *path) {
  if(!writable(path)) return unauthorized(403);
  FILE *fp = fopen(path, "r");
  char *title = clean(path);
  fp ? head(title, path, "vis") : head(title, NULL, NULL);
  char buffer[sysconf(_SC_PAGESIZE)];
  printf("<form method='post' enctype='text/plain'>\n");
  printf("<p><textarea name=text rows=24 cols=72>");
  if(fp)
    while(fgets(buffer, sizeof(buffer), fp))
      printf("%s", buffer);
  printf("</textarea>\n");
  printf("<p><input type=submit value=Update>");
  printf("</form>");
  foot(1);
  if(fp) fclose(fp);
  return 0;
}

int post(char *path) {
  char *header, *data;
  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);
  FILE *fp = fopen(path, "w");
  if(!fp) return redirect(path);
  fwrite(data + 5, clen - 5, 1, fp);
  fputc('\0', fp);
  fclose(fp);
  return redirect(path);
}

int list(char *raw, int dir) {
  int len = strlen(raw);
  char *pattern = malloc(len + 3);
  if(dir)
    snprintf(pattern, len + 3, "%s/*", raw);
  else
    snprintf(pattern, len + 1, "%s", raw);
  glob_t res;
  if(glob(pattern, GLOB_MARK, NULL, &res)) {
    head("ingen treff", NULL, NULL);
    foot(0);
    return 0;
  }

  head("treff", NULL, NULL);
  char *path;
  printf("<ul class='glob'>");
  for(int i = 0; i < res.gl_pathc; i++) {
    path = res.gl_pathv[i];
    printf("<li><a href='%s/%s'>%s</a>", script, path, path);
  }
  printf("</ul>");
  foot(0);
  free(pattern);
  return 0;
}

int text(char *path) {
  FILE *fp = fopen(path, "r");
  if(fp) {
    char buffer[sysconf(_SC_PAGESIZE)];
    printf("Content-Type: text/plain; charset=utf-8\n\n");
    while(fgets(buffer, sizeof(buffer), fp))
      printf("%s", buffer);
  }
  fclose(fp);
  return 0;
}

int view(char *path) {
  struct stat s;
  stat(path, &s);
  if(S_ISDIR(s.st_mode))
    return list(path, 1);
  FILE *fp = fopen(path, "r");
  if(!fp) return redirect("?e");
  char *title = clean(path);
  writable(path) ? head(title, "?e", "rediger") : head(title, NULL, NULL);
  marxup(fp, stdout);
  foot(1);
  return 0;
}

int main(int argc, char **argv) {
  char *method = getenv("REQUEST_METHOD");
  char *page   = getenv("PATH_INFO");
  char *query  = getenv("QUERY_STRING");

  if(!method) method = "GET";
  if(!script) script = getenv("SCRIPT_NAME");
  if(!script) script = "";
  if(!base) base = strdup(dirname(strdup(script)));

  if(authenticate && !(id = authenticate())) return unauthorized(401);
  if(!legit(page++)) return redirect("home");

  setenv("MARXUP_HEADER", "2", 1);
  setenv("MARXUP_PREFIX", base, 1);

  chdir("pages");

  if(!strncmp(method, "POST", 4)) return post(page);
  while(query && *query) {
    switch(*query) {
      case 'e': return edit(page);
      case 't': return text(page);
      // case 'p': return 
    }
    query++;
  }
  if(strchr(page, '*'))
    return list(page, 0);
  return view(page);
}