<?php
$container=$_REQUEST['container'];
if (!preg_match(',^[a-f0-9]+$,', $container)) exit(1);
exec('docker inspect '.$container, $res, $ret);
if ($ret!=0) exit(1);
$containers=json_decode(join($res), true);
?>
<div id="tabs">
   <ul>
       <li><a href="#tabs-1">Overview</a></li>
       <li><a href="#tabs-2">Logs</a></li>
       <li><a href="#tabs-3">Dump</a></li>
   </ul>
   <div id="tabs-1">
       <table class="details docker"><thead>
         <tr><th>Name</th><th>Ports</th><th>Volumes</th><th>Links</th><th>Environments</th><th>Image</th><th>Command</th></tr>
       </thead><tbody>
<?php
foreach ($containers as $c) {
  $name = preg_replace(',^/,', '', $c['Name']);
  $ports = "<ul>";
  if ($c['NetworkSettings']['Ports']) {
    foreach ($c['NetworkSettings']['Ports'] as $from => $to) {
      if ($to)
        foreach ($to as $p) {
          if (isset($p['HostPort']))
            $ports.="<li>-p ".($p['HostIp']&&$p['HostIp']!="0.0.0.0"?$p['HostIp'].":":"").$p['HostPort'].':'.$from.'</li>';
        }
    }
  } elseif ($c['HostConfig']['PortBindings']) {
    foreach ($c['HostConfig']['PortBindings'] as $from => $to) {
      if ($to)
        foreach ($to as $p) {
          if (isset($p['HostPort']))
            $ports.="<li>-p ".($p['HostIp']&&$p['HostIp']!="0.0.0.0"?$p['HostIp'].":":"").$p['HostPort'].':'.$from.'</li>';
        }
    }
  }
  $ports .= "</ul>";
  $volumes = "?";
  $links = "<ul>";
  if ($c['HostConfig']['Links'])
    foreach ($c['HostConfig']['Links'] as $link)
      $links.="<li>--link ".preg_replace(',^/?(.*):/?'.$name.'/?(.*)$,', '${1}:${2}', $link)."</li>";
  $links .= "</ul>";
  $envs = "<ul>";
  if ($c['Config']['Env'])
    foreach ($c['Config']['Env'] as $env)
      if (!preg_match(',^PATH=,', $env))
        $envs.="<li>-e ".$env."</li>";
  $envs .= "</ul>";
  $image = $c['Config']['Image'];
  $command = "";
  if ($c['Config']['Cmd']) $command = join(" ", $c['Config']['Cmd']);
  $status="dead";
  if ($c['State']['Dead']) $status = "dead";
  elseif ($c['State']['Restarting']) $status = "restarted";
  elseif ($c['State']['Paused']) $status = "paused";
  elseif ($c['State']['Running']) $status = "running";
  echo '<tr class="'.$status.'"><td><a href="javascript:details('."'".$c['Id']."'",')">'.$name.'</a></td><td>'.$ports.'</td><td>'.$volumes.'</td><td>'.$links.'</td><td>'.$envs.'</td><td>'.$image.'</td><td>'.$command.'</td></tr>'."\n";
}
echo "</tbody></table>";
echo'</div><div id="tabs-2">';
exec('docker logs '.$container, $logs, $ret);
echo "<pre>\n".join("\n", $logs)."\n</pre>";
echo'</div><div id="tabs-3">';
echo "<pre>\n".join("\n", $res)."\n</pre>";
echo'</div></div>';
?>
<script>
    $(function() {$("#tabs").tabs();});
</script>
