Fix for download counts.

Change-Id: I020ae684c47c253b29ea9bf019a2ce3b414b37ef
Signed-off-by: Denis Roy <denis.roy@eclipse-foundation.org>
diff --git a/download.php b/download.php
index b51b2e7..d5f431f 100755
--- a/download.php
+++ b/download.php
@@ -123,7 +123,7 @@
   $_url  = "";
 
   if($_debug == 1) {
-    echo "Mirror id: " . $_mirror_id . "<br />";
+    echo "Running on hostname " . gethostname() . "Mirror id: " . $_mirror_id . "<br />";
   }
 
 
@@ -234,42 +234,6 @@
 
   $app = new App();
 
-  # Process an incoming request for a committers-only download
-  if(isset($_file_id)) {
-    if($_file_id > 0) {
-      if($Friend->getIsCommitter()) {
-        $sql = "SELECT file_name FROM download_file_index WHERE file_id = " . $app->sqlSanitize($_file_id);
-        $rs = mysqli_query($dbh, $sql);
-        if($myrow = mysqli_fetch_assoc($rs)) {
-          $file_name   = $app->getDownloadBasePath() . $myrow['file_name'];
-
-          if(file_exists($file_name)) {
-            logDownload($_file_id, 4, $remote_addr);
-            header('Content-Description: File Transfer');
-            header('Content-Type: application/octet-stream');
-            header('Content-Disposition: attachment; filename=' . basename($file_name));
-            header('Content-Transfer-Encoding: binary');
-            header('Expires: 0');
-            header('Cache-Control: must-revalidate');
-            header('Pragma: public');
-            header('Content-Length: ' . filesize($file_name));
-            ob_clean();
-            flush();
-            readfile($file_name);
-            exit;
-          }
-        }
-      }
-      else {
-        header("HTTP/1.0 403 Forbidden");
-        echo "<html><head><title>403 Forbidden</title></head><body><h1>403 Forbidden</h1><p>Sorry, this type of request is for Eclipse committers only. Please <a href='" . $Session->getLoginPageURL() . "'>log in</a> if you are a committer and wish to download this file.</p>";
-        echo "<p>If you are trying to download Eclipse, please go to <a href='/downloads/'>Eclipse Downloads</a>. Otherwise, please copy this entire message and notify webmaster@eclipse.org.<br /><br />";
-        echo $_SERVER['HTTP_REFERER'] . "</p></body></html>";
-        exit;
-      }
-    }
-  }
-
   # find lowest drop location for this file
   # file could be in Release, which is in Full, which is in EclipseFull,
   # so we want to find all the possible mirrors
@@ -292,7 +256,7 @@
     # using left joins for the download_file_index took .55 sec, two queries uses < .15 sec
 
     # q1: get file info from slave
-    $sql = "SELECT /* downnload.php lowest_drop */ file_id, timestamp_disk, IF(md5sum = '0', '', md5sum) AS md5sum, IF(sha1sum = '0', '', sha1sum) AS sha1sum, IF(sha512sum = '0', '', sha512sum) AS sha512sum FROM download_file_index WHERE file_name = '$_file' ORDER BY file_id asc LIMIT 1";
+    $sql = "SELECT /* download.php lowest_drop */ file_id, timestamp_disk, IF(md5sum = '0', '', md5sum) AS md5sum, IF(sha1sum = '0', '', sha1sum) AS sha1sum, IF(sha512sum = '0', '', sha512sum) AS sha512sum FROM download_file_index WHERE file_name = '$_file' ORDER BY file_id asc LIMIT 1";
     if($_debug == 1) {
         echo $sql . "<br />";
     }
@@ -305,13 +269,16 @@
       $md5sum    = $myrow['md5sum'];
       $sha1sum  = $myrow['sha1sum'];
       $sha512sum  = $myrow['sha512sum'];
-    }
-    # If this is a new file, check the master DB in case the slave is simply lagged
-    if($file_id == 0 || $file_id == "") {
       if($_debug == 1) {
-        echo "This is a new file.  Checking the Master DB.<br />";
+        echo "Got file ID from secondary DB: $file_id (time: $filetime)<br />";
       }
-      # Lock tables for write
+    }
+    else {
+      if($_debug == 1) {
+        echo "Failed to get file info on secondary DB. Checking the Primary DB.<br />";
+      }
+      # Lock tables for write in case it's a new file
+      # todo: this is not Galera-safe
       $rs = mysqli_query($dbh_RW, "LOCK TABLES download_file_index WRITE");
       $rs = mysqli_query($dbh_RW, $sql);
       if($myrow = mysqli_fetch_assoc($rs)) {
@@ -320,13 +287,23 @@
         $md5sum    = $myrow['md5sum'];
         $sha1sum  = $myrow['sha1sum'];
         $sha512sum  = $myrow['sha512sum'];
+        if($_debug == 1) {
+            echo "Got file ID from primary DB: $file_id (time: $filetime)<br />";
+        }
+      }
+      else {
+        # this is a new file. Needs to be added to index, which is the next step.
+        $file_id = addFileToIndex($_file);
+        if($_debug == 1) {
+          echo "No file information from the database. Adding to index to clear table locks";
+        }
       }
     }
 
     # No file time in the index.  Check the filesystem
-    if($filetime == 0 || $file_id == 0 || $file_id == "") {
+    if($filetime == 0) {
       if($_debug == 1) {
-        echo "Checking filesystem for $filename time.<br />";
+        echo "Checking download filesystem for $filename time.<br />";
       }
       if(@fopen($filename, "r")) {
         $filetime = @filemtime($filename);
@@ -334,17 +311,16 @@
       }
 
       if(!$filetime || $filetime == 0) {
+        if($_debug == 1) {
+          echo "Checking archive filesystem for $filename time.<br />";
+        }
         # check on archive.eclipse.org
         $filename = $archiveBasePath . $_file;
         if(@fopen($filename, "r")) {
           $filetime = @filemtime($filename);
           $filesize = @filesize($filename);
-          # we finally have a filetime after examining disk.  Add it to the index if we're logging downloads.
-          if($log_download) {
-            $file_id = &addFileToIndex($_file);
-            $dbc_RW->disconnect();
-          }
-          # File exists on archive.eclipse.org.  No mirrors, or redirect to the actual file.
+
+          # File exists on archive.eclipse.org.  Since there are no mirrors, redirect to the actual file.
           if($_format == "xml") {
             # we are being asked for an xml list of mirrors for a file that we've established has none.
             # we must unset $rs (the mysql resource) as it contains a resultset for the lowest_drop query, not the mirrors query
@@ -379,13 +355,11 @@
           return FALSE;
         }
       }
-      else {
-        # We found a new file, let's add it to download_file_index.
-        $file_id = addFileToIndex($_file);
-        $filetime_update = true;
-      }
+      # We have a file_id, but the timestamp was zero. Update the index
+      $filetime_update = true;
     }
 
+
     $sql = "SELECT key_value FROM SYS_variables WHERE key_name = 'download_table' LIMIT 1";
     $rs = mysqli_query($dbh, $sql);
     if($myrow = mysqli_fetch_assoc($rs)) {
@@ -501,7 +475,8 @@
     echo "Pre phase-II<br /> URL: $_url <br />File: $_file:<br />Mirror: $_mirror_id";
   }
 
-  # Phase 2 - a user picked a mirror for his file, or redirect, or there are no mirrors
+  # Phase II - a user picked a mirror for his file, or redirect, or there are no mirrors
+  # ready the download, and log it
   if(($_file != "" && $_mirror_id > 0 && $_url != "" && $_format != "xml") || $_debug) {
     $ip     = $remote_addr;
     $cnt    = 0;   # recent downloads
@@ -513,6 +488,23 @@
       include("content/en_too_many_downloads.php");
     }
     else {
+      # Successful download. Log, and day goodbye
+      # this code is more reliably run before the header is sent
+      if($_debug == 1) {
+        echo "Logging the download if log_download is true<br>";
+      }
+      if ($log_download && !empty($file_id)) {
+        $sql = "INSERT INTO $log_download_table (file_id, download_date, remote_host, remote_addr, mirror_id, ccode)
+        VALUES ( $file_id, NOW(), '$client_hostname', '$ip', $_mirror_id, '$_country_code')  ";
+        mysqli_query($dbh_RW, $sql);
+        $string = "";
+        if($filetime_update) {
+          $string = ", timestamp_disk = $filetime";
+        }
+        $sql = "UPDATE LOW_PRIORITY download_file_index SET download_count = download_count + 1 " . $string . " WHERE file_id = " . $file_id;
+        mysqli_query($dbh_RW, $sql);
+      }
+
       if($_redirect == 1) {
         # Blindly send user off to a mirror site
         header("Location: " . $_url);
@@ -531,19 +523,6 @@
           header("Location: " . $_url);
         }
       }
-
-      if ($log_download && !empty($file_id)) {
-        $sql = "INSERT DELAYED INTO $log_download_table (file_id, download_date, remote_host, remote_addr, mirror_id, ccode)
-          VALUES ( $file_id, NOW(), '$client_hostname', '$ip', $_mirror_id, '$_country_code')  ";
-        mysqli_query($dbh_RW, $sql);
-
-        $string = "";
-        if($filetime_update) {
-          $string = ", timestamp_disk = $filetime";
-        }
-        $sql = "UPDATE LOW_PRIORITY download_file_index SET download_count = download_count + 1 " . $string . " WHERE file_id = " . $file_id;
-        mysqli_query($dbh_RW, $sql);
-      }
     }
 
     if($_debug == 1) {
@@ -653,6 +632,10 @@
   }
 
   function logDownload($in_file_id, $in_mirror_id, $ip = NULL) {
+    # todo: delete this function, it is never called.
+    if($_debug == 1) {
+      echo "This is unused codecode<br />";
+    }
     if (is_null($ip)) {
       $ip = $_SERVER['REMOTE_ADDR'];
     }
@@ -661,7 +644,7 @@
 
 
     if ($in_file_id != "") {
-      $sql = "INSERT DELAYED INTO $log_download_table (file_id, download_date, remote_host, remote_addr, mirror_id, ccode)
+      $sql = "INSERT INTO $log_download_table (file_id, download_date, remote_host, remote_addr, mirror_id, ccode)
         VALUES ( $in_file_id, NOW(), '$client_hostname', '$ip', $in_mirror_id, '$_country_code')  ";
       if($_debug == 1) {
         echo $sql . "<br />";
@@ -680,7 +663,7 @@
       error_log('Adding file to index: addFileToIndex()');
       error_log('File ($in_file): ' . var_export($in_file, TRUE));
       error_log('Filetime ($filetime): ' . var_export($filetime, TRUE));
-      error_log('Filesize ($filesize): ' . var_export($filesize, TRUE));
+      error_log('Filesize ($filesize): ' . var_export($filesize, TRUE)); 
     }
 
     # Add this file to the file index
@@ -692,6 +675,7 @@
       mysqli_query($dbh_RW, "UNLOCK TABLES");
       if ($_debug == 1) {
         error_log('Inserted ID ($inserted_id): ' . var_export($inserted_id, TRUE));
+        echo "Added file " . var_export($in_file, TRUE) . "to index<br>";
       }
       return $inserted_id;
     }