Tuesday, November 27, 2012

phpWhois -base class to do whois queries with php

Code block:
        $pathMain = WWW_ROOT."..\\libs\\phpwhois\\whois.main.php";
        $pathUtils = WWW_ROOT."..\\libs\\phpwhois\\whois.utils.php";
        if(file_exists($pathMain) && file_exists($pathUtils)) {
            include_once($pathMain);
            include_once($pathUtils);
            $whois = new Whois();
            $whois->non_icann = true;
            $result = $whois->Lookup("yourcause22.com");
            print_r($result);
        }

Download WHOIS from:
http://www.phpwhois.org/
http://sourceforge.net/projects/phpwhois/?source=dlp

jQuery Back Button (go to previous page)

You can check history length by jQuery and if can not possible to go back you can remove it.

First in html page introduce a hyperlink:
<a href="javascript:void(0)" class="back_link">Back</a>

Then add the jQuery code block:
<script type='text/javascript'>
    if(history.length <= 1) {
        jQuery(".back_link").remove();
    }
    jQuery(".back_link").bind("click", function() {
        if(history.length > 1) {
            parent.history.back();
        }
        return false;
    });
</script>

Saturday, November 24, 2012

Get xml from a php webservice url using android

public String getXML(){
    String line = null;
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpPost = new HttpGet("http://example.com/a.php");

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        line = EntityUtils.toString(httpEntity);
    } catch (Exception ex) {
        Log.d("Error reading xml", ex.toString());
    }
    return line;
}
 
But if u are using your local host as test purpose then use 10.0.2.2
instead of localhost/127.0.0.1  

Replace a string in file using shell script

Suppose my file a.conf is as following
Include /1
Include /2
Include /3
I want to replace "Include /2" with a new line, I write the code in .sh file : 
line="Include \\/2"
rep=""
sed "s/${line}/${rep}/g" /root/new_scripts/a.conf > /tmp/a.conf-new
 
mv /tmp/a.conf-new /root/new_scripts/a.conf 

Encrypt a file using bash shell script

openssl des3 -salt -in /pritom/input.sql -out /pritom/output.pk -pass pass:pritom
 
where:
 /pritom/input.sql is the input file
 /pritom/output.pk is encrypted output file
 -pass pass: pritom (pritom is used as password) 

Insert arabic word to mysql database using java

    Connection con = null;
    String unicode= "?useUnicode=yes&characterEncoding=UTF-8"; 
    String url = "jdbc:mysql://localhost/";
    String db = "students";
    String driver = "com.mysql.jdbc.Driver";
    try {
        Class.forName(driver);
        con = DriverManager.getConnection(url+db+unicode,"root","");
        Statement st = con.createStatement();
        String name = new String(txtName.getText().getBytes(), "UTF-8");
        int val = st.executeUpdate("insert into student(name, roll) "+
            " VALUES('"+name+"','"+txtRoll.getText()+"')");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
 
You have to create your database as a UTF-8 table otherwise no matter what you 
add it will never be able to store it... 


Ensure that your MySQL configuration encoding is defined correctly. Add these lines to either my.cnf "in the case if using linux" or my.ini "case you using windows"
[client] 
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
default-character-set=utf8
character-set-server=utf8

Thursday, November 22, 2012

Encrypt and decrypt string using AES 128 and PKCS7 algorithm using Php

Encrypt and decrypt string using AES algorithm executed by PHP script is given below:


<?php
function encrypt($key, $to_encrypt)
{
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
    $pad = $iv_size - (strlen($to_encrypt) % $iv_size);
    $to_encrypt = str_pad($to_encrypt, ($iv_size * (floor(strlen($to_encrypt) / $iv_size) + 1)), chr($pad));
    return base64_encode($iv . mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $to_encrypt, MCRYPT_MODE_CBC, $iv));
}

function decrypt($key, $to_decrypt)
{
    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = substr($to_decrypt, 0, $iv_size);
    $to_decrypt = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, substr($to_decrypt, $iv_size), MCRYPT_MODE_CBC, $iv);
    $pad = ord($to_decrypt[strlen($to_decrypt) - 1]);
    return substr($to_decrypt, 0, -$pad);
}

$key = "1111111111111111";

$str = "Value to be encrypt";
$enc = encrypt($key, $str);
$dec = decrypt($key, base64_decode($enc));

echo "Original=" . $str;
echo "<BR>Encrypted=" . $enc;
echo "<BR>Decrypted=" . $dec;

Below is output:

Original=Value to be encrypt
Encrypted=fyqhmBCGLVXcT5tvq38G/MAlpgP2FYq29wgC7KIMT8jt6Z45cq63oJeY8kGgv9QP
Decrypted=Value to be encrypt