Bravo List
Register
Go Back   > Bravo List > P2P > Forum > Community Cafe
Reply
  #1  
Old 6th March 2012, 10:44
DAKz's Avatar
DAKz DAKz is offline
Senior Member
 
Join Date: Jul 2009
P2P
Posts: 380
Exclamation Sample magnet link php code
Here is a sample I found of the magnet link php code.
Might not be a bad idea for everyone to start looking at ways to incorporate this into your existing work.
If nothing else it will solve a lot of tracker issues, open up a lot of non-tracker sources and give site users another choice for downloading.

Magnet links library for PHP


PHP Code:
<?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(' '$mix2);
                    
$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($uriPHP_URL_SCHEME);
        
# invalid URI scheme
        
if ($sheme !== 'magnet') return $this->valid false;
        
$query parse_url($uriPHP_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();
    }
}
Output;
Quote:
Magnet URI demo:
magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO 5C

valid: Yes
valid: Yes

exactTopic: urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C
xt: urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C

String output:

Magnet URI:
magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO 5C (valid)

Display Name .... (dn):
eXact Length .... (xl):
eXact Topic ..... (xt): urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C
Acceptable Source (as):
eXact Source .... (xs):
Keyword Topic ... (kt):
Manifest Topic .. (mt):
address TRacker . (tr):
Would be nice to see some of the developers create some mods to add this to the existing code....
just in case this is the way things are headed in the future it would promise the survival of bvlist, and us all.
Plus think about it you could actually brag that your source is magnet link ready.
__________________
Reply With Quote
Reply

Tags
code , link , magnet , php , sample

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT +2. The time now is 03:28. vBulletin skin by ForumMonkeys. Powered by vBulletin® Version 3.8.11 Beta 3
Copyright ©2000 - 2024, vBulletin Solutions Inc.