Archive for the ‘ Coding ’ Category

Find original process in Linux

When you need to find the original thread in a list of a hundred child threads, you can use “ps auxc | grep

[root@main www.ewea.org]# ps auxc | grep httpd
root      1989  0.0  0.2 319260 12916 ?        Ss   09:24   0:01 httpd
apache    6341  1.4  1.1 483420 69864 ?        S    13:18   3:10 httpd
apache   12844  1.5  1.1 472324 68452 ?        S    14:16   2:32 httpd
apache   12854  1.5  1.0 466824 62652 ?        S    14:16   2:26 httpd
apache   22137  1.6  1.0 465680 61540 ?        S    15:30   1:25 httpd
apache   22145  1.5  1.0 466868 61920 ?        S    15:30   1:17 httpd
apache   22501  1.5  1.0 466620 62464 ?        S    15:32   1:19 httpd
apache   22508  1.5  1.0 369720 62528 ?        S    15:32   1:18 httpd
apache   22509  1.5  1.1 480484 67440 ?        S    15:32   1:17 httpd

Exhibitor Press Release Uploads

There are three components to the system:

 

A) The exhibitor upload page

The first thing we need to do is build the form using the “WordPress Form Manager” plugin.
We create a form with the following fields: “Company name”, “Document title” and “Select document”. We also add a note field to display additional information about accepted file formats and file size, etc. Pay attention to the upload document section as to restrict the document types.

We also add a “Private Field” at the bottom of the “Form extra” tab. This is what our administrator will use to approve each press release.

Once you have created the form, add it to the exhibitor upload page with the following shortcode format:
[form form-XYZ]

 

B) The public listing page

We include a simple PHP script into the WordPress page to pull data off the database. The code I used can be obtained here.

To summarise, the script will need to be modified in the following places:

  1. Database name
  2. Form table
  3. User (an account with read-only access just to the form table was created for this application)
  4. Password
  5. The blob variables in the loop

 

C) Administration page

The administrator logs into WordPress and navigates to the Submission Data tab of the form being used.
Using the checkboxes and the “Edit selected” function from the dropdown list of actions, the admin can edit…

form-edit

and approve each press release.

form-approve

CSS box shadow

Here are some lesser-know tricks for CSS dropshadows.
http://css-tricks.com/snippets/css/css-box-shadow/

You can create the following styles:

Using the following code:

<style type="text/css">
#example {
background: none repeat scroll 0 0 #ccc;
box-shadow: 5px 5px 5px #666;
height: 100px;
margin: 15px 0;
width: 100px;
}
#example2 {
background: none repeat scroll 0 0 #ccc;
box-shadow: 0 0 10px #000000 inset;
height: 100px;
margin: 15px 0;
width: 100px;
}
#example3 {
background: none repeat scroll 0 0 #ccc;
box-shadow: 0 8px 6px -6px black;
height: 100px;
margin: 15px 0;
width: 100px;
}
</style>

Code snippet for Offshore 2013 morph

Here is some code I wrote to add a clickable triangle, and a custom background, to the ewea.org website to highlight the offshore event.

The HTML:

<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://www.ewea.org/uploads/pics/offshore-2013-morphing-corner.png"></a></div>

And some CSS:

body {background: url(http://ewea.org/uploads/pics/offshore-2013-morphing.jpg) 100% 0 no-repeat #005596; position: relative;}

Downgrading PHP

I installed PHP on a Centos 6.5 i686 server recently and by default in installed PHP 5.5 which broke some apps.
You can easily downgrade PHP with the following command:

# yum downgrade php*

(Repeat the process again if you want to downgrade a version more)

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.

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>