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


Posted in PHP onMay 03, 2016

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

<?php
/**
 * Class xBEncoder
 * Author: Angus.Fenying
 * Version: 0.1
 * Date:  2014-06-03
 *
 *  This class helps stringify or parse BENC
 *  codes.
 *
 * All Copyrights 2007 - 2014 Fenying Studio Reserved.
 */
class xBEncoder
{
  const READY = 0;
  const READ_STR = 1;
  const READ_DICT = 2;
  const READ_LIST = 3;
  const READ_INT = 4;
  const READ_KEY = 5;
  public $y;
  protected $z, $m, $n;
  protected $stat;
  protected $stack;
  /**
   * This method saves the status of current
   * encode/decode work.
   */
  protected function push($newY, $newStat)
  {
    array_push($this->stack, array($this->y, $this->z, $this->m, $this->n, $this->stat));
    list($this->y, $this->z, $this->m, $this->n, $this->stat) = array($newY, 0, 0, 0, $newStat);
  }
  /**
   * This method restore the saved status of current
   * encode/decode work.
   */
  protected function pop()
  {
    $t = array_pop($this->stack);
    if ($t) {
      if ($t[4] == self::READ_DICT) {
        $t[0]->{$t[1]} = $this->y;
        $t[1] = 0;
      } elseif ($t[4] == self::READ_LIST)
        $t[0][] = $this->y;
      list($this->y, $this->z, $this->m, $this->n, $this->stat) = $t;
    }
  }
  /**
   * This method initializes the status of work.
   * YOU SHOULD CALL THIS METHOD BEFORE EVERYTHING.
   */
  public function init()
  {
    $this->stat = self::READY;
    $this->stack = array();
    $this->z = $this->m = $this->n = 0;
  }
  /**
   * This method decode $s($l as length).
   * You can get $obj->y as the result.
   */
  public function decode($s, $l)
  {
    $this->y = 0;
    for ($i = 0; $i < $l; ++$i) {
      switch ($this->stat) {
        case self::READY:
          if ($s[$i] == 'd') {
            $this->y = new xBDict();
            $this->stat = self::READ_DICT;
          } elseif ($s[$i] == 'l') {
            $this->y = array();
            $this->stat = self::READ_LIST;
          }
          break;
        case self::READ_INT:
          if ($s[$i] == 'e') {
            $this->y->val = substr($s, $this->m, $i - $this->m);
            $this->pop();
          }
          break;
        case self::READ_STR:
          if (xBInt::isNum($s[$i]))
            continue;
          if ($s[$i] = ':') {
            $this->z = substr($s, $this->m, $i - $this->m);
            $this->y = substr($s, $i + 1, $this->z + 0);
            $i += $this->z;
            $this->pop();
          }
          break;
        case self::READ_KEY:
          if (xBInt::isNum($s[$i]))
            continue;
          if ($s[$i] = ':') {
            $this->n = substr($s, $this->m, $i - $this->m);
            $this->z = substr($s, $i + 1, $this->n + 0);
            $i += $this->n;
            $this->stat = self::READ_DICT;
          }
          break;
        case self::READ_DICT:
          if ($s[$i] == 'e') {
            $this->pop();
            break;
          } elseif (!$this->z) {
            $this->m = $i;
            $this->stat = self::READ_KEY;
            break;
          }
        case self::READ_LIST:
          switch ($s[$i]) {
            case 'e':
              $this->pop();
              break;
            case 'd':
              $this->push(new xBDict(), self::READ_DICT);
              break;
            case 'i':
              $this->push(new xBInt(), self::READ_INT);
              $this->m = $i + 1;
              break;
            case 'l':
              $this->push(array(), self::READ_LIST);
              break;
            default:
              if (xBInt::isNum($s[$i])) {
                $this->push('', self::READ_STR);
                $this->m = $i;
              }
          }
          break;
      }
    }
    $rtn = empty($this->stack);
    $this->init();
    return $rtn;
  }
  /**
   * This method encode $obj->y into BEncode.
   */
  public function encode()
  {
    return $this->_encDo($this->y);
  }
  protected function _encStr($str)
  {
    return strlen($str) . ':' . $str;
  }
  protected function _encDo($o)
  {
    if (is_string($o))
      return $this->_encStr($o);
    if ($o instanceof xBInt)
      return 'i' . $o->val . 'e';
    if ($o instanceof xBDict) {
      $r = 'd';
      foreach ($o as $k => $c)
        $r .= $this->_encStr($k) . $this->_encDo($c);
      return $r . 'e';
    }
    if (is_array($o)) {
      $r = 'l';
      foreach ($o as $c)
        $r .= $this->_encDo($c);
      return $r . 'e';
    }
  }
}
class xBDict
{
}
class xBInt
{
  public $val;
  public function __construct($val = 0)
  {
    $this->val = $val;
  }
  public static function isNum($chr)
  {
    $chr = ord($chr);
    if ($chr <= 57 && $chr >= 48)
      return true;
    return false;
  }
}
//使用实例
$s = file_get_contents("test.torrent");
$bc = new xBEncoder();
$bc->init();
$bc->decode($s, strlen($s));
var_dump($bc->y);

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
php xfocus防注入资料
Apr 27 PHP
提高PHP编程效率 引入缓存机制提升性能
Feb 15 PHP
PHP 可阅读随机字符串代码
May 26 PHP
一道求$b相对于$a的相对路径的php代码
Aug 08 PHP
php中强制下载文件的代码(解决了IE下中文文件名乱码问题)
May 09 PHP
PHP安全配置详细说明
Sep 26 PHP
去掉destoon资讯内容页keywords关键字自带的文章标题的方法
Aug 21 PHP
php使用SAE原生Mail类实现各种类型邮件发送的方法
Oct 10 PHP
PHP中Session ID的实现原理实例分析
Aug 17 PHP
使用PHP+Redis实现延迟任务,实现自动取消订单功能
Nov 21 PHP
php使用gearman进行任务分发操作实例详解
Feb 26 PHP
Yii redis集合的基本使用教程
Jun 14 PHP
Yii2 输出xml格式数据的方法
May 03 #PHP
php面向对象值单例模式
May 03 #PHP
php使用ffmpeg获取视频信息并截图的实现方法
May 03 #PHP
Linux环境下php实现给网站截图的方法
May 03 #PHP
PHPExcel笔记, mpdf导出
May 03 #PHP
PHP实现的进度条效果详解
May 03 #PHP
php实现按天数、星期、月份查询的搜索框
May 02 #PHP
You might like
redis 队列操作的例子(php)
2012/04/12 PHP
微信公众平台实现获取用户OpenID的方法
2015/04/15 PHP
CodeIgniter使用smtp服务发送html邮件的方法
2015/06/10 PHP
php+jQuery递归调用POST循环请求示例
2016/10/14 PHP
prototype 1.5相关知识及他人笔记
2006/12/16 Javascript
jQuery 版本的文本输入框检查器Input Check
2009/07/09 Javascript
js键盘上下左右键怎么触发function(实例讲解)
2013/12/14 Javascript
页面向下滚动ajax获取数据的实现方法(兼容手机)
2016/05/24 Javascript
xcode中获取js文件的路径方法(推荐)
2016/11/05 Javascript
Bootstrap缩略图的创建方法
2017/03/22 Javascript
微信小程序中form 表单提交和取值实例详解
2017/04/20 Javascript
vue实现登录后页面跳转到之前页面
2018/01/07 Javascript
详解微信小程序实现WebSocket心跳重连
2018/07/31 Javascript
VUE脚手架具体使用方法
2019/05/20 Javascript
vue实现分页栏效果
2019/06/28 Javascript
利用d3.js制作连线动画图与编辑器的方法实例
2019/09/05 Javascript
React-redux实现小案例(todolist)的过程
2019/09/29 Javascript
vue-preview动态获取图片宽高并增加旋转功能的实现
2020/07/29 Javascript
Python中使用装饰器和元编程实现结构体类实例
2015/01/28 Python
自己编程中遇到的Python错误和解决方法汇总整理
2015/06/03 Python
Python3.6简单操作Mysql数据库
2017/09/12 Python
Python字符串格式化输出代码实例
2019/11/22 Python
numpy ndarray 按条件筛选数组,关联筛选的例子
2019/11/26 Python
tensorflow求导和梯度计算实例
2020/01/23 Python
Python爬虫抓取指定网页图片代码实例
2020/07/24 Python
html5应用缓存_动力节点Java学院整理
2017/07/13 HTML / CSS
详解canvas绘制多张图的排列顺序问题
2019/01/21 HTML / CSS
瑜伽国际:Yoga International
2018/04/18 全球购物
英国复古服装购物网站:Collectif
2019/10/30 全球购物
精美的手工家居和生活用品:Nkuku
2019/11/01 全球购物
热能动力工程毕业生自荐信
2013/11/07 职场文书
开业典礼主持词
2014/03/21 职场文书
六一儿童节演讲稿
2014/05/23 职场文书
汽车专业求职信
2014/06/05 职场文书
招标保密承诺书
2015/01/20 职场文书
详细分析PHP7与PHP5区别
2021/06/26 PHP