Perl, How to Load parameters from config file

Posted: Tue, Apr 26 07:27 AM (PDT)

The easy solution for Perl to have any configuration parameters of your application in a file

#!/usr/bin/perl

my %config = load_config("/etc/perl_application.conf");

sub load_config {
        my $conf_name = $_[0];
        my %config = {};
        open(CFG, "<".$conf_name);
        while() {
                chomp;
                my ($var, $val) = split(/\s*\=\s*/);
                if ($val eq 'true') { $config{$var} = true; }
                if ($val eq 'false') { $config{$var} = false; }
                if (($var=~/^\#/is) || ($var eq '')) { } else { $config{$var} = $val; }
        }
        close(CFG);

# Dump config file

        foreach my $key (keys %config) {
        if ($key=~/^HASH/is) { } else {
                      print $key.": ".$config{$key}."\n";
              }
        }
        return %config;
}

perl_application.conf

# MongoDB Settings
mongo_host = 127.0.0.1
mongo_port = 27017
mongo_database = atlas365
mongo_auth = false

# MySQL Settings
mysql_host = 127.0.0.1
mysql_port = 3306
mysql_database = atlas365
mysql_username = mysqlusr
mysql_password = mysqlpswd
Tags:

How to change DOCUMENT_ROOT for PHP

Posted: Fri, Apr 22 01:22 AM (PDT)

If you are running into issues with the $_SERVER["DOCUMENT_ROOT"] variable showing "/usr/local/data/www/"; when using PHP5 and need to have it point to your correct document root for your application, here is a simple tutorial for changing it.

Create a file document_root.php into your root application directory with follow the code:

<?
$_SERVER["DOCUMENT_ROOT"]  = "/vhosts/domain.com/"
?>

Add to your root application directory .htaccess with follow the code

php_value	auto_prepend_file	"/vhosts/domain.com/document_root.php"

This tells Apache to parse the document_root.php file before doing anything else.

Tags:

Adding SPF support for Exim

Posted: Mon, Apr 18 11:08 AM (PDT)

Adding SPF (Sender Policy Framework) support for Exim version 4.75

To learn more about SPF, visit http://www.openspf.org

Before implement SPF in your Exim mail system, compile and install Exim with libspf2 library. Google will help you :-)

You can now run SPF checks in incoming SMTP by using the "spf" ACL condition in either the MAIL, RCPT or DATA ACLs. When using it in the RCPT ACL, you can make the checks dependend on the RCPT address (or domain), so you can check SPF records only for certain target domains. This gives you the possibility to opt-out certain customers that do not want their mail to be subject to SPF checking.

Tags:

Perl Net::DNS / Querying DNS Servers

Posted: Mon, Apr 18 10:33 AM (PDT)

Perl Net::DNS library can be used to write powerful web or OS system applications involving DNS lookups and queries. In this tutorial I will show you how to write a DNS diagnostic application involving DNS and resolver.

Tags:

How to encode and decode Base64 in Grails, Groovy or Java

Posted: Fri, Mar 25 11:54 AM (PDT)

I don't know why, but function decodeBase64 in Grails doesn't work correctly. And I found other solution. See below

Now we include the base64 library

import org.apache.commons.codec.binary.Base64

Our encode function

def encodeBase64(String text) {
	return new String(Base64.encodeBase64(text.getBytes()))
}

Our decode function

def decodeBase64(String text) {
	return new String(Base64.decodeBase64(text))
}

That's all

Tags:

How to parse JSON in Grails

Posted: Thu, Mar 24 01:49 PM (PDT)

1. include JSON library

import grails.converters.JSON

2. use JSON library

def jsonString = '{"syntax":true,"email":"test@test.local","status":true,"domain":true}'
def json = JSON.parse(jsonString)
println json

All records are serializabled

happy end :-)

Tags:

HTTP Request in Grails

Posted: Thu, Mar 24 04:39 AM (PDT)

Simple function how you can make HTTP request in Grails

def httpPostRequest(String address, String data, String referer = "http://www.google.com", method = "POST", userAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13") {
	method = method?.toString()?.toUpperCase()
	if (method != "GET") { method = "POST" }
	try {
		def url = new URL(address)
		def conn = url.openConnection()
		conn.setRequestMethod(method)
		conn.setRequestProperty("User-Agent", userAgent)
		conn.setRequestProperty("Referer", referer)
		conn.doOutput = true
		Writer wr = new OutputStreamWriter(conn.outputStream)
		wr.write(data)
		wr.flush()
		wr.close()
		conn.connect()
		return conn?.content?.text
	} catch (e) {
		println e
		return false
	}
}

Now, try to use our function

def content = httpPostRequest("http://www.atlas365.com", "test=my%20first%20test")
if (content) { println content } else { println "Error!" }

This example show us "How we can get the web page http://www.atlas365.com/ by POST request with test parameter". The test parameter is equal "my first test"

Tags:

Grails email address validation

Posted: Wed, Mar 23 06:43 AM (PDT)

Perfect Grails plugin "grails email validator"

This plugin

  • Check if domain exist in DNS
  • Check domain MX records
  • Check if email address is exist and valid

The plugin is available on GitHub: https://github.com/tomaslin/grails-email-validator

Tags:

How To Get URL Parts in JavaScript

Posted: Wed, Mar 23 05:27 AM (PDT)

The window.location object is a JavaScript class that is used to store URLs. It comes with properties that represent each part of the URL, and can be updated by changing the href property. The key properties that this article deals with are:

  • window.location.href - the full URL address
  • window.location.protocol - the protocol what you use http or https
  • window.location.host - the hostname (for example localhost or www.atlas365.com or different)
  • window.location.pathname - the parts of the URL (for example index.html)

In addition there are various methods that:

  • window.location.reload() - the function for reload current webpage
  • window.location.replace(url) - the function for change URL address // for example window.location.replace("http://www.atlas365.com");
Tags:

input field restricting to numbers only

Posted: Tue, Mar 22 04:14 PM (PDT)

This page shows a technique for restricting the field to just numbers. The script also allows you to automatically jump to the next field if the user presses the decimal key.

Put this JavaScript to head tag in your HTML page

<script type="text/javascript">
<!--
function numbersonly(inputfield, e, dec) {
	var key;
	var keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	if ((key == null) || (key == 0) || (key == 8) ||
		(key == 9) || (key == 13) || (key == 27))
		return true;
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else if (dec && (keychar == ".")) {
		inputfield.form.elements[dec].focus();
		return false;
	}
	else
		return false;
}
//-->
</script>

Now, we will use our script in HTML form

<form action="/signup" method="post">
	<label>U.S. ZIP Code:</label><input type="text" name="zip" size="5" maxlength="5" value="" onKeyPress="return numbersonly(this, event)"/>
	<button type="submit">Submit</button>
</form>
Tags: