1
0

functions.fileprio.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /* $Id: functions.fileprio.php 3125 2007-06-24 16:04:47Z 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. * File Prio Form
  17. *
  18. * @param $transfer
  19. * @param $withForm
  20. * @return string
  21. */
  22. function getFilePrioForm($transfer, $withForm = false) {
  23. global $cfg;
  24. $prioFileName = $cfg["transfer_file_path"].$transfer.".prio";
  25. require_once('inc/classes/BDecode.php');
  26. $retVal = "";
  27. // theme-switch
  28. if ((strpos($cfg["theme"], '/')) === false) {
  29. $retVal .= '<link rel="StyleSheet" href="themes/'.$cfg["theme"].'/css/dtree.css" type="text/css" />';
  30. $retVal .= '<script type="text/javascript">var dtree_path_images = "themes/'.$cfg["theme"].'/images/dtree/";</script>';
  31. } else {
  32. $retVal .= '<link rel="StyleSheet" href="themes/tf_standard_themes/css/dtree.css" type="text/css" />';
  33. $retVal .= '<script type="text/javascript">var dtree_path_images = "themes/tf_standard_themes/images/dtree/";</script>';
  34. }
  35. $retVal .= '<script type="text/javascript" src="js/dtree.js"></script>';
  36. $ftorrent = $cfg["transfer_file_path"].$transfer;
  37. $fp = @fopen($ftorrent, "rd");
  38. $alltorrent = @fread($fp, @filesize($ftorrent));
  39. @fclose($fp);
  40. $btmeta = @BDecode($alltorrent);
  41. $torrent_size = $btmeta["info"]["piece length"] * (strlen($btmeta["info"]["pieces"]) / 20);
  42. $dirnum = (array_key_exists('files',$btmeta['info'])) ? count($btmeta['info']['files']) : 0;
  43. if (@is_readable($prioFileName)) {
  44. $prio = explode(',', @file_get_contents($prioFileName));
  45. $prio = array_splice($prio,1);
  46. } else {
  47. $prio = array();
  48. for ($i=0; $i<$dirnum; $i++)
  49. $prio[$i] = -1;
  50. }
  51. $tree = new dir("/",$dirnum, isset($prio[$dirnum]) ? $prio[$dirnum] : -1);
  52. if (array_key_exists('files',$btmeta['info'])) {
  53. foreach( $btmeta['info']['files'] as $filenum => $file) {
  54. $depth = count($file['path']);
  55. $branch =& $tree;
  56. for ($i=0; $i < $depth; $i++) {
  57. if ($i != $depth - 1) {
  58. $d =& $branch->findDir($file['path'][$i]);
  59. if ($d) {
  60. $branch =& $d;
  61. } else {
  62. $dirnum++;
  63. $d =& $branch->addDir(new dir($file['path'][$i], $dirnum, (isset($prio[$dirnum]) ? $prio[$dirnum] : -1)));
  64. $branch =& $d;
  65. }
  66. } else {
  67. $branch->addFile(new file($file['path'][$i]." (".$file['length'].")", $filenum,$file['length'], $prio[$filenum]));
  68. }
  69. }
  70. }
  71. }
  72. $retVal .= "<table><tr>";
  73. $retVal .= "<tr><td width=\"110\">Metainfo File:</td><td>".$transfer."</td></tr>";
  74. $retVal .= "<tr><td>Directory Name:</td><td>".$btmeta['info']['name']."</td></tr>";
  75. $retVal .= "<tr><td>Announce URL:</td><td>".$btmeta['announce']."</td></tr>";
  76. if (array_key_exists('comment',$btmeta))
  77. $retVal .= "<tr><td valign=\"top\">Comment:</td><td>".tfb_htmlencode($btmeta['comment'])."</td></tr>";
  78. $retVal .= "<tr><td>Created:</td><td>".date("F j, Y, g:i a",$btmeta['creation date'])."</td></tr>";
  79. $retVal .= "<tr><td>Torrent Size:</td><td>".$torrent_size." (".@formatBytesTokBMBGBTB($torrent_size).")</td></tr>";
  80. $retVal .= "<tr><td>Chunk size:</td><td>".$btmeta['info']['piece length']." (".@formatBytesTokBMBGBTB($btmeta['info']['piece length']).")</td></tr>";
  81. if (array_key_exists('files',$btmeta['info'])) {
  82. $retVal .= "<tr><td>Selected size:</td><td id=\"sel\">0</td></tr>";
  83. $retVal .= "</table><br>\n";
  84. if ($withForm) {
  85. $retVal .= "<form name=\"priority\" action=\"dispatcher.php?action=setFilePriority&riid=_referer_\" method=\"POST\" >";
  86. $retVal .= "<input type=\"hidden\" name=\"transfer\" value=\"".$transfer."\" >";
  87. }
  88. $retVal .= "<script type=\"text/javascript\">\n";
  89. $retVal .= "var sel = 0;\n";
  90. $retVal .= "d = new dTree('d');\n";
  91. $retVal .= $tree->draw(-1);
  92. $retVal .= "document.write(d);\n";
  93. $retVal .= "sel = getSizes();\n";
  94. $retVal .= "drawSel();\n";
  95. $retVal .= "</script>\n";
  96. $retVal .= "<input type=\"hidden\" name=\"filecount\" value=\"".count($btmeta['info']['files'])."\">";
  97. $retVal .= "<input type=\"hidden\" name=\"count\" value=\"".$dirnum."\">";
  98. $retVal .= "<br>";
  99. if ($withForm) {
  100. $retVal .= '<input type="submit" value="Save" >';
  101. $retVal .= "<br>";
  102. $retVal .= "</form>";
  103. }
  104. } else {
  105. $retVal .= "</table><br>";
  106. $retVal .= $btmeta['info']['name'].$torrent_size." (".@formatBytesTokBMBGBTB($torrent_size).")";
  107. }
  108. // return
  109. return $retVal;
  110. }
  111. // =============================================================================
  112. // classes
  113. // =============================================================================
  114. /**
  115. * dir
  116. */
  117. class dir {
  118. var $name;
  119. var $subdirs;
  120. var $files;
  121. var $num;
  122. var $prio;
  123. function dir($name,$num,$prio) {
  124. $this->name = $name;
  125. $this->num = $num;
  126. $this->prio = $prio;
  127. $this->files = array();
  128. $this->subdirs = array();
  129. }
  130. function &addFile($file) {
  131. $this->files[] =& $file;
  132. return $file;
  133. }
  134. function &addDir($dir) {
  135. $this->subdirs[] =& $dir;
  136. return $dir;
  137. }
  138. // code changed to support php4
  139. // thx to Mistar Muffin
  140. function &findDir($name) {
  141. foreach (array_keys($this->subdirs) as $v) {
  142. $dir =& $this->subdirs[$v];
  143. if($dir->name == $name)
  144. return $dir;
  145. }
  146. $retVal = false;
  147. return $retVal;
  148. }
  149. function draw($parent) {
  150. $draw = ("d.add(".$this->num.",".$parent.",\"".$this->name."\",".$this->prio.",0);\n");
  151. foreach($this->subdirs as $v)
  152. $draw .= $v->draw($this->num);
  153. foreach($this->files as $v) {
  154. if(is_object($v))
  155. $draw .= ("d.add(".$v->num.",".$this->num.",\"".$v->name."\",".$v->prio.",".$v->size.");\n");
  156. }
  157. return $draw;
  158. }
  159. }
  160. /**
  161. * file
  162. */
  163. class file {
  164. var $name;
  165. var $prio;
  166. var $size;
  167. var $num;
  168. function file($name,$num,$size,$prio) {
  169. $this->name = $name;
  170. $this->num = $num;
  171. $this->size = $size;
  172. $this->prio = $prio;
  173. }
  174. }
  175. ?>