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; }
/** * Get list of files for a torrent * @return array */ public function getFiles($url) { $url = "uploads/torrents/" . $url; if (!file_exists($url)) { return false; } // Loading BitTorrent class require_once "../lib/bittorrent/Autoload.php"; // Open a new torrent instance $torrent = new PHP_BitTorrent_Torrent(); // Load torrent $torrent->loadFromTorrentFile($url); // Get filelist $fileList = $torrent->getFileList(); // If it returns only one file, build array like multi-file. if (is_string($fileList)) { return array(array("length" => $torrent->getSize(), "path" => array($fileList))); } else { return $fileList; } }
/** * Download */ public function executeDownload(sfWebRequest $r) { // Getting upload from route $u = $this->getRoute()->getObject(); $this->forward404Unless($u); // If it's a torrent if ($u->getHash()) { // If torrent file has gone away, send 404 if (!file_exists('uploads/torrents/' . $u->getUrl())) { $this->forward404(); } // We're gonna rewrite all HTTP headers $this->getResponse()->clearHttpHeaders(); // We're sending a torrent file $this->getResponse()->setContentType('application/x-bittorrent'); // Forcing download and setting filename $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="' . $u->getTitle() . '.torrent"'); // If user has a PID, we can use it inside the torrent file if ($this->getUser()->getAttribute("ses")->getPid()) { // Load torrent class require_once "../lib/bittorrent/Autoload.php"; // Open a new torrent instance $torrent = new PHP_BitTorrent_Torrent(); // Loading it $torrent->loadFromTorrentFile('uploads/torrents/' . $u->getUrl()); // Resetting announce $torrent->setAnnounce($torrent->getAnnounce() . '?pid=' . $this->getUser()->getAttribute("ses")->getPid()); // Prepare body content with torrent's one $this->getResponse()->setContent($torrent->save("uploads/torrents/" . $this->getUser()->getAttribute("ses")->getPid(), false)); unlink("uploads/torrents/" . $this->getUser()->getAttribute("ses")->getPid()); } else { $this->getResponse()->setContent(file_get_contents('uploads/torrents/' . $u->getUrl())); } // Sending all stuff to browser $this->getResponse()->send(); } else { // Record click $c = new UploadsHits(); $c->setUid($this->getUser()->getAttribute("id")); $c->setUpid($u->getId()); $c->save(); $this->redirect($u->getUrl()); } return sfView::NONE; }