<?php
  $page_title = "Allmydata - Deep Manifest";

  require(".inc/includes.php");
  require(".inc/ui/dbconnection.php");

//  echo "<table>";
//  echo "<tr><td><b>Your browser is:</b></td><td>{$_SERVER['HTTP_USER_AGENT']}</td></tr>";
//  echo "<tr><td><b>Your referrer is:</b></td><td>{$_SERVER['HTTP_REFERER']}</td></tr>";
//  echo "</table>";
//  echo "<table>";
//  echo "<tr><td>Num</td><td>Email</td><td>Account ID</td><td>Base URI</td><td>Size</td></tr>";

  // get list of accounts and then figure out amount of space used
  $query = "select * from user where active = 1 and base_uri <> '' order by account_id desc limit 0";
  echo "account_id,CHK,DIR<br>";
  $q_results = mysql_query($query);
  $i = 1;
  while($results = mysql_fetch_array($q_results)) {
    $base_uri = $results['base_uri'];
    $account_id = $results['account_id'];
    $email = $results['email'];
    $ch = curl_init();
    $uri_url = "http://webapi.allmydata.com:8123/uri/";
    $full_url = $uri_url . urlencode($base_uri) . "?t=manifest";
//    echo $full_url;
    curl_setopt($ch, CURLOPT_URL, $full_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $body = curl_exec($ch);
    $manifest_array = explode("URI:", $body);
    $j = 0;
    $CHK_count = 0;
    $DIR_count = 0;
    while($manifest_line = $manifest_array[$j]) {
      //echo '"' . $manifest_line . '"';
      if(strstr($manifest_line, "CHK")) {
        $CHK_count = $CHK_count + 1;
      } else if(strstr($manifest_line, "DIR")) {
        $DIR_count = $DIR_count + 1;
      }
      $j = $j+1;
    }
    echo $account_id . ",";
    echo $CHK_count . ",";
    echo $DIR_count;
    echo "<br>";
//    $inner_query = "update user set used_quota=" . $size . " where account_id = ". $account_id;
//    $results = mysql_query($inner_query);
//    echo "<tr><td>" . $i . "</td><td>" . $email . "</td><td>" . $account_id . "</td><td>" . $base_uri . "</td><td>" . $size . "</td></tr>";
    $i = $i + 1;
    usleep(500000);
  }
//  echo "</table>";
//  mysql_close($connect);
?>
