# cpanel - cPanel/Bulletin_Boards/phpBB3.pm       Copyright(c) 2012 cPanel, Inc.
#                                                           All rights Reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

package cPanel::Bulletin_Boards::phpBB3;

use strict;
use warnings;
BEGIN { unshift @INC, '/usr/local/cpanel'; }
use Cpanel::SafeDir;
use Cpanel::SafeRun;
use Cpanel::Version::Compare;

our $VERSION = 0.78;
my $pkg = __PACKAGE__;

our $meta_info = {
    'description'    => q{PHP/MySQL Based Bulletin Board<br /><br />If upgrading from 3.0.11, note that the semantic meaning of the LOCAL_URL BBcode has changed.  If it causes issues, replace LOCAL_URL with RELATIVE_URL in all custom BBcodes.},
    'adminuser_pass' => 1,
    'admin_email'    => 1,
    'security'       => 'Do not allow users to post HTML when posting. It has lower rating because it relies on 777 directory permissions and a 644 config file and has had many security issues inherent to PHP. Vendor does well in upgrading when problems are found but it requires more effort on your part to make sure you upgrade whenever necessary.',
    'version'        => '3.2.1',
    'security_rank'  => 1,
    'website'        => 'http://phpbb.com/',
    'config_files'   => ['config.php'],
    'chmod'          => {
        '0644' => ['config.php'],
        '0777' => [qw(store/ cache/ files/ images/avatars/upload/)],
    },
    'mysql'               => ['pp3'],
    'table_prefix'        => 'pp3',
    'minimum-php-version' => '5.3.3',
};

if ( defined &Cpanel::cPAddons::proc_keys_named_after_version ) {
    Cpanel::cPAddons::proc_keys_named_after_version( $meta_info, __PACKAGE__ );
}

sub _verify_php_version {
    my $minimum = shift;

    return 1 if !$minimum;

    my $phpv = Cpanel::SafeRun::saferunnoerror( '/usr/local/cpanel/3rdparty/bin/php', '-v' );
    my ($version) = $phpv =~ m/\bPHP (\d+(?:\.\d+)+)(?![\d.])/;

    if ( !$version ) {
        print "<p>Requires PHP $minimum or newer.</p>";
        return;
    }
    elsif ( Cpanel::Version::Compare::compare( $version, '<', $minimum ) ) {
        print qq(<p class="redtext">PHP must be version $minimum or newer. Currently it is version $version.</p>);
        return;
    }

    return 1;
}

#### action functions ##
sub install {
    my $cpo = shift;

    my @checkpaths = qw( /bin /sbin /usr/bin /usr/sbin /usr/local/bin /usr/local/sbin /usr/bin/X11 /usr/X11R6/bin /usr/local/bin /usr/X11R6/bin /usr/local/cpanel/3rdparty/bin);
    foreach my $path (@checkpaths) {
        if ( -x "$path/convert" ) {
            $cpo->{'pp3_imagemagickpath'} = "$path/";
        }
    }
    if ( !$cpo->{'pp3_imagemagickpath'} ) {
        print "Installation Failed.<br>\nThe ImageMagick program 'convert' was not found.  ImageMagick needs to be installed before installing phpBB3.<br>\nPlease contact support for further assistance.<br>\n";
        return;
    }

    _verify_php_version( $cpo->{'minimum-php-version'} ) or return;

    $cpo->stdinstall(@_) or return;
}

sub upgrade {
    my $cpo = shift;

    _verify_php_version( $cpo->{'minimum-php-version'} ) or return;

    $cpo->stdupgrade(@_) or return;

    # Remove the contents of the cache directory on upgrade, except for the
    # .htaccess and index.htm files.
    my $dir = $cpo->{'installed'}{ $cpo->{'workinginstall'} }{'installdir'} . '/cache';
    if ( -e $dir ) {
        opendir my $dh, $dir or return;
        while ( my $ent = readdir $dh ) {
            next if $ent =~ m/^\.\.?$/;
            next if $ent eq '.htaccess' || $ent eq 'index.htm';

            my $path = "$dir/$ent";
            if ( -d $path ) {
                if ( !Cpanel::SafeDir::safermdir($path) ) {
                    print "Failed to remove directory $path. $!";
                }
            }
            else {
                if ( !unlink $path ) {
                    print "Failed to remove file $path. $!";
                }
            }
        }
        closedir $dh;
    }
}

sub uninstall {
    my $cpo = shift;

    # Remove the install/ directory if it is still there (before uninstall)
    my $dir = $cpo->{'installed'}{ $cpo->{'workinginstall'} }{'installdir'} . '/install';
    if ( -d $dir ) {
        if ( !Cpanel::SafeDir::safermdir($dir) ) {
            print "Could not remove path '$dir': ($!) You will need to do that manually!";
        }
    }

    $cpo->stduninstall(@_);
}

#### non action functions ##
sub installform   { print shift->{'installform'} }
sub upgradeform   { print shift->{'upgradeform'} }
sub uninstallform { print shift->{'uninstallform'} }

1;
