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配置文件中最常用四个ini函数
Mar 19 PHP
PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
Jul 01 PHP
discuz程序的PHP加密函数原理分析
Aug 05 PHP
ajax php传递和接收变量实现思路及代码
Dec 19 PHP
php中多维数组按指定value排序的实现代码
Aug 19 PHP
PHP实现微信网页授权开发教程
Jan 19 PHP
PHP7安装Redis扩展教程【Linux与Windows平台】
Sep 30 PHP
详解PHP处理密码的几种方式
Nov 30 PHP
PHP实现十进制、二进制、八进制和十六进制转换相关函数用法分析
Apr 25 PHP
PHP编程实现计算抽奖概率算法完整实例
Aug 09 PHP
通过PHP设置BugFree获取邮箱通知
Apr 25 PHP
PHP7 windows支持
Mar 09 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
PHP 常见郁闷问题答解
2006/11/25 PHP
PHP中Date()时间日期函数的使用方法小结
2011/04/20 PHP
php使用百度翻译api示例分享
2014/01/31 PHP
php获取mysql字段名称和其它信息的例子
2014/04/14 PHP
PHP date()函数警告: It is not safe to rely on the system解决方法
2014/08/20 PHP
php中关于socket的系列函数总结
2015/05/18 PHP
php伪静态验证码不显示的解决方案
2019/09/26 PHP
TopList标签和JavaScript结合两例
2007/08/12 Javascript
js实现的日期操作类DateTime函数代码
2010/03/16 Javascript
用JS判断IE版本的代码 超管用!
2011/08/09 Javascript
ASP.NET jQuery 实例9  通过控件hyperlink实现返回顶部效果
2012/02/03 Javascript
Extjs中使用extend(js继承) 的代码
2012/03/15 Javascript
js父窗口关闭时子窗口随之关闭完美解决方案
2014/04/29 Javascript
jQuery学习笔记之创建DOM元素
2015/01/19 Javascript
Javascript中判断对象是否为空
2015/06/10 Javascript
Vue.JS入门教程之处理表单
2016/12/01 Javascript
knockoutjs模板实现树形结构列表
2017/07/31 Javascript
解决axios发送post请求返回400状态码的问题
2018/08/11 Javascript
JQuery的加载和选择器用法简单示例
2019/05/13 jQuery
vue2.0 解决抽取公用js的问题
2020/07/31 Javascript
Python装饰器用法实例总结
2018/02/07 Python
用pyqt5 给按钮设置图标和css样式的方法
2019/06/24 Python
利用Python实现手机短信监控通知的方法
2019/07/22 Python
Python实现Keras搭建神经网络训练分类模型教程
2020/06/12 Python
python中元组的用法整理
2020/06/15 Python
python 通过pip freeze、dowload打离线包及自动安装的过程详解(适用于保密的离线环境
2020/12/14 Python
英国知名的皮手套品牌:Dents
2016/11/13 全球购物
工商管理本科毕业生求职信范文
2013/10/05 职场文书
金融专业个人求职信范文
2013/11/28 职场文书
消防安全检查制度
2014/02/04 职场文书
父母寄语大全
2014/04/12 职场文书
常务副总经理岗位职责
2014/04/12 职场文书
大学计划书范文800字
2014/08/14 职场文书
2016民族团结先进个人事迹材料
2016/02/26 职场文书
2019森林防火宣传标语大全!
2019/07/03 职场文书
Nginx+SpringBoot实现负载均衡的示例
2021/03/31 Servers