source: trunk/wtorrent/cls/install.cls.php @ 93

Revision 93, 6.7 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
18Created by Roger Pau Monné
19*/
20class install extends Web
21{
22        protected $client;
23        protected $data;
24        protected $save;
25        private static $options = array(
26                'language' => 'string',
27                'db_file' => 'string',
28                'rt_host' => 'string',
29                'rt_port' => 'integer',
30                'rt_dir' => 'string',
31                'rt_auth' => 'boolean',
32                'rt_user' => 'string',
33                'rt_passwd' => 'string',
34                'no_multicall' => 'boolean',
35                'effects' => 'boolean',
36                'dir_torrents' => 'string',
37                'dir_exec' => 'string',
38                'dir_download' => 'string',
39                'user' => 'string',
40                'passwd' => 'string'
41        );
42        private $defaults = array(
43                'language' => 'en',
44                'db_file' => 'db/database.db',
45                'rt_host' => 'localhost',
46                'rt_port' => 80,
47                'rt_dir' => '/RPC2',
48                'rt_auth' => false,
49                'rt_user' => '',
50                'rt_passwd' => '',
51                'no_multicall' => true,
52                'effects' => true,
53                'dir_torrents' => 'torrents/',
54                'dir_exec' => '',
55                'dir_download' => '/data',
56                'user' => '',
57                'passwd' => ''
58                );
59        protected $user_options;
60
61        public function __construct( )
62        {
63                parent::__construct(true);
64                $this->save = false;
65                if(isset($this->_request))
66                {
67                        if(array_key_exists('try', $this->_request))
68                        {
69                                $this->tryConfig($this->createOptionsArray());
70                        }
71                        if(array_key_exists('save', $this->_request))
72                        {
73                                if(!empty($this->_request['user']) && !empty($this->_request['passwd']))
74                                {
75                                        $this->saveConfig($this->createOptionsArray());
76                                } else {
77                                        $this->addMessage($this->_str['missing_user']);
78                                }
79                        }
80                }
81                $this->defaults['dir_exec'] = DIR_EXEC;
82        }
83        /* Create an array from static $options and request data */
84        private function createOptionsArray()
85        {
86                foreach(self::$options as $option => $type)
87                {
88                        if($type == 'boolean')
89                        {
90                                if($this->_request[$option] == 1)
91                                {
92                                        $options[$option] = true;
93                                } else {
94                                        $options[$option] = false;
95                                }
96                        } else {
97                                $options[$option] = $this->_request[$option];
98                                settype($options[$option], $type);
99                        }
100                }
101                $this->user_options = $options;
102                return $options;
103        }
104        /* try the config before giving the option to save it */
105        private function tryConfig($options)
106        {
107                if(!is_writable('conf/user.conf.php'))
108                {
109                        $this->addMessage($this->str['config_install_err']);
110                }
111                if(!is_readable(DIR_LANG . $options['language'] . '.txt'))
112                {
113                        $this->addMessage($this->_str['language_err']);
114                }
115                $path = explode('/', $options['db_file']);
116                $num_folders = count($path) - 1;
117                for($i=0;$i<$num_folders;$i++)
118                {
119                        $folder .= $path[$i];
120                }
121                if(empty($folder))
122                {
123                        $folder = '.';
124                }
125                if(!is_writable($folder))
126                {
127                        $this->addMessage($this->_str['db_file_err']);
128                }
129                if(!$this->tryClient($options['rt_dir'], $options['rt_host'], $options['rt_port'], $options['rt_auth'], $options['rt_user'], $options['rt_passwd']))
130                {
131                        $this->addMessage($this->_str['rt_install_err']);
132                }
133                if(!is_writable($options['dir_torrents']))
134                {
135                        $this->addMessage($this->_str['dir_torrents_err']);
136                }
137                if(!file_exists($options['dir_exec']))
138                {
139                        $this->addMessage($this->_str['dir_exec_err']);
140                }
141                if(!$this->getMessages())
142                {
143                        $this->save = true;
144                        $this->addMessage($this->_str['try_ok']);
145                }
146        }
147        private function saveConfig($options)
148        {
149                $this->save = true;
150                $header = "<?php\n/* wTorrent autoconfiguration file. Created " . date('j/n/Y') . " */\n";
151                $fh = fopen("conf/user.conf.php", "w");
152                fwrite($fh, $header);
153                foreach($options as $define => $option)
154                {
155                        if($define != 'user' && $define != 'passwd')
156                        {
157                                $line = "define ('" . strtoupper($define) . "', ";
158                                if(is_bool($option) || is_numeric($option))
159                                {
160                                        $temp = $option;
161                                        if(is_bool($temp))
162                                        {
163                                                if($temp)
164                                                {
165                                                        $temp = 'true';
166                                                } else {
167                                                        $temp = 'false';
168                                                }
169                                        }
170                                        $line .= $temp . ");";
171                                }
172                                if(is_string($option))
173                                {
174                                        $line .= "'" . $option . "');";
175                                }
176                                $line .= "\n";
177                                fwrite($fh, $line);
178                        }
179                }
180                $footer = "?>\n";
181                fwrite($fh, $footer);
182                fclose($fh);
183                if (!defined('DB_DSN') && defined('DB_FILE'))
184                {
185                        define('DB_DSN', 'sqlite:' . DIR_EXEC . DB_FILE);
186                }
187                $db = new PDOe('sqlite:' . $options['db_file'], null, null, array(PDO::ERRMODE_EXCEPTION));
188                if(is_object($db))
189                {
190                        $db->modify('CREATE TABLE tor_passwd(id integer primary key, user text, passwd text, admin integer, dir text, force_dir integer)');
191                        $db->modify('INSERT INTO tor_passwd VALUES(1, ?, ?, 1, \'\', 0)', $options['user'], md5($options['passwd']));
192                        $db->modify('CREATE TABLE torrents(hash string, user int, private int)');
193                        $db->modify('CREATE TABLE feeds(id integer primary key, url text, user integer)');
194                        $db->modify('CREATE TABLE cookie(id integer primary key, userid integer, value text, hostname text)');
195                } else {
196                        $this->addMessage($this->_str['db_create_err']);
197                        return;
198                }
199                $this->addMessage($this->_str['create_config_ok']);
200        }
201        /* Return contents of language dir */
202        public function getLanguages()
203        {
204                $dh = @opendir(DIR_LANG) or die('Cannot open language path');
205                while($file = readdir($dh))
206                {
207                        $lang = explode('.', $file);
208                        if($lang[1] == 'txt')
209                        {
210                                $languages[] = $lang[0];
211                        }
212                }
213                return $languages;
214        }
215        public function getOption($option)
216        {
217                if(isset($this->user_options[$option]))
218                {
219                        return $this->user_options[$option];
220                } else {
221                        return $this->defaults[$option];
222                }
223        }
224        /* xmlrpc basic testing functions */
225        private function checkError($result)
226        {
227                if($result->errno == '0')
228                        return true;
229                else
230                        return false;
231        }
232        private function tryClient($rt_dir, $rt_host, $rt_port, $rt_auth, $rt_user, $rt_passwd)
233        {
234                $this->client = new xmlrpc_client($rt_dir, $rt_host, $rt_port);
235
236                if($rt_auth)
237                        $this->client->setCredentials($rt_user, $rt_passwd);
238   
239                $this->client->return_type = 'phpvals';
240    $this->client->no_multicall = $no_multicall;
241       
242    $message = new xmlrpcmsg("system.pid");
243                $result = $this->client->send($message);
244
245                return $this->checkError($result);
246        }
247        /* Basic defines for wTorrent to work that are in user.conf.php */
248        final public static function setDefines()
249        {
250                define( 'DIR_EXEC',                     str_replace( 'install.php' , '' , $_SERVER['SCRIPT_FILENAME'] ));
251        }
252        /* Void methods */
253        public function construct()
254        {
255               
256        }
257        public function isRegistered()
258        {
259                return false;
260        }
261}
262?>
Note: See TracBrowser for help on using the repository browser.