2010年4月2日 星期五

解決LOG4J MAIL中文亂碼問題 !

解決LOG4J MAIL中文亂碼問題 !

增加 DefineLayOut類別,用來繼承org.apache.log4j.HTMLLayout, 以改寫getContentType, 設定charset, 如下:
package wfc.daemon;
import org.apache.log4j.HTMLLayout; 

public class DefineLayOut extends HTMLLayout{ 
  public String getContentType() {
     return "text/html;charset=BIG5";
  }
}

XML 設定檔如下 :
<appender class="org.apache.log4j.net.SMTPAppender" name="mailAppender">
...    
<layout class="wfc.daemon.DefineLayOut">
<filter class="org.apache.log4j.varia.LevelRangeFilter">
</filter></layout></appender>
<param name="LevelMin" value="INFO" />
<param name="LevelMax" value="FATAL" />
2010年3月30日 星期二

如何使用GRUB4DOS製作隨身碟開機 !

如何使用GRUB4DOS製作隨身碟開機 !

1.使用 HP USB Disk Storage format Tool將隨身碟格式化成DOS可開機
2.將GRUB4DOS內的2個檔案複製到隨身碟根目錄 : grldr & menu.lst
3.執行 grubinst_gui.exe (GRUB4DOS GUI 1.1)
 (1)Device Name->Disk,選擇隨身碟
 (2)Device Name->Part List, 選擇隨身碟0:0C(FAT32X)
 (3)按 Install
4.完成
2010年3月17日 星期三

Perl 透過OLE寫資料至EXCEL

Perl 透過OLE寫資料至EXCEL


use OLE;
use Win32::OLE::Const "Microsoft Excel";

###################################################################################################################################

#___ DEFINE EXCEL

$excel = CreateObject OLE "Excel.Application";

#___ MAKE EXCEL VISIBLE

$excel -> {Visible} = 1;

#___ ADD NEW WORKBOOK

$workbook = $excel -> Workbooks -> Add;
$sheet = $workbook -> Worksheets("Sheet1");
$sheet -> Activate;

#___ OPEN EXISTING WORKBOOK

$workbook = $excel -> Workbooks -> Open("$file_name");
$sheet = $workbook -> Worksheets(1) -> {Name};
$sheet = $workbook -> Worksheets($sheet);
$sheet -> Activate;

#___ ACTIVATE EXISTING WORKBOOK

$excel -> Windows("Book1") -> Activate;
$workbook = $excel -> Activewindow;
$sheet = $workbook -> Activesheet;

#___ CLOSE WORKBOOK

$workbook -> Close;

#___ ADD NEW WORKSHEET

$workbook -> Worksheets -> Add({After => $workbook -> Worksheets($workbook -> Worksheets -> {Count})});

#___ CHANGE WORKSHEET NAME

$sheet -> {Name} = "Name of Worksheet";

#___ PRINT VALUE TO CELL

$sheet -> Range("A1") -> {Value} = 1234;

#___ SUM FORMULAS

$sheet -> Range("A3") -> {FormulaR1C1} = "=SUM(R[-2]C:R[-1]C)"; # Sum rows
$sheet -> Range("C1") -> {FormulaR1C1} = "=SUM(RC[-2]:RC[-1])"; # Sum columns

#___ RETRIEVE VALUE FROM CELL

$data = $sheet -> Range("G7") -> {Value};

#___ FORMAT TEXT

$sheet -> Range("G7:H7") -> Font -> {Bold} = "True";
$sheet -> Range("G7:H7") -> Font -> {Italic} = "True";
$sheet -> Range("G7:H7") -> Font -> {Underline} = xlUnderlineStyleSingle;
$sheet -> Range("G7:H7") -> Font -> {Size} = 8;
$sheet -> Range("G7:H7") -> Font -> {Name} = "Arial";
$sheet -> Range("G7:H7") -> Font -> {ColorIndex} = 4;

$sheet -> Range("G7:H7") -> {NumberFormat} = "\@"; # Text
$sheet -> Range("A1:H7") -> {NumberFormat} = "\$#,##0.00"; # Currency
$sheet -> Range("G7:H7") -> {NumberFormat} = "\$#,##0.00_);[Red](\$#,##0.00)"; # Currency - red negatives
$sheet -> Range("G7:H7") -> {NumberFormat} = "0.00_);[Red](0.00)"; # Numbers with decimals
$sheet -> Range("G7:H7") -> {NumberFormat} = "#,##0"; # Numbers with commas
$sheet -> Range("G7:H7") -> {NumberFormat} = "#,##0_);[Red](#,##0)"; # Numbers with commas - red negatives
$sheet -> Range("G7:H7") -> {NumberFormat} = "0.00%"; # Percents
$sheet -> Range("G7:H7") -> {NumberFormat} = "m/d/yyyy" # Dates

#___ ALIGN TEXT

$sheet -> Range("G7:H7") -> {HorizontalAlignment} = xlHAlignCenter; # Center text;
$sheet -> Range("A1:A2") -> {Orientation} = 90; # Rotate text

#___ SET COLUMN WIDTH/ROW HEIGHT

$sheet -> Range('A:A') -> {ColumnWidth} = 9.14;
$sheet -> Range("8:8") -> {RowHeight} = 30;
$sheet -> Range("G:H") -> {Columns} -> Autofit;

#___ FIND LAST ROW/COLUMN WITH DATA

$last_row = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByRows}) -> {Row};
$last_col = $sheet -> UsedRange -> Find({What => "*", SearchDirection => xlPrevious, SearchOrder => xlByColumns}) -> {Column};

#___ ADD BORDERS

$sheet -> Range("A3:I3") -> Borders(xlEdgeBottom) -> {LineStyle} = xlDouble;
$sheet -> Range("A3:I3") -> Borders(xlEdgeBottom) -> {Weight} = xlThick;
$sheet -> Range("A3:I3") -> Borders(xlEdgeBottom) -> {ColorIndex} = 1;
$sheet -> Range("A3:I3") -> Borders(xlEdgeLeft) -> {LineStyle} = xlContinuous;
$sheet -> Range("A3:I3") -> Borders(xlEdgeLeft) -> {Weight} = xlThin;
$sheet -> Range("A3:I3") -> Borders(xlEdgeTop) -> {LineStyle} = xlContinuous;
$sheet -> Range("A3:I3") -> Borders(xlEdgeTop) -> {Weight} = xlThin;
$sheet -> Range("A3:I3") -> Borders(xlEdgeBottom) -> {LineStyle} = xlContinuous;
$sheet -> Range("A3:I3") -> Borders(xlEdgeBottom) -> {Weight} = xlThin;
$sheet -> Range("A3:I3") -> Borders(xlEdgeRight) -> {LineStyle} = xlContinuous;
$sheet -> Range("A3:I3") -> Borders(xlEdgeRight) -> {Weight} = xlThin;
$sheet -> Range("A3:I3") -> Borders(xlInsideVertical) -> {LineStyle} = xlContinuous;
$sheet -> Range("A3:I3") -> Borders(xlInsideVertical) -> {Weight} = xlThin;
$sheet -> Range("A3:I3") -> Borders(xlInsideHorizontal) -> {LineStyle} = xlContinuous;
$sheet -> Range("A3:I3") -> Borders(xlInsideHorizontal) -> {Weight} = xlThin;

#___ PRINT SETUP

$sheet -> PageSetup -> {Orientation} = xlLandscape;
$sheet -> PageSetup -> {Order} = xlOverThenDown;
$sheet -> PageSetup -> {LeftMargin} = .25;
$sheet -> PageSetup -> {RightMargin} = .25;
$sheet -> PageSetup -> {BottomMargin} = .5;
$sheet -> PageSetup -> {CenterFooter} = "Page &P of &N";
$sheet -> PageSetup -> {RightFooter} = "Page &P of &N";
$sheet -> PageSetup -> {LeftFooter} = "Left\nFooter";
$sheet -> PageSetup -> {Zoom} = 75;
$sheet -> PageSetup -> FitToPagesWide = 1;
$sheet -> PageSetup -> FitToPagesTall = 1;

#___ ADD PAGE BREAK

$excel -> ActiveWindow -> SelectedSheets -> HPageBreaks -> Add({Before => $sheet -> Range("3:3")});

#___ HIDE COLUMNS

$sheet -> Range("G:H") -> EntireColumn -> {Hidden} = "True";

#___ MERGE CELLS

$sheet -> Range("H10:J10") -> Merge;

#___ INSERT PICTURE

$sheet -> Pictures -> Insert("picture_name"); # Insert in upper-left corner
$excel -> ActiveSheet -> Pictures -> Insert("picture_name"); # Insert in active cell

#___ GROUP ROWS

$sheet -> Range("7:8") -> Group;

#___ ACTIVATE CELL

$sheet -> Range("A2") -> Activate;

#___ FREEZE PANES

$excel -> ActiveWindow -> {FreezePanes} = "True";

#___ DELETE SHEET

$sheet -> Delete;

#___ SAVE AND QUIT

$excel -> {DisplayAlerts} = 0; # This turns off the "This file already exists" message.
$workbook -> SaveAs ("C:\\file_name.xls");
$excel -> Quit;

---------------------------------------------------------------------------------

use Win32::OLE;
use Win32::OLE qw(in with);
use Win32::OLE::Variant;
use Win32::OLE::Const 'Microsoft Excel';

$Excel = Win32::OLE->GetActiveObject('Excel.Application') ||
Win32::OLE->new('Excel.Application');
$Excel->{'Visible'} = 0; #0 is hidden, 1 is visible
$Excel->{DisplayAlerts}=0; #0 is hide alerts

# Open File and Worksheet
my $Book = $Excel->Workbooks->Open
('C:\report\results\check_all.csv'); # open Excel file
$Sheet = $Book->Worksheets(1);

# Create New Workbook
$Excel->{SheetsInNewWorkBook} = 1;
$Book2 = $Excel->Workbooks->Add();
$Sheet2 = $Book2->Worksheets(1);
$Sheet2->{Name} = 'My test worksheet';

# Find Last Column and Row
my $LastRow = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,
SearchOrder=>xlByRows})->{Row};

my $LastCol = $Sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,
SearchOrder=>xlByColumns})->{Column};
$mylastcol = a;
for (my $m=1;$m<$LastCol;$m++){$mylastcol++;}
$mylastcol2 = a;
for (my $m=1;$m<($LastCol - 1);$m++){$mylastcol2++;}

# Draw Borders
my @edges = qw (xlInsideHorizontal xlInsideVertical);
$range = "a1:$mylastcol$LastRow";
foreach my $edge (@edges)
{
with (my $Borders = $Sheet->Range($range)->Borders(eval($edge)),
LineStyle =>xlContinuous,
Weight => xlThin ,
ColorIndex => 1);
}

# Cell Values
$Sheet->Range('a1')->{Value} = $Sheet->Range('b2')->{Value};

# Resize Columns
my @columnheaders = qw(A:B);
foreach my $range(@columnheaders){
$Sheet->Columns($range)->AutoFit();
}
# Order Rows
$tmp = "$mylastcol2".'3';
$Rangea = $Sheet->Range("$tmp");
$Rangeb = $Sheet->Range("a3");
$Excel->Selection->Sort({Key1 => $Rangea,
Order1 => xlDescending,
Key2 => $Rangeb});

# Merge Cells
$mynextcol = 'b';
for (my $n=1;$n<$LastCol;$n+=2)
{
my $range = $mynextcol++ . '1:' . $mynextcol++ . '1';
$Sheet->Range($range)->Merge();
$Sheet->Range($range)->{HorizontalAlignment} = xlHAlignCenter;
}

# Pie Chart
my $Range = $Sheet->Range('a1:d2');
my $Chart = $Book->Charts->Add;
$Chart->ChartWizard($Range,xl3DPie,7,xlRows,1,0,2,"Sales Percentages");

# Bar Graph and Rotate
my $Range = $Sheet->Range('a1:a3');
my $Chart = $Excel->Charts->Add;
$Chart->{Type} = xl3DColumn;
for (my $i = 30; $i <=180; $i+=10)
{
$Chart->{Rotation} = $i;
sleep(1);
}

# Line Chart and Save
my $Range = $Sheet->Range('a1:d2');
my $Chart = $Excel->Charts->Add;
$Chart->{ChartType} = xlLine;
$Chart->SetSourceData({Source => $Range, PlotBy => xlColumns});
$Chart->{HasTitle} = 1;
$Chart->ChartTitle->{Text} = "Some Title";

my $ChartObj = $Sheet->ChartObjects;

$Chart->Export({
FileName => $filegraphname,
FilterName => 'GIF',
Interactive => 0});

# Save as PDF
$Excel->ActiveWindow->SelectedSheets->PrintOut({
Copy => 1,
ActivePrinter => 'Acrobat PDFWriter'});

# Save as Excel
$Book->SaveAs({Filename =>'C:\report\results\check_all.xls',
FileFormat => xlWorkbookNormal});
$Book->Close();
$Excel->Quit();

SQL SERVER 2005 啟用遠端登入

SQL SERVER 2005 啟用遠端登入

1. SQL SERVER 2005 介面區組態 - 服務程連接的介面區組態
2. SQL SERVER Configuration Manager - TCP/IP程具名管道啟用, IP設定
3. SQL SERVER Browser啟用
3. 伺服器設定SQL Server及Windows驗証模式
4. 啟用sa登入

Windows 7 安裝RMClock會造成小於1分鐘就會待機問題

Windows 7 安裝RMClock會造成小於1分鐘就會待機問題

解決方式是修改RMClock的設定,如下 :
RMClock --> Management --> Enable OS power management integration不要打勾

Drupal 顯示固定字數函數

Drupal 顯示固定字數函數

truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)

重設 SELinux 安全性本文

重設 SELinux 安全性本文

cd /home
chcon -R 2handbook -t httpd_sys_content_t 2handbook
ll -Z

如何解決Drupal6中文檔名上傳問題

如何解決Drupal6中文檔名上傳問題
Re: 解決中文檔名上傳/下載會出現錯誤 的問題可以參考下面file.inc的修改方式, 測試過是OK的.
http://drupalchina.org/node/5292
file.inc修改兩個地方就可以
第一個地方,管DB文件名字的
原本是: $file->filename = file_munge_filename(trim(basename($_FILES['files']['name'][$source]), '.'), $extensions);
修改後:$file->filename = file_munge_filename(trim($_FILES['files']['name'][$source], '.'), $extensions);
第二個地方,管上傳到Server的文件名稱
原本是:if (!move_uploaded_file($_FILES['files']['tmp_name'][$source], $file->filepath))
修改後:if (!move_uploaded_file($_FILES['files']['tmp_name'][$source],iconv("UTF-8","BIG-5",$file->filepath)))
修改檔案在includes/file.inc約在528行左右
在LINUX下實際測試,只要更改第一個地方即可,但如果裝在WINDOWS下,兩個地方都要修正

維護GIT程式庫

維護GIT程式庫

git fsck
git gc

活動管理系統,活動誤刪還原作業

活動管理系統,活動誤刪還原作業

1. 將昨日的mysql備份,倒回我的測試用電腦
2. 假設被誤刪的nid=317,最後將以下TABLE刪除不是nid=317的資料
(例如 : DELETE FROM node WHERE nid<>317)
content_type_webform
event
node_revisions
node
webform_component
webform_roles
webform
webform_submissions
webform_submitted_data
term_node
3.將上面第2點所列的TABLE,匯出成以下SQL檔
(例如 : 使用SQLyog->點選該node table->滑鼠右鍵->Backup/Export->Export Table Data AS->選SQL,)
content_type_webform.sql
event.sql
node_revisions.sql
node.sql
webform_component.sql
webform_roles.sql
webform.sql
webform_submissions.sql
webform_submitted_data.sql
term_node.sql
4.上傳至SERVER上,將上面第3點所列的SQL檔,匯入至活動報名系統資料庫中
(例如 : mysql -u signupuser -p signupdb < node.sql)

Mysql 增加max_connections數量

Mysql 增加max_connections數量

檢視目前max_connections設定值
mysql -u root -p
mysql> show variables like 'max_connections';


修改 /etc/my.cnf, 增加下列一行
set-variable = max_connections=512

VSFTP使用SSL連線設定

SFTP使用SSL連線設定
首先要產生合適的 ssl certificate:
openssl req -new -x509 -nodes -out vsftpd.pem -keyout vsftpd.pem
Copy "vsftpd.pem" to your vsftpd directory:
cp vsftpd.pem /etc/vsftpd
Add the following to vsftpd.conf:

# Turn on SSL
ssl_enable=YES

# Allow anonymous users to use secured SSL connections
allow_anon_ssl=YES

# All non-anonymous logins are forced to use a secure SSL connection in order to
# send and receive data on data connections.
force_local_data_ssl=YES

# All non-anonymous logins are forced to use a secure SSL connection in order to send the password.
force_local_logins_ssl=YES

# Permit TLS v1 protocol connections. TLS v1 connections are preferred
ssl_tlsv1=YES

# Permit SSL v2 protocol connections. TLS v1 connections are preferred
ssl_sslv2=YES

# permit SSL v3 protocol connections. TLS v1 connections are preferred
ssl_sslv3=NO

# Specifies the location of the RSA certificate to use for SSL encrypted connections
rsa_cert_file=/etc/vsftpd/vsftpd.pem

FTP上下傳檔案,如何能保留原有的時間戳記 ?

FTP上下傳檔案,如何能保留原有的時間戳記 ?

[上傳]
上傳時保留本地檔案時間戳記, 跟 MFMT or SITE UTIME command有關, ServU應該是有支援SITE UTIME(所以以前備份時, 時間戳記都能保留), 但FileZilla Server並不支援SITE UTIME, 不過FileZilla Server有支援MFMT command, 目前看起來支援MFMT應該是主流

要保留時間戳記,必須Client 跟 Server都有支援(MFMT or SITE UTIME command)才行 !

目前 FileZilla Client有支援MFMT, 而 Ncftp要 3.2.3才有支援 MFMT

[下載]
上載檔案時, 要保留伺服器上的時間戳記, 跟MDTM command有關

每日自動備份, 檔名如何加上日期 ?

每日自動備份, 檔名如何加上日期 ?

如何mv檔案, 依據原檔案來取新的名稱 !
ls -d *_0.txt | sed 's/\(.*\)_0.txt$/mv "&" "\1_1.txt"/' | sh

取得日期
echo $(date +%Y-%m-%d)

例子 :

#!/bin/bash

BACKUPDIR=/home/jeffrey/backup/rfid/etc
today=$(date +%Y-%m-%d)
cd $BACKUPDIR
days=4
for ((i=$days-1 ; i>=1 ; i=i-1))
do
declare -i j=$i-1
rm -f rfid_etc_????-??-??_$i.tar.bz2
for f in `ls rfid_etc_????-??-??_$j.tar.bz2`
do
echo $f | sed "s/\(.*\)_$j.tar.bz2$/mv '&' '\1_$i.tar.bz2'/" | sh
done
#mv rfid_etc_$j.tar.bz2 rfid_etc_$i.tar.bz2 2>> rfid_etc_backup_err.log
done


cd /

tar jcf $BACKUPDIR/rfid_etc_"$today"_0.tar.bz2 etc


做到這裏,有個問題 : ftp上傳時, 以前的舊檔無法覆蓋, 這會是個大問題

將舊DRUPAL網站, 複製一份, 但資料庫要清空, 會造成空白首頁問題 !

將舊DRUPAL網站, 複製一份, 但資料庫要清空, 會造成空白首頁問題 !
問題關鍵在於setting.php,
解決方法是, 將資料庫連結url, 改為
1. 建立新的database
2.將setting.php內的$db_url改為$db_url='mysqli://username:password@localhost/databasename';
讓Drupal重新建立新的資料表