Exemple #1
0
/**
 * Create a bookmark entry in the DB.
 */
int add_bookmark(Page* page, char* line, bool update) {
    datum key;
    datum val;
    char* sectpage = (char*) malloc(strlen(page->name) + 3);
    sectpage[0] = '0' + page->sect;
    sectpage[1] = ':';
    strcpy(sectpage + 2, page->name);
    sectpage[strlen(page->name) + 3] = '\0';
    _datum(&key, sectpage);
    _datum(&val, line);

    if (gdbm_store(db, key, val, GDBM_INSERT)) {
        /* If we couldn't add a record, this may be
         * because the key exists. If update requested,
         * remove the record and try again.
         */
        if (update) {
            if (gdbm_store(db, key, val, GDBM_REPLACE))
                return -1;
            update_bookmark_for_page(page, line);
        }
        return -1;
    }

    insert_bookmark(page, line);

    return 0;
}
int
main(int argc, char *argv[], char **envp)
{
    extern char *getenv();
    char *p;
    int length;
    Str qs = NULL;
    struct parsed_tagarg *cgiarg;
    char *mode;
    char *bmark;
    char *url;
    char *title;
    char *sent_cookie;

    p = getenv("REQUEST_METHOD");
    if (p == NULL || strcasecmp(p, "post"))
	goto request_err;
    p = getenv("CONTENT_LENGTH");
    if (p == NULL || (length = atoi(p)) <= 0)
	goto request_err;

    qs = Strfgets(stdin);
    Strchop(qs);
    if (qs->length != length)
	goto request_err;
    cgiarg = cgistr2tagarg(qs->ptr);

    p = getenv("LOCAL_COOKIE_FILE");
    if (p) {
	FILE *f = fopen(p, "r");
	if (f) {
	    Local_cookie = Strfgets(f)->ptr;
	    fclose(f);
	}
    }
    sent_cookie = tag_get_value(cgiarg, "cookie");
    if (sent_cookie == NULL || Local_cookie == NULL ||
	strcmp(sent_cookie, Local_cookie) != 0) {
	/* local cookie doesn't match: It may be an illegal invocation */
	printf("Content-Type: text/plain\n\n");
	printf("Local cookie doesn't match: It may be an illegal invocation\n");
	exit(1);
    }

    mode = tag_get_value(cgiarg, "mode");
    bmark = expandPath(tag_get_value(cgiarg, "bmark"));
    url = tag_get_value(cgiarg, "url");
    title = tag_get_value(cgiarg, "title");
    if (bmark == NULL || url == NULL)
	goto request_err;
    if (mode && !strcmp(mode, "panel")) {
	if (title == NULL)
	    title = "";
	print_bookmark_panel(bmark, url, title);
    }
    else if (mode && !strcmp(mode, "register")) {
	printf("Content-Type: text/plain\n");
	if (insert_bookmark(bmark, cgiarg)) {
	    printf("w3m-control: BACK\n");
	    printf("w3m-control: BACK\n");
	}
	printf("\n");
    }
    return 0;

  request_err:
    printf("Content-Type: text/plain\n\n");
    printf("Incomplete Request: %s\n", qs ? qs->ptr : "(null)");
    exit(1);
}