<?php
//////////////////////////////////////////////////////
//
// RSS Feed Integrator
// http://www.mediawiki.org/wiki/User:Barrylb/RSS Feed Integrator
//
// You also need MagpieRSS from:
// http://magpierss.sourceforge.net/
//
///////////////////////////////////////////////////////
function decode_header ( $str )
{
$part = preg_split ( "/\r?\n/", $str, -1, PREG_SPLIT_NO_EMPTY );
$out = array ();
for ( $h = 0; $h < sizeof ( $part ); $h++ ) {
if ( $h != 0 ) {
$pos = strpos ( $part[$h], ':' );
$k = strtolower ( str_replace ( ' ', '', substr ( $part[$h], 0, $pos ) ) );
$v = trim ( substr ( $part[$h], ( $pos + 1 ) ) );
}
else {
$k = 'status';
$v = explode ( ' ', $part[$h] );
$v = $v[1];
}
if ( $k == 'set-cookie' ) {
$out['cookies'][] = $v;
}
else if ( $k == 'content-type' ) {
if ( ( $cs = strpos ( $v, ';' ) ) !== false ) {
$out[$k] = substr ( $v, 0, $cs );
}
else {
$out[$k] = $v;
}
}
else {
$out[$k] = $v;
}
}
return $out;
}
function decode_body ( $info, $str, $eol = "\r\n" )
{
$tmp = $str;
$add = strlen ( $eol );
$str = '';
if ( isset ( $info['transfer-encoding'] ) && $info['transfer-encoding'] == 'chunked' ) {
do {
$tmp = ltrim ( $tmp );
$pos = strpos ( $tmp, $eol );
$len = hexdec ( substr ( $tmp, 0, $pos ) );
if ( isset ( $info['content-encoding'] ) ) {
$str .= gzinflate ( substr ( $tmp, ( $pos + $add + 10 ), $len ) );
}
else {
$str .= substr ( $tmp, ( $pos + $add ), $len );
}
$tmp = substr ( $tmp, ( $len + $pos + $add ) );
$check = trim ( $tmp );
}
while ( ! empty ( $check ) );
}
else if ( isset ( $info['content-encoding'] ) ) {
$str = gzinflate ( substr ( $tmp, 10 ) );
}
else {
$str = $tmp;
}
return $str;
}
function HttpGet($host, $port, $send, &$info, &$result) {
if ( ( $io = fsockopen( $host, $port, $errno, $errstr, 5 ) ) !== false )
{
fputs ( $io, $send );
$send = '';
do {
$send .= fgets ( $io, 4096 );
} while ( strpos ( $send, "\r\n\r\n" ) === false );
$info = decode_header ( $send );
$send = '';
while ( ! feof ( $io ) ) {
$send .= fread ( $io, 8192 );
}
fclose ( $io );
$result = decode_body ( $info, $send );
return true;
}
else
return false;
}
function DoLogin($host, $port, $username, $password, $cookie) {
$postcontent = "wpName=" . $username . "&wpPassword=" . $password . "&wpLoginattempt=Log+in";
$send = "POST /mediawiki/index.php?title=Special:Userlogin&action=submitlogin&type=login HTTP/1.1\r\n";
$send .= "Host: " . $host . ":" . $port . "\r\n";
$send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
$send .= "Accept: text/xml,application/xml,application/xhtml+xml,";
$send .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
$send .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$send .= "Accept-Language: en-us, en;q=0.50\r\n";
$send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$send .= "Content-Type: application/x-www-form-urlencoded\r\n";
$send .= "Content-Length: " . strlen($postcontent) . "\r\n";
$send .= 'Cookie: ' . $cookie . "\r\n";
$send .= "Connection: Close\r\n\r\n";
$send .= $postcontent;
if (HttpGet($host, $port, $send, $info, $result)) {
if (strpos($result, 'You are now logged in') !== false)
return true;
else
return false;
}
else
return false;
}
function GetLoginPage($host, $port) {
$send = "GET /mediawiki/index.php?title=Special:Userlogin HTTP/1.1\r\n";
$send .= "Host: " . $host . ":" . $port . "\r\n";
$send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
$send .= "Accept: text/xml,application/xml,application/xhtml+xml,";
$send .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
$send .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$send .= "Accept-Language: en-us, en;q=0.50\r\n";
$send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$send .= "Connection: Close\r\n\r\n";
HttpGet($host, $port, $send, $info, $result);
return $info['cookies'];
}
function DoEditPage($host, $port, $title, $pagecontent, $cookie) {
$send = "GET /mediawiki/index.php?title=" . $title . "&action=edit HTTP/1.1\r\n";
$send .= "Host: " . $host . ":" . $port . "\r\n";
$send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
$send .= "Accept: text/xml,application/xml,application/xhtml+xml,";
$send .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
$send .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$send .= "Accept-Language: en-us, en;q=0.50\r\n";
$send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$send .= 'Cookie: ' . $cookie . "\r\n";
$send .= "Connection: Close\r\n\r\n";
HttpGet($host, $port, $send, $info, $result);
$wpSection = "";
preg_match('/<input [^>]*value="(.*)" name="wpStarttime"/', $result, $matches);
$wpStarttime = $matches[1];
preg_match('/<input [^>]*value="(.*)" name="wpEdittime"/', $result, $matches);
$wpEdittime = $matches[1];
$wpScrolltop = "0";
preg_match('/<textarea.*name="wpTextbox1"[^>]*>(.*)<\/textarea/s', $result, $matches);
$wpTextbox1 = htmlspecialchars_decode($matches[1]);
if ($wpTextbox1 != $pagecontent)
$wpTextbox1 = $pagecontent;
$wpSummary = "";
$wpSave = "Save page";
preg_match('/<input [^>]*value="(.*)" name="wpEditToken"/', $result, $matches);
$wpEditToken = $matches[1];
preg_match('/<input name="wpAutoSummary"[^>]*value="(.*)"/', $result, $matches);
$wpAutoSummary = $matches[1];
$boundary = "---------------------------" . md5(uniqid(microtime()));
$postcontent =
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpSection"' . "\r\n\r\n" .
$wpSection . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpStarttime"' . "\r\n\r\n" .
$wpStarttime . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpEdittime"' . "\r\n\r\n" .
$wpEdittime . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpScrolltop"' . "\r\n\r\n" .
$wpScrolltop . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpTextbox1"' . "\r\n\r\n" .
$wpTextbox1 . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpSummary"' . "\r\n\r\n" .
$wpSummary . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpSave"' . "\r\n\r\n" .
$wpSave . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpEditToken"' . "\r\n\r\n" .
$wpEditToken . "\r\n" .
'--' . $boundary . "\r\n" .
'Content-Disposition: form-data; name="wpAutoSummary"' . "\r\n\r\n" .
$wpAutoSummary . "\r\n" .
'--' . $boundary . '--' . "\r\n";
$send = "POST /mediawiki/index.php?title=" . $title . "&action=submit HTTP/1.1\r\n";
$send .= "Host: " . $host . ":" . $port . "\r\n";
$send .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
$send .= "Accept: text/xml,application/xml,application/xhtml+xml,";
$send .= "text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,";
$send .= "image/jpeg,image/gif;q=0.2,text/css,*/*;q=0.1\r\n";
$send .= "Accept-Language: en-us, en;q=0.50\r\n";
$send .= "Accept-Encoding: gzip, deflate, compress;q=0.9\r\n";
$send .= "Content-Type: multipart/form-data; boundary=" . $boundary . "\r\n";
$send .= "Content-Length: " . strlen($postcontent) . "\r\n";
$send .= 'Cookie: ' . $cookie . "\r\n";
$send .= "Connection: Close\r\n\r\n";
$send .= $postcontent;
HttpGet($host, $port, $send, $info, $result);
if (($info['status'] == '302') && ($result == ''))
return true;
else
return false;
}
////////// Settings //////////
$wikihost = '127.0.0.1';
$wikiport = 81;
$wikiusername = 'user';
$wikipassword = 'pass';
$feed_article_name = 'Slashdot_RSS';
$rss_url = 'http://rss.slashdot.org/Slashdot/slashdot';
////////////////////////////
echo '<pre>Getting RSS feed...</pre>';
require_once('magpierss/rss_fetch.inc');
define('MAGPIE_OUTPUT_ENCODING', 'UTF-8');
$rss = fetch_rss($rss_url);
$newpage =
"__NOEDITSECTION__\r\n" .
"==[[Image:Rss.gif]] " . $rss->channel['title'] . "==\r\n";
foreach ($rss->items as $item) {
$newpage .= "* [" . $item['link'] . " " . $item['title'] . "]\r\n";
}
echo '<pre>Getting wiki login page..</pre>';
$cookies = GetLoginPage($wikihost,$wikiport);
$cookie = $cookies[0];
$cookie = substr($cookie,0, strpos($cookie,';'));
echo '<pre>Posting login credentials..</pre>';
if (DoLogin($wikihost,$wikiport,$wikiusername,$wikipassword,$cookie)) {
echo '<pre>Editing page..</pre>';
if (DoEditPage($wikihost,$wikiport,$feed_article_name,$newpage,$cookie))
echo '<pre>Ok</pre>';
else
echo '<pre>Error</pre>';
}
else
echo '<pre>Failed to login</pre>';
?>