<?php
// twitterOAuth.phpを読み込む。パスはあなたが置いた適切な場所に変更してください
require_once("twitterOAuth.php");

//buzztterのrssを取得
$host = "http://twitter.com/statuses/user_timeline/5567702.rss";
// Consumer keyの値
$consumer_key = "dJ3iu6H3zvHdXAKztmxwA";
// Consumer secretの値
$consumer_secret = "fYgZlYfFRUlDqR9B5mQuqSADMxBE92g94fNNdokQ";
// Access Tokenの値
$access_token = "79846719-N7YScl8UB8G7rPt3wkApBcexuojmZ0K0dBh8MZ94";
// Access Token Secretの値
$access_token_secret = "NcwHQiBJpYLQoOTwlch3KyLzthmyDHHZT9WcTjfQ7Q";
// OAuthオブジェクト生成
$to = new TwitterOAuth($consumer_key,$consumer_secret,$access_token,$access_token_secret);
$filename ="./tmp/last.dat";

//過去の開いて取得
$fp = @fopen($filename,'rb') or die("ファイルが開けません");
flock($fp, LOCK_EX);
$line = fgets($fp, 64);
fclose($fp); 

//自分がフォローしているfriendsのリストです。
$req = $to->OAuthRequest("http://twitter.com/statuses/friends_timeline.xml",array("count"=>"50"),"GET");
$XML = simplexml_load_string($req);

//一番最初の時やファイルが壊れた時、最新1件のみ発言を取得する
if(!empty($line)){
$last_id=$line;
$host .="?since_id=".$last_id;
}
else{
    $host .="?count=1";
    }

$req = $to->OAuthRequest("http://twitter.com/statuses/friends_timeline.xml",array("count"=>"50"),"GET");
$XML = simplexml_load_string($req);
$i = count($XML)-1;
$j = 0;
 while($i >= $j) {
$var =  $XML->status[$i]->text;    
$reply_name = $XML->status[$i]->user->screen_name;
$name = $XML->status[$i]->user->name;

$men = $to->OAuthRequest("http://twitter.com/statuses/mentions.xml",array("count"=>"30"),"GET");
$XML = simplexml_load_string($men);

$i = count($XML)-1;
$j = 0;
 while($i >= $j) {
$var =  $XML->status[$i]->text;    
$reply_name = $XML->status[$i]->user->screen_name;
$name = $XML->status[$i]->user->name;

$xyz=array("おはようー","おはもふもふ","おはようございます");

if($reply_name != $username){
if( !preg_match ( "/@[a-zA-Z0-9-_]{3,}/", $var ) ){

if(preg_match( "/おき|起き|むっくり|むくり|mkr|おは/u", $var )){
$key = array_rand($xyz);
$post = $xyz[$key];
$message = "@".$reply_name." ".$post;
tweet($message ,$to);
}


$abc=array("おつかれー","おつかれさま","おつかれさまでした");

if($reply_name != $username){
if( !preg_match ( "/@[a-zA-Z0-9-_]{3,}/", $var ) ){

if(preg_match( "/退|帰る|帰路/u", $var )){
$key = array_rand($abc);
$post = $abc[$key];
$message = "@".$reply_name." ".$post;
tweet($message ,$to);
}


$def=array("おかえり","おかえりなさい");

if($reply_name != $username){
if( !preg_match ( "/@[a-zA-Z0-9-_]{3,}/", $var ) ){

if(preg_match( "/ただいま|帰宅|かえって|かえり|復帰/u", $var )){
$key = array_rand($def);
$post = $def[$key];
$message = "@".$reply_name." ".$post;
tweet($message ,$to);
}

}
}
$i--;
}

//ファイルに最新id書き込み
$last_id = $XML->status[0]->id;
$dat = (string)$last_id;
file_put_contents($filename,$dat,LOCK_EX);

//ポスト部分の関数
$req = $to->OAuthRequest("https://twitter.com/statuses/update.xml",array("status"=>"$post"),"POST");
header("Content-Type: application/xml");
function tweet($message ,$to)

$men = $to->OAuthRequest("http://twitter.com/statuses/mentions.xml",array("count"=>"30"),"GET");
$XML = simplexml_load_string($men);

?>