Source for file theme.php
Documentation is available at theme.php
* @copyright (c) 2004-15 bitweaver.org
include_once( '../kernel/setup_inc.php' );
include_once( THEMES_PKG_PATH. 'css_lib.php' );
include_once( KERNEL_PKG_PATH. 'BitSystem.php' );
global $gBitUser, $gBitSystem;
if (!$gBitUser->isRegistered()) {
$gBitSmarty->assign('msg', tra("Permission denied: You are not logged in"));
$gBitSystem->display( 'error.tpl' , NULL, array( 'display_mode' => 'display' ));
include_once(USERS_PKG_PATH. 'lookup_user_inc.php');
if ($gQueryUser->mUserId != $gBitUser->mUserId && !$gBitUser->object_has_permission($gBitUser->mUserId, $gQueryUser->mInfo['content_id'], 'bituser', 'p_users_admin')) {
$gBitSmarty->assign('msg', tra('You do not have permission to edit this user\'s theme'));
$gBitSystem->display('error.tpl', NULL, array( 'display_mode' => 'display' ));
//******* HELPER FUNCTIONS *******
// get an array of filesnames in the given directory
while (false !== ($file = readdir($handle)))
if ($file !== "." && $file !== ".." )
if(!isset ($files)) $files= $file;
else $files = $file. "\r\n". $files;
function delete($dir, $pattern = "*.*")
if (substr($dir,- 1) != "/") $dir.= "/";
if (is_file($dir. $file) && ereg("^". $pattern. "$", $file)){
else // begin 2nd improvement
//******** END HELPER FUNCTIONS **********
// Special case: the admin has turned of custom user themes but this user currently uses his/her custom theme
if ($gQueryUser->getPreference( 'theme' ) == 'custom' && !$gBitUser->canCustomizeTheme() ) {
$gQueryUser->storePreference('theme', NULL); // Set their homepage theme to fall back to the site's themeImages
$gQueryUser->setPreference( 'theme', NULL ); // Update their mPrefs
$usingCustomTheme = ($gQueryUser->getPreference( 'theme' ) == 'custom' ? true : false);
$gBitSmarty->assign_by_ref('usingCustomTheme', $usingCustomTheme);
if( !$gBitUser->canCustomizeTheme() ) {
$gBitSmarty->assign('msg', tra("Feature disabled"));
$gBitSystem->display( 'error.tpl' , NULL, array( 'display_mode' => 'display' ));
$customCSSPath = $gQueryUser->getStoragePath('theme', $gQueryUser->mUserId, NULL); // Path to this user's storage directory
$customCSSFile = $customCSSPath. 'custom.css'; // Path to this user's custom stylesheet
$customCSSImageURL = $gQueryUser->getStorageURL( '/theme/images/', $gQueryUser->mUserId );
$gBitSmarty->assign_by_ref('customCSSImageURL',$customCSSImageURL);
// Create a custom.css for this user if they do not already have one
$gBitSmarty->assign('msg', tra("Unable to create a custom CSS file for you!"));
$gBitSystem->display( 'error.tpl' , NULL, array( 'display_mode' => 'display' ));
if (isset ($_REQUEST['fUseStandardTheme']) && $_REQUEST['fUseStandardTheme']) {
$gQueryUser->storePreference('theme', NULL);
$usingCustomTheme = false;
if (isset ($_REQUEST['fUseCustomTheme']) && $_REQUEST['fUseCustomTheme']) {
$gQueryUser->storePreference('theme', 'custom');
$usingCustomTheme = true;
if (isset ($_REQUEST["fSaveCSS"])and $_REQUEST["fSaveCSS"]) {
// Save any changes the user made to their CSS
$fp = fopen($customCSSFile, "w");
$gBitSmarty->assign('msg', tra("You dont have permission to write the style sheet"));
$gBitSystem->display( 'error.tpl' , NULL, array( 'display_mode' => 'display' ));
fwrite($fp, $_REQUEST["textData"]);
$successMsg[] = "CSS Updated and Saved";
} elseif (isset ($_REQUEST["fCancelCSS"]) && $_REQUEST['fCancelCSS']) {
// Cancel (e.g. do nothing)
$successMsg[] = "Changes have been cancelled";
} elseif (isset ($_REQUEST['fResetCSS'])) {
// Reset CSS (e.g. copy an existing style as a base for their custom style)
mkdir_p( $customCSSPath. '/images' );
$resetStyle = $_REQUEST['resetStyle'];
$cssData = $csslib->load_css2_file(THEMES_PKG_PATH. "styles/$resetStyle/$resetStyle.css");
$gBitThemes->expunge_dir($customCSSPath. '/images/');
$fp = fopen($customCSSFile, "w");
$gBitSmarty->assign('msg', tra("You dont have permission to write the style sheet"));
$gBitSystem->display( 'error.tpl' , NULL, array( 'display_mode' => 'display' ));
$successMsg[] = "Your CSS has been reset to the $resetStyle theme.";
} elseif (isset ($_REQUEST['fUpload'])) {
if (!ereg(".JPG$|.PNG$|.GIF$|.BMP$",strtoupper($_FILES['fImgUpload']['name']))) {
$errorMsg[] = "Your image must be one of the following types: .jpg, .png, .gif, .bmp";
if ($_FILES['fImgUpload']['error'] == UPLOAD_ERR_OK && copy($_FILES['fImgUpload']['tmp_name'], $customCSSPath. '/images/'. $_FILES['fImgUpload']['name'])) {
$successMsg[] = $_FILES['fImgUpload']['name']. " successfully added.";
$errorMsg[] = "There was a problem uploading your image.";
} elseif (isset ($_REQUEST['fDeleteImg'])) {
/*$imgArray = $_REQUEST['fDeleteImg'];
foreach($imgArray as $key => $value) {
$imgPath = $customCSSPath.'/images/'.$key;
if (file_exists($imgPath)) {
$successMsg[] = "$key successfully deleted";
$errorMsg[] = "$key does not exists!";
$imgName = key( $_REQUEST['fDeleteImg'] );
$imgPath = $customCSSPath. '/images/'. $imgName;
$successMsg[] = "$imgName successfully deleted";
$errorMsg[] = "$imgName does not exists!";
// User is selecting from the standard themes
if (isset ($_REQUEST['fChangeTheme']) && $_REQUEST['fChangeTheme']) {
$gQueryUser->storePreference('theme', NULL);
$successMsg[] = "Theme successfully changed to ". $_REQUEST['fStyleChoice'];
$assignStyle = $_REQUEST['fStyleChoice'];
header( 'Location: '. USERS_PKG_URL. 'assigned_modules.php' );
// Get the list of themes the user can choose to derive from (aka Reset to)
$styles = $gBitThemes->getStyles( NULL, ($usingCustomTheme ? FALSE : TRUE), FALSE );
$gBitSmarty->assign_by_ref( 'styles', $styles );
// $assignStyle is the default style which will be selected in the drop down list
if (!isset ($assignStyle)) {
$assignStyle = $gQueryUser->getPreference('theme', 'basic');
$gBitSmarty->assign_by_ref( 'assignStyle', $assignStyle);
// Read in this user's custom.css to display in the textarea
$lines = file($customCSSFile);
foreach ($lines as $line) {
$gBitSmarty->assign('data', $data);
// Export success/error messages for display in the tpl.
$gBitSmarty->assign_by_ref('successMsg',$successMsg);
$gBitSmarty->assign_by_ref('errorMsg', $errorMsg);
// Get the list of images used by this user's custom theme
$imageList = ls_a($customCSSPath. 'images/');
if( count( $imageList ) ) {
foreach ($imageList as $image) {
$gBitSmarty->assign('imagesCount', count($themeImages));
$gBitSmarty->assign_by_ref('themeImages',$themeImages);
$gBitSmarty->assign('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);
$gBitSmarty->assign_by_ref('gQueryUser', $gQueryUser);
$gBitSystem->display( 'bitpackage:users/user_theme.tpl', NULL, array( 'display_mode' => 'display' ));
|