StatFile.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /* $Id: StatFile.php 2403 2007-01-25 20:01:40Z 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. * StatFile
  17. */
  18. class StatFile
  19. {
  20. // public fields
  21. // file
  22. var $theFile;
  23. // af-props
  24. var $running = "1";
  25. var $percent_done = "0.0";
  26. var $time_left = "";
  27. var $down_speed = "";
  28. var $up_speed = "";
  29. var $sharing = "";
  30. var $transferowner = "";
  31. var $seeds = "";
  32. var $peers = "";
  33. var $seedlimit = "";
  34. var $uptotal = "";
  35. var $downtotal = "";
  36. var $size = "";
  37. // =========================================================================
  38. // public static methods
  39. // =========================================================================
  40. /**
  41. * factory
  42. *
  43. * @param $transfer
  44. * @param $user
  45. * @return StatFile
  46. */
  47. function getInstance($transfer, $user = '') {
  48. return new StatFile($transfer, $user);
  49. }
  50. // =========================================================================
  51. // ctor
  52. // =========================================================================
  53. /**
  54. * ctor
  55. *
  56. * @param $transfer
  57. * @param $user
  58. * @return StatFile
  59. */
  60. function StatFile($transfer, $user = "") {
  61. // init
  62. $this->init($transfer, $user);
  63. }
  64. // =========================================================================
  65. // public methods
  66. // =========================================================================
  67. /**
  68. * init stat-file
  69. *
  70. * @param $transfer
  71. * @param $user
  72. */
  73. function init($transfer, $user = '') {
  74. global $cfg;
  75. // file
  76. $this->theFile = $cfg["transfer_file_path"].$transfer.".stat";
  77. // set user
  78. if ($user != '')
  79. $this->transferowner = $user;
  80. // load file
  81. if (@file_exists($this->theFile)) {
  82. // read the stat file
  83. $data = @file_get_contents($this->theFile);
  84. // assign vars from content
  85. $content = @explode("\n", $data);
  86. $this->running = @ $content[0];
  87. $this->percent_done = @ $content[1];
  88. $this->time_left = @ $content[2];
  89. $this->down_speed = @ $content[3];
  90. $this->up_speed = @ $content[4];
  91. $this->transferowner = @ $content[5];
  92. $this->seeds = @ $content[6];
  93. $this->peers = @ $content[7];
  94. $this->sharing = @ $content[8];
  95. $this->seedlimit = @ $content[9];
  96. $this->uptotal = @ $content[10];
  97. $this->downtotal = @ $content[11];
  98. $this->size = @ $content[12];
  99. }
  100. }
  101. /**
  102. * call this on start
  103. *
  104. * @return boolean
  105. */
  106. function start() {
  107. // Reset all the var to new state (all but transferowner)
  108. $this->running = "1";
  109. $this->percent_done = "0.0";
  110. $this->time_left = "Starting...";
  111. $this->down_speed = "";
  112. $this->up_speed = "";
  113. $this->sharing = "";
  114. $this->seeds = "";
  115. $this->peers = "";
  116. $this->seedlimit = "";
  117. $this->uptotal = "";
  118. $this->downtotal = "";
  119. // write to file
  120. return $this->write();
  121. }
  122. /**
  123. * call this on enqueue
  124. *
  125. * @return boolean
  126. */
  127. function queue() {
  128. // Reset all the var to new state (all but transferowner)
  129. $this->running = "3";
  130. $this->time_left = "Waiting...";
  131. $this->down_speed = "";
  132. $this->up_speed = "";
  133. $this->seeds = "";
  134. $this->peers = "";
  135. $this->uptotal = "";
  136. $this->downtotal = "";
  137. // write to file
  138. return $this->write();
  139. }
  140. /**
  141. * Common write Method
  142. *
  143. * @return boolean
  144. */
  145. function write() {
  146. // content
  147. $content = $this->running."\n";
  148. $content .= $this->percent_done."\n";
  149. $content .= $this->time_left."\n";
  150. $content .= $this->down_speed."\n";
  151. $content .= $this->up_speed."\n";
  152. $content .= $this->transferowner."\n";
  153. $content .= $this->seeds."\n";
  154. $content .= $this->peers."\n";
  155. $content .= $this->sharing."\n";
  156. $content .= $this->seedlimit."\n";
  157. $content .= $this->uptotal."\n";
  158. $content .= $this->downtotal."\n";
  159. $content .= $this->size;
  160. // write file
  161. if ($handle = @fopen($this->theFile, "w")) {
  162. $resultSuccess = (@fwrite($handle, $content) !== false);
  163. @fclose($handle);
  164. return $resultSuccess;
  165. }
  166. return false;
  167. }
  168. }
  169. ?>