preload
Jan 21

早上7am 這個 lihotw 會為您報台北市天氣.

歡迎加入lihotw.

Tagged with:
Dec 09

If you do not specify Google Calendar URI, the event will be added to the primary calendar. So, you need to figure out which URI of calendar is the right URI you want to update.

In “Calendar Settings” -> “Calendars”, Select the calendar which you want to update. You will see the link of XML. The XML link will like below :

http://www.google.com/calendar/feeds/#KEY#%40group.calendar.google.com/public/basic

#KEY# is your secret.

Replacing the “public/basic” part with “private/full“. It will become the Google Calendar URI.

http://www.google.com/calendar/feeds/#KEY#%40group.calendar.google.com/private/full

Add the URI as the second parameter in the insertEvent function. Like below :

1
2
// $createdEntry = $gc->insertEvent($newEntry);
$createdEntry = $gc->insertEvent($newEntry, "http://www.google.com/calendar/feeds/#KEY#%40group.calendar.google.com/private/full");
Tagged with:
Dec 09

Following example is “Creating single-occurrence events” from Google web site.

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
function createEvent ($client, $title = 'Tennis with Beth',
    $desc='Meet for a quick lesson', $where = 'On the courts',
    $startDate = '2008-01-20', $startTime = '10:00',
    $endDate = '2008-01-20', $endTime = '11:00', $tzOffset = '-08')
{
  $gdataCal = new Zend_Gdata_Calendar($client);
  $newEvent = $gdataCal->newEventEntry();
 
  $newEvent->title = $gdataCal->newTitle($title);
  $newEvent->where = array($gdataCal->newWhere($where));
  $newEvent->content = $gdataCal->newContent("$desc");
 
  $when = $gdataCal->newWhen();
  $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
  $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
  $newEvent->when = array($when);
 
  // Upload the event to the calendar server
  // A copy of the event as it is recorded on the server is returned
  $createdEvent = $gdataCal->insertEvent($newEvent);
  return $createdEvent->id->text;
}
 
createEvent($client, 'New Years Party',
    'Ring in the new year with Kim and I',
    'Our house', 
    '2006-12-31', '22:00', '2007-01-01', '03:00', '-08' );

How about an all day event? From above example, I can’t simply change start time and end time fields to empty string. It will fail.

So, just modified few lines of createEvent function. Like below :

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
function createEvent ($client, $title = 'Tennis with Beth',
    $desc='Meet for a quick lesson', $where = 'On the courts',
    $startDate = '2008-01-20', $startTime = '10:00',
    $endDate = '2008-01-20', $endTime = '11:00', $tzOffset = '-08')
{
  $gdataCal = new Zend_Gdata_Calendar($client);
  $newEvent = $gdataCal->newEventEntry();
 
  $newEvent->title = $gdataCal->newTitle($title);
  $newEvent->where = array($gdataCal->newWhere($where));
  $newEvent->content = $gdataCal->newContent("$desc");
 
  $when = $gdataCal->newWhen();
 
  if ($startTime != "") {
    $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
  } else {
    $when->startTime = "{$startDate}";
  }
  if ($endTime != "") {
    $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
  } else {
   $when->endTime = "{$endDate}";
  }
  $newEntry->when = array($when);
 
  // Upload the event to the calendar server
  // A copy of the event as it is recorded on the server is returned
  $createdEvent = $gdataCal->insertEvent($newEvent);
  return $createdEvent->id->text;
}
 
createEvent($client, 'New Years Party',
    'Ring in the new year with Kim and I',
    'Our house', 
    '2006-12-31', '', '2007-01-01', '', '' );

After run it, that event will be updated on 12/31/2006 and 1/1/2007.

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 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 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:
May 18

First of all, you need to install Zend Gdata on your web server. Using following codes. Just modified at 12, 13 and 105 lines to your own. Make sure your username and password is safe. You better don’t write your password in your php codes. You can design your input form. And using https to send your password to your web 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
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
<?
/**
 * Author : Sam Tseng
 * Date : 18-MAY-2009
 *
 * Program Name : Accessing Google Calendar Demo
 * Function : Using php to add a Google Calendar event and a reminder.
 *
*/
 
// Please use your username and password at your google service.
$username = "username@gmail.com";
$password = "xxxxxxxxxxxxxxxxxxxxxx";
 
$alarm_method = "";
 
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_HttpClient');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
 
function getClientLoginHttpClient($user, $pass) {
  $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME;
 
  $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  return $client;
}
 
function createEvent($client, $title, $desc, $where,
                     $startDate, $startTime, $endDate, $endTime, $tzOffset = '+08') {
 
  $gc = new Zend_Gdata_Calendar($client);
  $newEntry = $gc->newEventEntry();
  $newEntry->title = $gc->newTitle(trim($title));
  $newEntry->where  = array($gc->newWhere($where));
 
  $newEntry->content = $gc->newContent($desc);
  $newEntry->content->type = 'text';
 
  $when = $gc->newWhen();
  $when->startTime = "{$startDate}T{$startTime}:00.000{$tzOffset}:00";
  $when->endTime = "{$endDate}T{$endTime}:00.000{$tzOffset}:00";
  $newEntry->when = array($when);
 
  $createdEntry = $gc->insertEvent($newEntry);
  return $createdEntry->id->text;
}
 
function setReminder($client, $eventId, $minutes=15) {
  $gc = new Zend_Gdata_Calendar($client);
  global $alarm_method;
  $method = $alarm_method;
  if ($event = getEvent($client, $eventId)) {
      $times = $event->when;
      foreach ($times as $when) {
          $reminder = $gc->newReminder();
          $reminder->setMinutes($minutes);
          $reminder->setMethod($method);
          $when->reminders = array($reminder);
      }
      $eventNew = $event->save();
      return $eventNew;
  } else {
      return null;
  }
}
 
function getEvent($client, $eventId) {
  $gdataCal = new Zend_Gdata_Calendar($client);
  $query = $gdataCal->newEventQuery();
  $query->setUser('default');
  $query->setVisibility('private');
  $query->setProjection('full');
  $query->setEvent($eventId);
 
  try {
      $eventEntry = $gdataCal->getCalendarEventEntry($query);
      return $eventEntry;
  } catch (Zend_Gdata_App_Exception $e) {
      var_dump($e);
      return null;
  }
}
 
function triger_event_now($method, $title, $desc, $where) {
  global $username, $password, $alarm_method;
  $alarm_method = $method;
  $client = getClientLoginHttpClient($username, $password);
  $today = date("Y-m-d");
  $now = date("H:i", mktime(date("H"), date("i") + 12, date("s"),
date("m"), date("d"), date("Y")));
  // by default, I use Taiwan timezone. You should modify your own timezone.
  $id = createEvent($client, $title, $desc,
$where, $today, $now, $today, $now, "+08");
  $id = str_replace("http://www.google.com/calendar/feeds/default/private/full/",
"", $id);
  setReminder($client, $id, 10);
}
 
// There are three type of reminders which are "sms", "email" and "alert".
// The below is using sms to send the event to your mobile. This program will delay two minutes.
triger_event_now("sms", "surf The Paradiso blog", "http://www.samtseng.liho.tw/~samtz/blog/", "Anywhere");
?>

使用 php 傳送 Google Calendar 簡訊

首先,你需要在你的網頁伺服器安裝 Zend Gdata. 更改下列程式碼 :

改成你的 Google 帳號與密碼. 你要確保這個檔案不會被其他人讀取到. 最好不要把密碼寫在程式內. 你可以設計你的表單使用 https 傳遞你的密碼到你的網頁伺服器.

11
12
13
// Please use your username and password at your google service.
$username = "username@gmail.com";
$password = "xxxxxxxxxxxxxxxxxxxxxx";

觸發事件有三種方式 “sms”, “email” and “alert”. 下面展示是用 sms 提醒. 兩分鐘後簡訊就會傳送到你的手機.

102
103
104
// There are three type of reminders which are "sms", "email" and "alert".
// The below is using sms to send the event to your mobile. This program will delay two minutes.
triger_event_now("sms", "surf The Paradiso blog", "http://www.samtseng.liho.tw/~samtz/blog/", "Anywhere");

如果有問題歡迎留言一起討論.

Tagged with:
May 09

Step 1 : Download json extensions from http://pecl.php.net/package/json
Step 2 : Extracting json-1.2.1.tgz
Step 3 : Compiling PECL json extensions by following commands

# cd json-1.2.1
# phpize
# ./configure
# make
# make install

Step 4 : Configuring php

# vi /etc/php.d/json.ini

; Enable json extension module
extension=json.so

Step 5 : Restarting httpd

# service httpd restart
Tagged with: