May 31

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.(:

Source code    
<?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>

Feel free to adapt it to fit your need!!

 

May 15

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:

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

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

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

However,i got error message:

Source code    
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

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

Source code    
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

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

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

BUT after install,when you tried to check with

Source code    
httpd -S

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

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

This will work.maybe this only happened in ubuntu.

May 13

javascript — get string utf8 encode and decode

Source code    
<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>
May 11

javascript — a simple progress indicator

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

Source code    
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);
May 09

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)

May 01

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:

Source code    
$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');

make sure your php is configured with openssl extension enabled

extension=php_openssl.dll

Posted in php
Apr 29

zen cart — one way to override function in the folder of includes/functions

zen cart don’t have a build in way to override all functions defined in the folder of includes/functions. PHP will throw out an error of “duplicate declare is not allowed”. well, we know that zen cart have built in mechanism to  override all the files in the includes/init_includes folder. And all the files in the includes/functions folder are loaded via includes/init_includes/init_general_funcs.php. let’s take a look at the php source:

Source code    
<?php
/**
 * load the system wide functions
 * see {@link  http://www.zen-cart.com/wiki/index.php/Developers_API_Tutorials#InitSystem wikitutorials} for more details.
 *
 * @package initSystem
 * @copyright Copyright 2003-2005 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: init_general_funcs.php 2845 2006-01-13 06:49:15Z drbyte $
 */
if (!defined('IS_ADMIN_FLAG')) {
  die('Illegal Access');
}
/**
 * General Functions
 */
require(DIR_WS_FUNCTIONS . 'functions_general.php');
/**
 * html_output functions (href_links, input types etc)
 */
require(DIR_WS_FUNCTIONS . 'html_output.php');
/**
 * basic email functions
 */
require(DIR_WS_FUNCTIONS . 'functions_email.php');
/**
 * EZ-Pages functions
 */
require(DIR_WS_FUNCTIONS . 'functions_ezpages.php');
/**
 * User Defined Functions
 */
include(DIR_WS_MODULES . 'extra_functions.php');
?>

 

To override the includes/functions/functions_general.php, we need make a change of this file.so

First,make a copy of includes/init_includes/init_general_funcs.php to includes/init_includes/overrides/init_general_funcs.php

Second, alter the includes/init_includes/overrides/init_general_funcs.php as below

/**
 * General Functions
 */
require(DIR_WS_FUNCTIONS . 'functions_general.php');

To

/**
 * General Functions
 */
require(DIR_WS_FUNCTIONS . '/overrides/functions_general.php');

Then you can do whatever changes as you want to any functions defined in that file.

if you want to overrides all files in includes/functions folder,just like zen cart build in override mechannism,the you can replace the content of includes/init_includes/overrides/init_general_funcs.php
as below:

Source code    
if ($za_dir = @dir(DIR_WS_INCLUDES . 'includes/functions/overrides')) {
  while ($zv_file = $za_dir->read()) {
    if (preg_match('/\.php$/', $zv_file) > 0) {
      include(DIR_WS_INCLUDES . 'includes/functions/overrides' . $zv_file);
    }
  }
  $za_dir->close();
}

 

 

 

 

 

 

Apr 29

linux — install cent os via url mirros in china

Below list some available mirrors url that can be used to do a network installation of cent os:

http://mirror.bit.edu.cn/centos/6.4/os/

http://mirrors.yun-idc.com/centos/6.4/os/

http://mirrors.btte.net/centos/6.4/os/

http://mirror.esocc.com/centos/6.4/os/

http://mirrors.163.com/centos/6.4/os/

http://mirrors.sohu.com/centos/6.4/os/

http://mirror.neu.edu.cn/centos/6.4/os/

Apr 27

linux — CentOS change the name of the network interface

The network didn’t work in my new minimal CentOS install. After some try on changing both “adapter type” and mac addr,i found that with ifconfig command

> ifconfig -a

First,you need to edit the file below,find the line with your mac(the one generated by Virtulbox),change ethx to ech0,remove all other lines.
> vim /etc/udev/rules.d/70-persistent-net.rules

the name of the network become eth3.not eth0 by default.but there is only ifcfg-eth0 in /etc/sysconfig/network-scirpts/. so is it possible for me to revert the name back to eth0?yes!

SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR{address}==”08:00:27:07:aa:e6″, ATTR{type}==”1″, KERNEL==”eth*”, NAME=”eth0″
SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR{address}==”03:00:23:24:45:e2″, ATTR{type}==”1″, KERNEL==”eth*”, NAME=”eth1″
SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR{address}==”03:00:37:27:ba:a3″, ATTR{type}==”1″, KERNEL==”eth*”, NAME=”eth2″
SUBSYSTEM==”net”, ACTION==”add”, DRIVERS==”?*”, ATTR{address}==”05:00:37:07:ab:c6″, ATTR{type}==”1″, KERNEL==”eth*”, NAME=”eth3″

Now, reload the udev configuration.
>  start_udev

Third,restart network service
> service network restart

 

 

Apr 25

Linux — how to know if php run as cgi or module

For some reason you want to know how is php working with web server like apache. if you are the people who config the server,you got the answer in your head. what if you are not? well,php have a function for this:

php_sapi_name

Per php manual,call this function will returns the type of interface between web server and PHP

Source code    
<?php
php_sapi_name();
?>

The section below is take from the php manual:
Although not exhaustive, the possible return values include aolserver, apache, apache2filter, apache2handler, caudium, cgi (until PHP 5.3), cgi-fcgi, cli, continuity, embed, isapi, litespeed, milter, nsapi, phttpd, pi3web, roxen, thttpd, tux, and webjames.

When php run in CGI mode( or any other mode other that module mode), you can not set php config via php_value directive in the .htaccess file.

you can also take a look at the phpinfo page to decide how php is working with your webserver.