Thursday, April 28, 2011

DNS dig alternative

dig alternative

Get DNS info for a domain:

$ host google.com
google.com has address 74.125.77.147
google.com has address 74.125.77.99
google.com has address 74.125.77.104
google.com mail is handled by 50 alt4.aspmx.l.google.com.
google.com mail is handled by 10 aspmx.l.google.com.
google.com mail is handled by 20 alt1.aspmx.l.google.com.
google.com mail is handled by 30 alt2.aspmx.l.google.com.
google.com mail is handled by 40 alt3.aspmx.l.google.com.

Sunday, April 17, 2011

Methods of communication

Today's communication tools:

0) E-mail
1) Instant messaging
2) Text messaging (SMS)
3) Group chats
4) Collaboration tools
5) Internal blogs
6) Social networks
7) Talking
8) Video conferences

Full original article: 8 Email Replacement Technologies

PHP HTML minimizer; PHP CSS minimizer

Minify HTML, minify CSS using PHP and regular expressions.

Minify HTML

function minify_html1($d) {
    $d = str_replace(array(chr(9), chr(10), chr(11), chr(13)), ' ', $d);
    $d = preg_replace('`<\!\-\-.*\-\->`U', ' ', $d);
    $d = preg_replace('/[ ]+/', ' ', $d);
    $d = str_replace('> <', '><', $d);
    return $d;
}

Minify CSS

function minify_css1($d) {
    $d = str_replace(array(chr(9), chr(10), chr(11), chr(13)), ' ', $d);
    $d = preg_replace('`/\*.*\*/`U', ' ', $d);
    $d = preg_replace('/[ ]+/', ' ', $d);
    $d = str_replace('; ', ';', $d);
    $d = str_replace('} ', '}', $d);
    $d = str_replace('{ ', '{', $d);
    $d = str_replace(': ', ':', $d);
    $d = str_replace(' {', '{', $d);
    return $d;
}

Thursday, April 14, 2011

Get redirect URL from headers (recursive)

Recursively obtain the redirect URL from web server page headers.
The method makes use of regular expressions, the PHP Curl library and also offers the possibility to indicate a number of consecutive (cascaded) redirects to follow. 

Usage:

$redirect_url = xDomainTools::getInstance()->getRedirectUrlRecursive($url, 2);

[Class ]methods:

class xDomainTools {
  
static private $instance = null;
  
private $ua = array(
    'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/3.5.1',
    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
);

public function __construct() {}

public function getInstance() {
    if(!self::$instance) {
        self::$instance = new xDomainTools;
    }
    return self::$instance;
}

private function getUaRand() {
    return $this->ua[rand(0, count($this->ua))];
}

public function getUrlContent($_url, $_referer = '', $_ua = '', $_return_transfer = true, $_timeout = 20, $_including_header = false, $_follow_location = true) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $_url);
    curl_setopt($ch, CURLOPT_HEADER, $_including_header);
    curl_setopt($ch, CURLOPT_REFERER, $_referer);
    curl_setopt($ch, CURLOPT_USERAGENT, strlen($_ua) ? $_ua : $this->getUaRand());
  
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies.txt');
    curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies.txt');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $_follow_location);
  
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $_timeout);
    curl_setopt($ch, CURLOPT_TIMEOUT, $_timeout);
  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, $_return_transfer);
  
    $d = curl_exec($ch);
    curl_close($ch);
  
    return $d;
}

public function getRedirectUrl($_url) {
    $header_content = xDomainTools::getInstance()->getUrlContent($_url, '', '', true, 20, true, false);
    $header_content = str_replace(array(chr(10), chr(11), chr(13)), ' ', $header_content) . ' ';
    preg_match('`.*(301 Moved Permanently|Redirect).*Location: (http://[^ ]+) .*`Ui', $header_content, $m);
    #printr($m);
    if(strlen($m[2])) {
        return $m[2];
    }
    return '';
}

public function getRedirectUrlRecursive($_url, $_no_redirects = 3, $_iteration = 0) {
    $_iteration++;
    if($_iteration == $_no_redirects + 1) {
        return $_url;
    }
    $url_redirect = $this->getRedirectUrl($_url);
    if(strlen($url_redirect)) {
        return $this->getRedirectUrlRecursive($url_redirect, $_no_redirects, $_iteration);
    }
    return $url_redirect;
}

}

Monday, April 4, 2011

Easy reading, hard writing

Easy reading is damn hard writing !

The simpler the interface, the heavier the code behind it.

Sunday, April 3, 2011

iconv and strtr combination

I had to decode input strings that some times were encoded using a known charset (ex: ISO-8859-2) and other times an unknown encoding.

I must apply for the first case a conversion from ISO-8859-2 to UTF-8 using iconv.
$t = iconv("ISO-8859-2", "UTF-8//TRANSLIT//IGNORE", $t);

For the unknown encoding I must convert it using strtr.
$pairs = array('ã' => 'ă', 'º' => 'ş'); // for example
$t = strtr($t, $pairs);

Applying both conversions to the input string secventially will affect the output, malforming the characters.

The encoding should be somehow detected before applying any of the given conversions. But it is difficult and time consuming to attempt to determine the encoding of the input.

SOLUTION:
Apply strtr conversion first and compare the output with the input and if they are different, that's the right conversion, if not, the iconv conversion mut be applied.

function convChars($t) {
    $pairs = array('ã' => 'ă', 'º' => 'ş'); // for example
    $t2 = strtr($t, $pairs);
    if($t2 != $t) {
        $t = $t2;
    } else {
        $t = iconv("ISO-8859-2", "UTF-8//TRANSLIT//IGNORE", $t);
    }
    return $t;
}

Friday, April 1, 2011

Rsync over ssh custom ssh port

Directory syncronization on 2 systems using rsync over ssh.

rsync -az -e "ssh -p XXXX" /path1 user1@server1:/path2/