CGISession
view release on metacpan or search on metacpan
Session/bin/CGI/Session/CookieJar/create_cookie_table view on Meta::CPAN
#!/usr/local/bin/perl5
#
use strict;
package CGI::Session::CookieJar::DBI::creakte_cookie_table;
use Carp;
use Getopt::Long;
use DBI;
use CGI::Session::CookieJar::DBI;
my $DB_MYSQL = 'MYSQL';
=item NAME
create_cookie_table. A tool to create a cookie jar for use with a cookie.
It will create a database if required. If the desired table exists
with a preexisting database then it will drop the table and then
recreate it. It will also create a grant for the cookie user if it is
requested.
=cut
my ( $help, $host, $database, $create_new_database, $tablename );
my ( $grantonly, $grantuser, $grantpass, $user, $pass );
my ( $user_column, $cookie_column, $passkey_column, $expiration_column );
my ( $server_side_data_column, $dbtype );
my $results = GetOptions( 'help' => \$help,
'database=s' => \$database,
'table=s' => \$tablename,
'host=s' => \$host,
'create_database' => \$create_new_database,
'grantonly' => \$grantonly,
'grantuser:s' => \$grantuser,
'grantpass:s' => \$grantpass,
'user:s' => \$user,
'password:s' => \$pass,
'user_column=s' => \$user_column,
'passkey_column=s' => \$passkey_column,
'cookie_column=s' => \$cookie_column,
'expiration_column=s' => \$expiration_column,
'server_side_data_column=s' => \$server_side_data_column,
'type=s' => \$dbtype, );
if ( $help )
{
print help();
exit 0;
}
$host = undef unless $host;
die_help() unless $user;
die_help() unless defined $pass;
die_help() unless $tablename;
die_help() unless $database;
if ( !$user )
{
print "Admin user: ";
$user = <STDIN>;
chop $user;
print "\n";
}
if ( !$pass )
{
print "Admin password: ";
system( 'stty -echo' );
$pass = <STDIN>;
system( 'stty echo' );
chop $pass;
print "\n";
}
if ( defined $grantuser )
{
$grantuser = $user unless $grantuser;
}
if ( defined $grantpass )
{
if ( !$grantpass )
{
print "Password for $grantuser: ";
system( 'stty -echo' );
$grantpass = <STDIN>;
system( 'stty echo' );
chop $grantpass;
print "\n";
}
}
else
{
$grantpass = $pass;
}
my $jar = CGI::Session::CookieJar::DBI->new();
$dbtype = $DB_MYSQL unless defined $dbtype;
if ( uc($dbtype ) eq $DB_MYSQL )
{
$dbtype=$DB_MYSQL;
}
else
{
die_help();
}
$jar->host( $host ) if $host;
$jar->user( $user );
$jar->password( $pass );
$jar->database( $database );
$jar->cookie_table( $tablename );
$jar->user_column( $user_column ) if $user_column;
$jar->passkey_column( $passkey_column ) if $passkey_column;
$jar->cookie_column( $cookie_column ) if $cookie_column;
$jar->server_side_data_column( $server_side_data_column ) if $server_side_data_column;
$jar->login_expiration_column( $expiration_column ) if $expiration_column;
my $rootdb = newdb( $jar, 'mysql' );
if ( !$grantonly )
{
create_database( $jar, $rootdb ) if $create_new_database;
$jar->open();
if( $jar->error )
{
$rootdb->disconnect;
croak $jar->error;
}
if ( table_exists( $jar->db, $jar->cookie_table ) )
{
drop_table( $jar, $rootdb ) if $create_new_database;
}
$jar->create_cookie_jar();
if ( $jar->error )
{
$jar->close;
$rootdb->disconnect;
croak $jar->error;
}
$jar->close;
}
if ( $grantuser and $grantpass )
{
grant( $rootdb, sprintf( '%s.%s', $database, $tablename ), $grantuser, $grantpass );
flush_privileges( $rootdb );
}
$rootdb->disconnect;
sub grant
{
my ( $db, $table, $user, $pass ) = @_;
( run in 1.176 second using v1.01-cache-2.11-cpan-39bf76dae61 )