--- ip2country.inc	2012-02-24 00:22:55 -0800
+++ ip2country.new	2012-05-21 15:35:43 -0700
@@ -41,6 +41,8 @@ function ip2country_update_database($reg
     );
   }
 
+  $entries   = 0;
+
   // Set a run-time long enough so the script won't break.
   drupal_set_time_limit(10*60);  // 10 minutes!
 
@@ -48,8 +50,8 @@ function ip2country_update_database($reg
   ip2country_empty_database();
 
   // Download data files from the chosen registry.
-  $entries   = 0;
   foreach ($ftp_urls as $ftp_file) {
+
     // Replace Registry source with chosen registry.
     $ftp_file = str_replace('ftp.ripe', 'ftp.' . $registry, $ftp_file);
 
@@ -64,7 +66,19 @@ function ip2country_update_database($reg
     // Break the FTP file into lines.
     $lines = explode("\n", $txt);
     unset($txt);  // Free up memory.
-    foreach ($lines as $line) {
+    $query = db_insert('ip2country')
+               ->fields(array(
+                 'ip_range_first',
+                 'ip_range_last',
+                 'ip_range_length',
+                 'country',
+                 'registry',
+               ));
+    //$chunk_lines = array_chunk($lines, 10000);
+    //unset($lines);
+    //foreach ($chunk_lines as $lines) {
+      foreach ($lines as $line) {
+
       // Trim each line for security.
       $line = trim($line, "\r\t \n");
 
@@ -86,15 +100,16 @@ function ip2country_update_database($reg
       $range_length = (int) $parts[4];
 
       // Insert range into the DB.
-      db_insert('ip2country')->fields(array(
-                'ip_range_first'  => min($ip_start, $ip_end),
-                'ip_range_last'   => max($ip_start, $ip_end),
-                'ip_range_length' => $range_length,
-                'country'         => $country_code,
-                'registry'        => $owner_registry,
-        )
-      )->execute();
+      $query->values(array(
+                min($ip_start, $ip_end),
+                max($ip_start, $ip_end),
+                $range_length,
+                $country_code,
+                $owner_registry,
+        ));
+      $query->execute();
       $entries++;
+      //}
     }
   }
 
@@ -114,11 +129,12 @@ function _ip2country_fetch_page($url) {
   $curl = curl_init();
 
   curl_setopt($curl, CURLOPT_URL, $url);
-  curl_setopt($curl, CURLOPT_TIMEOUT, 60*2);
+  curl_setopt($curl, CURLOPT_TIMEOUT, 60*5);
   curl_setopt($curl, CURLOPT_USERAGENT, 'Drupal (+http://drupal.org/)');
   curl_setopt($curl, CURLOPT_HEADER, FALSE);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
+  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
 
   $html = curl_exec($curl);
   curl_close($curl);
