-
Notifications
You must be signed in to change notification settings - Fork 1
/
sig_scrape_server_id.php
55 lines (50 loc) · 1.45 KB
/
sig_scrape_server_id.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
require('includes/simple_html_dom.php');
function scrapeServerID()
{
// Create DOM from URL or file
$html = file_get_html('https://battlefieldtracker.com/bf1/servers?platform=pc&name=nwg');
$serverURL = $html->find('a.name')[0]->href;//$server->href;
$serverID = str_replace("/bf1/servers/pc/", "", $serverURL);
if (is_numeric($serverID) && strlen($serverID) == 13)
{
return $serverID;
}
return false;
}
function getCurrentScriptServerID()
{
$file = fopen("sig.php","r");
while (!feof($file))
{
$line = fgets($file);
if (strpos($line, 'nwgServerID='))
{
//echo "***FOUND***";
$line = str_replace('$nwgServerID=',"",$line);
$line = str_replace(';',"",$line);
//echo $i . $line . "<br />";
return $line;
}
}
}
function updateServerIDinPHP($newServerID)
{ // https://stackoverflow.com/questions/3004041/how-to-replace-a-particular-line-in-a-text-file-using-php;
if($newServerID)
{
$data = file('sig.php'); // reads an array of lines
function replace_a_line($data) {
if (stristr($data, '$nwgServerID=')) {
$serverID = scrapeServerID();
return "\$nwgServerID=$serverID;\n";
}
return $data;
}
$data = array_map('replace_a_line',$data);
file_put_contents('sig.php', implode('', $data));
}
}
$serverID_scraper = scrapeServerID();
if ($serverID_scraper && getCurrentScriptServerID() != $serverID_scraper)
updateServerIDinPHP($serverID_scraper);
?>