Friday, July 6, 2012

SSL in Wamp Server

SSL Encryption can be added to your Wamp Server package to make it more secure as well as to test the SSL environment before taking your application to production. 

Note 1: Example given in case the folder in which apache is installed is apache2.2.6  where 2.2.6 is the version of apache installed. In case of another version being installed, say apache 2.2.22, this folder would change to apache2.2.22

Note 2: the foldername appName used below should be changed to whatever is the foldername where your application is placed which should be served to users connecting via https://

1. Create SSL Certificate and Key

Open the DOS command window and change directory to bin directory of wamp apache directory by using the DOS command

cd c:\wamp\bin\apache\apache2.2.6\bin

Now the DOS prompt should look like: C:\wamp\bin\apache\apache2.2.6\bin>

To create a server key with 1024 bits encryption, enter this command:

openssl genrsa -des3 -out server.key 1024

It’ll ask you a pass phrase, enter a passphrase of your choice which you will remember

Remove the pass phrase from the RSA private key (while keeping a backup copy of the original file). Enter these commands:

copy server.key server.key.org
openssl rsa -in server.key.org -out server.key

It’ll ask you the pass phrase you entered earlier, enter it

Create a self-signed Certificate (X509 structure) with the RSA key you just created. Enter the command:

openssl req -new -x509 -nodes -sha1 -days 365 -key server.key -out server.crt -config C:\wamp\bin\apache\apache2.2.6\conf\openssl.cnf


You'll be asked to fill in information after entering this command.

2. Copy the server.key and server.crt files


In the conf folder of apache2.2.6 folder, create two folders named as ssl.key and ssl.crt

Copy the server.key file to ssl.key folder and server.crt file to ssl.crt

3. Edit the httpd.conf file and php.ini


In httpd.conf file, remove the comment definer # at the line which says:
LoadModule ssl_module modules/mod_ssl.so

In httpd.conf, remove the comment definer # at the line which says:
Include conf/extra/httpd_ssl.conf

Then move that line after this block <IfModule ssl_module>…. </IfModule>

Open the php.ini file located in apache2.2.6/bin folder, remove the comment definer; at the line which says:
extension=php_openssl.dll

4. Edit the httpd-ssl.conf file


The httpd-ssl.conf file is located at C:\wamp\bin\apache\Apache2.2.6\conf\extra

This should be edited as follows

Find the line which says "SSLMutex …." and change it to SSLMutex default without quotes

Find the line which says: <VirtualHost _default_:443>.
Right after it, change the line which says DocumentRoot … to DocumentRoot "C:/wamp/www/appname" with quotes.  
Change the line "ErrorLog…." to Errorlog C:/wamp/logs/sslerror.log without quotes
Change the line "TransferLog …." to TransferLog C:/wamp/logs/sslaccess.log without quotes

Specify the SSL crt file:  Change the line "SSLCertificateFile …." to SSLCertificateFile "C:/wamp/bin/apache/apache2.2.6/conf/ssl.crt/server.crt"

Specify the SSL key file: Change the line "SSLCertificateKeyFile …." to SSLCertificateKeyFile " C:/wamp/bin/apache/apache2.2.6/conf/ssl.key/server.key"

Change the line which says <Directory "C:/Program Files/Apache Software Foundation/Apache2.2/cgi-bin"> to <Directory "C:/wamp/www/">
and add the following lines inside those <Directory … >…</Directory> tags:

Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all

Make sure the line CustomLog "C:/wamp/logs/ssl_request.log" \  is uncommented (remove the #).

In the DOS Command window, enter httpd -t . If it displays Syntax is OK, then proceed, else you have made a syntactical error and will need to troubleshoot

Go to C:/wamp/logs and create the following 3  files
                                i.            sslerror.log
                              ii.            ssl_request.log
                             iii.            sslaccess.log

5. Starting the HTTPS Server and Putting it Online


Restart the Apache server

If restart is successful, then open the browser and enter https://localhost

If you want to allow put your HTTPS secure server online then in the httpd_ssl.conf file, change the line which says "ServerName localhost:443" to "ServerName www.yourwebsitename.com:443" or  "ServerName X.Y.Z.A:443" without quotes where yourwebsitename is your registered internet domain name and/or  X.Y.Z.A is your WAN IP Address

The DocumentRoot you modified in Step 4 points to the correct website folder on your server.

If your server is connected to a router, setup the router to allow port 443 forwarding to your server.

If your server has a firewall enabled or behind a network firewall, set up the firewall to allow incoming port 443 connection

Monday, June 18, 2012

PHP to handle inputs copy pasted from MS Word

A common problem faced is that the user wants to copy & paste their article or part of it from Microsoft Word, directly into a textarea on a page. The problem is word uses non UTF-8 characters. Once the page is submitted, PHP gets it and the characters are encoded differently and they display weirdly when displayed back via an echo. 

A Commonly available solution for this is to create a function which parses the input and cleans it up either by removing the non standard characters or converting them into standard UTF-8 characters

Here is an example of one such

function fWordCharacterConverter($str)
{
$invalid = array('Š'=>'S', 'š'=>'s', 'Ð'=>'Dj', 'd'=>'dj', 'Ž'=>'Z', 'ž'=>'z',
'C'=>'C', 'c'=>'c', 'C'=>'C', 'c'=>'c', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A',
'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E',
'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y',
'Þ'=>'B', 'ß'=>'Ss', 'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a',
'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e',  'ë'=>'e', 'ì'=>'i', 'í'=>'i',
'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o',
'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y',  'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'R'=>'R', 'r'=>'r', "`" => "'", "´" => "'", "„" => ",", "`" => "'",
"´" => "'", "“" => "\"", "”" => "\"", "´" => "'", "&acirc;€™" => "'", "{" => "",
"~" => "", "–" => "-", "’" => "'");

$str = str_replace(array_keys($invalid), array_values($invalid), $str);

return $str;
}


Another solution which is much less known but works well is to use the php function iconv

$str= iconv('UTF-8', 'ASCII//TRANSLIT', $str);    

Details from the PHP Manual:


string iconv ( string $in_charset , string $out_charset , string $str )
Performs a character set conversion on the string str from in_charset to out_charset

Parameters

in_charset
The input charset.
out_charset
The output charset.
If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character and an E_NOTICE is generated.
str
The string to be converted.

Return Values

Returns the converted string or FALSE on failure.


Saturday, May 5, 2012

array_pop and array_push in PHP


array_push  Push one or more elements onto the end of array

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed.

Has the same effect as:

<?php
$array[] = $var;
?>
repeated for each var.





array_pop  Pop the element off the end of array

array_pop() pops and returns the last value of the array, shortening the array by one element. If array is empty (or is not an array), NULL will be returned. Will additionally produce a Warning when called on a non-array.

Note: This function will reset() the array pointer of the input array after use.


 


Saturday, March 17, 2012

Redirecting based on the URL string using PHP

In many cases, one ends up hosting multiple sites on a single server, shared or VPS.

Also in many other cases a company has multiple domains and multiple page addresses to service clients and prospects from different countries, demographics and walks of life.

And the need of the hour is that one wants to send a user to a different folder on your web site, based on what domain they use? Here we will explain a simple way to do this using PHP.

For instance on my VPS, I have 2 domains pointing: one domain called sampleno1.com and the other one called sampleno2.com. Both point to the IP of the VPS.
We have an index.php file in the root of the web server on the VPS containing the following code.

<?php

if (($_SERVER['HTTP_HOST'] == “sampleno1.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno1.com”))
{
    header("location: folderforsampleno1");
}

if (($_SERVER['HTTP_HOST'] == “sampleno2.com”) || ($_SERVER['HTTP_HOST'] == “www.sampleno2.com”))
{
    header("location: folderforsampleno2");
}

?>


The code asks the PHP server to see what the HTTP_HOST header is. If it is sampleno1.com or www.sampleno1.com, then it redirects to the folder on the server which contains the code for the site sampleno1.

If it is sampleno2.com or www.sampleno2.com, then it redirects to the folder on the server which contains the code for the site sampleno2

Saturday, March 3, 2012

Find a File containing a specific text string in Ubuntu

Find a File containing a specific text string in Ubuntu can be done using the grep command.

The format is

grep “text string to search” directory-path

Some Examples

Simple String Search in a directory:
Search for a string called "star wars" in all text files located in /home/movies/*.txt directory, use

$ grep "star wars" /home/movies/*.txt

Search for a string in all files in all contained folders

For this, you can search for a text string all files under each directory, recursively with -r option:

$ grep -r "star wars" /home/movies/

Monday, February 6, 2012

Determine file size with du

The du command ( disk usage ) gather and summarize about how much your disk space being used by your file and the disk space being use by directory in the Linux system The du command can be use to find the size of file and the size of directory in Linux system.

du takes a single argument, specifying a pathname for du to work; if it is not specified, the current directory is used. The SUS mandates for du the following options:
-a, display an entry for each file (and not directory) contained in the current directory
-H, calculate disk usage for link references specified on the command line
-k, show sizes as multiples of 1024 bytes, not 512-byte
-L, calculate disk usage for link references anywhere
-s, report only the sum of the usage in the current directory, not for each file
-x, only traverse files and directories on the device on which the pathname argument is specified.
du is flexible and offers a variety of options. For example, it allows you to determine the size of the current directory and all subdirectories:
$ du -sh

Using du by itself displays the size of every subdirectory under the current directory--it summarizes and reports the data in human-readable format, such as 402 MB instead of 411012 bytes. You can also see the size of every file in every subdirectory by incorporating the "-a" option:
$ du -ah

In addition, you can use du in combination with grep to find a particular file size. If you're searching in a directory and suspect a file is growing extremely large, reduce the output of du to all files and directories 1 GB in size or larger with this command:
$ du -ah|grep -e '[0-9]G'

Examples 

Sum of directories in kilobytes:

 $ du -sk *
 152304  directoryOne
 1856548 directoryTwo
Sum of directories in human-readable format (Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and Petabyte):
 $ du -sh *
 149M directoryOne
 1.8G directoryTwo
disk usage of all subdirectories and files including hidden files within the current directory (sorted by filesize) :
 $ du -sk .[!.]* *| sort -n
disk usage of all subdirectories and files including hidden files within the current directory (sorted by reverse filesize) :
 $ du -sk .[!.]* *| sort -nr
The weight of directories:
 $ du -d 1 -c -h

More information on du command:
# info du
# man du
# du --help

Thursday, February 2, 2012

node-static for Serving Static Files with Node.js

node-static can be installed via npm with the following command:
npm install node-static
To use it: include the module in your script via the require function, create a server, pass it your preferred options, and tell it to serve files:
var static = require('node-static'),
  http = require('http'),
  util = require('util');
var webroot = './public',
  port = 8181;
var file = new(static.Server)(webroot, {
  cache: 900,
  headers: { 'X-Powered-By': 'node-static' }
});
http.createServer(function(req, res) {
  req.addListener('end', function() {
    file.serve(req, res, function(err, result) {
      if (err) {
        console.error('Error serving %s - %s', req.url, err.message);
        if (err.status === 404 || err.status === 500) {
          file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res);
        } else {
          res.writeHead(err.status, err.headers);
          res.end();
        }
      } else {
        console.log('%s - %s', req.url, res.message);
      }
    });
  });
}).listen(port);
console.log('node-static running at http://localhost:%d', port);
Here, when we create a new instance of a node-static server, we pass it:
  • the directory we want it to serve files from by way of the `webroot` variable (defaults to serving files from the current directory unless you tell it otherwise)
  • a cache value of 900, so each file served will be cached for 15 minutes (default value for cache is 3600, meaning files are cached for 1 hour)
  • a custom ‘X-Powered-By’ header (I’ve used X-Powered-By solely as an example – you may, or may not, want to disclose the software you’re server is running)
We then use the http module’s createServer function, and add an event handler for the request’s end event where we use our instance of node-static to serve the files.
When we call file.serve, we pass it an optional callback function that let’s us customise how we handle any errors:
  • we check err.status, and serve either 404.html or 500.html accordingly
  • if there’s an error and the status code is something other than 404 or 500, we send the status code and the headers back to the client explicitly – when you pass a callback function to file.serve and there is an error, node-static will not respond to the client by itself.