/** * Encode a variable * * @param mixed $var * @return string * @uses Zend_BitTorrent_Encoder::encode() */ public static function encode($var) { return Zend_BitTorrent_Encoder::encode($var); }
/** * Decode an encoded PHP associative array * * @param string $dictionary * @return array * @uses Zend_BitTorrent_Decoder::decodeString() * @uses Zend_BitTorrent_Decoder::decode() * @throws Zend_BitTorrent_Decoder_Exception */ public static function decodeDictionary($dictionary) { if ($dictionary[0] !== 'd') { /** @see Zend_BitTorrent_Decoder_Exception */ require_once 'Zend/BitTorrent/Decoder/Exception.php'; throw new Zend_BitTorrent_Decoder_Exception('Parameter is not an encoded dictionary.'); } $length = strlen($dictionary); $ret = array(); $i = 1; while ($i < $length) { if ($dictionary[$i] === 'e') { break; } $keyPart = substr($dictionary, $i); $key = self::decodeString($keyPart); $keyPartLength = strlen(Zend_BitTorrent_Encoder::encodeString($key)); $valuePart = substr($dictionary, $i + $keyPartLength); $value = self::decode($valuePart); $valuePartLength = strlen(Zend_BitTorrent_Encoder::encode($value)); $ret[$key] = $value; $i += $keyPartLength + $valuePartLength; } return $ret; }
/** * Save the current torrent object to the filename specified * * This method will save the current object to a file. If the file specified exists it will be overwritten. * * @param string $filename * @throws Zend_BitTorrent_Torrent_Exception * @return Zend_BitTorrent_Torrent */ public function save($filename) { /* Build the torrent if it is not yet built */ if (!$this->isBuilt) { $this->build(); } /* Open the file if it is writeable */ if (!is_writeable($filename)) { /** @see Zend_BitTorrent_Torrent_Exception */ require_once 'Zend/BitTorrent/Torrent/Exception.php'; throw new Zend_BitTorrent_Torrent_Exception('Could not open file "' . $filename . '" for writing.'); } /* Open file for writing */ $fp = fopen($filename, 'wb'); $torrent = array('announce' => $this->getAnnounce(), 'creation date' => $this->getCreationDate(), 'info' => $this->getInfo()); if (($comment = $this->getComment()) !== null) { $torrent['comment'] = $comment; } if (($createdBy = $this->getCreatedBy()) !== null) { $torrent['created by'] = $createdBy; } /* Create the encoded dictionary */ $dictionary = Zend_BitTorrent_Encoder::encodeDictionary($torrent); /* Write the encoded data to the file */ fwrite($fp, $dictionary, strlen($dictionary)); /* Close the file handle */ fclose($fp); return $this; }