| 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 | Modified version of class done by David Marco Martinez |
|---|
| 19 | */ |
|---|
| 20 | class rtorrent extends Web |
|---|
| 21 | { |
|---|
| 22 | protected $client; |
|---|
| 23 | protected $multicall; |
|---|
| 24 | protected $torrents; |
|---|
| 25 | protected $data; |
|---|
| 26 | |
|---|
| 27 | private $menu = array( |
|---|
| 28 | 'Main' => 'ListT', |
|---|
| 29 | 'Add Torrent' => 'AddT', |
|---|
| 30 | 'Feeds' => 'Feeds', |
|---|
| 31 | 'Cookie' => 'Cookie'); |
|---|
| 32 | private $menu_admin = array('Main' => 'ListT', |
|---|
| 33 | 'Add Torrent' => 'AddT', |
|---|
| 34 | 'Feeds' => 'Feeds', |
|---|
| 35 | 'Admin' => 'Admin', |
|---|
| 36 | 'Cookie' => 'Cookie'); |
|---|
| 37 | private $submenu = array('General' => 'General&tpl=details', |
|---|
| 38 | 'Files' => 'Files&tpl=details', |
|---|
| 39 | 'Tracker' => 'Tracker&tpl=details'); |
|---|
| 40 | protected $admin; |
|---|
| 41 | /////////////////////////////////// C O N S T R U C T O R A S Y D E S T R U C T O R A /////////////////////////////////// |
|---|
| 42 | |
|---|
| 43 | public function __construct( ) |
|---|
| 44 | { |
|---|
| 45 | parent::__construct( ); |
|---|
| 46 | if(isset($this->_request['logout'])) $this->logout(); |
|---|
| 47 | if(isset($this->_request['user_login'])) $this->login($this->_request['userf'], $this->_request['passwdf']); |
|---|
| 48 | } |
|---|
| 49 | private function checkError($result) |
|---|
| 50 | { |
|---|
| 51 | if($result->errno == '0') |
|---|
| 52 | return true; |
|---|
| 53 | else |
|---|
| 54 | return false; |
|---|
| 55 | } |
|---|
| 56 | protected function construct( ){} |
|---|
| 57 | |
|---|
| 58 | public function registrado( ) |
|---|
| 59 | { |
|---|
| 60 | return !is_null( $this->_sesion->id_user ); |
|---|
| 61 | } |
|---|
| 62 | public function compLogin($user, $passwd) |
|---|
| 63 | { |
|---|
| 64 | $passwd = md5($passwd); |
|---|
| 65 | $sql = "select id from tor_passwd where user = '$user' and passwd = '$passwd'"; |
|---|
| 66 | $result = $this->_db->query( $sql ); |
|---|
| 67 | if(is_object($result)) |
|---|
| 68 | { |
|---|
| 69 | $num = $result->numRows(); |
|---|
| 70 | |
|---|
| 71 | if($num > 0) |
|---|
| 72 | $return = $result->current(); |
|---|
| 73 | else |
|---|
| 74 | $return = false; |
|---|
| 75 | } else { |
|---|
| 76 | $return = false; |
|---|
| 77 | } |
|---|
| 78 | return $return; |
|---|
| 79 | } |
|---|
| 80 | public function getUser( ) |
|---|
| 81 | { |
|---|
| 82 | return $this->_sesion->user; |
|---|
| 83 | } |
|---|
| 84 | public function getIdUser( ) |
|---|
| 85 | { |
|---|
| 86 | return $this->_sesion->id_user; |
|---|
| 87 | } |
|---|
| 88 | public function getMenu() |
|---|
| 89 | { |
|---|
| 90 | if($this->_sesion->admin === true) |
|---|
| 91 | $return = $this->menu_admin; |
|---|
| 92 | else |
|---|
| 93 | $return = $this->menu; |
|---|
| 94 | |
|---|
| 95 | return $return; |
|---|
| 96 | } |
|---|
| 97 | public function getDetailsMenu() |
|---|
| 98 | { |
|---|
| 99 | return $this->submenu; |
|---|
| 100 | } |
|---|
| 101 | public function getWidth($total, $menu_items) |
|---|
| 102 | { |
|---|
| 103 | return floor($total/(count($menu_items)+1)) - 1; |
|---|
| 104 | } |
|---|
| 105 | public function Admin($id) |
|---|
| 106 | { |
|---|
| 107 | $sql = "select admin from tor_passwd where id = '" . $this->_sesion->id_user . "'"; |
|---|
| 108 | $result = $this->_db->query( $sql ); |
|---|
| 109 | $admin = $result->current(); |
|---|
| 110 | if($admin['admin'] == 1) |
|---|
| 111 | $return = true; |
|---|
| 112 | else |
|---|
| 113 | $return = false; |
|---|
| 114 | |
|---|
| 115 | return $return; |
|---|
| 116 | } |
|---|
| 117 | public function login( $user, $password ) |
|---|
| 118 | { |
|---|
| 119 | $id = $this->compLogin($user, $password); |
|---|
| 120 | if($id != false) |
|---|
| 121 | { |
|---|
| 122 | $this->_sesion->id_user = $id['id']; |
|---|
| 123 | $this->_sesion->user = $user; |
|---|
| 124 | $this->_sesion->admin = $this->Admin($id['id']); |
|---|
| 125 | $return = true; |
|---|
| 126 | } else { |
|---|
| 127 | $return = false; |
|---|
| 128 | $this->addMessage($this->_str['err_login']); |
|---|
| 129 | } |
|---|
| 130 | return $return; |
|---|
| 131 | } |
|---|
| 132 | public function isAdmin() |
|---|
| 133 | { |
|---|
| 134 | return $this->_sesion->admin; |
|---|
| 135 | } |
|---|
| 136 | public function logout( ) |
|---|
| 137 | { |
|---|
| 138 | if($this->registrado()) |
|---|
| 139 | { |
|---|
| 140 | $this->_sesion = null; |
|---|
| 141 | $this->addMessage($this->_str['info_logout']); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | private function getPrivate() |
|---|
| 145 | { |
|---|
| 146 | $tt = array(); |
|---|
| 147 | $sql = "select hash, user from torrents"; |
|---|
| 148 | $result = $this->_db->query($sql); |
|---|
| 149 | $torr = $result->fetchAll(); |
|---|
| 150 | foreach($torr as $torrent) |
|---|
| 151 | $tt[$torrent['hash']] = $torrent['user']; |
|---|
| 152 | |
|---|
| 153 | return $tt; |
|---|
| 154 | } |
|---|
| 155 | public function setClient() |
|---|
| 156 | { |
|---|
| 157 | $this->client = new xmlrpc_client(RT_DIR, RT_HOST, RT_PORT); |
|---|
| 158 | $this->multicall = new multicall($this->client, $this->data); |
|---|
| 159 | |
|---|
| 160 | if(RT_AUTH) |
|---|
| 161 | $this->client->setCredentials(RT_USER, RT_PASSWD); |
|---|
| 162 | |
|---|
| 163 | $this->client->return_type = 'phpvals'; |
|---|
| 164 | $this->client->no_multicall = NO_MULTICALL; |
|---|
| 165 | |
|---|
| 166 | $message = new xmlrpcmsg("system.pid"); |
|---|
| 167 | $result = $this->client->send($message); |
|---|
| 168 | |
|---|
| 169 | if($result->errno != 0) |
|---|
| 170 | { |
|---|
| 171 | $this->addMessage($this->_str['err_conn']); |
|---|
| 172 | return false; |
|---|
| 173 | } else { |
|---|
| 174 | $this->setTorrents(); |
|---|
| 175 | return true; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | /* Initializes torrent objects */ |
|---|
| 179 | protected function setTorrents() { |
|---|
| 180 | $message = new xmlrpcmsg("d.multicall", array(new xmlrpcval('default', 'string'),new xmlrpcval('d.get_hash=', 'string'))); |
|---|
| 181 | $result = $this->client->send($message); |
|---|
| 182 | if(is_array($result->val)) |
|---|
| 183 | { |
|---|
| 184 | foreach($result->val as $hash) |
|---|
| 185 | { |
|---|
| 186 | $this->torrents[$hash[0]] = new torrent($hash[0], $this->client, $this->multicall, $this->data[$hash[0]]); |
|---|
| 187 | } |
|---|
| 188 | /* Mark torrents as public/private and assign owners */ |
|---|
| 189 | $private_torrents = $this->getPrivate(); |
|---|
| 190 | $hashes = $this->getHashes(); |
|---|
| 191 | foreach($hashes as $hash) |
|---|
| 192 | { |
|---|
| 193 | if(array_key_exists($hash, $private_torrents)) |
|---|
| 194 | { |
|---|
| 195 | $this->data[$hash]['private'] = true; |
|---|
| 196 | $this->data[$hash]['owner'] = $private_torrents[$hash]; |
|---|
| 197 | } else { |
|---|
| 198 | $this->data[$hash]['private'] = false; |
|---|
| 199 | $this->data[$hash]['owner'] = 0; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | } |
|---|
| 204 | protected function getHashes() |
|---|
| 205 | { |
|---|
| 206 | return array_keys($this->torrents); |
|---|
| 207 | } |
|---|
| 208 | protected function erase_db($hash) |
|---|
| 209 | { |
|---|
| 210 | $sql = "delete from torrents where hash = '" . $hash . "'"; |
|---|
| 211 | $this->_db->query($sql); |
|---|
| 212 | } |
|---|
| 213 | protected function setPerm() |
|---|
| 214 | { |
|---|
| 215 | $this->admin = false; |
|---|
| 216 | } |
|---|
| 217 | /* xmlrpc rtorrent admin functions (not torrent related ) */ |
|---|
| 218 | protected function set_download_rate($limit) |
|---|
| 219 | { |
|---|
| 220 | $message = new xmlrpcmsg("set_download_rate", array(new xmlrpcval($limit, 'int'))); |
|---|
| 221 | $result = $this->client->send($message); |
|---|
| 222 | |
|---|
| 223 | if($this->checkError($result)) |
|---|
| 224 | { |
|---|
| 225 | $return = true; |
|---|
| 226 | } else { |
|---|
| 227 | $return = false; |
|---|
| 228 | } |
|---|
| 229 | return $return; |
|---|
| 230 | } |
|---|
| 231 | protected function set_upload_rate($limit) |
|---|
| 232 | { |
|---|
| 233 | $message = new xmlrpcmsg("set_download_rate", array(new xmlrpcval($limit, 'int'))); |
|---|
| 234 | $result = $this->client->send($message); |
|---|
| 235 | |
|---|
| 236 | if($this->checkError($result)) |
|---|
| 237 | { |
|---|
| 238 | $return = true; |
|---|
| 239 | } else { |
|---|
| 240 | $return = false; |
|---|
| 241 | } |
|---|
| 242 | return $return; |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 245 | ?> |
|---|