File: createadmin.cgi

package info (click to toggle)
sporum 1.8b1-3.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,108 kB
  • ctags: 676
  • sloc: perl: 14,300; makefile: 52
file content (135 lines) | stat: -rwxr-xr-x 4,149 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl

use SmallPigVars qw($config);
use SPDB;
use SPCGI;

use strict;

my $lang =
{(
  'user_name1'             => "Login Name",
  'nick_name1'             => "Nick Name",
  'real_email1'            => "Real Email",
  'is_missing'             => "is missing",
  'passwds_not_match'      => "Passwords do not match.",
  'passwd_too_short'       => "Passwords need to > 5 chars.",
  'need_passwd'            => "Verified password/passord field has not been filled in.",
  'profile_saved1'         => "Administrator Account Created",
  'profile_saved2'         => "An administrator account has been created.  Go to the login and enter the username and password you chose. Now you are ready to start creating categories and boards, assigning moderators, and customizing your sporum site."
  )};

sub main{
  # --- create a SPDB object
  my $spdb = SPDB->new;
  
  my $spcgi = SPCGI->new($spdb);
  $spcgi->set_state_for_client();

  my $STATE = $spcgi->{'STATE'};
  
  my ($succ, $err_msg, $btitle, $url) = &save_first_user($STATE, $spdb);
  
  my $template;
  if($succ){
      require Templates::Caution;

      # --- create a template object
      $template = 
	Templates::Caution->new($spcgi, $spdb,
				$btitle, 
				$err_msg, $url);
  }
  else{
      require Templates::Default;
      # --- create a template object
      $template = Templates::Default->new($spcgi, $spdb);

      $template->{'caution'} = $err_msg;
      $template->{'dpage'} = "createadmin";
  }

  $template->print_http_header("");
  $template->print_template();	
}

sub save_first_user{
  my ($STATE, $spdb) = @_;
  my ($uri, $msg);

  my ($vpasswd, $passwd, $nickname, $realname, $realemail, 
      $fakeemail, $homepage, $bio, $icq, $uid, $username, $privlev) = 
	map{ $STATE->{$_} } qw(vpasswd passwd 
			       mynickname realname realemail 
			       myfakeemail homepage mybio myicq
			       myuid myusername myprivlev);

  if(!$username){
      $msg .= "<LI> $lang->{'user_name1'} $lang->{'is_missing'}";
  }
  if(!$nickname){
      $msg .= "<LI> $lang->{'nick_name1'} $lang->{'is_missing'}";
  }
  if(!$realemail){
      $msg .= "<LI> $lang->{'real_email1'} $lang->{'is_missing'}";
  }
  if($passwd && $vpasswd){
      unless($vpasswd eq $passwd){
	  $msg .= "<LI>".$lang->{'passwds_not_match'};
      }
      if(length($passwd) < 5 || length($vpasswd) <5){
	  $msg .= "<LI>".$lang->{'passwd_too_short'};
      }
  }
  if(!$passwd || !$vpasswd){
      $msg .= "<LI>".$lang->{'need_passwd'};
  }
  if($msg){
      return (0, $msg, "", "$config->{'cgidir'}/default.cgi?dpage=createadmin");
  }

  # --- insert new user in *Users*
  my $data = {('nickname'=>$nickname, 'realname'=>$realname,
	       'realemail'=>$realemail, 'fakeemail'=>$fakeemail,
	       'homepage'=>$homepage, 'bio'=>$bio,
	       'registered'=> $spdb->db_date(), 'isadmin'=>1, 'active'=>1,
	       'icq'=>$icq, 'username'=>$username,
	       'uid'=>1, 'privlev'=>$privlev)};
  my ($succ, $errmsg) = $spdb->db_insert("Users", $data);
  return (0, $errmsg, "", "$config->{'cgidir'}/default.cgi?dpage=createadmin") 
      if !$succ;    

  my ($succ, $errmsg) = $spdb->db_insert("BoardPrefs", {'uid'=>1});
  return (0, $errmsg, "", "$config->{'cgidir'}/default.cgi?dpage=createadmin") 
      if !$succ;    
  
  &_update_passwd(1, $passwd, $spdb) if $passwd && $vpasswd;

  # --- insert dummy node to *Users*
  $data = {('uid'       =>0,
	    'username'  =>'anon','nickname'  =>'anon',
	    'realemail' =>'anon','fakeemail' =>'anon',
	    'privlev' => 1)};
  my ($succ, $errmsg) = $spdb->db_insert("Users", $data);
  return (0, $errmsg, "", "$config->{'cgidir'}/default.cgi?dpage=createadmin") 
      if !$succ;

  return (1, $lang->{'profile_saved2'}, $lang->{'profile_saved1'},
	  "$config->{'cgidir'}/user.cgi?op=login");
}

sub _update_passwd{
  my ($uid, $passwd, $spdb)= @_;
  my $DBH = $spdb->{'dbh'};

  my $crypt = crypt($passwd, &_salt());
  my $data = {('passwd'=>$DBH->quote("$crypt"))};
  return $spdb->db_update("Users", $data, "uid=$uid");
}

sub _salt{
  my @saltset = (0..9, 'A'..'Z', 'a'..'z', '.', '/');
  return join('', @saltset[rand @saltset, rand @saltset]);
}

main();