protected function doClean($value) { // Getting uploaded file (still in tmp at this stage) $validatedFile = parent::doClean($value); // Loading BitTorrent class require "../lib/bittorrent/Autoload.php"; // Open a new torrent instance $torrent = new PHP_BitTorrent_Torrent(); // Loading it $torrent->loadFromTorrentFile($validatedFile->getTempName()); // Generating hash $hash = sha1(PHP_BitTorrent_Encoder::encode($torrent->getInfo())); // I want to use this hash outside this class, not clean at all, shame on me ^^ define('HASH', $hash); define('SIZE', $torrent->getSize()); // Looking if hash is already posted $sh = Doctrine_Query::create()->select('COUNT(*)')->from("Uploads")->where("hash = ?", $hash)->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR); if ($sh > 0) { throw new sfValidatorError($this, 'already', array('hash' => $hash)); } // Private mode $i = $torrent->getInfo(); if ($this->getOption('private') != $i['private']) { throw new sfValidatorError($this, 'private', array('private' => $this->getOption('private'))); } // Checking if announce URL is correct /*if ($this->getOption('announce') != $torrent->getAnnounce()) throw new sfValidatorError($this, 'announce', array('announce' => $this->getOption('announce'))); */ return $validatedFile; }
/** * Decode an encoded PHP associative array * * @param string $dictionary * @return array * @throws PHP_BitTorrent_Decoder_Exception */ public static function decodeDictionary($dictionary) { if ($dictionary[0] !== 'd') { throw new PHP_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 = static::decodeString($keyPart); $keyPartLength = strlen(PHP_BitTorrent_Encoder::encodeString($key)); $valuePart = substr($dictionary, $i + $keyPartLength); $value = static::decode($valuePart); $valuePartLength = strlen(PHP_BitTorrent_Encoder::encode($value)); $ret[$key] = $value; $i += $keyPartLength + $valuePartLength; } return $ret; }
/** * Sending an error to BT client and stopping process */ protected function btSendError($msg) { require_once "../lib/bittorrent/Encoder.php"; echo PHP_BitTorrent_Encoder::encode(array("failure reason" => $this->getContext()->getI18N()->__($msg))); exit; }
/** * 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 * @param boolean $write * @throws PHP_BitTorrent_Torrent_Exception * @return PHP_BitTorrent_Torrent */ public function save($filename, $write = true) { if (!is_writable($filename) && !is_writable(dirname($filename))) { throw new PHP_BitTorrent_Torrent_Exception('Could not open file "' . $filename . '" for writing.'); } $announce = $this->getAnnounce(); if (empty($announce)) { throw new PHP_BitTorrent_Torrent_Exception('Announce URL is missing.'); } $info = $this->getInfo(); if (empty($info)) { throw new PHP_BitTorrent_Torrent_Exception('The info part of the torrent is empty.'); } $createdAt = $this->getCreatedAt(); if (empty($createdAt)) { $createdAt = time(); } $torrent = array('announce' => $announce, 'creation date' => $createdAt, 'info' => $info); if (($comment = $this->getComment()) !== null) { $torrent['comment'] = $comment; } if (($createdBy = $this->getCreatedBy()) !== null) { $torrent['created by'] = $createdBy; } // Create the encoded dictionary $dictionary = PHP_BitTorrent_Encoder::encodeDictionary($torrent); // Write the encoded data to the file if ($write) { file_put_contents($filename, $dictionary); } else { return $dictionary; } return $this; }