看板SetupBBS
标 题[doc] bbs + gallery
发信站猪头纪公园 (Mon Mar 8 09:27:01 2004)
转信站ptt!ctu-reader!ctu-peer!news.nctu!news.iem.NCTU!CteNewsService!AT-News
/*-------------------------------------------------------*/
/* SO/gallery.c ( NTHU CS MapleBBS Ver 2.36 ) */
/*-------------------------------------------------------*/
/* target : create gallery account */
/* create : 04/03/08 */
/* update : 04/03/08 */
/* author :
[email protected] */
/*-------------------------------------------------------*/
#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 >