phpbbs.post.c

来自「linux/unix环境下的建站系统」· C语言 代码 · 共 1,270 行 · 第 1/3 页

C
1,270
字号
#include "php_kbs_bbs.h"  int check_last_post_time(struct user_info *uinfo) {    int lastpost, now;    lastpost = uinfo->lastpost;    now = time(0);    uinfo->lastpost = now;    if (abs(now - lastpost) < 6) {        return 1;    } else {        return 0;    }}PHP_FUNCTION(bbs_getattachtmppath){    char buf[MAXPATH];    if (getCurrentUser() == NULL) {        RETURN_FALSE;        //用户未初始化    }    getattachtmppath(buf, MAXPATH, getSession());    RETURN_STRING(buf, 1);}PHP_FUNCTION(bbs_filteruploadfilename){    char *filename;    int flen;    if (zend_parse_parameters(1 TSRMLS_CC, "s/", &filename, &flen) == FAILURE) {        WRONG_PARAM_COUNT;    }    if (!flen) {        RETURN_FALSE;    }    filename = filter_upload_filename(filename);    RETURN_STRING(filename, 1);}static int dump_attachment_info(zval *ret, struct ea_attach_info *ai){    zval *element;    int count;    if (array_init(ret) == FAILURE) {        return -1;    }        for(count=0; count<MAXATTACHMENTCOUNT&&ai[count].name[0]; count++) {        MAKE_STD_ZVAL(element);        array_init(element);        add_assoc_string(element, "name", ai[count].name, 1);        add_assoc_long(element, "size", ai[count].size);        add_assoc_long(element, "pos", ai[count].offset+ATTACHMENT_SIZE);        zend_hash_index_update(Z_ARRVAL_P(ret), count, (void *) &element, sizeof(zval *), NULL);    }    return 0;}PHP_FUNCTION(bbs_upload_read_fileinfo){    struct ea_attach_info ai[MAXATTACHMENTCOUNT];    upload_read_fileinfo(ai, getSession());    if (dump_attachment_info(return_value, ai)) {        RETURN_FALSE;    }}PHP_FUNCTION(bbs_upload_del_file){    char *ofilename;    int oflen, ret;    if (zend_parse_parameters(1 TSRMLS_CC, "s", &ofilename, &oflen) == FAILURE) {        WRONG_PARAM_COUNT;    }    if (!oflen) {        RETURN_ERROR(GENERAL);    }    ret = upload_del_file(ofilename, getSession());    switch(ret) {        case -1:            RETURN_ERROR(GENERAL);            break;        case -2:            RETURN_ERROR(ATTACH_DELNONE);            break;        default:            RETURN_LONG(0);            break;    }}PHP_FUNCTION(bbs_upload_add_file){    char *filename, *ofilename;    int flen, oflen, ret;    if (zend_parse_parameters(2 TSRMLS_CC, "ss/", &filename, &flen, &ofilename, &oflen) == FAILURE) {        WRONG_PARAM_COUNT;    }    if (!flen || !oflen) {        RETURN_ERROR(GENERAL);    }    ret = upload_add_file(filename, ofilename, getSession());    switch(ret) {        case -1:            RETURN_ERROR(GENERAL);            break;        case -2:            RETURN_ERROR(ATTACH_CLIMIT);            break;        case -3:            RETURN_ERROR(ATTACH_INVALIDNAME);            break;        case -4:            RETURN_ERROR(ATTACH_DUPNAME);            break;        case -5:            RETURN_ERROR(GENERAL);            break;        case -6:            RETURN_ERROR(ATTACH_SLIMIT);            break;        default:            RETURN_LONG(0);            break;                }}static int update_index_attpos(const struct boardheader *bh, int ent, struct fileheader *fh, int attpos){/* TODO: update .ORIGIN? */    struct write_dir_arg dirarg;    char dir[PATHLEN];    int ret;    setbdir(DIR_MODE_NORMAL, dir, bh->filename);    init_write_dir_arg(&dirarg);    dirarg.filename = dir;      dirarg.ent = ent;    fh->attachment = attpos;    if(change_post_flag(&dirarg,DIR_MODE_NORMAL, bh, fh, FILE_ATTACHPOS_FLAG, fh, false,getSession())!=0)        ret = 1;    else        ret = 0;    free_write_dir_arg(&dirarg);    return(ret);}PHP_FUNCTION(bbs_attachment_add){    struct ea_attach_info ai[MAXATTACHMENTCOUNT];    const boardheader_t *brd;    struct fileheader f;    char dir[PATHLEN];    char* board;    int ent,fd,ret;    long id;    int board_len;    char *filename, *ofilename;    int flen, oflen;    	int ac = ZEND_NUM_ARGS();    if (ac != 4 || zend_parse_parameters(4 TSRMLS_CC, "slss", &board, &board_len,&id, &filename, &flen, &ofilename, &oflen) == FAILURE) {		WRONG_PARAM_COUNT;	}	brd = getbcache(board);    if (!brd)        RETURN_ERROR(BOARD_NONEXIST);    if (!(brd->flag & BOARD_ATTACH) && !HAS_PERM(getCurrentUser(), PERM_SYSOP)) {        RETURN_ERROR(ATTACH_ADDPERM);    }    ent = get_ent_from_id_ext(DIR_MODE_NORMAL, id, brd->filename, &f);    if (ent < 0)    {        RETURN_ERROR(POST_NONEXIST);    }    ret = deny_modify_article(brd, &f, DIR_MODE_NORMAL, getSession());    if (ret) {        RETURN_ERROR(POST_MODPERM);    }    setbfile(dir, brd->filename, f.filename);    add_edit_mark(dir, 0, f.title, getSession());        fd = open(dir, O_RDWR);    if (fd < 0)        RETURN_ERROR(GENERAL);    ret = ea_locate(fd, ai);    if (ret>=0) {        int count, size=0;        struct stat st;        for(count=0; count<MAXATTACHMENTCOUNT&&ai[count].name[0]; count++) {            size += ai[count].size;        }        if(stat(filename,&st)||!S_ISREG(st.st_mode)){            unlink(filename);            ret = PHPBBS_ERROR_GENERAL;        } else if((size+st.st_size)>MAXATTACHMENTSIZE && !HAS_PERM(getCurrentUser(), PERM_SYSOP)){            unlink(filename);            ret = PHPBBS_ERROR_ATTACH_SLIMIT;        } else {            ret = ea_append(fd, ai, filename, ofilename);            if (ret < 0) ret = PHPBBS_ERROR_GENERAL;        }    }    close(fd);    if (ret>=0)        ret = dump_attachment_info(return_value, ai);    if (ret < 0) {        RETURN_LONG(ret);    } else {        update_index_attpos(brd, ent, &f, ai[0].offset);    }}PHP_FUNCTION(bbs_attachment_del){    struct ea_attach_info ai[MAXATTACHMENTCOUNT];    const boardheader_t *brd;    struct fileheader f;    char dir[PATHLEN];    char* board;    int ent,fd,ret;    long id,pos;    int board_len;	int ac = ZEND_NUM_ARGS();    if (ac != 3 || zend_parse_parameters(3 TSRMLS_CC, "sll", &board, &board_len,&id,&pos) == FAILURE) {		WRONG_PARAM_COUNT;	}	brd = getbcache(board);    if (!brd)        RETURN_ERROR(BOARD_NONEXIST);    ent = get_ent_from_id_ext(DIR_MODE_NORMAL, id, brd->filename, &f);    if (ent < 0)    {        RETURN_ERROR(POST_NONEXIST);    }    ret = deny_modify_article(brd, &f, DIR_MODE_NORMAL, getSession());    if (ret) {        RETURN_ERROR(POST_MODPERM);    }    setbfile(dir, brd->filename, f.filename);    add_edit_mark(dir, 0, f.title, getSession());        fd = open(dir, O_RDWR);    if (fd < 0)        RETURN_ERROR(GENERAL);    ret = ea_locate(fd, ai);    if (ret>=0) {        ret = ea_delete(fd, ai, pos);        if (ret < 0) ret = PHPBBS_ERROR_ATTACH_DELNONE;    }    close(fd);    if (ret>=0)        ret = dump_attachment_info(return_value, ai);    if (ret < 0) {        RETURN_LONG(ret);    } else {        update_index_attpos(brd, ent, &f, ai[0].offset);    }}PHP_FUNCTION(bbs_attachment_list){    struct ea_attach_info ai[MAXATTACHMENTCOUNT];    const boardheader_t *brd;    struct fileheader f;    char dir[PATHLEN];    char* board;    int ent,fd,ret;    long id;    int board_len;	int ac = ZEND_NUM_ARGS();    if (ac != 2 || zend_parse_parameters(2 TSRMLS_CC, "sl", &board, &board_len,&id) == FAILURE) {		WRONG_PARAM_COUNT;	}	brd = getbcache(board);    if (!brd)        RETURN_ERROR(BOARD_NONEXIST);    ent = get_ent_from_id_ext(DIR_MODE_NORMAL, id, brd->filename, &f);    if (ent < 0)    {        RETURN_ERROR(POST_NONEXIST);    }    setbfile(dir, brd->filename, f.filename);    fd = open(dir, O_RDONLY);    if (fd < 0)        RETURN_ERROR(GENERAL);    ret = ea_locate(fd, ai);    close(fd);    if (ret<0 || dump_attachment_info(return_value, ai)) {        RETURN_ERROR(GENERAL);    }}PHP_FUNCTION(bbs_postarticle){	char *boardName, *title, *content;    char path[80],board[80];	int blen, tlen, clen;    long sig, mailback, is_tex;	long reid;    struct fileheader *oldx = NULL;    const boardheader_t *brd;    long local_save, outgo, anony;    struct fileheader post_file, oldxx;    char filepath[MAXPATH];    int anonyboard, color;	int retvalue;    FILE *fp;	int ac = ZEND_NUM_ARGS();    /*     * getting arguments      */        if (ac == 7) {        if (zend_parse_parameters(7 TSRMLS_CC, "ss/s/llll", &boardName, &blen, &title, &tlen, &content, &clen, &sig, &reid, &outgo,&anony) == FAILURE) {            WRONG_PARAM_COUNT;        }        mailback = 0;        is_tex = 0;    } else if (ac == 9) {        if (zend_parse_parameters(9 TSRMLS_CC, "ss/s/llllll", &boardName, &blen, &title, &tlen, &content, &clen, &sig, &reid, &outgo,&anony,&mailback,&is_tex) == FAILURE) {            WRONG_PARAM_COUNT;        }    } else {        WRONG_PARAM_COUNT;    }    brd = getbcache(boardName);    if (getCurrentUser() == NULL) {        RETURN_FALSE;	}     if (brd == 0)        RETURN_LONG(-1); //错误的讨论区名称    if (brd->flag&BOARD_GROUP)        RETURN_LONG(-2); //二级目录版    strcpy(board, brd->filename);    if (tlen > 256) title[256] = '\0';    process_control_chars(title,NULL);    if (tlen == 0)        RETURN_LONG(-3); //标题为NULL    if (true == checkreadonly(board) || !haspostperm(getCurrentUser(), board))        RETURN_LONG(-4); //此讨论区是唯读的, 或是您尚无权限在此发表文章.#ifdef NEWSMTH    if(!check_score_level(getCurrentUser(),brd)){        RETURN_LONG(-21);    }#endif /* NEWSMTH */    if (deny_me(getCurrentUser()->userid, board) && !HAS_PERM(getCurrentUser(), PERM_SYSOP))        RETURN_LONG(-5); //很抱歉, 你被版务人员停止了本版的post权利.    if (check_last_post_time(getSession()->currentuinfo)) {        RETURN_LONG(-6); // 两次发文间隔过密, 请休息几秒后再试    }    if(reid > 0){        int pos;int fd;		setbfile(path,board,DOT_DIR);		fd = open(path,O_RDWR);		if(fd < 0) RETURN_LONG(-7); //索引文件不存在		get_records_from_id(fd,reid,&oldxx,1,&pos);		close(fd);        if (pos >= 0) {            oldx = &oldxx;            if (oldx->accessed[1] & FILE_READ) {                RETURN_LONG(-8); //本文不能回复            }        }    }    local_save = (is_outgo_board(board) && outgo) ? 0 : 1;    memset(&post_file, 0, sizeof(post_file));    anonyboard = anonymousboard(board); /* 是否为匿名版 */    /*     * 自动生成 POST 文件名      */    setbfile(filepath, board, "");    if (GET_POSTFILENAME(post_file.filename, filepath) != 0) {        RETURN_LONG(-9);    }    setbfile(filepath, board, post_file.filename);    anony = anonyboard && anony;    strncpy(post_file.owner, anony ? board : getCurrentUser()->userid, OWNER_LEN);    post_file.owner[OWNER_LEN - 1] = 0;    if ((!strcmp(board, "Announce")) && (!strcmp(post_file.owner, board)))        strcpy(post_file.owner, "SYSOP");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?