Wednesday, February 27, 2013

use multiple database in one cakephp project

Your app/Config/database.php file should look like this

<?php 
class DATABASE_CONFIG {

 public $default = array(
  'datasource' => 'Database/Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'root',
  'password' => '',
  'database' => 'spamassassin',
  'prefix' => '',
  //'encoding' => 'utf8',
 );

 public $vpopmail = array(
  'datasource' => 'Database/Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'root',
  'password' => '',
  'database' => 'vpopmail',
  'prefix' => '',
  //'encoding' => 'utf8',
 );
}
?> 

Your Model class should look like this to use database "spamassassin"

<?php
class Userpref extends AppModel {
    public $name = 'Userpref';
    var $primaryKey = "prefid";
    public $useTable = 'userpref';
}
?>

Your Model class should look like this to use database "vpopmail"


<?php
class VPopMailSetting extends AppModel {
    public $name = "VPopMailSetting";
    var $useDbConfig = 'vpopmail';
    var $useTable = 'settings';
}
?>

Tuesday, February 26, 2013

online code beautifier

simple class for calling a url using either curl or fopen


<?php
class EmailManager {
    var $_server;
    var $_account;

    const client_login_url = 'https://www.google.com/accounts/ClientLogin';

    function __construct($params = array())
    {
        if(!isset($params["server"])) {
            throw new Exception("Server name required.");
        }
        if(!isset($params["account"])) {
            throw new Exception("Account name required.");
        }
        $this->_account = $params["account"];
        $this->_server = $params["server"];
    }

    protected function authenticateUser($email, $password)
    {
        $postVariables = array(
            'accountType' => 'GOOGLE',
            'Email' => $email,
            'Passwd' => $password,
            'service' => 'analytics'
        );
        $response = $this->httpRequest(EmailManager::client_login_url, null, $postVariables);
    }

    protected function httpRequest($url, $get_variables=null, $post_variables=null, $headers=null)
    {
        $interface = null;
        if(function_exists('curl_exec')) {
            $interface = 'curl';
        } else {
            $interface = 'fopen';
        }
        if($interface == 'curl') {
            return $this->curlRequest($url, $get_variables, $post_variables, $headers);
        } else if($interface == 'fopen') {
            return $this->fOpenRequest($url, $get_variables, $post_variables, $headers);
        }
    }

    private function curlRequest($url, $get_variables=null, $post_variables=null, $headers=null)
    {
        $ch = curl_init();

        if(is_array($get_variables))
        {
            $get_variables = '?' . str_replace('&amp;','&',urldecode(http_build_query($get_variables)));
        }
        else
        {
            $get_variables = null;
        }

        curl_setopt($ch, CURLOPT_URL, $url . $get_variables);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //CURL doesn't like google's cert

        if(is_array($post_variables))
        {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables);
        }

        if(is_array($headers))
        {
            curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        }

        $response = curl_exec($ch);
        $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

        curl_close($ch);

        return array('body'=>$response,'code'=>$code);
    }

    private function fOpenRequest($url, $get_variables=null, $post_variables=null, $headers=null)
    {
        $http_options = array('method'=>'GET','timeout'=>3);

        if(is_array($headers))
        {
            $headers = implode("\r\n",$headers) . "\r\n";
        }
        else
        {
            $headers = '';
        }

        if(is_array($get_variables))
        {
            $get_variables = '?' . str_replace('&amp;','&',urldecode(http_build_query($get_variables)));
        }
        else
        {
            $get_variables = null;
        }

        if(is_array($post_variables))
        {
            $post_variables = str_replace('&amp;','&',urldecode(http_build_query($post_variables)));
            $http_options['method'] = 'POST';
            $headers = "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($post_variables) . "\r\n" . $headers;
            $http_options['header'] = $headers;
            $http_options['content'] = $post_variables;
        }
        else
        {
            $post_variables = '';
            $http_options['header'] = $headers;
        }

        $context = stream_context_create(array('http'=>$http_options));
        $response = @file_get_contents($url . $get_variables, null, $context);

        return array('body'=>$response!==false?$response:'Request failed, fopen provides no further information','code'=>$response!==false?'200':'400');
    }
}
?>

Monday, February 25, 2013

restarting and reloading name server zone files

Restart zone files:
service named restart

Reload changed zone files:
rndc reload
 tail -f ./var/log/messages

Sunday, February 24, 2013

validating fqdn url email hostname using java regular expression

Using java Pattern class to validate the FQDN, URL, E-Mail address hostname


import java.io.*;
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 
 

public class Validator 
{
    private static final Pattern emailPattern = Pattern.compile("^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$", Pattern.CASE_INSENSITIVE);


    private static final Pattern fqdnPattern = Pattern.compile("(?=^.{1,254}$)(^(?:(?!\\d+\\.|-)[a-zA-Z0-9_\\-]{1,63}(?<!-)\\.?)+(?:[a-zA-Z]{2,})$)", Pattern.CASE_INSENSITIVE);

     private static final Pattern hostPattern = Pattern.compile("^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9])$", Pattern.CASE_INSENSITIVE);

    private static final Pattern urlPattern = Pattern.compile("[^(http\\:\\/\\/[a-zA-Z0-9_\\-]+(?:\\.[a-zA-Z0-9_\\-]+)*\\.[a-zA-Z]{2,4}(?:\\/[a-zA-Z0-9_]+)*(?:\\/[a-zA-Z0-9_]+\\.[a-zA-Z]{2,4}(?:\\?[a-zA-Z0-9_]+\\=[a-zA-Z0-9_]+)?)?(?:\\&[a-zA-Z0-9_]+\\=[a-zA-Z0-9_]+)*)$]", Pattern.CASE_INSENSITIVE);


    public static boolean isInteger(String intVal)
    {
        try {
            int i = new Integer(intVal);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isBoolean(String boolVal)
    {
        try {
            boolean b = new Boolean(boolVal);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isFloat(String floatVal)
    {
        try {
            float f = new Float(floatVal);
            return true;
        } catch (Exception e) {
        }
        return false;
    }

    public static boolean isLong(String logVal)
    {
        try {
            long l = new Long(logVal);
            return true;
        } catch(Exception e) {
        }
        return false;
    }

    public static boolean isFQDN(String fqdnVal)
    {
        try {
                   Matcher matcher = fqdnPattern.matcher(fqdnVal); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;


    }

    public static boolean isURL(String url)
    {
        try {
                   Matcher matcher = urlPattern.matcher(url); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;

    }

    public static boolean isEmailAddr(String emailAddr)
    {
        try {
                   Matcher matcher = emailPattern.matcher(emailAddr); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;
    }

    public static boolean isHost (String hostname)
    {
        try {
                   Matcher matcher = hostPattern.matcher(hostname); 
                return matcher.matches(); 
        } catch (Exception e){
        }
        return false;
    }

    public static void main(String args[])
    {
        try {
            String action = args[0];
            String input = args[1];
            boolean result = false;
            if (action.equals("email")) {
                result = isEmailAddr(input);   
            } else if (action.equals("host")) {
                result = isHost(input);
            } else if(action.equals("url")) {
                result = isURL(input);
            } else if(action.equals("fqdn")) {
                result = isFQDN(input);
            }
            System.out.println("RESULT : " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

shell script echo new line to file


var1="Hello"
var2="World!"
logwrite="$var1\n$var2"
echo -e "$logwrite"  >> /Users/username/Desktop/user.txt
Explanation:
The \n escape sequence indicates a line feed. Passing the -e argument to echo enables interpretation of escape sequences.
It may even be simplified further:
var1="Hello"
var2="World!"
echo -e "$var1\n$var2"  >> /Users/username/Desktop/user.txt
or even:
echo -e "Hello\nWorld! "  >> /Users/username/Desktop/user.txt

How to tell if a string is not defined in a bash shell script

#!/bin/bash

if [ -z $1 ]; then
        echo 'Enter something please.';
        exit 1;
fi;