Event morphing code on EWEA website

This is the code I used to create the Offshore 2013 morphing on EWEA.org leading up to the offshore conference.

<div id="offshore-morph" style="position: absolute; top: 0; left: 0; z-index: 99999;"><a title="Click to register for EWEA OFFSHORE 2013" href="/offshore2013/registration/?ref=morph"><img src="http://ewea.org/uploads/pics/offshore-2013-morphing-corner.png"></a></div>
body {background: url(http://ewea.org/uploads/pics/offshore-2013-morphing.jpg) 100% 0 no-repeat #005596; position: relative;}

You will need to edit the main template file as follows:

[root@main]# cd /var/www/html/sites/www.ewea.org/fileadmin/templates/fluid/layouts
[root@main layouts]# vi Home.html

Working with iptables and dynamic DNS

My new service provider does not assign static IP addresses to its customers so I had find a way around this issue in order to connect to the servers quickly from home.

Because my IP address is constantly changing, and not even within a consistent set of address blocks, I decided to use my personal domain name as the access point.
See here for information on setting up Dynamic DNS.

iptables will detect and resolve the ip address for any hostnames in the configuration file only the first time the service is started, so this is not ideal for a hostname with a changing (dynamic) IP address. This is why we need to use a script that will detect any changes and force iptables to restart.

I found this script below from here and it does everything I need it to do perfectly (change the values highlighted below to suit your own needs):

#!/usr/bin/python
# Version 1.0
# 6 January 2014
# Jason Bickley - Senior Web Manager
# Dynamic DNS script to update IP address for home network

import os

def gettextoutput(cmd):
    """Return (status, output) of executing cmd in a shell."""
    pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
    pipe = os.popen(cmd + ' 2>&1', 'r')
    text = pipe.read()
    if text[-1:] == '\n': text = text[:-1]
    return text

home_dyndns = "home.jasonbickley.net"
log_dyndns  = "/var/log/dyndns.log"
all_dyndns  = "/var/log/dyndns-all.log"

last_dyndns = gettextoutput("cat  " + log_dyndns)
curr_dyndns = gettextoutput("host " + home_dyndns)

print "Log: "+ last_dyndns
print "Cur: "+ curr_dyndns

if last_dyndns == curr_dyndns:
    print "IPs match, no restart necessary"
else:
    print "Updating last IP with current"
    os.system("echo '" + curr_dyndns + "' > " + log_dyndns)
    os.system("echo `date '+%Y%m%d %H:%M:%S'` '" + curr_dyndns + "' >> " + all_dyndns)
    print "Restarting iptables to update it"
    os.system("/etc/init.d/iptables restart")

Here is a summary of what the script above does:

It checks the IP address in the file /var/log/dyndns.log and compares the IP address with my subdomain’s (home.jasonbickley.net).
If the IP addresses match, then it does nothing. If they differ, it overwrites the IP address file with the new value, logs more specific details in /var/log/dyndns-all.log, and restarts the iptables service. This means that iptables now has the updated value for my subdomain.

I put the script here /root/Scripts/dyndns.py, chmod 700 it, and created a cron entry to run it every 10 mins.

Log files are full of “pam_loginuid(crond:session)”

The /var/log/secure file is full of the following lines:

Dec 16 06:12:01 dev crond[10012]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 07:00:01 dev crond[10891]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 07:12:01 dev crond[11133]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 08:00:01 dev crond[12014]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 08:12:01 dev crond[12227]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 09:00:01 dev crond[13136]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 09:12:01 dev crond[13349]: pam_loginuid(crond:session): set_loginuid failed opening loginuid
Dec 16 10:00:01 dev crond[14240]: pam_loginuid(crond:session): set_loginuid failed opening loginuid

We can stop this from happening by editing the following file:

# vi /etc/pam.d/crond

And commenting out the following line:

#
# The PAM configuration file for the cron daemon
#
auth       sufficient pam_env.so
auth       required   pam_rootok.so
auth       include    system-auth
account    required   pam_access.so
account    include    system-auth
#session    required   pam_loginuid.so
session    include    system-auth

Constructing a custom Google Map

For the EWEA 2014 venue map, we implemented a custom Google Map.
The settings we made were to the map position. marker position, marker type and info box:

<html>
<head>
<style type="text/css">
html, body {margin: 0; padding: 0; height: 100%;}
#infoContents         {color: #000; font-family: "Franklin Gothic Medium", sans-serif; font-size: 13px; min-width: 150px; min-height: 100px; padding: 0 20px 0 20px;}
#infoContents h4      {padding: 0; margin: 1em 0 0.5em 0; color: #005596;}
#infoContents p       {margin: 0; padding: 0;}
#infoContents small   {margin: 10px 0 0 0; display: block;}
#infoContents small a {font-size: 10px; font-family: "Franklin Gothic Medium", sans-serif; color: #005596;}
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var spot = new google.maps.LatLng(41.354867,2.138019);
var mapCenter = new google.maps.LatLng(41.355600,2.138019);
var mapOptions = { center: mapCenter, zoom: 17, mapTypeId: google.maps.MapTypeId.SATELLITE };

function initialize() {
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    var iconShadow = new google.maps.MarkerImage(
        "http://www.google.com/mapfiles/ms/micons/msmarker.shadow.png",
        new google.maps.Size(59,32),
        new google.maps.Point(0,0),
        new google.maps.Point(15,32));

    var marker = new google.maps.Marker({
        map: map,
        position: spot,
        icon: "http://www.google.com/mapfiles/markerA.png",
        shadow: iconShadow
    });

    var infowindow = new google.maps.InfoWindow({
        content: '<div id="infoContents"><h4>NORTH ENTRANCE</h4><p>Fira de Barcelona Gran Via<br />Carrer del Foc 31<br />08038 Barcelona, Spain<br /><small><a href="http://goo.gl/maps/GIytH" target="_blank">Go to street view</a></small></p></div>'
    });

    infowindow.open(map, marker);
}

google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map_canvas" style="width: 740px; height: 400px;"></div>
</body>
</html>

This gives a result like this:
GoogleMap

Displaying the contents of a mysql table in a web page using php

Here is a PHP script I made to display the contents of a workshop registered participants table.

<?php
// STATE YOUR VARIABLES
////////////////////////////////////////////////////////////////////
$user = "workshops";
$pass = "FV8sbyJuJ7bdT5Rj";
$db   = "workshops";
$sql  = "SELECT * FROM `wp_workshops_fm_data_15` LIMIT 0, 100";

// ESTABLISH THE CONNECTION
////////////////////////////////////////////////////////////////////
$cxn=mysqli_connect("localhost","$user","$pass","$db");   //Declare the mysqli connection parameters.
$cxn->set_charset("utf8");   // Define the default character set.
if (mysqli_connect_errno())  // Create a check for connection, if nothing then die.
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
$result = mysqli_query($cxn,"$sql");

// GET NUMBER OF ATTENDEES
////////////////////////////////////////////////////////////////////
$rows = mysqli_num_rows($result);
printf("<h2>Current number of registered attendees: %d</h2>",$rows);

// CREATE THE TABLE
////////////////////////////////////////////////////////////////////
echo "<table>
        <tr>
                <th>First name</th>
                <th>Last name</th>
                <th>Job title</th>
                <th>Organisation</th>
                <th>Country</th>
                <th>Email address</th>
        </tr>\n";

while($row = mysqli_fetch_array($result))
  {
  echo "        <tr>\n";
  echo "                <td>" . "<b>" . $row['text-526647958e563'] . "</b>" . "</td>\n";
  echo "                <td>" . "<b>" . $row['text-5266498080c9d'] . "</b>" . "</td>\n";
  echo "                <td>" . $row['text-52664992cbe3f'] . "</td>\n";
  echo "                <td>" . $row['text-526649a164f02'] . "</td>\n";
  echo "                <td>" . $row['text-526649bb26849'] . "</td>\n";
  echo "                <td>" . "<a href=\"mailto:" .$row['text-526649d2a727a'] . "\">" .$row['text-526649d2a727a'] . "</a>" . "</td>\n";
  echo "        </tr>\n";
  }
echo "</table>";

// CLOSE THE CONNECTION
////////////////////////////////////////////////////////////////////
mysqli_close($cxn);

?>

How to turn off YouTube related videos

At the end of a YouTube video, you might see a bunch of “related videos”.

When you embed a video on your website, the YouTube player may end up showing your user related videos that are distasteful or offensive.

YouTube_related

In order to prevent this, we can actually turn off the feature.

All you need to do is add the text “?rel=0” to the URL being viewed or embedded.

<iframe width="640" height="480" src="//www.youtube.com/embed/bDVz4ZCA4a0?rel=0" frameborder="0" allowfullscreen></iframe>

UTF-8 encoded data corrupted in CSV exports

When exporting from PHPMyAdmin as CSV, Excel usually doesn’t know how to deal with the special characters, resulting in corrupted data.

csv_corruption

 

In order to avoid this, we use the following method:

  1. Open a new Excel book
  2. On the “Data” tab, click on the “From Text” button:
    data
  3. Locate your CSV file and click on “Import”
  4. In the dialog box, select the appropriate encoding (i.e. UTF-8) from the dropdown list:
    utf-8
  5. Next, select the delimiter for the CSV (i.e. comma):
    delimit
  6. Then click on Finish (you will be asked where to insert the data – normally [A1] is the place to put it).
  7. Now the data is correctly displayed in Excel:
    csv_fixed

PHP emailing script

This is a tested script for correctly sending emails via php:

<?php
$to      = '[email protected]';
$subject = 'This is the subject';
$message = 'Hello! this is the bottom text';
$headers = 'Content-type: text/html; charset="UTF-8"' . "\r\n" .
'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers,"[email protected]");
?>

Jason’s default .bashrc file

alias mv='mv -i'
alias df='df -h'
alias du='du -sh'
alias vi=vim
alias scp='scp -P 10022'
alias dir='ls -lah'
alias clearall='history -c;clear'
alias cleard='clear;dir'
alias servicerestart='service httpd restart && service mysqld restart && service vsftpd restart'
alias networkrestart='service iptables restart && service network restart'
alias ipconfig=ifconfig
alias version='cat /etc/redhat-release && uname -a'
alias cdh='cd /var/www/html/sites/www.ewea.org'
alias apachify='chown -R apache:apache . ; find ./ -type d -print -exec chmod 775 {} \;; find ./ -type f -print -exec chmod 664 {} \;'
alias send='rsync -avze "ssh -p 10022 -i /root/.ssh/main_prv_key"'
alias wgett='wget --page-requisites'

SQL manipulations for WordPress comments and trackbacks

Disable comments and trackbacks globally:

UPDATE wp_posts SET comment_status = 'closed';
UPDATE wp_posts SET ping_status = 'closed';

Disable comments and trackbacks on post type:

UPDATE wp_posts SET ping_status = 'closed' WHERE post_type = 'post|page';

Disable comments and trackbacks on post status:

UPDATE wp_posts SET ping_status = 'closed' WHERE post_status = 'publish|inherit|pending|private|future|draft|trash' AND post_type = 'post';