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 常用类整理
Dec 23 PHP
php 数组二分法查找函数代码
Feb 16 PHP
解析php中const与define的应用区别
Jun 18 PHP
浅析php变量修饰符static的使用
Jun 28 PHP
javascript some()函数用法详解
Nov 13 PHP
详解WordPress开发中get_header()获取头部函数的用法
Jan 08 PHP
php进程间通讯实例分析
Jul 11 PHP
phalcon model在插入或更新时会自动验证非空字段的解决办法
Dec 29 PHP
PHP实现PDO操作mysql存储过程示例
Feb 13 PHP
Linux下源码包安装Swoole及基本使用操作图文详解
Apr 02 PHP
php反序列化长度变化尾部字符串逃逸(0CTF-2016-piapiapia)
Feb 15 PHP
PHP程序员简单的开展服务治理架构操作详解(二)
May 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
PHP新手上路(十二)
2006/10/09 PHP
PHP关联链接常用代码
2012/11/05 PHP
PHP生成唯一的促销/优惠/折扣码(附源码)
2012/12/28 PHP
Laravel Eloquent分表方法并使用模型关联的实现
2019/11/25 PHP
DOM精简教程
2006/10/03 Javascript
jQuery与ExtJS之选择实例分析
2010/08/19 Javascript
JSChart轻量级图形报表工具(内置函数中文参考)
2010/10/11 Javascript
js 获取radio按钮值的实例
2013/08/17 Javascript
NodeJS Express框架中处理404页面一个方式
2014/05/28 NodeJs
node.js中的http.createClient方法使用说明
2014/12/15 Javascript
js正则表达式中exec用法实例
2015/07/23 Javascript
JavaScript仿静态分页实现方法
2015/08/04 Javascript
Ajax和Comet技术总结
2017/02/19 Javascript
vue.js中mint-ui框架的使用方法
2017/05/12 Javascript
bootstrap fileinput实现文件上传功能
2017/08/23 Javascript
基于Bootstrap表单验证功能
2017/11/17 Javascript
Javacript中自定义的map.js  的方法
2017/11/26 Javascript
vue 简单自动补全的输入框的示例
2018/03/12 Javascript
解决vue单页使用keep-alive页面返回不刷新的问题
2018/03/13 Javascript
解决vue.js 数据渲染成功仍报错的问题
2018/08/25 Javascript
前端路由&amp;webpack基础配置详解
2019/06/10 Javascript
Vue.js 中的实用工具方法【推荐】
2019/07/04 Javascript
Python实现PS滤镜Fish lens图像扭曲效果示例
2018/01/29 Python
使用python的pandas库读取csv文件保存至mysql数据库
2018/08/20 Python
Python 运行 shell 获取输出结果的实例
2019/01/07 Python
python与字符编码问题
2019/05/24 Python
python如何利用paramiko执行服务器命令
2020/11/07 Python
P D PAOLA法国官网:西班牙著名的珠宝首饰品牌
2020/02/15 全球购物
六十岁生日答谢词
2014/01/10 职场文书
给医务人员表扬信
2014/01/12 职场文书
房地产财务部员工岗位职责
2014/03/12 职场文书
2014年教师德育工作总结
2014/11/10 职场文书
先进教师个人事迹材料
2014/12/15 职场文书
环境建议书
2015/02/04 职场文书
员工试用期工作总结
2019/06/20 职场文书
python 爬取京东指定商品评论并进行情感分析
2021/05/27 Python