include('config.php');
function file_get_contents2($url, $content = NULL, $cookie = NULL){
$url_parsed = parse_url($url);
$fd = fsockopen($url_parsed['host'], 80);
$query = '';
if (isset($url_parsed['query']))
$query = $url_parsed['query'];
if (isset($content))
$method = 'POST';
else
$method = 'GET';
fputs($fd, $method." ".$url_parsed['path'].'?'.$query." HTTP/1.0\r\n");
fputs($fd, "Host: ".$url_parsed['host']."\r\n");
fputs($fd, "Accept-Charset: utf-8\r\n");
fputs($fd, "Accept-Language: fr\r\n");
fputs($fd, "Referer: http://forums.darkfallonline.com/index.php\r\n");
fputs($fd, "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n");
if (isset($cookie))
fputs($fd, "Cookie: ".$cookie."\r\n");
if (isset($content)) {
fputs($fd, "Content-Type: application/x-www-form-urlencoded\r\n");
fputs($fd, "Content-Length: ".strlen($content)."\r\n");
}
fputs($fd, "Connection: close\r\n\r\n");
if (isset($content))
fputs($fd, $content);
$content = '';
$header = '';
$is_content = false;
while ($line = fgets($fd)) {
if ($is_content)
$content .= $line;
else
$header .= $line;
if(strlen($line) <= 2)
$is_content = true;
}
fclose($fd);
return array($header, $content);
}
function handleCookies(&$cookies, $header) {
preg_match_all('`Set-Cookie: ([^=]+)=([^;]+);`', $header, $out);
foreach ($out[1] as $key => $val) {
if ($out[2][$key] == 'delete') {
unset($cookies[$out[1][$key]]);
} else {
$cookies[$out[1][$key]] = $out[2][$key];
}
}
}
function dumpCookies($cookies) {
if (count($cookies) == 0)
return NULL;
$c = '';
foreach($cookies as $key => $val) {
$c .= $key.'='.$val.'; ';
}
return substr($c, 0, strlen($c) - 2);
}
function fl() {
ob_flush();
flush();
usleep(300000);
}
/* Login */
function doLogin(&$cookies) {
$r = file_get_contents2('http://forums.darkfallonline.com/');
handleCookies($cookies, $r[0]);
$r = file_get_contents2(
'http://forums.darkfallonline.com/login.php?do=login',
'vb_login_username=blorg&vb_login_password=&s=&securitytoken=guest&do=login&vb_login_md5password=021c56e68c23563912ba08e52440e47a&vb_login_md5password_utf=021c56e68c23563912ba08e52440e47a',
dumpCookies($cookies)
);
handleCookies($cookies, $r[0]);
}
/* Gather Posts */
function GatherDev(&$cookies, $id) {
$r = file_get_contents2(
'http://forums.darkfallonline.com/search.php?do=finduser&u='.$id,
NULL,
dumpCookies($cookies)
);
preg_match('`Location: ([^\r]+)`', $r[0], $location);
if (count($location) > 0) {
$r = file_get_contents2($location[1], NULL, dumpCookies($cookies));
handleCookies($cookies, $r[0]);
} else {
preg_match('`Please try again in ([0-9]+) second`', $r[1], $time);
if (!array_key_exists(1, $time)) {
echo $r[1];
exit;
}
echo 'Delay '.($time[1] + 1).'s...
';
fl();
sleep($time[1] + 1);
return GatherDev($cookies, $id);
}
preg_match_all('`[^<]+
`', $r[1], $threads);
foreach ($threads[0] as $key => $val) {
if (!GatherPost($threads[1][$key])) {
return;
}
}
}
function getHour($hour, $ampm) {
if (($ampm == 'PM') && ($hour != 12))
return $hour + 12;
if (($ampm == 'AM') && ($hour == 12))
return $hour - 12;
return $hour;
}
function get_date($date) {
if (!preg_match('`(?[0-9]+)-(?[0-9]+)-(?[0-9]+), (?[0-9]+):(?[0-9]+) (?AM|PM)`', $date, $res)) {
preg_match('`(?[a-zA-Z]+), (?[0-9]+):(?[0-9]+) (?AM|PM)`', $date, $res);
if ($res['day'] == 'Yesterday') {
return mktime(getHour($res['hour'], $res['ampm']), $res['min'], 0, date("m") , date("d")-1, date("Y"));
} elseif ($res['day'] == 'Today') {
return mktime(getHour($res['hour'], $res['ampm']), $res['min'], 0, date("m") , date("d"), date("Y"));
}
}
return mktime(getHour($res['hour'], $res['ampm']), $res['min'], 0, $res['month'], $res['day'], $res['year']);
}
function GatherFirstPost($id, $post_id) {
$query = 'SELECT `id` FROM `threads` WHERE `source`="1" AND `id`="'.(int) $id.'"';
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
return;
$file = file_get_contents('http://forums.darkfallonline.com/showthread.php?t='.$id);
preg_match('`.*?[^A-Za-z0-9]+(?[^<]+)`ms', $file, $out);
preg_match('`(.*?) - Darkfall Forums`', $file, $title);
$query = 'INSERT INTO `threads`
(`source`, `id`, `title`, `author_name`, `dev_name`, `dev_post_count`, `last_date`)
VALUES (1, "'.(int) $id.'", "'.mysql_escape_string(trim($title[1])).'", "'.mysql_escape_string($out['author']).'", "", 0, 0)';
mysql_query($query);
if ($out['post_id'] != $id) {
$query = 'INSERT INTO `posts`
(`source`, `thread_id`, `post_id`, `author_name`, `date`, `is_dev`, `message`)
VALUES (1, "'.(int) $id.'", "'.(int) $out['post_id'].'", "'.mysql_escape_string(strip_tags($out['author'])).'", '.(int) get_date($out['date']).', FALSE, "'.mysql_escape_string($out['message']).'")';
mysql_query($query);
}
}
function GatherPost($id) {
fl();
$file = file_get_contents('http://forums.darkfallonline.com/showthread.php?p='.$id);
preg_match('`href="showthread.php[^"]*t=([0-9]+)"`', $file, $thread_id);
GatherFirstPost($thread_id[1], $id);
$query = 'SELECT `post_id` FROM `posts` WHERE `source`="1" AND `post_id`="'.(int) $id.'"';
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
return false;
preg_match('`.*?[^A-Za-z0-9]+(?[^<]+)`ms', $file, $out);
$query = 'INSERT INTO `posts`
(`source`, `thread_id`, `post_id`, `author_name`, `date`, `is_dev`, `message`)
VALUES (1, "'.(int) $thread_id[1].'", "'.(int) $id.'", "'.mysql_escape_string(strip_tags($out['author'])).'", '.(int) get_date($out['date']).', TRUE, "'.mysql_escape_string($out['message']).'")';
mysql_query($query);
$query = 'UPDATE `threads` SET `dev_name`="'.mysql_escape_string(strip_tags($out['author'])).'", `dev_post_count`=`dev_post_count`+1, `last_date`="'.(int) get_date($out['date']).'"';
mysql_query($query);
echo 'Insert `'.$id.'` by `'.strip_tags($out['author']).'`
';
return true;
}
// 1065
$dev_list = array(
1065 => 'Brannoc',
1889 => 'Tasos',
5044 => 'Sprite',
32136 => 'Squatch',
3315 => 'Phooey',
10317 => 'Marrock'
);
echo 'Login ...
';
fl();
$cookies = array();
doLogin($cookies);
echo 'Logged
';
fl();
foreach ($dev_list as $key => $val) {
echo 'Handling `'.$val.'`
';
fl();
GatherDev($cookies, $key);
}
?>