# ================================================================== # Gossamer Forum - Advanced web community # # Website : http://gossamer-threads.com/ # Support : http://gossamer-threads.com/scripts/support/ # Revision : $Id: WWWThreads_PHP.pm,v 1.14 2002/03/16 06:20:22 adrian Exp $ # # Copyright (c) 2001 Gossamer Threads Inc. All Rights Reserved. # Redistribution in part or in whole strictly prohibited. Please # see LICENSE file for full details. # ================================================================== package GForum::Import::WWWThreads_PHP; BEGIN { eval { require Time::HiRes; import Time::HiRes qw/time/; }; } use strict; use GForum qw/$CFG/; use vars qw/@ISA/; use GForum::Import::WWWThreads; use GT::Dumper; @ISA = qw/GForum::Import::WWWThreads/; sub new { my $class = shift; my $self = GForum::Import::WWWThreads->new(@_); return bless($self, $class); } sub read_conf { my $start_time = time; my $self = shift; # Read WWWThreads database, and other misc. information from the config. my $conf; if (-f "$self->{source}/config.inc.php") { $conf = "$self->{source}/config.inc.php"; } else { $self->{error} = 'Cannot locate WWWThreads configuration file (config.inc.php).'; return 0; } $self->debug("Reading WWWThreads configuration from: $conf", 1); my $config = {}; local ($!, $@); open(CONFIG, "< $conf") or $self->{error} = "Unable to load WWWThreads configuration file $conf: " . ("$!" || $@), return 0; while () { chomp; s/<\?(?:php)?|\?>//; # Remove PHP tags s/^\s*//; # Remove leading white space s/\s*$//; # Remove trailing white space s|//.*||; # Remove C++ style comments (doesn't work with /* ... */ s/#.*//; # Remove shell style comments next unless length; # Anything left? my ($var, $value) = split(/\s*=\s*/, $_, 2); $var =~ s/\$config\[['"]?//; $var =~ s/['"]?\]//; $value =~ s/^['"]?|['"]?\s*;$//g; # Strip off beginning and ending quotes $config->{$var} = $value; } close CONFIG; # Save database config. $self->{db_driver} = $config->{dbtype}; $self->{db_name} = $config->{dbname}; $self->{db_host} = $config->{dbserver}; $self->{db_user} = $config->{dbuser}; $self->{db_pass} = $config->{dbpass}; $self->{post_att_dir} = $config->{files}; # Save extra fields. $self->{user_extra_col_1} = $config->{extra1} if exists $config->{extra1} and $config->{extra1} ne ''; $self->{user_extra_col_2} = $config->{extra2} if exists $config->{extra2} and $config->{extra2} ne ''; $self->{user_extra_col_3} = $config->{extra3} if exists $config->{extra3} and $config->{extra3} ne ''; $self->{user_extra_col_4} = $config->{extra4} if exists $config->{extra4} and $config->{extra4} ne ''; $self->{user_extra_col_5} = $config->{extra5} if exists $config->{extra5} and $config->{extra5} ne ''; $self->debug("db config:\n driver: $self->{db_driver}\n db name: $self->{db_name}\n db host: $self->{db_host}\n db user: $self->{db_user}\n db pass: $self->{db_pass}", 1); if ((!defined $self->{db_driver}) || (!defined $self->{db_name}) || (!defined $self->{db_host}) || (!defined $self->{db_user})) { $self->{error} = "Your WWWThreads configuration file, $conf, is missing data."; return 0; } else { # Fix case of dbdriver. if (lc($self->{db_driver}) eq 'mysql') { $self->{db_driver} = 'mysql'; } # WWWThreads-PHP only currently supports MySQL. # elsif (lc($self->{db_driver}) eq 'oracle') { # $self->{db_driver} = 'Oracle'; # } # elsif (lc($self->{db_driver}) eq 'pg') { # $self->{db_driver} = 'Pg'; # } else { $self->{error} = "Invalid database driver used in WWWThreads configuration."; return 0; } } # Read WWWThreads version info. unless ($self->{force_version}) { for my $vers_conf (qw/main.inc.php config.inc.php/) {; if (-f "$self->{source}/$vers_conf") { $vers_conf = "$self->{source}/$vers_conf"; } else { next; } $self->debug("Reading WWWThreads version from: $vers_conf", 1); local ($!, $@); open (VERS_CONFIG, "< $vers_conf") or $self->{error} = "Unable to load WWWThreads file $vers_conf: " . ("$!" || $@), return 0; # Version is hardcoded into main.inc.php and includes/header.php :( # Newer versions seem to have this stored in config.inc.php (at least 5.5.1). while () { chomp; if (m/Powered BY WWWThreads\s*([0-9.]+)/i) { $self->{wwwthreads_ver} = $1; last; } elsif (m/UBBThreads PHP Version\s*([0-9.]+)/i) { $self->{wwwthreads_ver} = $1; last; } elsif (m/\$VERSION\s*=\s*['"]([0-9.]+)/i) { $self->{wwwthreads_ver} = $1; last; } } close VERS_CONFIG; last if $self->{wwwthreads_ver}; } } else { $self->{wwwthreads_vers} = $self->{force_version}; } $self->debug("WWWThreads version: $self->{wwwthreads_ver}", 1); if (!$self->{wwwthreads_ver}) { $self->{error} = "Couldn't find the version of WWWThreads you are are using."; return 0; } if ($self->version_comp($self->{wwwthreads_ver}, '5')) { $self->{error} = "The WWWThreads version you are currently using (v$self->{wwwthreads_ver}) is not supported."; return 0; } # Read the User Title info. my $user_titles; if (-f "$self->{source}/filters/usertitles") { $user_titles = "$self->{source}/filters/usertitles"; $self->debug("Reading WWWThreads User Titles from: $user_titles", 1); local ($!, $@); open (USER_TITLES, "< $user_titles") or $self->{error} = "Unable to load WWWThreads file $user_titles: " . ("$!" || $@), return 0; my $user_title_hash = {}; while () { chomp; my ($count, @user_title) = split(/\s+/); my $user_title = "@user_title"; # Save it for later use. $self->{user_titles}->{$user_title} = $count; # Save it for $CFG modification. $user_title_hash->{$count} = $user_title; } close USER_TITLES; # Save replace the current user titles with the imported ones. $CFG->{user_post_title} = $user_title_hash; $CFG->save(); } $self->{post_edit_time} = $config->{edittime} || 'unlimited'; # This is in hours, 0 means disabled. $self->debug('WWWThreads User Titles => count: ', 2); $self->debug(Dumper($self->{user_titles}), 2); my $run_time = time - $start_time; printf ("(%.2fs) ", $run_time) if ($run_time >= 1); return 1; } sub close_forums { my $start_time = time; my $self = shift; # Close GForum. $CFG->{disabled} = 1; $CFG->{disabled_message} = 'Import in progress. The forum is unavailable until the import is completed successfully.'; $CFG->save(); # Close WWWThreads. my $conf; if (-f "$self->{source}/config.inc.php") { $conf = "$self->{source}/config.inc.php"; } else { $self->{error} = 'Cannot locate WWWThreads configuration file (config.inc.php).'; return 0; } open (CONF, "+< $conf") or $self->{error} = "Unable to load WWWThreads configuration file $conf: " . ("$!" || $@), return 0; my $out = ''; while () { s/(\$config\s*\[['"]?isclosed['"]?\]\s*=\s*)0/${1}1/gi; $out .= $_; } seek (CONF, 0, 0) or $self->{error} = "Unable to seek to start of $conf: " . ("$!" || $@), return 0; print CONF $out or $self->{error} = "Unable to print to $conf: " . ("$!" || $@), return 0; truncate (CONF, tell (CONF)) or $self->{error} = "Unable to truncate $conf: " . ("$!" || $@), return 0; close (CONF); my $run_time = time - $start_time; print '(' . $run_time . 's) ' if ($run_time > 0); return 1; } sub open_forums { my $start_time = time; my $self = shift; # Open GForum. $CFG->{disabled} = 0; $CFG->save(); # Open WWWThreads. my $conf; if (-f "$self->{source}/config.inc.php") { $conf = "$self->{source}/config.inc.php"; } else { $self->{error} = 'Cannot locate WWWThreads configuration file (config.inc.php).'; return 0; } open (CONF, "+< $conf") or $self->{error} = "Unable to load WWWThreads configuration file $conf: " . ("$!" || $@), return 0; my $out = ''; while () { s/(\$config\s*\[['"]?isclosed['"]?\]\s*=\s*)1/${1}0/gi; $out .= $_; } seek (CONF, 0, 0) or $self->{error} = "Unable to seek to start of $conf: " . ("$!" || $@), return 0; print CONF $out or $self->{error} = "Unable to print to $conf: " . ("$!" || $@), return 0; truncate (CONF, tell (CONF)) or $self->{error} = "Unable to truncate $conf: " . ("$!" || $@), return 0; close (CONF); my $run_time = time - $start_time; printf ("(%.2fs) ", $run_time) if ($run_time >= 1); return 1; } 1;