# ==================================================================
# Gossamer Links - enhanced directory management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/scripts/support/
#   CVS Info : 087,071,086,086,085      
#   Revision : $Id: Config.pm,v 1.117 2009/05/12 02:24:18 brewt Exp $
# 
# Copyright (c) 2004 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

package Links::Config;
# ======================================================================
# Sets up our config variables -- the data itself is stored in
# Links/Config/Data.pm, but you shouldn't need to edit it directly!
#
use GT::Config;
use strict;
use vars  qw/@ISA $BIN %FILE_CACHE/;
@ISA = 'GT::Config';

sub new {
# --------------------------------------------------------------------
# Takes one optional argument, the path to the admin directory.
#
    my $class   = ref $_[0] ? ref shift : shift;
    my $path    = shift || '.';
    my $file    = $path . '/Links/Config/Data.pm';
    my $header  = <<END_OF_CONFIG;
# ==================================================================
# Gossamer Links - enhanced directory management system
#
#   Website  : http://gossamer-threads.com/
#   Support  : http://gossamer-threads.com/support/
#   Updated  : [localtime]
# 
# Copyright (c) 2004 Gossamer Threads Inc.  All Rights Reserved.
# Redistribution in part or in whole strictly prohibited. Please
# see LICENSE file for full details.
# ==================================================================

END_OF_CONFIG

    my $self = $class->load($file, { inheritance => 0, cache => 1, header => $header });
    exists $self->{admin_root_path} or ($self->{admin_root_path} = $path || '.');
    exists $self->{version}         or ($self->{version} = $Links::VERSION);
    exists $self->{setup}           or ($self->{setup} = 0);
    return $self;
}

sub textarea {
# ------------------------------------------------------------------
# Taken from gforum. This takes a string of a config hash key whose value is an array reference.
# Returns the array elements separated by \n's
#
    my $want = shift;
    if (ref $Links::CFG->{$want} eq 'ARRAY') {
        return join "\n", @{$Links::CFG->{$want}};
    }
    elsif (ref $Links::CFG->{$want} eq 'HASH') {
        return join "\n", map "$_ => $Links::CFG->{$want}->{$_}", sort { $a <=> $b } keys %{$Links::CFG->{$want}};
    }
    else {
        return $Links::CFG->{$want};
    }
}

sub load_vars {
# ------------------------------------------------------------------
# Returns a hash of config variables for use in templates.
#
    my $t = {};
    while (my ($key, $val) = each %{$Links::CFG}) {
        if (ref $val eq 'ARRAY') { $val = join ", ", @$val }
        elsif (ref $val eq 'HASH') { $val = join ", ", map "$_ = $val->{$_}", keys %$val }
        $t->{"cfg_$key"} = $Links::IN->html_escape($val);
    }
    return $t;
}

sub set_defaults {
# ------------------------------------------------------------------
# Set sensible defaults for the config values, overwriting old values.
#
    my ($self, $val) = @_;

    $self->{setup} = 1;
    $self->default_path($val);
    $self->default_build($val);
    $self->default_search($val);
    $self->default_review($val);
    $self->default_user($val);
    $self->default_email($val);
    $self->default_misc($val);
    $self->default_date($val);
    $self->default_other($val);
}
sub default         { shift->set_defaults(1); }  # Overwrite
sub create_defaults { shift->set_defaults(0); }  # Don't Overwrite

sub set {
# ------------------------------------------------------------------
# Sets a value.
#
    my ($self, $key, $val, $overwrite) = @_;
    if ($overwrite or ! exists $self->{$key}) { $self->{$key} = $val; }
}

sub default_path {
# ------------------------------------------------------------------
# Set the path settings to default values.
#
    my ($self, $overwrite) = @_;
    $self->set('admin_root_url',        _find_admin_url(),                              $overwrite);
    $self->set('db_cgi_url',            _find_cgi_url(),                                $overwrite);
    $self->set('db_cgi_url_https',      '',                                             $overwrite);
    $self->set('build_root_url',        _find_pages_url(),                              $overwrite);
    $self->set('path_to_perl',          _find_perl(),                                   $overwrite);
    $self->set('build_static_path',     "$self->{build_root_path}/static",              $overwrite);
    $self->set('build_static_url',      "$self->{build_root_url}/static",               $overwrite);
    $self->set('fileman_root_dir',      $self->{admin_root_path},                       $overwrite);
    $self->set('build_images_url',      "$self->{build_root_url}/images",               $overwrite);
    $self->set('build_css_url',         "$self->{build_root_url}/links.css",            $overwrite);
    $self->set('build_new_path',        "$self->{build_root_path}/New",                 $overwrite);
    $self->set('build_new_url',         "$self->{build_root_url}/New",                  $overwrite);
    $self->set('build_cool_path',       "$self->{build_root_path}/Cool",                $overwrite);
    $self->set('build_cool_url',        "$self->{build_root_url}/Cool",                 $overwrite);
    $self->set('build_ratings_path',    "$self->{build_root_path}/Ratings",             $overwrite);
    $self->set('build_ratings_url',     "$self->{build_root_url}/Ratings",              $overwrite);
    $self->set('build_detail_path',     "$self->{build_root_path}/Detailed",            $overwrite);
    $self->set('build_detail_url',      "$self->{build_root_url}/Detailed",             $overwrite);
}

sub default_build {
# ------------------------------------------------------------------
# Set the build settings to default values.
#
    my ($self, $overwrite) = @_;
    $self->set('build_default_tpl',             'luna', $overwrite);
    $self->set('build_new_cutoff',              7, $overwrite);
    $self->set('build_pop_cutoff',              0.01, $overwrite);
    $self->set('build_use_backup',              1, $overwrite);
    $self->set('db_gen_category_list',          2, $overwrite);
    $self->set('add_system_fields',             { Hits => 0, isNew => 'No', isPopular => 'No', isChanged => 'No', Status => 0, Rating => 0, Votes => 0 }, $overwrite);
    $self->set('build_auto_validate',           0, $overwrite);
    $self->set('db_referers',                   [], $overwrite);
    $self->set('links_cols_update_category',    'isPopular, Rating, Votes', $overwrite);
    $self->set('protected_vars',                [qw/error message secondarynav Meta_Description Meta_Keywords/], $overwrite);

    $self->set('build_sort_order_category',     "isNew DESC,isPopular DESC,Title", $overwrite);
    $self->set('build_sort_paid_first',         1, $overwrite);
    $self->set('build_sort_order_new',          "Add_Date DESC,Title", $overwrite);
    $self->set('build_sort_order_cool',         "Title", $overwrite);
    $self->set('build_sort_order_editor',       "isValidated DESC, Title ASC", $overwrite);
    $self->set('build_span_pages',              1, $overwrite);
    $self->set('build_links_per_page',          25, $overwrite);
    $self->set('build_new_date_span_pages',     1, $overwrite);
    $self->set('build_new_gb',                  1, $overwrite);
    $self->set('build_cool_gb',                 1, $overwrite);
    $self->set('build_category_sort',           'Name', $overwrite);
    $self->set('build_category_yahoo',          1, $overwrite);
    $self->set('build_category_columns',        2, $overwrite);
    $self->set('build_category_table',          'border=0 width="100%"', $overwrite);
    $self->set('dynamic_no_url_transform',      ['<%build_static_url%>'], $overwrite);
    $self->set('dynamic_pages',                 1, $overwrite);
    $self->set('dynamic_preserve',              ['t', 'd', 's'], $overwrite);
    $self->set('dynamic_preserve_sort_pages',   ['category', 'detailed'], $overwrite);
    $self->set('compress',                      0, $overwrite);

    $self->set('build_detailed',                0, $overwrite);
    $self->set('build_home',                    '', $overwrite);
    $self->set('build_index',                   'index.html', $overwrite);
    $self->set('build_index_include',           1, $overwrite);
    $self->set('build_more',                    'more', $overwrite);
    $self->set('build_extension',               '.html', $overwrite);
    $self->set('build_detail_format',           '%ID%', $overwrite);
    $self->set('build_category_format',         '%Full_Name%', $overwrite);
    $self->set('build_format_compat',           0, $overwrite);
    $self->set('build_category_dynamic',        'Full_Name', $overwrite);
    $self->set('build_dir_per',                 '0777', $overwrite);
    $self->set('build_file_per',                '0666', $overwrite);
}

sub default_user {
# ------------------------------------------------------------------
# Set the user settings to default values.
#
    my ($self, $overwrite) = @_;
    $self->set('user_validation',               1, $overwrite);
    $self->set('user_required',                 1, $overwrite);
    $self->set('user_rate_required',            1, $overwrite);
    $self->set('user_direct_mod',               0, $overwrite);
    $self->set('user_allow_pass',               1, $overwrite);
    $self->set('user_sessions',                 'Cookies', $overwrite);
    $self->set('user_session_length',           3, $overwrite);

    $self->set('framed_jump',                   0, $overwrite);

    $self->set('bookmark_enabled',              1, $overwrite);
    $self->set('bookmark_folder_default_name',  'Default', $overwrite);
    $self->set('bookmark_folder_limit',         5, $overwrite);
    $self->set('bookmark_folders_per_page',     10, $overwrite);
    $self->set('bookmark_links_limit',          25, $overwrite);
    $self->set('bookmark_links_per_page',       25, $overwrite);
    $self->set('bookmark_links_sort',           'Title', $overwrite);
    $self->set('bookmark_links_sort_order',     'ASC', $overwrite);
    $self->set('bookmark_users_per_page',       25, $overwrite);

    $self->set('newsletter_enabled',            0, $overwrite);
    $self->set('newsletter_global_subscribe',   0, $overwrite);
    $self->set('newsletter_max_depth',          2, $overwrite);
}

sub default_email {
# ------------------------------------------------------------------
# Set the email settings to default values.
#
    my ($self, $overwrite) = @_;
    $self->set('db_admin_email',                '', $overwrite);
    $self->set('db_smtp_server',                '', $overwrite);
    $self->set('db_mail_path',                  _find_sendmail(), $overwrite);
    $self->set('email_add',                     1, $overwrite);
    $self->set('email_mod',                     1, $overwrite);
    $self->set('email_review_add',              1, $overwrite);
    $self->set('email_payment',                 1, $overwrite);
    $self->set('admin_email_add',               1, $overwrite);
    $self->set('admin_email_mod',               1, $overwrite);
    $self->set('admin_email_review_add',        1, $overwrite);
    $self->set('admin_email_review_mod',        1, $overwrite);
}

sub default_date {
# ------------------------------------------------------------------
# Set the default settings for any date routines.
#
    my ($self, $overwrite) = @_;
    $self->set('date_db_format',            '%yyyy%-%mm%-%dd%', $overwrite);
    $self->set('date_review_format',        '%mmm% %d% %yyyy% %h%:%MM%%tt%', $overwrite);
    $self->set('date_user_format',          '%ddd% %mmm% %dd% %yyyy%', $overwrite);
    $self->set('date_long_format',          '%dddd%, %mmmm% %dd% %yyyy%', $overwrite);
    $self->set('date_expiry_format',        '%dddd% %mmm% %d% %yyyy% %h%:%MM% %tt%', $overwrite);
    $self->set('date_offset',               0, $overwrite);
    $self->set('date_days_short',           [qw/Sun Mon Tue Wed Thu Fri Sat/], $overwrite);
    $self->set('date_days_long',            [qw/Sunday Monday Tuesday Wednesday Thursday Friday Saturday/], $overwrite);
    $self->set('date_month_short',          [qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/], $overwrite);
    $self->set('date_month_long',           [qw/January February March April May June July August September October November December/], $overwrite);
}

sub default_search {
# ------------------------------------------------------------------
# Update the search settings to default values.
#
    my ($self, $overwrite) = @_;
    $self->set('search_maxhits',                25, $overwrite);
    $self->set('search_bool',                   'AND', $overwrite);
    $self->set('search_substring',              0, $overwrite);
    $self->set('build_sort_order_search',       "score", $overwrite);
    $self->set('build_sort_order_search_cat',   "score", $overwrite);
    $self->set('build_search_gb',               1, $overwrite);
    $self->set('search_blocked',                [], $overwrite);
    $self->set('search_highlighting',           1, $overwrite);
    $self->set('search_highlight_colors',       5, $overwrite);
}

sub default_review {
# ------------------------------------------------------------------
# Update the review settings to default values.
#
    my ($self, $overwrite) = @_;
    $self->set('user_review_required',          1, $overwrite);
    $self->set('review_auto_validate',          0, $overwrite);
    $self->set('review_allow_modify',           1, $overwrite);
    $self->set('review_modify_timeout',         0, $overwrite);
    $self->set('review_max_reviews',            1, $overwrite);
    $self->set('reviews_per_page',              5, $overwrite);
    $self->set('review_sort_by',                'Review_Date', $overwrite);
    $self->set('review_sort_order',             'desc', $overwrite);
    $self->set('review_convert_br_tags',        1, $overwrite);
    $self->set('review_days_old',               7, $overwrite);
}


sub default_misc {
# ------------------------------------------------------------------
# Set the misc settings to default values.
#
    my ($self, $overwrite) = @_;
    $self->set('reg_number',                    '', $overwrite);
    $self->set('nph_headers',                   1,  $overwrite);
    $self->set('header_charset',                'ISO-8859-1', $overwrite);
    $self->set('debug_level',                   0,  $overwrite);
    $self->set('error_message',                 '', $overwrite);
    $self->set('disabled',                      0,  $overwrite);
    $self->set('bans',                          [], $overwrite);
    $self->set('link_validate_date',            1,  $overwrite);
}

sub default_other {
# ------------------------------------------------------------------
# Update settings not available throught the web.
#
    my ($self, $overwrite) = @_;
    $self->set('private_sessions', 1, $overwrite);
    $self->set('db_hit_expire',    2, $overwrite);
    $self->set('db_rate_expire',   2, $overwrite);
    $self->set('quick_links', {
        'admin.cgi?do=page&page=tools_validate.html' => 'Validate Links',
        'admin.cgi?do=page&page=tools_validate_changes.html' => 'Validate Changes',
        'admin.cgi?do=page&page=tools_validate_reviews.html' => 'Validate Reviews'
    }, $overwrite);
}

sub _find_admin_url {
# ------------------------------------------------------------------
# Return base url of current script.
#
    my $url = GT::CGI->url({ absolute => 1, query_string => 0 });
    $url    =~ s,/[^/]*$,,;
    return $url;
}

sub _find_cgi_url {
# ------------------------------------------------------------------
# Returns base url of one level back.
#
    my $url = _find_admin_url();
    $url =~ s,/admin$,,;
    return $url;
}

sub _find_pages_url {
# ------------------------------------------------------------------
# Returns pages url.
#
    return _find_cgi_url();
}

sub _find_perl {
# ------------------------------------------------------------------
# Returns path to perl.
#
    my @poss_perls = qw! 
        /usr/local/bin/perl  /usr/bin/perl  /bin/perl
        /usr/local/bin/perl5 /usr/bin/perl5 /bin/perl
        /perl/bin/perl.exe c:/perl/bin/perl.exe d:/perl/bin/perl.exe
    !;
    foreach my $perl_path (@poss_perls) {
        return $perl_path if -f $perl_path and -x _;
    }
    return '';
}

sub _find_sendmail {
# ------------------------------------------------------------------
# Looks for sendmail.
#
    for my $sendmail (qw|/usr/sbin/sendmail /usr/lib/sendmail /usr/bin/sendmail /bin/sendmail|) {
        return $sendmail if -f $sendmail and -x _;
    }
    return '';
}

1;
