============================================================================================================================
Modification Name: Custom Themes

Version: 1.0

Author: GuldantheWarlock (GuldantheWarlock@xmbgarage.com)

Last Updated: 04-21-13

Description:
This modification will allow members to create their own custom theme to use on the board.
This modification will allow members to share their custom themes with other members on the board.
This modification will allow Administrators to set which image directories members can use in their custom themes.

Supported Version: XMB 1.9.5 SP1

Notes:
DO NOT under any circumstances use the 'Fix Member UIDs' modification/tool or you will break this modification as it is UID based.

License Note: This modification is released under the GPL License v3. A copy is provided with this software package.

Please backup your files before installing this modification.
Neither XMBGarage nor the author can be held responsible if your board stops functioning properly due to you installing this modification.
============================================================================================================================
=======
Step 1:
=======

Upload files 'install.php' and 'customtheme.xmb' from the 'Contents' folder to your forum directory and
run install.php by directing your browser to it.

Example: http://www.domain.ext/forum/install.php

NOTE: MAKE SURE YOU DO THIS STEP BEFORE EDITING ANYTHING ELSE, OR YOU MAY RESULT IN ERRORS!

============================================================================================================================
=======
Step 2:
=======
================
Edit File: header.php
================
==========
Find Code:
==========

,'members'

================
Replace Code With:
================

,'members','members_themes'

==========
Find Code:
==========

// Check what theme to use
if ((int) $themeuser > 0) {
    $theme = (int) $themeuser;
} elseif (!empty($forumtheme) && (int) $forumtheme > 0) {
    $theme = $forumtheme;
} else {
    $theme = $SETTINGS['theme'];
}
    
================
Replace Code With:
================

// Check what theme to use
if (X_MEMBER && checkctsetting($self['customtheme'], 1) == '1') {
    $themetable = $table_members_themes;
    $theme = "uid='$self[uid]'";
} else {
    if ((int) $themeuser > 0) {
        $theme = (int) $themeuser;
        $theme = "themeid='$theme'";
        $themetable = $table_themes;
    } elseif (!empty($forumtheme) && (int) $forumtheme > 0) {
        $theme = "themeid='$forumtheme'";
        $themetable = $table_themes;
    } else {
        $theme = $SETTINGS['theme'];
        $theme = "themeid='$theme'";
        $themetable = $table_themes;
    }
}

==========
Find Code:
==========

$query = $db->query("SELECT * FROM $table_themes WHERE themeid='$theme'");
    
================
Replace Code With:
================

$query = $db->query("SELECT * FROM $themetable WHERE $theme");
    
==========
Find Code:
==========

$imgdir = './'.$imgdir;
    
================
Replace Code With:
================

$imgdir = (false === strpos($imgdir, 'http')) ? ROOT.$imgdir : $imgdir;
    
============================================================================================================================
=======
Step 3:
=======
==================
Edit File: functions.php
==================
=========================
Add To End Of File ABOVE  ?>
=========================

// Custom Themes Mod Begin
function checkctsetting($hash, $value) {
    $hash = explode("|", $hash);
    return $hash[$value-1];
}
// Custom Themes Mod End

============================================================================================================================
=======
Step 4:
=======
=================
Edit File: memcp.php
=================
==========
Find Code:
==========

loadtemplates(

================
Replace Code With:
================

loadtemplates(
'memcp_customthemes',
'customthemes_activate',
'customthemes_admin',
'customthemes_adminblock',
'customthemes_settings',
'customthemes_userthemes',

==========
Find Code:
==========

    case "favorites":
        nav('<a href="memcp.php">'.$lang['textusercp'].'</a>');
        nav($lang['textfavorites']);
        break;
        
===============
Add Code Below:
==============

    // Custom Themes Mod Begin
    case "customtheme":
        nav('<a href="memcp.php">'.$lang['textusercp'].'</a>');
        nav($lang['customtheme']);
        break;
    // Custom Themes Mod End
        
==========
Find Code:
==========

    if ($current == "favorites") {
        echo "<td bgcolor=\"$altbg1\" width=\"15%\" class=\"ctrtablerow\">" .$lang['textfavorites']. "</td>";
    } else {
        echo "<td bgcolor=\"$altbg2\" width=\"15%\" class=\"ctrtablerow\"><a href=\"memcp.php?action=favorites\">" .$lang['textfavorites']. "</a></td>";
    }
    
===============
Add Code Below:
===============

    // Custom Themes Mod Begin
    if ($current == 'customtheme') {
        echo "<td bgcolor=\"$altbg1\" width=\"15%\" class=\"ctrtablerow\">".$lang['customtheme']."</td>";
    } else {
        echo "<td bgcolor=\"$altbg2\" width=\"15%\" class=\"ctrtablerow\"><a href=\"memcp.php?action=customtheme\">".$lang['customtheme']."</a></td>";
    }
    // Custom Themes Mod End

==========
Find Code:
==========

} else {
    eval('echo "'.template('header').'";');
    eval($lang['evalusercpwelcome']);
    
==========
Add Above:
==========

// Custom Themes Mod Begin
} elseif ($action == 'customtheme') {
    $section = isset($section) ? $section : '';
    $selHTML = 'selected="selected"';
    $cheHTML = 'checked="checked"';
    $disHTML = 'disabled="disabled"';
    $query = $db->query("SELECT * FROM $table_members_themes WHERE uid='0'");
    $ctsettings = $db->fetch_array($query);
    $db->free_result($query);
    
    switch($section) {
        case 'admin':
            if (!X_ADMIN) {
                redirect('memcp.php?action=customtheme', 0);
            }
            if (isset($ctadminsubmit) && !empty($ctadminsubmit)) {
                if ($ctsettings['customtheme'] != $dthemenew) {
                    $t = $db->fetch_array($db->query("SELECT * FROM $table_themes WHERE themeid='$dthemenew'"));
                    $db->query("UPDATE $table_members_themes SET customtheme='$t[themeid]', name='$t[name]', bgcolor='$t[bgcolor]', altbg1='$t[altbg1]', altbg2='$t[altbg2]', link='$t[link]', bordercolor='$t[bordercolor]', header='$t[header]', headertext='$t[headertext]', top='$t[top]', catcolor='$t[catcolor]', tabletext='$t[tabletext]', text='$t[text]', borderwidth='$t[borderwidth]', tablewidth='$t[tablewidth]', tablespace='$t[tablespace]', fontsize='$t[fontsize]', font='".addslashes($t[font])."', boardimg='$t[boardimg]', imgdir='$t[imgdir]', smdir='$t[smdir]', cattext='$t[cattext]' WHERE uid='0'");
                }
                $db->query("UPDATE $table_settings SET customtheme='$option1new|$option2new|$option3new|$option4new'");
                $message = '<center><span class="mediumtxt">'.$lang['ctsettingsupdate'].'</span></center>';
                redirect('memcp.php', 2);
            } else {
                $tquery = $db->query("SELECT themeid, name FROM $table_themes ORDER BY name ASC");
                $themebox   = array();
                $themebox[] = "<select name=\"dthemenew\">";
                while($theme = $db->fetch_array($tquery)) {
                    $selected   = ($ctsettings['customtheme'] == $theme['themeid']) ? $selHTML : '';
                    $themebox[] = "<option value=\"$theme[themeid]\" $selected>$theme[name]</option>";
                }
                $themebox[] = "</select>";
                $themebox   = implode("\n", $themebox);
                $option1yes = (checkctsetting($SETTINGS['customtheme'], 1) == '1') ? $cheHTML : '';
                $option2yes = (checkctsetting($SETTINGS['customtheme'], 2) == '1') ? $cheHTML : '';
                $option3yes = (checkctsetting($SETTINGS['customtheme'], 3) == '1') ? $cheHTML : '';
                $option4yes = (checkctsetting($SETTINGS['customtheme'], 4) == '1') ? $cheHTML : '';
                $option1no  = (checkctsetting($SETTINGS['customtheme'], 1) == '0') ? $cheHTML : '';
                $option2no  = (checkctsetting($SETTINGS['customtheme'], 2) == '0') ? $cheHTML : '';
                $option3no  = (checkctsetting($SETTINGS['customtheme'], 3) == '0') ? $cheHTML : '';
                $option4no  = (checkctsetting($SETTINGS['customtheme'], 4) == '0') ? $cheHTML : '';
                $template = 'customthemes_admin';
            }
            break;
            
        case 'activate':
            $query = $db->query("SELECT themeid FROM $table_members_themes WHERE uid='$self[uid]'");
            if ($db->num_rows($query) == 0) {
                if (isset($activatesubmit)) {
                    $tname = addslashes(str_replace('*USER*', $self['username'], $lang['customthemename']));
                    if (checkctsetting($SETTINGS['cusomtheme'], 4) == '1') {
                        $db->query("INSERT INTO $table_members_themes SELECT * FROM $table_members_themes WHERE uid='0'");
                        $db->query("UPDATE $table_members_themes SET uid='$self[uid]', customtheme='0|0', name='$tname' WHERE uid='0' AND themeid != '1'");
                    } else {
                        $db->query("INSERT INTO $table_members_themes (uid, customtheme, name) VALUES ('$self[uid]', '0|0', '$tname')");
                    }
                    $message = '<center><span class="mediumtxt">'.$lang['ctactivated'].'</span></center>';
                    redirect('memcp.php?action=customtheme', 2);
                } else {
                    $template = 'customthemes_activate';
                }
            } else {
                redirect('memcp.php?action=customtheme', 0);
            }
            break;
            
        default:
            $query = $db->query("SELECT * FROM $table_members_themes WHERE uid='$self[uid]'");
            if ($db->num_rows($query) != 0) {
                if (isset($settingsubmit)) {
                    $db->query("UPDATE $table_members SET customtheme='$usethemenew|$sharethemenew' WHERE uid='$self[uid]'");
                    $db->query("UPDATE $table_members_themes SET customtheme='$usethemenew|$sharethemenew' WHERE uid='$self[uid]'");
                    
                    $message = '<center><span class="mediumtxt">'.$lang['ctusersettingsupdated'].'</span></center>';
                    redirect('memcp.php?action=customtheme', 2);
                } elseif (isset($themesubmit)) {
                    $ctheme = $db->fetch_array($query);
                    $nametxt = '';
                    $namenew = addslashes($namenew);
                    $namenew = isset($namenew) ? $namenew : $ctheme['name'];
                    if ($namenew != $ctheme['name']) {
                        $nametxt = "name='$namenew',";
                    }
                    $db->query("UPDATE $table_members_themes SET $nametxt bgcolor='$bgcolornew', altbg1='$altbg1new', altbg2='$altbg2new', link='$linknew', bordercolor='$bordercolornew', header='$headernew', headertext='$headertextnew', top='$topnew', catcolor='$catcolornew', tabletext='$tabletextnew', text='$textnew', borderwidth='$borderwidthnew', tablewidth='$tablewidthnew', tablespace='$tablespacenew', fontsize='$fontsizenew', font='".addslashes($fontnew)."', boardimg='$boardlogonew', imgdir='$imgdirnew', smdir='$smdirnew', cattext='$cattextnew' WHERE uid='$self[uid]'");
                    
                    $message = '<center><span class="mediumtxt">'.$lang['ctthemeupdated'].'</span></center>';
                    redirect('memcp.php?action=customtheme', 2);
                } elseif (isset($changetheme)) {
                    $tquery = $db->query("SELECT t.*, m.username FROM $table_members_themes t LEFT JOIN $table_members m ON t.uid=m.uid WHERE t.themeid='$newtheme'");
                    $ntheme = $db->fetch_array($tquery);
                    $db->query("UPDATE $table_members_themes SET bgcolor='$ntheme[bgcolor]', altbg1='$ntheme[altbg1]', altbg2='$ntheme[altbg2]', link='$ntheme[link]', bordercolor='$ntheme[bordercolor]', header='$ntheme[header]', headertext='$ntheme[headertext]', top='$ntheme[top]', catcolor='$ntheme[catcolor]', tabletext='$ntheme[tabletext]', text='$ntheme[text]', borderwidth='$ntheme[borderwidth]', tablewidth='$ntheme[tablewidth]', tablespace='$ntheme[tablespace]', fontsize='$ntheme[fontsize]', font='".addslashes($ntheme[font])."', boardimg='$ntheme[boardimg]', imgdir='$ntheme[imgdir]', smdir='$ntheme[smdir]', cattext='$ntheme[cattext]' WHERE uid='$self[uid]'");
                    
                    eval($lang['cttransfercode']);
                    $message = '<center><span class="mediumtxt">'.$lang['ctthemetransfer'].'</span></center>';
                    redirect('memcp.php?action=customtheme', 2);
                } elseif (isset($resetsubmit)) {
                    $db->query("UPDATE $table_members_themes SET bgcolor='$ctsettings[bgcolor]', altbg1='$ctsettings[altbg1]', altbg2='$ctsettings[altbg2]', link='$ctsettings[link]', bordercolor='$ctsettings[bordercolor]', header='$ctsettings[header]', headertext='$ctsettings[headertext]', top='$ctsettings[top]', catcolor='$ctsettings[catcolor]', tabletext='$ctsettings[tabletext]', text='$ctsettings[text]', borderwidth='$ctsettings[borderwidth]', tablewidth='$ctsettings[tablewidth]', tablespace='$ctsettings[tablespace]', fontsize='$ctsettings[fontsize]', font='".addslashes($ctsettings[font])."', boardimg='$ctsettings[boardimg]', imgdir='$ctsettings[imgdir]', smdir='$ctsettings[smdir]', cattext='$ctsettings[cattext]' WHERE uid='$self[uid]'");
                    
                    $message = '<center><span class="mediumtxt">'.$lang['ctthemereset'].'</span></center>';
                    redirect('memcp.php?action=customtheme', 2);
                } else {
                    $ctheme = $db->fetch_array($query);
                    $usethemeyes   = (checkctsetting($self['customtheme'], 1) == '1') ? $cheHTML : '';
                    $usethemeno    = (checkctsetting($self['customtheme'], 1) == '0') ? $cheHTML : '';
                    $sharethemeyes = (checkctsetting($self['customtheme'], 2) == '1') ? $cheHTML : '';
                    $sharethemeno  = (checkctsetting($self['customtheme'], 2) == '0') ? $cheHTML : '';
                    eval('$settingstable = "'.template('customthemes_settings').'";');
                    
                    $namedisabled  = (checkctsetting($SETTINGS['customtheme'], 3) == '0') ? $disHTML : '';
                    $themefont = htmlspecialchars($ctheme['font']);
                    $imgdirbox = (checkctsetting($SETTINGS['customtheme'], 1) == '1') ? '<input type="text" name="imgdirnew" value="'.$ctheme['imgdir'].'" />' : NULL;
                    $smdirbox  = (checkctsetting($SETTINGS['customtheme'], 2) == '1') ? '<input type="text" name="smdirnew" value="'.$ctheme['smdir'].'" />' : NULL;
                    if ($imgdirbox === NULL) {
                        $imgdirbox = array();
                        $imgdirquery = $db->query("SELECT DISTINCT imgdir FROM $table_themes ORDER BY imgdir ASC");
                        $imgdirbox[] = "<select name=\"imgdirnew\">";
                        while($imd = $db->fetch_array($imgdirquery)) {
                            $selected = ($imd['imgdir'] == $ctheme['imgdir']) ? $selHTML : '';
                            $imgdirbox[] = "<option value=\"$imd[imgdir]\" $selected>$imd[imgdir]</option>";
                        }
                        $imgdirbox[] = "</select>";
                        $imgdirbox   = implode("\n", $imgdirbox);
                    }
                    
                    if ($smdirbox === NULL) {
                        $smdirbox = array();
                        $smdirquery = $db->query("SELECT DISTINCT smdir FROM $table_themes ORDER BY smdir ASC");
                        $smdirbox[] = "<select name=\"smdirnew\">";
                        while($smd = $db->fetch_array($smdirquery)) {
                            $selected = ($smd['smdir'] == $ctheme['smdir']) ? $selHTML : '';
                            $smdirbox[] = "<option value=\"$smd[smdir]\" $selected>$smd[smdir]</option>";
                        }
                        $smdirbox[] = "</select>";
                        $smdirbox   = implode("\n", $smdirbox);
                    }
                    
                    $tquery = $db->query("SELECT themeid, name FROM $table_members_themes WHERE uid != '$self[uid]' AND (customtheme='0|1' OR customtheme='1|1')");
                    $tbox   = array();
                    $tbox[] = "<select name=\"newtheme\">";
                    $tbox[] = "<option value=\"\">".$lang['ctchoosetheme']."</option>";
                    while($theme = $db->fetch_array($tquery)) {
                        $tbox[] = "<option value=\"$theme[themeid]\">$theme[name]</option>";
                    }
                    $tbox[] = "</select>";
                    $tbox   = implode("\n", $tbox);
                    eval('$themetable = "'.template('customthemes_userthemes').'";');
                    
                    $adminarea = '';
                    if (X_ADMIN) {
                        eval('$adminarea = "'.template('customthemes_adminblock').'";');
                    }
                    $template = 'memcp_customthemes';
                }
            } else {
                redirect('memcp.php?action=customtheme&section=activate', 0);
            }
            break;
    }
    eval('echo "'.template('header').'";');
    makenav($action);
    if (isset($template)) {
        eval('echo stripslashes("'.template($template).'");');
    } else {
        echo $message;
    }
// Custom Themes Mod End

============================================================================================================================
=======
Step 5:
=======
=======================
Edit File: lang/English.lang.php
=======================
=========================
Add To End Of File ABOVE  ?>
=========================

// Custom Themes Mod Begin
$lang['customtheme'] = "Custom Theme";
$lang['ctsettingsupdate'] = "Custom Theme settings successfully updated!";
$lang['customthemename'] = "*USER*'s Custom Theme";
$lang['ctactivated'] = "Your custom theme has now been activated!";
$lang['ctusersettingsupdated'] = "Your settings have been updated.";
$lang['ctthemeupdated'] = "Your Custom Theme has been successfully updated!";
$lang['cttransfercode'] = '$lang["ctthemetransfer"] = "You are now using $ntheme[username]\'s Custom Theme";';
$lang['ctwelcome'] = "Welcome to the Custom Theme Manager. Here you can create your own Custom Theme or edit someone else's.";
$lang['ctthemehead'] = "Create Your ".$lang['customtheme'];
$lang['ctedittheme'] = "Edit your Custom Theme Below:";
$lang['textexample'] = "Example:";
$lang['textsave'] = "Save";
$lang['textcancel'] = "Cancel";
$lang['ctoptions'] = "Custom Theme Options";
$lang['ctoptionshead'] = "Edit your Custom Theme Settings Below:";
$lang['ctreset'] = "Reset to Default Theme";
$lang['ctthemereset'] = "Custom Theme has been reset to default values";
$lang['ctusetheme'] = "Use Custom Theme?";
$lang['ctsharetheme'] = "Do you want to share your custom theme with other users?";
$lang['ctusestheme'] = "Use this Theme";
$lang['ctuserthemes'] = "Use a Shared Theme";
$lang['ctuserthemeshead'] = "Instead of making your own Custom Theme, you can use a Custom Theme someone else has decided to share.<br />Scroll through the dropdown list to see who has shared their Custom Theme.<br />Then click the ".$lang['ctusestheme']." button to copy their Custom Theme to yours.<br />Note: you can not see your own theme in the list.";
$lang['ctadminopts'] = "Administrator Options";
$lang['ctadminarea'] = "Access Admin Area";
$lang['ctactivation'] = $lang['customtheme']." Activation";
$lang['ctnotactivated'] = "You have not activated your Custom Theme yet.";
$lang['ctactivate'] = "Click here to activate your Custom Theme now";
$lang['ctadminsettings'] = "Custom Theme Settings";
$lang['ctadminhead'] = "Edit the global Custom Theme Settings here";
$lang['ctdtheme'] = "Select the Theme that should be used as a default for new Custom Themes:";
$lang['ctuseimgdir'] = "Can users supply their own Image Directory? If not, then they must choose from the local theme directories.";
$lang['ctusesmdir'] = "Can users supply their own Smilie Directory? If not, then they must choose from the local smilie directories.";
$lang['ctownthemename'] = "Can users set their own Theme name? If not, it will be set to:<br /><em>*Username*'s Custom Theme</em>";
$lang['ctusedtheme'] = "When a user activates their Custom Theme, should they get the Default Theme values by Default?";
$lang['ctchoosetheme'] = "Choose Theme";
// Custom Themes Mod End

============================================================================================================================
Enjoy!