summaryrefslogtreecommitdiff
path: root/portdb/portdb/index.php
blob: 6cee69e24f3b412b2421f10e420cbae80b52f83b (plain)
    1 <?php
    2 require('DB.php');
    3 # Mini port repostory browser
    4 # Written by Simone Rota <sip@crux.nu>
    5 
    6 $dsn = 'sqlite:////home/crux/public_html/local/portdb.db';
    7 
    8 function sanitize($str) {
    9     return $str;
   10 } 
   11 
   12 function nospam($mail) {
   13     $mail = preg_replace("/\@/", " at ", $mail);
   14     $mail = preg_replace("/\./", " dot ", $mail);
   15 	return htmlspecialchars($mail);
   16 }
   17 
   18 function printHeader() {
   19 	echo '
   20 	<html>
   21 	<head>
   22 	<title>CRUX port browser</title>
   23 	<link rel="stylesheet" type="text/css" href="portdb.css"/>
   24 	</head>
   25 	<body>
   26 	<div class="cruxheader">
   27 	<a href="/" title="">Home</a> :: 
   28 	<a href="/Main/Documentation">Documentation</a> :: 
   29 	<a href="/Main/Download">Download</a> :: 
   30 	<a href="/Main/Development">Development</a> :: 
   31 	<a href="/Main/Community">Community</a> :: 
   32 	<a href="/Public/HomePage">Public</a> :: 
   33 	<a href="/portdb">Ports</a> :: 
   34 	<a href="/Main/Bugs" title="">Bugs</a> :: 
   35 	<a href="/Main/Links" title="">Links</a> :: 
   36 	<a href="/Main/About" title="">About</a>
   37 	</div>
   38 	';
   39 }
   40 
   41 function printFooter() {
   42     echo '</div><div class="footer"><b>DISCLAIMER</b>: the ports not belonging to the core and opt collections are provided by contributors; there is no guarantee or support by the CRUX team.</div></body></html>';
   43 }
   44 
   45 function printNav() {
   46     echo '<div class="content"><b>Sections: </b><a href="?a=index">Repositories</a> :: <a href="?a=search">Search</a> :: <a href="?a=register">Register</a> :: <a href="?a=dups">Duplicates</a>';
   47 }
   48 
   49 function showCollections() {
   50     global $db;
   51     $sql = "select collname,maintainer,colltype,url,count(*) as tot from collections
   52         join ports on collection=collname
   53         group by collname order by collections.collid";
   54     $res =& $db->Query($sql);
   55     if (DB::isError($res)) die("Query error");
   56     echo "<h2>Overview of available repositories</h2>\n";
   57     echo '<table class="listing">';
   58     echo '<thead><tr><th>Repo Name</th><th># ports</th><th>Type</th><th>Maintainer</th><th>Repo URL</th></tr></thead>';
   59     $i=0;
   60 	while ($repo =& $res->fetchRow()) {
   61 		$cl = "row".$i%2;
   62         echo '<tr class="'.$cl.'">
   63                 <td><a href="?a=repo&q='.$repo['collname'].'">'.$repo['collname'].'</a></td>
   64                 <td>'.$repo['tot'].'</td>
   65                 <td><a href="?a=getup&q='.$repo['collname'].'">'.$repo['colltype'].'</a></td>
   66                 <td>'.nospam($repo['maintainer']).'</td>';
   67        if ($repo['colltype'] == "httpup") {
   68             echo '<td><a href="'.$repo['url'].'">'.$repo['url'].'</a></td></tr>';
   69         } else {
   70             echo '<td>'.$repo['url'].'</td></tr>';
   71        }
   72 	   $i++;
   73     }
   74     echo "</table>";
   75  
   76 }
   77 
   78 function showSearch($singlerepo) {
   79     global $db;
   80     $query = sanitize($_GET['q']);
   81 	$strict = sanitize($_GET['s']);
   82     if ($query == "") {
   83         echo '<h2>Simple port search</h2>
   84                 <p>Search for ports by name</p>
   85                 <form name="searchform" method="get">
   86                 <input name="q" value="">
   87                 <input type=hidden name="a" value="search">
   88                 <input value="search" type="submit">
   89                 </form>';
   90     } else {
   91         if ($singlerepo) {
   92             echo "<h2>Ports in repository '$query'".' (<a href="?a=getup&q='.$query.'">get httpup/rsync</a>)'."</h2>";
   93             $sql = "select * from ports join collections on collection=collname
   94                 where collection = ? order by portname";
   95         } else {
   96             echo "<h2>Search results for '$query'</h2>";
   97 			if ($strict === "true") {
   98             	$sql = "select * from ports join collections on collection=collname
   99                 	where portname=? order by portname, collection";
  100 			} else {
  101             	$query = "%".$query."%";
  102             	$sql = "select * from ports join collections on collection=collname
  103                 	where portname like ? order by portname, collection";
  104 			}
  105         }
  106         $sth = $db->prepare($sql);
  107         $res = $db->execute($sth, array($query));
  108         if (DB::isError($res)) die($res->getDebugInfo());#die("Query error (search)");
  109 	echo "\n<table class=\"listing\"><thead><tr><th>Port</th><th>Collection</th><th>Files</th><th>Download command</th>\n</tr></thead>\n";
  110 	$i = 0;
  111 	while ($port =& $res->fetchRow()) {
  112 		$cl = "row".$i%2;
  113 	    echo '<tr class="'.$cl.'">
  114 		<td>'.$port['ports.portname'].'</td>
  115 		<td><a href="?a=repo&q='.$port['collections.collname'].'">'.$port['collections.collname']."</a></td>\n";
  116 	    if ($port['collections.colltype'] == "httpup") {
  117 		echo '<td><a href="'.$port['collections.url'].$port['ports.portname'].'/Pkgfile">P</a> '."\n";
  118 		echo '<a href="'.$port['collections.url'].$port['ports.portname'].'/.footprint">F</a> '."\n";
  119 		echo '<a href="'.$port['collections.url'].$port['ports.portname'].'/.md5sum">M</a></td>'."\n";
  120 		echo '<td>httpup sync '.trim($port['collections.url'],"/")."#".$port['ports.portname']." ".$port['ports.portname'] .'</td>'."\n";
  121 	    } else {
  122 		echo '<td>-</td>'."\n";
  123 		echo '<td>rsync -aqz '.$port['collections.url'].$port['ports.portname']."/ ".$port['ports.portname'] .'</td>'."\n";
  124 	    }
  125 	    echo "</tr>\n";
  126 		$i++;
  127 	}
  128 	echo "</table>";
  129     }
  130 }
  131 
  132 function showRegister() {
  133 	$query = sanitize($_GET['q']);
  134 	echo "<h2>Register your ports repository</h2>";
  135 	echo "<p>You can register your personal HttpUp repository sending an email with the following information to <i>contrib-admin at crux dot nu</i>:</p>";
  136 	echo '<ul><li>Repository Name <small>(e.g. myports)</small></li>
  137 		<li>Root URL <small>(e.g. http://mypage.se/ports/)</small></li>
  138 		<li>Your name</li>
  139 		<li>Your Email</li></ul>';
  140     echo "<p>A CRUX team member will put your repository into our database. Please give us some time to do this. Once it is active it is synced once a day.  <b>Please do not submit .httpup files, only the URL for the repository. This means the URL to the repository itself, not portspage or other indexes.</b>";
  141 }
  142 
  143 function showDuplicates() {
  144     global $db;
  145     $sql = "select portname, count(*) as dup from ports
  146 				group by portname
  147 				having dup>1
  148 				order by dup desc;";
  149     $res =& $db->Query($sql);
  150     if (DB::isError($res)) die("Query error");
  151     echo "<h2>List of duplicate ports</h2>\n";
  152     echo '<table class="listing">';
  153     echo '<thead><tr><th>Ports</th><th># of duplicates</th></tr></thead>';
  154     $i=0;
  155 	while ($port =& $res->fetchRow()) {
  156         $cl = "row".$i%2;
  157 	    echo '<tr class="'.$cl.'">
  158 		<td>'.$port['portname'].'</td>
  159 		<td>Found in <a href="?a=search&s=true&q='.$port['portname'].'">'.$port['dup']." repositories</a></td></tr>\n";
  160         $i++;
  161 	}
  162 	echo "</table>";
  163 }
  164 
  165 function getUp() {
  166     global $db;
  167     $query = sanitize($_GET['q']);
  168     $sql = "select * from collections where collname=?";
  169     $sth = $db->prepare($sql);
  170     $res = $db->execute($sth, array($query));
  171     if (DB::isError($res)) die("Query error");
  172     if ($res->numRows() != 1)
  173         die("Error generating the file");
  174     $coll =& $res->fetchRow();
  175     header('Content-type: text/plain');
  176     header('Content-Disposition: attachment; filename="'.$coll['collname'].".".$coll['colltype'].'"');
  177     echo "# Collection ".$coll['collname'].", by ".$coll['maintainer']."\n";
  178     echo "# File generated by the CRUX portdb http://crux.nu/portdb/"."\n\n";
  179     if ($coll['colltype'] == "httpup") {
  180         echo "ROOT_DIR=/usr/ports/" . $coll['collname']."\n";
  181         echo "URL=" . $coll['url']."\n";
  182     } else {
  183         $ar = explode('::', $coll['url']);
  184         echo "host=" . $ar[0]."\n";
  185         echo "collection=" . $ar[1]."\n";
  186         echo "destination=/usr/ports/" . $coll['collname']."\n";
  187     }
  188 }
  189 
  190 $db =& DB::connect($dsn);
  191 if (DB::isError($dbc)) die("Cannot connect to database");
  192 $db->setFetchMode(DB_FETCHMODE_ASSOC);
  193 
  194 $action = sanitize($_GET['a']);
  195 if ($action == "getup") {
  196     getUp();
  197     exit(0);
  198 }
  199 
  200 printHeader();
  201 printNav();
  202 
  203 switch ($action) {
  204     case "index":
  205         showCollections();
  206         break;
  207     case "search":
  208         showSearch(FALSE);
  209         break;
  210     case "repo":
  211         showSearch(TRUE);
  212         break;
  213     case "register":
  214         showRegister();
  215         break;
  216     case "dups":
  217         showDuplicates();
  218         break;
  219     default:
  220         showCollections();
  221 }
  222 
  223 printFooter();
  224 ?>

Generated by cgit