1
0

functions.common.transfer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /* $Id: functions.common.transfer.php 3269 2007-11-11 22:48:00Z b4rt $ */
  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. * get log of a Transfer
  17. *
  18. * @param $transfer
  19. * @return string
  20. */
  21. function getTransferLog($transfer) {
  22. global $cfg;
  23. $emptyLog = "log empty";
  24. // sanity-check
  25. if (!isset($transfer) || (tfb_isValidTransfer($transfer) !== true))
  26. return "invalid transfer";
  27. // log-file
  28. $transferLogFile = $cfg["transfer_file_path"].$transfer.".log";
  29. // check
  30. if (!(file_exists($transferLogFile)))
  31. return $emptyLog;
  32. // open
  33. $handle = false;
  34. $handle = @fopen($transferLogFile, "r");
  35. if (!$handle)
  36. return $emptyLog;
  37. // read
  38. $data = "";
  39. while (!@feof($handle))
  40. $data .= @fgets($handle, 8192);
  41. @fclose ($handle);
  42. if ($data == "")
  43. return $emptyLog;
  44. // return
  45. return $data;
  46. }
  47. /**
  48. * Function to delete saved Settings
  49. *
  50. * @param $transfer
  51. * @return boolean
  52. */
  53. function deleteTransferSettings($transfer) {
  54. global $db;
  55. $sql = "DELETE FROM tf_transfers WHERE transfer = ".$db->qstr($transfer);
  56. $db->Execute($sql);
  57. if ($db->ErrorNo() != 0) dbError($sql);
  58. // set transfers-cache
  59. cacheTransfersSet();
  60. return true;
  61. }
  62. /**
  63. * sets the running flag in the db to stopped.
  64. *
  65. * @param $transfer name of the transfer
  66. */
  67. function stopTransferSettings($transfer) {
  68. global $db;
  69. $db->Execute("UPDATE tf_transfers SET running = '0' WHERE transfer = ".$db->qstr($transfer));
  70. // set transfers-cache
  71. cacheTransfersSet();
  72. return true;
  73. }
  74. /**
  75. * waits until transfer is up/down
  76. *
  77. * @param $transfer name of the transfer
  78. * @param $state : true = start, false = stop
  79. * @param $maxWait in seconds
  80. * @return boolean
  81. */
  82. function waitForTransfer($transfer, $state, $maxWait = 15) {
  83. $maxLoops = $maxWait * 5;
  84. $loopCtr = 0;
  85. for (;;) {
  86. @clearstatcache();
  87. if (isTransferRunning($transfer) === $state) {
  88. return true;
  89. } else {
  90. $loopCtr++;
  91. if ($loopCtr > $maxLoops)
  92. return false;
  93. else
  94. usleep(200000); // wait for 0.2 seconds
  95. }
  96. }
  97. return true;
  98. }
  99. /**
  100. * resets totals of a transfer
  101. *
  102. * @param $transfer name of the transfer
  103. * @param $delete boolean if to delete meta-file
  104. * @return array
  105. */
  106. function resetTransferTotals($transfer, $delete = false) {
  107. global $cfg, $db, $transfers;
  108. $msgs = array();
  109. $tid = getTransferHash($transfer);
  110. // delete meta-file
  111. if ($delete) {
  112. $ch = ClientHandler::getInstance(getTransferClient($transfer));
  113. $ch->delete($transfer);
  114. if (count($ch->messages) > 0)
  115. $msgs = array_merge($msgs, $ch->messages);
  116. } else {
  117. // reset in stat-file
  118. $sf = new StatFile($transfer, getOwner($transfer));
  119. $sf->uptotal = 0;
  120. $sf->downtotal = 0;
  121. $sf->write();
  122. }
  123. // reset in db
  124. $sql = "DELETE FROM tf_transfer_totals WHERE tid = ".$db->qstr($tid);
  125. $db->Execute($sql);
  126. if ($db->ErrorNo() != 0) dbError($sql);
  127. // set transfers-cache
  128. cacheTransfersSet();
  129. return $msgs;
  130. }
  131. /**
  132. * deletes data of a transfer
  133. *
  134. * @param $transfer name of the transfer
  135. * @return array
  136. */
  137. function deleteTransferData($transfer) {
  138. global $cfg, $transfers;
  139. $msgs = array();
  140. if (($cfg['isAdmin']) || (IsOwner($cfg["user"], getOwner($transfer)))) {
  141. // only torrent
  142. if (substr($transfer, -8) != ".torrent")
  143. return $msgs;
  144. // delete data
  145. $datapath = getTransferDatapath($transfer);
  146. if (($datapath != "") && ($datapath != ".")) {
  147. $targetPath = getTransferSavepath($transfer).$datapath;
  148. if (tfb_isValidPath($targetPath)) {
  149. if ((@is_dir($targetPath)) || (@is_file($targetPath))) {
  150. avddelete($targetPath);
  151. AuditAction($cfg["constants"]["fm_delete"], $targetPath);
  152. }
  153. } else {
  154. $msg = "ILLEGAL DELETE: ".$cfg["user"]." attempted to delete data of ".$transfer;
  155. AuditAction($cfg["constants"]["error"], $msg);
  156. array_push($msgs, $msg);
  157. }
  158. }
  159. } else {
  160. $msg = "ILLEGAL DELETE: ".$cfg["user"]." attempted to delete data of ".$transfer;
  161. AuditAction($cfg["constants"]["error"], $msg);
  162. array_push($msgs, $msg);
  163. }
  164. return $msgs;
  165. }
  166. /**
  167. * gets size of data of a torrent
  168. *
  169. * @param $transfer name of the torrent
  170. * @return int with size of data of torrent.
  171. * -1 if error
  172. * 4096 if dir (lol ~)
  173. */
  174. function getTorrentDataSize($transfer) {
  175. global $cfg;
  176. $datapath = getTransferDatapath($transfer);
  177. return (($datapath != "") && ($datapath != "."))
  178. ? file_size(getTransferSavepath($transfer).$datapath)
  179. : -1;
  180. }
  181. /**
  182. * gets datapath of a transfer.
  183. *
  184. * @param $transfer name of the torrent
  185. * @return var with transfer-datapath or empty string
  186. */
  187. function getTransferDatapath($transfer) {
  188. global $cfg, $db, $transfers;
  189. if (isset($transfers['settings'][$transfer]['datapath'])) {
  190. return $transfers['settings'][$transfer]['datapath'];
  191. } else {
  192. $datapath = $db->GetOne("SELECT datapath FROM tf_transfers WHERE transfer = ".$db->qstr($transfer));
  193. if (empty($datapath)) {
  194. if (substr($transfer, -8) == ".torrent") {
  195. // this is a torrent-client
  196. require_once('inc/classes/BDecode.php');
  197. $ftorrent = $cfg["transfer_file_path"].$transfer;
  198. $fd = fopen($ftorrent, "rd");
  199. $alltorrent = fread($fd, filesize($ftorrent));
  200. $btmeta = @BDecode($alltorrent);
  201. $datapath = (empty($btmeta['info']['name']))
  202. ? ""
  203. : trim($btmeta['info']['name']);
  204. } else if (substr($transfer, -5) == ".wget") {
  205. // this is wget.
  206. $datapath = ".";
  207. } else if (substr($transfer, -4) == ".nzb") {
  208. // This is nzbperl.
  209. $datapath = ".";
  210. } else {
  211. $datapath = "";
  212. }
  213. }
  214. $transfers['settings'][$transfer]['datapath'] = $datapath;
  215. return $datapath;
  216. }
  217. }
  218. /**
  219. * gets savepath of a transfer.
  220. *
  221. * @param $transfer name of the torrent
  222. * @return var with transfer-savepath or empty string
  223. */
  224. function getTransferSavepath($transfer) {
  225. global $cfg, $db, $transfers;
  226. if (isset($transfers['settings'][$transfer]['savepath'])) {
  227. return $transfers['settings'][$transfer]['savepath'];
  228. } else {
  229. $savepath = $db->GetOne("SELECT savepath FROM tf_transfers WHERE transfer = ".$db->qstr($transfer));
  230. if (empty($savepath))
  231. $savepath = ($cfg["enable_home_dirs"] != 0)
  232. ? $cfg["path"].getOwner($transfer).'/'
  233. : $cfg["path"].$cfg["path_incoming"].'/';
  234. /*Origin*/
  235. //$transfers['settings'][$transfer]['savepath'] = $savepath;
  236. //return $savepath;
  237. /*Mod*/
  238. $dir = save_to_folder($transfer);
  239. if($dir){
  240. $dirs = $dir;
  241. } else {
  242. $dirs = 'Orhers';
  243. }
  244. $transfers['settings'][$transfer]['savepath'] = $savepath.$dirs;
  245. return $savepath.$dirs;
  246. }
  247. }
  248. /**
  249. * Save to Folder
  250. */
  251. function save_to_folder($transfer){
  252. $path = array(
  253. 'xxx' => 'xXx',
  254. 'Tvs' => 'Sorozatok', //TVStore special
  255. 'ebook' => 'eBook', //nCore Special
  256. 'xvidser' => 'Sorozatok', //nCore Special
  257. 'xvid' => 'Filmek', //nCore Special
  258. 'game' => 'Game', //nCore Special
  259. 'iso' => 'Utils', //nCore Special
  260. 'misc' => 'Utils', //nCore Special
  261. 'mp3' => 'Zene', //nCore Special
  262. 'flac' => 'Zene', //nCore Special
  263. 'va-' => 'Zene',
  264. 'HDTV' => 'Sorozatok',
  265. 'IPTV' => 'Sorozatok', //BitHUmen Special
  266. 'WEB-DLRip' => 'Sorozatok', //Other torrent error (Filmek or Sorozatok)
  267. 'BDRip' => 'Filmek', //Other torrent error (Filmek or Sorozatok)
  268. '_hd_' => 'Filmek', //nCore Special
  269. 'HD' => 'Filmek',
  270. '_dvd9_' => 'Filmek', //nCore Special
  271. 'DVDRiP' => 'Filmek',
  272. 'DVD' => 'Filmek'
  273. );
  274. foreach($path as $a => $b){
  275. $pos = stripos($transfer, $a);
  276. if ($pos !== false) {
  277. return $b;
  278. }
  279. }
  280. }
  281. /**
  282. * set file prio
  283. *
  284. * @param $transfer
  285. */
  286. function setFilePriority($transfer) {
  287. global $cfg;
  288. // we will use this to determine if we should create a prio file.
  289. // if the user passes all 1's then they want the whole thing.
  290. // so we don't need to create a prio file.
  291. // if there is a -1 in the array then they are requesting
  292. // to skip a file. so we will need to create the prio file.
  293. $okToCreate = false;
  294. if (!empty($transfer)) {
  295. $fileName = $cfg["transfer_file_path"].$transfer.".prio";
  296. $result = array();
  297. $files = array();
  298. if (isset($_REQUEST['files'])) {
  299. $filesTemp = (is_array($_REQUEST['files']))
  300. ? $_REQUEST['files']
  301. : array($_REQUEST['files']);
  302. $files = array_filter($filesTemp, "getFile");
  303. }
  304. // if there are files to get then process and create a prio file.
  305. if (count($files) > 0) {
  306. for ($i=0; $i <= tfb_getRequestVar('count'); $i++) {
  307. if (in_array($i,$files)) {
  308. array_push($result, 1);
  309. } else {
  310. $okToCreate = true;
  311. array_push($result, -1);
  312. }
  313. }
  314. if ($okToCreate) {
  315. $fp = fopen($fileName, "w");
  316. fwrite($fp,tfb_getRequestVar('filecount').",");
  317. fwrite($fp,implode($result,','));
  318. fclose($fp);
  319. } else {
  320. // No files to skip so must be wanting them all.
  321. // So we will remove the prio file.
  322. @unlink($fileName);
  323. }
  324. } else {
  325. // No files selected so must be wanting them all.
  326. // So we will remove the prio file.
  327. @unlink($fileName);
  328. }
  329. }
  330. }
  331. /**
  332. * gets scrape-info of a torrent as string
  333. *
  334. * @param $transfer name of the torrent
  335. * @return string with torrent-scrape-info
  336. */
  337. function getTorrentScrapeInfo($transfer) {
  338. global $cfg;
  339. $hasClient = false;
  340. // transmissioncli
  341. if (is_executable($cfg["btclient_transmission_bin"])) {
  342. $hasClient = true;
  343. $retVal = "";
  344. $retVal = @shell_exec("HOME=".tfb_shellencode($cfg["path"])."; export HOME; ".$cfg["btclient_transmission_bin"] . " -s ".tfb_shellencode($cfg["transfer_file_path"].$transfer));
  345. if ((isset($retVal)) && ($retVal != "") && (!preg_match('/.*failed.*/i', $retVal)))
  346. return trim($retVal);
  347. }
  348. // ttools.pl
  349. if (is_executable($cfg["perlCmd"])) {
  350. $hasClient = true;
  351. $retVal = "";
  352. $retVal = @shell_exec($cfg["perlCmd"].' -I '.tfb_shellencode($cfg["docroot"].'bin/ttools').' '.tfb_shellencode($cfg["docroot"].'bin/ttools/ttools.pl').' -s '.tfb_shellencode($cfg["transfer_file_path"].$transfer));
  353. if ((isset($retVal)) && ($retVal != "") && (!preg_match('/.*failed.*/i', $retVal)))
  354. return trim($retVal);
  355. }
  356. // failed
  357. return ($hasClient)
  358. ? "Scrape failed"
  359. : "No Scrape-Client";
  360. }
  361. /**
  362. * gets ary of running clients (via call to ps)
  363. *
  364. * @param $client
  365. * @return array
  366. */
  367. function getRunningClientProcesses($client = '') {
  368. // client-array
  369. $clients = ($client == '')
  370. ? array('tornado', 'transmission', 'mainline', 'wget', 'nzbperl', 'azureus')
  371. : array($client);
  372. // get clients
  373. $retVal = array();
  374. foreach ($clients as $client) {
  375. // client-handler
  376. $ch = ClientHandler::getInstance($client);
  377. $procs = $ch->runningProcesses();
  378. if (!empty($procs))
  379. $retVal = array_merge($retVal, $procs);
  380. }
  381. // return
  382. return $retVal;
  383. }
  384. /**
  385. * get info of running clients (via call to ps)
  386. *
  387. * @param $client
  388. * @return string
  389. */
  390. function getRunningClientProcessInfo($client = '') {
  391. // client-array
  392. $clients = ($client == '')
  393. ? array('tornado', 'transmission', 'mainline', 'wget', 'nzbperl', 'azureus')
  394. : array($client);
  395. // get clients
  396. $retVal = "";
  397. foreach ($clients as $client) {
  398. // client-handler
  399. $ch = ClientHandler::getInstance($client);
  400. $retVal .= $ch->runningProcessInfo();
  401. }
  402. // return
  403. return $retVal;
  404. }
  405. ?>