PHP GeoIP redirection.

This script uses the Maxmind GeoIP redirection which in this case is hosted by https://freegeoip.net which will return a XML with the details of the visitors IP.


<?php 

//Start session to avoid infinity loops
session_start();
// store session data
$_SESSION['geo']=1;


//Get the client country and set ip that not be redirected
$countriesrow = array('US','CA','AP','AR','BR','CN','CL','CO','CR','GT','HK','JP','KR','MX','SG','TH','TW'); //These are the countries that would be redirected to the normal site
$countrieseuro = array('AT','BE','BG','CH','CZ','DE','DK','EE','EG','ES','EU','FI','FR','GB','GR','HR','HU', 'IE','IL','IN','IS','IT','LU','LV','MA','MC','NL','NO','PL','PT','RO','RS','RU','SE','SI','SK','UA','ZA'); //This will be redirected to the eu version
$admin = '31.53.62.141'; //Put here your IP

//Get the client IP
$ipc =  $_SERVER['REMOTE_ADDR'];
$surl = "https://freegeoip.net/xml/".$ipc;

//Check if freegeoip is available
$response = get_headers($surl);
$response = $response[0];
if ($response == "HTTP/1.1 200 OK") {
	$avl = 1;
} else {
	$avl = 0;
}
if ($avl == 1){
$getxml = trim(file_get_contents($surl)); // get raw post data + trim out the spaces
$xmlsimp = new SimpleXMLElement($getxml);  // convert to object array
$ccode = $xmlsimp->CountryCode;  // returns the country code of the visitor example "US"
}
$country = $ccode;

//$xmlsimp->Ip; // returns the Ip address of the visitor
$gurl = $_SERVER['REQUEST_URI']; //returns the current URL
$parts = explode('/',$gurl);
$cdir = $_SERVER['SERVER_NAME'];
for ($i = 0; $i < count($parts) - 1; $i++) {
 $cdir .= $parts[$i] . "/";
}

$urlParts = explode('.', $_SERVER['HTTP_HOST']);
$subdomain = $urlParts[0]; //or 0 if no www


if ($subdomain !== "eu" && in_array($country, $countrieseuro) && $_SESSION['geo'] == 1 &&  $avl == 1  && ($admin !== $ipc)) 
	{ header('Location: https://eu.rockandpebble.com' . $gurl . '');
	  exit;
	}
elseif ($subdomain !== "eu" && in_array($country, $countrieseuro) && $_SESSION['geo'] == 1 &&  $avl == 1 && ($admin !== $ipc))  
	{ 
		header('Location: https://rockandpebble.com' . $gurl . '');
	  exit;
	}
?>


Check System Load and restart services


#!/bin/sh
# script to check server for extremely high load and restart Apache/Nginx/Varnish/MySQL if the cond$
check=`cat /proc/loadavg | sed 's/\./ /' | awk '{print $1}'`
# define max load avarage when script is triggered
max_load='9'
# log file
high_load_log='/var/log/server_high_load_restart.log';
#
if [ $check -gt "$max_load" ]; then
service nginx restart
sleep 5;
service php5-fpm restart
sleep 5;
service varnish restart
echo "$(date) : Server Restart due to excessive load | $check |" >> $high_load_$
fi



Shell VPS Backup Script

This script uses rsync and ssh keys over ssh to backup remote server with anacron, it should work with cron too, but it haven’t been tested.


#!/bin/sh
#Script for VPS back ups

#This is for the desktop notification on Ubuntu
notify-send -i drive-multidisk Rsync "VPS Backup Started"

#This is for the reallusion VPS
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/home /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/etc /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/var/log /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/var/mail /mnt/DATA-L/backups
rsync -e "ssh -i /home/user/.ssh/id_rsa.2 -p 104" -rtv --delete root@domain.com:/var/backups /mnt/DATA-L/backups

#This is for the desktop notification on Ubuntu
notify-send -i drive-multidisk Rsync "VPS Backup Finished"

Simple Text Sliding

This will slide the according div id.


function ani(){
    $('#text').animate(
        {'margin-left': '1000px'}, 
        10000, // duration
        function(){
            $('#text').css('margin-left','-100px');
            ani();
        });
     $("#text").hover(function(){
    $("#text").stop();
  });    
    $("#text").mouseout(function(){
    ani();
  });
    }
ani();

Theme Options Page

Sample code for theme options page. It can be added to the functions.php file of the theme, or extend it and put in on different file and then include the file in functions.php
Note that the fields are not sanitized, refer to WordPress site for more info.


 
<div id="theme-options-wrap">
<div class="icon32" id="icon-tools"></div>
<h2>Theme Settings</h2>
Update various settings throughout your website.

<form action="options.php" enctype="multipart/form-data" method="post"><!--?php settings_fields('theme_options'); ?--> <!--?php do_settings_sections(__FILE__); ?-->
<p class="submit"><input class="button-primary" type="submit" name="Submit" value="<?php esc_attr_e('Save Changes'); ?>" /></p>

</form></div>
<!--?php <br ?-->
} add_action('admin_init', 'register_and_build_fields');

function register_and_build_fields() {

register_setting('theme_options', 'theme_options', 'validate_setting');

add_settings_section('homepage_settings', 'Homepage Settings', 'section_homepage', __FILE__);

add_settings_section('footer_settings', 'Footer Settings', 'section_footer', __FILE__);

function section_homepage() {

}

function section_footer() {

}

add_settings_field('button1text', 'Button 1 Text', 'button1text_setting', __FILE__, 'homepage_settings');

add_settings_field('button1link', 'Button 1 URL', 'button1link_setting', __FILE__, 'homepage_settings');

add_settings_field('button2text', 'Button 2 Text', 'button2text_setting', __FILE__, 'homepage_settings');

add_settings_field('button2link', 'Button 2 URL', 'button2link_setting', __FILE__, 'homepage_settings');

add_settings_field('button3text', 'Button 3 Text', 'button3text_setting', __FILE__, 'homepage_settings');

add_settings_field('button3link', 'Button 3 URL', 'button3link_setting', __FILE__, 'homepage_settings');

add_settings_field('phonenumber', 'Phone Number', 'phonenumber', __FILE__, 'footer_settings');

add_settings_field('facebookurl', 'Facebook URL', 'facebookurl', __FILE__, 'footer_settings');

add_settings_field('googleurl', 'Google+ URL', 'googleurl', __FILE__, 'footer_settings');

add_settings_field('twitterurl', 'Twitter URL', 'twitterurl', __FILE__, 'footer_settings');

}

function validate_setting($theme_options) {

return $theme_options;

}

function button1text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button1text_setting]" value="{$options[" />";

}

function button1link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button1link_setting]" value="{$options[" />";

}

function button2text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button2text_setting]" value="{$options[" />";

}

function button2link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button2link_setting]" value="{$options[" />";

}

function button3text_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button3text_setting]" value="{$options[" />";

}

function button3link_setting() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[button3link_setting]" value="{$options[" />";

}

function phonenumber() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[phonenumber]" value="{$options[" />";

}

function facebookurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[facebookurl]" value="{$options[" />";

}

function googleurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[googleurl]" value="{$options[" />";

}

function twitterurl() {

$options = get_option('theme_options');

echo "<input type="text" name="theme_options[twitterurl]" value="{$options[" />";

}

add_action('admin_menu', 'theme_options_page');

function theme_options_page() {

add_menu_page('Theme Settings', 'Theme Settings', 'administrator', __FILE__, 'build_options_page', '', '62.2');

}

Then we create a variable with all our settings so we can use it in the template.

<?php $options = get_option('theme_options'); ?>

Then we can use some of the options like this:

<a href="<?php echo $options['button1link_setting']; ?>"><?php echo $options['button1text_setting']; ?></a>

Remove comments

This code snippet will remove the comments from everywhere, just put in the functions.php file in the theme. Here is the code:

<?php
// Removes from admin menu
add_action( 'admin_menu', 'my_remove_admin_menus' );
function my_remove_admin_menus() {
    remove_menu_page( 'edit-comments.php' );
}
// Removes from post and pages
add_action('init', 'remove_comment_support', 100);

function remove_comment_support() {
    remove_post_type_support( 'post', 'comments' );
    remove_post_type_support( 'page', 'comments' );
}
// Removes from admin bar
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );
?>

Shell Script to check system load and send e-mail

This script can check the sytem load with cronjob and send email or even SMS if the load is too high. Anyway I preffer using monit. Tested with ubuntu.

#!/bin/bash
#Load Monitor - sends email when the sytem load reach 5.00, and sms when reach 9.
#first {print$10} check for the last 5 min load, replace with 9 to check for 1 min and 11 for 15 min.

LOAD=`uptime | tr ' ' ' ' | awk '{print$10}' | tr '.' ' ' | awk '{print$1}'`

if [ $LOAD -ge "2" ];
then
    echo "High Load: $LOAD" | mail -s "Load Alert" your@email.com
fi

if [ $LOAD -ge "5" ];
then
    echo "High Load: $LOAD" | mail -s "Load Alert" yourphonenumber@sms.mtel.net
fi

Shell backup script

Script to backup the web dorectory and mysql databases. Tested on ubuntu with cronjob every week.

#!/bin/bash
#=======================================================================================================================
# VPS Backup Script by Ivan Denkov
#=======================================================================================================================
DATE=$(date +"%Y-%m-%d")
MYSQLDUMP="$(which mysqldump)"
TAR="$(which tar)"
PASS="MySQLPSWD"
DIR="/home/backups"
DBS=`mysql -u root -h localhost -p$PASS -e"show databases"`
UPTIME=`uptime`
FREEHD=`df -h`

#This will remove files older than 35 days
find $DIR/* -mtime +35 -exec rm -rf {} \;

$TAR -cvf $DIR/Backup-$DATE.tar /var/www
for DATABASE in $DBS
do
if [ $DATABASE != "Database" ]; then
FILENAME=$DATABASE
$MYSQLDUMP -u root -h localhost -p$PASS $DATABASE > $DIR/$FILENAME.sql
$TAR -rvf $DIR/Backup-$DATE.tar $DIR/$FILENAME.sql
fi
done

#Remove dumped .sql in the folder
rm -rf $DIR/*.sql

sleep 40

#send me e-mail when done
echo "Email Body - Back Up completed at date $DATE, or maybe not!? Let see the statistic \n\n $UPTIME \n\n  $FREEHD" | mail -s "Email Subject - Back UP $DATE" your@email.com

Kill and Logout users in pts/* Linux

As a  Linux administrator you may need to force logout and kill a specific user, or an active user in pts/0 pts/1 pts/3 etc. Also this tutorial will work in most linux distros.
First of all display the out put of “w” command.

[root@server ~#]w
18:08:30 up 3:54, 3 users, load average: 0.05, 0.02, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.10.100 14:15 0.00s 0.05s 0.01s w
james pts/1 192.168.10.100 18:07 50.00s 0.02s 0.02s -bash
james pts/2 10.10.20.23 18:07 39.00s 0.02s 0.02s -bash

Here the user “james” is logged in from two different machines. And you need to force logout and kill the user “james” logged in from ip: 192.168.10.100 (pts/1)

use the below command

[root@server ~#]skill -KILL -v pts/1

This command will force logout and kill the user in pts/1. and the same user logged in pts/2 will not be logged out.

If need to kill a users all the connected sessions at once

[root@server ~#]skill -KILL -u james

(this will kill both pts/1 and pts/2 cessions)

To STOP/PAUSE a user’s activities

[root@server ~#]skill -STOP -u james

To RESUME a stopped user

[root@server ~#]skill -CONT -u james

Edit

There is some bug in recent Debian(7) and distros based on them like Ubuntu, which will make the command

skill -KILL -v pts/1

do nothing

What you can do in such case is to get the PID of the terminal you want to kill with

 skill -i -t pts/1

it will return something like

pts/1    root     27933 bash               ?

Just kill it –

kill 27933