<!doctype html>
<html>
<head>
<title>aMule control panel</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<?php
	// Auto-refresh: reload on a timer, but skip while any checkbox is
	// checked so the user's selection isn't wiped.
	if ( $_SESSION["auto_refresh"] > 0 ) {
		echo "<script type=\"text/JavaScript\">
	setInterval(function() {
		if (document.querySelectorAll('input[type=\"checkbox\"]:checked').length > 0) {
			return;
		}
		window.location.href = window.location.href;
	}, 1000 * ", $_SESSION["auto_refresh"], ");
</script>";
	}
?>

<link href="style.css" rel="stylesheet" type="text/css">
<script language="JavaScript" type="text/JavaScript">
function formCommandSubmit(command)
{
	<?php
		if ($_SESSION["guest_login"] != 0) {
				echo 'alert("You logged in as guest - commands are disabled");';
				echo "return;";
		}
	?>
	if ( command == "download" ) {
		var boxchecked = document.querySelectorAll('input[type="checkbox"]:checked');
		var selectedFiles = Object.values(boxchecked).filter(selected => selected.name != 'selectAllFiles').length;
		if (selectedFiles == 0)
			return;
		var res = confirm("Download selected " + (selectedFiles) + " files ?")
		if ( res == false ) {
			return;
		}
	}
	var frm=document.forms.mainform
	frm.command.value=command
	frm.submit()
}

function selectAll(check)
{
	var checkboxes = document.querySelectorAll('input[type="checkbox"]');
	checkboxes.forEach(function(checkbox) {
		checkbox.checked = check.checked;
	});
}

</script>
</head>
<body class="main">
<table class="page">
  <tr> 
    <td class="logo-cell"><a href="amuleweb-main-dload.php" title="Home"><img src="images/logo.png" width="143" height="64" alt="aMule"></a></td>
    <td class="navbar-cell"> <table class="navbar-table">
        <tr> 
          <td><a class="navbutton nav-transfer" href="amuleweb-main-dload.php" title="Transfers"></a></td>
          <td><a class="navbutton nav-shared" href="amuleweb-main-shared.php" title="Shared files"></a></td>
          <td><a class="navbutton nav-search" href="amuleweb-main-search.php" title="Search"></a></td>
          <td><a class="navbutton nav-servers" href="amuleweb-main-servers.php" title="Servers"></a></td>
          <td><a class="navbutton nav-kad" href="amuleweb-main-kad.php" title="Kad"></a></td>
          <td><a class="navbutton nav-stats" href="amuleweb-main-stats.php" title="Statistics"></a></td>
          <td><img src="images/col.png"></td>
          <td></td>
          <td><a href="login.php">exit</a><br> 
            <a href="amuleweb-main-log.php">log &bull;</a> <a href="amuleweb-main-prefs.php">configuration</a></td>
          <td></td>
        </tr>
      </table></td>
  </tr>
  <tr> 
    <td colspan="2">
        <table class="tab">
          <caption>
        SEARCH
        </caption>
          <tr> 
            <td><img src="images/tab_top_left.png" width="24" height="24"></td>
            <td>&nbsp;</td>
            <td><img src="images/tab_top_right.png" width="24" height="24"></td>
          </tr>
          <tr> 
            <td>&nbsp;</td>
            
      <td><form name="mainform" action="amuleweb-main-search.php" method="post">
              <table class="w100p pad4">
                <tr class="al-center"> 
                  <td class="al-center">
<input type="hidden" name="command" value=""> 
                    <input name="searchval" type="text" id="searchval4" size="60"> 
                    <input name="Search" type="submit" id="Search4" value="Search" title="Start the search" onClick="javascript:formCommandSubmit('search');"></td>
                  <td class="al-right">Availability :</td>
                  <td class="al-left"> 
                    <input name="avail" type="text" id="avail13" size="6"></td>
                  <td class="al-left">Min Size : </td>
                  <td class="al-left">
<input name="minsize" type="text" id="minsize2" size="5"> 
                    <select name="minsizeu" id="select8">
                      <option>Byte</option>
                      <option>KByte</option>
                      <option selected>MByte</option>
                      <option>GByte</option>
                    </select></td>
                </tr>
                <tr> 
                  <td class="al-center"><input name="UpdateResults" type="button" value="Update results" title="Update the search results" onClick="window.location.href='amuleweb-main-search.php'"></td>
                  <td class="al-right">Search type :</td>
                  <td> 
                    <select name="searchtype" id="select">
                      <option selected>Local</option>
                      <option>Global</option>
                      <option>Kad</option>
                    </select></td>
                  <td>Max Size : </td>
                  <td>
<input name="maxsize" type="text" id="maxsize4" size="5"> 
                    <select name="maxsizeu" id="select10">
                      <option>Byte</option>
                      <option>KByte</option>
                      <option selected>MByte</option>
                      <option>GByte</option>
                    </select></td>
                </tr>
              </table>
              <table class="w100p">
                <tr class="al-right">
                  <td colspan="4" scope="col">
                    <input name="Download" type="submit" id="Download6" value="Download" title="Download selected files" onClick="javascript:formCommandSubmit('download'); return false;" >
                    <select name="targetcat" id="select32">
                      <?php
                	$cats = amule_get_categories();
                	foreach($cats as $c) {
                		$label = ($c == 'all') ? 'No category' : $c;
                		echo '<option value="', htmlspecialchars($c), '">', htmlspecialchars($label), '</option>';
                	}
                ?>
                    </select></td>
                </tr>
                <tr><td colspan="9" class="h10"></td></tr>
                <tr><td colspan="9" class="sep-dark"></td></tr>
                <tr>
                  <th class="al-left"><input type="checkbox" name="selectAllFiles" onclick="selectAll(this)"></th>
                  <th><a href="amuleweb-main-search.php?sort=name">File Name</a></th>
                  <th><a href="amuleweb-main-search.php?sort=size">Size</a></th>
                  <th><a href="amuleweb-main-search.php?sort=sources">Sources</a></th>
    </tr><tr><td colspan="9" class="sep-dark"></td></tr>
    <?php
		function CastToXBytes($size)
		{
			// Emit the raw byte count; the unit formatting is done
			// client-side (see the js-size script at the end of the page).
			return '<span class="js-size">' . $size . '</span>';
		}

		//
		// declare it here, before any function reffered it in "global"
		//
		$sort_order;$sort_reverse;

		function my_cmp($a, $b)
		{
			global $sort_order, $sort_reverse;
			
			switch ( $sort_order) {
				case "size": $result = $a->size > $b->size; break;
				case "name": $result = $a->name > $b->name; break;
				case "sources": $result = $a->sources > $b->sources; break;
			}

			if ( $sort_reverse ) {
				$result = !$result;
			}

			return $result;
		}

		function str2mult($str)
		{
			$result = 1;
			switch($str) {
				case "Byte":	$result = 1; break;
				case "KByte":	$result = 1024; break;		
				case "MByte":	$result = 1024*1024; break;
				case "GByte":	$result = 1024*1024*1024; break;
			}
			return $result;
		}

		function cat2idx($cat)
		{
                	$cats = amule_get_categories();
                	$result = 0;
                	foreach($cats as $i => $c) {
                		if ( $cat == $c) $result = $i;
                	}
            		return $result;
		}

		if ($_SESSION["guest_login"] == 0) {
			if ( $HTTP_GET_VARS["command"] == "search") {
				$search_type = -1;
				switch($HTTP_GET_VARS["searchtype"]) {
					case "Local": $search_type = 0; break;
					case "Global": $search_type = 1; break;
					case "Kad": $search_type = 2; break;
				}
				$min_size = $HTTP_GET_VARS["minsize"] == "" ? 0 : $HTTP_GET_VARS["minsize"];
				$max_size = $HTTP_GET_VARS["maxsize"] == "" ? 0 : $HTTP_GET_VARS["maxsize"];
	
				$min_size *= str2mult($HTTP_GET_VARS["minsizeu"]);
				$max_size *= str2mult($HTTP_GET_VARS["maxsizeu"]);
				
				amule_do_search_start_cmd($HTTP_GET_VARS["searchval"],
					//$HTTP_GET_VARS["ext"], $HTTP_GET_VARS["filetype"],
					"", "",
					$search_type, $HTTP_GET_VARS["avail"], $min_size, $max_size);
			} elseif ( $HTTP_GET_VARS["command"] == "download") {
				foreach ( $HTTP_GET_VARS as $name => $val) {
					// this is file checkboxes
					if ( (strlen($name) == 32) and ($val == "on") ) {
						$cat = $HTTP_GET_VARS["targetcat"];
						$cat_idx = cat2idx($cat);
						amule_do_search_download_cmd($name, $cat_idx);
					}
				}
			} else {
			}
		}		
		$search = amule_load_vars("searchresult");

		// Column-header links use ?sort=<key> and TOGGLE the sort
		// direction on each click. Any request without a (valid) sort
		// key — including the "Update results" refresh, which just
		// reloads the page — falls through to the order remembered in
		// the session, so refreshing the streamed results keeps the
		// current order without flipping it. The key is whitelisted
		// against the keys my_cmp() understands, same pattern as the
		// dload/shared/servers pages: this prevents an attacker-
		// controlled value from being stored in $_SESSION["search_sort"]
		// and later reflected into rendered HTML (#869 follow-up).
		$sort_raw = isset($HTTP_GET_VARS["sort"]) ? $HTTP_GET_VARS["sort"] : "";
		if ($sort_raw == "size" || $sort_raw == "name" || $sort_raw == "sources") {
			$sort_order = $sort_raw;
			if ( $_SESSION["search_sort_reverse"] == "" ) {
				$_SESSION["search_sort_reverse"] = 0;
			} else {
				$_SESSION["search_sort_reverse"] = !$_SESSION["search_sort_reverse"];
			}
		} else {
			$sort_order = $_SESSION["search_sort"];
		}

		$sort_reverse = $_SESSION["search_sort_reverse"];
		if ( $sort_order != "" ) {
			$_SESSION["search_sort"] = $sort_order;
			usort(&$search, "my_cmp");
		}

		foreach ($search as $file) {
			print "<tr>";

			echo "<td class='texte'>", '<input type="checkbox" name="', $file->hash, '" >', "</td>";

			echo "<td class='texte texte-full-name texte-full-name-search'>", htmlspecialchars($file->name), "</td>";
			
			echo "<td class='texte al-center'>", CastToXBytes($file->size), "</td>";

			echo "<td class='texte al-center'>", $file->sources, "</td>";

			print "</tr><tr><td colspan='9' class='sep-light'></td></tr>";
		}

	  ?>
  </table>
</form></td>
            <td>&nbsp;</td>
          </tr>
          <tr> 
            <td><img src="images/tab_bottom_left.png" width="24" height="24"></td>
            <td>&nbsp;</td>
            <td><img src="images/tab_bottom_right.png" width="24" height="24"></td>
          </tr>
        </table></td>
  </tr>
  <tr> 
    <td colspan="2"> <table class="footer-bar">
        <tr> 
          <td> <iframe name="stats" src="footer.php" height="35">edklink</iframe> 
          </td>
          <td> <iframe name="stats" src="stats.php" height="35">connection</iframe> 
          </td>
        </tr>
      </table></td>
  </tr>
</table>
<script type="text/JavaScript">
// Format the raw byte counts emitted by the backend (spans with class
// "js-size") into human-readable units. Done here in the browser because
// the webserver's PHP interpreter lacks sprintf/round.
function formatBytes(value) {
	var b = parseFloat(value);
	if ( isNaN(b) ) return value;
	if ( b < 1024 ) return b + " Bytes";
	if ( b < 1048576 ) return (b / 1024).toFixed(2) + " KB";
	if ( b < 1073741824 ) return (b / 1048576).toFixed(2) + " MB";
	return (b / 1073741824).toFixed(2) + " GB";
}
(function() {
	var els = document.getElementsByClassName("js-size");
	for ( var i = 0; i < els.length; i++ ) {
		els[i].textContent = formatBytes(els[i].textContent);
	}
})();
</script>
</body>
</html>
