File_Bittorrent
[ class tree: File_Bittorrent ] [ index: File_Bittorrent ] [ all elements ]

Source for file Encode.php

Documentation is available at Encode.php

  1. <?php
  2.  
  3. // +----------------------------------------------------------------------+
  4. // | Decode and Encode data in Bittorrent format                          |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (C) 2004-2005 Markus Tacker <m@tacker.org>                 |
  7. // +----------------------------------------------------------------------+
  8. // | This library is free software; you can redistribute it and/or        |
  9. // | modify it under the terms of the GNU Lesser General Public           |
  10. // | License as published by the Free Software Foundation; either         |
  11. // | version 2.1 of the License, or (at your option) any later version.   |
  12. // |                                                                      |
  13. // | This library is distributed in the hope that it will be useful,      |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
  16. // | Lesser General Public License for more details.                      |
  17. // |                                                                      |
  18. // | You should have received a copy of the GNU Lesser General Public     |
  19. // | License along with this library; if not, write to the                |
  20. // | Free Software Foundation, Inc.                                       |
  21. // | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA               |
  22. // +----------------------------------------------------------------------+
  23.  
  24.  
  25.  
  26. /**
  27. * Encode data in Bittorrent format
  28. *
  29. * Based on
  30. *   Original Python implementation by Petru Paler <petru@paler.net>
  31. *   PHP translation by Gerard Krijgsman <webmaster@animesuki.com>
  32. *   Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk>
  33. * BEncoding is a simple, easy to implement method of associating
  34. * data types with information in a file. The values in a torrent
  35. * file are bEncoded.
  36. * There are 4 different data types that can be bEncoded:
  37. * Integers, Strings, Lists and Dictionaries.
  38. * [http://www.monduna.com/bt/faq.html]
  39. *
  40. @package File_Bittorrent
  41. @category File
  42. @author Markus Tacker <m@tacker.org>
  43. @version $Id: Encode.php 62 2006-09-04 08:53:26Z m $
  44. */
  45.  
  46. /**
  47. * Include required classes
  48. */
  49. require_once 'PEAR.php';
  50.  
  51. /**
  52. * Encode data in Bittorrent format
  53. *
  54. * Based on
  55. *   Original Python implementation by Petru Paler <petru@paler.net>
  56. *   PHP translation by Gerard Krijgsman <webmaster@animesuki.com>
  57. *   Gerard's regular expressions removed by Carl Ritson <critson@perlfu.co.uk>
  58. * BEncoding is a simple, easy to implement method of associating
  59. * data types with information in a file. The values in a torrent
  60. * file are bEncoded.
  61. * There are 4 different data types that can be bEncoded:
  62. * Integers, Strings, Lists and Dictionaries.
  63. * [http://www.monduna.com/bt/faq.html]
  64. *
  65. @package File_Bittorrent
  66. @category File
  67. @author Markus Tacker <m@tacker.org>
  68. */
  69. {
  70.     /**
  71.     * Encode a var in BEncode format
  72.     *
  73.     * @param mixed    Variable to encode
  74.     * @return string 
  75.     */
  76.     function encode($mixed)
  77.     {
  78.         switch (gettype($mixed)) {
  79.         case is_null($mixed):
  80.             return $this->encode_string('');
  81.         case 'string':
  82.             return $this->encode_string($mixed);
  83.         case 'integer':
  84.         case 'double':
  85.             return  $this->encode_int(round($mixed));
  86.         case 'array':
  87.             return $this->encode_array($mixed);
  88.         default:
  89.             PEAR::raiseError('File_Bittorrent_Encode::encode() - Unsupported type.'nullnull"Variable must be one of 'string', 'integer', 'double' or 'array'");
  90.         }
  91.     }
  92.  
  93.     /**
  94.     * BEncodes a string
  95.     *
  96.     * Strings are prefixed with their length followed by a colon.
  97.     * For example, "Monduna" would bEncode to 7:Monduna and "BitTorrents"
  98.     * would bEncode to 11:BitTorrents.
  99.     *
  100.     * @param string 
  101.     * @return string 
  102.     */
  103.     function encode_string($str)
  104.     {
  105.         return strlen($str':' $str;
  106.     }
  107.  
  108.     /**
  109.     * BEncodes a integer
  110.     *
  111.     * Integers are prefixed with an i and terminated by an e. For
  112.     * example, 123 would bEcode to i123e, -3272002 would bEncode to
  113.     * i-3272002e.
  114.     *
  115.     * @param int 
  116.     * @return string 
  117.     */
  118.     function encode_int($int)
  119.     {
  120.         return 'i' $int 'e';
  121.     }
  122.  
  123.     /**
  124.     * BEncodes an array
  125.     * This code assumes arrays with purely integer indexes are lists,
  126.     * arrays which use string indexes assumed to be dictionaries.
  127.     *
  128.     * Dictionaries are prefixed with a d and terminated by an e. They
  129.     * are similar to list, except that items are in key value pairs. The
  130.     * dictionary {"key":"value", "Monduna":"com", "bit":"Torrents", "number":7}
  131.     * would bEncode to d3:key5:value7:Monduna3:com3:bit:8:Torrents6:numberi7ee
  132.     *
  133.     * Lists are prefixed with a l and terminated by an e. The list
  134.     * should contain a series of bEncoded elements. For example, the
  135.     * list of strings ["Monduna", "Bit", "Torrents"] would bEncode to
  136.     * l7:Monduna3:Bit8:Torrentse. The list [1, "Monduna", 3, ["Sub", "List"]]
  137.     * would bEncode to li1e7:Mondunai3el3:Sub4:Listee
  138.     *
  139.     * @param array 
  140.     * @return string 
  141.     */
  142.     function encode_array($array)
  143.     {
  144.         // Check for strings in the keys
  145.         $isList = true;
  146.         foreach (array_keys($arrayas $key{
  147.             if (!is_int($key)) {
  148.                 $isList = false;
  149.                 break;
  150.             }
  151.         }
  152.         if ($isList{
  153.             // Wie build a list
  154.             ksort($arraySORT_NUMERIC);
  155.             $return 'l';
  156.             foreach ($array as $val{
  157.                 $return .= $this->encode($val);
  158.             }
  159.             $return .= 'e';
  160.         else {
  161.             // We build a Dictionary
  162.             ksort($arraySORT_STRING);
  163.             $return 'd';
  164.             foreach ($array as $key => $val{
  165.                 $return .= $this->encode(strval($key));
  166.                 $return .= $this->encode($val);
  167.             }
  168.             $return .= 'e';
  169.         }
  170.         return $return;
  171.     }
  172. }
  173.  
  174. ?>

Documentation generated on Tue, 13 Mar 2007 10:00:11 -0500 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.