11 ธันวาคม 2550

แก้ปัญหาไม่สามารถใช้ eth0 ได้เมื่อเปลี่ยน Network Card ใน Ubuntu 7.10

ใน Ubuntu 7.10 (อันที่จริงตั้งแต่ที่เริ่มใช้ udev) Network Card ที่ udev ตรวจสอบได้จะถูกบันทึกไว้ที่ /etc/udev/rules.d/70-persistent-net.rules (ไม่มี /etc/iftab ต่อไป) ถ้าเปลี่ยน Network Card ตัวใหม่จะถูกเพิ่มต่อท้าย ให้เปลี่ยนตัวที่เพิ่มขึ้นมาเป็น eth0 (หรือ Interface อื่น) และลบบรรทัดที่มี MAC address เดิมทิ้ง

20 มิถุนายน 2550

ปัญหาเรื่อง Time Zone ใน PHP และ MySQL

Time Zone Problems, ใน PHP และ MySQL ทำให้เวลาที่ใช้งานคลาดเคลื่อน สำหรับ PHP สามารถแก้ได้ใน Code ด้วย
date_default_timezone_set("Asia/Bangkok"); ซึ่งจะตั้งให้เป็นเวลามาตรฐานประเทศไทย
ถ้าต้องการแก้ค่า default ให้แก้ใน php.ini ที่
[Date]
; Defines the default timezone used by the date functions
date.timezone = Asia/Bangkok


ส่วน MySQL ให้ใช้ SQL Command "SET time_zone = '+7:00'" ตั้งให้เป็นเวลามาตรฐานประเทศไทย
ถ้าต้องการแก้ค่า default ให้แก้ใน /etc/mysql/my.cnf (my.ini สำหรับ Windows) ที่
[mysqld]
default-time-zone=+07:00
หรือตามด้านล่าง (ใช้บน Windows ไม่ได้)
[mysqld]
default-time-zone=Asia/Bangkok

16 มิถุนายน 2550

Zip และ Unzip ด้วย PHP

บันทึกวิธีการ Zip/Unzip ด้วย PHP:
1. Download dUnzip2 จาก phpclasses.org มี 2 classes, dZip & dUnzip

2. Zip โดยใช้ dZip classs
require_once("dzip.inc.php");
$zip = new dZip("archive.zip");
$zip->addFile("myfile.doc", "myfile.doc"); // parameter แรกเป็น source file, parameter สองเป็น ชื่อ file ใน zip
$zip->addDir("subdir"); // สร้าง Folder ใน Zip
$zip->addFile("anotherfile.jpg", "subdir/newname.jpg"); // เพิ่ม file ใต้ Folder
$zip->save();
3. Unzip โดยใช้ dUnzip class
require_once("dzip.inc.php");
$zip = new dUnzip("archive.zip");
$zip->unzipAll("targetfolder"); // Unzip ทุก file ไปยัง folder ชื่อ targetfolder
$zip->unzip("source.doc", "destination.doc"); // unzip source.doc ใน zip file เป็น destination.doc

28 พฤษภาคม 2550

php: baaGrid::setGPCol

baaGrid เป็น Data Grid class ตัวหนึ่งซึ่งสามารถแสดงผล SQL Query จาก Database ได้หลายตัว โดย Default เป็น MySQL

ความสามารถหนึ่งที่ทำให้ baaGrid แสดงรายงานในแบบตารางได้ดีคือ General Purpose Column ซึ่งจะสามารถนำข้อมูลจาก query มาแสดงในรูปแบบอื่นได้เช่น image หรือ hyperlink แต่อย่างหนึ่งซึ่งตัว baaGrid Guide ไม่ได้บอกไว้ก็คือการสนับสนุนการใช้ function ได้ (**อย่างมีข้อจำกัด**) ทั้ง php built-in และ user defined โดยในการ parse funtion นั้น baaGrid ใช้ function_exists ของ php ตรวจอักขระที่ ตำแหน่ง 0 จนถึง วงเล็บเปิดแรก ถ้ามี function ที่รู้จักก็จะผ่านไปให้ eval ของ php ทำงาน
ตัวอย่าง
...
$grid->setGPCol(4, 'myfunction(#1)'); // pass column 1 from query to myfunction, valid for numeric data
$grid->setGPCol(5, 'myfunction("#2")'); // pass column 2 from query to myfunction, valid for string data
$grid->setGPCol(6, 'myfunction(#1)+123'); // also valid
...
$grid->setGPCol(6, '123+myfunction(#1)'); // invalid, function_exists('123+myfunction') returns null

11 พฤษภาคม 2550

wxWidgets ใช้ Image File แบบอื่นนอกเหนือ BMP

ถ้าจะใช้ Image File แบบอื่นนอกเหนือ BMP ต้องทำการระบุชนิดที่ต้องการใช้เพิ่มด้วยดังนี้
1 ใน class ที่ derived จาก wxApp ที่ OnInit จะต้องทำการ add image handler

#include

bool MyApp::OnInit() {
...
wxImage::AddHandler( new wxPNGHandler );
wxImage::AddHandler( new wxJPEGHandler );
wxImage::AddHandler( new wxGIFHandler );
wxImage::AddHandler( new wxXPMHandler );
}



ถ้าเอาทุกแบบใช้ wxInitAllImageHandlers.
Available Image Handlers

  • wxBMPHandler For loading and saving Windows bitmap files.
  • wxPNGHandler For loading and saving PNG files. Images with transparency or an alpha channel are supported.
  • wxJPEGHandler For loading and saving JPEG files.
  • wxGIFHandler GIF files: only for loading, due to legal issues.
  • wxPCXHandler For loading and saving PCX files. wxPCXHandler will count the number of different colors in the image; if there are 256 or fewer colors, it will save as 8-bit; otherwise it will save as 24-bit.
  • wxPNMHandler For loading and saving PNM files. Loading PNMs only works for ASCII or raw RGB images. When saving in PNM format, wxPNMHandler will always save as raw RGB.
  • wxTIFFHandler For loading and saving TIFF files.
  • wxIFFHandler For loading IFF files.
  • wxXPMHandler For loading and saving XPM files.
  • wxICOHandler For loading and saving Windows icon files.
  • wxCURHandler For loading and saving Windows cursor files.
  • wxANIHandler For loading Windows animated cursor files.

2. อย่าลืม Link library ที่เกี่ยวข้อง

  • JPEG, wxjpeg.lib
  • PNG, wxpng.lib & wxzlib.lib

27 พฤศจิกายน 2549

ใช้ LDAP Authentication Client บน Ubuntu

ลูกค้ารายหนึ่งของผมต้องการให้ Mail Server ที่ติดตั้งไว้แล้วเป็นตัว Authenticate ให้กับ application อื่นๆ บน Server หลัก เนื่องจากผู้ใช้เล่นบอก password ของตัวเองกับคนอื่น MD ของบริษัทเลยสั่งว่า งั้นให้ใช้ password เดียวกับ email แล้วดูว่าจะบอก password กันอีกหรือเปล่า IT department เลยขอให้ผมซึ่งเป็นที่ปรึกษาของบริษัทอยู่ช่วยทำให้

มาดู Mail Server ที่ติดตั้งไว้เป็น Linux SIS 5.0 ก็เลยทำอะไรง่ายขึ้นมานิดนึง เพราะว่า email ของ Linux SIS ใช้ OpenLDAP เลยคิดว่าน่าจะใช้ LDAP authentication client ดีกว่า ส่วน Application/File Server เป็น Ubuntu 6.10 (Edgy)

ลองหาว่ามีใครทำแบบนี้แล้วบ้างก็เจอที่นี่ เลยเอามาดัดแปลงวิธีการ

ก่อนอื่นติดตั้ง libpam-ldap และ libnss-ldap
$ sudo aptitude install libpam-ldap libnss-ldap
หลังจากนั้นจะเข้าสู่การ configure libnss-ldap โดยมีขั้นตอนดังนี้

LDAP Server Host ใส่ IP address ของ LDAP Server ในที่นี้ก็คือ IP ของ Linux SIS ซึ่งเป็น LDAP Server ที่จะใช้
distinguished name of search base ใส่ dc=sis,dc=com ซึ่งเป็น default dn ที่ Linux SIS ใช้
LDAP version เลือก 3 ตาม Linux SIS
LDAP account for root ใส่ uid=root,ou=People,dc=sis,dc=com
LDAP root account password ใส่ password ของ Linux SIS root
จากนั้นจะได้รับคำเตือนว่า nsswitch.conf ไม่ได้ถูกแก้ เราจะกลับมาจัดการทีหลัง

เสร็จแล้วก็จะต่อด้วยการ configure libpam-ldap

ถามว่า Make local root Database admin ตอบ Yes
Database requires logging in ตอบ No
Root login account ใส่ uid=root,ou=People,dc=sis,dc=com
Root login password ใส่ password ของ Linux SIS root

ลองทดสอบว่าเราใส่ค่าถูกต้องหรือไม่ โดยใช้คำสั่ง getent passwd ถ้าถูกต้องจะแสดงผลลักษณะนี้
admin:x:499:499:admin:/home/admin:/bin/bash

อย่าใช้ username ที่มีอยู่บนเครื่องที่กำลังแก้ไขเพราะถ้าค่าที่ป้อนผิด ข้อมูลก็จะออกมาเหมือนกัน หลังจากนั้นตรวจสอบว่า /etc/libnss-ldap.conf และ /etc/pam_ldap.conf มีบรรทัดเหล่านี้ตามที่กำหนดไว้หรือไม่ เพราะอาจมีปัญหาถึงกับ login ไม่ได้
host
base dc=sis,dc=com
ldap_version 3
rootbinddn uid=root,ou=People,dc=sis,dc=com

ตรวจสอบว่า /etc/libnss-ldap.secret และ /etc/pam_ldap.secret เก็บ password ของ Linux SIS root หรือเปล่า

แก้ไข /etc/pam.d/common-account
account sufficient pam_ldap.so # ก่อน pam_unix.so
account required pam_unix.so


แก้ไข /etc/pam.d/common-auth
auth sufficient pam_ldap.so # ก่อน pam_unix.so
auth required pam_unix.so nullok_secure


แก้ไข /etc/pam.d/common-password
password sufficient pam_ldap.so # ก่อน pam_unix.so
password required pam_unix.so nullok obscure min=4 max=8 md5


แก้ไข /etc/nsswitch.conf เปลี่ยน compat ไปเป็น files ldap ในทุกบรรทัด
passwd: files ldap
group: files ldap
shadow: files ldap


เพื่อให้ user สามารถ login บน server ที่ใช้ LDAP Authentication client ต้องสร้าง home directory ให้ตรงกับที่กำหนดที่ Linux SIS ซึ่งก็คือ /home/ แล้วเปลี่ยนเจ้าของและสิทธิตามสมควร
$sudo mkdir /home/
$sudo chown : /home/

เสร็จแล้ว!!!