[doc] bbs + gallery

看板SetupBBS作者時間22年前 (2004/03/09 01:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
/*-------------------------------------------------------*/ /* SO/gallery.c ( NTHU CS MapleBBS Ver 2.36 ) */ /*-------------------------------------------------------*/ /* target : create gallery account */ /* create : 04/03/08 */ /* update : 04/03/08 */ /* author : shakalaca.bbs@php.twbbs.org */ /*-------------------------------------------------------*/ #include "bbs.h" #include <md5.h> #define ALBUM_PATH "/home/data/www/albums" #define FN_GALLERY_LOG "run/gallery.log" typedef struct { char username[32]; char fullname[32]; char password[64]; char email[64]; char uid[32]; char lastactiondate[64]; } GalleryUser; /* ------------------------------------------------------------------ */ void f_add(fpath, msg) char *fpath; char *msg; { int fd; if ((fd = open(fpath, O_WRONLY | O_CREAT | O_APPEND, 0664)) >= 0) { write(fd, msg, strlen(msg)); close(fd); } } void f_join(from, to) char *from, *to; { int in, out, len; char buf[512]; if ((in = open(from, O_RDONLY)) >= 0) { if ((out = open(to, O_WRONLY | O_CREAT | O_APPEND, 0664)) >= 0) { while (len = read(in, buf, sizeof(buf))) write(out, buf, len); close(out); } close(in); } } void gallery_log(mode) char *mode; { time_t now = time(0); char buf[128]; sprintf(buf, "%s %s (%s)\n", Cdate(&now), mode, cuser.userid); f_add(FN_GALLERY_LOG, buf); } /* ------------------------------------------------------------------ */ int create_album(u, uid) GalleryUser u; char *uid; { char fpath[128], file[128], buf[256], buf2[128]; sprintf(fpath, "%s/%s", ALBUM_PATH, cuser.userid); if (!dashd(fpath)) { umask(0002); if (mkdir(fpath, 0775) == -1) { pressanykey("錯誤:無法建立相簿,請通知站長!"); sprintf(fpath, "%s/.users/%s", ALBUM_PATH, uid); unlink(fpath); gallery_log("ERROR CREATING DIRECTORY"); return 0; } sprintf(file, "%s/photos.dat", fpath); f_add(file, "N;"); sprintf(file, "%s/album.dat", fpath); sprintf(buf2, "%s 的相簿", cuser.userid); sprintf(buf, "O:5:\"album\":5:{s:6:\"fields\";a:57:{s:5:\"title\";s:%d:\"%s\";", strlen(buf2), buf2); f_add(file, buf); f_join("etc/gallery/album.dat.head", file); sprintf(buf, "s:11:\"clicks_date\";i:%ld;", time(NULL)); f_add(file, buf); f_join("etc/gallery/album.dat.body", file); sprintf(buf, "s:4:\"name\";s:%d:\"%s\";s:5:\"owner\";%s;s:13:\"last_mod_time\";i:%ld;", strlen(cuser.userid), cuser.userid, u.uid, time(NULL)); f_add(file, buf); f_join("etc/gallery/album.dat.foot", file); } return 1; } int create_hashfile(u, hash) GalleryUser u; char *hash; { char fpath[64], input[1024]; sprintf(input, "O:12:\"gallery_user\":13:{s:8:\"username\";%s;\ s:8:\"fullname\";%s;s:8:\"password\";%s;s:5:\"email\";%s;s:7:\"isAdmin\";b:0;\ s:15:\"canCreateAlbums\";s:1:\"0\";s:3:\"uid\";%s;s:15:\"defaultLanguage\";\ s:5:\"en_US\";s:7:\"version\";i:5;s:15:\"recoverPassHash\";N;\ s:10:\"lastAction\";s:8:\"register\";s:14:\"lastActionDate\";%s;\ s:9:\"origEmail\";%s;}", u.username, u.fullname, u.password, u.email, u.uid, u.lastactiondate, u.email); sprintf(fpath, "%s/.users/%s", ALBUM_PATH, hash); f_add(fpath, input); if (!dashf(fpath)) return 0; else return 1; } void gen_salt(password, retbuf) char *password, *retbuf; { int i, ch; char salt[5], tmpbuf[64], buf[33]; srand(time(NULL) * 10000000); for (i = 0; i < 4; i++) { ch = rand()%(109-48+1) +48; if(ch > 57) ch += 7; if(ch > 90) ch += 6; salt[i] = ch; } salt[4] = '\0'; sprintf(tmpbuf, "%s%s", salt, password); MD5Data(tmpbuf, strlen(tmpbuf), buf); sprintf(retbuf, "%s%s", salt, buf); } int create_account() { GalleryUser gu; char tmpbuf[64], passbuf[32]; if (cuser.uflag & USER_GALLERY) { pressaykey("您已經申請過相簿了!"); return 0; } sprintf(gu.username, "s:%d:\"%s\"", strlen(cuser.userid), cuser.userid); sprintf(gu.fullname, "s:%d:\"%s\"", strlen(cuser.username), cuser.username); sprintf(gu.email, "s:%d:\"%s\"", strlen(cuser.email), cuser.email); sprintf(gu.lastactiondate, "i:%ld", time(NULL)); for (;;) { if ((getdata(b_lines, 0, "請設定密碼:", passbuf, 9, NOECHO, 0) < 3) || !strcmp(passbuf, cuser.userid)) { pressanykey("密碼太簡單,易遭入侵,至少要 4 個字,請重新輸入"); continue; } getdata(b_lines, 0, "請檢查密碼:", passbuf + 10, 9, NOECHO, 0); if (!strcmp(passbuf, passbuf + 10)) break; pressanykey("密碼輸入錯誤,請重新輸入密碼。"); } passbuf[strlen(passbuf)] = '\0'; gen_salt(passbuf, tmpbuf); sprintf(gu.password, "s:%d:\"%s\"", strlen(tmpbuf), tmpbuf); sprintf(tmpbuf, "%ld_%ld", time(NULL), rand()); sprintf(gu.uid, "s:%d:\"%s\"", strlen(tmpbuf), tmpbuf); if (!create_hashfile(gu, tmpbuf)) { gallery_log("ERROR CREATING USER PROFILE"); pressanykey("錯誤:無法建立資料檔,請通知站長!"); return 0; } if (!create_album(gu, tmpbuf)) { return 0; } cuser.uflag |= USER_GALLERY; gallery_log("CREATE ALBUM"); pressanykey("已建立資料 :)"); return 0; } 程式說明: * 先裝 gallery 吧,FreeBSD 可以用 ports : /usr/ports/www/gallery 不然就去 gallery.sf.net 抓。 * 這是給我的系統使用的 (SOB 2.36),理論上 SOB 系列的都可以用,M3 系列的 改一下也行。 其他地方要增加的為 cuser.uflag,在程式裡面是￿ USER_GALLERY,用來記錄使用 者是否已經申請過了。放到 struct.h 吧,我的是 #define USER_GALLERY 0x0200 如果用 SOB 的請注意一下 pressanykey 的部分,有可能不同。 如果用 M3 的,getdata 那邊要修改一下才能用。 至於細部的修改就別問我了,如果有人願意 port 到其他 bbs 上面歡迎 :p * 去抓 ftp://php.twbbs.org/pub/bbs/gallery/etc 裡面的東西,放到 BBS 家目錄下的 etc/gallery 裡面。這是 album.dat 的內容,直接拿來用然後換上自訂的欄位, gallery 會很聰明的把該資料夾當作是相簿。 當然這邊可以作很多變化,在 album.dat.head 的最後幾個欄位有一個是 s:15:"parentAlbumName";i:0; 指的是該目錄的父目錄為何,如果想把 bbs 使用者申請的目錄都放到某個 資料夾下面的話 (比如 bbs),先用 gallery 建立 bbs 相簿,然後修改 album.dat.head,把上面那些字串改為 s:15:"parentAlbumName";s:3:"bbs"; "bbs" 前面的 3 就是指 "bbs" 這個字串的長度,依此類推可以換成 你想要的資料夾名稱。 * 編輯你的 menu.c,在你想要加上選項的地方加上這行: "SO/gallery.so:create_account", PERM_LOGINOK, "GGGallery 網路相簿", 1, 我是用成 so 檔,如果你想要直接 compile 到你的 bbs 也行。 * 把上面的程式碼存成 gallery.c ,或者到這邊抓 ftp://php.twbbs.org/pub/bbs/gallery/gallery.c 放到 src/SO 裡面,然後編輯一下 gallery.c,修改 ALBUM_PATH 為你的相簿所 在路徑,以及 GALLERY_LOG￿ 的路徑,這是存放 log file 的。接著修改 Makefile ,因為我有用到 MD5Data,所以要修改一下: 在 SO = .. 的最後面加上 gallery.so,然後加上下面幾行: gallery.o: gallery.c $(CC) $(CFLAGS) $(OS_DEF) -c $*.c gallery.so: gallery.o ld -s -G $*.o -o $*.so -L../lib -ldao -lmd 儲存起來之後 make 應該是沒什麼大問題 :) * 這樣子應該就行了 :D 如果有問題歡迎提出 :) ￿程式目前不確定是否為 bug free,如果因為使用這支程式造成 CPU 燒掉, 資料消失,記憶體不見,被人入侵,女朋友跑掉或是股市套牢,本人不負責。 :pp -- 我的簽名檔只有十個字. -- ^..^ < 豬 頭 紀 公 園 - php.twbbs.org (140.113.208.200) > -w @@ w-- < bittern.csie.nctu.edu.tw >
文章代碼(AID): #10JAuL00 (SetupBBS)