Monday, November 23, 2009

Perl 定義ファイルの例と使い方

---定義ファイルの例
# Mysql DB Configure
DB_HOST = 'localhost'
DB_USER = 'test'
DB_PASSWORD = 'test1234'
DB_DATABASE = 'test'
----

#!/usr/bin/perl
open (CONF,"dbs.conf") || die "cannot open conf file!";
while () {
$_ =~ s/\#.*//g;
if ($_ =~ /'.*'/) {
/(\S+).*=.*\'(\S+)\'/;
${lc($1)} = $2;
}
}
close (CONF);

Wednesday, November 04, 2009

wget PHP

/*
$url = " http://192.168.1.1/test/test.php";
$www = new wget();
$but = $www->get($url,true,true, "aaa=1&www=2"); // パラメータでシングルコーテーションは'に変更する
echo $but;
*/

class wget{
const SAVE_COOKIE = "--save-cookies cookies.txt --keep-session-cookies";
const LOAD_COOKIE = "--load-cookies cookies.txt";
const OUT_FILE = "test11.text";

const DEV = " >/dev/null 2>&1 ";
const OP_TIME = "--timeout=60 --wait=5";
const SAVE_FILE = "--output-document=test11.text";
const TRIES = " -t 1 ";
const USERAGENT = '--user-agent="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"';


public function __construct(){

}
/*
post_data = "ssss=qqq&eee=1";
*/
public function get($url,$save_cookie=null,$load_cookie=null,$post_data=null){
if ($save_cookie==true){
$save_cookie = self::SAVE_COOKIE;
}else{
$save_cookie = "";
}
if ($load_cookie==true){
$load_cookie = self::LOAD_COOKIE;
}else{
$load_cookie = "";
}
if ($post_data!=null){
$post_data = "--post-data='".$post_data."'";
}

$com = "wget". self::TRIES ." $save_cookie $load_cookie ". self::OP_TIME." ". self::USERAGENT." ". $post_data." ". $url ." " .self::SAVE_FILE." " .self::DEV;
$foo = system($com,$output);
$buff = file_get_contents( self::OUT_FILE, true);
return $buff;

}
public function __destruct(){
}


}

Thursday, October 22, 2009

file_get_contentsのCookie取得のサンプル

$url = "http://hoge.com/";
$data = array();

$data['TITLE'] = "hhh";
$data['NAME'] = "aaa";

$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),

));
$content = file_get_contents($url, false, stream_context_create($options));


$nLines = count( $http_response_header ); // after file_get_contents
$match = "/Set-Cookie: JSESSIONID=(.+):/";
$cookie = "";
$matches = array();
for ( $i = 0; $i < $nLines; $i++ )
{
$line = $http_response_header[$i];
if (preg_match( $match,$line,$matches))
{
$cookie = $matches[1];
break;
}
}
echo $cookie;

curl Postデータ

$data = array(
'MAX' =>'20',
'NAME' => '田中',
'ADDRESS' => 'test@test.com',
);

$post_words=array();
foreach( $data as $key => $value ){
$post_words[] = $key. "=" .$value;
}
$post_word = implode("&", $post_words);

curl_setopt($ch,CURLOPT_POSTFIELDS,$post_word);

Wednesday, October 21, 2009

curl とCookie

$useragent = 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)';
$cookie_save_file = 'cookie_data';

$url = 'http://example.com/test.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt ($ch,CURLOPT_POST,1);
$post_word = "input=了承";
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_word);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_save_file);// 読み込まれるファイル
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_save_file);// 内容が保存される。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
$content = curl_exec($ch);
curl_close($ch);

echo $content;

Thursday, August 27, 2009

PHPでXlsファイルを作成

xml方式でxlsを作るのですが、結構役立っています。


http://www.phpclasses.org/browse/file/8061.html

Wednesday, August 26, 2009

PHP 画像を縮小

<?php


$convert = "/usr/bin/convert";

$image = '/tmp/ssss.jpg';

$newimage = '/tmp/ssss-70x70.jpg';

$option = ' -resize 70x70 ';

exec(escapeshellcmd($convert. ' ' .$option . ' ' .$image. ' ' .$newimage));

chmod ($newimage,0666);
echo "ok";
?>

Wednesday, August 19, 2009

postfix 拡張アドレス

test+111@mydomain.com

vi /etc/postfix/main.cf
に以下を追加

recipient_delimiter = +


vi .forward
~/Maildir/

Tuesday, August 18, 2009

postfixで .qmail-defaultのようなこと

[/etc/postfix/main.cf]
virtual_alias_maps = hash:/etc/postfix/virtual << 追加


書き込み

/etc/postfix/virtual:
admin@mydomain.com admin このようにしておくとadmin@mydomain.comは転送されない。
@mydomain.com forward

設定反映
postmap /etc/postfix/virtual
/etc/init.d/postfix reload

forwardユーザの.forwardにプログラムをキックする場合
vi ~forward/.forward:
"|/test/forward.pl

Wednesday, February 04, 2009

イベント監視 prototype.jsと jquery

prototype.js

window.onload = function() {
$$('a.box').invoke('observe', 'click', function(){
alert('Hello world!');
});
}

jquery.js

$(document).ready(function(){
$("a.box").click(function(){
alert('Hello world!');
});
});


<a href="#" class="box" >link</a>