<?php
/**
* Prints {@link http://magnet-uri.sf.net MAGNET} links for
* specified files.  Uses
* {@link http://bitzi.com/bitcollider/ bitcollider} if
* installed, standalone functions if not.
*
* Demonstrates use of Bitcollider class and functions
* available in bitcollider.php.
*/
include('bitcollider.php');

$USE_BITCOLLIDER = true;

function
usage() {
  echo(
"Usage: php magnetlink.php [file ...]\n");
}

if (
$argc < 2) {
  
usage();
}

function
bitcollide($file) {
  
$b = new Bitcollider();
  if (!
$b->analyze_file($file)) {
    return
false;
  }
  
$magnetlink = $b->get_magnetlink();
  echo(
"$magnetlink\n");
  return
true;
}

function
bitfallback($file) {
  
$sha1_hex = sha1_file($file);
  
$sha1_base32 = hex_to_base32($sha1_hex);
  
$magnetlink = magnetlink($sha1_base32,$file,'','','');
  echo(
"$magnetlink\n");
}

foreach (
array_slice($argv,1) as $arg) {
  if (!
is_readable($arg)) {
    echo(
"Cannot read: ".$arg."\n");
    continue;
  }
  if (
$USE_BITCOLLIDER) {
    if (!
bitcollide($arg)) {
      
$USE_BITCOLLIDER = false;
      
bitfallback($arg);
    }
  } else {
    
bitfallback($arg);
  }
}
?>