2

I have a program that I've created for my School District to use to share files & other information in a network.

What I'm trying to do is to create a "Magnet Link" type of button on the intranet site so that users can click on it and open an external program that downloads the file from the server and decompresses it.

If anyone could provide more information, or help me know what to look for, that'd be great.

Like how you can click on a link on Curse and it opens it in the client, ect. A popup would appear asking "Are you sure this site may open this application?"

Trennon Dunn
  • 21
  • 1
  • 3

2 Answers2

2

You need to register your application as a protocol handler.

When a link using your custom protocol (ie myapp://server/file is clicked, your application will be launched with the URI on the command line. It is up to you how the URIs are formatted and parsed.

josh3736
  • 139,160
  • 33
  • 216
  • 263
1

Here's a reference to the issue: Magnet links library for PHP

And you can use this class to create them, referenced here: https://gist.github.com/ehime/03c02757b4bab58fef2c

<?php
/**
 * Magnet links library for PHP
 *
 * @link https://stackoverflow.com/questions/6634059/magnet-links-library-for-php
 */

$magnetUri = 'magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C';

echo 'Magnet URI demo: ', $magnetUri, "\n\n";

$mUri = new MagnetUri($magnetUri);

# Check if the mUri is valid:
echo '     valid: ', $mUri->isValid() ? 'Yes' : 'No', "\n";
# ->valid works as well:
echo '     valid: ', $mUri->valid ? 'Yes' : 'No', "\n\n";

# Access Parts of the URI by their name:
echo 'exactTopic: ', $mUri->exactTopic, "\n";

# Same for the parameter:
echo '        xt: ', $mUri->xt, "\n";

echo "\nString output:\n\n";
echo (string) $mUri;

/**
 * MagnetUri
 * 
 * Parser and validator for MagnetUris
 * 
 * Supports the following parameters:
 * 
 * @@support-params-start
 * dn (Display Name) - Filename
 * xl (eXact Length) - Size in bytes
 * xt (eXact Topic) - URN containing file hash
 * as (Acceptable Source) - Web link to the file online
 * xs (eXact Source) - P2P link.
 * kt (Keyword Topic) - Key words for search
 * mt (Manifest Topic) - link to the metafile that contains a list of magneto (MAGMA - MAGnet MAnifest)
 * tr (address TRacker) - Tracker URL for BitTorrent downloads
 * @@support-params-end
 */
class MagnetUri {
    private $def;
    private $uri;
    private $data;
    private $valid=false;
    private function initDefFromLines(array $lines) {
        $state = 0;
        foreach($lines as $line) {
            if ($state) {
              if ($line === ' * @@support-params-end') break;
              $line = ltrim($line, '* ');
              list($mix, $desc) = explode(' - ', $line);
              list($key, $name) = explode(' ', $mix, 2);
              $name = trim($name, '()');
              $this->def['keys'][$key] = $name;
              $norm = strtolower(str_replace(' ', '', $name));
              $this->def['names'][$norm] = $key;

            }
            if ($line === ' * @@support-params-start') $state = 1;
        }
        if (!$state || null === $this->def) {
            throw new Exception('Supported Params are undefined.');
        }
    }
    private function init() {
        $refl = new ReflectionClass($this);
        $this->initDefFromLines(explode("\n", str_replace("\r", '', $refl->getDocComment())));
    }
    private function getKey($param) {
        $param = strtolower($param);
        $key = false;
        if (isset($this->def['keys'][$param]))
            $key = $param;
        elseif (isset($this->def['names'][$param]))
            $key = $this->def['names'][$param];
        return $key;
    }
    public function __isset($name) {
        return false !== $this->getKey($name);
    }
    public function __get($name) {
        if ($name === 'valid') {
            return $this->valid;
        }
        if (false === $key = $this->getKey($name)) {
            $trace = debug_backtrace();
            trigger_error(
                'Undefined property ' . $name .
                ' in ' . $trace[0]['file'] .
                ' on line ' . $trace[0]['line'],
                E_USER_NOTICE)
                ;                
            return null;
        }
        return isset($this->data[$key])?$this->data[$key]:'';
    }
    public function setUri($uri) {
        $this->uri = $uri;
        $this->data = array();
        $sheme = parse_url($uri, PHP_URL_SCHEME);
        # invalid URI scheme
        if ($sheme !== 'magnet') return $this->valid = false;
        $query = parse_url($uri, PHP_URL_QUERY);
        if ($query === false) return $this->valid = false;
        parse_str($query, $data);
        if (null == $data) return $this->valid = false;
        $this->data = $data;
        return $this->valid = true;
    }
    public function isValid() {
        return $this->valid;
    }
    public function getRawData() {
        return $this->data;
    }
    public function __construct($uri) {
        $this->init();
        $this->setUri($uri);
    }
    public function __toString() {
        ob_start();
        printf("Magnet URI: %s (%s)\n\n", $this->uri, $this->valid?'valid':'invalid');
        $l = max(array_map('strlen', $this->def['keys']));
        foreach($this->def['keys'] as $key => $name) {
            printf("  %'.-{$l}.{$l}s (%s): %s\n", $name.' ', $key, $this->$key);
        }
        return ob_get_clean();
    }
}
Community
  • 1
  • 1
ehime
  • 8,025
  • 14
  • 51
  • 110