2012年5月30日 星期三

php run system command and redirect stderr

Reference: http://php.net/manual/en/function.shell-exec.php

<?php
$cmd = 'ls -l';
$output = shell_exec($cmd);
print "<pre>$output";
?>

如果 $cmd 會產生 stderr,可以加上 2>&1 將 stderr 導向 stdout。
Ex: $cmd = 'ls -l 2>&1';

厲害的反安裝軟體 Revo uninstaller Pro

http://www.revouninstaller.com/revo_uninstaller_free_download.html

有一個軟體已經用控制台裡面的 新增移除程式 刪掉了,但是裝新版卻說已安裝 ...
用 Revo uninstaller 可以完全清除,這樣就能安裝新版軟體了。

2012年5月28日 星期一

PHP send MIME mail with attachment

Reference:
http://www.dk101.com/index.php/viewnews-837.html
http://www.codewalkers.com/c/a/Email-Code/PHP-Email-Attachment-v1/

設定 PHP 寄信:(Linux)
如果有啟用 sendmail 並設定完成,則不需修改 /etc/php.ini。

如果沒有啟用 sendmail,編輯 /etc/php.ini,設定 SMTP = your_mail_server。



寄有附件的 mail:
function mail_with_attach($to, $subject, $message, $filepath, $filename) {
$fileatt = $filepath . '/' . $filename; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = $filename; // Filename that will be used for the file as the attachment

$email_from = "from@domain.com"; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it

$email_to = $to; // Who the email is to

$headers = "From: ".$email_from;

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"big5\"\n" .
"Content-Transfer-Encoding: quoted-printable\n\n" .
$email_txt . "\n\n";

$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";

$ok = @mail($email_to, $email_subject, $email_message, $headers);

if($ok) {
echo "<font face=verdana size=2>The file was successfully sent!</font>";
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}

2012年5月25日 星期五

PHP debug

Reference: http://yuanann.pixnet.net/blog/post/25456607-php-debug-info-%E8%A8%AD%E5%AE%9A

Ex: debug http://mysite.com/my_test.php

Solution 1:
在 linux terminal 執行 php my_test.php,如果有錯誤會顯示訊息 。

Solution 2:
修改 /etc/php.ini,設定 display_errors = On,再將 httpd restart。
開啟網頁 http://mysite.com/my_test.php,如果有錯誤會顯示訊息。

紙模型 F-117 Nighthawk

Download: http://www.papercraftmuseum.com/f117/

紙張:2張
製作時間:2.5小時

完成圖:









補色前:(補色大約 0.5 小時)






2012年5月23日 星期三

vsftpd log file xferlog format

Reference: http://www.castaglia.org/proftpd/doc/xferlog.html

Format:
current-time transfer-sec remote-host file-size filename transfer-type special-action-flag direction access-mode username service-name authentication-method authenticated-user-id completion-status


direction
o: outgoing, i: incoming, d: deleted

Install Perl module on Linux

Reference: http://perl.about.com/od/packagesmodules/qt/perlcpan.htm

Ex: need module MIME::Lite

Option1:
Command: perl -MCPAN -e 'install  MIME::Lite'

Option2:
Command: cpan -i MIME::Lite

Check Perl module install or not.
Command perldoc -m MIME::Lite

vsftpd log with localtime

Reference: http://vsftpd.beasts.org/vsftpd_conf.html

edit /etc/vsftpd/vsftpd.conf
add use_localtime=YES

And log with localtime is OK.

Perl MIME::LITE with UTF8

Reference: http://www.databasesandlife.com/using-utf-8-and-unicode-data-with-perl-mimelite/

use MIME::Lite;
MIME::Lite->send('smtp', $mail_host, Debug=>0, Timeout=>60);

$msg = MIME::Lite->new(
From     => $mail_from,
To       => $mail_to,
Bcc       => $mail_bcc,
Subject  => $mail_subject,
Type     =>'text/html; charset=UTF-8',
Data     => $mail_body
);
$msg->send();

Perl script and MSSQL DB

Solution 1:
use Win32::ODBC;
$DSN = "DSN=[my_DSN];UID=[DB_account];PWD=[DB_password];";   # please mind the red part
$DBH = new Win32::ODBC($DSN) or die("Can't connect to SQL Server");
$sql_command = "select * from table";
$DBH->sql($sql_command);
while ($DBH->FetchRow()) {
  @result = $DBH->Data();
  print @result;
}
$DBH->Close();

Solution 2:
use DBI;

my $dbs = "dbi:ODBC:DRIVER={SQL Server};SERVER={[DB_server]}";
my ($username, $password) = (' [DB_account] ', ' [DB_password] ');
my $dbh = DBI->connect($dbs, $username, $password);
$sql_command = "select * from table";
$sth = $dbh->prepare($sql_command);
$sth->execute();
while ( @result = $sth->fetchrow_array ) {
  print @result;
}
$dbh->disconnect;

Perl script to get MSSQL 2008 rows with Chinese (UTF8)

Reference:
http://blog.wu-boy.com/2009/07/perl-with-utf-8-mode/
http://www.jeffhung.net/blog/articles/jeffhung/417/

Perl output error message:
Wide character in print at test.pl line xx.

Root Cause:
Perl script 如果用一般的 ANSI 編碼格式儲存,輸出時也會是 ANSI 的格式。我從 MSSQL 2008 讀出的資料含有 UTF8 的中文字,print UTF8 data to STDOUT 會產生錯誤訊息。

Solution:
1. 將 test.pl 另存為 UTF-8 編碼格式
2. 在 test.pl 加上這幾行

use utf8;
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');

這樣就可以正常印出 UTF8 的資料了。

另外,發現 ActivePerl 5.14.2 的 use Win32::ODBC; 對於 MSSQL 2008 UTF8 資料似乎會發生問題。改用 use DBI; 才正確取得資料。

MSSQL連線方式請參考這一篇

2016/2/17 update:
原本的 perl script 在 Windows 2003 (32bit) 運作正常,移到 Windows 2008 (64bit) 後會有 Wide character in syswrite 的錯誤。
解法:(參考這一篇)
1. 取消這四行
use utf8;
binmode(STDIN, ':encoding(utf8)');
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
2. 加上
use Encode qw(encode);
3. 將 UTF-8 的資料編碼
$MA002 = encode("utf8", $result[1]);

2012年5月22日 星期二

紙模型 賽車 F1 Bar005

Download Link: http://metmania.com/dif1/f1/ff0.html

紙張:2張
製作時間:6小時

完成圖:








2012年5月21日 星期一

2012年5月18日 星期五

憑證 IC 卡無法讀取 (救回來囉)

用了好幾年的 自然人憑證 IC 卡,今天拿出來要報稅時,讀卡機居然說讀不到...
去年用還是正常的啊。

到內政部網站上看,重新申請憑證要工本費275元,還要親自跑一趟。
試了好幾次都不行,突然想到可能是 IC 卡金屬的部份氧化了。

酒精可以除去氧化的部份,但是家裡沒有。那就用化妝水代替一下,反正都讀不到,就試試看也不會更糟。

哈哈,用化妝水擦拭一下 IC 卡金屬的部份果然就讀到了。

2012年5月17日 星期四

MS Access Query parameters from Forms

Reference:
http://www.techrepublic.com/blog/msoffice/run-a-parameter-query-within-an-access-form/701
http://support.microsoft.com/kb/304428

目的:用 表單 設計 查詢的輸入參數

做法:
1. 在表單新增輸入的欄位

2. 在查詢中填入 [Forms]![報表名稱]![報表輸入欄位名稱]

PS. 如果要做成模糊查詢,請填入 like "*"&[Forms]![報表名稱]![報表輸入欄位名稱]&"*"

MS SQL 2008 outer join

目的:保留 Table IDLI12 所有的 Row,取出 Table PURMB, CMSNK 相關的欄位,如果 PURMB, CMSNK 的欄位對應不到相關的值,則顯示空白。

方法:使用 Outer Join。在 SQL 2008 欄位對應關係中選擇 Select All Rows from IDLI12。

Windows 8 Build 8370 installation

2012/5/17:
Install on Acer Veriton M4610 fail.
Error message: Your PC run into a problem and needs to restart. We're just collecting some error info and then we'll restart for you.

Use another old PC Acer Veriton M670 to install and success.

2012/5/30:
Install Windows 8 build 8375 on Acer Veriton M4610 success.

2012年5月15日 星期二

Windows ODBC DSN export and import

Reference:
http://stackoverflow.com/questions/4902685/exporting-odbc-system-dsns-from-a-windows-2003-machine
http://freetoad.pixnet.net/blog/post/23500196-regedit.exe-command-line%E6%AD%A3%E7%A2%BA%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95

目的:將設定好的ODBC DSN (Data Source Name) 移植到其他 PC

做法:
1. 執行 regedit,將 HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI 匯出成 DSN.reg 檔
2. 用 notepad 開啟 DSN.reg,將不需要的部分刪除
3. 將 DSN.reg 放到其他 PC,double click DSN.reg 匯入 ODBC 設定。

如果寫成 batch script,可以用 regedit /s DSN.reg。

2012年5月8日 星期二

紙模型 iPad 2

Download Link: http://hisashi-im-works.blogspot.com/

紙張:2張
製作時間:1小時

完成圖:





2012年5月7日 星期一

Microsoft RMS client error message

Error message: you do not have permission to open this document.

Root cause: the E-mail of this account is empty in AD.

Solution: Add E-mail address setting.

Reference: http://social.technet.microsoft.com/Forums/da-DK/rms/thread/b0a70770-2667-4420-a74b-0dfc3477b528

Novo 7 極光 刷機及軟體

Reference:
http://www.techorz.com/tablet/ainol-novo-7-flash-rom-install-android-market-tutorial/
http://www.mobile01.com/topicdetail.php?f=605&t=2579581&last=34137518

ROM download:
http://www.ainol.com/plugin.php?identifier=ainol&module=download (原廠)
http://www.slatedroid.com/topic/29280-romics-ecr-essential-clean-rom-v11a-for-novo-7/ (外國人改過的)

刷機相當於系統重裝,原本的資料全部都會消失!

Prepare:
1. 在 PC 安裝 Live Suit 1.09
2. download ROM 備用。國外的Essential Crean Rom v1.0a LiveSuite v1.0a有兩版本,用FT5X版正常。試過刷Goodix觸控無效。

刷機:
1. 確保 Ainol NOVO7 極光已關機,如未,可長按 Power 並選擇關機

2. 長按及保持機旁的 Back 返回鍵
3. 執行 Live Suit 1.09,選擇要刷的 ROM
4. 利用 USB 線把 NOVO7 極光連接到電腦
5. 保持長按機旁的 Back 返回鍵,快速按動 Power 鍵 5 至 6 次
6. 選擇 Yes 開始刷機

軟體安裝:(用 Google play 搜尋)
1. chrome (Browser)
2. chaozhuyin (超注音輸入法)
3. PPStream (看影片)
4. Documents to Go (for PDF, Microsoft Word, Excel, Powerpoint)

接下來請參考這一篇,可以解決 Novo 7 開網頁反應慢的問題。

2012年5月4日 星期五

2012年5月3日 星期四

Use Perl script to login https web page

Reference: http://www.ibm.com/developerworks/linux/library/wa-perlsecure/index.html

目的:取得 Skype 可用餘額
Goal: get skype credit balance by perl script

Perl Script:

use LWP::Simple;
use HTML::Parse;
use WWW::Mechanize;
use HTTP::Cookies;
use LWP::Debug qw(+);
$account1 = "Your_Skype_account";
$passwd1 = "Your_Skype_password";
$credit1 = get_credit($account1, $passwd1);
print "$credit1\n";
sub get_credit {
my $url = "https://login.skype.com/intl/zh-Hant/account/login-form";
my $username = $_[0];
my $password = $_[1];
my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_id('loginForm');
$mech->field(username => $username);
$mech->field(password => $password);
$mech->click();
my $output_page = $mech->content();
$page_text = parse_html($output_page)->format;
@tmp = split(/NT\$/, $page_text);
@tmp2 = split(/\s+/, $tmp[1]);
$credit_balance = @tmp2[1];
return $credit_balance;
}

如果 Perl 出現缺少部分模組的訊息,請參考 Perl 安裝模組 範例。

原本參考的網頁說 use LWP::Debug qw(+); 可以看到 debug message,但是我加上這一行沒反應。

環保捕蚊器 DIY (尚未實測)

Reference: http://tw.myblog.yahoo.com/jw!gie7EYWTBB8wndGhJy.uBG_c/article?mid=8722

在 Facebook 看到的,找個時間測試看看。

2012年5月2日 星期三

2012/05/02 幼稚園報名

今天去幼稚園報名,說什麼少子化,哪有啊,報名的父母是人山人海~~~~

排到125號應該擠得進去吧。