source: trunk/wtorrent/home/cls/ListT.cls.php @ 50

Revision 50, 11.7 KB checked in by royger, 2 years ago (diff)
  • Fixed 'stoped' typo error, #78
Line 
1<?php
2/*
3This file is part of wTorrent.
4
5wTorrent is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 3 of the License, or
8(at your option) any later version.
9
10wTorrent is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18class ListT extends rtorrent
19{
20        private  $view;
21        private  $torrents = array();
22        private  $MAX_LENGTH_NAME = 55;
23  private  $info_dowload = array('default', 
24                                                        'd.get_hash=', 
25                                                        'd.get_name=',
26                                                                'd.get_down_rate=',
27                                                                'd.get_up_rate=',
28                                                                'd.get_chunk_size=',
29                                                                'd.get_completed_chunks=',
30                                                                'd.get_size_chunks=',
31                                                                'd.get_state=',
32                                                                'd.get_peers_accounted=',
33                                                                'd.get_peers_complete=',
34                                                                'd.is_hash_checking=',
35                                                                'd.get_ratio=',
36                                                                'd.get_tracker_size=',
37                                                                'd.is_active=',
38                                                                'd.is_open=',
39                'd.get_message=',
40                'd.get_creation_date=',
41                                                        );
42        private $info_tracker = array('',
43                                                                "",
44                                                                't.get_scrape_complete=',
45                                                                't.get_scrape_incomplete=');
46
47    public function construct()
48        {
49                if($this->_request['view'] == 'public') $this->view = 'public';
50                if($this->_request['view'] == 'private') $this->view = 'private';
51                if(!isset($this->_request['view'])) $this->view = 'public';
52               
53                if(!$this->setClient())
54                        return false;
55               
56                if(isset($this->_request['start'])) $this->start($this->_request['start']);
57                if(isset($this->_request['stop'])) $this->stop($this->_request['stop']);
58                if(isset($this->_request['erase'])) $this->erase($this->_request['erase']);
59               
60                $this->getTorrents();
61        }
62
63        public function getView()
64        {
65                return $this->view;
66        }
67        public function getPublicHashes()
68        {
69                $tor_hashes = array_keys($this->torrents);
70                foreach($tor_hashes as $hash)
71                        if($this->torrents[$hash]['private'] === false)
72                                $return[] = $hash;
73                               
74                return $return;
75        }
76        public function getPublicHashesNum()
77        {
78                $tor_hashes = array_keys($this->torrents);
79                $i = 0;
80                foreach($tor_hashes as $hash)
81                        if($this->torrents[$hash]['private'] === false)
82                                $i++;
83                               
84                return $i;
85        }
86        public function getPrivateHashes()
87        {
88                $tor_hashes = array_keys($this->torrents);
89                foreach($tor_hashes as $hash)
90                        if($this->torrents[$hash]['private'] === true)
91                                $return[] = $hash;
92                               
93                return $return;
94        }
95        public function getPrivateHashesNum()
96        {
97                $tor_hashes = array_keys($this->torrents);
98                $i = 0;
99                foreach($tor_hashes as $hash)
100                        if($this->torrents[$hash]['private'] === true)
101                                $i++;
102                               
103                return $i;
104        }
105        public function getName($hash)
106        {
107                // Just in case the torrent name is too long return a shortened version.
108                $name = $this->torrents[$hash]['name'];
109                if(strlen($name) > $this->MAX_LENGTH_NAME){
110                        $name = substr($name, 0, $this->MAX_LENGTH_NAME) . ' ...';
111                }
112                return $name;
113        }
114        // public function getTags($hash)
115        // {
116        //      return $this->torrents[$hash]['tags'];
117        // }
118        public function getState($hash)
119        {
120                return $this->torrents[$hash]['state'];
121        }
122        public function getOpen($hash)
123        {
124                return $this->torrents[$hash]['is_open'];
125        }
126        public function getActive($hash)
127        {
128                return $this->torrents[$hash]['is_active'];
129        }
130        public function getConnPeers($hash)
131        {
132                return $this->torrents[$hash]['peers'];
133        }
134        public function getConnSeeds($hash)
135        {
136                return $this->torrents[$hash]['seeds'];
137        }
138        public function getTotalPeers($hash)
139        {
140                return $this->torrents[$hash]['peers_scrape'];
141        }
142        public function getTotalSeeds($hash)
143        {
144                return $this->torrents[$hash]['seeds_scrape'];
145        }
146        public function getDownRate($hash)
147        {
148                return $this->torrents[$hash]['down_rate'];
149        }
150        public function getUpRate($hash)
151        {
152                return $this->torrents[$hash]['up_rate'];
153        }
154        public function getPercent($hash)
155        {
156                return $this->torrents[$hash]['percent'];
157        }
158        public function getRatio($hash)
159        {
160                return $this->torrents[$hash]['ratio'];
161        }
162        public function isHashChecking($hash)
163        {
164                return $this->torrents[$hash]['is_hash_checking'];
165        }
166        public function getETA($hash)
167        {
168                return $this->torrents[$hash]['ETA'];
169        }
170        public function getSize($hash)
171        {
172                return $this->getCorrectUnits($this->torrents[$hash]['size_in_chunks'] * $this->torrents[$hash]['chunk_size']);
173        }
174        public function getDone($hash)
175        {
176                return $this->getCorrectUnits($this->torrents[$hash]['completed_chunks'] * $this->torrents[$hash]['chunk_size']);
177        }
178    public function getTstate($hash)
179    {
180        return $this->torrents[$hash]['tstate'];
181    }
182    public function getTstyle($hash)
183    {
184        switch($this->torrents[$hash]['tstate'])
185        {
186                case 'downloading':
187                        $return = 'green';
188                        break;
189                case 'stopped':
190                        $return = 'black';
191                        break;
192                case 'seeding':
193                        $return = 'blue';
194                        break;
195                case 'closed':
196                        $return = 'black';
197                        break;
198                case 'message':
199                        $return = 'red';
200                        break;
201                case 'chash':
202                        $return = 'yellow';
203                        break;
204        }
205        return $return;
206    }
207    public function getMessage($hash)
208    {
209        return $this->torrents[$hash]['message'];
210    }
211    public function getTooltipText($hash)
212    {
213        if($this->getTstate($hash) == 'message')
214            $return = $this->getMessage($hash);
215        else
216            $return = null;
217        return $return;
218    }
219    public function getCreationDate($hash)
220    {
221        return $this->torrents[$hash]['creation_date'];
222    }
223        private function getCorrectUnits($size)
224    {
225                $size_units = 'bytes';
226                if($size >= 1024)
227                {
228                $size /= 1024;
229                $size_units = 'Kb';
230                }
231                if($size >= 1024)
232                {
233            $size /= 1024;
234            $size_units = 'Mb';
235        }
236                if($size >= 1024)
237        {
238            $size /= 1024;
239            $size_units = 'Gb';
240        }
241        return round($size, 1) .  $size_units;
242
243    }
244        private function getPrivate()
245        {
246                $tt = array();
247                $sql = "select hash, user from torrents";
248                $result = $this->_db->query($sql);
249                $torr = $result->fetchAll();
250                foreach($torr as $torrent)
251                        $tt[$torrent['hash']] = $torrent['user'];
252                       
253                return $tt;
254        }
255
256        private function getTorrents()
257        {
258                //define("XMLRPC_DEBUG", 1);
259                //$this->client->setDebug(2);
260                foreach($this->info_dowload as $param)
261                        $array_post[] = new xmlrpcval($param, 'string');
262
263                //print_r(XMLRPC_prepare($array_post));
264                //print_r(XMLRPC_prepare($array_multi));
265
266                //$result = XMLRPC_request(RT_HOST , '/RPC2', 'd.multicall' ,$array_post);
267                $message = new xmlrpcmsg("d.multicall", $array_post);
268                $result = $this->client->send($message);
269                //print_r($result);
270                // Multicall for tracker info (reduces load time on big torrent list
271                        $array_post = array();
272                        foreach($this->info_tracker as $param)
273                                $array_post[] = new xmlrpcval($param, 'string');
274
275                        if(count($result->val)) {
276                                foreach($result->val as $torrent)
277                                {
278                                        $array_post[0] = new xmlrpcval($torrent[0], 'string');
279                                        $messages[] = new xmlrpcmsg("t.multicall", $array_post);
280                                }
281                                if(count($messages) > 0)
282                                        $Tresponses = $this->client->multicall($messages);
283                                //print_r($responses);
284                                foreach($result->val as $key => $torrent)
285                                {
286                                        // Check if the torrent is private and if the user can see it
287                                        $private = false;
288                                        $pr_torrent = $this->getPrivate();
289                                        if(array_key_exists($torrent[0], $pr_torrent))
290                                        {
291                                                $private = true;
292                                                if($pr_torrent[$torrent[0]] != $this->getIdUser())
293                                                        continue;
294                                        }
295                                        $this->torrents[$torrent[0]]['private'] = $private;
296                                        $this->torrents[$torrent[0]]['name'] = $torrent[1];
297                                        $this->torrents[$torrent[0]]['down_rate'] = round($torrent[2]/1024,2);
298                                        $this->torrents[$torrent[0]]['up_rate'] = round($torrent[3]/1024,2);
299                                        $this->torrents[$torrent[0]]['chunk_size'] = $torrent[4];
300                                        $this->torrents[$torrent[0]]['completed_chunks'] = $torrent[5];
301                                        $this->torrents[$torrent[0]]['size_in_chunks'] = $torrent[6];
302                                        $this->torrents[$torrent[0]]['state'] = $torrent[7];
303                                        $this->torrents[$torrent[0]]['peers'] = $torrent[8];
304                                        $this->torrents[$torrent[0]]['seeds'] = $torrent[9];
305                                        $this->torrents[$torrent[0]]['is_hash_checking'] = $torrent[10];
306                                        $this->torrents[$torrent[0]]['ratio'] = round($torrent[11]/1000,2);
307                                        $this->torrents[$torrent[0]]['num_trackers'] = $torrent[12];
308                                        $this->torrents[$torrent[0]]['is_active'] = $torrent[13];
309                                        $this->torrents[$torrent[0]]['is_open'] = $torrent[14];
310                                        $this->torrents[$torrent[0]]['message'] = $torrent[15];
311                                        $this->torrents[$torrent[0]]['creation_date'] = $torrent[16];
312
313                                        $this->torrents[$torrent[0]]['percent'] = floor(($this->torrents[$torrent[0]]['completed_chunks']/$this->torrents[$torrent[0]]['size_in_chunks'])*100);
314
315                                        $this->torrents[$torrent[0]]['ETA'] = '--';
316                                        if(($this->torrents[$torrent[0]]['percent'] != 100) && ($this->torrents[$torrent[0]]['down_rate'] != 0))
317                                                $this->torrents[$torrent[0]]['ETA'] = $this->formatETA(ceil((($this->torrents[$torrent[0]]['size_in_chunks'] - $this->torrents[$torrent[0]]['completed_chunks']) * $this->torrents[$torrent[0]]['chunk_size'] / 1024) / $torrent[2]));
318
319                                        if($this->torrents[$torrent[0]]['state'] == 0)
320                                        {
321                                                $this->torrents[$torrent[0]]['tstate'] = 'stopped';
322                                        } else {
323                                                if($this->torrents[$torrent[0]]['percent'] != 100){
324                                                        $this->torrents[$torrent[0]]['tstate'] = 'downloading';
325                                                } else {
326                                                        $this->torrents[$torrent[0]]['tstate'] = 'seeding';
327                                                }
328                                        }
329                                        if($this->torrents[$torrent[0]]['is_open'] != 1)
330                                                $this->torrents[$torrent[0]]['tstate'] = 'closed';
331
332                                        if(($this->torrents[$torrent[0]]['message'] != '') && ($this->torrents[$torrent[0]]['message'] != 'Tracker: [Tried all trackers.]'))
333                                                $this->torrents[$torrent[0]]['tstate'] = 'message';
334                                       
335                                        if($this->torrents[$torrent[0]]['is_hash_checking'] == 1)
336                                                $this->torrents[$torrent[0]]['tstate'] = 'chash';                   
337
338                                        foreach($Tresponses[$key]->val as $tracker)
339                                        {
340                                                $this->torrents[$torrent[0]]['seeds_scrape'] += $tracker[0];
341                                                $this->torrents[$torrent[0]]['peers_scrape'] += $tracker[1];
342                                        }
343                                        // $this->torrents[$torrent[0]]['tags'] = Array();
344                                        // $sql = "SELECT tagid FROM tags_torrents WHERE hash = '" . $torrent[0] . "';";
345                                        // $res = $this->_db->query( $sql );
346                                        //                      $result = $res->fetchAll();
347                                        // foreach($result as $tag){
348                                                //      $sql = "SELECT value FROM tags WHERE id = '" . $tag['tagid'] . "';"; // Falta afegir lo del user
349                                                //      $res = $this->_db->query( $sql );
350                                                //      $rslt = $res->fetch();
351                                                //      array_push($this->torrents[$torrent[0]]['tags'], $rslt['value']);
352                                                // }
353
354                                        }
355
356                                }
357                                //print_r($this->torrents);
358                                //XMLRPC_debug_print();*/
359                        }
360    private function formatETA($time)
361    {
362      if (!is_array($periods)) {
363        $periods = array (
364          'weeks'     => 604800,
365          'days'      => 86400,
366          'hours'     => 3600,
367          'minutes'   => 60,
368          );
369      }
370 
371      $seconds = (float) $time * 1000;
372      foreach ($periods as $period => $value) 
373      {
374        $count = floor($seconds / $value);
375        if ($count == 0) 
376          continue;
377 
378        $values[$period] = $count;
379        $seconds = $seconds % $value;
380      }
381 
382      foreach ($values as $key => $value) 
383      {
384        $segment_name = substr($key, 0, 1);
385        $segment = $value . $segment_name; 
386        // If ETA is weeks away, don't display minutes:       
387        if ($key == "minutes")
388          if ($values["weeks"] >= 1)
389            $segment = "";
390       
391        $array[] = $segment;
392      }
393      // If ETA is more then 30 weeks away, display "inf" instead of precise ETA:
394      if ($values["weeks"] > 30)
395        $str = "inf";
396      else
397        $str = implode('', $array);
398     
399      return $str;
400    }
401}
402?>
Note: See TracBrowser for help on using the repository browser.