phpbbslib.c

来自「bbs server linux平台下软件源码」· C语言 代码 · 共 2,310 行 · 第 1/5 页

C
2,310
字号
            ret = 0;        } else {            if (getcurrentuinfo()) {                assign_userinfo(user_array, getcurrentuinfo(), getcurrentuinfo_num());            } else                ret = 0;        }    } else if (ZEND_NUM_ARGS() != 0)        WRONG_PARAM_COUNT;    if (ret)        ret = getcurrentuinfo_num();    RETURN_LONG(ret);}static PHP_FUNCTION(bbs_getcurrentuser){    zval *user_array;    long ret;    MAKE_STD_ZVAL(user_array);    getcwd(old_pwd, 1023);    chdir(BBSHOME);    old_pwd[1023] = 0;    if (zend_parse_parameters(1 TSRMLS_CC, "a", &user_array) != SUCCESS) {        WRONG_PARAM_COUNT;    }    if (array_init(user_array) != SUCCESS) {        ret = 0;    } else {        if (getcurrentuser()) {            assign_user(user_array, getcurrentuser(), getcurrentuser_num());            ret = getcurrentuser_num();        } else            ret = 0;    }    RETURN_LONG(ret);}static PHP_FUNCTION(bbs_setonlineuser){    zval *user_array;    char *userid;    int userid_len;    long utmpnum;    long utmpkey;    long ret;    struct user_info *pui;    int idx;    struct userec *user;    long compat_telnet;    MAKE_STD_ZVAL(user_array);    if (ZEND_NUM_ARGS() == 4) {        if (zend_parse_parameters(4 TSRMLS_CC, "slla", &userid, &userid_len, &utmpnum, &utmpkey, &user_array) != SUCCESS) {            WRONG_PARAM_COUNT;        }        compat_telnet = false;    } else {        if (zend_parse_parameters(5 TSRMLS_CC, "sllal", &userid, &userid_len, &utmpnum, &utmpkey, &user_array, &compat_telnet) != SUCCESS) {            WRONG_PARAM_COUNT;        }    }    if (userid_len > IDLEN)        RETURN_LONG(1);    if (utmpnum < 0 || utmpnum >= MAXACTIVE)        RETURN_LONG(2);    if (userid_len == 0)        userid = NULL;    if ((ret = www_user_init(utmpnum, userid, utmpkey, &user, &pui, compat_telnet)) == 0) {        setcurrentuinfo(pui, utmpnum);        idx = getuser(pui->userid, &user);        setcurrentuser(user, idx);        if (user == NULL)            RETURN_LONG(6);        if (array_init(user_array) != SUCCESS)            ret = 7;        else {            assign_userinfo(user_array, pui, idx);            ret = 0;        }    }    u_info = pui;    RETURN_LONG(ret);}static void flush_buffer(buffered_output_t * output){    *(output->outp) = '\0';    zend_printf("%s", output->buf);    output->outp = output->buf;}static int buffered_output(char *buf, size_t buflen, void *arg){    buffered_output_t *output = (buffered_output_t *) arg;    if (output->buflen <= buflen) {        output->flush(output);        zend_printf("%s", buf);        return 0;    }    if ((output->buflen - (output->outp - output->buf) - 1) <= buflen)        output->flush(output);    strncpy(output->outp, buf, buflen);    output->outp += buflen;    return 0;}static char *output_buffer = NULL;static int output_buffer_len = 0;static int output_buffer_size = 0;static void output_printf(char *buf, size_t len){    int bufLen;    int n, newsize;    char *newbuf;    if (output_buffer == NULL) {        output_buffer = (char *) emalloc(51200);        //first 50k        if (output_buffer == NULL) {            return;        }        output_buffer_size = 51200;    }    bufLen = strlen(buf);    if (bufLen > len) {        bufLen = len;    }    n = 1 + output_buffer_len + bufLen - output_buffer_size;    if (n >= 0) {        newsize = output_buffer_size + ((n / 102400) + 1) * 102400;     //n*100k every time        newbuf = (char *) erealloc(output_buffer, newsize);        if (newbuf == NULL) {            return;        }        output_buffer = newbuf;        output_buffer_size = newsize;    }    memcpy(output_buffer + output_buffer_len, buf, bufLen);    output_buffer_len += bufLen;}static char *get_output_buffer(){    return output_buffer;}static int get_output_buffer_len(){    int len = output_buffer_len;    output_buffer_len = 0;    return len;}static int new_buffered_output(char *buf, size_t buflen, void *arg){    output_printf(buf, buflen);    return 0;}static void new_flush_buffer(buffered_output_t * output){}static PHP_FUNCTION(bbs_printansifile){    char *filename;    long filename_len;    long linkmode;    char *ptr;    int fd;    struct stat st;    const int outbuf_len = 4096;    buffered_output_t *out;    char *attachlink;    long attachlink_len;    getcwd(old_pwd, 1023);    chdir(BBSHOME);    old_pwd[1023] = 0;    if (ZEND_NUM_ARGS() == 1) {        if (zend_parse_parameters(1 TSRMLS_CC, "s", &filename, &filename_len) != SUCCESS) {            WRONG_PARAM_COUNT;        }        linkmode = 1;        attachlink = NULL;    } else if (ZEND_NUM_ARGS() == 2) {        if (zend_parse_parameters(2 TSRMLS_CC, "sl", &filename, &filename_len, &linkmode) != SUCCESS) {            WRONG_PARAM_COUNT;        }        attachlink = NULL;    } else if (zend_parse_parameters(3 TSRMLS_CC, "sls", &filename, &filename_len, &linkmode, &attachlink, &attachlink_len) != SUCCESS) {        WRONG_PARAM_COUNT;    }    fd = open(filename, O_RDONLY);    if (fd < 0)        RETURN_LONG(2);    if (fstat(fd, &st) < 0) {        close(fd);        RETURN_LONG(2);    }    if (!S_ISREG(st.st_mode)) {        close(fd);        RETURN_LONG(2);    }    if (st.st_size <= 0) {        close(fd);        RETURN_LONG(2);    }    ptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);    close(fd);    if (ptr == NULL)        RETURN_LONG(-1);    if ((out = alloc_output(outbuf_len)) == NULL) {        munmap(ptr, st.st_size);        RETURN_LONG(2);    }/*	override_default_output(out, buffered_output);	override_default_flush(out, flush_buffer);*/    override_default_output(out, new_buffered_output);    override_default_flush(out, new_flush_buffer);    if (!sigsetjmp(bus_jump, 1)) {        signal(SIGBUS, sigbus);        signal(SIGSEGV, sigbus);        output_ansi_html(ptr, st.st_size, out, attachlink);        free_output(out);    }    signal(SIGBUS, SIG_IGN);    signal(SIGSEGV, SIG_IGN);    munmap(ptr, st.st_size);    RETURN_STRINGL(get_output_buffer(), get_output_buffer_len(), 1);}static void bbs_make_article_array(zval * array, struct fileheader *fh, char *flags, size_t flags_len){    add_assoc_string(array, "FILENAME", fh->filename, 1);    add_assoc_long(array, "ID", fh->id);    add_assoc_long(array, "GROUPID", fh->groupid);    add_assoc_long(array, "REID", fh->reid);    add_assoc_long(array, "POSTTIME", get_posttime(fh));    add_assoc_stringl(array, "INNFLAG", fh->innflag, sizeof(fh->innflag), 1);    add_assoc_string(array, "OWNER", fh->owner, 1);    add_assoc_string(array, "TITLE", fh->title, 1);    add_assoc_long(array, "LEVEL", fh->level);    add_assoc_stringl(array, "FLAGS", flags, flags_len, 1);    add_assoc_long(array, "ATTACHPOS", fh->attachment);}static PHP_FUNCTION(bbs_searchtitle){    char *board, *title, *title2, *title3, *author;    long bLen, tLen, tLen2, tLen3, aLen;    long date, mmode, origin, attach;    bcache_t bh;    char dirpath[STRLEN];    int fd;    struct stat buf;    struct flock ldata;    struct fileheader *ptr1;    char *ptr;    long int threadsFounded;    unsigned int *IDList;    unsigned int *IDList2;    unsigned int *index;    long int threads;    int total, i;    zval *element;    int is_bm;    char flags[4];              /* flags[0]: flag character                                 * flags[1]: imported flag                                 * flags[2]: no reply flag                                 * flags[3]: attach flag                                 */    struct boardheader *bp;    int found;    if (ZEND_NUM_ARGS() != 9        || zend_parse_parameters(9 TSRMLS_CC, "sssssllll", &board, &bLen, &title, &tLen, &title2, &tLen2, &title3, &tLen3, &author, &aLen, &date, &mmode, &attach, &origin) != SUCCESS) {        WRONG_PARAM_COUNT;    }    if (date <= 0)        date = 9999;    if (date > 9999)        date = 9999;    if ((bp = getbcache(board)) == NULL) {        RETURN_FALSE;    }    is_bm = is_BM(bp, currentuser);    if (getboardnum(board, &bh) == 0)        RETURN_LONG(-1);        //"错误的讨论区";    if (!check_read_perm(currentuser, &bh))        RETURN_LONG(-2);        //您无权阅读本版;    setbdir(DIR_MODE_NORMAL, dirpath, bh.filename);    if ((fd = open(dirpath, O_RDONLY, 0)) == -1)        RETURN_LONG(-3);    ldata.l_type = F_RDLCK;    ldata.l_whence = 0;    ldata.l_len = 0;    ldata.l_start = 0;    if (fcntl(fd, F_SETLKW, &ldata) == -1) {        close(fd);        RETURN_LONG(-200);    }    if (fstat(fd, &buf) == -1) {        ldata.l_type = F_UNLCK;        fcntl(fd, F_SETLKW, &ldata);        close(fd);        RETURN_LONG(-201);    }    total = buf.st_size / sizeof(struct fileheader);    if ((i = safe_mmapfile_handle(fd, O_RDONLY, PROT_READ, MAP_SHARED, (void **) &ptr, (size_t *) & buf.st_size)) != 1) {        if (i == 2)            end_mmapfile((void *) ptr, buf.st_size, -1);        ldata.l_type = F_UNLCK;        fcntl(fd, F_SETLKW, &ldata);        close(fd);        RETURN_LONG(-4);    }    /*     * fetching articles      */    if (array_init(return_value) == FAILURE) {        RETURN_LONG(-210);    }#ifdef HAVE_BRC_CONTROL    brc_initial(currentuser->userid, board);#endif    IDList = emalloc((1000) * sizeof(long int));    if (IDList == NULL) {        RETURN_LONG(-211);    }    IDList2 = emalloc((50000) * sizeof(long int));    if (IDList2 == NULL) {        RETURN_LONG(-212);    }    index = emalloc((50000) * sizeof(long int));    if (index == NULL) {        RETURN_LONG(-213);    }    threadsFounded = 0;    threads = 0;    ptr1 = (struct fileheader *) ptr;    for (i = total - 1; i >= 0; i--) {        if (foundInArray(ptr1[i].groupid, IDList2, threads) == -1) {            found = Search_Bin(ptr, ptr1[i].groupid, 0, total - 1);            if (found >= 0) {                IDList2[threads] = ptr1[i].groupid;                index[threads] = found;                threads++;            }        } else {            break;        }        if (threads > 10000)            break;        if (title[0] && !strcasestr(ptr1[i].title, title))            continue;        if (title2[0] && !strcasestr(ptr1[i].title, title2))            continue;        if (author[0] && strcasecmp(ptr1[i].owner, author))            continue;        if (title3[0] && strcasestr(ptr1[i].title, title3))            continue;        if (abs(time(0) - get_posttime(ptr1 + i)) > date * 86400)            break;        if (mmode && !(ptr1[i].accessed[0] & FILE_MARKED) && !(ptr1[i].accessed[0] & FILE_DIGEST))            continue;        if (origin && (ptr1[i].groupid != ptr1[i].id))            continue;        if (origin && ptr1[i].attachment == 0)            continue;        if (foundInArray(ptr1[i].groupid, IDList, threadsFounded) == -1) {            int found, tmp = foundInArray(ptr1[i].groupid, IDList2, threads);            found = index[tmp];            IDList[threadsFounded] = ptr1[i].groupid;            threadsFounded++;            MAKE_STD_ZVAL(element);            array_init(element);            flags[0] = get_article_flag(ptr1 + found, currentuser, board, is_bm);            if (is_bm && (ptr1[found].accessed[0] & FILE_IMPORTED))                flags[1] = 'y';            else                flags[1] = 'n';            if (ptr1[found].accessed[1] & FILE_READ)                flags[2] = 'y';            else                flags[2] = 'n';            if (ptr1[found].attachment)                flags[3] = '@';            else                flags[3] = ' ';            bbs_make_article_array(element, ptr1 + found, flags, sizeof(flags));            add_assoc_long(element, "threadsnum", tmp);            zend_hash_index_update(Z_ARRVAL_P(return_value), threadsFounded, (void *) &element, sizeof(zval *), NULL);            if (threadsFounded >= 999) {                break;            }        }    }    end_mmapfile((void *) ptr, buf.st_size, -1);    ldata.l_type = F_UNLCK;    fcntl(fd, F_SETLKW, &ldata);        /* 退出互斥区域 */    close(fd);    efree(IDList);    efree(IDList2);    efree(index);}/* function bbs_caneditfile(string board, string filename); * 判断当前用户是否有权编辑某文件 */static PHP_FUNCTION(bbs_caneditfile){    char *board, *filename;    long boardLen, filenameLen;    char path[512];    struct fileheader x;    bcache_t *brd;    getcwd(old_pwd, 1023);    chdir(BBSHOME);    old_pwd[1023] = 0;    if ((ZEND_NUM_ARGS() != 2) || (zend_parse_parameters(2 TSRMLS_CC, "ss", &board, &boardLen, &filename, &filenameLen) != SUCCESS)) {        WRONG_PARAM_COUNT;    }    brd = getbcache(board);    if (brd == NULL) {        RETURN_LONG(-1);        //讨论区名称错误    }    if (currentuser == NULL)        RETURN_FALSE;    if (!strcmp(brd->filename, "syssecurity")        || !strcmp(brd->filename, "junk")        || !strcmp(brd->filename, "deleted"))   /* Leeward : 98.01.22 */        RETURN_LONG(-2);        //本版不能修改文章    if (checkreadonly(brd->filename) == true) {        RETURN_LONG(-3);        //本版已被设置只读    }    if (get_file_ent(brd->filename, filename, &x) == 0) {        RETURN_LONG(-4);        //无法取得文件记录    }    setbfile(path, brd->filename, filename);    if (!HAS_PERM(currentuser, PERM_SYSOP)      /* SYSOP、当前版主、原发信人 可以编辑 */        &&!chk_currBM(brd->BM, currentuser)) {        if (!isowner(currentuser, &x)) {            RETURN_LONG(-5);    //不能修改他人文章!        } else if (file_time(path) < currentuser->firstlogin) {

⌨️ 快捷键说明

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