| 1 | <?php |
|---|
| 2 | /* |
|---|
| 3 | This file is part of wTorrent. |
|---|
| 4 | |
|---|
| 5 | wTorrent is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 3 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | wTorrent is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | */ |
|---|
| 18 | class ListT extends rtorrent |
|---|
| 19 | { |
|---|
| 20 | private $view; |
|---|
| 21 | private $MAX_LENGTH_NAME = 55; |
|---|
| 22 | |
|---|
| 23 | public function construct() |
|---|
| 24 | { |
|---|
| 25 | if($this->_request['view'] == 'public') $this->view = 'public'; |
|---|
| 26 | if($this->_request['view'] == 'private') $this->view = 'private'; |
|---|
| 27 | if(!isset($this->_request['view'])) $this->view = 'public'; |
|---|
| 28 | |
|---|
| 29 | if(!$this->setClient()) |
|---|
| 30 | return false; |
|---|
| 31 | |
|---|
| 32 | /* d multicall with all the necessary info to generate the torrent list */ |
|---|
| 33 | $array_d = array('d.get_name', 'd.get_down_rate', 'd.get_up_rate', 'd.get_chunk_size','d.get_completed_chunks','d.get_size_chunks','d.get_state','d.get_peers_accounted','d.get_peers_complete','d.is_hash_checking','d.get_ratio','d.get_tracker_size','d.is_active','d.is_open','d.get_message','d.get_creation_date'); |
|---|
| 34 | $this->multicall->d_multicall($array_d); |
|---|
| 35 | // t multicall |
|---|
| 36 | $array_t = array('t.get_scrape_complete', 't.get_scrape_incomplete'); |
|---|
| 37 | $hashes = $this->getHashes(); // Retrieve hashes |
|---|
| 38 | foreach($hashes as $hash) |
|---|
| 39 | $this->multicall->t_multicall($hash, $array_t); |
|---|
| 40 | |
|---|
| 41 | if(isset($this->_request['start'])) $this->start($this->_request['start']); |
|---|
| 42 | if(isset($this->_request['stop'])) $this->stop($this->_request['stop']); |
|---|
| 43 | if(isset($this->_request['erase'])) $this->erase($this->_request['erase']); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | public function getView() |
|---|
| 47 | { |
|---|
| 48 | return $this->view; |
|---|
| 49 | } |
|---|
| 50 | public function getPublicHashes() |
|---|
| 51 | { |
|---|
| 52 | $hashes = $this->getHashes(); |
|---|
| 53 | foreach($hashes as $hash) |
|---|
| 54 | if($this->torrents[$hash]->get_private() === false) |
|---|
| 55 | $return[] = $hash; |
|---|
| 56 | |
|---|
| 57 | return $return; |
|---|
| 58 | } |
|---|
| 59 | public function getPublicHashesNum() |
|---|
| 60 | { |
|---|
| 61 | $hashes = $this->getHashes(); |
|---|
| 62 | $i = 0; |
|---|
| 63 | foreach($hashes as $hash) |
|---|
| 64 | if($this->torrents[$hash]->get_private() === false) |
|---|
| 65 | $i++; |
|---|
| 66 | |
|---|
| 67 | return $i; |
|---|
| 68 | } |
|---|
| 69 | public function getPrivateHashes() |
|---|
| 70 | { |
|---|
| 71 | $hashes = $this->getHashes(); |
|---|
| 72 | foreach($hashes as $hash) |
|---|
| 73 | if(($this->torrents[$hash]->get_private() === true) && ($this->torrents[$hash]->get_owner() == $this->getIdUser())) |
|---|
| 74 | $return[] = $hash; |
|---|
| 75 | |
|---|
| 76 | return $return; |
|---|
| 77 | } |
|---|
| 78 | public function getPrivateHashesNum() |
|---|
| 79 | { |
|---|
| 80 | $hashes = $this->getHashes(); |
|---|
| 81 | $i = 0; |
|---|
| 82 | foreach($hashes as $hash) |
|---|
| 83 | if(($this->torrents[$hash]->get_private() === false) && ($this->torrents[$hash]->get_owner() == $this->getIdUser())) |
|---|
| 84 | $i++; |
|---|
| 85 | |
|---|
| 86 | return $i; |
|---|
| 87 | } |
|---|
| 88 | public function getName($hash) |
|---|
| 89 | { |
|---|
| 90 | // Just in case the torrent name is too long return a shortened version. (THIS SHOULD BE DONE IN THE TEMPLATE WITH SMARTY TRUNCATE) |
|---|
| 91 | $name = $this->torrents[$hash]->get_name(); |
|---|
| 92 | if(strlen($name) > $this->MAX_LENGTH_NAME){ |
|---|
| 93 | $name = substr($name, 0, $this->MAX_LENGTH_NAME) . ' ...'; |
|---|
| 94 | } |
|---|
| 95 | return $name; |
|---|
| 96 | } |
|---|
| 97 | public function getState($hash) |
|---|
| 98 | { |
|---|
| 99 | return $this->torrents[$hash]->get_state(); |
|---|
| 100 | } |
|---|
| 101 | public function getOpen($hash) |
|---|
| 102 | { |
|---|
| 103 | return $this->torrents[$hash]->is_open(); |
|---|
| 104 | } |
|---|
| 105 | public function getConnPeers($hash) |
|---|
| 106 | { |
|---|
| 107 | return $this->torrents[$hash]->get_peers_accounted(); |
|---|
| 108 | } |
|---|
| 109 | public function getConnSeeds($hash) |
|---|
| 110 | { |
|---|
| 111 | return $this->torrents[$hash]->get_peers_complete(); |
|---|
| 112 | } |
|---|
| 113 | public function getTotalPeers($hash) |
|---|
| 114 | { |
|---|
| 115 | $peers = 0; |
|---|
| 116 | $num = $this->torrents[$hash]->get_tracker_size(); |
|---|
| 117 | |
|---|
| 118 | for($i = 0; $i < $num; $i++) |
|---|
| 119 | { |
|---|
| 120 | $t_peers = $this->torrents[$hash]->t_get_scrape_incomplete($i); |
|---|
| 121 | $peers += $t_peers; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | return $peers; |
|---|
| 125 | } |
|---|
| 126 | public function getTotalSeeds($hash) |
|---|
| 127 | { |
|---|
| 128 | $seeds = 0; |
|---|
| 129 | $num = $this->torrents[$hash]->get_tracker_size(); |
|---|
| 130 | |
|---|
| 131 | for($i = 0; $i < $num; $i++) |
|---|
| 132 | { |
|---|
| 133 | $t_seeds = $this->torrents[$hash]->t_get_scrape_complete($i); |
|---|
| 134 | $seeds += $t_seeds; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | return $seeds; |
|---|
| 138 | } |
|---|
| 139 | public function getDownRate($hash) |
|---|
| 140 | { |
|---|
| 141 | return round($this->torrents[$hash]->get_down_rate()/1024,2); |
|---|
| 142 | } |
|---|
| 143 | public function getUpRate($hash) |
|---|
| 144 | { |
|---|
| 145 | return round($this->torrents[$hash]->get_up_rate()/1024,2); |
|---|
| 146 | } |
|---|
| 147 | public function getPercent($hash) |
|---|
| 148 | { |
|---|
| 149 | return floor(($this->torrents[$hash]->get_completed_chunks()/$this->torrents[$hash]->get_size_chunks())*100); |
|---|
| 150 | } |
|---|
| 151 | public function getRatio($hash) |
|---|
| 152 | { |
|---|
| 153 | return round($this->torrents[$hash]->get_ratio()/1000,2); |
|---|
| 154 | } |
|---|
| 155 | public function isHashChecking($hash) |
|---|
| 156 | { |
|---|
| 157 | return $this->torrents[$hash]->is_hash_checking(); |
|---|
| 158 | } |
|---|
| 159 | public function getETA($hash) |
|---|
| 160 | { |
|---|
| 161 | $return = '--'; |
|---|
| 162 | if(($this->getPercent($hash) != 100) && ($this->torrents[$hash]->get_down_rate() != 0)) |
|---|
| 163 | $return = $this->formatETA(ceil((($this->torrents[$hash]->get_size_chunks() - $this->torrents[$hash]->get_completed_chunks()) * $this->torrents[$hash]->get_chunk_size() / 1024) / $this->torrents[$hash]->get_down_rate())); |
|---|
| 164 | |
|---|
| 165 | return $return; |
|---|
| 166 | } |
|---|
| 167 | public function getSize($hash) |
|---|
| 168 | { |
|---|
| 169 | return $this->getCorrectUnits($this->torrents[$hash]->get_size_chunks() * $this->torrents[$hash]->get_chunk_size()); |
|---|
| 170 | } |
|---|
| 171 | public function getDone($hash) |
|---|
| 172 | { |
|---|
| 173 | return $this->getCorrectUnits($this->torrents[$hash]->get_completed_chunks() * $this->torrents[$hash]->get_chunk_size()); |
|---|
| 174 | } |
|---|
| 175 | public function getTstate($hash) |
|---|
| 176 | { |
|---|
| 177 | if($this->torrents[$hash]->get_state() == 0) |
|---|
| 178 | { |
|---|
| 179 | $return = 'stopped'; |
|---|
| 180 | } else { |
|---|
| 181 | if($this->getPercent($hash) != 100){ |
|---|
| 182 | $return = 'downloading'; |
|---|
| 183 | } else { |
|---|
| 184 | $return = 'seeding'; |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | if($this->torrents[$hash]->is_open() != 1) |
|---|
| 188 | $return = 'closed'; |
|---|
| 189 | |
|---|
| 190 | if(($this->torrents[$hash]->get_message() != '') && ($this->torrents[$hash]->get_message() != 'Tracker: [Tried all trackers.]')) |
|---|
| 191 | $return = 'message'; |
|---|
| 192 | |
|---|
| 193 | if($this->torrents[$hash]->is_hash_checking() == 1) |
|---|
| 194 | $return = 'chash'; |
|---|
| 195 | } |
|---|
| 196 | public function getTstyle($hash) |
|---|
| 197 | { |
|---|
| 198 | switch($this->getTstate($hash)) |
|---|
| 199 | { |
|---|
| 200 | case 'downloading': |
|---|
| 201 | $return = 'green'; |
|---|
| 202 | break; |
|---|
| 203 | case 'stopped': |
|---|
| 204 | $return = 'black'; |
|---|
| 205 | break; |
|---|
| 206 | case 'seeding': |
|---|
| 207 | $return = 'blue'; |
|---|
| 208 | break; |
|---|
| 209 | case 'closed': |
|---|
| 210 | $return = 'black'; |
|---|
| 211 | break; |
|---|
| 212 | case 'message': |
|---|
| 213 | $return = 'red'; |
|---|
| 214 | break; |
|---|
| 215 | case 'chash': |
|---|
| 216 | $return = 'yellow'; |
|---|
| 217 | break; |
|---|
| 218 | } |
|---|
| 219 | return $return; |
|---|
| 220 | } |
|---|
| 221 | public function getMessage($hash) |
|---|
| 222 | { |
|---|
| 223 | return $this->torrents[$hash]->get_message(); |
|---|
| 224 | } |
|---|
| 225 | public function getTooltipText($hash) |
|---|
| 226 | { |
|---|
| 227 | if($this->getTstate($hash) == 'message') |
|---|
| 228 | $return = $this->getMessage($hash); |
|---|
| 229 | else |
|---|
| 230 | $return = null; |
|---|
| 231 | return $return; |
|---|
| 232 | } |
|---|
| 233 | public function getCreationDate($hash) |
|---|
| 234 | { |
|---|
| 235 | return $this->torrents[$hash]->get_creation_date(); |
|---|
| 236 | } |
|---|
| 237 | private function getCorrectUnits($size) |
|---|
| 238 | { |
|---|
| 239 | $size_units = 'bytes'; |
|---|
| 240 | if($size >= 1024) |
|---|
| 241 | { |
|---|
| 242 | $size /= 1024; |
|---|
| 243 | $size_units = 'Kb'; |
|---|
| 244 | } |
|---|
| 245 | if($size >= 1024) |
|---|
| 246 | { |
|---|
| 247 | $size /= 1024; |
|---|
| 248 | $size_units = 'Mb'; |
|---|
| 249 | } |
|---|
| 250 | if($size >= 1024) |
|---|
| 251 | { |
|---|
| 252 | $size /= 1024; |
|---|
| 253 | $size_units = 'Gb'; |
|---|
| 254 | } |
|---|
| 255 | return round($size, 1) . $size_units; |
|---|
| 256 | } |
|---|
| 257 | private function formatETA($time) |
|---|
| 258 | { |
|---|
| 259 | if (!is_array($periods)) { |
|---|
| 260 | $periods = array ( |
|---|
| 261 | 'weeks' => 604800, |
|---|
| 262 | 'days' => 86400, |
|---|
| 263 | 'hours' => 3600, |
|---|
| 264 | 'minutes' => 60, |
|---|
| 265 | ); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | $seconds = (float) $time * 1000; |
|---|
| 269 | foreach ($periods as $period => $value) |
|---|
| 270 | { |
|---|
| 271 | $count = floor($seconds / $value); |
|---|
| 272 | if ($count == 0) |
|---|
| 273 | continue; |
|---|
| 274 | |
|---|
| 275 | $values[$period] = $count; |
|---|
| 276 | $seconds = $seconds % $value; |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | foreach ($values as $key => $value) |
|---|
| 280 | { |
|---|
| 281 | $segment_name = substr($key, 0, 1); |
|---|
| 282 | $segment = $value . $segment_name; |
|---|
| 283 | // If ETA is weeks away, don't display minutes: |
|---|
| 284 | if ($key == "minutes") |
|---|
| 285 | if ($values["weeks"] >= 1) |
|---|
| 286 | $segment = ""; |
|---|
| 287 | |
|---|
| 288 | $array[] = $segment; |
|---|
| 289 | } |
|---|
| 290 | // If ETA is more then 30 weeks away, display "inf" instead of precise ETA: |
|---|
| 291 | if ($values["weeks"] > 30) |
|---|
| 292 | $str = "inf"; |
|---|
| 293 | else |
|---|
| 294 | $str = implode('', $array); |
|---|
| 295 | |
|---|
| 296 | return $str; |
|---|
| 297 | } |
|---|
| 298 | } |
|---|
| 299 | ?> |
|---|