Comparing revisions for MySkin

Additions:
===== MySkin: choose and modify your favorite skin =====
{{lastedit}}
<<Give it a [[TestSkin try]]! (v.1.0)<<
::c::
=== Current version: ##2.1## ===
~-added "Hide source" button, as per NilsLinenberg's suggestion;
* This action allows the user to select a skin among those available in
* the Wikka css folder. A form allows to display the markup of each skin.
* Registered users can create it and modify their own custom skin. The
* first creation of a custom user skin consists in the generation of a new
* css stylesheet containing the default wikka CSS settings (as specified
* in the Wikka config file) and stored with the name of the user. Once a
* it with an existing skin, or restore the default settings. Wikka
* Cookies must be enabled for the selector to work and the css folder must
* be write-accessible to the script in order to read, edit and save skins.
* @version 2.1
* @since Wikka 1.1.6.X
* @output displays a form for selecting alternate skins and edit
* @todo - store custom skin in DB;
if ($this->GetConfigValue("allow_select_skin") == "0") {
echo $this->Format('//Sorry, skin selection is disabled on this site//');
} else {
// initialize variables
$defaultskin = $this->GetConfigValue("default_stylesheet");
$currentskin = $this->GetSkin();
$postedskin = '';
$myskin = '';
$submit = '';
// get skin names
$postedskin = (isset($_POST["skin"]) && (strlen($_POST["skin"]) > 4))? $_POST["skin"] : $currentskin;
if ($this->GetUser()) $myskin = 'user_'.strtolower($this->GetUserName()).'.css';

// build form chunks
$setskin = '<input type="submit" name="action" value="Set skin" title="Load this skin" />';
$showsource = '<input type="submit" name="action" value="Show source" title="Display the source of '.$currentskin.'" />';
$hidesource = '<input type="submit" name="action" value="Hide source" title="Hide the source box" />';
$editsource = '<input type="submit" name="action" value="Edit source" title="Modify the source of this skin" />';
$savesource = '<input type="submit" name="action" value="Save modified source" title="Store changes to '.$currentskin.'" />';
$createskin = '<input type="submit" name="action" value="Create custom skin" title="Generate a custom skin for '.$this->GetUserName().'" />';
$importskin = '<input type="submit" name="action" value="Save current skin as custom skin" title="Store a copy of '.$currentskin.' as '.$myskin.'" />';
$restoreskin = '<input type="submit" name="action" value="Restore to default settings" title="Reset '.$myskin.' to the default system stylesheet ('.$defaultskin.')" />';
echo $this->Format('==== Select a skin: ==== --- ');
echo $this->Format('(Current skin: **##'.$currentskin.'##**) --- --- ');
switch ($_POST["action"]) {
case "Save modified source":
// saves modified skin to file
$this->WriteSkin($currentskin, $_POST["mod_css_content"]);
// no break - skin is automatically updated

case "Set skin":
// load skin and reload page
if (file_exists("css/".$postedskin)) {
$this->SetSkin($postedskin);
$this->Redirect($this->href());
break;

case "Save current skin as custom skin":
// import and save current skin as user skin
if ($this->GetUser() && file_exists("css/".$myskin)) {
$css_content = $this->ReadSkin($currentskin);
$this->WriteSkin($myskin, $css_content);
$this->SetSkin($myskin);
$this->Redirect($this->href());
break;
case "Create custom skin":
// first-time custom user skin creation
if ($this->GetUser() && !file_exists("css/".$myskin)) {
$css_content = $this->ReadSkin($defaultskin);
$this->WriteSkin($myskin, $css_content);
$this->SetSkin($myskin);
$this->Redirect($this->href());
break;
case "Restore to default settings":
// restore user skin to default settings
if ($this->GetUser() && file_exists("css/".$myskin)) {
$css_content = $this->ReadSkin($defaultskin);
$this->WriteSkin($myskin, $css_content);
$this->SetSkin($myskin);
$this->Redirect($this->href());
break;
case "Show source":
if ($this->GetConfigValue("allow_display_skin_source") == "1") {
// open a readonly textarea with skin source
$css_contents = $this->ReadSkin($currentskin);
$showskin = '<textarea id="skinedit" name="display_css_content" cols="80" rows="15" readonly="readonly">'.$css_contents.'</textarea><br />';
$submit = $hidesource;
break;
case "Edit source":
if ($this->GetConfigValue("allow_display_skin_source") == "1" || $this->IsAdmin || $currentskin == $myskin) {
// open an editable textarea with skin source
$css_contents = $this->ReadSkin($currentskin);
$showskin = '<textarea id="skinedit" name="mod_css_content" cols="80" rows="15">'.$css_contents.'</textarea><br />';
$submit = $savesource.$hidesource;
break;
default:
// give write access to the skin owner and to admins
switch ($this->GetConfigValue('allow_display_skin_source')) {
case '0': #hide source, no one except admins can edit skins
$submit = ($this->IsAdmin())? $editsource : '';
break;
case '1': #anyone can display source, skin owners can edit their skin, admins can edit any skin
$submit = ($this->IsAdmin() || $currentskin == $myskin)? $editsource : $showsource;
break;
}
// retrieve skin list
$handle = opendir('css/');
$skinlist = '<select name="skin">';
// put on top of the list the default and custom skins
$defaultselected = ($defaultskin == $currentskin)? " selected=\"selected\"" : "";
$myselected = ($myskin == $currentskin)? " selected=\"selected\"" : "";
$skinlist .= '<option value="'.$defaultskin.'"'.$defaultselected.'>(Default skin: '.$defaultskin.')</option>';
if ($this->GetUser() && file_exists("css/".$myskin)) $skinlist .= '<option value="'.$myskin.'"'.$myselected.'>(Custom skin: '.$myskin.')</option>';

// get system skins
$noskinmask = '^('.$defaultskin.'|'.$myskin.'|xml.css|print.css|\.(.*)|user_(.*))$';
$skinlist .= '<option value="" disabled="disabled">--- Default skins: ---</option>';
while (false !== ($file = readdir($handle))) {
if ((!preg_match('/'.$noskinmask.'/', $file)) && (preg_match('/(.*)\.css/', $file))) {
$selected = ($file == $currentskin)? " selected=\"selected\"" : "";
$skinlist .= '<option value="'.$file.'"'.$selected.'>'.$file.'</option>';
}
// get user skins
if ($this->GetConfigValue("display_custom_skins") == "1") {
$handle = opendir('css/'); #reset directory
$skinlist .= '<option value="" disabled="disabled">--- User skins: ---</option>';
while (false !== ($file = readdir($handle))) {
if ((preg_match('/user_(.*)/', $file)) && (!preg_match('/'.$myskin.'/', $file))) {
$selected = ($file == $currentskin)? " selected=\"selected\"" : "";
$skinlist .= '<option value="'.$file.'"'.$selected.'>'.$file.'</option>';
}
$skinlist .= '</select>';
// create form
print $this->FormOpen("","","post");
print $skinlist.$setskin."<br /><br />".$showskin.$submit."<br /><br />\n";
// display custom skin options
if (($this->GetConfigValue("allow_create_custom_skin") == "1") && $this->GetUser()) {
if (!file_exists("css/".$myskin)) {
$mysubmit = $createskin; #display skin creation button
} else {
$myskinname = "(".$myskin.")";
$mysubmit = ($currentskin == $myskin)? $restoreskin : $importskin.$restoreskin;
print $this->Format(" ---- (Custom skin **##".$myskinname."##**) --- --- ");
print $mysubmit;
// close form
print $this->FormClose();
closedir($handle);
Add the following code in ##./wikka.php## immediately after the ##""//COOKIES""## section
$skin = $_SESSION['skin'];
if ($_COOKIE['skin'])
$skin = $_COOKIE['skin'];
$skin = $this->GetConfigValue("default_stylesheet");
$_SESSION['skin'] = $skin;
$this->SetPersistentCookie("skin", $skin);
function WriteSkin($file, $content){
$css_file = fopen("css/".$file, "w+");
fwrite($css_file, $content);
fclose($css_file);
}

function ReadSkin($file){
$source = fopen("css/".$file, "r");
$css_content = fread($source, filesize("css/".$file));
fclose($source);
return $css_content;
}
"default_stylesheet" => "wikka.css",
~-##default_stylesheet## --- Defines the system stylesheet;
Deletions:
===== MySkin: select, create and edit your favorite stylesheet =====
<<===Theme support available in 1.2!===
This page refers to beta functionality supported until Wikka 1.1.x. As of [[Docs:WhatsNew12 version 1.2]] Wikka introduces support for 100%-modular **themes**: check [[Docs:WikkaThemes this page]] for more information or this [[http://blog.wikkawiki.org/2009/09/11/how-to-design-themes-for-wikkawiki/ tutorial]] to learn how to design custom themes.
<<::c::
=== Current version: ##2.2## ===
==2.2==
~-Added JW's patches
==2.1==
~-added "Hide source" button, as per NilsLindenberg's suggestion;
%%(php;1)
* This action allows the user to select a skin among those available in
* the Wikka css folder. A form allows to display the markup of each skin.
* Registered user can create it and modify their own custom skin. The
* first creation of a custom user skin consists in the generation of a new
* css stylesheet containing the default wikka CSS settings (as specified
* in the Wikka config file) and stored with the name of the user. Once a
* it with an existing skin, or restore the default settings. Wikka
* Cookies must be enabled for the selector to work and the css folder must
* be write-accessible to the script in order to read, edit and save skins.
* @version 2.2
* @since Wikka 1.1.X
* @output displays a form for selecting alternate skins and edit
* @todo -integrate with UserSettings.
// get skin names
$defaultskin = $this->config['stylesheet'];
$currentskin = (!$this->GetCookie('wikiskin')) ? $defaultskin : $this->GetCookie('wikiskin'); # JW 2005-07-08 FIX possibly undefined cookie
$postedskin = $_POST["skin"];
$myskin = strtolower($this->GetUserName().".css");
// build form chunks
$setskin = '<input type="submit" name="action" value="Set skin" />';
$showsource = '<input type="submit" name="action" value="Show source" />';
$hidesource = '<input type="submit" name="action" value="Hide source" />';
$editsource = '<input type="submit" name="action" value="Edit source" />';
$savesource = '<input type="submit" name="action" value="Save modified source" />';
$createskin = '<input type="submit" name="action" value="Create my skin" />';
$importskin = '<input type="submit" name="action" value="Save current skin as my skin" />';
$restoreskin = '<input type="submit" name="action" value="Restore to default settings" />';
// functions
function WriteSkin($file, $content){
$css_file = fopen("css/".$file, "w+");
fwrite($css_file, $content);
fclose($css_file);
function ReadSkin($file){
$source = fopen("css/".$file, "r");
$css_content = fread($source, filesize("css/".$file));
fclose($source);
return $css_content;
echo $this->Format("=== Select a Wikka skin: === --- ");
switch ($_POST["action"]) {
case "Save modified source":
// saves modified skin to file
WriteSkin($currentskin, $_POST["mod_css_content"]);
// no break - skin is automatically updated
case "Set skin":
// change current skin and reload page
// begin change by AlexRust - May 28 2009
//$this->SetPersistentCookie("wikiskin", $postedskin);
$this->SetSkin($postedskin);
// end change by AlexRust - May 28 2009
$this->Redirect($this->href());
break;
case "Save current skin as my skin":
// import and save current skin as user skin
if ($this->GetUser() && file_exists("css/".$myskin)) {
$css_content = ReadSkin($currentskin);
WriteSkin($myskin, $css_content);
$this->SetPersistentCookie("wikiskin", $myskin);
$this->Redirect($this->href());
break;
case "Create my skin":
// first time user skin creation
if ($this->GetUser() && !file_exists("css/".$myskin)) {
$css_content = ReadSkin($defaultskin);
WriteSkin($myskin, $css_content);
$this->SetPersistentCookie("wikiskin", $myskin);
$this->Redirect($this->href());
break;
case "Restore to default settings":
// restore user skin to default settings
if ($this->GetUser() && file_exists("css/".$myskin)) {
$css_content = ReadSkin($defaultskin);
WriteSkin($myskin, $css_content);
$this->SetPersistentCookie("wikiskin", $myskin);
$this->Redirect($this->href());
break;
case "Show source":
// open a readonly textarea with skin source
$css_contents = ReadSkin($currentskin);
$showskin = '<textarea id="skinedit" name="display_css_content" cols="80" rows="15" readonly="readonly">'.$css_contents.'</textarea><br />';
$submit = $hidesource;
break;
case "Edit source":
// open an editable textarea with skin source
$css_contents = ReadSkin($currentskin);
$showskin = '<textarea id="skinedit" name="mod_css_content" cols="80" rows="15">'.$css_contents.'</textarea><br />';
$submit = $savesource;
break;
$handle = opendir('css/');
// retrieve skin list
$skinlist = '<select name="skin">';
// put on top of the list the default and custom skin
$defaultselected = ($defaultskin == $currentskin)? " selected=\"selected\"" : "";
$myselected = ($myskin == $currentskin)? " selected=\"selected\"" : "";
$skinlist .= '<option value="'.$defaultskin.'"'.$defaultselected.'>(Default skin: '.$defaultskin.')</option>';
if ($this->GetUser() && file_exists("css/".$myskin)) $skinlist .= '<option value="'.$myskin.'"'.$myselected.'>(My skin: '.$myskin.')</option>';
// get other skins
$noskinmask = '^('.$defaultskin.'|'.$myskin.'|xml.css|print.css|\.(.*))$';
while (false !== ($file = readdir($handle))) {
if (!preg_match('/'.$noskinmask.'/', $file)) {
$selected = ($file == $currentskin)? " selected=\"selected\"" : "";
$skinlist .= '<option value="'.$file.'"'.$selected.'>'.$file.'</option>';
$skinlist .= '</select>';
// give write access to the skin owner and to admins
if (!isset($submit)) {
$submit = ($this->IsAdmin() || $currentskin == $myskin)? $editsource : $showsource;
// create form
print $this->FormOpen("","","post");
print $skinlist.$setskin."<br /><br />".$showskin.$submit."<br /><br />\n";
// show user skin options
if ($this->GetUser()) {
if (!file_exists("css/".$myskin)) {
$mysubmit = $createskin;
} else {
$myskinname = "(".$myskin.")";
$mysubmit = ($currentskin == $myskin)? $restoreskin : $importskin.$restoreskin;
print $this->Format(" ---- === My skin ".$myskinname." === --- ");
print $mysubmit;
// close form
print $this->FormClose();
closedir($handle);
Add the following code in ##./wikka.php## immediately after the ##""//COOKIES""## section.
**Note:** This section has moved to ./libs/Wakka.class.php, and the following can be placed after the ""GetCookie($name)"" function.
**Updated 5/5/2009 by PezHore:** Corrected the reference of cookie 'skin' to 'wikiskin', and changed the cookie retrieval from $skin = $_COOKIE['wikiskin] to use ""GetCookie"" function. Also removed the duplicate functions
$skin = $_SESSION['wikiskin'];
if ($_COOKIE['wikiskin'])
$skin = $this->GetCookie('wikiskin');
$skin = $this->GetConfigValue("stylesheet");
$_SESSION['wikiskin'] = $skin;
$this->SetPersistentCookie('wikiskin', $skin);
~-##stylesheet## --- Defines the system stylesheet;
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki