##### TADChat版CGI関連処理ライブラリ
### CGI Lib / 2007 &copy; 結城由羅＠世界忍者国 / BSD Lisence
### $Id: cgilib.pl,v 1.2 2007/05/06 03:27:40 jyugoya Exp $
### CGI.pm に移行中

###
# クッキー取得
sub getCookie {
    local($cookiekey) = $_[0];
    local(@cook) = split(/;/, $ENV{'HTTP_COOKIE'});
    foreach (@cook) {
	local($key, $val) = split(/=/);
	$key =~ s/\s//g;
	if ($key eq $cookiekey) {
	    local(%ck) = split(/<>/, $val);
	    return %ck;
	}
    }
}

###
#  クッキー設定
sub setCookie {
    local($cookiekey, $ck, $expday) = @_;
    
    local (@t) = gmtime(time + $expday*24*60*60);
    local ($expt) = sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT",
			    (qw(Sun Mon Tue Wed Thu Fri Sat))[$t[6]],
			    $t[3],
			    (qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec))[$t[4]],
			    $t[5]+1900, $t[2], $t[1], $t[0]);
    
    print "Set-Cookie: $cookiekey=$ck; expires=$expt\n";
}

###
#  URLエンコード
sub url_enc {
    local($_) = @_;

    s/(\W)/'%' . unpack('H2', $1)/eg;
    s/\s/+/g;
    $_;
}

# end of cgilib
1;
