~thomas-juberg/bebot/main

« back to all changes in this revision

Viewing changes to Extras/WhoisUpdate/WhoisUpdate.php

  • Committer: Thomas Juberg
  • Date: 2013-10-28 03:11:16 UTC
  • Revision ID: git-v1:55563839a2071dbee7d3bda0a2f643a0c9c32852
- Fix IntelliJ project files
- Update .gitignore
- Auto reformat code to PSR-1/PSR-2 code formatting (A lot of work will need to be done to implement PSR-0, PSR-1 and PSR-2 fully, but this should take care of the general formatting)

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
// Default values:
43
43
$hours = 72;
44
44
$delaytime = 10;
45
 
$do_unorged_users = FALSE;
46
 
$delete_not_updated = FALSE;
47
 
$show_org_names = TRUE;
48
 
$show_character_names = FALSE;
49
 
require ('whois-update.conf');
 
45
$do_unorged_users = false;
 
46
$delete_not_updated = false;
 
47
$show_org_names = true;
 
48
$show_character_names = false;
 
49
require('whois-update.conf');
50
50
// disable execution timeout:
51
51
set_time_limit(0);
52
52
 
55
55
    if ($os_windows) {
56
56
        if (@!dl("php_curl.dll")) {
57
57
            echo "Curl not available\n";
58
 
        }
59
 
        else {
 
58
        } else {
60
59
            echo "Curl extension loaded\n";
61
60
            if (!extension_loaded("sockets")) {
62
61
                if (!dl("php_sockets.dll")) {
64
63
                }
65
64
            }
66
65
        }
67
 
    }
68
 
    else {
 
66
    } else {
69
67
        if (function_exists('curl_init')) {
70
68
            echo "Curl extension loaded\n";
71
 
        }
72
 
        else {
 
69
        } else {
73
70
            echo "Curl not available\n";
74
71
            if (!extension_loaded("sockets")) {
75
72
                die("Sockets extention required to run this script");
83
80
        if (!dl("php_mysql.dll")) {
84
81
            die("Loading php_mysql.dll failed. MySQL extention required to run this script");
85
82
        }
86
 
    }
87
 
    else {
 
83
    } else {
88
84
        die("MySQL support required to run this script");
89
85
    }
90
86
}
91
87
 
92
88
echo "Connecting to the database.\n\n";
93
 
$link = ($GLOBALS["___mysqli_ston"] = mysqli_connect($dbserver,  $username,  $password)) or die("Could not connect : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
 
89
$link = ($GLOBALS["___mysqli_ston"] = mysqli_connect(
 
90
    $dbserver,
 
91
    $username,
 
92
    $password
 
93
)) or die("Could not connect : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error(
 
94
        $GLOBALS["___mysqli_ston"]
 
95
    ) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
94
96
((bool)mysqli_query($GLOBALS["___mysqli_ston"], "USE $dbname")) or die("Could not select database");
95
97
// Get all org ids out of the whois table, get the org infos, and add/update all user:
96
98
$sqlquery = "SELECT org_id, org_name FROM (SELECT org_id, org_name, count(nickname) AS count FROM " . $tablename;
97
99
$sqlquery .= " WHERE org_id != '' GROUP BY org_name) AS temp_whois ORDER BY count DESC;";
98
 
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
 
100
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object(
 
101
        $GLOBALS["___mysqli_ston"]
 
102
    )) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error(
 
103
    )) ? $___mysqli_res : false)));
99
104
// simple counter, increased by one each time a get_site() call is made:
100
105
$httpqueries = 0;
101
106
// same for REPLACE/UPDATE in db:
104
109
$failedrosterlookup = 0;
105
110
// same for failed person lookups:
106
111
$failedpersonlookup = 0;
107
 
while ($orgid = mysqli_fetch_array($result,  MYSQLI_ASSOC)) {
 
112
while ($orgid = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
108
113
    if ($show_org_names) {
109
114
        echo gmdate("H:i:s") . " - Querying roster for " . $orgid['org_name'] . " (" . $orgid['org_id'] . ")...\n";
110
115
    }
111
 
    $org_cont = get_site($baseurl . "/org/stats/d/" . $dimension . "/name/" . $orgid['org_id'] . "/basicstats.xml", 0, 60, 60);
 
116
    $org_cont = get_site(
 
117
        $baseurl . "/org/stats/d/" . $dimension . "/name/" . $orgid['org_id'] . "/basicstats.xml",
 
118
        0,
 
119
        60,
 
120
        60
 
121
    );
112
122
    $httpqueries++;
113
123
    if (!$org_cont) {
114
124
        echo "     Could not get roster for " . $orgid['org_name'] . " (" . $orgid['org_id'] . ")!\n";
115
125
        $failedrosterlookup++;
116
 
    }
117
 
    else {
 
126
    } else {
118
127
        $starttime = time();
119
128
        $orgname = $this->bot->db->real_escape_string(xmlparse($org_cont, "name"));
120
129
        $orgfaction = xmlparse($org_cont, "side");
142
151
            if (empty($who["nickname"])) {
143
152
                echo "     Warning! Missing members nickname!\n";
144
153
                $failedpersonlookup++;
145
 
            }
146
 
            else {
 
154
            } else {
147
155
                if (empty($who["level"])) {
148
156
                    echo "     Warning! Missing members level! (" . $who["nickname"] . ")\n";
149
157
                    $failedpersonlookup++;
150
 
                }
151
 
                else {
 
158
                } else {
152
159
                    if (empty($who["gender"])) {
153
160
                        echo "     Warning! Missing members gender! (" . $who["nickname"] . ")\n";
154
161
                        $failedpersonlookup++;
155
 
                    }
156
 
                    else {
 
162
                    } else {
157
163
                        if (empty($who["breed"])) {
158
164
                            echo "     Warning! Missing members breed! (" . $who["nickname"] . ")\n";
159
165
                            $failedpersonlookup++;
160
 
                        }
161
 
                        else {
 
166
                        } else {
162
167
                            if (empty($who["profession"])) {
163
168
                                echo "     Warning! Missing members profession! (" . $who["nickname"] . ")\n";
164
169
                                $failedpersonlookup++;
165
 
                            }
166
 
                            else {
 
170
                            } else {
167
171
                                if (empty($who["faction"])) {
168
172
                                    echo "     Warning! Missing members faction! (" . $who["nickname"] . ")\n";
169
173
                                    $failedpersonlookup++;
170
 
                                }
171
 
                                else {
 
174
                                } else {
172
175
                                    if (empty($who["rank"])) {
173
176
                                        echo "     Warning! Missing members rank! (" . $who["nickname"] . ")\n";
174
177
                                        $failedpersonlookup++;
175
 
                                    }
176
 
                                    else {
 
178
                                    } else {
177
179
                                        if (empty($who["org"])) {
178
180
                                            echo "     Warning! Missing members organization name! (" . $who["nickname"] . ")\n";
179
181
                                            $failedpersonlookup++;
180
 
                                        }
181
 
                                        else {
 
182
                                        } else {
182
183
                                            if (empty($who["org_id"])) {
183
184
                                                echo "     Warning! Missing members organization ID! (" . $who["nickname"] . ")\n";
184
185
                                                $failedpersonlookup++;
185
 
                                            }
186
 
                                            else {
 
186
                                            } else {
187
187
                                                // INSERT new entries into DB, UPDATE existing ones:
188
188
                                                $query = "INSERT INTO " . $tablename . " (nickname, firstname, lastname, level, gender, breed, faction,"
189
189
                                                    . " profession, defender_rank_id, org_id, org_name, org_rank, org_rank_id, pictureurl, updated)" . " VALUES ('"
202
202
                                                    . "faction = VALUES(faction), profession = VALUES(profession), org_id = VALUES(org_id), "
203
203
                                                    . "defender_rank_id = VALUES(defender_rank_id), pictureurl = VALUES(pictureurl), updated = "
204
204
                                                    . "VALUES(updated), org_name = VALUES(org_name), org_rank = VALUES(org_rank), org_rank_id = " . "VALUES(org_rank_id)";
205
 
                                                mysqli_query($GLOBALS["___mysqli_ston"], $query) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
 
205
                                                mysqli_query(
 
206
                                                    $GLOBALS["___mysqli_ston"],
 
207
                                                    $query
 
208
                                                ) or die("Query failed : " . ((is_object(
 
209
                                                        $GLOBALS["___mysqli_ston"]
 
210
                                                    )) ? mysqli_error(
 
211
                                                        $GLOBALS["___mysqli_ston"]
 
212
                                                    ) : (($___mysqli_res = mysqli_connect_error(
 
213
                                                    )) ? $___mysqli_res : false)));
206
214
                                                $sqlquerycount++;
207
215
                                            }
208
216
                                        }
216
224
        }
217
225
        // Unset all org entries that are still not updated but pointing to the current org id
218
226
        $query = "UPDATE whois SET org_id = 0, org_name = '', updated = updated - 86400 WHERE org_id = " . $orgid['org_id'] . " AND updated < " . $thistime . " - 10";
219
 
        mysqli_query($GLOBALS["___mysqli_ston"], $query) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
 
227
        mysqli_query($GLOBALS["___mysqli_ston"], $query) or die("Query failed : " . ((is_object(
 
228
                $GLOBALS["___mysqli_ston"]
 
229
            )) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error(
 
230
            )) ? $___mysqli_res : false)));
220
231
 
221
232
        echo "     " . $i . " members completed in " . (time() - $starttime) . " seconds\n";
222
233
    }
237
248
    // Now work on all not updated entries left over (no org/switched org):
238
249
    $comptime = $thistime - 10;
239
250
    $sqlquery = "SELECT nickname FROM " . $tablename . " WHERE updated < " . $comptime;
240
 
    $result = mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
241
 
    while ($user = mysqli_fetch_array($result,  MYSQLI_ASSOC)) {
 
251
    $result = mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object(
 
252
            $GLOBALS["___mysqli_ston"]
 
253
        )) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error(
 
254
        )) ? $___mysqli_res : false)));
 
255
    while ($user = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
242
256
        if ($show_character_names) {
243
257
            echo "Reading current stats for character " . $user['nickname'] . "...\n";
244
258
        }
245
 
        $content_arr = get_site($baseurl . "/character/bio/d/" . $dimension . "/name/" . strtolower($user['nickname']) . "/bio.xml");
 
259
        $content_arr = get_site(
 
260
            $baseurl . "/character/bio/d/" . $dimension . "/name/" . strtolower($user['nickname']) . "/bio.xml"
 
261
        );
246
262
        $httpqueries++;
247
263
        if ($content_arr) {
248
264
            $content = $content_arr;
279
295
                    . $who["gender"] . "', breed='" . $who["breed"] . "', faction='" . $who["faction"] . "', profession='" . $who["profession"] . "', defender_rank_id='"
280
296
                    . $who["at"] . "', org_id='" . $who["org_id"] . "', org_name='" . $who["org"] . "', org_rank='" . $who["rank"] . "', org_rank_id='" . $who["rank_id"]
281
297
                    . "', updated='" . $thistime . "', pictureurl = '" . $who["pictureurl"] . "'" . " WHERE nickname='" . $who["nick"] . "';";
282
 
                mysqli_query($GLOBALS["___mysqli_ston"], $query) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
 
298
                mysqli_query($GLOBALS["___mysqli_ston"], $query) or die("Query failed : " . ((is_object(
 
299
                        $GLOBALS["___mysqli_ston"]
 
300
                    )) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error(
 
301
                    )) ? $___mysqli_res : false)));
283
302
                $sqlquerycount++;
284
303
            }
285
304
            // delay next parsing by $delaytime in seconds:
292
311
$deletetime = $thistime - $hours * 3600;
293
312
// Clean org infos on seriously outdated entries (using same value as for deleting):
294
313
$query = "UPDATE whois SET org_id = 0, org_name = '' WHERE updated < " . $deletetime;
295
 
mysqli_query($GLOBALS["___mysqli_ston"], $query) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
 
314
mysqli_query($GLOBALS["___mysqli_ston"], $query) or die("Query failed : " . ((is_object(
 
315
        $GLOBALS["___mysqli_ston"]
 
316
    )) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error(
 
317
    )) ? $___mysqli_res : false)));
296
318
// Only delete non-updated entries if explicitly wished:
297
319
if ($delete_not_updated) {
298
320
    // all not-updated entries:
299
321
    $sqlquery = "DELETE FROM " . $tablename . " WHERE updated < " . $deletetime;
300
 
    mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
 
322
    mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object(
 
323
            $GLOBALS["___mysqli_ston"]
 
324
        )) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error(
 
325
        )) ? $___mysqli_res : false)));
301
326
}
302
327
// do some statistics:
303
328
$sqlquery = "SELECT count(*) as count FROM " . $tablename;
304
 
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
305
 
$count = mysqli_fetch_array($result,  MYSQLI_ASSOC);
 
329
$result = mysqli_query($GLOBALS["___mysqli_ston"], $sqlquery) or die("Query failed : " . ((is_object(
 
330
        $GLOBALS["___mysqli_ston"]
 
331
    )) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error(
 
332
    )) ? $___mysqli_res : false)));
 
333
$count = mysqli_fetch_array($result, MYSQLI_ASSOC);
306
334
((mysqli_free_result($result) || (is_object($result) && (get_class($result) == "mysqli_result"))) ? true : false);
307
335
$runtime = time() - $thistime;
308
336
$mins = floor($runtime / 60);
319
347
echo $mins . "min " . $sec . "sec total runtime!\n";
320
348
echo "================================================\n";
321
349
if (file_exists($addons) && is_readable($addons)) {
322
 
    include ($addons);
 
350
    include($addons);
323
351
}
324
352
((is_null($___mysqli_res = mysqli_close($link))) ? false : $___mysqli_res);
325
353
 
326
354
 
327
 
function get_site($url, $strip_headers = FALSE, $read_timeout = FALSE)
 
355
function get_site($url, $strip_headers = false, $read_timeout = false)
328
356
{
329
357
    echo "     Fetching $url";
330
358
    $starttime = time();
331
359
 
332
360
    if (!function_exists('curl_init')) {
333
361
        $return = get_site_sock($url, $strip_headers, $read_timeout);
334
 
    }
335
 
    else {
 
362
    } else {
336
363
        $return = get_site_curl($url, $strip_headers, $read_timeout);
337
364
    }
338
365
 
339
366
    if ($return) {
340
367
        echo " .... completed in " . (time() - $starttime) . " seconds\n";
341
 
    }
342
 
    else {
 
368
    } else {
343
369
        echo "\n";
344
370
    }
345
371
 
346
372
    return $return;
347
373
}
348
374
 
349
 
function get_site_sock($url, $strip_headers = FALSE, $read_timeout = FALSE)
 
375
function get_site_sock($url, $strip_headers = false, $read_timeout = false)
350
376
{
351
377
    $return = get_site_data($url, $strip_headers, $read_timeout);
352
378
 
361
387
    Gets the data from a URL
362
388
    */
363
389
function get_site_data(
364
 
    $url, $strip_headers = FALSE, $read_timeout = FALSE,
 
390
    $url,
 
391
    $strip_headers = false,
 
392
    $read_timeout = false,
365
393
    $proxy = ''
366
 
)
367
 
{
 
394
) {
368
395
    $get_url = parse_url($url);
369
396
    // Check to see if we're using a proxy, and get the IP address for the target host.
370
397
    if (!empty($proxy)) {
371
398
        $proxy_address = explode(":", $proxy);
372
399
        $address = gethostbyname($proxy_address[0]);
373
400
        $service_port = $proxy_address[1];
374
 
    }
375
 
    else {
 
401
    } else {
376
402
        $address = gethostbyname($get_url['host']);
377
403
        /* Get the port for the WWW service. */
378
404
        $service_port = getservbyname('www', 'tcp');
380
406
    /* Create a TCP/IP socket. */
381
407
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
382
408
    // Check to see if the socket failed to create.
383
 
    if ($socket === FALSE) {
 
409
    if ($socket === false) {
384
410
        echo "Failed to create socket. Error was: " . socket_strerror(socket_last_error());
385
 
        return FALSE;
 
411
        return false;
386
412
    }
387
413
 
388
414
    // Set some sane read timeouts to prevent the bot from hanging forever.
391
417
    }
392
418
 
393
419
    socket_set_option(
394
 
        $socket, SOL_SOCKET, SO_RCVTIMEO, array(
395
 
            "sec" => $read_timeout,
396
 
            "usec" => 0
 
420
        $socket,
 
421
        SOL_SOCKET,
 
422
        SO_RCVTIMEO,
 
423
        array(
 
424
             "sec" => $read_timeout,
 
425
             "usec" => 0
397
426
        )
398
427
    );
399
428
 
400
429
    $connect_result = @socket_connect($socket, $address, $service_port);
401
430
 
402
431
    // Make sure we have a connection
403
 
    if ($connect_result === FALSE) {
404
 
        echo "Failed to connect to server " . $address . ":" . $service_port . " (" . $url . ") Error was: " . socket_strerror(socket_last_error());
405
 
        return FALSE;
 
432
    if ($connect_result === false) {
 
433
        echo "Failed to connect to server " . $address . ":" . $service_port . " (" . $url . ") Error was: " . socket_strerror(
 
434
                socket_last_error()
 
435
            );
 
436
        return false;
406
437
    }
407
438
    // Rebuild the full query after parse_url
408
439
    $url = $get_url["path"];
415
446
    $in .= "User-Agent: \r\n\r\n";
416
447
    $write_result = @socket_write($socket, $in, strlen($in));
417
448
    // Make sure we wrote to the server okay.
418
 
    if ($write_result === FALSE) {
 
449
    if ($write_result === false) {
419
450
        echo "Failed to write to server: " . socket_strerror(socket_last_error());
420
 
        return FALSE;
 
451
        return false;
421
452
    }
422
453
    $return["content"] = "";
423
454
    $read_result = @socket_read($socket, 2048);
424
 
    while ($read_result != "" && $read_result !== FALSE) {
 
455
    while ($read_result != "" && $read_result !== false) {
425
456
        $return .= $read_result;
426
457
        $read_result = @socket_read($socket, 2048);
427
458
    }
428
459
    // Make sure we got a response back from the server.
429
 
    if ($read_result === FALSE) {
 
460
    if ($read_result === false) {
430
461
        echo "Failed to read response: " . socket_strerror(socket_last_error());
431
 
        return FALSE;
 
462
        return false;
432
463
    }
433
464
    $close_result = @socket_close($socket);
434
465
    // Make sure we closed our socket properly.  Open sockets are bad!
435
 
    if ($close_result === FALSE) {
 
466
    if ($close_result === false) {
436
467
        echo "Failed to close socket: " . socket_strerror(socket_last_error());
437
 
        return FALSE;
 
468
        return false;
438
469
    }
439
470
    // Did the calling function want http headers stripped?
440
471
    if ($strip_headers) {
446
477
 
447
478
 
448
479
function get_site_curl(
449
 
    $url, $strip_headers = FALSE, $timeout = FALSE,
450
 
    $post = NULL,
451
 
    $login = NULL
 
480
    $url,
 
481
    $strip_headers = false,
 
482
    $timeout = false,
 
483
    $post = null,
 
484
    $login = null
452
485
) // login should be username:password
453
486
{
454
487
    $ch = curl_init();
474
507
    // CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if
475
508
    // CURLOPT_SSL_VERIFYPEER is disabled (it defaults to 2 - check the existence of a
476
509
    // common name and also verify that it matches the hostname provided)
477
 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
 
510
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
478
511
    // Optional: Return the result instead of printing it
479
512
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
480
513
    // Specify a timeout