2011年12月29日 星期四

Javascript 與 PHP 傳遞跳脫 URL

前提:Magic Quotes OFF

encodeURIComponent (Javascript)
rawurldecode (PHP)


2011年12月23日 星期五

jQuery Plugin flog - Formatted Log

flog 意思為 Formatted Log

由此篇文章得到靈感。
http://blog.darkthread.net/blogs/darkthreadtw/archive/2008/12/31/jquery-logging-plugin.aspx

沒有 Firebug 時,會自動忽略此功能。
並導入了 Console.log 的 Format 字串 與 無限參數個數。
外加判斷,若第一個參數為 Format 字串 且 選擇對象為 使用者選擇之目標,則將 Format 字串後加入 ": %o" 以顯示 使用者選擇目標。

原始碼:

/*
* jQuery Formatted Log Plugin v1.0.0 @ 20111223
*
* Print formatted log to console.log with smart object display.
*
* Copyright (c) 2011 Firch Tsai
*
* Licensed under the GPL v2 licenses.
*/
jQuery.flog = jQuery.fn.flog = function(msg) {

if (typeof console != "undefined")
{
var arguments_array = [];

for (var i in arguments)
{
arguments_array[i] = arguments[i];
}

var first_arg = arguments_array.shift();

if(typeof first_arg == "string" && this.selector != $.selector)
{
first_arg += ': %o';
arguments_array.push(this);
}

arguments_array.unshift(first_arg);


var logfn = console.log;
logfn.apply(this, arguments_array);
}

return this;
};


使用範例:

item = "Book";
$.flog("This is a %s !", item);

將輸出:
This is a Book !


使用範例:

tag_name = "Links";
$('a').flog("These %s are Formatted", tag_name );

將輸出:
These Links are Formatted: +[a... , a...]


2011年11月11日 星期五

2011年11月9日 星期三

svn diff 使用 vimdiff

首先寫好 wrapper,明名為 .svndiff_to_vimdiff

#!/bin/sh

# Configure your favorite diff program here.
DIFF="/usr/local/bin/vimdiff"

# Subversion provides the paths we need as the sixth and seventh
# parameters.
LEFT=${6}
RIGHT=${7}

# Call the diff command (change the following line to make sense for
# your merge program).
$DIFF $LEFT $RIGHT

# Return an errorcode of 0 if no differences were detected, 1 if some were.
# Any other errorcode will be treated as fatal.

資料來源:http://blog.tplus1.com/index.php/2007/08/29/how-to-use-vimdiff-as-the-subversion-diff-tool/



接下將 .svndiff_to_vimdiff 放在 ~ 底下



再於 ~/.cshrc 中加入 alias

alias svndiff 'svn diff \!* --diff-cmd ~/.svndiff_to_vimdiff'



從此就可以開心的使用 svndiff 指令取代 svn diff 摟!

example:

# svndiff file_path
# svndiff -r N:M file_path


Xdebug Profiler Tigger


http://www.example.com/testScript.php?XDEBUG_PROFILE=1


2011年11月8日 星期二

SVN Diff 忽略空白


# svn diff 檔名 --diff-cmd `which diff` -x "-w"


2011年8月4日 星期四

Windows WebDav 認證錯誤解決方法


NET USE * http://servername/fs/ password /USER:username


2011年6月29日 星期三

tmux 解決 not a terminal 問題

直接下指令改目前設定

# sysctl net.inet.udp.maxdgram=65536
# sysctl net.local.stream.sendspace=8192


或者修改 /etc/sysctl.conf 將以上的設定值放進去,以後開機即自動生效。


2011年5月15日 星期日

GDB trace PHP on Apache


#gdb /usr/local/sbin/httpd
run -X


2011年5月11日 星期三

FreeBSD 移除照片 EXIF

安裝 p5-Image-ExifTool。


exiftool -all= image_file


2011年4月21日 星期四

FreeBSD 以 csup 更新 ports

設定 csup 設定檔

#cp /usr/share/examples/cvsup/ports-supfile /etc
#vim /etc/ports-supfile
將 CHANGE_THIS.FreeBSD.org 改為 FreeBSD.twaren.net


之後開始更新 ports

#csup -g -L 2 /etc/ports-supfile


完成!


2011年3月29日 星期二

FreeBSD Ramdisk 設定

建立一個 Ramdisk

#/sbin/mdmfs -M -S -o async -s 512m md0 /tmp/ramdisk
#/bin/chmod -R 777 /tmp/ramdisk


移除一個 Ramdisk

卸載

#umount /tmp/ramdisk


找出 md 代號

#ls /dev/md*
/dev/md5


釋放記憶體

#mdconfig -d -u 5


2011年3月4日 星期五

人生目標其實就是這麼一回事

「沒有人生來具有使命,我們唯一能做的,只是下定決心去假裝自己有一項使命,一直假裝到自己都信以為真。為了達成使命,他必須付出代價,刻苦自己,對自己殘酷。」by 洪堡 @ 丈量世界 - 關旭玲, 丹尼爾.凱曼


「不要可憐自己,可憐自己是卑劣懦夫干的勾當!」by 永澤 @ 挪威的森林 - 村上春樹


2011年1月13日 星期四

H.264 無法 Streaming 問題解決

問題:
H.264 的影片中的 ATOM (影片的 index) 被放在影片的最後面,因此需要將整個檔案下載完之後才能夠播放。

解決方法:
使用工具將 ATOM 移到影片的最前端,即可一邊下載一邊播放。

OpenSource 工具:
1.QTIndexSwapper (Adobe AIR application by Renaun Erickson)
2.qt-faststart (C application by Mike Melanson)


為避免 qt-faststart.c 斷頭,特別將 Source Code 貼於此。

/*
* qt-faststart.c, v0.1
* by Mike Melanson (mike at multimedia.cx)
* This file is placed in the public domain. Use the program however you
* see fit.
*
* This utility rearranges a Quicktime file such that the moov atom
* is in front of the data, thus facilitating network streaming.
*
* Compile this program using:
* cc qt-faststart.c -o qt-faststart
* Invoke the program with:
* qt-faststart
*
* Notes: Quicktime files can come in many configurations of top-level
* atoms. This utility stipulates that the very last atom in the file needs
* to be a moov atom. When given such a file, this utility will rearrange
* the top-level atoms by shifting the moov atom from the back of the file
* to the front, and patch the chunk offsets along the way. This utility
* presently only operates on uncompressed moov atoms.
*/

#include
#include
#include

#define BE_16(x) ((((uint8_t*)(x))[0] << 8) | ((uint8_t*)(x))[1])
#define BE_32(x) ((((uint8_t*)(x))[0] << 24) | \
(((uint8_t*)(x))[1] << 16) | \
(((uint8_t*)(x))[2] << 8) | \
((uint8_t*)(x))[3])
#define BE_64(x) (((uint64_t)(((uint8_t*)(x))[0]) << 56) | \
((uint64_t)(((uint8_t*)(x))[1]) << 48) | \
((uint64_t)(((uint8_t*)(x))[2]) << 40) | \
((uint64_t)(((uint8_t*)(x))[3]) << 32) | \
((uint64_t)(((uint8_t*)(x))[4]) << 24) | \
((uint64_t)(((uint8_t*)(x))[5]) << 16) | \
((uint64_t)(((uint8_t*)(x))[6]) << 8) | \
((uint64_t)((uint8_t*)(x))[7]))

#define BE_FOURCC( ch0, ch1, ch2, ch3 ) \
( (uint32_t)(unsigned char)(ch3) | \
( (uint32_t)(unsigned char)(ch2) << 8 ) | \
( (uint32_t)(unsigned char)(ch1) << 16 ) | \
( (uint32_t)(unsigned char)(ch0) << 24 ) )

#define QT_ATOM BE_FOURCC
/* top level atoms */
#define FREE_ATOM QT_ATOM('f', 'r', 'e', 'e')
#define JUNK_ATOM QT_ATOM('j', 'u', 'n', 'k')
#define MDAT_ATOM QT_ATOM('m', 'd', 'a', 't')
#define MOOV_ATOM QT_ATOM('m', 'o', 'o', 'v')
#define PNOT_ATOM QT_ATOM('p', 'n', 'o', 't')
#define SKIP_ATOM QT_ATOM('s', 'k', 'i', 'p')
#define WIDE_ATOM QT_ATOM('w', 'i', 'd', 'e')
#define PICT_ATOM QT_ATOM('P', 'I', 'C', 'T')
#define FTYP_ATOM QT_ATOM('f', 't', 'y', 'p')

#define CMOV_ATOM QT_ATOM('c', 'm', 'o', 'v')
#define STCO_ATOM QT_ATOM('s', 't', 'c', 'o')
#define CO64_ATOM QT_ATOM('c', 'o', '6', '4')

#define ATOM_PREAMBLE_SIZE 8
#define COPY_BUFFER_SIZE 1024

int main(int argc, char *argv[])
{
FILE *infile;
FILE *outfile;
unsigned char atom_bytes[ATOM_PREAMBLE_SIZE];
uint32_t atom_type = 0;
uint64_t atom_size;
uint64_t last_offset;
unsigned char *moov_atom;
uint64_t moov_atom_size;
uint64_t i, j;
uint32_t offset_count;
uint64_t current_offset;
unsigned char copy_buffer[COPY_BUFFER_SIZE];
int bytes_to_copy;

if (argc != 3) {
printf ("Usage: qt-faststart \n");
return 0;
}

infile = fopen(argv[1], "rb");
if (!infile) {
perror(argv[1]);
return 1;
}

/* traverse through the atoms in the file to make sure that 'moov' is
* at the end */
while (!feof(infile)) {
if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
break;
}
atom_size = BE_32(&atom_bytes[0]);
atom_type = BE_32(&atom_bytes[4]);

if ((atom_type != FREE_ATOM) &&
(atom_type != JUNK_ATOM) &&
(atom_type != MDAT_ATOM) &&
(atom_type != MOOV_ATOM) &&
(atom_type != PNOT_ATOM) &&
(atom_type != SKIP_ATOM) &&
(atom_type != WIDE_ATOM) &&
(atom_type != PICT_ATOM) &&
(atom_type != FTYP_ATOM)) {
printf ("encountered non-QT top-level atom (is this a Quicktime file?)\n");
break;
}

/* 64-bit special case */
if (atom_size == 1) {
if (fread(atom_bytes, ATOM_PREAMBLE_SIZE, 1, infile) != 1) {
break;
}
atom_size = BE_64(&atom_bytes[0]);
fseek(infile, atom_size - ATOM_PREAMBLE_SIZE * 2, SEEK_CUR);
} else {
fseek(infile, atom_size - ATOM_PREAMBLE_SIZE, SEEK_CUR);
}
}

if (atom_type != MOOV_ATOM) {
printf ("last atom in file was not a moov atom\n");
fclose(infile);
return 0;
}

/* moov atom was, in fact, the last atom in the chunk; load the whole
* moov atom */
fseek(infile, -atom_size, SEEK_END);
last_offset = (uint64_t)ftell(infile);
moov_atom_size = atom_size;
moov_atom = malloc(moov_atom_size);
if (!moov_atom) {
printf ("could not allocate 0x%llX byte for moov atom\n",
atom_size);
fclose(infile);
return 1;
}
if (fread(moov_atom, atom_size, 1, infile) != 1) {
perror(argv[1]);
free(moov_atom);
fclose(infile);
return 1;
}

/* this utility does not support compressed atoms yet, so disqualify
* files with compressed QT atoms */
if (BE_32(&moov_atom[12]) == CMOV_ATOM) {
printf ("this utility does not support compressed moov atoms yet\n");
free(moov_atom);
fclose(infile);
return 1;
}

/* close; will be re-opened later */
fclose(infile);

/* crawl through the moov chunk in search of stco or co64 atoms */
for (i = 4; i < moov_atom_size - 4; i++) {
atom_type = BE_32(&moov_atom[i]);
if (atom_type == STCO_ATOM) {
printf (" patching stco atom...\n");
atom_size = BE_32(&moov_atom[i - 4]);
if (i + atom_size - 4 > moov_atom_size) {
printf (" bad atom size\n");
free(moov_atom);
return 1;
}
offset_count = BE_32(&moov_atom[i + 8]);
for (j = 0; j < offset_count; j++) {
current_offset = BE_32(&moov_atom[i + 12 + j * 4]);
current_offset += moov_atom_size;
moov_atom[i + 12 + j * 4 + 0] = (current_offset >> 24) & 0xFF;
moov_atom[i + 12 + j * 4 + 1] = (current_offset >> 16) & 0xFF;
moov_atom[i + 12 + j * 4 + 2] = (current_offset >> 8) & 0xFF;
moov_atom[i + 12 + j * 4 + 3] = (current_offset >> 0) & 0xFF;
}
i += atom_size - 4;
} else if (atom_type == CO64_ATOM) {
printf (" patching co64 atom...\n");
atom_size = BE_32(&moov_atom[i - 4]);
if (i + atom_size - 4 > moov_atom_size) {
printf (" bad atom size\n");
free(moov_atom);
return 1;
}
offset_count = BE_32(&moov_atom[i + 8]);
for (j = 0; j < offset_count; j++) {
current_offset = BE_64(&moov_atom[i + 12 + j * 8]);
current_offset += moov_atom_size;
moov_atom[i + 12 + j * 8 + 0] = (current_offset >> 56) & 0xFF;
moov_atom[i + 12 + j * 8 + 1] = (current_offset >> 48) & 0xFF;
moov_atom[i + 12 + j * 8 + 2] = (current_offset >> 40) & 0xFF;
moov_atom[i + 12 + j * 8 + 3] = (current_offset >> 32) & 0xFF;
moov_atom[i + 12 + j * 8 + 4] = (current_offset >> 24) & 0xFF;
moov_atom[i + 12 + j * 8 + 5] = (current_offset >> 16) & 0xFF;
moov_atom[i + 12 + j * 8 + 6] = (current_offset >> 8) & 0xFF;
moov_atom[i + 12 + j * 8 + 7] = (current_offset >> 0) & 0xFF;
}
i += atom_size - 4;
}
}

/* re-open the input file and open the output file */
infile = fopen(argv[1], "rb");
if (!infile) {
perror(argv[1]);
free(moov_atom);
return 1;
}
outfile = fopen(argv[2], "wb");
if (!outfile) {
perror(argv[2]);
fclose(outfile);
free(moov_atom);
return 1;
}

/* dump the new moov atom */
printf (" writing moov atom...\n");
if (fwrite(moov_atom, moov_atom_size, 1, outfile) != 1) {
perror(argv[2]);
goto error_out;
}

/* copy the remainder of the infile, from offset 0 -> last_offset - 1 */
printf (" copying rest of file...\n");
while (last_offset) {
if (last_offset > COPY_BUFFER_SIZE)
bytes_to_copy = COPY_BUFFER_SIZE;
else
bytes_to_copy = last_offset;

if (fread(copy_buffer, bytes_to_copy, 1, infile) != 1) {
perror(argv[1]);
goto error_out;
}
if (fwrite(copy_buffer, bytes_to_copy, 1, outfile) != 1) {
perror(argv[2]);
goto error_out;
}

last_offset -= bytes_to_copy;
}

fclose(infile);
fclose(outfile);
free(moov_atom);

return 0;

error_out:
fclose(infile);
fclose(outfile);
free(moov_atom);
return 1;
}