Created
July 26, 2010 16:41
-
-
Save rmasters/490807 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FourChanPoster | |
{ | |
const VERSION = "1.0"; | |
public $name; | |
public $email; | |
public $password; | |
public function post($board, $subject = "", $comment, $imagePath) { | |
return $this->request(array( | |
"board" => $board, | |
"sub" => $subject, | |
"com" => $comment, | |
"upfile" => "@$imagePath" | |
)); | |
} | |
public function reply($thread, $board, $subject = "", $comment, $imagePath = null) { | |
return $this->request(array( | |
"board" => $board, | |
"sub" => $subject, | |
"com" => $comment, | |
"upfile" => ($imagePath) ? "@$imagePath" : null, | |
"resto" => $thread | |
)); | |
} | |
protected function request(array $options) { | |
// Add required options | |
$reqOptions = array( | |
"MAX_FILE_SIZE" => 2097152, | |
"submit" => "Submit", | |
"mode" => "regist", | |
"pwd" => $this->password, | |
"name" => $this->name, | |
"email" => $this->email | |
); | |
$options = array_merge($options, $reqOptions); | |
// Setup curl | |
$ch = curl_init(); | |
curl_setopt_array($ch, array( | |
CURLOPT_URL => "http://sys.4chan.org/{$options["board"]}/post", | |
CURLOPT_POST => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_USERAGENT => "4chanPoster (" . self::VERSION . ")", | |
CURLOPT_POSTFIELDS => $options | |
)); | |
// Execute and check for error/success | |
$result = curl_exec($ch); | |
return $this->checkResult($result); | |
} | |
/** | |
* Check the resulting page - if it returns a thread id the post has been | |
* successful. | |
* @param string Result page html | |
* @return int|bool False if we ran into an error, otherwise the id of the thread we posted to | |
* @todo Detecting the exact error reason | |
*/ | |
protected function checkResult($result) { | |
// Get the comment containing the thread id | |
$check = substr($result, | |
strpos($result, "<td><b>"), | |
strpos($result, "</b></td>") | |
); | |
// If the comment is there, return the thread id | |
if ($check) { | |
$regex = "/<!-- thread:[0-9]+,no:([0-9]+) -->/"; | |
if(preg_match($regex, $clean, $matches) == 1) { | |
return $matches[1]; | |
} | |
} | |
return false; | |
} | |
/** | |
* Generate an image containing a text string and a timestamp | |
* This is useful for generating a unique image to test | |
* @param string Text to show | |
* @param string Directory where images are stored | |
* @return string Image path | |
* @todo This is temporarily broken (bbox is strange) | |
* | |
public static function generateTestFile($dir = "~/", $text = "BURRRRRRRRRR") { | |
$font = "/usr/share/fonts/truetype/luxi-sans/luxisr.ttf"; | |
$text = $text . "\n" . date("U"); | |
// Calculate the size of the resulting text | |
$bbox = imagettfbbox(12, 0, $font, $text); | |
// Create the image | |
$spacing = 10; | |
$bbox[0] = ($bbox[0] < 0) ? abs($bbox[0]) : $bbox[0]; | |
$bbox[1] = ($bbox[1] < 0) ? abs($bbox[1]) : $bbox[1]; | |
$bbox[2] = ($bbox[2] < 0) ? abs($bbox[2]) : $bbox[2]; | |
$bbox[7] = ($bbox[7] < 0) ? abs($bbox[7]) : $bbox[7]; | |
$x = $bbox[2] - $bbox[0] + $spacing; | |
$y = $bbox[3] - $bbox[5] + $spacing; | |
var_dump($x, $y); | |
$image = imagecreatetruecolor($x, $y); | |
$white = imagecolorallocate($image, 255, 255, 255); | |
imagettftext($image, 12, 0, $spacing, $spacing, $white, $font, $text); | |
$path = $dir . date("U") . ".png"; | |
imagepng($image, $path); | |
imagedestroy($image); | |
return $path; | |
}*/ | |
} | |
/** | |
* A NOTE BEFORE USING | |
* 4chan has flood control - meaning if you try to send many posts quickly they'll just fail. | |
* Allow for plenty of time when posting, I have no idea if there are measures in place to | |
* automatically ban after X flood-bounced posts. | |
*/ | |
$poster = new FourChanPoster; | |
$poster->email = "noko"; | |
$poster->password = "testing"; | |
$im = FourChanPoster::generateTestFile("/home/ross/testimages/"); | |
$thread = $poster->post("b", "test", "test", "$im"); | |
$poster->post($thread, "b", "testing again", "lol"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment