User:Polyglot/testing/phpbot

$val) { if (!empty($reqbody)) $reqbody.= "&"; $reqbody.= $key."=".urlencode($val); }

$contentlength = strlen($reqbody); $reqheader = "POST $uri HTTP/1.1\r\n". "Host: $host\n". "User-Agent: PostIt\r\n". "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n". "Content-Length: $contentlength\r\n\r\n". "$reqbody\r\n";

$socket = fsockopen($host, 80, $errno, $errstr);

if (!$socket) { $result["errno"] = $errno; $result["errstr"] = $errstr; return $result; }

fputs($socket, $reqheader);

while (!feof($socket)) { $result[] = fgets($socket, 4096); }

fclose($socket);

return $result; }

function utf2html ($utf2html_string) { $utf2html_retstr = ""; for ($utf2html_p=0; $utf2html_p>5 == 6) { // 110x xxxx, 110 prefix for 2 bytes unicode $utf2html_p++; $utf2html_t = substr ($utf2html_string, $utf2html_p, 1); $utf2html_c2 = ord ($utf2html_t); $utf2html_c1 &= 31; // remove the 3 bit two bytes prefix $utf2html_c2 &= 63; // remove the 2 bit trailing byte prefix $utf2html_c2 |= (($utf2html_c1 & 3) << 6); // last 2 bits of c1 become first 2 of c2       $utf2html_c1 >>= 2; // c1 shifts 2 to the right $utf2html_n = dechex($utf2html_c1).dechex($utf2html_c2); $utf2html_retstr .= sprintf ("&#%03d;", hexdec($utf2html_n)); }     else{ $utf2html_retstr .= $utf2html_c; }     }    return $utf2html_retstr; }

// *********************************************************************** // Read in all the text files and inventorize the languages they contain // ***********************************************************************

$magusfile[1]="./magus/slavic1.txt"; $magusfile[2]="./magus/slavic2.txt"; $magusfile[3]="./magus/slavic3.txt"; $magusfile[4]="./magus/germanic.txt"; $magusfile[5]="./magus/romance1.txt"; $magusfile[6]="./magus/romance2.txt"; $magusfile[7]="./magus/baltic&ural-altaic.txt"; $magusfile[8]="./magus/celtic&basque.txt"; $magusfile[9]="./magus/other.txt";

foreach($magusfile as $key=>$currentword) {

$maguscontents = file($currentword); $magustext[$key] = implode(" ",$maguscontents);

$maguslanguages[$key] = $maguscontents[0]; // read first line

// echo $maguslanguages[$key]." ";

$languages = explode ("\t", $maguslanguages[$key]);

}

// *********************************************************************** // What word are we processing? // ***********************************************************************

$indexfile="./magus/index.txt"; $indexcontents = file($indexfile);

//foreach($indexcontents as $key=>$current) {

$key=0; $current=$indexcontents[0]; $currentword=trim($current); //echo $key.": "; //echo $currentword."| ";

//$textpos = strpos ($magustext[$key], $currentword); //$lineendpos = strpos($magustext[$key], chr(10), $textpos+1); //search end of line char //$textextract = substr ($magustext[$key], $textpos, $lineendpos-$textpos); //$nextchar = substr ($magustext[$key], $lineendpos +1, 1); //echo $textextract. " |" . ord($nextchar). "| ";

// ***********************************************************************   // Find this word in the different text files // ***********************************************************************

foreach($magustext as $langkey=>$text) {

//echo "text: ".$text." ";

$textpos = strpos ($text,$currentword); $lineendpos = strpos ($text,chr(10),$textpos+1); //" "

//echo "textpos: ".$textpos." lineendpos: ".$lineendpos." ";

$textextract = substr ($text, $textpos, $lineendpos-$textpos);

$words = explode ("\t", $textextract);

//echo $words[0].$words[1]. $words[2]." ";

//echo $textextract." ";

$languages = explode ("\t", $maguslanguages[$langkey]);

//echo $languages[0].$languages[1]. $languages[2]." ";

foreach($languages as $languagekey=>$language) { $word[$language] = $words[$languagekey]; //utf8_decode(

//echo "languagekey: ". $languagekey. " language: ". $language. " word: ". $word[$language]. " ";       }

//echo $languages[0]." "; //echo $languages[1]." "; //echo $languages[2]." ";

}

$entry="=== Noun ===\r\n\r\n"; $entry.="#animal\r\n\r\n"; $entry.="=== Translations ===\r\n\r\n"; $curlang="English"; $entry.="*".$curlang.": ".$word[$curlang]."\r\n"; $curlang="Latin"; $entry.="*".$curlang.": ".$word[$curlang]."\r\n"; $curlang="Dutch"; $entry.="*".$curlang.": ".$word[$curlang]."\r\n"; $curlang="Czech"; $entry.="*".$curlang.": ".$word[$curlang]."\r\n"; $curlang="Polish"; $entry.="*".$curlang.": ".$word[$curlang]."\r\n";

//echo "     ".$entry;

// $entry=utf8_decode($entry);

//echo "  ".$entry;

// phpinfo;

//}

// *********************************************************************** // Build up text area contents // ***********************************************************************

// *********************************************************************** // Get existing contents // *********************************************************************** $file1="http://wiktionary.org/w/wiki.phtml?title=User:Polyglot/testing/test1&action=edit"; $contents1 = file($file1);

$text1 = implode($contents1);

$textareainit = strpos ($text1, "", $textareainit); $textareaend = strpos ($text1, "</textarea");

$textareawpTextbox1 = substr ($text1, $textareapos+1, $textareaend - $textareapos - 1);

$textareainit = strpos ($text1, '<input type=hidden value="'); $textareapos = strpos ($text1, "name='wpEdittime'", $textareainit);

//$textareaend = strpos ($text1, "</textarea");

$textareawpEdittime = substr ($text1, $textareainit+26, $textareapos - $textareainit - 28);

// *********************************************************************** // Propose new contents merged with old contents // ***********************************************************************

$data["enctype"] = "application/x-www-form-urlencoded; charset=utf-8"; $data["wpTextbox1"] = $textareawpTextbox1. "\r\n\r\n*********************\r\n\r\n".$entry; //$textareawpEdittime; $data["wpSummary"] = "testing PHP bot I'm creating"; $data["wpMinoredit"] = "0"; //  $data["wpSave"] = "Save page"; $data["wpPreview"] = "Show preview"; $data["wpEdittime"] = $textareawpEdittime; $data["action"] = "edit";

$resulta = post_it($data, "http://wiktionary.org/wiki/User:Polyglot/testing/test1");

if (isset($resulta["errno"])) { $errno = $resulta["errno"]; $errstr = $resulta["errstr"]; echo "Error $errno $errstr"; exit; } else {

header('Content-type: text/html; charset=utf-8', false);

for($i=17;$i< count($resulta); $i++) { //  if ($i==19) echo ''."\r\n"; echo $resulta[$i]; }

}

?>