Aug 07

apache — Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe.

This afternoon,i was trying to install zen cart v1.5.1.The zen cart installer shows that my running php is of version 5.2.6. To continue the installer, the minimum version of php should be 5.2.14.

Unlucky,i didn’t find any php addons which meets zen cart 1.5.1 version. The newest php version wamp support is 5.2.11  which means i will have to grab a new php developing platform(kit).that’s no good.it takes too much time and too much work need to be done. i decided to integrate php5.2.17 to wamp.

At the first time,i download  php-5.2.17-nts-Win32-VC6-x86.zip (VC6 x86 Non Thread Safe) from http://windows.php.net/downloads/releases/php-5.2.17-nts-Win32-VC6-x86.zip.After some setting tweaks,i restart the apache,it shows the error:

Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.
Pre-configuration failed.

Then i noticed the nts from the file name of the downloaded zip. To fix the problem,i need to download a Thread Safe version. i download from http://windows.php.net/downloads/releases/php-5.2.17-Win32-VC6-x86.zip. It works. maybe you can config apache to run in cgi mode.i didn’t try that.

VC6 x86 Thread Safe

http://windows.php.net/downloads/releases/php-5.2.17-Win32-VC6-x86.zip

Jul 06

how to check if php suport jpeg or png

how to check if your php support jpeg or png on your web server? you can create a phpinfo page to check the gd section. or you can run the following command directly in your shell:

# php -r 'print_r(gd_info());'
Array
(
 [GD Version] => bundled (2.0.34 compatible)
 [FreeType Support] =>
 [T1Lib Support] =>
 [GIF Read Support] => 1
 [GIF Create Support] => 1
 [JPEG Support] =>
 [PNG Support] => 1
 [WBMP Support] => 1
 [XPM Support] =>
 [XBM Support] => 1
 [JIS-mapped Japanese Font Support] =>
)
Jun 18

mysql – how to stop infinite loop in mysql trigger

i have met such situation:two triggers on two talbes,which operate on each other,lead to circular dependencies.sometimes it may get mysql query failed:

Can’t update table ‘v9_member’
in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

Source code    
delimiter //
DROP TRIGGER IF EXISTS in_bbs_cms //
CREATE TRIGGER `in_bbs_cms` AFTER INSERT ON `ucenter_members`
FOR EACH ROW BEGIN
	INSERT INTO `ucenter_members` (username, password, email, regip, regdate, random, ucuserid) VALUES(NEW.username, NEW.password, NEW.email, NEW.regip, NEW.regdate, NEW.salt, NEW.uid);
END
//
 
-- FOR NEW RECORD
DROP TRIGGER IF EXISTS in_cms_bbs //
CREATE TRIGGER `in_cms_bbs` AFTER INSERT ON `v9_member`
FOR EACH ROW BEGIN
	INSERT INTO v9_member (username, password, email, regip, regdate, salt) VALUES (NEW.username, NEW.password, NEW.email, NEW.regip, NEW.regdate, NEW.encrypt);
END
//

 

to avoid this problem, we need to make use of mysql global variable as a flag to trigger.

Source code    
delimiter //
DROP TRIGGER IF EXISTS in_bbs_cms //
CREATE TRIGGER `in_bbs_cms` AFTER INSERT ON `ucenter_members`
FOR EACH ROW BEGIN
	IF @TRIGGERED = NULL THEN
		SET @TRIGGERED = TRUE;
		INSERT INTO `ucenter_members` (username, password, email, regip, regdate, random, ucuserid) VALUES(NEW.username, NEW.password, NEW.email, NEW.regip, NEW.regdate, NEW.salt, NEW.uid);
		SET @TRIGGERED = NULL;
	END IF;
END
//
 
-- FOR NEW RECORD
DROP TRIGGER IF EXISTS in_cms_bbs //
CREATE TRIGGER `in_cms_bbs` AFTER INSERT ON `v9_member`
FOR EACH ROW BEGIN
	IF @TRIGGERED = NULL THEN
		SET @TRIGGERED = TRUE;
		INSERT INTO v9_member (username, password, email, regip, regdate, salt) VALUES (NEW.username, NEW.password, NEW.email, NEW.regip, NEW.regdate, NEW.encrypt);
		SET @TRIGGERED = NULL;
	END IF;
END
//

 

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();
}