[Affelio-cvs 595] CVS update: affelio/lib/Affelio/misc

Back to archive index

Tadashi Okoshi slash****@users*****
2005年 10月 24日 (月) 20:37:27 JST


Index: affelio/lib/Affelio/misc/InitAffelio.pm
diff -u /dev/null affelio/lib/Affelio/misc/InitAffelio.pm:1.1
--- /dev/null	Mon Oct 24 20:37:27 2005
+++ affelio/lib/Affelio/misc/InitAffelio.pm	Mon Oct 24 20:37:26 2005
@@ -0,0 +1,414 @@
+# Copyright (C) 2005 FishGrove Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+# $Id: InitAffelio.pm,v 1.1 2005/10/24 11:37:26 slash5234 Exp $
+
+package Affelio::misc::InitAffelio;
+{
+    use strict;
+    use Exporter;
+    @Affelio::misc::InitAffelio::ISA = "Exporter";
+    @Affelio::misc::InitAffelio::EXPORT = qw (create_userdir get_userdir create_af_cfg create_db_cfg create_login_cfg copy_def_files init_db set_datadir_perm);
+
+    use lib("../../../extlib/");
+    use Error qw(:try);
+    use lib(".");
+    use lib("../../../lib/");
+    use Affelio;
+    use Affelio::misc::CGIError;
+    use Affelio::misc::Debug;
+    use Affelio::exception::Exception;
+    use Affelio::exception::DBException;
+    use Affelio::exception::IOException;
+
+    #####################################################################
+    sub create_userdir{
+	my $topdir = shift;
+
+	srand(time ^ ($$ + ($$ << 15)));
+	#userdata
+	my $dir1 = Affelio::misc::MyCrypt::generate_password();
+	mkdir("$topdir/userdata/$dir1", 0777);
+	my $dir2 = Affelio::misc::MyCrypt::generate_password();
+	mkdir("$topdir/session/$dir2", 0777);
+    }
+
+    #####################################################################
+    sub get_userdir{
+	my $userdata_dir = shift;
+
+	my $dir;
+	my $ret;
+	try{
+	    opendir(DIR, $userdata_dir);
+
+	    while (defined($dir = readdir(DIR))) {
+		if(($dir ne '.') && ($dir ne '..') 
+		   && ($dir ne 'default') && ($dir ne 'CVS')
+		   && ($dir ne 'index.html')){
+		    $ret = "$userdata_dir/$dir";
+		}
+	    }
+	}catch Error with{
+	    my $e=shift;
+	    throw($e);
+	};
+	return($ret);
+    }
+
+    #####################################################################
+    sub create_af_cfg{
+	my $affelio_cfg_path = shift;
+	my $fs_root = shift;
+	my $web_root = shift;
+	my $char_set = shift;
+	my $template = shift;
+	my $sendmail_path = shift;
+
+	eval{
+	    open(OUT, "> $affelio_cfg_path");
+	    print OUT "[site_config]\n";
+	    print OUT "fs_root=$fs_root\n";
+	    print OUT "web_root=$web_root\n";
+	    print OUT "char_set =$char_set\n";
+	    print OUT "template =$template\n";
+	    print OUT "\n";
+	    print OUT "[command]\n";
+	    print OUT "sendmail=$sendmail_path\n";
+	    close OUT;
+
+	    chmod 0444, "$affelio_cfg_path";
+	};
+
+
+    }
+
+    #####################################################################
+    sub create_db_cfg{
+	my $db_cfg_path = shift;
+	my $db_type = shift;
+	my $db_dbname=shift;
+	my $db_username = shift;
+	my $db_password = shift;
+	my $db_hostname = shift;
+	my $db_port = shift;
+
+	eval{
+	    open(OUT, "> $db_cfg_path");
+	    print OUT "[db]\n";
+	    print OUT "type=$db_type\n";
+	    print OUT "dbname=$db_dbname\n";
+	    print OUT "username=$db_username\n";
+	    print OUT "password=$db_password\n";
+	    print OUT "hostname=$db_hostname\n";
+	    print OUT "port=$db_port\n";
+	    print OUT "[appdb]\n";
+	    print OUT "type=$db_type\n";
+	    print OUT "dbname=$db_dbname\n";
+	    print OUT "username=$db_username\n";
+	    print OUT "password=$db_password\n";
+	    print OUT "hostname=$db_hostname\n";
+	    print OUT "port=$db_port\n";
+	    close OUT;
+	};
+    }
+
+    #####################################################################
+    sub create_login_cfg{
+	my $login_cfg_path = shift;
+	my $username = shift;
+	my $crypted_password = shift;
+
+	eval{
+	    open(OUT, "> $login_cfg_path");
+	    print OUT "[auth]\n";
+	    print OUT "username=$username\n";
+	    print OUT "password=$crypted_password\n";
+	    close OUT;
+	};
+    }
+
+    #####################################################################
+    sub copy_def_files{
+	my $top_dir=shift;
+	my $user_dir=shift;
+	my $locale = shift;
+
+	#Copy default face JPEG file 
+	system("cp -f $top_dir/defaults/profile_face.jpg $user_dir/profile_face.jpg");
+	system("chmod 666 $user_dir/profile_face.jpg");
+
+	#Copy default preference file
+	system("cp -f $top_dir/defaults/preference.cfg $user_dir/preference.cfg");
+
+	system("cp -fr $top_dir/defaults/af_templates/$locale $user_dir/af_templates");
+      }
+
+    #####################################################################
+    sub init_db{
+	my $top_dir = shift;
+	my $g_nickname = shift;
+	my $g_email =shift;
+	my $g_lh=shift;
+
+	debug_print("init_db: [$top_dir] [$g_nickname] [$g_email]");
+
+	################################################################
+	#Stage 0: load Affelio (init mode)
+	################################################################
+	my $cfg_dir = "$top_dir/config/";
+	my $af;
+	my $dbh;
+	try{
+	    $af = new Affelio(ConfigDir => $cfg_dir,
+			      Mode => "init");
+	    $dbh = $af->{db};
+	}catch Error with{
+	    my $e = shift;
+	    throw Affelio::exception::Exception("Could not load Affelio (init): $e");
+	};
+
+	################################################################
+	#Stage 1: DB creation
+	################################################################
+
+	################################
+	#profile DB
+	################################
+	my $create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_CORE_prof(attribute TEXT, value TEXT) 
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating prof: $@");
+	}
+
+	$af->{user__nickname} = $g_nickname;
+	$af->{user__email1} = $g_email;
+	try{
+	    $af->{pm}->save_profile();
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::Exception("Cannot save_profile: $@");
+	};
+	debug_print("saved profile");
+
+	################################
+	#profile attribute DB
+	################################
+	$create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_CORE_prof_attr(aid INTEGER, name TEXT, type INTEGER)
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating attr: $@");
+	}
+	
+	my $sth = "";
+	eval{
+	    $sth = $dbh->prepare(q{insert into AFuser_CORE_prof_attr(aid, name, type) values (?,?,?)});
+	};
+	if($@){
+	    throw  Affelio::exception::DBException("SQL prepare: $@");
+	}
+
+	try{
+	    open(FIN, "$top_dir/defaults/AFuser_CORE_prof_attr.csv");
+	    
+	    while(my $line=<FIN>){
+		chomp($line);
+		my ($aid, $name, $type) = split(',', $line);
+		#print "$aid - $name - $type\n";
+		
+		$sth->execute($aid, $name, $type);
+	    }
+	    close(FIN);
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::IOException("prof_attr: $@");
+	};
+
+	################################
+	#friends DB
+	################################
+	$create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_CORE_friends(uid INTEGER PRIMARY KEY, af_id CHAR(255), nickname TEXT, timestamp TEXT, password TEXT, intro TEXT, option_pid INTEGER, lastupdated TEXT, f2list TEXT)
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating friends tbl: $@");
+	}
+	
+	################################
+	#erasedfriends DB
+	################################
+	$create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_CORE_erasedfriends(uid INTEGER PRIMARY KEY, af_id CHAR(255), timestamp TEXT)
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating erased friends tbl: $@");
+	}
+
+	################################
+	#friendsfriends DB
+	################################
+	$create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_CORE_friendsfriends(uid INTEGER PRIMARY KEY, af_id CHAR(255), nickname TEXT, timestamp TEXT, f1list TEXT)
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating F2 tbl: $@");
+	}
+
+	################################
+	#group DB
+	################################
+	$create_tbl_cmd = "CREATE TABLE AFuser_CORE_group(gid INTEGER, group_name TEXT, members TEXT, option_pid INTEGER)";
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating grp tbl: $@");
+	}
+
+	################################
+	#Permission DB
+	################################
+	$create_tbl_cmd = "CREATE TABLE AFuser_CORE_permission(pid INTEGER, type TEXT, target_id  TEXT, ";
+
+        for(my $i=0; $i<=63; $i++){
+	    $create_tbl_cmd .= " attr$i INT,";
+	}
+        chop($create_tbl_cmd);
+        $create_tbl_cmd .= ")";
+        debug_print("setup: create [$create_tbl_cmd]");
+	if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating perm tbl: $@");
+	}
+
+	################################
+	#tmp_recvd_hs
+        ################################
+	$create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_SNS_tmp_recvd_hs(sessionid TEXT, timestamp TEXT, af_id CHAR(255), nickname TEXT, DH_key_str TEXT)
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating tmp_recved tbl: $@");
+	}
+
+	################################
+	#tmp_sent_hs
+	################################
+	$create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_SNS_tmp_sent_hs(sessionid TEXT, timestamp TEXT, af_id CHAR(255), nickname TEXT, DH_key_str TEXT)
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating tmp_send tbl: $@");
+	}
+				
+	################################
+	#message
+	################################
+	$create_tbl_cmd = <<EOT;
+CREATE TABLE AFuser_CORE_message(mid INTEGER PRIMARY KEY, timestamp TEXT, msgtitle TEXT, msgtype TEXT, msgfrom TEXT, msgbody TEXT, readflag INTEGER)
+EOT
+        if(!$dbh->do($create_tbl_cmd)){
+	    throw  Affelio::exception::DBException("creating msg tbl: $@");
+	} 
+
+	try{				    
+	    $dbh->disconnect;
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::DBException("DB disconnecting: $@");
+	};  
+
+	################################################################
+	#Stage 2: Reload Affelio
+	################################################################
+	try{				    
+	    undef($af);
+	    $af = new Affelio(ConfigDir => $cfg_dir);
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::Exception("Couldnot load Affelio: $e");
+	};
+
+	################################
+	#Set permission to F1
+	################################
+	#                 n names  b i  intro   email     url     im
+	my @flag_array = (1,1,1,1, 1,1, 1,1,    0,0,0,0, 1,1,1,1, 0,0,0,0,0,0, 1);
+	try{				    
+	    $af->{perm}->add_permission("f", "f1", \@flag_array); 
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::Exception("adding F1 perm: $@");
+	};
+	
+	################################
+	#Set permission to F2
+	################################
+	#                 n names  b i  intro   email     url     im
+	my @flag_array = (1,0,0,0, 0,1, 1,1,    0,0,0,0, 1,1,1,1, 0,0,0,0,0,0, 1);
+	try{				    
+	    $af->{perm}->add_permission("f", "f2", \@flag_array); 
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::Exception("adding F2 perm: $@");
+	};
+	
+	################################
+	#Set permission to PB
+	################################
+	#                 n names  b i  intro   email     url     im
+	my @flag_array = (1,0,0,0, 0,0, 1,0,    0,0,0,0, 0,0,0,0, 0,0,0,0,0,0, 0);
+	try{				    
+	    $af->{perm}->add_permission("f", "pb", \@flag_array); 
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::Exception("adding PB perm: $@");
+	};
+	
+	################################
+	#Make a new group "dear_friend"
+	################################
+	my $gid;
+	try{				    
+	    $gid = $af->{gm}->add_group($g_lh->maketext("_SETUP_group_dear_friend"));
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::Exception("adding dear Grp: $@");
+	};
+	
+	#####################################
+	#Set permission to group "dear_friend"
+	#####################################
+	#                 n names  b i  intro   email     url     im
+	my @flag_array = (1,1,1,1, 1,1, 1,1,    1,1,1,1, 1,1,1,1, 1,1,1,1,1,1, 1);
+	try{				    
+	    $af->{perm}->add_permission("g", $gid, \@flag_array); 
+	}catch Error with{
+	    my $e = shift;
+	    throw  Affelio::exception::Exception("adding perm to Grp: $@");
+	};
+    }
+
+    #####################################################################
+    sub set_datadir_perm{
+	my $top_dir = shift;
+	my $userdir = get_userdir("$top_dir/userdata");
+
+	#hmmmmmm....
+	system("chmod -R 777 $userdir");
+    }
+
+
+}
+1;


Affelio-cvs メーリングリストの案内
Back to archive index