Xfer.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. <?php
  2. /* $Id: Xfer.php 2936 2007-04-20 17:48:17Z 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. * Original Code:
  17. * TorrentFlux xfer Statistics hack
  18. * blackwidow - matt@mattjanssen.net
  19. ******************************************************************************/
  20. /*
  21. TorrentFlux xfer Statistics hack is free code; you can redistribute it
  22. and/or modify it under the terms of the GNU General Public License as
  23. published by the Free Software Foundation; either version 2 of the License,
  24. or (at your option) any later version.
  25. */
  26. /**
  27. * class Xfer
  28. */
  29. class Xfer
  30. {
  31. // public fields
  32. // stats-arrays
  33. var $xfer = array();
  34. var $xfer_total = array();
  35. // newday-flag
  36. var $xfer_newday = 0;
  37. // =========================================================================
  38. // public static methods
  39. // =========================================================================
  40. /**
  41. * accessor for singleton
  42. *
  43. * @return Xfer
  44. */
  45. function getInstance() {
  46. global $instanceXfer;
  47. // initialize if needed
  48. if (!isset($instanceXfer))
  49. Xfer::initialize();
  50. return $instanceXfer;
  51. }
  52. /**
  53. * initialize Xfer.
  54. */
  55. function initialize() {
  56. global $instanceXfer;
  57. // create instance
  58. if (!isset($instanceXfer))
  59. $instanceXfer = new Xfer();
  60. }
  61. /**
  62. * init Xfer (login-task).
  63. */
  64. function init() {
  65. global $db;
  66. // if xfer is empty, insert a zero record for today
  67. $xferRecord = $db->GetRow("SELECT 1 FROM tf_xfer");
  68. if (empty($xferRecord))
  69. $db->Execute("INSERT INTO tf_xfer (user_id,date) values ('',".$db->DBDate(time()).")");
  70. }
  71. /**
  72. * gets new-day
  73. *
  74. * @return int
  75. */
  76. function getNewday() {
  77. global $instanceXfer;
  78. return (isset($instanceXfer))
  79. ? $instanceXfer->xfer_newday
  80. : 0;
  81. }
  82. /**
  83. * sets new-day
  84. */
  85. function setNewday() {
  86. global $instanceXfer, $db;
  87. // initialize if needed
  88. if (!isset($instanceXfer))
  89. Xfer::initialize();
  90. // set new-day
  91. $instanceXfer->xfer_newday = 0;
  92. $instanceXfer->xfer_newday = !$db->GetOne('SELECT 1 FROM tf_xfer WHERE date = '.$db->DBDate(time()));
  93. }
  94. /**
  95. * get Stats
  96. *
  97. * @return array
  98. */
  99. function getStats() {
  100. global $instanceXfer;
  101. return (isset($instanceXfer))
  102. ? $instanceXfer->xfer
  103. : array();
  104. }
  105. /**
  106. * get Stats Total
  107. *
  108. * @return array
  109. */
  110. function getStatsTotal() {
  111. global $instanceXfer;
  112. return (isset($instanceXfer))
  113. ? $instanceXfer->xfer_total
  114. : array();
  115. }
  116. /**
  117. * get Stats Formatted
  118. *
  119. * @return array
  120. */
  121. function getStatsFormatted() {
  122. global $cfg, $instanceXfer;
  123. return array(
  124. /* global */
  125. (isset($instanceXfer->xfer_total['total']['total']))
  126. ? @formatFreeSpace($instanceXfer->xfer_total['total']['total'] / 1048576)
  127. : 0,
  128. (isset($instanceXfer->xfer_total['month']['total']))
  129. ? @formatFreeSpace($instanceXfer->xfer_total['month']['total'] / 1048576)
  130. : 0,
  131. (isset($instanceXfer->xfer_total['month']['total']))
  132. ? @formatFreeSpace($instanceXfer->xfer_total['week']['total'] / 1048576)
  133. : 0,
  134. (isset($instanceXfer->xfer_total['month']['total']))
  135. ? @formatFreeSpace($instanceXfer->xfer_total['day']['total'] / 1048576)
  136. : 0,
  137. /* user */
  138. (isset($instanceXfer->xfer[$cfg["user"]]['total']['total']))
  139. ? @formatFreeSpace($instanceXfer->xfer[$cfg["user"]]['total']['total'] / 1048576)
  140. : 0,
  141. (isset($instanceXfer->xfer[$cfg["user"]]['total']['total']))
  142. ? @formatFreeSpace($instanceXfer->xfer[$cfg["user"]]['month']['total'] / 1048576)
  143. : 0,
  144. (isset($instanceXfer->xfer[$cfg["user"]]['total']['total']))
  145. ? @formatFreeSpace($instanceXfer->xfer[$cfg["user"]]['week']['total'] / 1048576)
  146. : 0,
  147. (isset($instanceXfer->xfer[$cfg["user"]]['total']['total']))
  148. ? @formatFreeSpace($instanceXfer->xfer[$cfg["user"]]['day']['total'] / 1048576)
  149. : 0
  150. );
  151. }
  152. /**
  153. * reset Xfer-Stats
  154. *
  155. * @return true or function exits with error
  156. */
  157. function resetStats() {
  158. global $db;
  159. // exec delete
  160. $db->Execute("DELETE FROM tf_xfer");
  161. // set transfers-cache
  162. cacheTransfersSet();
  163. //return
  164. return ($db->ErrorNo() == 0)
  165. ? true
  166. : $db->ErrorMsg();
  167. }
  168. /**
  169. * get Xfer-Usage
  170. *
  171. * @param $username
  172. * @return number
  173. */
  174. function getUsage($username = "") {
  175. global $db;
  176. // sql-state
  177. $sql = "SELECT SUM(download) AS download, SUM(upload) AS upload FROM tf_xfer";
  178. if ($username != "")
  179. $sql .= " WHERE user_id LIKE ".$db->qstr($username);
  180. // exec state
  181. $result = $db->Execute($sql);
  182. // error-check
  183. if ($db->ErrorNo() != 0) dbError($sql);
  184. // get usage
  185. $row = $result->FetchRow();
  186. $rtnValue = "0";
  187. if (!empty($row))
  188. $rtnValue = @formatFreeSpace($row["download"] / 1048576 + $row["upload"] / 1048576);
  189. // return
  190. return $rtnValue;
  191. }
  192. /**
  193. * get Xfer-Usage by date
  194. *
  195. * @param $username
  196. * @param $dateBegin
  197. * @param $dateEnd
  198. * @return array
  199. */
  200. function getUsageByDate($username = "", $dateBegin = "", $dateEnd = "") {
  201. global $db;
  202. // sql-state
  203. $sql = "SELECT SUM(download) AS download, SUM(upload) AS upload, date FROM tf_xfer";
  204. if ($username != "")
  205. $sql .= " WHERE user_id LIKE ".$db->qstr($username);
  206. if (($dateBegin != "") && ($dateEnd != ""))
  207. $sql .= " AND date >= ".$db->qstr($dateBegin)." AND date < ".$db->qstr($dateEnd);
  208. $sql .= " GROUP BY date ORDER BY date";
  209. // exec state
  210. $rtnValue = $db->GetAll($sql);
  211. // error-check
  212. if ($db->ErrorNo() != 0) dbError($sql);
  213. // return
  214. return $rtnValue;
  215. }
  216. /**
  217. * xfer update 1
  218. * add upload/download stats to the xfer array
  219. *
  220. * @param $entry
  221. * @param $transferowner
  222. * @param $client
  223. * @param $hash
  224. * @param $uptotal
  225. * @param $downtotal
  226. */
  227. function update1($entry, $transferowner, $client, $hash, $uptotal, $downtotal) {
  228. global $instanceXfer;
  229. // initialize if needed
  230. if (!isset($instanceXfer))
  231. Xfer::initialize();
  232. // call instance-method
  233. $instanceXfer->instance_update1($entry, $transferowner, $client, $hash, $uptotal, $downtotal);
  234. }
  235. /**
  236. * xfer update 2
  237. */
  238. function update2() {
  239. global $instanceXfer;
  240. // initialize if needed
  241. if (!isset($instanceXfer))
  242. Xfer::initialize();
  243. // call instance-method
  244. $instanceXfer->instance_update2();
  245. }
  246. /**
  247. * Inserts or updates SQL upload/download for user
  248. *
  249. * @param $user
  250. * @param $down
  251. * @param $up
  252. */
  253. function save($user, $down, $up) {
  254. global $db;
  255. // just to be safe..
  256. if (empty($down)) $down = "0";
  257. if (empty($up)) $up = "0";
  258. $sql = ($db->GetRow("SELECT 1 FROM tf_xfer WHERE user_id = ".$db->qstr($user)." AND date = ".$db->DBDate(time())))
  259. ? "UPDATE tf_xfer SET download = download+".$down.", upload = upload+".$up." WHERE user_id = ".$db->qstr($user)." AND date = ".$db->DBDate(time())
  260. : "INSERT INTO tf_xfer (user_id,date,download,upload) values (".$db->qstr($user).",".$db->DBDate(time()).",".$down.",".$up.")";
  261. $db->Execute($sql);
  262. }
  263. // =========================================================================
  264. // ctor
  265. // =========================================================================
  266. /**
  267. * ctor
  268. */
  269. function Xfer() {
  270. }
  271. // =========================================================================
  272. // public methods
  273. // =========================================================================
  274. /**
  275. * xfer update 1
  276. * add upload/download stats to the xfer array
  277. *
  278. * @param $entry
  279. * @param $transferowner
  280. * @param $client
  281. * @param $hash
  282. * @param $uptotal
  283. * @param $downtotal
  284. */
  285. function instance_update1($entry, $transferowner, $client, $hash, $uptotal, $downtotal) {
  286. global $cfg, $db;
  287. $ch = ClientHandler::getInstance($client);
  288. $transferTotalsCurrent = $ch->getTransferCurrentOP($entry, $hash, $uptotal, $downtotal);
  289. $this->_sumUsage($transferowner, $transferTotalsCurrent["downtotal"], $transferTotalsCurrent["uptotal"], 'total');
  290. $this->_sumUsage($transferowner, $transferTotalsCurrent["downtotal"], $transferTotalsCurrent["uptotal"], 'month');
  291. $this->_sumUsage($transferowner, $transferTotalsCurrent["downtotal"], $transferTotalsCurrent["uptotal"], 'week');
  292. $this->_sumUsage($transferowner, $transferTotalsCurrent["downtotal"], $transferTotalsCurrent["uptotal"], 'day');
  293. //XFER: if new day add upload/download totals to last date on record and subtract from today in SQL
  294. if ($this->xfer_newday != 0) {
  295. $this->xfer_newday = 2;
  296. $lastDate = $db->GetOne('SELECT date FROM tf_xfer ORDER BY date DESC');
  297. $sql = ($db->GetOne("SELECT 1 FROM tf_xfer WHERE user_id = ".$db->qstr($transferowner)." AND date = ".$db->qstr($lastDate)))
  298. ? "UPDATE tf_xfer SET download = download+".@($transferTotalsCurrent["downtotal"] + 0).", upload = upload+".@($transferTotalsCurrent["uptotal"] + 0)." WHERE user_id = ".$db->qstr($transferowner)." AND date = ".$db->qstr($lastDate)
  299. : "INSERT INTO tf_xfer (user_id,date,download,upload) values (".$db->qstr($transferowner).",".$db->qstr($lastDate).",".@($transferTotalsCurrent["downtotal"] + 0).",".@($transferTotalsCurrent["uptotal"] + 0).")";
  300. $db->Execute($sql);
  301. $sql = ($db->GetOne("SELECT 1 FROM tf_xfer WHERE user_id = ".$db->qstr($transferowner)." AND date = ".$db->DBDate(time())))
  302. ? "UPDATE tf_xfer SET download = download-".@($transferTotalsCurrent["downtotal"] + 0).", upload = upload-".@($transferTotalsCurrent["uptotal"] + 0)." WHERE user_id = ".$db->qstr($transferowner)." AND date = ".$db->DBDate(time())
  303. : "INSERT INTO tf_xfer (user_id,date,download,upload) values (".$db->qstr($transferowner).",".$db->DBDate(time()).",".@($transferTotalsCurrent["downtotal"] + 0).",".@($transferTotalsCurrent["uptotal"] + 0).")";
  304. $db->Execute($sql);
  305. }
  306. }
  307. /**
  308. * xfer update 2
  309. */
  310. function instance_update2() {
  311. global $cfg, $db;
  312. //XFER: if a new day but no .stat files where found put blank entry into the
  313. // DB for today to indicate accounting has been done for the new day
  314. if ($this->xfer_newday == 1)
  315. $db->Execute("INSERT INTO tf_xfer (user_id,date) values ('',".$db->DBDate(time()).")");
  316. $this->_getUsage('0001-01-01', 'total');
  317. $month_start = (date('j')>=$cfg['month_start']) ? date('Y-m-').$cfg['month_start'] : date('Y-m-',strtotime('-1 Month')).$cfg['month_start'];
  318. $this->_getUsage($month_start, 'month');
  319. $week_start = date('Y-m-d', strtotime('last '.$cfg['week_start']));
  320. $this->_getUsage($week_start, 'week');
  321. $day_start = date('Y-m-d');
  322. $this->_getUsage($day_start, 'day');
  323. }
  324. // =========================================================================
  325. // private methods
  326. // =========================================================================
  327. /**
  328. * Gets upload/download usage for all users starting at timestamp from SQL
  329. *
  330. * @param $start
  331. * @param $period
  332. */
  333. function _getUsage($start, $period) {
  334. global $db;
  335. $sql = "SELECT user_id, SUM(download) AS download, SUM(upload) AS upload FROM tf_xfer WHERE date >= ".$db->qstr($start)." AND user_id != '' GROUP BY user_id";
  336. $rtnValue = $db->GetAll($sql);
  337. if ($db->ErrorNo() != 0) dbError($sql);
  338. foreach ($rtnValue as $row)
  339. $this->_sumUsage($row[0], $row[1], $row[2], $period);
  340. }
  341. /**
  342. * Adds download/upload into correct usage_array (total, month, etc)
  343. *
  344. * @param $user
  345. * @param $download
  346. * @param $upload
  347. * @param $period
  348. */
  349. function _sumUsage($user, $download, $upload, $period) {
  350. @ $this->xfer[$user][$period]['download'] += $download;
  351. @ $this->xfer[$user][$period]['upload'] += $upload;
  352. @ $this->xfer[$user][$period]['total'] += $download + $upload;
  353. @ $this->xfer_total[$period]['download'] += $download;
  354. @ $this->xfer_total[$period]['upload'] += $upload;
  355. @ $this->xfer_total[$period]['total'] += $download + $upload;
  356. }
  357. }
  358. ?>