cvs: php-master-web /scripts mirror-test

From: Date: Wed, 08 Jan 2003 07:39:45 +0000
Subject: cvs: php-master-web /scripts mirror-test
Groups: php.mirrors 
Request: Send a blank email to php-mirrors+get-14838@lists.php.net to get a copy of this message
goba Wed Jan 8 02:39:45 2003 EDT Modified files: /php-master-web/scripts mirror-test Log: Get all mirror data into a PHP array before doing anything with the outside world. Also use foreach() loops instead of $for for more flexibility... Also adding one more checkpoint. Index: php-master-web/scripts/mirror-test diff -u php-master-web/scripts/mirror-test:1.28 php-master-web/scripts/mirror-test:1.29 --- php-master-web/scripts/mirror-test:1.28 Tue Jan 7 17:05:42 2003 +++ php-master-web/scripts/mirror-test Wed Jan 8 02:39:45 2003 @@ -28,30 +28,33 @@ $result = mysql_query($query) or sendmessage("unable to get from the database: " . mysql_error(), TRUE); -// Get the number of mirrors in the resultset -$total = mysql_num_rows($result); +// Get all mirror site data to the $hosts array +$hosts = array(); +while ($row = mysql_fetch_array($result)) { + $hosts[] = $row; +} -// Get all mirror sites, and count the index of them -for($i = 0; $row = mysql_fetch_array($result, MYSQL_ASSOC); $i++) { +// DEBUG +sendmessage("Database data now in a PHP array"); - // Add information gathered from database table - $hosts[$i] = $row; +// Get all mirror sites, and count the index of them +foreach ($hosts as $index => $host) { // Open port 80 on the mirror site - $fp = @fsockopen($row['hostname'], 80, $errno, $errstr, 15); + $fp = @fsockopen($host['hostname'], 80, $errno, $errstr, 15); // If we were unable to open the port, continue with the next mirror if (!$fp) { - if ($test_debug) { echo "unable to connect to $row[hostname]\n"; flush(); } + if ($test_debug) { echo "unable to connect to $host[hostname]\n"; flush(); } continue; } // GET mirror-info.php from the registered hostname - $rc = fputs($fp, "GET /mirror-info.php HTTP/1.0\r\nHost: $row[hostname]\r\n\r\n"); + $rc = fputs($fp, "GET /mirror-info.php HTTP/1.0\r\nHost: $host[hostname]\r\n\r\n"); // If we were unable to write to socket, skip mirror site if ($rc === FALSE) { - if ($test_debug) { echo "unable to send request to $row[hostname]\n"; flush(); } + if ($test_debug) { echo "unable to send request to $host[hostname]\n"; flush(); } continue; } @@ -66,7 +69,7 @@ // If we were unable to pass the HTTP header, skip mirror site if ($line === FALSE || $line == "") { - if ($test_debug) { echo "didn't get past headers for $row[hostname]\n"; flush(); } + if ($test_debug) { echo "didn't get past headers for $host[hostname]\n"; flush(); } continue; } @@ -75,26 +78,26 @@ // If unable to read that row, skip mirror site if ($rv === FALSE) { - if ($test_debug) { echo "didn't get data for $row[hostname]\n"; flush(); } + if ($test_debug) { echo "didn't get data for $host[hostname]\n"; flush(); } continue; } else { - if ($test_debug) { echo "received data from $row[hostname]\n"; flush(); } + if ($test_debug) { echo "received data from $host[hostname]\n"; flush(); } } // Exlode information row by delimiter $info = explode("|", $rv); // Put pieces into the $hosts array - $hosts[$i]['phpversion'] = $info[1]; + $hosts[$index]['phpversion'] = $info[1]; // The last updated date is a number [db security] if (is_numeric($info[2])) { - $hosts[$i]['mirrorupdated'] = $info[2]; + $hosts[$index]['mirrorupdated'] = $info[2]; } // If new search information is available, override previous one if (in_array($info[3], array('0', '1', '2'))) { - $hosts[$i]['has_search'] = $info[3]; + $hosts[$index]['has_search'] = $info[3]; } } @@ -103,16 +106,16 @@ // Go through all mirror sites checked $mirrorsupdated = 0; -for($i=0; $i < $total; $i++) { +foreach ($hosts as $host) { // If "mirrorupdated" date specified, then we were able to check // the mirror, so the mirror data can be updated. Otherwise // the previous data need to be kept! - if (isset($hosts[$i]['mirrorupdated'])) { - $query = "UPDATE mirrors SET lastchecked = NOW(), has_search = '" . ((int) $hosts[$i]['has_search']) . "', " . - "lastupdated = FROM_UNIXTIME(" . $hosts[$i]['mirrorupdated'] . "), " . - "phpversion = '" . addslashes($hosts[$i]['phpversion']) . "' " . - "WHERE hostname = '" . $hosts[$i]['hostname'] . "'"; + if (isset($host['mirrorupdated'])) { + $query = "UPDATE mirrors SET lastchecked = NOW(), has_search = '" . ((int) $host['has_search']) . "', " . + "lastupdated = FROM_UNIXTIME(" . $host['mirrorupdated'] . "), " . + "phpversion = '" . addslashes($host['phpversion']) . "' " . + "WHERE hostname = '" . $host['hostname'] . "'"; $result = mysql_query($query) or sendmessage("unable to update the database: $query: " . mysql_error(), TRUE); if ($result) { $mirrorsupdated++; } } @@ -133,11 +136,11 @@ // Custom error handler function myhandler($errno, $errstr, $errfile, $errline) { - global $row; + global $host; // See if the error is under the error reporting level if ($errno & error_reporting()) { - if (isset($row)) { $hostname = $row['hostname']; } + if (isset($host)) { $hostname = $host['hostname']; } else { $hostname = ""; } sendmessage("$errno : $errstr : $errfile : $errline : $hostname", TRUE); }

« previous php.mirrors (#14838) next »