twitterでブロックしているユーザの表示、ブロック解除

修正した。

ブラックリストに加えて、NG wordリストを作成し、
NG wordを含むアカウントがブロックされている場合はブロック解除しないようにした。

それから、ブラックリストへの登録は今までは手作業であったが、
ブロックしているユーザを選択して登録できるようにした。

※ドメイン名は example.comに置き換えてある。

<?php
session_start();
require_once 'common.php';
require_once 'vendor/abraham/twitteroauth/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;
$access_token = $_SESSION['access_token'];
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$user = $connection->get("account/verify_credentials");
$blocking_users = array();
echo '<img src="' . $user->profile_image_url . '">' . ' ' . $user->name . ' is blocking following users.<br><br>';

$showdetail = 0;
$black_list = @file('blacklist.txt');
$black_list = array_map('trim', $black_list);
$ngwords = @file('ngwords.txt');
$ngwords = array_map('trim', $ngwords);
echo '<a href="https://example.com/bl/mypage.php?mode=unblock">unblock all</a><br><hr>';
echo '<a href="https://example.com/bl/mypage.php">refresh</a><hr>';
echo '<a href="https://example.com/bl/addbl.php">add blacklist</a><br><br><hr>';
echo '<a href="https://example.com/bl/mypage.php?mode=reblock">reblock users in bl</a><br><hr>';
echo '<a href="https://example.com/bl/edbl.php">edit blacklist</a><hr>';
echo '<a href="https://example.com/bl/mypage.php?mode=logout">logout</a><br>';
$blu_file = 'blocking.txt';
$req2 = $connection->OAuthRequest('https://api.twitter.com/1.1/blocks/list.json','GET',array('count'=>'100'));
$blocks = json_decode($req2,true);
if (isset($blocks) && empty($blocks->errors)) {
        echo '<hr>';
        foreach ($blocks as $val) {
                for($i = 0; $i<count($val)-1;$i++)
                {
                        $blocking_user = $val[$i]['screen_name'];
                        $is_ng = False;
                        foreach($ngwords as $ng) {
                                if(stripos($blocking_user, $ng) !== false) {
                                        $is_ng = True;
                                }
                        }
                        if ($is_ng) {
                                echo '<font size="1">* NG * </font>';
                                $showdetail = 0;
                        } elseif (in_array($blocking_user,$black_list)) {
                                echo '<font size="1">* BL * </font>';
                                $showdetail = 0;
                        } else {
                                $showdetail = 1;
                        }
                        echo
                                '<font size="1">' . $blocking_user . '  ' .$val[$i]['name'] . '   '
                                . '<font color=blue>' . $blocking_user . '</font><br>' ;
                                if ($showdetail > 0) {
                                        echo '<blockquote>' . '<img src="' . $val[$i]['profile_image_url'] . '"><br>'
                                        . $val[$i]['description'] . '<br></blockquote>';
                                        $blu_current .= $blocking_user . "\n";
                                }
                        $blocking_users_list[] = $blocking_user;
                }
        }
} else {
        echo 'you are blocking nobody.';
}
file_put_contents($blu_file, $blu_current);
if(isset($_GET['mode']))        {
        if ($_GET['mode'] == 'unblock') {
                foreach($blocking_users_list as $a)     {
                        $is_ng = False;
                        foreach($ngwords as $ng) {
                                if(stripos($a, $ng) !== false) {
                                        $is_ng = True;
                                }
                        }
                        if (in_array($a,$black_list)) {
                                echo $a . " is in blacklist.<br>";
                        } elseif($is_ng) {
                                echo $a . " contains ngwords.<br>";
                        }  else {
                                $result = $connection->OAuthRequest('https://api.twitter.com/1.1/blocks/destroy.json','POST'
                                        ,array('screen_name'=>$a));
                                $j = json_decode($result,true);
                                if (isset($j)) {
                                        echo $j['screen_name']." is unblocked.<br>";
                                }
                        }
                }
        } elseif ($_GET['mode'] == 'reblock') {
                        foreach($black_list as $a)     {
                                $result = $connection->OAuthRequest('https://api.twitter.com/1.1/blocks/create.json','POST'
                                        ,array('screen_name'=>$a));
                                $j = json_decode($result,true);
                                if (isset($j)) {
                                        echo $j['screen_name']." is blocked.<br>";
                                }
                        }
                }
        header("location:https://example.com/bl/mypage.php");
        exit();
}
if(isset($_GET['mode']))        {
        if ($_GET['mode'] == 'logout')  {
                session_destroy();
                header("location:https://example.com/bl/login.php");
        }
}
?>


ブラックリスト登録

--------------------------
<form name='form1' action = 'addbl_result.php' method='get' >
<?php
$array = file("blocking.txt");
for($i = 0 ; $i < count($array); $i++){
        echo '<input name="blocking[]" type="checkbox" value=' . $array[$i] .'>' . $array[$i] . '<br>' ;
}
?>
<input type="submit" vlaue="submit">
</form>

------------------------------

<?php
$bl_file = 'blacklist.txt';
$current = file_get_contents($bl_file);

if(empty($_GET["blocking"])){
     echo "何も選んでません";
}else{
     $blocking = $_GET["blocking"];
     foreach($blocking as $value){
#       echo $value . '<br>';
        $current .= $value. "\n";
     }
}
file_put_contents($bl_file, $current);
header("location:http://example.com/bl/mypage.php");
exit();
?>