1
0

functions.maketorrent.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /* $Id: functions.maketorrent.php 2903 2007-04-14 21:17:10Z warion $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * create torrent with BitTornado
  17. *
  18. * @return string $onLoad
  19. */
  20. function createTorrentTornado() {
  21. global $cfg, $path, $tfile, $announce, $ancelist, $comment, $piece, $alert, $private, $dht;
  22. // sanity-check
  23. if ((empty($announce)) || ($announce == "http://"))
  24. return;
  25. $onLoad = "";
  26. // Clean up old files
  27. if (@file_exists($cfg["transfer_file_path"].$tfile))
  28. @unlink($cfg["transfer_file_path"].$tfile );
  29. // This is the command to execute
  30. $command = "nohup ".$cfg["pythonCmd"]." -OO";
  31. $command .= " ".tfb_shellencode($cfg["docroot"]."bin/clients/tornado/btmakemetafile.py");
  32. $command .= " ".tfb_shellencode($announce);
  33. $command .= " ".tfb_shellencode($cfg["path"].$path);
  34. // Is there comments to add?
  35. if (!empty($comment))
  36. $command .= " --comment ".tfb_shellencode($comment);
  37. // Set the piece size
  38. if (!empty($piece))
  39. $command .= " --piece_size_pow2 ".tfb_shellencode($piece);
  40. if (!empty($ancelist)) {
  41. $check = "/".str_replace("/", "\/", quotemeta($announce)) . "/i";
  42. // if they didn't add the primary tracker in, we will add it for them
  43. if (preg_match( $check, $ancelist, $result))
  44. $command .= " --announce_list ".tfb_shellencode($ancelist);
  45. else
  46. $command .= " --announce_list ".tfb_shellencode($announce.",".$ancelist);
  47. }
  48. // Set the target torrent field
  49. $command .= " --target ".tfb_shellencode($cfg["transfer_file_path"].$tfile);
  50. // Set to never timeout for large torrents
  51. @set_time_limit(0);
  52. // Let's see how long this takes...
  53. $time_start = microtime(true);
  54. // Execute the command
  55. exec($command);
  56. // We want to check to make sure the file was successful
  57. $success = false;
  58. $raw = @file_get_contents($cfg["transfer_file_path"].$tfile );
  59. if (preg_match( "/6:pieces([^:]+):/i", $raw, $results)) {
  60. // This means it is a valid torrent
  61. $success = true;
  62. // Make an entry for the owner
  63. AuditAction($cfg["constants"]["file_upload"], $tfile);
  64. // Check to see if one of the flags were set
  65. if ($private || $dht) {
  66. // Add private/dht Flags
  67. // e7:privatei1e
  68. // e17:dht_backup_enablei1e
  69. // e20:dht_backup_requestedi1e
  70. if(preg_match( "/6:pieces([^:]+):/i", $raw, $results)) {
  71. $pos = strpos( $raw, "6:pieces" ) + 9 + strlen( $results[1] ) + $results[1];
  72. $fp = @fopen( $cfg["transfer_file_path"] . $tfile, "r+" );
  73. @fseek( $fp, $pos, SEEK_SET );
  74. if ($private)
  75. @fwrite($fp,"7:privatei1eee");
  76. else
  77. @fwrite($fp,"e7:privatei0e17:dht_backup_enablei1e20:dht_backup_requestedi1eee");
  78. @fclose( $fp );
  79. }
  80. }
  81. } else {
  82. // Something went wrong, clean up
  83. if (@file_exists($cfg["transfer_file_path"].$tfile))
  84. @unlink($cfg["transfer_file_path"].$tfile);
  85. }
  86. // We are done! how long did we take?
  87. $time_end = microtime(true);
  88. $diff = duration($time_end - $time_start);
  89. // make path URL friendly to support non-standard characters
  90. $downpath = urlencode($tfile);
  91. // Depending if we were successful, display the required information
  92. $onLoad = ($success)
  93. ? "completed('".$downpath."',".$alert.",'".$diff."');"
  94. : "failed('".$downpath."',".$alert.");";
  95. return $onLoad;
  96. }
  97. /**
  98. * create torrent with Mainline
  99. *
  100. * @return string $onLoad
  101. */
  102. function createTorrentMainline() {
  103. global $cfg, $path, $tfile, $comment, $piece, $use_tracker, $tracker_name, $alert;
  104. $onLoad = "";
  105. // Clean up old files
  106. if (@file_exists($cfg["transfer_file_path"].$tfile))
  107. @unlink($cfg["transfer_file_path"].$tfile );
  108. // build command-string
  109. $command = "cd ".tfb_shellencode($cfg["transfer_file_path"]).";";
  110. $command .= " HOME=".tfb_shellencode($cfg["path"]);
  111. $command .= "; export HOME;";
  112. $command .= "nohup ".$cfg["pythonCmd"]." -OO ";
  113. $command .= tfb_shellencode($cfg["docroot"]."bin/clients/mainline/maketorrent-console.py");
  114. $command .= " --no_verbose";
  115. $command .= " --no_debug";
  116. // $command .= " --language en";
  117. // Is there comments to add?
  118. if (!empty($comment))
  119. $command .= " --comment ".tfb_shellencode($comment);
  120. // Set the piece size
  121. if (!empty($piece))
  122. $command .= " --piece_size_pow2 ".tfb_shellencode($piece);
  123. // trackerless / tracker
  124. /*
  125. if ((isset($use_tracker)) && ($use_tracker == 1))
  126. $command .= " --use_tracker";
  127. else
  128. $command .= " --no_use_tracker";
  129. */
  130. $command .= " --use_tracker";
  131. // tracker-name
  132. //if ((!empty($tracker_name)) && ($tracker_name != "http://"))
  133. $command .= " --tracker_name ".tfb_shellencode($tracker_name);
  134. // Set the target torrent field
  135. $command .= " --target ".tfb_shellencode($cfg["transfer_file_path"].$tfile);
  136. // tracker (i don't know...)
  137. $command .= " ".tfb_shellencode($tracker_name);
  138. // input
  139. $command .= " ".tfb_shellencode($cfg["path"].$path);
  140. // Set to never timeout for large torrents
  141. @set_time_limit(0);
  142. // Let's see how long this takes...
  143. $time_start = microtime(true);
  144. // Execute the command
  145. exec($command);
  146. // We want to check to make sure the file was successful
  147. $success = false;
  148. $raw = @file_get_contents($cfg["transfer_file_path"].$tfile );
  149. if (preg_match( "/6:pieces([^:]+):/i", $raw, $results)) {
  150. // This means it is a valid torrent
  151. $success = true;
  152. // Make an entry for the owner
  153. AuditAction($cfg["constants"]["file_upload"], $tfile);
  154. } else {
  155. // Something went wrong, clean up
  156. if (@file_exists($cfg["transfer_file_path"].$tfile))
  157. @unlink($cfg["transfer_file_path"].$tfile);
  158. }
  159. // We are done! how long did we take?
  160. $time_end = microtime(true);
  161. $diff = duration($time_end - $time_start);
  162. // make path URL friendly to support non-standard characters
  163. $downpath = urlencode($tfile);
  164. // Depending if we were successful, display the required information
  165. $onLoad = ($success)
  166. ? "completed('".$downpath."',".$alert.",'".$diff."');"
  167. : "failed('".$downpath."',".$alert.");";
  168. return $onLoad;
  169. }
  170. /**
  171. * Strip the folders from the path
  172. *
  173. * @param $path
  174. * @return string
  175. */
  176. function StripFolders($path) {
  177. $pos = strrpos($path, "/");
  178. $pos = ($pos === false) ? 0 : $pos + 1;
  179. $path = substr($path, $pos);
  180. return $path;
  181. }
  182. /**
  183. * Convert a timestamp to a duration string
  184. *
  185. * @param $timestamp
  186. * @return string
  187. */
  188. function duration($timestamp) {
  189. $years = floor($timestamp / (60 * 60 * 24 * 365));
  190. $timestamp %= 60 * 60 * 24 * 365;
  191. $weeks = floor($timestamp / (60 * 60 * 24 * 7));
  192. $timestamp %= 60 * 60 * 24 * 7;
  193. $days = floor($timestamp / (60 * 60 * 24));
  194. $timestamp %= 60 * 60 * 24;
  195. $hrs = floor($timestamp / (60 * 60));
  196. $timestamp %= 60 * 60;
  197. $mins = floor($timestamp / 60);
  198. $secs = $timestamp % 60;
  199. $str = "";
  200. if ($years >= 1)
  201. $str .= "{$years} years ";
  202. if ($weeks >= 1)
  203. $str .= "{$weeks} weeks ";
  204. if ($days >= 1)
  205. $str .= "{$days} days ";
  206. if ($hrs >= 1)
  207. $str .= "{$hrs} hours ";
  208. if ($mins >= 1)
  209. $str .= "{$mins} minutes ";
  210. if ($secs >= 1)
  211. $str.="{$secs} seconds ";
  212. return $str;
  213. }
  214. ?>