function getMobileVersion($apkfile)
{
    $result = array();
    $apkinfo = new ApkParser($apkfile);
    $manifest = $apkinfo->getManifest();
    $result['apk_version'] = $manifest->getVersionName();
    $archive = $apkinfo->getApkArchive();
    $stream = $archive->getStream("assets/total-conversion-build.user.js");
    if ($stream) {
        $header = loadUserScriptHeader($stream);
        $result['iitc_version'] = $header['@version'];
    } else {
        $result['iitc_version'] = 'unknown';
    }
    return $result;
}
                $header = loadUserScriptHeader($stream);
                $response['mobile']['iitc_version'] = $header['@version'];
            }
        } else {
            $response['error'] = 'Failed to find .apk file ' . $apkfile;
        }
    } else {
        // desktop - .user.js scripts
        // load main script version
        $iitc_details = loadUserScriptHeader("{$dir}/total-conversion-build.user.js");
        $response['iitc'] = array('version' => $iitc_details['@version'], 'downloadUrl' => url_to_absolute($pageurl, "{$dir}/total-conversion-build.user.js"), 'pageUrl' => url_to_absolute($pageurl, $info['web']));
        // and now the plugins
        $response['plugins'] = array();
        foreach (glob("{$dir}/plugins/*.user.js") as $path) {
            $basename = basename($path, ".user.js");
            $details = loadUserScriptHeader($path);
            $response['plugins'][$basename] = array('version' => $details['@version'], 'downloadUrl' => url_to_absolute($pageurl, "{$dir}/plugins/{$basename}.user.js"), 'pageUrl' => url_to_absolute($pageurl, $info['web'] . "#plugin-{$basename}"));
        }
    }
} else {
    $response['error'] = 'Unsupported build for version check';
}
$data = json_encode($response);
# send the response - allow either jsonp (using a 'callback' parameter), or regular json
if (array_key_exists('callback', $_GET)) {
    header('Content-Type: text/javascript; charset=utf8');
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Max-Age: 3628800');
    header('Access-Control-Allow-Methods: GET, POST');
    $callback = $_GET['callback'];
    echo $callback . '(' . $data . ');';
function iitcDesktopPluginDownloadTable($build)
{
    $categories = array('Portal Info' => "Enhanced information on the selected portal", 'Info' => "Display additional information", 'Keys' => "Manual key management", 'Controls' => "Map controls/widgets", 'Highlighter' => "Portal highlighters", 'Layer' => "Additional map layers", 'Map Tiles' => "Alternative map layers", 'Tweaks' => "Adjust IITC settings", 'Misc' => "Unclassified plugins", 'Obsolete' => "Plugins that are no longer recommended, due to being superceded by others or similar", 'Deleted' => "Deleted plugins - listed here for reference only. No download available");
    $popularity = loadPopularity();
    $plugins = array();
    foreach (glob("{$build}/plugins/*.user.js") as $path) {
        $basename = basename($path, ".user.js");
        $details = loadUserScriptHeader($path);
        if (array_key_exists('total-conversion-build', $popularity) && array_key_exists('plugins/' . $basename, $popularity)) {
            $details['popularity'] = $popularity['plugins/' . $basename] / $popularity['total-conversion-build'];
        }
        $plugins[$basename] = $details;
        $category = array_key_exists('@category', $details) ? $details['@category'] : 'Misc';
        if (!array_key_exists($category, $categories)) {
            # add missing categories
            $categories[$category] = '';
        }
    }
    ksort($plugins);
    uasort($plugins, 'popularity_cmp');
    ?>
<table class="table table-condensed">
 <thead>
  <tr>
   <th>Name</th>
   <th>Popularity</th>
   <th>ID</th>
   <th>Version</th>
<!--
   <th>Description</th>
-->
   <th>Download</th>
  </tr>
 </thead>
 <tbody>

<?php 
    foreach ($categories as $category => $category_desc) {
        print "<tr class=\"category_header\"><th colspan=\"1\">Category: {$category}</th><td colspan=\"4\">{$category_desc}</td></tr>\n";
        $empty = True;
        foreach ($plugins as $basename => $details) {
            $path = "{$build}/plugins/{$basename}.user.js";
            $this_category = array_key_exists('@category', $details) ? $details['@category'] : 'Misc';
            if ($category != $this_category) {
                continue;
            }
            $empty = False;
            print "<tr id=\"plugin-{$basename}\">\n";
            # remove 'IITC Plugin: ' prefix if it's there, for neatness
            $name = preg_replace('/^IITC plugin: /i', '', $details['@name']);
            # format extended version info in less prominent font
            $version = preg_replace('/^(\\d+\\.\\d+\\.\\d+)\\.(\\d{8}\\.\\d{1,6})/', '\\1<br><small class="text-muted">.\\2</small>', $details['@version']);
            # remove unneeded prefix from description
            $description = preg_replace('/^\\[[^]]*\\] */', '', $details['@description']);
            $plugin_users = "-";
            if (array_key_exists('popularity', $details)) {
                $plugin_users = (int) ($details['popularity'] * 1000) / 10 . "%";
            }
            print "<td class=\"name\">{$name}</td>";
            print "<td class=\"popularity\">{$plugin_users}</td>";
            print "<td class=\"id\">{$basename}</td>";
            print "<td class=\"version\" rowspan=\"2\">{$version}</td>";
            if ($category != "Deleted") {
                print "<td class=\"download\" rowspan=\"2\"><a onclick=\"if(track){track('desktop','iitc-plugin-{$basename}','{$build}');}\" href=\"https://static.iitc.me/{$path}\" target=\"_blank\" class=\"btn btn-small btn-primary\" title=\"Download\"><span class=\"glyphicon glyphicon-download\"></span></a></td>";
            }
            print "</tr>\n";
            print "<tr><td class=\"description\" colspan=\"3\" style=\"border-top: none; padding-top: 0; padding-bottom: 0.5em\">{$description}</td></tr>\n";
        }
        if ($empty) {
            print "<tr><td class=\"empty\" colspan=\"4\">(no plugins in this category)</td></tr>\n";
        }
    }
    ?>
 </tbody>
</table>

<?php 
}