PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/remote/ajax/index.ajx.php

https://github.com/messyo/rEmote
PHP | 201 lines | 170 code | 28 blank | 3 comment | 11 complexity | c9090f3136d2dd6e72664fb313ca372b MD5 | raw file
  1. <?php
  2. define('TO_ROOT', '../');
  3. define('NO_GC', true); // Don't call Garbage-Collector
  4. require_once(TO_ROOT.'inc/global.php');
  5. function add($hash, $field, $value)
  6. {
  7. global $output;
  8. $value = str_replace('\'', '\\\'', $value);
  9. $output .= ", '$hash', '$field', '$value'";
  10. }
  11. function error_handler($code, $text, $file, $line)
  12. {
  13. exit("ERROR ($code) in $file:$line: $text");
  14. }
  15. define('IS_REFRESH', true);
  16. require_once(TO_ROOT.'inc/defines/torrent.php');
  17. require_once(TO_ROOT.'inc/functions/list.fun.php');
  18. require_once(TO_ROOT.'inc/boxarea.php');
  19. session_write_close();
  20. set_error_handler('error_handler');
  21. //if(!(isset($_SESSION['status']) && $_SESSION['status'] > 0))
  22. // die('ERROR: Invalid Session, maybe timeout');
  23. $totup_format = format_bytes($global['upspeed']);
  24. $totdown_format = format_bytes($global['downspeed']);
  25. $percup = $settings['maxupspeed'] == 0 ? 0 : $global['upspeed']*100/($settings['maxupspeed']*1024);
  26. $percdwn = $settings['maxdownspeed'] == 0 ? 0 : $global['downspeed']*100/($settings['maxdownspeed']*1024);
  27. $output = "[ '$totup_format/s / $totdown_format/s - {$settings['html_title']}'";
  28. $output .= ', \''.progressbar($percup > 100 ? 100 : $percup, $totup_format.'/s').'\'';
  29. $output .= ', \''.progressbar($percdwn > 100 ? 100 : $percdwn, $totdown_format.'/s').'\'';
  30. $free = disk_free_space($_SESSION['dir']); $total = disk_total_space($_SESSION['dir']); $progress = ($total - $free) / $total * 100;
  31. $free = format_bytes($free); $total = format_bytes($total);
  32. $output .= ", '<div>{$lng['freespace']}:<br />$free/$total</div>".progressbar($progress).'\'';
  33. if($settings['shoutbox']
  34. && (
  35. in_array(BoxArea::BOX_SHOUTBOX, $_SESSION['boxpositions'][BOX_SIDE])
  36. || in_array(BoxArea::BOX_SHOUTBOX, $_SESSION['boxpositions'][BOX_TOP])
  37. || in_array(BoxArea::BOX_SHOUTBOX, $_SESSION['boxpositions'][BOX_BOTTOM])
  38. || in_array(BoxArea::BOX_SHOUTBOX, $_SESSION['boxpositions'][BOX_RIGHT])
  39. )
  40. )
  41. {
  42. $shoutbox = new Shoutbox;
  43. $shoutboxcontent = addslashes($shoutbox->getShouts());
  44. }
  45. else
  46. {
  47. $shoutboxcontent = '';
  48. }
  49. $output .= ", '$shoutboxcontent'";
  50. if(($_SESSION['status'] >= 2) && ($l = fopen('/proc/loadavg', 'r')))
  51. {
  52. $loads = explode(' ', fgets($l));
  53. fclose($l);
  54. $perc = $loads[0] > 1 ? 100 : ($loads[0]*100);
  55. $output .= ", '<div>{$lng['load']}: {$loads[0]} {$loads[1]} {$loads[2]}</div>".progressbar($perc, $perc.'%').'</div>\'';
  56. }
  57. else
  58. $output .= ', \'\'';
  59. if($_SESSION['refmode'] == 2)
  60. {
  61. // refresh all
  62. $data=get_full_list($_SESSION['viewmode'], $_SESSION['groupmode'], $_SESSION['sourcemode']);
  63. $cache = cache_get('refresh');
  64. if($cache === false)
  65. $cache = array();
  66. $changed = false;
  67. if(is_array($data) && count($data))
  68. {
  69. foreach($data as $groupid => $group)
  70. {
  71. $t_count = 0;
  72. $t_done = 0.0;
  73. $t_eta = 0.0;
  74. $t_sup = 0.0;
  75. $t_sdwn = 0.0;
  76. $t_seeded = 0.0;
  77. $t_completed = 0.0;
  78. $t_size = 0.0;
  79. $t_ratio = 0.0;
  80. foreach($group as $item)
  81. {
  82. $l = array();
  83. $l_hash = $item[HASH];
  84. $l_status = $item[STATUS];
  85. $l['statuskey'] = $lng["status$l_status"];
  86. $l['statusimg'] = "<img src=\"{$imagedir}status_$l_status.png\" alt=\"{$l['statuskey']}\" />";
  87. $l['done'] = progressbar($item[PERCENT_COMPLETE], $item[PERCENT_COMPLETE].'%');
  88. $l['eta'] = $item[ETA];
  89. $l['upspeed'] = ($item[UP_RATE] ? ('<span class="speedhighlight">'.format_bytes($item[UP_RATE]).'/s</span>') : format_bytes($item[UP_RATE]).'/s');
  90. $l['downspeed'] = ($item[DOWN_RATE] ? ('<span class="speedhighlight">'.format_bytes($item[DOWN_RATE]).'/s</span>') : format_bytes($item[DOWN_RATE]).'/s');
  91. $l['seeded'] = format_bytes($item[UP_TOTAL]);
  92. $l['completed'] = format_bytes($item[COMPLETED_BYTES]);
  93. $l['peers'] = "{$item[PEERS_CONNECTED]}/{$item[PEERS_NOT_CONNECTED]} ({$item[PEERS_COMPLETE]})";
  94. $l['ratio'] = round($item[RATIO]/1000, 2);
  95. $l['message'] = $item[MESSAGE];
  96. if($l['ratio'] < 1)
  97. $l['ratio'] = '<span style="color: #'.dechex(255-($l['ratio']*255))."0000;\">{$l['ratio']}</span>";
  98. //$l['check'] = "<input type=\"checkbox\" class=\"checkbox\" name=\"multiselect[]\" value=\"$l_hash\" />";
  99. $t_count += 1;
  100. $t_done += $item[PERCENT_COMPLETE];
  101. $t_sup += $item[UP_RATE];
  102. $t_sdwn += $item[DOWN_RATE];
  103. $t_seeded += $item[UP_TOTAL];
  104. $t_completed += $item[COMPLETED_BYTES];
  105. $t_size += $item[SIZE_BYTES];
  106. $t_ratio += $item[RATIO];
  107. if(isset($cache[$l_hash]))
  108. {
  109. $diff = array_diff($l, $cache[$l_hash]);
  110. if(count($diff))
  111. {
  112. $changed = true;
  113. foreach($diff as $key => $val)
  114. add($l_hash, $key, $val);
  115. }
  116. }
  117. else
  118. {
  119. $changed = true;
  120. $cache[$l_hash] = $l;
  121. foreach($l as $key => $val)
  122. add($l_hash, $key, $val);
  123. }
  124. }
  125. $ident = 'group'.$groupid;
  126. $l['count'] = $t_count;
  127. $l['done'] = round($t_done/$t_count, 2).'%';
  128. $l['upspeed'] = ($t_sup ? ('<span class="speedhighlight">'.format_bytes($t_sup).'/s</span>') : format_bytes($t_sup).'/s');
  129. $l['downspeed'] = ($t_sdwn ? ('<span class="speedhighlight">'.format_bytes($t_sdwn).'/s</span>') : format_bytes($t_sdwn).'/s');
  130. $l['speeds'] = "{$l['upspeed']}/{$l['downspeed']}";
  131. $l['seeded'] = format_bytes($t_seeded);
  132. $l['completed'] = format_bytes($t_completed);
  133. $l['size'] = format_bytes($t_size);
  134. $l['ratio'] = $t_completed > 0 ? round(($t_seeded/$t_completed), 2) : 0;
  135. if($l['ratio'] < 1)
  136. $l['ratio'] = '<span style="color: #'.dechex(255-($l['ratio']*255))."0000;\">{$l['ratio']}</span>";
  137. if(isset($cache[$ident]))
  138. {
  139. $diff = array_diff($l, $cache[$ident]);
  140. if(count($diff))
  141. {
  142. $changed = true;
  143. foreach($diff as $key => $val)
  144. add($ident, $key, $val);
  145. }
  146. }
  147. else
  148. {
  149. $changed = true;
  150. $cache[$ident] = $l;
  151. foreach($l as $key => $val)
  152. add($ident, $key, $val);
  153. }
  154. }
  155. }
  156. if($changed)
  157. cache_put('refresh', $cache, $_SESSION['uid'], time()+(7*24*60*60));
  158. }
  159. header("Content-type: text/txt");
  160. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  161. header("Last-Modified: " . gmdate("D, d M Y H:i:s"). " GMT");
  162. header("Cache-Control: no-cache, must-revalidate");
  163. header("Pragma: no-cache");
  164. echo $output;
  165. echo ']';
  166. ?>