1
0

functions.transfer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /* $Id: functions.transfer.php 3022 2007-05-19 22:50:34Z danez $ */
  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. * init
  17. */
  18. function transfer_init() {
  19. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  20. // request-var
  21. $transfer = tfb_getRequestVar('transfer');
  22. if (empty($transfer))
  23. @error("missing params", "", "", array('transfer'));
  24. // validate transfer
  25. if (tfb_isValidTransfer($transfer) !== true) {
  26. AuditAction($cfg["constants"]["error"], "INVALID TRANSFER: ".$transfer);
  27. @error("Invalid Transfer", "", "", array($transfer));
  28. }
  29. // permission
  30. if ((!$cfg['isAdmin']) && (!IsOwner($cfg["user"], getOwner($transfer)))) {
  31. AuditAction($cfg["constants"]["error"], "ACCESS DENIED: ".$transfer);
  32. @error("Access Denied", "", "", array($transfer));
  33. }
  34. // get label
  35. $transferLabel = (strlen($transfer) >= 39) ? substr($transfer, 0, 35)."..." : $transfer;
  36. // set transfer vars
  37. $tmpl->setvar('transfer', $transfer);
  38. $tmpl->setvar('transferLabel', $transferLabel);
  39. $tmpl->setvar('transfer_exists', (transferExists($transfer)) ? 1 : 0);
  40. }
  41. /**
  42. * setCustomizeVars
  43. */
  44. function transfer_setCustomizeVars() {
  45. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  46. // customize settings
  47. if ($cfg['transfer_customize_settings'] == 2)
  48. $customize_settings = 1;
  49. elseif ($cfg['transfer_customize_settings'] == 1 && $cfg['isAdmin'])
  50. $customize_settings = 1;
  51. else
  52. $customize_settings = 0;
  53. $tmpl->setvar('customize_settings', $customize_settings);
  54. // set supported-vars for transfer
  55. if ($customize_settings == 0) {
  56. $tmpl->setvar('upload_support_enabled', 0);
  57. $tmpl->setvar('download_support_enabled', 0);
  58. $tmpl->setvar('max_uploads_enabled', 0);
  59. $tmpl->setvar('superseeder_enabled', 0);
  60. $tmpl->setvar('die_when_done_enabled', 0);
  61. $tmpl->setvar('sharekill_enabled', 0);
  62. $tmpl->setvar('minport_enabled', 0);
  63. $tmpl->setvar('maxport_enabled', 0);
  64. $tmpl->setvar('maxcons_enabled', 0);
  65. $tmpl->setvar('rerequest_enabled', 0);
  66. } else {
  67. $tmpl->setvar('upload_support_enabled', $cfg["supportMap"][$ch->client]['max_upload_rate']);
  68. $tmpl->setvar('download_support_enabled', $cfg["supportMap"][$ch->client]['max_download_rate']);
  69. $tmpl->setvar('max_uploads_enabled', $cfg["supportMap"][$ch->client]['max_uploads']);
  70. $tmpl->setvar('superseeder_enabled', $cfg["supportMap"][$ch->client]['superseeder']);
  71. $tmpl->setvar('die_when_done_enabled', $cfg["supportMap"][$ch->client]['die_when_done']);
  72. $tmpl->setvar('sharekill_enabled', $cfg["supportMap"][$ch->client]['sharekill']);
  73. $tmpl->setvar('minport_enabled', $cfg["supportMap"][$ch->client]['minport']);
  74. $tmpl->setvar('maxport_enabled', $cfg["supportMap"][$ch->client]['maxport']);
  75. $tmpl->setvar('maxcons_enabled', $cfg["supportMap"][$ch->client]['maxcons']);
  76. $tmpl->setvar('rerequest_enabled', $cfg["supportMap"][$ch->client]['rerequest']);
  77. }
  78. }
  79. /**
  80. * setGenericVarsFromCH
  81. */
  82. function transfer_setGenericVarsFromCH() {
  83. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  84. // set generic vars for transfer
  85. $tmpl->setvar('type', $ch->type);
  86. $tmpl->setvar('client', $ch->client);
  87. $tmpl->setvar('hash', $ch->hash);
  88. $tmpl->setvar('datapath', $ch->datapath);
  89. $tmpl->setvar('savepath', $ch->savepath);
  90. $tmpl->setvar('running', $ch->running);
  91. }
  92. /**
  93. * setVarsFromCHSettings
  94. */
  95. function transfer_setVarsFromCHSettings() {
  96. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  97. // set generic vars for transfer
  98. transfer_setGenericVarsFromCH();
  99. // set vars for transfer
  100. $tmpl->setvar('max_upload_rate', $ch->rate);
  101. $tmpl->setvar('max_download_rate', $ch->drate);
  102. $tmpl->setvar('max_uploads', $ch->maxuploads);
  103. $tmpl->setvar('superseeder', $ch->superseeder);
  104. $tmpl->setvar('die_when_done', $ch->runtime);
  105. $tmpl->setvar('sharekill', $ch->sharekill);
  106. $tmpl->setvar('minport', $ch->minport);
  107. $tmpl->setvar('maxport', $ch->maxport);
  108. $tmpl->setvar('maxcons', $ch->maxcons);
  109. $tmpl->setvar('rerequest', $ch->rerequest);
  110. }
  111. /**
  112. * setVarsFromProfileSettings
  113. *
  114. * @param $profile
  115. */
  116. function transfer_setVarsFromProfileSettings($profile) {
  117. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  118. // set generic vars for transfer
  119. transfer_setGenericVarsFromCH();
  120. //load custom settings
  121. $settings = GetProfileSettings($profile);
  122. // set vars for transfer
  123. $tmpl->setvar('max_upload_rate', $settings["rate"]);
  124. $tmpl->setvar('max_download_rate', $settings["drate"]);
  125. $tmpl->setvar('max_uploads', $settings["maxuploads"]);
  126. $tmpl->setvar('superseeder', $settings['superseeder']);
  127. $tmpl->setvar('die_when_done', $settings["runtime"]);
  128. $tmpl->setvar('sharekill', $settings["sharekill"]);
  129. $tmpl->setvar('minport', $settings["minport"]);
  130. $tmpl->setvar('maxport', $settings["maxport"]);
  131. $tmpl->setvar('maxcons', $settings["maxcons"]);
  132. $tmpl->setvar('rerequest', $settings["rerequest"]);
  133. }
  134. /**
  135. * setFileVars
  136. */
  137. function transfer_setFileVars() {
  138. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  139. // set vars for transfer
  140. $transferFilesList = array();
  141. switch ($ch->type) {
  142. case "torrent":
  143. require_once("inc/classes/BDecode.php");
  144. $tFile = $cfg["transfer_file_path"].$transfer;
  145. if ($fd = @fopen($tFile, "rd")) {
  146. $alltorrent = @fread($fd, @filesize($tFile));
  147. $btmeta = @BDecode($alltorrent);
  148. @fclose($fd);
  149. }
  150. $transferSizeSum = 0;
  151. if ((isset($btmeta)) && (is_array($btmeta)) && (isset($btmeta['info']))) {
  152. if (array_key_exists('files', $btmeta['info'])) {
  153. foreach ($btmeta['info']['files'] as $filenum => $file) {
  154. $name = (is_array($file['path'])) ? (implode("/", ($file['path']))) : $file['path'];
  155. $size = ((isset($file['length'])) && (is_numeric($file['length']))) ? $file['length'] : 0;
  156. $transferSizeSum += $size;
  157. array_push($transferFilesList, array(
  158. 'name' => $name,
  159. 'size' => ($size != 0) ? formatBytesTokBMBGBTB($size) : 0
  160. )
  161. );
  162. }
  163. } else {
  164. $size = $btmeta["info"]["piece length"] * (strlen($btmeta["info"]["pieces"]) / 20);
  165. $transferSizeSum += $size;
  166. array_push($transferFilesList, array(
  167. 'name' => $btmeta["info"]["name"],
  168. 'size' => formatBytesTokBMBGBTB($size)
  169. )
  170. );
  171. }
  172. }
  173. if (empty($transferFilesList)) {
  174. $tmpl->setvar('transferFilesString', "Empty");
  175. $tmpl->setvar('transferFileCount', count($btmeta['info']['files']));
  176. } else {
  177. $tmpl->setloop('transferFilesList', $transferFilesList);
  178. $tmpl->setvar('transferFileCount', count($transferFilesList));
  179. }
  180. $tmpl->setvar('transferSizeSum', ($transferSizeSum > 0) ? formatBytesTokBMBGBTB($transferSizeSum) : 0);
  181. return;
  182. case "wget":
  183. $ch = ClientHandler::getInstance('wget');
  184. $ch->setVarsFromFile($transfer);
  185. $transferSizeSum = 0;
  186. if (!empty($ch->url)) {
  187. require_once("inc/classes/SimpleHTTP.php");
  188. $size = SimpleHTTP::getRemoteSize($ch->url);
  189. $transferSizeSum += $size;
  190. array_push($transferFilesList, array(
  191. 'name' => $ch->url,
  192. 'size' => formatBytesTokBMBGBTB($size)
  193. )
  194. );
  195. }
  196. if (empty($transferFilesList)) {
  197. $tmpl->setvar('transferFilesString', "Empty");
  198. $tmpl->setvar('transferFileCount', 0);
  199. } else {
  200. $tmpl->setloop('transferFilesList', $transferFilesList);
  201. $tmpl->setvar('transferFileCount', count($transferFilesList));
  202. }
  203. $tmpl->setvar('transferSizeSum', ($transferSizeSum > 0) ? formatBytesTokBMBGBTB($transferSizeSum) : 0);
  204. return;
  205. case "nzb":
  206. require_once("inc/classes/NZBFile.php");
  207. $nzb = new NZBFile($transfer);
  208. $transferSizeSum = 0;
  209. if (empty($nzb->files)) {
  210. $tmpl->setvar('transferFilesString', "Empty");
  211. $tmpl->setvar('transferFileCount', 0);
  212. } else {
  213. foreach ($nzb->files as $file) {
  214. $transferSizeSum += $file['size'];
  215. array_push($transferFilesList, array(
  216. 'name' => $file['name'],
  217. 'size' => formatBytesTokBMBGBTB($file['size'])
  218. )
  219. );
  220. }
  221. $tmpl->setloop('transferFilesList', $transferFilesList);
  222. $tmpl->setvar('transferFileCount', $nzb->filecount);
  223. }
  224. $tmpl->setvar('transferSizeSum', ($transferSizeSum > 0) ? formatBytesTokBMBGBTB($transferSizeSum) : 0);
  225. return;
  226. }
  227. }
  228. /**
  229. * setDetailsVars
  230. *
  231. * @param $withForm
  232. */
  233. function transfer_setDetailsVars() {
  234. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  235. // set vars for transfer
  236. $tmpl->setvar('clientType', $ch->type);
  237. switch ($ch->type) {
  238. case "torrent":
  239. $tmpl->setvar('transferMetaInfo', getTorrentMetaInfo($transfer));
  240. return;
  241. case "wget":
  242. $ch->setVarsFromFile($transfer);
  243. $tmpl->setvar('transferUrl', $ch->url);
  244. return;
  245. case "nzb":
  246. $tmpl->setvar('transferMetaInfo', @htmlentities(file_get_contents($cfg["transfer_file_path"].$transfer), ENT_QUOTES));
  247. return;
  248. }
  249. }
  250. /**
  251. * setProfiledVars
  252. */
  253. function transfer_setProfiledVars() {
  254. global $cfg, $tmpl, $transfer, $transferLabel, $ch;
  255. // set vars for transfer
  256. if ($cfg['transfer_profiles'] <= 0) {
  257. $with_profiles = 0;
  258. } else {
  259. if ($cfg['transfer_profiles'] >= 2)
  260. $with_profiles = 1;
  261. else
  262. $with_profiles = ($cfg['isAdmin']) ? 1 : 0;
  263. }
  264. if ($with_profiles == 0) {
  265. // set vars for transfer from ch
  266. transfer_setVarsFromCHSettings();
  267. $tmpl->setvar('useLastSettings', 1);
  268. } else {
  269. $profile = tfb_getRequestVar('profile');
  270. if (($profile != "") && ($profile != "last_used")) {
  271. // set vars for transfer from profile
  272. transfer_setVarsFromProfileSettings($profile);
  273. $tmpl->setvar('useLastSettings', 0);
  274. $tmpl->setvar('profile', $profile);
  275. } else {
  276. // set vars for transfer from ch
  277. transfer_setVarsFromCHSettings();
  278. $tmpl->setvar('useLastSettings', 1);
  279. }
  280. // load profile lists
  281. $profiles = ($cfg['transfer_profiles'] >= 3 || $cfg['isAdmin'])
  282. ? GetProfiles($cfg["uid"], $profile)
  283. : array();
  284. $public_profiles = ($cfg['transfer_profiles'] >= 2 || $cfg['isAdmin'])
  285. ? GetPublicProfiles($profile)
  286. : array();
  287. if ((count($profiles) + count($public_profiles)) > 0) {
  288. $tmpl->setloop('profiles', $profiles);
  289. $tmpl->setloop('public_profiles', $public_profiles);
  290. } else {
  291. $with_profiles = 0;
  292. }
  293. }
  294. $tmpl->setvar('with_profiles', $with_profiles);
  295. }
  296. ?>