preload
Jan 20
1
2
NSDictionary *playerInfo = [aNotification userInfo];
NSLog(@"%@", playerInfo);

我很好奇這個 NSDictionary 裡面裝了什麼膏藥? 就用NSLog幫忙揭開神祕的面紗 :

Album = “In the Enchanted Garden”;
“Album Rating” = 0;
“Album Rating Computed” = 1;
Artist = “Kevin Kern”;
“Artwork Count” = 1;
Genre = “New Age”;
“Library PersistentID” = “-8755280181446606464″;
Location = “file://localhost/Users/xxxxx/Music/iTunes/iTunes%20Music/Kevin%20Kern/In%20the%20Enchanted%20Garden/07%20Water%20Lilies.mp3″;
Name = “Water Lilies”;
PersistentID = 8747019994823533665;
“Play Count” = 0;
“Play Date” = “2040-02-06 06:28:16 +0800″;
“Player State” = Playing;
“Playlist PersistentID” = “-8755280181446606453″;
“Rating Computed” = 1;
“Skip Count” = 0;
“Skip Date” = “2040-02-06 06:28:16 +0800″;
“Store URL” = “itms://itunes.com/link?n=Water%20Lilies&an=Kevin%20Kern&pn=In%20the%20Enchanted%20Garden”;
“Total Time” = 257802;
“Track Count” = 10;
“Track Number” = 7;
Year = 1996;

另外, 還可以用下列方式

NSString *strOutput = [NSString stringWithFormat:@"%@", playerInfo];

NSString *strOutput = [playerInfo description];

Tagged with:
Jan 13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main(int argc, char *argv[])
{
    [NSThread detachNewThreadSelector:@selector(checkiTunesStatus) toTarget:self withObject:nil];
}
 
- (void) checkiTunesStatus
{	
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	while (pTunesRun) {
		[NSThread sleepForTimeInterval:1.0];
	}
	[pool release];
	[NSThread release];
}

這個程式在一般的Application執行沒問題. 但是寫成 widget plugin就死翹翹了. 先自己記錄起來.之後再用這個 codes.

Tagged with:
Nov 10

I’m going to use the Google Mini pdf file as this demonstration. Please download that file to the same directory of your php script in advance. Check it out here. You can see how GOOD Google did the document preview function is. Of course, I’m not as well as Google’s experts. But, I just knew a small trick.

我將使用Google Mini pdf 檔案進行這個範例解說. 請先將這個檔案下載到你的php程式相同目錄. 按這裡. , 你可以先看看 Google 的 document preview 的功能已經相當完善. 當然, 我不像 Google 的專業人士那麼厲害. 但是, 我知道一點小技巧.

Before you start to generate pdf file preview, you need to make sure your system is installed the GhostScript and the ImageMagick.

在開始產生pdf預覽前, 你先在你的系統安裝 GhostScriptImageMagick程式.

Following php script to generate thumbnails pdf files:
下列php程式碼會產生縮圖:

1
exec('/usr/bin/convert "googlemini_datasheet.pdf" -geometry 200 "googlemini_thumbnails.png"');

Output :

file name : googlemini_thumbnails-0.png


file name : googlemini_thumbnails-1.png

Following php script to generate large pdf files:
下列php程式碼會產生大圖:

1
exec('/usr/bin/convert "googlemini_datasheet.p22df" -density 1200x1200 -geometry 1024 -quality 100 "googlemini_large.png"');

Output :

file name : googlemini_large-0.png


file name : googlemini_large-1.png

From above php scripts, you can see the convert command generated two png files. Because, the Google Mini pdf file has two pages. If there are many pages in pdf file, It will be generated some file name with “-0″, “-1″, “-2″, “-3″ and so on.

從上列php程式碼, 你可以了解 convert 指令幫你產生了兩張 png 圖. 因為那個 Google Mini pdf 檔案有兩頁. 如果pdf檔案有很多頁面, 那會產生”-0″, “-1″, “-2″, “-3″…等.

If you would like to generate the first page, you can use following php script:
如果你只想產生第一頁, 你可以使用下列php程式.

1
exec('/usr/bin/convert "googlemini_datasheet.pdf[0]" -geometry 200 "googlemini_page1.png"');

Just add the [0] after the pdf file name. How about the second page? Just change inside number of [ ] from 0 to 1.

只要在pdf檔案後面加入[0]就可以了. 那如果要只輸出第二頁? 只要改變 [ ] 裡面的數子. 從 0 到 1.

References :

Tagged with:
Sep 19

建立 rrd 檔案. 然後更新溫度資訊到 rrd 檔案內 :
initial rrd file and update temperature into rrd file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// update.php
$rrd_file = "r733.rrd";
 
function init_rrd() {
    global $rrd_file;
 
    $opts = array("--step", "300", "--start", 0,
                 "DS:Battery:GAUGE:600:1:100",
                 "DS:EMU:GAUGE:600:1:100",
                 "RRA:AVERAGE:0.5:1:600",
                 "RRA:AVERAGE:0.5:6:700",
                 "RRA:AVERAGE:0.5:24:775",
                 "RRA:AVERAGE:0.5:288:797",
                 "RRA:MAX:0.5:1:600",
                 "RRA:MAX:0.5:6:700",
                 "RRA:MAX:0.5:24:775",
                 "RRA:MAX:0.5:288:797");
    $rtn = rrd_create($rrd_file, $opts, count($opts));
 
    if ($rtn == 0) {
        $err = rrd_error();
        exit("ERROR : $err\n");
    }
 
} // end function init_rrd()
 
function update_rrd() {
    global $rrd_file;
    $battery_cmd = "/usr/bin/snmpwalk -v 1 -c tiara_pub 192.168.170.250 1.3.6.1.4.1.318.1.1.1.2.2.2.0";
    $emu_cmd = "/usr/bin/snmpwalk -v 1 -c tiara_pub 192.168.170.250 1.3.6.1.4.1.318.1.1.10.2.3.2.1.4.1";
    $flag = true;
 
    while($flag) {
        $battery_temperature = "";
        $emu_temperature = "";
        while($battery_temperature == "") {
            $output = shell_exec($battery_cmd);
            $battery_temperature = trim(str_replace("SNMPv2-SMI::enterprises.318.1.1.1.2.2.2.0 = Gauge32:", "", $output));
            if ($battery_temperature == "") sleep(5);
        }
 
        while($emu_temperature == "") {
            $output = shell_exec($emu_cmd);
            $emu_temperature = trim(str_replace("SNMPv2-SMI::enterprises.318.1.1.10.2.3.2.1.4.1 = INTEGER:", "", $output));
            if ($emu_temperature == "") sleep(5);
        }
        $battery_temperature = (int)$battery_temperature;
        $emu_temperature = (int)$emu_temperature;
        $rtn = rrd_update($rrd_file, "N:$battery_temperature:$emu_temperature");
        if ($rtn == 0) {
            $err = rrd_error();
            exit("ERROR : $err\n");
        }
 
        if (isset($_GET["t"])) {
            sleep(300);
        } else {
            $flag = false;
        }
    } // end while(true)
 
} // end function update_rrd()
 
if (file_exists($rrd_file)) {
    update_rrd();
} else {
    init_rrd();
    update_rrd();
}

畫 rrd 圖 :
draw rrd graphs :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// index.php
$rrd_file = "r733.rrd";
 
include_once "update_r733.php";
 
function draw_rrd() {
    global $rrd_file;
 
    $opts = array( "--start", "-1h", "--vertical-label=deg C", "--title=R733 Temperature last hour",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );
 
    $ret = rrd_graph("r733_1h.gif", $opts, count($opts));
 
    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }
 
    $opts = array( "--start", "-1d", "--vertical-label=deg C", "--title=R733 Temperature last day",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );
 
    $ret = rrd_graph("r733_1d.gif", $opts, count($opts));
 
    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }
 
    $opts = array( "--start", "-1w", "--vertical-label=deg C", "--title=R733 Temperature last week",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );
 
    $ret = rrd_graph("r733_1w.gif", $opts, count($opts));
 
    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }
 
    $opts = array( "--start", "-1m", "--vertical-label=deg C", "--title=R733 Temperature last month",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );
 
    $ret = rrd_graph("r733_1m.gif", $opts, count($opts));
 
    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }
 
    $opts = array( "--start", "-1y", "--vertical-label=deg C", "--title=R733 Temperature last year",
                   "DEF:battery=".$rrd_file.":Battery:AVERAGE",
                   "DEF:emu=".$rrd_file.":EMU:AVERAGE",
                   "COMMENT:                                           Max    Average    Last\\n",
                   "AREA:battery#99CC00:Battery \:",
                   "GPRINT:battery:MAX:%2.0lfdeg C",
                   "GPRINT:battery:AVERAGE:%2.0lfdeg C",
                   "GPRINT:battery:LAST:%2.0lfdeg C\\r",
                   "LINE2:emu#6699CC:EMU \:",
                   "GPRINT:emu:MAX:%2.0lfdeg C",
                   "GPRINT:emu:AVERAGE:%2.0lfdeg C",
                   "GPRINT:emu:LAST:%2.0lfdeg C\\r"
               );
 
    $ret = rrd_graph("r733_1y.gif", $opts, count($opts));
 
    if(!is_array($ret)) {
        $err = rrd_error();
        exit("draw_rrd() ERROR: $err\n");
    }
} // end function draw_rrd()
 
draw_rrd();
 
?>
<img src="r733_1h.gif"><br>
<img src="r733_1d.gif"><br>
<img src="r733_1w.gif"><br>
<img src="r733_1m.gif"><br>
<img src="r733_1y.gif">

在 linux 內執行 curl http://xxx.liho.tw/temperature/update.php & 這樣就可以每 300 秒回報一次溫度.

展示畫一天的溫度的rrd圖 :

Tagged with:
Jun 11

程式碼如下 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <gtk/gtk.h>
 
static void destroy(GtkWidget *widget, gpointer data) {
    gtk_main_quit();
}
 
int main(int argc, char *argv[]) {
    GtkWidget *window;
    GtkWidget *label;
 
    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy), NULL);
    label = gtk_label_new("hello world");
    gtk_container_add(GTK_CONTAINER(window), label);
    gtk_container_set_border_width(GTK_CONTAINER(window), 15);
    gtk_window_set_title(GTK_WINDOW(window), "hello world");
    gtk_widget_show(label);
    gtk_widget_show(window);
    gtk_main();
 
    return 0;
}

這是執行結果 :

GTK+我不是那麼懂! 還是初學者.請多多指教!

參考 : GTK+ 2.0 Tutorial

Tagged with:
Jun 04

下面這個. 會把輸出儲存在$output

1
2
$cmd = "ls -l";
$output = shell_exec($cmd);

還有另外一種作法用 system function . 這個在執行過程就會直接顯示出來.

1
2
$cmd = "ls -l";
$output = system($cmd, $retval);
Tagged with:
Jun 03

Using php to create openldap sha password hash

應該很少有人有這樣的需求. 不過我就是有. 下面程式可以讓你產生 ldap sha 密碼 hash :

1
2
$passwd = "TOP_SECRET";
$ldap_passwd = "{SHA}".base64_encode(pack("H*", sha1($passwd)));
Tagged with:
Jun 03

程式目的 : 當使用者了按 checkbox 後, 才讓 delete button 啟用. 當使用者取消 checkbox 後, 將 delete button 停用.

html 程式碼如下 :

1
2
3
4
5
6
7
(omit)...
<input type="submit" id="delete" name="action" value="Delete users" disabled>
(omit)...
<input type="checkbox" name="uid[]" value="samtseng" onclick="enableAction();">
(omit)...
<input type="checkbox" name="uid[]" value="harrytseng" onclick="enableAction();">
(omit)...

用 jQuery 檢查每個 checkbox 是否被選取. 然後設定 button enabled/disabled. :)
javascript 程式碼如下 :

1
2
3
4
5
6
7
8
9
10
11
12
13
  function enableAction() {
    flag = false;
    $("input[type=checkbox][checked]").each(
      function() {
        flag = true;
      }
    );
    if (flag) {
      $("#delete").attr("disabled", false);
    } else {
      $("#delete").attr("disabled", true);
    }
  } // end function enableAction()

按這裡看DEMO.

Tagged with:
Jun 02

如何使用 php 連結 ldap server?

下面是我的程式範例 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$ldap_host = "ldaps://ldap.liho.tw";
$ldap_port = 636;
$admin = "cn=Manager, dc=liho,dc=tw";
$passwd = "XxXxXxXxXxXxXxXxXxXxXxXxXxXxXx";
 
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
 
$ds = ldap_connect($ldap_host, $ldap_port);
 
if (!$ds) {
    echo "ldap_connect() failed\n";
    exit;
}
 
if (ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)) {
   echo "Using LDAPv3\n";
} else {
   echo "Failed to set protocol version to 3\n";
}
// ldap_set_option($ds, LDAP_OPT_REFERRALS, 0);
 
// if (!@ldap_start_tls($ds)) echo "start tls failed\n";
 
if ($ds) {
    $r = ldap_bind($ds, $admin, $passwd);
    if(!$r) die("ldap_bind failed<br>");
   echo "ldap_bind success";
    ldap_close($ds);
} else {
    echo "Unable to connect to LDAP server";
}
Tagged with:
Mar 26

Codes :
<label for=”breakfast”>早餐</label>
<select id=”breakfast” name=”breakfast”>
<optgroup label=”中式”></optgroup> <option value=”c1″>饅頭</option> <option value=”c2″>燒餅</option> <option value=”c3″>油條</option>       <optgroup label=”西式”></optgroup> <option value=”w1″>鬆餅</option> <option value=”w2″>漢堡</option> <option value=”w3″>薯條</option>
</select>

Live Demo :


參考 : 10 Rare HTML Tags You Really Should Know

Tagged with: