PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/Torrent.php

https://github.com/predakanga/impression
PHP | 132 lines | 73 code | 19 blank | 40 comment | 6 complexity | e85c4878fd709fbd87f5d7a4680a39d6 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2011, predakanga
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of the <organization> nor the
  14. * names of its contributors may be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. namespace Impression\Controllers;
  29. use Fossil\Requests\BaseRequest,
  30. Impression\Models\Torrent as TorrentModel,
  31. Impression\Models\TorrentGroup,
  32. Impression\BDecode,
  33. Fossil\Plugins\Users\Models\User,
  34. Impression\Forms\Upload as UploadForm,
  35. Doctrine\Common\Util\Debug;
  36. /**
  37. * Description of Upload
  38. *
  39. * @author predakanga
  40. */
  41. class Torrent extends LoginRequiredController {
  42. /**
  43. * @F:Inject("Tracker")
  44. * @var Impression\Trackers\BaseTracker
  45. */
  46. protected $tracker;
  47. public function runList() {
  48. Debug::dump(TorrentModel::findAll($this->container));
  49. }
  50. public function runUpload(UploadForm $uploadForm) {
  51. require_once("File/Bittorrent2/Encode.php");
  52. if($uploadForm->isSubmitted() && $uploadForm->isValid()) {
  53. $decoder = new BDecode;
  54. $encoder = new \File_Bittorrent2_Encode;
  55. // Create the torrent
  56. $torrent = TorrentModel::create($this->container);
  57. $torrent->filename = $uploadForm->file['name'];
  58. // Process the uploaded file
  59. $decoder->decodeFile($uploadForm->file['tmp_name']);
  60. unlink($uploadForm->file['tmp_name']);
  61. // Sanitise the data
  62. $sanitisedDict = $this->sanitiseTorrent($decoder->getData());
  63. $decoder->setData($sanitisedDict);
  64. $torrent->group = $this->decideTorrentGroup($uploadForm->file['name'], $sanitisedDict);
  65. // And store it
  66. $torrent->torrentData = $encoder->encode($sanitisedDict);
  67. $torrent->infohash = $decoder->getInfoHash(true);
  68. $torrent->trackerID = 2;
  69. $torrent->uploadedAt = new \DateTime();
  70. $torrent->uploader = User::me($this->container);
  71. $torrent->save();
  72. return $this->redirectResponse("?controller=torrent&action=list");
  73. } else {
  74. return $this->templateResponse("fossil:torrent/upload");
  75. }
  76. }
  77. protected function sanitiseTorrent($data) {
  78. $restricted = array('announce-list',
  79. 'nodes',
  80. 'azureus_properties',
  81. 'libtorrent_resume');
  82. foreach($restricted as $key) {
  83. if(isset($data[$key]))
  84. unset($data[$key]);
  85. }
  86. $data['info']['private'] = 1;
  87. $data['announce'] = "0xDEADBEEF";
  88. return $data;
  89. }
  90. protected function decideTorrentGroup($filename, $torrentData) {
  91. $group = TorrentGroup::findOneByName($this->container, $filename);
  92. if(!$group) {
  93. $group = TorrentGroup::create($this->container);
  94. $group->name = $filename;
  95. $group->save();
  96. }
  97. return $group;
  98. }
  99. public function runDownload(TorrentModel $id = null) {
  100. $torrent = $id;
  101. if(!$torrent) {
  102. return $this->redirectResponse("?controller=torrent&action=list");
  103. }
  104. header("Content-Disposition: attachment; filename=\"{$torrent->filename}\"");
  105. // Replace the announce URL
  106. $announceURL = $this->tracker->getAnnounceURL(User::me($this->container));
  107. $announceStr = strlen($announceURL) . ":" . $announceURL;
  108. $toPrint = str_replace("10:0xDEADBEEF", $announceStr, $torrent->torrentData);
  109. echo $toPrint;
  110. die();
  111. }
  112. }
  113. ?>