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
|
/*************************************************************************************************
* CGI script for file uploader
* Copyright (C) 2000-2003 Mikio Hirabayashi
* This file is part of QDBM, Quick Database Manager.
* QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; either version
* 2.1 of the License or any later version. QDBM is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License along with QDBM; if
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*************************************************************************************************/
#include <depot.h>
#include <cabin.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#undef TRUE
#define TRUE 1 /* boolean true */
#undef FALSE
#define FALSE 0 /* boolean false */
#define CONFFILE "qupl.conf" /* name of the configuration file */
#define DEFENC "US-ASCII" /* default encoding */
#define DEFLANG "en" /* default language */
#define DEFTITLE "File Uploader" /* default title */
#define DEFDATADIR "qupldir" /* directory containing files */
/* global variables */
const char *scriptname; /* name of the script */
const char *enc; /* encoding of the page */
const char *lang; /* language of the page */
const char *title; /* title of the page */
int quota; /* limit of the total size */
/* function prototypes */
int main(int argc, char **argv);
const char *skiplabel(const char *str);
void htmlprintf(const char *format, ...);
int getdirsize(const char *path);
const char *datestr(time_t t);
const char *gettype(const char *path);
/* main routine */
int main(int argc, char **argv){
CBLIST *lines, *parts, *files;
CBMAP *params;
FILE *ifp;
const char *tmp, *datadir, *body, *sname;
char *wp, *bound, *cdata, *filedata, *filename, *ebuf, *dbuf, *getname, *delname, numbuf[32];
int i, clen, blen, filesize, c, sdir, ssize, total;
time_t stime;
/* set configurations */
cbstdiobin();
scriptname = argv[0];
if((tmp = getenv("SCRIPT_NAME")) != NULL) scriptname = tmp;
enc = NULL;
lang = NULL;
title = NULL;
datadir = NULL;
quota = 0;
if((lines = cbreadlines(CONFFILE)) != NULL){
for(i = 0; i < cblistnum(lines); i++){
tmp = cblistval(lines, i, NULL);
if(cbstrfwmatch(tmp, "encoding:")){
enc = skiplabel(tmp);
} else if(cbstrfwmatch(tmp, "lang:")){
lang = skiplabel(tmp);
} else if(cbstrfwmatch(tmp, "title:")){
title = skiplabel(tmp);
} else if(cbstrfwmatch(tmp, "datadir:")){
datadir = skiplabel(tmp);
} else if(cbstrfwmatch(tmp, "quota:")){
quota = atoi(skiplabel(tmp));
}
}
}
if(!enc) enc = DEFENC;
if(!lang) lang = DEFLANG;
if(!title) title = DEFTITLE;
if(!datadir) datadir = DEFDATADIR;
if(quota < 0) quota = 0;
/* read parameters */
filedata = NULL;
filesize = 0;
filename = NULL;
getname = NULL;
delname = NULL;
if((tmp = getenv("REQUEST_METHOD")) != NULL && !strcmp(tmp, "POST") &&
(tmp = getenv("CONTENT_LENGTH")) != NULL && (clen = atoi(tmp)) > 0 &&
(tmp = getenv("CONTENT_TYPE")) != NULL && cbstrfwmatch(tmp, "multipart/form-data") &&
(tmp = strstr(tmp, "boundary=")) != NULL){
tmp += 9;
if(*tmp == '"') tmp++;
bound = cbmemdup(tmp, -1);
if((wp = strchr(bound, ';')) != NULL) *wp = '\0';
cdata = cbmalloc(clen + 1);
for(i = 0; i < clen && (c = getchar()) != EOF; i++){
cdata[i] = c;
}
parts = cbmimeparts(cdata, clen, bound);
if(cblistnum(parts) > 0){
body = cblistval(parts, 0, &blen);
params = cbmapopen();
filedata = cbmimebreak(body, blen, params, &filesize);
if((tmp = cbmapget(params, "FILENAME", -1, NULL)) != NULL) filename = cbmemdup(tmp, -1);
cbmapclose(params);
}
cblistclose(parts);
free(cdata);
free(bound);
} else if((tmp = getenv("PATH_INFO")) != NULL){
if(tmp[0] == '/') tmp++;
getname = cburldecode(tmp, NULL);
if(cbstrfwmatch(getname, "../") || strstr(getname, "/../") != NULL){
free(getname);
getname = NULL;
}
} else if((tmp = getenv("QUERY_STRING")) != NULL && (tmp = strstr(tmp, "delfile="))){
tmp += 8;
delname = cburldecode(tmp, NULL);
if(cbstrfwmatch(delname, "../") || strstr(delname, "/../") != NULL){
free(delname);
delname = NULL;
}
}
if(getname){
/* send data of the file */
if(chdir(datadir) == 0 && (ifp = fopen(getname, "rb")) != NULL){
printf("Content-Type: %s\r\n", gettype(getname));
printf("Cache-Control: no-cache, must-revalidate\r\n");
printf("Pragma: no-cache\r\n");
printf("\r\n");
while((c = fgetc(ifp)) != EOF){
putchar(c);
}
fclose(ifp);
} else {
printf("Status: 404 Not Found\r\n");
printf("Content-Type: text/plain; charset=%s\r\n", enc);
printf("Cache-Control: no-cache, must-revalidate\r\n");
printf("Pragma: no-cache\r\n");
printf("\r\n");
printf("Not Found\n");
printf("%s\n", getname);
}
} else {
/* output headers */
printf("Content-Type: text/html; charset=%s\r\n", enc);
printf("Cache-Control: no-cache, must-revalidate\r\n");
printf("Pragma: no-cache\r\n");
printf("\r\n");
htmlprintf("<?xml version=\"1.0\" encoding=\"%s\"?>\n", enc);
htmlprintf("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
htmlprintf("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"%s\" lang=\"%s\">\n",
lang, lang);
htmlprintf("<head>\n");
htmlprintf("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=%s\" />\n", enc);
htmlprintf("<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />\n");
htmlprintf("<link rel=\"contents\" href=\"./\" />\n");
htmlprintf("<title>%@</title>\n", title);
htmlprintf("<style type=\"text/css\">\n");
htmlprintf("body { background-color: #eeeeee; color: #111111;"
" margin: 1em 1em; padding: 1em 1em; }\n");
htmlprintf("hr { margin: 1.5em 0em; height: 1pt;"
" color: #999999; background-color: #999999; border: none; }\n");
htmlprintf("a { color: #0022aa; text-decoration: none; }\n");
htmlprintf("a:hover { color: #1144ff; text-decoration: underline; }\n");
htmlprintf("table { border-collapse: collapse; }\n");
htmlprintf("th,td { text-align: left; }\n");
htmlprintf("th { padding: 0.1em 0.4em; border: none;"
" font-size: smaller; font-weight: normal; }\n");
htmlprintf("td { padding: 0.2em 0.5em; background-color: #ddddee;"
" border: 1pt solid #888888; }\n");
htmlprintf(".note { margin: 0.5em 0.5em; text-align: right; }\n");
htmlprintf("</style>\n");
htmlprintf("</head>\n");
htmlprintf("<body>\n");
htmlprintf("<h1>%@</h1>\n", title);
htmlprintf("<hr />\n");
htmlprintf("<form method=\"post\" enctype=\"multipart/form-data\" action=\"%s\">\n",
scriptname);
htmlprintf("<div>\n");
htmlprintf("<input type=\"file\" name=\"file\" size=\"64\""
" tabindex=\"1\" accesskey=\"0\" />\n");
htmlprintf("<input type=\"submit\" value=\"UPLOAD\" tabindex=\"2\" accesskey=\"1\" />\n");
htmlprintf("<a href=\"%s\">[RELOAD]</a>\n", scriptname);
htmlprintf("</div>\n");
htmlprintf("</form>\n");
htmlprintf("<hr />\n");
/* change the currnt directory */
if(chdir(datadir) == -1){
htmlprintf("<p>Changing the current directory was failed.</p>\n");
htmlprintf("<hr />\n");
} else {
/* save the file */
if(filedata && filename){
sname = filename;
if((ebuf = cbiconv(filename, -1, enc, "UTF-8", NULL, NULL)) != NULL) sname = ebuf;
if((tmp = strrchr(sname, '/')) != NULL) sname = tmp + 1;
if((tmp = strrchr(sname, '\\')) != NULL) sname = tmp + 1;
if(ebuf){
while((tmp = strstr(sname, "\xc2\xa5")) != NULL){
sname = tmp + 2;
}
}
dbuf = NULL;
if(ebuf && (dbuf = cbiconv(sname, -1, "UTF-8", enc, NULL, NULL)) != NULL) sname = dbuf;
if(getdirsize(".") + filesize > quota){
htmlprintf("<p>Exceeding the quota. -- %@</p>\n", sname);
} else if(!cbwritefile(sname, filedata, filesize)){
htmlprintf("<p>Uploading was failed. -- %@</p>\n", sname);
} else {
htmlprintf("<p>Uploading was succeeded. -- %@</p>\n", sname);
}
htmlprintf("<hr />\n");
free(dbuf);
free(ebuf);
} else if(delname){
/* delete the file */
if(unlink(delname) == 0){
htmlprintf("<p>Deleting was succeeded. -- %@</p>\n", delname);
} else {
htmlprintf("<p>Deleting was failed. -- %@</p>\n", delname);
}
htmlprintf("<hr />\n");
}
/* show the file list */
if((files = cbdirlist(".")) != NULL){
cblistsort(files);
htmlprintf("<table summary=\"files\">\n");
htmlprintf("<tr>\n");
htmlprintf("<th abbr=\"name\">Name</th>\n");
htmlprintf("<th abbr=\"size\">Size</th>\n");
htmlprintf("<th abbr=\"mtime\">Modified Time</th>\n");
htmlprintf("<th abbr=\"act\">Actions</th>\n");
htmlprintf("</tr>\n");
for(i = 0; i < cblistnum(files); i++){
sname = cblistval(files, i, NULL);
if(!strcmp(sname, ".") || !strcmp(sname, "..")) continue;
if(!cbfilestat(sname, &sdir, &ssize, &stime) || sdir) continue;
htmlprintf("<tr>\n");
htmlprintf("<td>%@</td>\n", sname);
htmlprintf("<td>%d</td>\n", ssize);
htmlprintf("<td>%@</td>\n", datestr(stime));
htmlprintf("<td>\n");
htmlprintf("<a href=\"%s/%?\">[GET]</a>", scriptname, sname);
htmlprintf(" / ");
htmlprintf("<a href=\"%s?delfile=%?\">[DEL]</a>", scriptname, sname);
htmlprintf("</td>\n");
htmlprintf("</tr>\n");
}
htmlprintf("</table>\n");
cblistclose(files);
total = getdirsize(".");
sprintf(numbuf, "%.2f%%", (total * 100.0) / quota);
htmlprintf("<div class=\"note\">Capacity: %@ (%d/%d)</div>\n", numbuf, total, quota);
} else {
htmlprintf("<p>Listing files in the data directory was failed.</p>\n");
}
htmlprintf("<hr />\n");
}
/* output footers */
htmlprintf("<div class=\"note\">Powered by QDBM %@.</div>\n", dpversion);
htmlprintf("</body>\n");
htmlprintf("</html>\n");
}
/* release resources */
if(getname) free(getname);
if(delname) free(delname);
if(filename) free(filename);
if(filedata) free(filedata);
if(lines) cblistclose(lines);
return 0;
}
/* skip the label of a line */
const char *skiplabel(const char *str){
if(!(str = strchr(str, ':'))) return str;
str++;
while(*str != '\0' && (*str == ' ' || *str == '\t')){
str++;
}
return str;
}
/* HTML-oriented printf */
void htmlprintf(const char *format, ...){
va_list ap;
char *tmp;
unsigned char c;
va_start(ap, format);
while(*format != '\0'){
if(*format == '%'){
format++;
switch(*format){
case 's':
tmp = va_arg(ap, char *);
if(!tmp) tmp = "(null)";
printf("%s", tmp);
break;
case 'd':
printf("%d", va_arg(ap, int));
break;
case '@':
tmp = va_arg(ap, char *);
if(!tmp) tmp = "(null)";
while(*tmp){
switch(*tmp){
case '&': printf("&"); break;
case '<': printf("<"); break;
case '>': printf(">"); break;
case '"': printf("""); break;
default: putchar(*tmp); break;
}
tmp++;
}
break;
case '?':
tmp = va_arg(ap, char *);
if(!tmp) tmp = "(null)";
while(*tmp){
c = *(unsigned char *)tmp;
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9') || (c != '\0' && strchr("_-.", c))){
putchar(c);
} else if(c == ' '){
putchar('+');
} else {
printf("%%%02X", c);
}
tmp++;
}
break;
case '%':
putchar('%');
break;
}
} else {
putchar(*format);
}
format++;
}
va_end(ap);
}
/* get the total size of files in a directory */
int getdirsize(const char *path){
CBLIST *files;
const char *sname;
int i, total, isdir, size;
total = 0;
if((files = cbdirlist(path)) != NULL){
for(i = 0; i < cblistnum(files); i++){
sname = cblistval(files, i, NULL);
if(!strcmp(sname, ".") || !strcmp(sname, "..")) continue;
if(!cbfilestat(sname, &isdir, &size, NULL) || isdir) continue;
total += size;
}
}
return total;
}
/* get static string of the date */
const char *datestr(time_t t){
static char buf[32];
struct tm *stp;
if(!(stp = localtime(&t))) return "0000/00/00 00:00:00";
sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d",
stp->tm_year + 1900, stp->tm_mon + 1, stp->tm_mday,
stp->tm_hour, stp->tm_min, stp->tm_sec);
return buf;
}
/* get the media type of a file */
const char *gettype(const char *path){
char *types[] = {
".txt", "text/plain", ".asc", "text/plain", ".html", "text/html", ".htm", "text/html",
".mht", "message/rfc822", ".sgml", "application/sgml", ".sgm", "application/sgml",
".xml", "application/xml", ".rtf", "application/rtf", ".pdf", "application/pdf",
".doc", "application/msword", ".xls", "application/vnd.ms-excel",
".ppt", "application/vnd.ms-powerpoint", ".xdw", "application/vnd.fujixerox.docuworks",
".zip", "application/zip", ".tar", "application/x-tar", ".gz", "application/x-gzip",
".png", "image/png", ".gif", "image/gif", ".jpg", "image/jpeg", ".jpeg", "image/jpeg",
".tif", "image/tiff", ".tiff", "image/tiff", ".bmp", "image/bmp", ".mid", "audio/midi",
".midi", "audio/midi", ".mp3", "audio/mpeg", ".wav", "audio/x-wav", ".mpg", "video/mpeg",
".mpeg", "video/mpeg", NULL
};
int i;
for(i = 0; types[i]; i += 2){
if(cbstrbwimatch(path, types[i])) return types[i+1];
}
return "application/octet-stream";
}
/* END OF FILE */
|