I found few bugs in myskin (good) action from Dartar for WikkaWiki <1.2 version. I decided to adapt it for my purpose without knowing anything from PHP !
Let's go...
Add lines marked with // EMERALDISLAND if you want myskin works with session and registered user.
Add lines with // EMERALDISLAND * if you want a subtle but major change in myskin : you can choose and edit any css file instead of your own. Very useful for me, I changed myskin as an administrator tool.
Create ~/plugins/actions/myskin.
Get this code and save it as myskin.php in previous created directory.
~/plugins/actions/myskin/myskin.php
<?php
/**
* Allows users to select alternate skins and to edit their custom skins.
*
* 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
* custom user skin exists, the owner of the skin can modify it, overwrite
* it with an existing skin, or restore the default settings. Wikka
* administrators have write access to all the skins.
*
* 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.
*
* @package Actions
* @name MySkin
*
* @author {@link http://wikka.jsnx.com/DarTar DarTar}
* @version 2.2
* @since Wikka 1.1.X
*
* @output displays a form for selecting alternate skins and edit
* custom user skins.
* @todo -integrate with UserSettings.
*/
// get skin names
$defaultskin = $this->GetConfigValue('stylesheet');
if (!$this->GetCookie('wikiskin')) { $this->SetPersistentCookie('wikiskin', $defaultskin); } // EMERALDISLAND
$currentskin = (!$this->GetCookie('wikiskin')) ? $defaultskin : $this->GetCookie('wikiskin'); # JW 2005-07-08 FIX possibly undefined cookie
if (!isset($_POST['skin']))
$_POST['skin'] = NULL;
$postedskin = $_POST['skin'];
$myskin = strtolower($this->GetUserName().'.css');
$skintosave = $postedskin; // EMERALDISLAND
$showskin = '<textarea id="skinedit" name="display_css_content" cols="120" rows="16" readonly="readonly"></textarea><br />';
// 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, $wakka){
$css_file = fopen($wakka->GetThemePath().'/css/'.$file, 'w+');
fwrite($css_file, $content);
fclose($css_file);
}
function ReadSkin($file, $wakka){
$source = fopen($wakka->GetThemePath().'/css/'.$file, 'r');
$css_content = fread($source, filesize($wakka->GetThemePath().'/css/'.$file));
fclose($source);
return $css_content;
}
echo $this->Format("=== Select a Wikka skin: === --- ");
if (!isset($_POST['action']))
$_POST['action'] = '';
switch ($_POST['action']) {
case 'Save modified source':
// saves modified skin to file
WriteSkin($skintosave, $_POST["mod_css_content"], $this);
// no break - skin is automatically updated
case 'Set skin':
// change current skin and reload page
if ($this->GetUser()) { $_SESSION['wikiskin'] = $postedskin; } // EMERALDISLAND
else { $this->SetPersistentCookie("wikiskin", $postedskin); }
$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($this->GetThemePath().'/css/'.$myskin)) {
$css_content = ReadSkin($currentskin, $this);
WriteSkin($myskin, $css_content, $this);
$_SESSION['wikiskin'] = $myskin; // EMERALDISLAND
//$this->SetPersistentCookie("wikiskin", $myskin);
$this->Redirect($this->href());
}
break;
case "Create my skin":
// first time user skin creation
if ($this->GetUser() && !file_exists($this->GetThemePath().'/css/'.$myskin)) {
$css_content = ReadSkin($defaultskin, $this);
WriteSkin($myskin, $myskin, $this);
$_SESSION['wikiskin'] = $myskin; // EMERALDISLAND
//$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($this->GetThemePath().'/css/'.$myskin)) {
$css_content = ReadSkin($defaultskin, $this);
WriteSkin($myskin, $css_content, $this);
$_SESSION['wikiskin'] = $myskin; // EMERALDISLAND
//$this->SetPersistentCookie("wikiskin", $myskin);
$this->Redirect($this->href());
}
break;
case "Show source":
// open a readonly textarea with skin source
$css_contents = ReadSkin($currentskin, $this);
$showskin = '<textarea id="skinedit" name="display_css_content" cols="120" rows="16" readonly="readonly">'.$css_contents.'</textarea><br />';
$submit = $hidesource;
break;
case 'Edit source':
// open an editable textarea with skin source
//$css_contents = ReadSkin($currentskin, $this);
$css_contents = ReadSkin($postedskin, $this); // EMERALDISLAND
$skintosave = $postedskin; // EMERALDISLAND
$showskin = '<textarea id="skinedit" name="mod_css_content" cols="120" rows="16">'.$css_contents.'</textarea><br />';
$submit = $savesource.$hidesource;
break;
case 'Hide source':
$this->Redirect($this->href());
break;
}
$handle = opendir($this->GetThemePath().'/css/');
// retrieve skin list
$skinlist = '<select name="skin">';
// put on top of the list the default and custom skin
if (isset($skintosave)) { $skinlist .= '<option value="'.$skintosave.'"'.$skintosave.' selected="selected">(Edited skin: '.$skintosave.')</option>'; }
else { // EMERALDISLAND
$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)) {
if ($this->IsAdmin() || $currentskin == $myskin) {$submit = $editsource;}
else if ($this->GetConfigValue('allow_display_skin_source')) {$submit = $showsource;}
}
// create form
print $this->FormOpen("","","post");
echo '<fieldset>'."\n";
//print $skinlist.$setskin."<br /><br />".$showskin.$submit."<br /><br />\n";
print $skinlist.$setskin."<br /><br />";
if (isset($postedskin)) {print $this->Format(" ---- === Source of ".$postedskin." === --- ");} // EMERALDISLAND *
print $showskin.$submit."<br /><br />\n";
// show user skin options
if ($this->GetUser()) {
if (!file_exists($this->GetThemePath().'/css/'.$myskin)) {
$mysubmit = $createskin;
} else {
$myskinname = "(".$myskin.")";
$mysubmit = ($currentskin == $myskin)? $restoreskin : $importskin.$restoreskin;
}
print $this->Format(" ---- === My skin ".$myskinname." === --- ");
print $mysubmit;
}
// close form
echo "\n".'</fieldset>'."\n";
print $this->FormClose();
closedir($handle);
?>
/**
* Allows users to select alternate skins and to edit their custom skins.
*
* 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
* custom user skin exists, the owner of the skin can modify it, overwrite
* it with an existing skin, or restore the default settings. Wikka
* administrators have write access to all the skins.
*
* 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.
*
* @package Actions
* @name MySkin
*
* @author {@link http://wikka.jsnx.com/DarTar DarTar}
* @version 2.2
* @since Wikka 1.1.X
*
* @output displays a form for selecting alternate skins and edit
* custom user skins.
* @todo -integrate with UserSettings.
*/
// get skin names
$defaultskin = $this->GetConfigValue('stylesheet');
if (!$this->GetCookie('wikiskin')) { $this->SetPersistentCookie('wikiskin', $defaultskin); } // EMERALDISLAND
$currentskin = (!$this->GetCookie('wikiskin')) ? $defaultskin : $this->GetCookie('wikiskin'); # JW 2005-07-08 FIX possibly undefined cookie
if (!isset($_POST['skin']))
$_POST['skin'] = NULL;
$postedskin = $_POST['skin'];
$myskin = strtolower($this->GetUserName().'.css');
$skintosave = $postedskin; // EMERALDISLAND
$showskin = '<textarea id="skinedit" name="display_css_content" cols="120" rows="16" readonly="readonly"></textarea><br />';
// 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, $wakka){
$css_file = fopen($wakka->GetThemePath().'/css/'.$file, 'w+');
fwrite($css_file, $content);
fclose($css_file);
}
function ReadSkin($file, $wakka){
$source = fopen($wakka->GetThemePath().'/css/'.$file, 'r');
$css_content = fread($source, filesize($wakka->GetThemePath().'/css/'.$file));
fclose($source);
return $css_content;
}
echo $this->Format("=== Select a Wikka skin: === --- ");
if (!isset($_POST['action']))
$_POST['action'] = '';
switch ($_POST['action']) {
case 'Save modified source':
// saves modified skin to file
WriteSkin($skintosave, $_POST["mod_css_content"], $this);
// no break - skin is automatically updated
case 'Set skin':
// change current skin and reload page
if ($this->GetUser()) { $_SESSION['wikiskin'] = $postedskin; } // EMERALDISLAND
else { $this->SetPersistentCookie("wikiskin", $postedskin); }
$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($this->GetThemePath().'/css/'.$myskin)) {
$css_content = ReadSkin($currentskin, $this);
WriteSkin($myskin, $css_content, $this);
$_SESSION['wikiskin'] = $myskin; // EMERALDISLAND
//$this->SetPersistentCookie("wikiskin", $myskin);
$this->Redirect($this->href());
}
break;
case "Create my skin":
// first time user skin creation
if ($this->GetUser() && !file_exists($this->GetThemePath().'/css/'.$myskin)) {
$css_content = ReadSkin($defaultskin, $this);
WriteSkin($myskin, $myskin, $this);
$_SESSION['wikiskin'] = $myskin; // EMERALDISLAND
//$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($this->GetThemePath().'/css/'.$myskin)) {
$css_content = ReadSkin($defaultskin, $this);
WriteSkin($myskin, $css_content, $this);
$_SESSION['wikiskin'] = $myskin; // EMERALDISLAND
//$this->SetPersistentCookie("wikiskin", $myskin);
$this->Redirect($this->href());
}
break;
case "Show source":
// open a readonly textarea with skin source
$css_contents = ReadSkin($currentskin, $this);
$showskin = '<textarea id="skinedit" name="display_css_content" cols="120" rows="16" readonly="readonly">'.$css_contents.'</textarea><br />';
$submit = $hidesource;
break;
case 'Edit source':
// open an editable textarea with skin source
//$css_contents = ReadSkin($currentskin, $this);
$css_contents = ReadSkin($postedskin, $this); // EMERALDISLAND
$skintosave = $postedskin; // EMERALDISLAND
$showskin = '<textarea id="skinedit" name="mod_css_content" cols="120" rows="16">'.$css_contents.'</textarea><br />';
$submit = $savesource.$hidesource;
break;
case 'Hide source':
$this->Redirect($this->href());
break;
}
$handle = opendir($this->GetThemePath().'/css/');
// retrieve skin list
$skinlist = '<select name="skin">';
// put on top of the list the default and custom skin
if (isset($skintosave)) { $skinlist .= '<option value="'.$skintosave.'"'.$skintosave.' selected="selected">(Edited skin: '.$skintosave.')</option>'; }
else { // EMERALDISLAND
$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)) {
if ($this->IsAdmin() || $currentskin == $myskin) {$submit = $editsource;}
else if ($this->GetConfigValue('allow_display_skin_source')) {$submit = $showsource;}
}
// create form
print $this->FormOpen("","","post");
echo '<fieldset>'."\n";
//print $skinlist.$setskin."<br /><br />".$showskin.$submit."<br /><br />\n";
print $skinlist.$setskin."<br /><br />";
if (isset($postedskin)) {print $this->Format(" ---- === Source of ".$postedskin." === --- ");} // EMERALDISLAND *
print $showskin.$submit."<br /><br />\n";
// show user skin options
if ($this->GetUser()) {
if (!file_exists($this->GetThemePath().'/css/'.$myskin)) {
$mysubmit = $createskin;
} else {
$myskinname = "(".$myskin.")";
$mysubmit = ($currentskin == $myskin)? $restoreskin : $importskin.$restoreskin;
}
print $this->Format(" ---- === My skin ".$myskinname." === --- ");
print $mysubmit;
}
// close form
echo "\n".'</fieldset>'."\n";
print $this->FormClose();
closedir($handle);
?>
CategoryWikka - CategoryResource
Except where otherwise noted, content on this site is licensed
under a Creative Commons Attribution - Noncommercial - No Derivative Works 3.0 License.