source: trunk/wtorrent/home/cls/Feeds.cls.php @ 4

Revision 4, 10.3 KB checked in by royger, 3 years ago (diff)
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 Feeds extends rtorrent
19{
20        /*private  $torrents;
21    private  $info_dowload = array('default',
22                                                        'd.get_hash=',
23                                                        'd.get_name=',
24                                                                'd.get_down_rate=',
25                                                                'd.get_up_rate=',
26                                                                'd.get_chunk_size=',
27                                                                'd.get_completed_chunks=',
28                                                                'd.get_size_chunks=',
29                                                                'd.get_state=',
30                                                                'd.get_peers_accounted=',
31                                                                'd.get_peers_complete=',
32                                                                'd.is_hash_checking=',
33                                                                'd.get_ratio=',
34                                                                'd.get_tracker_size=');
35        private $info_tracker = array('',
36                                                                "",
37                                                                't.get_scrape_complete=',
38                                                                't.get_scrape_incomplete=');*/
39        private $info = array();
40        private $feeds = array();
41        private $view_feed = false;
42
43    public function construct()
44        {
45                if(!$this->setClient())
46                        return false;
47               
48                if(isset($this->_request['download'])) $this->Download($this->_request['download']);
49                if(isset($this->_request['feed_add'])) $this->AddFeed($this->_request['feed_url']);
50                if(isset($this->_request['erase'])) $this->DeleteFeed($this->_request['erase']);
51                if(isset($this->_request['ch_dir'])) $this->changeDir($this->_request['down_dir']);
52                if(isset($this->_request['view'])) $this->viewFeed($this->_request['view']);
53               
54                if($this->view_feed === false)
55                {
56                        $this->setFeeds();
57                        $this->fetchFeeds();
58                /*if(isset($this->_request['stop'])) $this->stop($this->_request['stop']);
59                if(isset($this->_request['erase'])) $this->erase($this->_request['erase']);
60                if(isset($this->_request['ch_dw'])) $this->setDownLimit($this->_request['down_rate']);
61                if(isset($this->_request['ch_up'])) $this->setUploadLimit($this->_request['up_rate']);*/       
62                }
63        }
64        private function viewFeed($id)
65        {
66                $this->view_feed = $id;
67                $sql = "SELECT url FROM feeds where user = " . $this->getIdUser() . " AND id = '$id';";
68                $res = $this->_db->query( $sql );
69                $result = $res->fetchAll();
70               
71                $this->feeds = new SimplePie();
72                $this->feeds->set_feed_url($result[0]['url']);
73                $this->feeds->set_cache_location(DIR_TPL_COMPILE);
74                $this->feeds->init();
75                $this->feeds->handle_content_type();
76               
77                /*$this->info['title'] = $this->feeds->get_title();
78        $this->info['description'] = $this->feeds->get_description();
79        $this->info['items'] = $this->feeds->get_items();*/
80        }
81        private function setFeeds()
82        {       
83                $sql = "SELECT id, url FROM feeds where user = " . $this->getIdUser() . ";";
84                $res = $this->_db->query( $sql );
85                $result = $res->fetchAll();
86                $num_feeds = count($result);
87                /*$sql = "select count(*) from feeds where user = " . $this->getIdUser() . ";";
88                $num_feeds = 1 ;//count($this->feeds);*/
89                for($i = 0; $i < $num_feeds; $i++)
90                {
91                        $this->feeds[$i]['feed'] = new SimplePie();
92                        $this->feeds[$i]['feed']->set_feed_url($result[$i]['url']);
93                        $this->feeds[$i]['feed']->set_cache_location(DIR_TPL_COMPILE);
94                        $this->feeds[$i]['feed']->init();
95                        $this->feeds[$i]['feed']->handle_content_type();
96                        $this->feeds[$i]['id'] = $result[$i]['id'];
97                }
98        }
99        private function changeDir($dir)
100        {
101                $message = new xmlrpcmsg("set_directory", array(new xmlrpcval($dir , 'string')));
102                $result = $this->client->send($message);
103                if($result->errno == 0)
104                        $this->addMessage($this->_str['info_ch_dir']);
105                else
106                        $this->addMessage($this->_str['err_ch_dir']);
107        }
108        private function Download($uri)
109        {
110                $message = new xmlrpcmsg("load_start", array(new xmlrpcval($uri , 'string')));
111                $result = $this->client->send($message);
112                $this->addMessage($this->_str['down_started']);
113        }
114        public function getDownDir()
115        {
116                $message = new xmlrpcmsg("get_directory");
117                $result = $this->client->send($message);
118                return $result->val;
119        }
120        public function isView()
121        {
122                if($this->view_feed === false)
123                        return false;
124                else
125                        return true;
126        }
127        public function getFeeds()
128        {
129                return $this->info;
130        }
131        public function getFeed()
132        {
133                return $this->feeds;
134        }
135        public function getView()
136        {
137                return $this->view_feed;
138        }
139        public function getTitle($index)
140        {
141                return $this->info[$index]['title'];
142        }
143        public function getUpLimit()
144        {
145                $message = new xmlrpcmsg("get_upload_rate");
146                $result = $this->client->send($message);
147                return round($result->val/1024, 1);
148        }
149        public function getDownLimit()
150        {
151                $message = new xmlrpcmsg("get_download_rate");
152                $result = $this->client->send($message);
153                return round($result->val/1024, 1);
154        }
155        public function getName($hash)
156        {
157                return $this->torrents[$hash]['name'];
158        }
159        public function getDownRate($hash)
160        {
161                return $this->torrents[$hash]['down_rate'];
162        }
163        public function getUpRate($hash)
164        {
165                return $this->torrents[$hash]['up_rate'];
166        }
167        public function getState($hash)
168        {
169                return $this->torrents[$hash]['state'];
170        }
171        public function getConnPeers($hash)
172        {
173                return $this->torrents[$hash]['peers'];
174        }
175        public function getConnSeeds($hash)
176        {
177                return $this->torrents[$hash]['seeds'];
178        }
179        public function getTotalPeers($hash)
180        {
181                return $this->torrents[$hash]['peers_scrape'];
182        }
183        public function getTotalSeeds($hash)
184        {
185                return $this->torrents[$hash]['seeds_scrape'];
186        }
187        public function getPercent($hash)
188        {
189                return $this->torrents[$hash]['percent'];
190        }
191        public function getRatio($hash)
192        {
193                return $this->torrents[$hash]['ratio'];
194        }
195        public function isHashChecking($hash)
196        {
197                return $this->torrents[$hash]['is_hash_checking'];
198        }
199        public function getETA($hash)
200        {
201                return $this->torrents[$hash]['ETA'];
202        }
203        public function getSize($hash)
204        {
205                return $this->getCorrectUnits($this->torrents[$hash]['size_in_chunks'] * $this->torrents[$hash]['chunk_size']);
206        }
207        public function getDone($hash)
208        {
209                return $this->getCorrectUnits($this->torrents[$hash]['completed_chunks'] * $this->torrents[$hash]['chunk_size']);
210        }
211        private function getCorrectUnits($size)
212    {
213                $size_units = 'bytes';
214                if($size >= 1024)
215                {
216                $size /= 1024;
217                $size_units = 'Kb';
218                }
219                if($size >= 1024)
220                {
221            $size /= 1024;
222            $size_units = 'Mb';
223        }
224                if($size >= 1024)
225        {
226            $size /= 1024;
227            $size_units = 'Gb';
228        }
229        return round($size, 2) .  $size_units;
230
231    }
232        private function getPrivate()
233        {
234                $tt = array();
235                $sql = "select hash, user from torrents";
236                $result = $this->_db->query($sql);
237                $torr = $result->fetchAll();
238                foreach($torr as $torrent)
239                        $tt[$torrent['hash']] = $torrent['user'];
240                       
241                return $tt;
242        }
243   
244        private function fetchFeeds()
245    {
246        $num_feeds = count($this->feeds);
247        for($i = 0; $i < $num_feeds;$i++)
248        {
249                $this->info[$i]['title'] = $this->feeds[$i]['feed']->get_title();
250                $this->info[$i]['description'] = $this->feeds[$i]['feed']->get_description();
251                $this->info[$i]['id'] = $this->feeds[$i]['id'];
252                /*foreach ($this->feeds[$i]->get_items() as $key => $item)
253                {
254                        $this->info[$i]['items'][$key]['title'] = $item->get_title();
255                        $this->info[$i]['items'][$key]['description'] = $item->get_description();
256                        $this->info[$i]['items'][$key]['link'] = $item->get_link();
257                        $this->info[$i]['items'][$key]['date'] = $item->get_date("U");
258                }*/
259        }
260    }
261    /*private function formatETA($time)
262    {
263                $time = $time/1000;
264                $sec = sprintf("%02d",floor(($time/60 - floor($time/60))*60));
265                $min =  sprintf("%02d",floor(($time/3600 - floor($time/3600))*60));
266                $hour =  sprintf("%02d",floor(($time/216000 - floor($time/216000))*60));
267       
268                if($hour > 23)
269                {
270                        $days = floor($hour/24) . "d ";
271                        $hour = sprintf("%02d",floor(($hour/24 - floor($hour/24))*24));
272                } else {
273                        $days = '';
274                }
275
276                return $days . $hour.':'.$min.':'.$sec;
277    }
278    private function stop($hashes)
279    {
280        if(is_array($hashes))
281                $hashes = array_keys($hashes);
282        else
283                $hashes = array($hashes);
284       
285        foreach($hashes as $hash)
286        {
287                $message = new xmlrpcmsg("d.stop", array(new xmlrpcval($hash, 'string')));
288                        $result = $this->client->send($message);
289        }
290       
291        $this->addMessage($this->_str['info_tor_stop']);
292    }
293    private function start($hashes)
294    {
295        if(is_array($hashes))
296                $hashes = array_keys($hashes);
297        else
298                $hashes = array($hashes);
299       
300        foreach($hashes as $hash)
301        {
302                $message = new xmlrpcmsg("d.start", array(new xmlrpcval($hash, 'string')));
303                        $result = $this->client->send($message);
304        }
305       
306        $this->addMessage($this->_str['info_tor_start']);
307    }
308    private function erase($hashes)
309    {
310        if(is_array($hashes))
311                $hashes = array_keys($hashes);
312        else
313                $hashes = array($hashes);
314       
315        foreach($hashes as $hash)
316        {
317                $message = new xmlrpcmsg("d.erase", array(new xmlrpcval($hash, 'string')));
318                        $result = $this->client->send($message);
319                        $sql = "delete from torrents where hash = '" . $hash . "'";
320                        $this->_db->query($sql);
321        }
322       
323        $this->addMessage($this->_str['info_tor_erase']);
324    }
325    private function setDownLimit($limit)
326    {
327        $message = new xmlrpcmsg("set_download_rate", array(new xmlrpcval($limit*1024, 'int')));
328                $result = $this->client->send($message);
329                //print_r($result);
330                if($result->errno == 0)
331                        $this->addMessage($this->_str['info_down_limit']);
332                else
333                        $this->addMessage($this->_str['err_down_limit']);       
334    }
335    private function setUploadLimit($limit)
336    {
337        $message = new xmlrpcmsg("set_upload_rate", array(new xmlrpcval($limit*1024, 'int')));
338                $result = $this->client->send($message);
339                if($result->errno == 0)
340                        $this->addMessage($this->_str['info_up_limit']);
341                else
342                        $this->addMessage($this->_str['err_up_limit']);
343    }*/
344    private function AddFeed($feed_url)
345    {
346        $sql = "INSERT INTO feeds(url, user) VALUES('$feed_url' , '". $this->getIdUser() . "');";
347        $this->_db->query($sql);
348        $this->addMessage($this->_str['info_add_feed']);
349    }
350    private function DeleteFeed($id)
351    {
352        $sql = "DELETE FROM feeds WHERE user = '". $this->getIdUser() . "' AND id = '$id';";
353        $this->_db->query($sql);
354        $this->addMessage($this->_str['info_erase_feed']);
355    }
356}
357?>
Note: See TracBrowser for help on using the repository browser.