phpbbs.announce.c

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

C
1,615
字号
 *        -1:dir NOT exist *        -2:can NOT find .Names file *        -3:No article here *        -9:system error; */PHP_FUNCTION(bbs_read_ann_dir){    char  *path;    int   path_len;    zval *board,*path2,*element,*articles;        struct userec *u;    char pathbuf[256];    int len;    MENU me;    int i,j;    char *id,*ptr;    char buf[256];    char r_title[STRLEN],r_path[256],r_bm[256];    int  r_flag,r_time;    struct stat st;    bool cansee;    long seespecial=0;        if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,"szza|l",&path,&path_len,&board,&path2,&articles,&seespecial) == FAILURE) {        WRONG_PARAM_COUNT;    }    if (!PZVAL_IS_REF(board) || !PZVAL_IS_REF(path2)) {       	zend_error(E_WARNING, "Parameter wasn't passed by reference");       	RETURN_FALSE;    }        if(array_init(articles) != SUCCESS)        RETURN_LONG(-9);        u = getCurrentUser();    if (!u)        RETURN_LONG(-9);        if (strstr(path, "..") || strstr(path, "SYSHome"))  		RETURN_LONG(-1);    if (path[0] != '\0') {        len = strlen(path);        if (path[len - 1] == '/')            path[len - 1] = '\0';        if (path[0] == '/')            snprintf(pathbuf, 255, "0Announce%s", path);        else            snprintf(pathbuf, 255, "0Announce/%s", path);        if (ann_traverse_check(pathbuf, u) < 0)			RETURN_LONG(-1);    } else        strcpy(pathbuf, "0Announce");    ZVAL_STRING(path2,pathbuf,1);    bzero(&me,sizeof(MENU));    me.path = pathbuf;    if(a_loadnames(&me,NULL)==-1)        RETURN_LONG(-2);    buf[0] = '\0';    ann_get_board(pathbuf, buf, sizeof(buf));    ZVAL_STRING(board,buf,1);    if (me.num <= 0)         RETURN_LONG(-3);    me.now = 0;    j = 0;    for (i = 0; i < me.num; i++) {        strncpy(r_title, M_ITEM(&me,i)->title, sizeof(r_title) - 1);        r_title[sizeof(r_title) - 1] = '\0';        if (strlen(r_title) <= 39) {            id = "";        } else {            if ((ptr = strchr(r_title + 38, '(')) != NULL) {                *ptr = '\0';                id = ptr + 1;                if (strncmp(id, "BM: ", 4) == 0)                    id += 4;                if ((ptr = strchr(id, ')')) != NULL)                    *ptr = '\0';            } else if ((ptr = strchr(r_title + 38, ' ')) != NULL) {                *ptr = '\0';                id = ptr + 1;            } else                id = "";        }        snprintf(buf, sizeof(buf), "%s/%s", me.path, M_ITEM(&me,i)->fname);        ptr = strchr(me.path, '/');                if (stat(buf, &st) == -1) {            r_time = 0;            r_flag = 0;        } else {            r_time = st.st_mtime;            if (S_ISDIR(st.st_mode))                r_flag = 1;            else                 r_flag = M_ITEM(&me,i)->attachpos?3:2;        }                snprintf(r_path, sizeof(r_path), "%s/%s", ptr == NULL ? "" : ptr, M_ITEM(&me,i)->fname);        strncpy(r_bm,id[0]?id:"",sizeof(r_bm)-1);        r_bm[sizeof(r_bm)-1] = '\0';                cansee = false;        if (strcmp(r_bm, "BMS")==0) {            if (HAS_PERM(getCurrentUser(), PERM_BOARDS) && (seespecial!=0))                cansee = true;        }        else if (strcmp(r_bm, "SYSOPS")==0) {            if (HAS_PERM(getCurrentUser(), PERM_SYSOP) && (seespecial!=0))                cansee = true;        }        else            cansee = true;        if (cansee) {            MAKE_STD_ZVAL(element);            array_init(element);            add_assoc_string(element,"TITLE",r_title,1);            add_assoc_string(element,"PATH",r_path,1);            add_assoc_string(element,"BM",r_bm,1);            add_assoc_long(element,"FLAG",r_flag);            add_assoc_long(element,"TIME",r_time);            add_assoc_string(element,"FNAME",M_ITEM(&me,i)->fname,1);            zend_hash_index_update(Z_ARRVAL_P(articles),j,(void*) &element, sizeof(zval*), NULL);            j ++;        }        me.now++;    }    a_freenames(&me);    RETURN_LONG(0);}/* get ann dir's title from parent .Names, pig2532 2006.3string bbs_ann_get_title(string path);*/PHP_FUNCTION(bbs_ann_get_title){    int ac, path_len, i;    char *path, *fname, title[STRLEN], fpath[PATHLEN];    MENU me;    bool find=false;    ac = ZEND_NUM_ARGS();    if((ac != 1) || (zend_parse_parameters(1 TSRMLS_CC, "s", &path, &path_len) != SUCCESS))    {        WRONG_PARAM_COUNT;    }    strncpy(fpath, path, PATHLEN);    fpath[PATHLEN - 1] = '\0';    if(strcmp(fpath, "0Announce") == 0)    {        RETURN_STRING("精华公布栏", 1);    }    if(!(dashd(fpath) || dashf(fpath)))    {        RETURN_STRING("精华区目录不存在", 1);    }    fname = strrchr(fpath, '/');    if(fname == NULL)    {        RETURN_STRING("找不到精华区目录", 1);    }    *fname = '\0';    fname++;    bzero(&me, sizeof(me));    me.path = fpath;    me.level = PERM_BOARDS;    a_loadnames(&me, getSession());    for(i=0; i<me.num; i++)    {        if(strcmp(fname, M_ITEM(&me,i)->fname) == 0)        {            find = true;            break;        }    }    if(!find)    {        a_freenames(&me);        RETURN_STRING("找不到精华区项目", 1);    }    strncpy(title, M_ITEM(&me,i)->title, STRLEN);    title[STRLEN - 1] = '\0';    a_freenames(&me);    RETURN_STRING(title, 1);}/* BM functions in announce below * by pig2532@newsmth * NOTICE: These functions do not check BM perm, *         please use bbs_ann_traverse_check to check perm first in php. */static void ann_write_bmlog(char *path){    char *ptr, board[STRLEN];    if((ptr = strstr(path, "groups/")) != NULL)        ann_get_board(ptr, board, sizeof(board));    if(board[0] != '\0')        bmlog(getCurrentUser()->userid, board, 13, 1);}/* create directorybbs_ann_mkdir(string path, string fname, string title, string bm);return:     0: success    -1: path not found    -2: filename not valid    -3: file already exists    -4: a_savenames failed*/PHP_FUNCTION(bbs_ann_mkdir){    int ac;    char *path, *sfname, *title, *bm, fname[80];    int path_len, fname_len, title_len, bm_len;    char fpath[PATHLEN], buf[STRLEN];    MENU me;    FILE *fp;    ac = ZEND_NUM_ARGS();    if((ac != 4) || (zend_parse_parameters(4 TSRMLS_CC, "ssss", &path, &path_len, &sfname, &fname_len, &title, &title_len, &bm, &bm_len) != SUCCESS))    {        WRONG_PARAM_COUNT;    }        if(!dashd(path))    {        RETURN_LONG(-1);    }#ifdef ANN_AUTONAME    sprintf(fname, "D%X", time(0) + rand());#else    strncpy(fname, sfname, 80);    fname[79] = '\0';    if(!valid_fname(fname))    {        RETURN_LONG(-2);    }#endif    sprintf(fpath, "%s/%s", path, fname);    if(dashf(fpath) || dashd(fpath))    {        RETURN_LONG(-3);    }    mkdir(fpath, 0755);    chmod(fpath, 0755);    bzero(&me, sizeof(me));    me.path = path;    me.level = PERM_BOARDS;    a_loadnames(&me, getSession());    if(bm[0] == '\0')        sprintf(buf, "%-38.38s", title);    else        sprintf(buf, "%-38.38s(BM: %s)", title, bm);    a_additem(&me, buf, fname, NULL, 0, 0);    if(a_savenames(&me) == 0)    {        sprintf(fpath, "%s/%s/.Names", path, fname);        if((fp = fopen(fpath, "w")) != NULL)        {            fprintf(fp, "#\n");            fprintf(fp, "# Title=%s", buf);            fprintf(fp, "#\n");            fclose(fp);        }    }    else    {        a_freenames(&me);        RETURN_LONG(-4);    }    a_freenames(&me);    ann_write_bmlog(path);    RETURN_LONG(0);}/* create filebbs_ann_mkfile(string path, string fname, string title, string content[, long import]); notice: set import=1 to make file from import file.return:     0: success    -1: path not found    -2: fname not valid    -3: file already exists    -4: error to write file    -5: a_savenames failed*/PHP_FUNCTION(bbs_ann_mkfile){    int ac;    char *path, *sfname, *title, *content, fname[80];    int path_len, fname_len, title_len, content_len;    FILE *fp;    MENU me;    long import=0;    char fpath[PATHLEN], buf[STRLEN];    ac = ZEND_NUM_ARGS();    if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ssss/|l", &path, &path_len, &sfname, &fname_len, &title, &title_len, &content, &content_len, &import) != SUCCESS)    {        WRONG_PARAM_COUNT;    }    if(!dashd(path))    {        RETURN_LONG(-1);    }#ifdef ANN_AUTONAME    sprintf(fname, "A%X", time(0) + rand());#else    strncpy(fname, sfname, 80);    fname[79] = '\0';    if(!valid_fname(fname))    {        RETURN_LONG(-2);    }#endif    sprintf(fpath, "%s/%s", path, fname);    if(dashf(fpath) || dashd(fpath))    {        RETURN_LONG(-3);    }    if(import)    {        char importpath[PATHLEN], attachpath[PATHLEN];        sprintf(importpath, "tmp/bm.%s", getCurrentUser()->userid);        if(!dashf(importpath))        {            RETURN_LONG(0);        }        sprintf(attachpath, "tmp/bm.%s.attach", getCurrentUser()->userid);        if(dashf(attachpath))        {            a_append_attachment(importpath, attachpath);            my_unlink(attachpath);        }        f_mv(importpath, fpath);        chmod(fpath, 0644);    }    else    {        if((fp = fopen(fpath, "w")) == NULL)        {            RETURN_LONG(-4);        }        fwrite(content, content_len, 1, fp);        fclose(fp);        chmod(fpath, 0644);    }    bzero(&me, sizeof(me));    me.path = path;    me.level = PERM_BOARDS;    a_loadnames(&me, getSession());    sprintf(buf, "%-38.38s %s ", title, getCurrentUser()->userid);    a_additem(&me, buf, fname, NULL, 0, 0);    if(a_savenames(&me) != 0)    {        a_freenames(&me);        RETURN_LONG(-5);    }    a_freenames(&me);    ann_write_bmlog(path);    RETURN_LONG(0);}/* modify directorybbs_ann_editdir(string path, string fname, string newfname, string title, string bm);return:     0: success    -1: dir not found    -2: newfname not valid    -3: newfname already exists    -4: dir record not found in .Names    -5: a_savenames failed*/PHP_FUNCTION(bbs_ann_editdir){    int ac, i;    char *path, *fname, *newfname, *title, *bm;    int path_len, fname_len, newfname_len, title_len, bm_len;    MENU me;    char fpath[PATHLEN], newfpath[PATHLEN];    bool find = false;    ac = ZEND_NUM_ARGS();    if((ac != 5) || (zend_parse_parameters(5 TSRMLS_CC, "sssss", &path, &path_len, &fname, &fname_len, &newfname, &newfname_len, &title, &title_len, &bm, &bm_len) != SUCCESS))    {        WRONG_PARAM_COUNT;    }    snprintf(fpath, PATHLEN, "%s/%s", path, fname);    if(!dashd(fpath))    {        RETURN_LONG(-1);    }    if(!valid_fname(newfname))    {        RETURN_LONG(-2);    }    if(strcmp(fname, newfname))    {        snprintf(newfpath, PATHLEN, "%s/%s", path, newfname);        if(dashf(newfpath) || dashd(newfpath))        {            RETURN_LONG(-3);        }        f_mv(fpath, newfpath);    }    bzero(&me, sizeof(me));    me.path = path;    me.level = PERM_BOARDS;    a_loadnames(&me, getSession());    for(i=0; i<me.num; i++)    {        if(strcmp(fname, M_ITEM(&me,i)->fname) == 0)        {            strncpy(M_ITEM(&me,i)->fname, newfname, 80);            M_ITEM(&me,i)->fname[79] = '\0';            if(bm[0] == '\0')                sprintf(M_ITEM(&me,i)->title, "%-38.38s", title);            else                sprintf(M_ITEM(&me,i)->title, "%-38.38s(BM: %s)", title, bm);            find = true;            break;        }    }    if(!find)    {        a_freenames(&me);        RETURN_LONG(-4);    }    if(a_savenames(&me) != 0)    {        a_freenames(&me);        RETURN_LONG(-5);    }    a_freenames(&me);    RETURN_LONG(0);}/* return file content for editingbbs_ann_originfile(string path);*/PHP_FUNCTION(bbs_ann_originfile){    int ac, path_len;    char *path;    FILE* fp;	char buf[512];    char *content, *ptr;    int chunk_size=51200, calen, clen, buflen;    ac = ZEND_NUM_ARGS();    if((ac != 1) || (zend_parse_parameters(1 TSRMLS_CC, "s", &path, &path_len) != SUCCESS))    {        WRONG_PARAM_COUNT;    }	if (!dashf(path))    {		RETURN_LONG(-1);	}    fp = fopen(path, "r");    if (fp == NULL)    {        RETURN_LONG(-1);    }	    calen = chunk_size;    content = (char *)emalloc(calen);    clen = 0;    ptr = content;    while (skip_attach_fgets(buf, sizeof(buf), fp) != 0) {        buflen = strlen(buf);        if((clen + buflen) >= (calen + 1))        {            calen += chunk_size;            content = (char *)erealloc(content, calen);            ptr = content + clen;        }        memcpy(ptr, buf, buflen);        clen += buflen;        ptr += buflen;    }    fclose(fp);    content[clen] = '\0';    RETURN_STRINGL(content, clen + 1, 0);}/* edit filebbs_ann_editfile(string path, string fname, string newfname, string title, string content);return:     0: success    -1: file not found    -2: newfname not valid    -3: newfname already exists    -4: failed to open file    -5: file not found in .Names    -6: a_savenames failed*/PHP_FUNCTION(bbs_ann_editfile){    int ac, asize, i;    char *path, *fname, *newfname, *title, *content;    int path_len, fname_len, newfname_len, title_len, content_len;    char fpath[PATHLEN], newfpath[PATHLEN], tmpfpath[PATHLEN], buf[256];    FILE *fin, *fout;    bool find = false;    MENU me;    ac = ZEND_NUM_ARGS();    if((ac != 5) || (zend_parse_parameters(5 TSRMLS_CC, "sssss/", &path, &path_len, &fname, &fname_len, &newfname, &newfname_len, &title, &title_len, &content, &content_len) != SUCCESS))    {        WRONG_PARAM_COUNT;    }    snprintf(fpath, PATHLEN, "%s/%s", path, fname);    if(!dashf(fpath))    {        RETURN_LONG(-1);    }

⌨️ 快捷键说明

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