当前位置:首页 > PHP教程 > php高级应用 > 列表

php读取torrent种子文件内容的方法(测试可用)

发布:smiling 来源: PHP粉丝网  添加日期:2019-08-12 11:48:48 浏览: 评论:0 

本文实例讲述了php读取torrent种子文件内容的方法。分享给大家供大家参考,具体如下:

  1. <?php 
  2.  
  3. /** 
  4.  
  5.  * Class xBEncoder 
  6.  
  7.  * Author: Angus.Fenying 
  8.  
  9.  * Version: 0.1 
  10.  
  11.  * Date:  2014-06-03 
  12.  
  13.  * 
  14.  
  15.  *  This class helps stringify or parse BENC 
  16.  
  17.  *  codes. 
  18.  
  19.  * 
  20.  
  21.  * All Copyrights 2007 - 2014 Fenying Studio Reserved. 
  22.  
  23.  */ 
  24.  
  25. class xBEncoder 
  26.  
  27. { 
  28.  
  29.   const READY = 0; 
  30.  
  31.   const READ_STR = 1; 
  32.  
  33.   const READ_DICT = 2; 
  34.  
  35.   const READ_LIST = 3; 
  36.  
  37.   const READ_INT = 4; 
  38.  
  39.   const READ_KEY = 5; 
  40.  
  41.   public $y; 
  42.  
  43.   protected $z, $m, $n; 
  44.  
  45.   protected $stat; 
  46.  
  47.   protected $stack; 
  48.  
  49.   /** 
  50.  
  51.    * This method saves the status of current 
  52.  
  53.    * encode/decode work. 
  54.  
  55.    */ 
  56.  
  57.   protected function push($newY, $newStat) 
  58.  
  59.   { 
  60.  
  61.     array_push($this->stack, array($this->y, $this->z, $this->m, $this->n, $this->stat)); 
  62.  
  63.     list($this->y, $this->z, $this->m, $this->n, $this->stat) = array($newY, 0, 0, 0, $newStat); 
  64.  
  65.   } 
  66.  
  67.   /** 
  68.  
  69.    * This method restore the saved status of current 
  70.  
  71.    * encode/decode work. 
  72.  
  73.    */ 
  74.  
  75.   protected function pop() 
  76.  
  77.   { 
  78.  
  79.     $t = array_pop($this->stack); 
  80.  
  81.     if ($t) { 
  82.  
  83.       if ($t[4] == self::READ_DICT) { 
  84.  
  85.         $t[0]->{$t[1]} = $this->y; 
  86.  
  87.         $t[1] = 0; 
  88.  
  89.       } elseif ($t[4] == self::READ_LIST) 
  90.  
  91.         $t[0][] = $this->y; 
  92.  
  93.       list($this->y, $this->z, $this->m, $this->n, $this->stat) = $t; 
  94.  
  95.     } 
  96.  
  97.   } 
  98.  
  99.   /** 
  100.  
  101.    * This method initializes the status of work. 
  102.  
  103.    * YOU SHOULD CALL THIS METHOD BEFORE EVERYTHING. 
  104.  
  105.    */ 
  106.  
  107.   public function init() 
  108.  
  109.   { 
  110.  
  111.     $this->stat = self::READY; 
  112.  
  113.     $this->stack = array(); 
  114.  
  115.     $this->z = $this->m = $this->n = 0; 
  116.  
  117.   } 
  118.  
  119.   /** 
  120.  
  121.    * This method decode $s($l as length). 
  122.  
  123.    * You can get $obj->y as the result. 
  124.  
  125.    */ 
  126.  
  127.   public function decode($s, $l) 
  128.  
  129.   { 
  130.  
  131.     $this->y = 0; 
  132.  
  133.     for ($i = 0; $i < $l; ++$i) { 
  134.  
  135.       switch ($this->stat) { 
  136.  
  137.         case self::READY: 
  138.  
  139.           if ($s[$i] == 'd') { 
  140.  
  141.             $this->y = new xBDict(); 
  142.  
  143.             $this->stat = self::READ_DICT; 
  144.  
  145.           } elseif ($s[$i] == 'l') { 
  146.  
  147.             $this->y = array(); 
  148.  
  149.             $this->stat = self::READ_LIST; 
  150.  
  151.           } 
  152.  
  153.           break; 
  154.  
  155.         case self::READ_INT: 
  156.  
  157.           if ($s[$i] == 'e') { 
  158.  
  159.             $this->y->val = substr($s, $this->m, $i - $this->m); 
  160.  
  161.             $this->pop(); 
  162.  
  163.           } 
  164.  
  165.           break; 
  166.  
  167.         case self::READ_STR: 
  168.  
  169.           if (xBInt::isNum($s[$i])) 
  170.  
  171.             continue; 
  172.  
  173.           if ($s[$i] = ':') { 
  174.  
  175.             $this->z = substr($s, $this->m, $i - $this->m); 
  176.  
  177.             $this->y = substr($s, $i + 1, $this->z + 0); 
  178.  
  179.             $i += $this->z; 
  180.  
  181.             $this->pop(); 
  182.  
  183.           } 
  184.  
  185.           break; 
  186.  
  187.         case self::READ_KEY: 
  188.  
  189.           if (xBInt::isNum($s[$i])) 
  190.  
  191.             continue; 
  192.  
  193.           if ($s[$i] = ':') { 
  194.  
  195.             $this->n = substr($s, $this->m, $i - $this->m); 
  196.  
  197.             $this->z = substr($s, $i + 1, $this->n + 0); 
  198.  
  199.             $i += $this->n; 
  200.  
  201.             $this->stat = self::READ_DICT; 
  202.  
  203.           } 
  204.  
  205.           break; 
  206.  
  207.         case self::READ_DICT: 
  208.  
  209.           if ($s[$i] == 'e') { 
  210.  
  211.             $this->pop(); 
  212.  
  213.             break; 
  214.  
  215.           } elseif (!$this->z) { 
  216.  
  217.             $this->m = $i; 
  218.  
  219.             $this->stat = self::READ_KEY; 
  220.  
  221.             break; 
  222.  
  223.           } 
  224.  
  225.         case self::READ_LIST: 
  226.  
  227.           switch ($s[$i]) { 
  228.  
  229.             case 'e': 
  230.  
  231.               $this->pop(); 
  232.  
  233.               break; 
  234.  
  235.             case 'd': 
  236.  
  237.               $this->push(new xBDict(), self::READ_DICT); 
  238.  
  239.               break; 
  240.  
  241.             case 'i': 
  242.  
  243.               $this->push(new xBInt(), self::READ_INT); 
  244.  
  245.               $this->m = $i + 1; 
  246.  
  247.               break; 
  248.  
  249.             case 'l': 
  250.  
  251.               $this->push(array(), self::READ_LIST); 
  252.  
  253.               break; 
  254.  
  255.             default: 
  256.  
  257.               if (xBInt::isNum($s[$i])) { 
  258.  
  259.                 $this->push('', self::READ_STR); 
  260.  
  261.                 $this->m = $i; 
  262.  
  263.               } 
  264.  
  265.           } 
  266.  
  267.           break; 
  268.  
  269.       } 
  270.  
  271.     } 
  272.  
  273.     $rtn = emptyempty($this->stack); 
  274.  
  275.     $this->init(); 
  276.  
  277.     return $rtn; 
  278.  
  279.   } 
  280.  
  281.   /** 
  282.  
  283.    * This method encode $obj->y into BEncode. 
  284.  
  285.    */ 
  286.  
  287.   public function encode() 
  288.  
  289.   { 
  290.  
  291.     return $this->_encDo($this->y); 
  292.  
  293.   } 
  294.  
  295.   protected function _encStr($str) 
  296.  
  297.   { 
  298.  
  299.     return strlen($str) . ':' . $str; 
  300.  
  301.   } 
  302.  
  303.   protected function _encDo($o) 
  304.  
  305.   { 
  306.  
  307.     if (is_string($o)) 
  308.  
  309.       return $this->_encStr($o); 
  310.  
  311.     if ($o instanceof xBInt) 
  312.  
  313.       return 'i' . $o->val . 'e'; 
  314.  
  315.     if ($o instanceof xBDict) { 
  316.  
  317.       $r = 'd'; 
  318.  
  319.       foreach ($o as $k => $c) 
  320.  
  321.         $r .= $this->_encStr($k) . $this->_encDo($c); 
  322.  
  323.       return $r . 'e'; 
  324.  
  325.     } 
  326.  
  327.     if (is_array($o)) { 
  328.  
  329.       $r = 'l'; 
  330.  
  331.       foreach ($o as $c) 
  332.  
  333.         $r .= $this->_encDo($c); 
  334.  
  335.       return $r . 'e'; 
  336.  
  337.     } 
  338.  
  339.   } 
  340.  
  341. } 
  342.  
  343. class xBDict 
  344.  
  345. { 
  346.  
  347. } 
  348.  
  349. class xBInt 
  350.  
  351. { 
  352.  
  353.   public $val; 
  354.  
  355.   public function __construct($val = 0) 
  356.  
  357.   { 
  358.  
  359.     $this->val = $val; 
  360.  
  361.   } 
  362.  
  363.   public static function isNum($chr) 
  364.  
  365.   { 
  366.  
  367.     $chr = ord($chr); 
  368.  
  369.     if ($chr <= 57 && $chr >= 48) 
  370.  
  371.       return true; 
  372.  
  373.     return false; 
  374.  
  375.   } 
  376.  
  377. } 
  378.  
  379. //使用实例 
  380.  
  381. $s = file_get_contents("test.torrent"); 
  382. //phpfensi.com 
  383. $bc = new xBEncoder(); 
  384.  
  385. $bc->init(); 
  386.  
  387. $bc->decode($s, strlen($s)); 
  388.  
  389. var_dump($bc->y); 

Tags: php读取torrent

分享到: