User:BMO/src

From Encyclopedia Dramatica
Jump to navigationJump to search

<?php
error_reporting(E_ALL | E_STRICT);
$COMMON_GET_CONTENTS = 0;
$COMMON_NEWLINE = 0;
function alert ($file, $fn, $line, $alert = '') {
	global $COMMON_NEWLINE;
	if ($COMMON_NEWLINE) {
		print ("\n");
	}
	printf ("%s %s %s\n", $file, $fn, $line, $alert);
	$COMMON_NEWLINE = 0;
	return false;
}
function msg ($msg) {
	global $COMMON_NEWLINE;
	$COMMON_NEWLINE = 1;
	printf ('%s ', $msg);
}
function dump_array ($array, $filename = 'tmp.txt') {
	ob_start ();
	print_r ($array);
	file_put_contents ($filename, ob_get_clean ());
}
function xml_get_array ($xml) {
    $array = array();
    if ($xml->hasAttributes ()) {
        foreach ($xml->attributes as $a) {
            $array['@attributes'][$a->name] = $a->value;
        }
    }
    if ($xml->hasChildNodes()) {
        $nodes = $xml->childNodes;
        if ($nodes->length == 1) {
            $node = $nodes->item(0);
            if ($node->nodeType == XML_TEXT_NODE) {
                $array['_value'] = $node->nodeValue;
                return count($array) == 1 ? $array['_value'] : $array;
            }
        }
        $groups = array();
        foreach ($nodes as $node) {
            if (!isset ($array[$node->nodeName])) {
                $array[$node->nodeName] = xml_get_array ($node);
            } else {
                if (!isset ($groups[$node->nodeName])) {
                    $array[$node->nodeName] = array ($array[$node->nodeName]);
                    $groups[$node->nodeName] = 1;
                }
                $array[$node->nodeName][] = xml_get_array ($node);
            }
        }
    }
    return $array;
}
function append_array ($source_array, $values_array, $path_array, &$append_array) {
	$array = $source_array;
	foreach ($path_array as $value) {
		if (isset ($array[$value]) === false) {
			$array = array ();
			break;
		} else {
			$array = $array[$value];
		}
	}
	if (count ($values_array) == 0) {
		$append_array = $array;
		return true;
	}
	if (count ($array)) {
		foreach ($values_array as $value) {
			if (isset ($array[$value]) == true) {
				$append_array[$value] = $array[$value];
			}
		}
		return true;
	} else {
		return false;
	}
}
function wikiput_cookie ($cookieprefix, $sessionid, &$cookie) {
	$cookie = "Cookie: " . $cookieprefix . "_session=$sessionid\r\n";
}
function wikiget_contents ($cookie, $post_array, $format = 'xml') {
	sleep (.5);
	msg ('.');
	global $COMMON_GET_CONTENTS;
	$COMMON_GET_CONTENTS++;
	$stream_context = stream_context_create (array(
		'http' => array(
			'header'  => "Content-type: application/x-www-form-urlencoded\r\n" . $cookie,
			'method'  => 'POST',
			'content' => http_build_query (array_merge (array ('format' => $format), $post_array)),
			'timeout' => 10,
		),
	));
	if ($stream_context === false) {
		return false;
	} else {
		$file_contents = file_get_contents (MEDIAWIKI_API, false, $stream_context);
		if ($file_contents === false) {
			return false;
		} else {
			if ($format == 'xml') {
				try {
					$xml = new DOMDocument ();
					$xml->loadXML ($file_contents);
				} catch (Exception $e) {
					return false;
				}
				return xml_get_array ($xml);
			} else {
				return $file_contents;
			}
		}
	}
}
function wikiput_logintoken (&$cookie, $username, $pwd, &$logintoken) {
	msg (chr (10) . 'login');
	$logintoken = '';
	$logintoken_array = array ();
	$xml_contents_array = wikiget_contents ('', array (
		'action' => 'login',
		'lgname' => $username,
		'lgpassword' => $pwd
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array ('token', 'cookieprefix', 'sessionid', 'result'), array ('api', 'login', '@attributes'), $logintoken_array) == false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	wikiput_cookie ($logintoken_array ['cookieprefix'], $logintoken_array ['sessionid'], $cookie);
	if ($logintoken_array ['result'] != 'NeedToken') {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	$xml_contents_array = wikiget_contents ($cookie, array (
		'action' => 'login',
		'lgname' => $username,
		'lgpassword' => $pwd,
		'lgtoken' => $logintoken_array ['token'],
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array ('lgtoken'), array ('api', 'login', '@attributes'), $logintoken_array) == false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	$logintoken = $logintoken_array['lgtoken'];
	return true;
}
function wikiput_userinfo ($cookie, $user, &$userinfo) {
	$userinfo_array = array ();
	$xml_contents_array = wikiget_contents ('', array (
		'action' => 'query',
		'list' => 'users',
		'ususers' => $user,
		'usprop' => 'editcount|blockinfo|groups|registration',
	));
	if (append_array ($xml_contents_array, array ('editcount', 'blockedby', 'registration'), array ('api', 'query', 'users', 'user', '@attributes'), $userinfo_array) === false) {			
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (isset ($userinfo_array['blockedby']) === false) {
		$userinfo_array['blockedby'] = false;
	}
	$userinfo_array['days'] = (int)((time () - strtotime ($userinfo_array['registration'])) / 60 / 60 / 24);
	$userinfo = $userinfo_array;
	return true;
}
function wikicall_rollback ($cookie, $title, $user, $rollbacktoken) {
	$xml_contents_array = wikiget_contents ($cookie, array (
		'action' => 'rollback',
		'title' => $title,
		'token' => $rollbacktoken,
		'user' => $user,
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	return true;
}
function wikiput_edittoken ($cookie, $logintoken, $username, &$edittoken) {
	$edittoken = '';
	$edittoken_array = array ();
	$xml_contents_array = wikiget_contents ($cookie, array (
		'action' => 'query',
		'prop' => 'info',
		'titles' => 'User:' . $username,
		'intoken' => 'edit',
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array ('edittoken'), array ('api', 'query', 'pages', 'page', '@attributes'), $edittoken_array) === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (substr ($edittoken_array['edittoken'], -2) != '+\\') {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	$edittoken = $edittoken_array['edittoken'];
	return true;
}
function wikiput_redlink ($cookie, $edittoken, $title, &$redlink) {
	$redlink = false;
	$redlink_array = array ();
	$xml_contents_array = wikiget_contents ('', array (
		'action' => 'query',
		'prop' => 'revisions',
		'titles' => $title,
		'rvprop' => 'timestamp',
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array (), array ('api', 'query', 'pages', 'page', '@attributes'), $redlink_array) === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	$redlink = isset ($redlink_array['missing']);
	return true;
}
function wikicall_logout ($cookie) {
	msg (chr (10) . 'logout');
	$login_array = wikiget_contents ($cookie, array (
		'action' => 'logout',
	));
	if ($login_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	return true;
}
function wikicall_edit ($cookie, $edittoken, $title, $summary, $post_array = array ()) {
	$edit_array = array ();
	$xml_contents_array = wikiget_contents ($cookie, array_merge ($post_array, array (
		'action' => 'edit',
		'title' => $title,
		'summary' => $summary,
		'token' => $edittoken,
	)));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array (), array ('api'), $edit_array) === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (isset ($edit_array['error']) !== false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	return true;
}
function wikiput_rollbacktoken ($cookie, $edittoken, $title, &$rollbacktoken) {
	$rollbacktoken = '';
	$rollbacktoken_array = array ();
	$xml_contents_array = wikiget_contents ($cookie, array (
		'action' => 'query',
		'prop' => 'revisions',
		'rvtoken' => 'rollback',
		'titles' => $title,
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array ('rollbacktoken'), array ('api', 'query', 'pages', 'page', 'revisions', 'rev', '@attributes'), $rollbacktoken_array) === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (substr ($rollbacktoken_array['rollbacktoken'], -2) != '+\\') {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	$rollbacktoken = $rollbacktoken_array['rollbacktoken'];
	return true;
}
function wikicall_blockuser ($cookie, $user, $expiry, $reason) {
	$xml_contents_array = wikiget_contents ($cookie, array (
		'action' => 'block',
		'user' => $user,
		'expiry' => $expiry,
		'reason' => $reason,
		'nocreate' => 'nocreate',
		'autoblock' => 'autoblock',
		'noemail' => 'noemail',
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	return true;
}
?>

<?php
define ('MEDIAWIKI_API', 'https://encyclopediadramatica.se/api.php');
$wiki_username = 'Weaselbot';
$wiki_cookie = '';
require_once ('common.php');
function newusers ($cookie, $edittoken, &$id_array) {
	msg (chr (10) . 'newusers');
	$newusers_array = array ();
	$xml_contents_array = wikiget_contents ('', array (
		'action' => 'query',
		'list' => 'logevents',
		'letype' => 'newusers',
		'leprop' => 'title|ids',
		'lelimit' => '5',
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array (), array ('api', 'query', 'logevents', 'item'), $newusers_array) === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	foreach ($newusers_array as $newuser_array) {
		if (append_array ($newuser_array, array ('title', 'logid'), array ('@attributes'), $newuser_array) === false) {
			return alert (__FILE__, __FUNCTION__, __LINE__);
		}
		if (in_array ($newuser_array['logid'], $id_array, true) == true) {
			continue;
		}
		msg ($id_array[] = $newuser_array['logid']);
		$redlink = false;
		$newuser_array['title'] = str_replace ('User:', 'User talk:', $newuser_array['title']);
		if (wikiput_redlink ($cookie, $edittoken, $newuser_array['title'], $redlink) === false) {
			return alert (__FILE__, __FUNCTION__, __LINE__);
		}
		if ($redlink == true) {
			if (wikicall_edit ($cookie, $edittoken, $newuser_array['title'], 'welcome!', array ('text' => '{{welcome}}~~~~', 'createonly' => 'createonly')) === false) {
				return false;
			} else {
				msg ($newuser_array['title']);
			}
		}
	}
	return true;
}
function blockedusers ($cookie, $edittoken, &$id_array) {
	msg (chr (10) . 'blockedusers');
	$blockedusers_array = array ();
	$xml_contents_array = wikiget_contents ('', array (
		'action' => 'query',
		'list' => 'logevents',
		'letype' => 'block',
		'lelimit' => '5',
		'leprop' => 'title|type|ids',
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array (), array ('api', 'query', 'logevents', 'item'), $blockedusers_array) === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	foreach ($blockedusers_array as $blockeduser_array) {
		if (append_array ($blockeduser_array, array ('title', 'action', 'logid'), array ('@attributes'), $blockeduser_array) === false) {
			return alert (__FILE__, __FUNCTION__, __LINE__);
		}
		if (in_array ($blockeduser_array['logid'], $id_array, true) == true) {
			continue;
		}
		msg ($id_array[] = $blockeduser_array['logid']);
		if ($blockeduser_array['action'] != 'block') {
			continue;
		}
		$xml_contents_array = wikiget_contents ('', array (
			'action' => 'query',
			'list' => 'users',
			'ususers' => str_replace ('User:', '', $blockeduser_array['title']),
			'usprop' => 'editcount|blockinfo',
		));
		if (append_array ($xml_contents_array, array ('editcount', 'blockedby'), array ('api', 'query', 'users', 'user', '@attributes'), $blockeduser_array) === false) {			
			return alert (__FILE__, __FUNCTION__, __LINE__);
		}
		if (isset ($blockeduser_array['blockedby']) === false) {
			continue;
		}
		$file_contents = wikiget_contents ('', array (
			'action' => 'query',
			'prop' => 'revisions',
			'titles' => $blockeduser_array['title'],
			'rvprop' => 'content',
		), 'dbg');
		if ($file_contents === false) {
			return alert (__FILE__, __FUNCTION__, __LINE__);		
		}
		if (stripos ($file_contents, 'banned') === false) {
			if (wikicall_edit ($cookie, $edittoken, $blockeduser_array['title'], 'banned: editcount=' . $blockeduser_array['editcount'], array ('prependtext' => '{{banned}}')) === false) {
				return alert (__FILE__, __FUNCTION__, __LINE__);
			} else {
				msg ($blockeduser_array['title']);
			}
		}
	}
	return true;
}
function recentchanges ($cookie, $edittoken, &$id_array) {
	msg (chr (10) . 'recentchanges');
	$recentchanges_array = array ();
	$xml_contents_array = wikiget_contents ($cookie, array (
		'action' => 'query',
		'list' => 'recentchanges',
		'rcnamespace' => '0',
		'rcdir' => 'newer',
		'rcprop' => 'user|comment|parsedcomment|flags|timestamp|title|ids|sizes|redirect|patrolled|loginfo|tags',
		'rcdir' => 'older',
		'rctype' => 'edit',
		'rclimit' => 20,
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	if (append_array ($xml_contents_array, array (), array ('api', 'query', 'recentchanges', 'rc'), $recentchanges_array) == false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
	foreach ($recentchanges_array as $recentchange_array) {
		if (append_array ($recentchange_array, array ('title', 'user', 'rcid', 'oldlen', 'newlen'), array ('@attributes'), $recentchange_array) == false) {
			return alert (__FILE__, __FUNCTION__, __LINE__);
		}
		if ($recentchange_array['newlen'] != 0 || in_array ($recentchange_array['rcid'], $id_array, true) == true) {
			continue;
		}
		msg ($id_array[] = $recentchange_array['rcid']);
		if (wikiput_userinfo ($cookie, $recentchange_array['user'], $recentchange_userinfo) === false) {
			alert (__FILE__, __FUNCTION__, __LINE__);
			continue;
		}
		if ($recentchange_array['user'] != 'Schnookums' && ($recentchange_userinfo['days'] > 3 || $recentchange_userinfo['editcount'] > 20)) {
			continue;
		}
		$recentchange_rollbacktoken = '';
		if (wikiput_rollbacktoken ($cookie, $edittoken, $recentchange_array['title'], $recentchange_rollbacktoken) === false) {
			alert (__FILE__, __FUNCTION__, __LINE__);
			continue;
		}
		if (wikicall_rollback ($cookie, $recentchange_array['title'], $recentchange_array['user'], $recentchange_rollbacktoken) === false) {
			alert (__FILE__, __FUNCTION__, __LINE__);
			continue;
		}
		//if (wikicall_blockuser ($cookie, $recentchange_array['user'], '1 year', 'BLANKING IN PROGRESS') === false) {
			//return alert (__FILE__, __FUNCTION__, __LINE__);
		//}
		msg ($recentchange_array['user'] . ' blanked ' . $recentchange_array['title']);
	}
	return true;
}
function reviewusers ($cookie, $edittoken, $users) {
	foreach ($users as $user) {
		$user_array = array ();
		$xml_contents_array = wikiget_contents ($cookie, array (
		'action' => 'query',
		'list' => 'recentchanges',
		'rcnamespace' => '0',
		'rcdir' => 'newer',
		'rcprop' => 'user|comment|parsedcomment|flags|timestamp|title|ids|sizes|redirect|patrolled|loginfo|tags',
		'rcdir' => 'older',
		'rctype' => 'edit',
		'rclimit' => 20,
	));
	if ($xml_contents_array === false) {
		return alert (__FILE__, __FUNCTION__, __LINE__);
	}
}
$id_array = array ();
$wiki_reviewusers = array ('User:Schnookums');
while (1) {
	$COMMON_GET_CONTENTS = 0;
	$wiki_logintoken = '';
	$wiki_edittoken = '';
	if (wikiput_logintoken ($wiki_cookie, $wiki_username, $wiki_pwd, $wiki_logintoken) !== false) {
		if (wikiput_edittoken ($wiki_cookie, $wiki_logintoken, $wiki_username, $wiki_edittoken) !== false) {
			reviewusers ($wiki_cookie, $wiki_edittoken, $wiki_reviewusers);
			if (recentchanges ($wiki_cookie, $wiki_edittoken, $id_array) !== false) {
				if (blockedusers ($wiki_cookie, $wiki_edittoken, $id_array) !== false) {
					if (newusers ($wiki_cookie, $wiki_edittoken, $id_array) !== false) {
						//
					}
				}
			}
		}
		wikicall_logout ($wiki_cookie);
	}
	msg (chr (10) . 'http requests: ' . $COMMON_GET_CONTENTS);
	for ($x = 0; $x < 15; $x++) {
		if ($x == 0) {
			msg (chr (10) . 'z');
		} else {
			msg ('z');
		}
		sleep (1);
	}
}
?>