Archive for June, 2009

0062 | qmailtoaster patch for spamassassin SPF validation

Sunday, June 7th, 2009 Posted in Misc | No Comments »

จดไว้เผื่อไว้ใช้ build ใหม่

qmail-spf.patch

diff -rua qmail-1.03.orig/qmail-qmqpd.c qmail-1.03/qmail-qmqpd.c
--- qmail-1.03.orig/qmail-qmqpd.c       1998-06-15 18:53:16.000000000 +0800
+++ qmail-1.03/qmail-qmqpd.c    2004-06-06 05:21:02.000000000 +0800
@@ -53,6 +53,8 @@
   }
 }
 
+char envelope_from[1000];
+
 void getcomma()
 {
   char ch;
@@ -78,7 +80,7 @@
   if (!local) local = env_get("TCPLOCALIP");
   if (!local) local = "unknown";
 
-  received(&qq,"QMQP",local,remoteip,remotehost,remoteinfo,(char *) 0);
+  received(&qq,"QMQP",local,remoteip,remotehost,remoteinfo,(char *) 0,envelope_from);
 }
 
 char buf[1000];
@@ -132,9 +134,11 @@
   }
   getcomma();
 
-  if (getbuf())
+  envelope_from[0]=0;
+  if (getbuf()){
     qmail_from(&qq,buf);
-  else {
+    str_copy(envelope_from,buf);
+  } else {
     qmail_from(&qq,"");
     qmail_fail(&qq);
     flagok = 0;
diff -rua qmail-1.03.orig/received.c qmail-1.03/received.c
--- qmail-1.03.orig/received.c  1998-06-15 18:53:16.000000000 +0800
+++ qmail-1.03/received.c       2004-06-06 05:14:39.000000000 +0800
@@ -37,7 +37,7 @@
 /* "Received: from relay1.uu.net (HELO uunet.uu.net) (7@192.48.96.5)\n" */
 /* "  by silverton.berkeley.edu with SMTP; 26 Sep 1995 04:46:54 -0000\n" */
 
-void received(qqt,protocol,local,remoteip,remotehost,remoteinfo,helo)
+void received(qqt,protocol,local,remoteip,remotehost,remoteinfo,helo,envelope_from)
 struct qmail *qqt;
 char *protocol;
 char *local;
@@ -45,6 +45,7 @@
 char *remotehost;
 char *remoteinfo;
 char *helo;
+char *envelope_from;
 {
   struct datetime dt;
 
@@ -63,6 +64,9 @@
   safeput(qqt,remoteip);
   qmail_puts(qqt,")\n  by ");
   safeput(qqt,local);
+  qmail_puts(qqt," (envelope-from ");
+  safeput(qqt,envelope_from);
+  qmail_puts(qqt,")\n\t");
   qmail_puts(qqt," with ");
   qmail_puts(qqt,protocol);
   qmail_puts(qqt,"; ");

qmail-spf2.patch

--- qmail-smtpd.c       2009-06-07 12:46:04.000000000 +0700
+++ qmail-smtpd.c.new   2009-06-07 12:39:42.000000000 +0700
@@ -831,7 +831,7 @@
   qp = qmail_qp(&qqt);
   out("354 go ahead\r\n");
 
-  received(&qqt,protocol,local,remoteip,remotehost,remoteinfo,fakehelo);
+  received(&qqt,protocol,local,remoteip,remotehost,remoteinfo,fakehelo,mailfrom.s);
   spfreceived();
   blast(&hops);
   hops = (hops >= MAXHOPS);

0061 | PHP simple object cache

Tuesday, June 2nd, 2009 Posted in Database, PHP Coding, Programming, Web Server | 3 Comments »

ระบบ cache มาอีกแล้ว เอ๊อกๆ
คราวนี้เป็น object cache ครับ ไม่ใช่ cache ทั้งหน้าแบบคราวก่อน

<?php
$_cachetime = 3600;
$_cacheroot = '/path/to/cache/';
function cacheget($key) {
	global $_cachetime, $_cacheroot;
	$_cachename = sprintf('%x', crc32($key));
	$_cachefolder = $_cacheroot.substr($_cachename,0,2).'/';
	$_cachefile = $_cachefolder.$_cachename;
	if (file_exists($_cachefile) && (filemtime($_cachefile) > time() - $_cachetime || 
		(file_exists($_cachefile.'.lock') && time() - filemtime($_cachefile.'.lock') < 120))) {
		return unserialize(file_get_contents($_cachefile));
	}
	return false;
}
function cachelock($key) {
	global $_cacheroot;
	$_cachename = sprintf("%x", crc32($key));
	$_cachefolder = $_cacheroot.substr($_cachename,0,2).'/';
	$_cachefile = $_cachefolder.$_cachename;
	umask(0);
	if (!file_exists($_cachefolder)) {
		mkdir($_cachefolder,0777);
	}
	touch($_cachefile.'.lock');
}
function cacheset($key, $value) {
	global $_cacheroot;
	$_cachename = sprintf('%x', crc32($key));
	$_cachefolder = $_cacheroot.substr($_cachename,0,2).'/';
	$_cachefile = $_cachefolder.$_cachename;
	umask(0);
	if (!file_exists($_cachefolder)) {
		mkdir($_cachefolder,0777);
	}
	if ($_h = fopen($_cachefile, 'w')) {
		fwrite($_h, serialize($value));
		fclose($_h);
		unlink($_cachefile.'.lock');
	}
}
?>

วิธีติดตั้ง:

include ไฟล์ไปบนสุดเลย แก้ตัวแปร $_cacheroot กับ $_cachetime ด้วยนะครับ
chmod 777 folder ตามที่ตั้งใน $_cacheroot ไว้ด้วย

วิธีเรียกใช้:

สมมติว่า cache คำสั่ง sql นะครับ
ถ้าปกติใช้อย่างนี้

<?php
$query = 'SELECT * FROM table';
$sql = mysql_query($query);
while ($row = mysql_fetch_assoc($sql)) {
        var_dump($row);
}
?>

ก็แก้เป็นประมาณนี้

<?php
$query = 'SELECT * FROM table';
if (!$rows = cacheget($query)) {
        cachelock($query);
        $sql = mysql_query($query);
        $rows = array();
        while ($row = mysql_fetch_assoc($sql))
                $rows[] = $row;
        cacheset($query, $rows);
}
foreach($rows as $row) {
        var_dump($row);
}
?>

Tags: , ,