<?php
$cookiefile = tempnam("", "reddit_");

function imageshack_post($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://www.imageshack.us/transload.php");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "url=$url");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

function imageshack_url($url) {
    $data = imageshack_post($url);
    preg_match("/\[IMG\](.*?)\[\/IMG\]/", $data, $result_match);
    $result = str_replace('.th', '', $result_match[1]);
    return $result;
}

function strip_utf($string){
    for($i = 0; $i < strlen($string); $i++) {
        if(ord($string[$i]) > 127) {
            switch(ord($string[$i])) {
                default:
                    $string[$i] = "";
                    break;
            }
        }
    }
    return $string;
}

function curl_get($url, $cookiefile) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

function login($cookiefile) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://www.reddit.com/post/login");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "op=login-main&user=&passwd=&rem=on&id=%23login_login-main");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

function hide($cookiefile, $id, $modhash) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://www.reddit.com/api/hide");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "uh=$modhash&id=$id&_=");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

function comment($cookiefile, $id, $modhash, $post) {
    $post = urlencode($post);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://www.reddit.com/api/comment");
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, "isroot=1&replace=&parent=$id&comment=$post&id=%23commentreply_$id&uh=$modhash");
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

function process_post($url, $cookiefile) {
    $posting = strip_tags(curl_get($url, $cookiefile), '<h2><div><table><td><img>');
    $posting = strip_utf($posting);
    if(strpos($posting, "This posting has been flagged for removal") || strpos($posting, "This posting has expired") || strpos($posting, "This posting has been deleted by its author") || strpos($posting, "Page Not Found")) {
        $post = "flagged";
    } else {
        preg_match("/<h2>(.*?)<\/h2>/s", $posting, $title);
        preg_match("/<div id=\"userbody\">(.*?)<table/s", $posting, $message);
        preg_match("/Date\: (.*?)\n/", $posting, $date);
        preg_match_all("/[<td>|<td align=\"center\">]<img src=\"(.*?)\"><\/td>/", $posting, $images);
        preg_match_all("/[<td>|<td align=\"center\">]<img src=\"(.*?)\" alt=\".*?\"><\/td>/", $posting, $images_alt);
        $message[1] = preg_replace("/<img src=\"(.*?)\" border=\"0\">/", "$1", $message[1]);
        $message[1] = strip_tags($message[1]);
        echo $post = "$title[1]\n\nDate: $date[1]\n\n$message[1]";

        for($i = 0; $i < count($images[1]); $i++) {
            $post_var = $images_alt[1][$i];
            $post .= "\n$post_var";
            $mirror_url = imageshack_url($post_var);
            if(!empty($mirror_url)) {
                $post .= " | [mirror]($mirror_url)";
            }
        }
        if(empty($message[1])) {
            $post = "flagged";
        }
    }
    return $post;
}

login($cookiefile);
$domain_list = array("craigslist.org", "craigslist.ca");

for($j = 0; $j < count($domain_list); $j++) {
    $data = curl_get("http://www.reddit.com/domain/$domain_list[$j]", $cookiefile);
    $data = str_replace("rel=\"nofollow\" ", "", $data);
    preg_match("/modhash\: '(.*?)'/", $data, $modhash);
    preg_match_all("/<a class=\"title loggedin \" href=\"(.*?)\" >.*?<\/a>/", $data, $listings);
    preg_match_all("/<form action=\"\/post\/(\bhide\b|\bunhide\b)\" method=\"post\" class=\"state-button (\bhide\b|\bunhide\b)-button\"><input type=\"hidden\" name=\"executed\" value=\"(\bhidden\b|\bunhidden\b)\" \/>/", $data, $hide);
    preg_match_all("/<div class=\" thing id-(.*?) (\bodd\b|\beven\b).*? linkcompressed\" >/", $data, $id_postings);

    $list_array = array();
    $hide_array = array();
    $id_array = array();
    for($i = 0; $i < count($listings[1]) && $i < 101; $i++) {
        array_push($list_array, $listings[1][$i]);
        array_push($hide_array, $hide[1][$i]);
        array_push($id_array, $id_postings[1][$i]);
    }

    for($i = 0; $i < count($list_array); $i++) {
        if($hide_array[$i] == 'hide') {
            echo "$list_array[$i] | $id_array[$i] | $hide_array[$i]<br>\n";
            hide($cookiefile, $id_array[$i], $modhash[1]);
            $temp_id = str_replace("t3_", "", $id_array[$i]);
            $comment_data = curl_get("http://www.reddit.com/comments/$temp_id", $cookiefile);
            preg_match("/modhash\: '(.*?)'/", $comment_data, $modhash_comment);
            $post = process_post($list_array[$i], $cookiefile);
            if($post != "flagged") {
                comment($cookiefile, $id_array[$i], $modhash_comment[1], $post);
                sleep(180);
            }
        }
    }
}

unlink($cookiefile);
?>