<?php

function tumblrpost($title, $body) {
	global $error;
	// Authorization info
	$tumblr_email    = TUMBLR_USER;
	$tumblr_password = TUMBLR_PASS;

	// Data for new record
	$post_type  = 'regular';
	$post_title = $title;
	$post_body  = $body;

	// Prepare POST request
	$request_data = http_build_query(
	    array(
	        'email'     => $tumblr_email,
	        'password'  => $tumblr_password,
	        'type'      => $post_type,
	        'title'     => $post_title,
	        'body'      => $post_body,
	        'generator' => 'Megatokyo',
			'format'	=> 'html',
			'tags'		=> 'rant',
	    )
	);

	// Send the POST request (with cURL)
	$c = curl_init('http://www.tumblr.com/api/write');
	curl_setopt($c, CURLOPT_POST, true);
	curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
	curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
	$result = curl_exec($c);
	$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
	curl_close($c);

	// Check for success
	if ($status == 201) {
	    $info .= "<p>Successfully posted to Tumblr.</p>";
		adminlog("Post to Tumblr successful. Post id $result", MTS_TUMBLR, MTA_ADD);
	} else if ($status == 403) {
	    $error .= '<p>Bad email or password posting to Tumblr.</p>';
		adminlog('Bad email or password posting to Tumblr.', MTS_TUMBLR, MTA_ADD);
	} else {
	    $error .= "<p>There was an error posting to Tumblr.</p>";
		adminlog("Error posting to Tumblr: $result", MTS_TUMBLR, MTA_ADD);
	}
}

?>
