source: trunk/wtorrent/wt/cls/AddT.cls.php @ 93

Revision 93, 6.3 KB checked in by royger, 2 years ago (diff)
  • Migrated database from sqlite 2 to sqlite 3.
  • From now on, you will need php to be compiled with PDO and sqlite to run wTorrent.
  • Applied patch from tn123 to migrate to PDO and use prepared statements (ticket #188).
  • Created new install script.
  • To apply this update you will need to run sqlite_migration.sh to migrate your old database to sqlite3. This script assumes you have your database in db/database.db, if that's not the case edit the script and change the path.
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 AddT extends rtorrent
19{
20
21        public function construct()
22        {
23                if(!$this->setClient())
24                {
25                        return false;
26                }
27
28                if (!empty($this->_files['uploadedfile']) && !$this->_files['uploadedfile']['error'])
29                {
30                        $this->uploadTorrent(
31                                $this->_files['uploadedfile'],
32                                $this->_request['download_dir'],
33                                $this->_request['start_now'],
34                                $this->_request['private']
35                        );
36                }
37                else if(!empty($this->_request['torrenturl']))
38                { 
39                        $this->addRemoteTorrent(
40                                $this->_request['torrenturl'],
41                                $this->_request['download_dir'],
42                                $this->_request['start_now'],
43                                $this->_request['private']
44                        );
45                }       
46        }
47        // Add remote torrent
48        private function addRemoteTorrent( $url, $dir, $start_now, $private ) 
49        {
50                // Get Dir if user can only download to a certain directory
51                if($this->getForceDir() == 1)
52                {
53                        $dir = $this->getDir();
54                }
55
56                // Parsing url
57                $purl = parse_url($url);
58                $uploadfile = DIR_EXEC . DIR_TORRENTS . sha1( $url ) . md5($url) . ".torrent";
59                // Get sha1/md5 for avoid filename problems & Multiple torrents
60                if (file_exists($uploadfile))
61                { 
62                        $this->addMessage($this->_str['err_add_file']);
63                        return false;
64                }
65                $fh = fopen($uploadfile, 'w');
66                // Open a filehandle and check for curl function in php
67                if (!function_exists("curl_init"))
68                {
69                        $this->addMessage( $this->_str['no_curl_function'] );
70                        return;
71                }
72                $ua = curl_init();
73                $cookie = $this->getCookie( $url );
74                if (!empty($cookie))
75                {
76                        curl_setopt($ua, CURLOPT_COOKIE, $cookie);
77                }
78                curl_setopt($ua, CURLOPT_PORT,                          $purl["port"] );
79                curl_setopt($ua, CURLOPT_URL,                           $url);
80                curl_setopt($ua, CURLOPT_VERBOSE,                       FALSE);
81                curl_setopt($ua, CURLOPT_HEADER,                        FALSE);
82                // Dont put the header into the file
83                curl_setopt($ua, CURLOPT_USERAGENT,                     "Mozilla/5.0 (U; en-US; rv) Gecko Firefox (compatible wtorrent)");
84                // Avoid problems with user agent sniffing
85                curl_setopt($ua, CURLOPT_RETURNTRANSFER,        TRUE);
86                curl_setopt($ua, CURLOPT_SSL_VERIFYHOST,        FALSE);
87                curl_setopt($ua, CURLOPT_SSL_VERIFYPEER,        FALSE);
88                // Avoid ssl problems
89                curl_setopt($ua, CURLOPT_FOLLOWLOCATION,        TRUE);
90                // Follow the location
91                curl_setopt($ua, CURLOPT_AUTOREFERER,           TRUE);
92                curl_setopt($ua, CURLOPT_REFERER,                       $url);
93                // Avoid referrer problems
94                curl_setopt($ua, CURLOPT_FILE,                           $fh);
95                $file = curl_exec( $ua );
96                // Execute the query
97                curl_close($ua);
98                fclose($fh);
99                chmod( $uploadfile, PERM_TORRENTS);
100                // Setting up the permissions
101                $torrent = new BDECODE($uploadfile);
102                // Try to load the torrent, and check is it valid or not
103                if ($torrent->result['error'])
104                {
105                        $this->addMessage($torrent->result['error']);
106                        @unlink($uploadfile);
107                        return false;
108                }
109                $message = new xmlrpcmsg("set_directory", array(new xmlrpcval($dir , 'string')));
110                $result1 = $this->client->send($message);
111                if($start_now == 'on')
112                {
113                        $method = 'load_start';
114                }
115                else
116                {
117                        $method = 'load';
118                }
119                if ($private == 'on')
120                { 
121                        $bencode = new BEncodeLib();
122                        $hash = strtoupper(bin2hex(sha1($bencode->bencode($torrent->result['info']), true)));
123                        $this->_db->modify('INSERT INTO torrents VALUES(?, ?, 1)', $hash, $this->getIdUser());
124                }
125
126                $message = new xmlrpcmsg($method, array(new xmlrpcval($uploadfile , 'string')));
127                $result2 = $this->client->send($message);
128
129
130                if (($result1->errno == 0) && ($result2->errno == 0) && ($res[0] !== false))
131                {
132                        $this->addMessage($this->_str['info_add_torrent']);
133                }
134                else
135                { 
136                        $this->addMessage($this->_str['err_add_torrent']);
137                        @unlink($uploadfile);
138                }
139                $message = new xmlrpcmsg("set_directory", array(new xmlrpcval(DIR_DOWNLOAD, 'string')));
140                $this->client->send($message);
141        }
142        private function getCookie($url) 
143        {
144                // Getting cookie depends on hostname
145                $purl = parse_url( $url );
146                return implode(
147                        '; ',
148                        $this->_db->queryColumnAll(
149                                'SELECT id, value, hostname FROM cookie WHERE userid = ? AND hostname LIKE ?',
150                                $this->getIdUser(),
151                                "%{$purl['host']}%"
152                        )
153                );
154        } 
155        public function getDir()
156        {
157                return $this->_db->queryColumn('SELECT dir FROM tor_passwd WHERE id = ?', $this->getIdUser());
158        }
159        public function getForceDir()
160        {
161                return $this->_db->queryColumn('SELECT force_dir FROM tor_passwd WHERE id = ?', $this->getIdUser());
162        }
163        private function uploadTorrent($fileU, $dir, $start_now, $private)
164        {
165                if ($this->getForceDir() == 1)
166                {
167                        $dir = $this->getDir();
168                }
169                $uploadfile = DIR_EXEC . DIR_TORRENTS . time() . basename($fileU['name']);
170                if (!is_writable(DIR_EXEC . DIR_TORRENTS))
171                {
172                        $this->addMessage($this->_str['err_add_dir']);
173                        return false;
174                }
175                if(file_exists($uploadfile))
176                {
177                        $this->addMessage($this->_str['err_add_file']);
178                        return false;
179                }
180
181                $res[] = move_uploaded_file($fileU['tmp_name'], $uploadfile);
182                chmod( $uploadfile, PERM_TORRENTS);
183
184                $message = new xmlrpcmsg("set_directory", array(new xmlrpcval($dir , 'string')));
185                $result1 = $this->client->send($message);
186
187                if($start_now == 'on')
188                {
189                        $method = 'load_start';
190                }
191                else
192                {
193                        $method = 'load';
194                }
195                if($private == 'on')
196                {
197                        $torrent = new BDECODE($uploadfile);
198                        $bencode = new BEncodeLib();
199                        $hash = strtoupper(bin2hex(sha1($bencode->bencode($torrent->result['info']), true)));
200                        $this->_db->modify('INSERT INTO torrents VALUES(?, ?, 1)', $hash, $this->getIdUser());
201                }
202
203                $message = new xmlrpcmsg($method, array(new xmlrpcval($uploadfile , 'string')));
204                $result2 = $this->client->send($message);
205
206                if(($result1->errno == 0) && ($result2->errno == 0) && ($res[0] !== false))
207                        $this->addMessage($this->_str['info_add_torrent']);
208                else
209                {
210                        $this->addMessage($this->_str['err_add_torrent']);
211                        @unlink($uploadfile);
212                }
213                $message = new xmlrpcmsg("set_directory", array(new xmlrpcval(DIR_DOWNLOAD, 'string')));
214                $result1 = $this->client->send($message);
215        }
216}
217?>
Note: See TracBrowser for help on using the repository browser.