javascript – A simple javascript countdown timer

This is a simple but light weight javascript countdown timer,which can be integrated to your zen cart store by uploading the file to the jscript sub folder of your current zen cart template folder.

I name it as jscript_timer.php, so i can  put some php configure variable to the zen cart admin backend. if you dont’ want,you can rename it to jscript_timer.js.  This countdown timer countdown 4 days and it will start over again.so no bother to admin setting once and once again.(:

[codesyntax lang=”php”]

<?php
$path = '/' . DIR_WS_TEMPLATE . 'jscript/';
$img = $path . 'timer.png';
?>
<script language="javascript" type="text/javascript"><!--
var present;
var future;
var tseconds;
var seconds;
var minutes;
var hours;
var days;
var limit;
limit =4;
// ID=setInterval("countdown();", 1000);

function countdown() {
	present = new Date();
	present = present.getTime();
	future = new Date("MAY 27, 2015 11:59:59");

	tseconds = (future - present)  / 1000;

	days = tseconds /24/60/60;
	days = Math.floor(days);
	odays = days;
	days = days%limit;
	tseconds = tseconds - (odays - days) * 24 * 60 * 60 - (days * 24 * 60 * 60);

	hours = tseconds /60/60;
	hours = Math.floor(hours);
	tseconds = tseconds - (hours * 60 * 60);

	minutes = tseconds /60;
	minutes = Math.floor(minutes);
	tseconds = tseconds - (minutes * 60);

	seconds = tseconds;
	seconds = Math.floor(seconds);
	htmlstr = '<span style="font-weight:bold;font-size:16px">' + days + '</span> days <span style="font-weight:bold;font-size:12px">' +  hours + ':' + minutes + ':' + seconds + '</span>  Left';
	document.getElementById('timerdiv').innerHTML = htmlstr;
	//console.info(days + ' days ' + hours + ' hours ' + minutes + ' minutes ' + seconds + ' seconds');
}
window.onload = function () {
    ID=setInterval("countdown();", 1000);
};
//--></script>

[/codesyntax]

Feel free to adapt it to fit your need!!

 

linux — install mod_rapf to work with apache2.4 backend in ubuntu

you can not get real ip of the client making the request if you put apache behind the nginx server.All cilent ip become 127.0.0.1 in the apache access log.tha’s because that nginx now become the client of the apache server.To circumvent this, you’d want to install mod_rpaf (http://stderr.net/apache/rpaf/).

To get and source code:

[codesyntax lang=”bash”]

http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz

[/codesyntax]

To build and install a third-party Apache module,we need the help of apxs

[codesyntax lang=”bash”]

apxs -c mod_rpaf-2.0.c
apxs -i -a -n mod_rpaf-2.0 mod_rpaf-2.0.la

[/codesyntax]

However,i got error message:

[codesyntax lang=”bash”]

usr/local/apache2/build/libtool --silent --mode=compile gcc -std=gnu99 -prefer-pic   -D_REENTRANT -D_GNU_SOURCE -g -O2 -pthread -I/usr/local/apache2/include  -I/usr/local/apache2/include   -I/usr/local/apache2/include   -c -o mod_rpaf-2.0.lo mod_rpaf-2.0.c && touch mod_rpaf-2.0.slo
mod_rpaf-2.0.c: In function 'rpaf_cleanup':
mod_rpaf-2.0.c:150: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:151: error: 'conn_rec' has no member named 'remote_addr'
mod_rpaf-2.0.c:151: warning: implicit declaration of function 'inet_addr'
mod_rpaf-2.0.c:151: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c: In function 'change_remote_ip':
mod_rpaf-2.0.c:164: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:183: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:186: error: 'conn_rec' has no member named 'remote_ip'
mod_rpaf-2.0.c:187: error: 'conn_rec' has no member named 'remote_addr'
mod_rpaf-2.0.c:187: error: 'conn_rec' has no member named 'remote_ip'
apxs:Error: Command failed with rc=65536

[/codesyntax]

After google,i know that the mod_rpaf need to pached to work with apache2.4.

[codesyntax lang=”bash”]

apt-get install git
git clone git://gist.github.com/2716030.git
apt-get install patch
patch mod_rpaf-2.0.c 2716030/mod_rpaf-2.0.c.patch

[/codesyntax]

if you don’t like to use git,you can just copy and save the diff file.Now compile and install again:

[codesyntax lang=”bash”]

apxs -c mod_rpaf-2.0.c
apxs -i -a -n mod_rpaf-2.0 mod_rpaf-2.0.la

[/codesyntax]

BUT after install,when you tried to check with

[codesyntax lang=”bash”]

httpd -S

[/codesyntax]

it shows that there are some syntax error if apache load the new complied mod_rpaf-2.0.so.after some try,

[codesyntax lang=”bash”]

apxs -c mod_rpaf-2.0.c
apxs -i -a -n mod_rpaf mod_rpaf-2.0.c

[/codesyntax]

This will work.maybe this only happened in ubuntu.

javascript — get string utf8 encode and decode

[codesyntax lang=”javascript”]

<script type="text/javascript">
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
function encode64(input)
{
    input = escape(input)
    var output = ""
    var chr1, chr2, chr3 = ""
    var enc1, enc2, enc3, enc4 = ""
    var i = 0
    do
    {
        chr1 = input.charCodeAt(i++)
        chr2 = input.charCodeAt(i++)
        chr3 = input.charCodeAt(i++)
        enc1 = chr1 >> 2
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
        enc4 = chr3 & 63
        if (isNaN(chr2))
        {
            enc3 = enc4 = 64
        }
        else if (isNaN(chr3))
        {
            enc4 = 64
        }
        output = output +
        keyStr.charAt(enc1) +
        keyStr.charAt(enc2) +
        keyStr.charAt(enc3) +
        keyStr.charAt(enc4)
        chr1 = chr2 = chr3 = ""
        enc1 = enc2 = enc3 = enc4 = ""
    }
    while(i < input.length)
    return output
}
function decode64(input)
{
    var output = ""
    var chr1, chr2, chr3 = ""
    var enc1, enc2, enc3, enc4 = ""
    var i = 0
    // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
    var base64test = /[^A-Za-z0-9\+\/\=]/g
    if (base64test.exec(input))
    {
        alert("There were invalid base64 characters in the input text.\n" +
        "Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
        "Expect errors in decoding.")
    }
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "")
    do
    {
        enc1 = keyStr.indexOf(input.charAt(i++))
        enc2 = keyStr.indexOf(input.charAt(i++))
        enc3 = keyStr.indexOf(input.charAt(i++))
        enc4 = keyStr.indexOf(input.charAt(i++))
        chr1 = (enc1 << 2) | (enc2 >> 4)
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
        chr3 = ((enc3 & 3) << 6) | enc4
        output = output + String.fromCharCode(chr1)
        if (enc3 != 64)
        {
            output = output + String.fromCharCode(chr2)
        }
        if (enc4 != 64)
        {
            output = output + String.fromCharCode(chr3)
        }
        chr1 = chr2 = chr3 = ""
        enc1 = enc2 = enc3 = enc4 = ""
    }
    while (i < input.length)
    return unescape(output)
}
</script>

[/codesyntax]

javascript — a simple progress indicator

well,the javascript snippet below is a simple js progress indicator

[codesyntax lang=”javascript”]

var start = 0;
  var step = 9;
  var s =  setInterval(function () {
     if (start >= 90) {
         start < 95  ? start = 95 : '';
         start +=1 ;
     } else {
         start += Math.floor(Math.random()*10) + step;
         step = ((step - 1) > 1) ? step: 9;
     }
     if (start >= 100) {
         start = 100;
         clearInterval(s);
     }
     console.info(start + '%');
   }, 600);

[/codesyntax]

linux – “Disk xxx contains BIOS RAID metadata”

While installing Cent OS 6.4,i got the error like below:

Getting error “Disk sda contains BIOS RAID metadata, but is not part of any recognized BIOS RAID sets. Ignoring disk sda.”

after googling,i reboot into rescue mode,with the following command to erase the whole disk:

dd if=/dev/zero of=/dev/xxx (xxx is the disk you would like to install linux)

php — fsockopen post https request

to make a https post request, the difference is the port and the protocal used.take a look at the example code below:

[codesyntax lang=”php”]

$data = array();
$data['name'] = $name;
$data['tel'] = $tel;
//error_log('data: ' . print_r($data,1) ."\n" , 3, 'log');    
$req = '';
foreach ($data as $key => $value) {
    //$value = urlencode(stripslashes($value));
    $req .= "&$key=$value";
}
//error_log('req: ' . $req ."\n" , 3, 'log');   
$post_url = 'ssl://www.abcdomain.com';
$header = '';
$header .= "POST /index.php/api/ HTTP/1.0\r\n";
$header .= "Host: www.abcdomain.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ($post_url, 443, $errno, $errstr, 60);
//error_log('errno: ' . $errno . ' errstr: ' . $errstr ."\n" , 3, 'log');
if (!$fp) {
    //do something
} else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
        if ($body == '' && strpos ( $res, "\r\n\r\n" ) === FALSE) {
            $res = fgets ($fp, 1024);
        } else {
            $body = fgets ($fp, 1024);
        }
    }
}
$objRes = json_decode($body);
//error_log('res: ' . $res . "\n", 3 , 'log');

[/codesyntax]

make sure your php is configured with openssl extension enabled

extension=php_openssl.dll