屬於 「about PHP」 的文章彙整

Counterize II 2.14.1 IP patch(還原顯示IP功能)

老伯 | 十二月 15, 2009 9:29 下午

前文在這裡,裡面說得網站掛掉了,所以說我剛好做出了一份diff ptach出來。

ip_patch.patch download

使用方法:
將官方的Counterize II 2.14.1解壓縮後的資料夾和patch放在一起後,輸入

patch -p0 < ip_patch.patch

如果出現

patching file counterizeii/counterize_install.php
patching file counterizeii/counterize.php

表示完成了,這是在Linux底下,在windows的話可能需要GnuWin32或是其他對應的軟體。

Google AJAX Libraries API

老伯 | 十月 28, 2009 5:34 下午

其實今天就是在google AJAX看到的鬼東西,而且是一個非常有趣的鬼東西,依照我淺薄的接觸以為javascript Libraries只有jQuery,竟然沒想到還有這麼多的種類,

網站:http://code.google.com/intl/en/apis/ajaxlibs/

其實這讓開發者真的可以非常的方便來使用一些javascript的Libraries,如果要使用jQuery的話只要

1
2
3
4
5
6
<script src="http://www.google.com/jsapi"></script> <!--先include google js api-->
<script>
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
<!-- your js code -->
</script>

真是方便的東西...XDrz

利用php將UTF-8 -> big5 UAO沒有PATCH iconv

老伯 | 十月 6, 2009 3:43 下午

最近因為嘗試要轉換一些HTML->BBS的文章會發生的問題,所以說就寫了一個這樣的程式,因為我完全不想要動到iconv的PATCH,所以說就先利用ZTerm src中的BIG5表格下去做轉換,結果還不錯。

(修正了幾個bug 091006 21:00)

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
<?php
/*  
 *  This php is using GNU General Public License v2.0
 *  renn999<AT>ccns.ncku.edu.tw
 *  http://renn999.twbbs.org
 *
 *  USAGE:ucs2big5( $str );
 * 
 *  Big5 table(big5.txt)
 *  using from Ztrem
 *  http://zhouer.org/ZTerm/
 */


function ucs2big5($ucs_str) {
    $fp = fopen( 'big5.txt', 'r' );
    $len = strlen($ucs_str);
    unset($big5_str);
   
    $x = 0;
    for( $i = 0 ; $i < $len ; $i++ ) {
        $b1 = ord($ucs_str[$i]);
        if( $b1 < 0x80 ) {
            $big5_str[$x++] = chr($b1);
        }
        elseif( $b1 >= 224 ) {
            #3code UTF-8
            $b1 -= 224;
            $b2 = ord($ucs_str[++$i]) - 128;
            $b3 = ord($ucs_str[++$i]) - 256;
            $ucs_code = $b1 * 4096 + $b2 * 64 + $b3;
            fseek( $fp, $ucs_code * 2);
            $big5_code = fread( $fp, 2 );
            $big5_str[$x++] = $big5_code[0];
            $big5_str[$x++] = $big5_code[1];
        }
        elseif( $b1 >= 192 ) {
            #2code UTF-8
            $b1 -= 192;
            $b2 = ord($ucs_str[++$i]) - 256;
            $ucs_code = $b1 * 64 + $b2 ;
            fseek( $fp, $ucs_code * 2 );
            $big5_code = fread( $fp, 2 );
            $big5_str[$x++] = $big5_code[0];
            $big5_str[$x++] = $big5_code[1];
        }else{
            $big5_str[$x++] = '?';
        }
    }

    fclose($fp);
    if(isset($big5_str)) {
        return join( '', $big5_str);
    }
}
?>

至於BIG5.txt可以到http://zterm.googlecode.com/svn/trunk/org/zhouer/utils/conv/中去下載 這裡就不附上了

PHP備份Pixiv的bookmark(お気に入りユーザー)修正版

老伯 | 九月 30, 2009 10:18 上午

基本上之前的程式一定不能用了,但是phpQuery在備份的時候都會吐出亂碼來,可能是內部的編碼偵測出了問題。

所以基本上我就用了simple html dom但是這個有一個缺點,就是需要比較多的記憶體空間。
(這是Memory Leaks的問題已經解決)

以下為修改完的程式碼。

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
<?php
    require 'simplehtmldom/simple_html_dom.php';
       
    $loginurl='http://www.pixiv.net/index.php';
    $pixiv_id='';
    $password='';
   
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5); InfoPath.2)');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_COOKIESESSION,true);
    curl_setopt($curl, CURLOPT_URL, $loginurl);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'mode=login&pixiv_id='.$pixiv_id.'&pass='.$password );
    curl_exec($curl);
   
    $p=1;
    $bookmark_rest='show';//show
    do{
        $url='http://www.pixiv.net/bookmark.php?type=user&rest='.$bookmark_rest.'&p='.$p;
        curl_setopt($curl, CURLOPT_URL, $url);
        $profile=curl_exec($curl);
        $dom1 = new simple_html_dom();
        $dom1 = str_get_html($profile);
       
        $test=$dom1->find('div#content2 div[align="right"]',0)->plaintext;
       
        preg_match('/次の(.*?)件/',$test,$match);
                       
        $title2 = $dom1->find('div#content2 form#f div[style="width:140px;height:130px;float:left;text-align:center;"]');

        foreach($title2 as $illsthtml){
        $illst = $illsthtml->outertext;
        $illst=preg_replace('/<div(.*?)"><input(.*?)><\/div>/','',$illst);
        $illst=preg_replace('/\n/', '',$illst);
        $illst=preg_replace('/<img(.*?)>/', '',$illst);
        preg_match_all('/<a href="member.php\?id=(.*?)"><\/a><div style=\"padding-top:5px;\">(.*?)<\/div>(<a href="jump.php\?(.*?)" target="_blank"><\/a>)?/', $illst,$match2,PREG_SET_ORDER);
            foreach ($match2 as $val) {
                echo 'http://www.pixiv.net/member.php?id='.$val[1].' - '.$val[2];
                if(!empty($val[4])){echo ' - '.$val[4];}
                echo "<br />\n";
            }
        }
        $dom1->__destruct();//解決Memory Leaks的問題
        unset($profile,$dom1,$test);
        $p++;
    }while(!empty($match));
   
    curl_close($curl);
?>

PHP+phpQuery備份Pixiv的bookmark(お気に入りユーザー)

老伯 | 八月 6, 2009 11:58 下午

最近老實說又多玩了一個語言,外加我今年算是個考生,所以說很多時候晚上忙一下就已經11點多了,當然最近都在養肝,忙完就睡著了...XD

至於廢話就不多說了,最近學了一點Action Script 3.0,但是我基本上還是用Flash CS4下去學習,改天會開始去摸摸看Flex,希望是有時間就好(笑)。但是老實說在這個7月時也算是我過的最充實的一個月了,因為搞懂了非常之多的東西。

這一個PHP只能算是一個小部份,因為也是一個小程式....XD,但是基本上需要phpQuery,因為這個函式庫對於要抓取網頁資訊來說的話非常之方便而且簡單明瞭,對於一個複雜的網頁原始碼而言,可以簡化取出需要的資訊之後,再去做preg的動作繪比較的簡單。

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
<?php
    require 'phpQuery/phpQuery/phpQuery.php';
   
    $loginurl='http://www.pixiv.net/index.php';
    $pixiv_id='你的PIXIV_id';
    $password='password';
    //以上為一些登入基本資訊
   
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; (R1 1.5); InfoPath.2)');
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
    curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_COOKIESESSION,true);
    curl_setopt($curl, CURLOPT_URL, $loginurl);
    curl_setopt($curl, CURLOPT_POSTFIELDS, 'mode=login&pixiv_id='.$pixiv_id.'&pass='.$password );
    curl_exec($curl);
    //登入的動作

    $p=1;//設定起始頁
    $bookmark_rest='show';//hide為非公開 show為公開
   
    do{
        $url='http://www.pixiv.net/bookmark.php?type=user&rest='.$bookmark_rest.'&p='.$p; //bookmark網址
        curl_setopt($curl, CURLOPT_URL, $url);
        $profile=curl_exec($curl);
       
        phpQuery::newDocument($profile)->find('html > body > div#wrapper > div#pixiv > div#content2');
        $title = pq('div[align="right"] > a') -> html();
        preg_match('/次の(.*?)件/',$title,$match);
        //是否有下一頁的判斷用

        $title2 = pq('form#f > div[style="width:140px;height:120px;float:left;text-align:center;"]') -> html();
        $title2=preg_replace('/\n/', '',$title2);
        $title2=preg_replace('/<input (.*?)/>/', '',$title2);
        $title2=preg_replace('/<img (.*?)/>/', '',$title2);
        preg_match_all('/<a href="member.php\?id=(.*?)">< \/a><div style=\"padding-top:5px;\">(.*?)< \/div>/', $title2,$match2,PREG_SET_ORDER);
       
        foreach ($match2 as $val) {
            echo 'http://www.pixiv.net/member.php?id='.$val[1].' - '.$val[2]."\n";
        }//映出所有的繪師
        $p++;
    }while(!empty($match));
   
    curl_close($curl);
?>

以上就是一個非常簡單的小範例。對於熟悉PHPQuery之後要處理抓取網頁的資訊的動作的話真的非常的簡單。

這是一個非常簡單的,把PIXIV上的喜愛繪師的網址給備份出來的一個小程式,用這個列出來後才知道,原來我已經收了快要1150個繪師了...Orz

ImageUpon檔案副檔名判斷修改

老伯 | 六月 20, 2009 9:11 上午

今天趁著空閒的時候把ImageUpon的檔案格式副檔名判斷的部份小修了一下,主要的問題在於他是直接使用上傳之後的檔名去作切割,這對於一些安全上有些疑慮,假設有人上傳改附檔檔名的檔案...Orz,優點也是可以正確的去辨識檔案

修改部份:
找到

1
$suffix = strtolower(substr($_FILES['uploadimg']['name'],-4));

改成:

1
2
3
4
5
6
$img_info = getimagesize($_FILES['uploadimg']['tmp_name']);
$mime = $img_info['mime'];
list($t, $suffix) = split('/', $mime);
if ($suffix == 'jpeg') //這個部份將JPEG檔名轉成jpg
    $suffix = 'jpg';
$suffix='.'.$suffix;//加上點這樣整體架構不用去作修改

雖然說多了很多的步驟,但是隨便拿一張圖附檔亂改,都還可以正確的去作辨識,取得應有的副檔名。

如果還是要使用上傳檔案的檔名去取得副檔名的話,我想到比較好的方式還是使用preg_match會比較正統,因為畢竟副檔名不是只有三個字

1
2
preg_match('/(.*?)[.](\w+$)/u', $_FILES['uploadimg']['name'], $regs);
$suffix = '.'.strtolower($regs[2]);