[WordPress] filter out unneeded menu classes

March 19th, 2013 No comments

Here’s a code snippet for WordPress to filter out unneeded menu classes. Put the following line in your functions.php

// Reduce nav classes, leaving only 'current-menu-item'
function nav_class_filter($var)
{
    return is_array($var) ? array_intersect($var, array(
        'menu',
        'menu-main',
        'menu-primary',
        'menu-item',
        'sub-menu',
        'menu-last-item',
        'menu-first-item',
        'menu-noparent',
        'menu-parent',
        'menu-top',
        'current-menu-item'
    )) : '';
}
add_filter('nav_menu_css_class', 'nav_class_filter', 100, 1);
add_filter('nav_menu_item_id', 'my_css_attributes_filter', 100, 1);
Categories: Uncategorized Tags: ,

[WordPress] set cookie

March 19th, 2013 No comments

Here’s a code snippet for WordPress. Since WordPress doesn’t support any sessions a cookie might be useful. Here’s a code snippet to use for your functions.php

//Set cookie
function set_newuser_cookie() {
	if (!isset($_COOKIE['sitename_newvisitor'])) {
		setcookie('sitename_newvisitor', 1, time()+1209600, COOKIEPATH, COOKIE_DOMAIN, false);
	}
}
add_action( 'init', 'set_newuser_cookie');
Categories: Uncategorized Tags:

[WordPress & Genesis] Add parent and child classses to menu

March 19th, 2013 No comments

Here’s another code snippet for WordPress Genesis Framework.

If you would like to add menu classes to parent and child menu’s, use the code below.
Put this in the functions.php file:


// Function to add parent and child classses to menu
class Arrow_Walker_Nav_Menu extends Walker_Nav_Menu
{
    function display_element($element, &$children_elements, $max_depth, $depth = 0, $args, &$output)
    {
        $id_field = $this->db_fields['id'];
        if (0 == $depth) {
            $element->classes[] = 'menu-top'; //top main menu
            if (empty($children_elements[$element->$id_field])) {
                $element->classes[] = 'menu-noparent'; //no childs
            }
        }
        if (!empty($children_elements[$element->$id_field])) {
            $element->classes[] = 'menu-parent'; //child in menu
        }
        Walker_Nav_Menu::display_element($element, $children_elements, $max_depth, $depth, $args, $output);
    }
}
Categories: Uncategorized Tags: ,

[WordPress & Genesis] Add menu classes to first en last menu items

March 19th, 2013 No comments

Here’s a code snippet for WordPress Genesis Framework.

If you would like to add menu classes to first en last menu items, use the code below. Put this in the functions.php file:

// Function to add menu classes to first en last menu items
function add_first_and_last($items)
{
    $items[1]->classes[]             = 'menu-first-item';
    $items[count($items)]->classes[] = 'menu-last-item';
    return $items;
}
add_filter('wp_nav_menu_objects', 'add_first_and_last');
Categories: Uncategorized Tags: ,

[WordPress & Genesis] custom viewport

March 19th, 2013 No comments

Here’s a code snippet for WordPress Genesis Framework.

If you would like to use a custom viewport for mobile devices, for example, use the code below:

/** Add Viewport meta tag for mobile browsers */
add_action('genesis_meta', 'add_viewport_meta_tag');
function add_viewport_meta_tag()
{
    echo '<meta name="viewport" content="width=1020">';
}
Categories: Uncategorized Tags: ,

[WordPress & Genesis] custom footer or custom header

March 19th, 2013 No comments

Here’s a code snippet for WordPress Genesis. If you would like to use a custom header or custom footer, don’t use any header.php of footer.php. Use the code snippet below:

// Include Header, seperate from functions
include 'custom-header.php';

// Include Footer, seperate from functions
include 'custom-footer.php';
Categories: Uncategorized Tags: ,

[Synology] How to secure photostation with htaccess

March 15th, 2013 No comments

Here’s a short instruction on how to protect your synology photostation by using htaccess:

Create the following file: /volume1/@appstore/PhotoStation/photo/.htaccess

AuthName "Restricted Area"
AuthType Basic
AuthUserFile /volume1/@appstore/PhotoStation/photo/.htpasswd
AuthGroupFile /dev/null
require valid-user

and the following file: /volume1/@appstore/PhotoStation/photo/.passwd

admin:xxxxxxxxxxxxxxxx

Use the passwd htaccess online generate for your own password.

Categories: Uncategorized Tags:

Synology: Monitoring Apache with mod_status

Here another quick manual. If you would like to monitor your apache webserver, you can do that with mod_status. Open the httpd.conf-user file:

pico /usr/syno/apache/conf/httpd.conf-user

Copy paste the following content from below:

<Location /server-status>
   SetHandler server-status
   Order Deny,Allow
   Deny from all
   Allow from all
</Location>

Save the httpd.conf-user file and restart apache with:

/usr/syno/etc/rc.d/S97apache-user.sh restart

You can now obtain the apache server status by querying the following url:

http://diskstation/server-status?auto

This might be useful for people working with cacti.

Categories: Uncategorized Tags: , ,

Synology: Run sabnzbd behind apache

Here’s a quick instruction for those who would like to run sabnzbd behind apache on a synology nas system. Open up a ssh connection, create the following file:

nano /usr/syno/etc/sites-enabled-user/sabnzbd.conf

Copy paste the contents below to this file. Please note that my sabnzbd port is 9090. If you would like to change this, change the config file below.

# Put this after the other LoadModule directives
LoadModule proxy_module /usr/syno/apache/modules/mod_proxy.so
LoadModule proxy_http_module /usr/syno/apache/modules/mod_proxy_http.so

<Location /sabnzbd>
order deny,allow
deny from all
allow from all
ProxyPass http://localhost:9090/sabnzbd
ProxyPassReverse http://localhost:9090/sabnzbd
</Location>

Save the file by using control-x. Restart apache with the following command:

/usr/syno/etc/rc.d/S97apache-user.sh restart

Restore / Extract Plesk 9.5.4 backup

December 24th, 2012 No comments

If you want to do a manual restore of your Plesk 9.5.4 backup you should use the following commando:

cat plesk-backup_1205270308.tar* | tar xvf -

The domains, vhosts and databases are found in the clients, domains and resellers folders.

Categories: Plesk Tags: ,

updatedb on Synology

December 19th, 2012 No comments

If you would like to index your Synology file system you should install the mlocate package from optware

Install then the  mlocate package with the following command:

ipkg install mlocate

To update your filesystem use:

updatedb

To then search use the locate command.

Categories: Linux Tags: ,

Useful rc.local script on synology

Here’s a useful rc.local script which restores the optware (ipkg) functionality after an upgrade:

#!/bin/sh

#optware fixes http://forum.synology.com/enu/viewtopic.php?f=189&amp;t=46095
mount -o bind /volume1/@optware /opt

# remount with noatime
mount -o remount,noatime /
mount -o remount,noatime /volume1

# enable debug spindown
syno_hibernate_debug_tool --enable 10

# Optware setup
[ -x /etc/rc.optware ] && /etc/rc.optware start

# configure ssh to run on 443
if ! grep "Port 443" /etc/ssh/sshd_config ; then
sed -i "s/Port 22/#Port 22/g" /etc/ssh/sshd_config
echo 'Port 443' &gt;&gt; /etc/ssh/sshd_config
/usr/syno/etc.defaults/rc.d/S95sshd.sh restart
fi

# restore /opt/bin path
if ! grep "/opt/bin" /root/.profile ; then
sed -i "s/PATH=/PATH=\/opt\/bin:/g" /root/.profile
fi

exit 0
Categories: Uncategorized Tags:

Howto to authenticate with apache against the /etc/passwd file

I used the following files to let apache authenticate against the local /etc/passwd file.

The perl-pass.nl is used to generate a file based on the /etc/passwd and shadow file. You can place this in the crontab for example. Only the lower id’s are used, so root won’t be in the list.

The index.php reads from the passwd.html file generated by the perl script and will redirect the users to welcome.php when a correct password and username are used.

perl-pass.pl

#!/usr/bin/perl
#

open(PASSWD,"/etc/passwd");
open(SHADOW,"/etc/shadow");
open(FLATFILE,">/var/www/passwd.httpd");
while(<SHADOW>){
        chop;
        ($uname,$temppass)=split(/:/);
        $pass{$uname}=$temppass;
}
while(<PASSWD>){
        chop;
        ($uname,$temppass,$uid,$gid,$fn,$homedir,$shell)=split(/:/);
        if ($temppass ne 'x'){ $pass{$uname}=$temppass; }
        if ($uid>=500) {
                print FLATFILE
"$uname:$pass{$uname}:$uid:$gid:$fn:$homedir:$shell\n";
        }
}

close(PASSWD);
close(SHADOW);
close(FLATFILE);
chmod(0644,"/var/www/passwd.httpd");
chown(65534,65534,"/var/www/passwd.httpd");

index.php

<? session_start() ?>

<html>
<body>
<p align="center"><br><br>
<br><br><b>Welcome to Server Manager</b>
</p>
<table align="center" border="0" cellpadding="0" cellspacing="0" bordercolor="#CCCCCC">
<tr><td>
<form method="POST" action="index.php">
  Username: </td><td>
  <input type="text" name="username" size="19"></td></tr>
  <br>
  <tr><td>Password: &nbsp;</td><td>
  <input type="password" name="password" size="19"></td></tr>
  <tr><td colspan="2" align="center">
    <br><input type="submit" value="Login">
</form>
</td></tr>
</table>

</body>
</html>

<?
// Var
$user = ($_POST['username']);
$pass = ($_POST['password']);
$autharray = file("/var/www/passwd.httpd"); 

// Check passwd
for ($x = 0; $x < count($autharray); $x++)
  {
   if (eregi("^$user:", $autharray[$x])) {
       $passwd = explode(":", $autharray[$x]);
       $salt =  substr($passwd[1],0,11);
       $cryptpw = crypt($pass,$salt);
       if ($cryptpw == $passwd[1]) {
		// Succes
             	echo "<br><p align='center'>Username found on</p>";
		echo "<br><p align='center'>Starting connection: $_SESSION[user]...   Moment Please.....</p>";
		$_SESSION['user'] 		= $user;
		$_SESSION['server'] 		= $servername;
		if ( $_SESSION['user'] == "admin" ) { 
			echo" <META HTTP-EQUIV='Refresh' CONTENT='1; URL=admin_overview.php'> ";
		} else {
			echo" <META HTTP-EQUIV='Refresh' CONTENT='1; URL=welcome.php'> "; 
		}
            } else {
			
		// Error on password
              	echo "<br><p align='center'>Wrong password or username!</p>";
	      }
     	}
}
?>

welcome.php

<? session_start() ?>

<?

if ( $_SESSION[user] == "" ) { 
   echo "No Access!"; 
} else {
   echo "Welcome $_SESSION['user']</b><br><br>";
}

?>
Categories: Uncategorized Tags:

Stop Synology index, thumb and postgresql services

If your synology NAS is slowing down your speed, you might want to give the following commands a try:

/usr/syno/etc/rc.d/S77synomkthumbd.sh stop
/usr/syno/etc/rc.d/S66synoindexd.sh stop
/usr/syno/etc/rc.d/S20pgsql.sh stop
Categories: Uncategorized Tags: , ,

Hyper-V backup with volume shadow

April 16th, 2012 No comments

As promised; Here’s my Hyper-v volume schadow backup script. I coded this a while ago.

It pauses the hyper-v and creates a virtual shadow. The virtual machine will be started while the backup is running. This results in a minimum downtime. Useful for small production or test environments.

backup.bat

diskshadow -s shadowcreate.txt

shadowcreate.txt

#DiskShadow script file
delete shadows all
list providers
set context persistent
set metadata D:\MetaData.cab
set verbose on

#Create network share
EXEC vmnetwork.cmd

#Write save state files to harddisk before start backup
EXEC vmsavestate.cmd

begin backup

add volume d: alias DataVolumeShadow

#verify the "Microsoft Hyper-V VSS Writer" writer will be included in the snapshot
#writer verify {66841cd4-6ded-4f4b-8f17-fd23f8ddc3de}
create

expose %DataVolumeShadow% q:

#Start Virtual Machine
EXEC vmstart.cmd

#copy all files to d:\backup
EXEC vmcopy.cmd

unexpose q:
end backup
delete shadows all

#End of script

vmcopy.vbs

'Usage: cscript //NoLogo vmcopy.vbs D:\Backup\
'todo: mailfunctie
'todo: meer logging

Dim ArgObj, backuppath, count
Set ArgObj = WScript.Arguments
backuppath = ArgObj(0)

On Error Resume Next

Set objFS = CreateObject("Scripting.FileSystemObject")

Dim iSpc
Set objWMIService = GetObject("winmgmts:")
Set objLogicalDisk = objWMIService.Get("Win32_LogicalDisk.DeviceID='X:'")
iSpc = objLogicalDisk.FreeSpace/1024/1024
iSpc = FormatNumber(iSpc,0)
Wscript.Echo "Free space on Harddisk: " &amp; iSpc &amp; "GB"

Dim Directory
Set Directory = objFS.GetFolder("Q:\Hyper-V")
Dim Dsize
Dsize = Directory.Size/1024/1024
Dsize = FormatNumber(Dsize,0)
Wscript.Echo "Folder size to back-up: " &amp; Dsize &amp; "GB"

Dim BackupLocation
Set BackupLocation = objFS.GetFolder(backuppath)
Dim Tel
Dim Minimum
Minimum = Dsize + 30*1024
Minimum = FormatNumber(Minimum,0)
Wscript.Echo "Folder size needed: " &amp; Minimum &amp; "GB"

Wscript.Echo "Amount of Backup* directories found is: " &amp; ArgObj(1)
count = ArgObj(1)

Wscript.Echo "Looking for " &amp; backuppath
If objFS.FolderExists(backuppath) then
Wscript.Echo "No need to create " &amp; backuppath
Else
objFS.CreateFolder(backuppath)
Wscript.Echo "Path not found.... Creating: " &amp; backuppath
End If
WScript.Sleep 200

If objFS.FolderExists(backuppath &amp; "Backup0" &amp; count) then
Wscript.Echo "Delete folder: " &amp; backuppath &amp; "Backup0" &amp; count
objFS.DeleteFolder(backuppath &amp; "Backup0" &amp; count)
WScript.Sleep 200
Else
Wscript.Echo "Folder not found.... " &amp; backuppath &amp; "Backup0" &amp; count
WScript.Sleep 200
End If

Do While count &gt; 1

If objFS.FolderExists(backuppath &amp; "Backup0" &amp; count - 1) then
Wscript.Echo "Rename folder: " &amp; backuppath &amp; "Backup0" &amp; count - 1 &amp; " to Backup0" &amp; count
objFS.MoveFolder backuppath &amp; "Backup0" &amp; count - 1 , backuppath &amp; "Backup0" &amp; count
WScript.Sleep 200
Else
Wscript.Echo "Path not found.... " &amp; backuppath &amp; "Backup0" &amp; count -1
WScript.Sleep 200
End If

count = count - 1

Loop

Wscript.Echo "Create folder: " &amp; backuppath &amp; "Backup0" &amp; count
objFS.CreateFolder(backuppath &amp; "Backup0" &amp; count)
WScript.Sleep 200

Wscript.Echo "Copying files to destination folder"
objFS.CopyFolder "Q:\Hyper-V", backuppath &amp; "Backup01\"
Wscript.Echo "Move MetaData.cab to destination folder"
objFS.MoveFile "D:\MetaData.cab", backuppath &amp; "Backup01\"

Wscript.Echo "Back-up done!"

vmsavestate.vbs

Option Explicit
Dim WMIService
Dim VMList
Dim VMName
Dim ArgObj
Set ArgObj = WScript.Arguments

'Specify the name of the virtual machine that I want to start
VMName = ArgObj(0)

'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

'Get all the MSVM_ComputerSystem object
Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" &amp; VMName &amp; "'")

WScript.Echo "============================================="
WScript.Echo "VM Name: " &amp; VMList.ItemIndex(0).ElementName
WScript.Echo "VM GUID: " &amp; VMList.ItemIndex(0).Name
WScript.Echo "VM State: " &amp; VMList.ItemIndex(0).EnabledState
WScript.Echo "============================================="

If VMList.ItemIndex(0).EnabledState = 3 Then
WScript.Echo "Virtual Machine is turned off, no need for saving"
Else

'Request a state change on the first VM that is returned
'2 = start, 3 = stop and 32769 = save state
VMList.ItemIndex(0).RequestStateChange(32769)

'Wait till save state is done
while not VMList.ItemIndex(0).EnabledState = 32769
Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" &amp; VMName &amp; "'")
WScript.Echo "VM State: " &amp; VMList.ItemIndex(0).EnabledState
WScript.Sleep 1500
wend
WScript.Echo "============================================="
Wscript.Echo "Save State-Succeeded!"
WScript.Echo "============================================="

End If

vmstart.vbs

Option Explicit
Dim WMIService
Dim VMList
Dim VMName
Dim ArgObj
Set ArgObj = WScript.Arguments

'Specify the name of the virtual machine that I want to start
VMName = ArgObj(0)

'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization")

'Get all the MSVM_ComputerSystem object
Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" &amp; VMName &amp; "'")

WScript.Echo "============================================="
WScript.Echo "VM Name: " &amp; VMList.ItemIndex(0).ElementName
WScript.Echo "VM GUID: " &amp; VMList.ItemIndex(0).Name
WScript.Echo "VM State: " &amp; VMList.ItemIndex(0).EnabledState
WScript.Echo "============================================="

If VMList.ItemIndex(0).EnabledState = 3 Then
WScript.Echo "Virtual Machine is turned off, no need to start it"
Else

'Request a state change on the first VM that is returned
'2 = start, 3 = stop and 32769 = save state
VMList.ItemIndex(0).RequestStateChange(2)

'Wait till save state is done
while not VMList.ItemIndex(0).EnabledState = 2
Set VMList = WMIService.ExecQuery("SELECT * FROM Msvm_ComputerSystem WHERE ElementName='" &amp; VMName &amp; "'")
WScript.Echo "VM State: " &amp; VMList.ItemIndex(0).EnabledState
WScript.Sleep 1500
wend
WScript.Echo "============================================="
Wscript.Echo "Start-Succeeded!"
WScript.Echo "============================================="

End If