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 相关文章推荐
PHP4(windows版本)中的COM函数
Oct 09 PHP
php MYSQL 数据备份类
Jun 19 PHP
codeigniter教程之上传视频并使用ffmpeg转flv示例
Feb 13 PHP
个人写的PHP验证码生成类分享
Aug 21 PHP
php缩放gif和png图透明背景变成黑色的解决方法
Oct 14 PHP
Windows下编译PHP5.4和xdebug全记录
Apr 03 PHP
php中namespace use用法实例分析
Jan 22 PHP
抛弃 PHP 代价太高
Apr 26 PHP
PHP利用超级全局变量$_GET来接收表单数据的实例
Nov 05 PHP
PHP生成各种随机验证码的方法总结【附demo源码】
Jun 05 PHP
php微信公众号开发之欢迎老朋友
Oct 20 PHP
YII2框架中actions的作用与使用方法示例
Mar 13 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
CI(CodeIgniter)框架中的增删改查操作
2014/06/10 PHP
thinkphp的静态缓存用法分析
2014/11/29 PHP
Zend Framework教程之Bootstrap类用法概述
2016/03/14 PHP
PHP getID3类的使用方法学习笔记【附getID3源码下载】
2019/10/18 PHP
js 弹出新页面避免被浏览器、ad拦截的一种新方法
2014/04/30 Javascript
动态载入js提高网页打开速度的方法
2014/07/04 Javascript
JavaScript中用字面量创建对象介绍
2014/12/31 Javascript
JS给超链接加确认对话框的方法
2015/02/24 Javascript
页面内容排序插件jSort使用方法
2015/10/10 Javascript
拥Bootstrap入怀——导航栏篇
2016/05/30 Javascript
深入研究jQuery图片懒加载 lazyload.js使用方法
2017/08/16 jQuery
vuex学习之Actions的用法详解
2017/08/29 Javascript
webpack开发环境和生产环境的深入理解
2018/11/08 Javascript
详解关于Vuex的action传入多个参数的问题
2019/02/22 Javascript
React 父子组件通信的实现方法
2019/12/05 Javascript
jQuery实现王者荣耀手风琴效果
2020/01/17 jQuery
es6函数之严格模式用法实例分析
2020/03/17 Javascript
vue iview实现动态新增和删除
2020/06/17 Javascript
[01:10]DOTA2次级职业联赛 - EP战队宣传片
2014/12/01 DOTA
Python Web框架Flask信号机制(signals)介绍
2015/01/01 Python
Python实现二分法算法实例
2015/02/02 Python
对python中执行DOS命令的3种方法总结
2018/05/12 Python
Django框架HttpResponse对象用法实例分析
2019/11/01 Python
Python搭建HTTP服务过程图解
2019/12/14 Python
Python如何输出警告信息
2020/07/30 Python
香港时尚女装购物网站:ZAFUL
2017/07/19 全球购物
laravel使用redis队列实例讲解
2021/03/23 PHP
师范生实习的个人自我鉴定
2013/10/20 职场文书
元旦联欢会感言
2014/03/04 职场文书
机房搬迁方案
2014/05/01 职场文书
民族团结演讲稿范文
2014/08/27 职场文书
群众路线学习心得体会范文
2014/11/05 职场文书
社区活动总结
2015/02/04 职场文书
素质教育培训心得体会
2016/01/19 职场文书
描写九月优美句子(39条)
2019/09/11 职场文书
Python实现学生管理系统并生成exe可执行文件详解流程
2022/01/22 Python