| 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 | // Units for getCorrectUnits |
|---|
| 23 | // not the "correct" units, however this is what users are used to. |
|---|
| 24 | private static $UNITS = array(array(0, 'b'), array(1, 'kb'), array(2, 'MB'), array(2, 'GB'), array(3,'TB'), array(3, 'PT')); |
|---|
| 25 | |
|---|
| 26 | // Sorting stuff (there must be corresponding compare$name$order functions) |
|---|
| 27 | // Possible sort orders |
|---|
| 28 | protected static $SORT_ORDERS = array('asc', 'desc'); |
|---|
| 29 | // Possible sort keys |
|---|
| 30 | protected static $SORT_KEYS = array('name', 'dl', 'up', 'done', 'size', 'percent', 'ratio'); |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | protected $client; |
|---|
| 34 | protected $multicall; |
|---|
| 35 | protected $torrents; |
|---|
| 36 | protected $data; |
|---|
| 37 | protected $rtorrent_view; |
|---|
| 38 | |
|---|
| 39 | private $menu = array( |
|---|
| 40 | 'Main' => 'ListT', |
|---|
| 41 | 'Add Torrent' => 'AddT', |
|---|
| 42 | 'Feeds' => 'Feeds', |
|---|
| 43 | 'Cookie' => 'Cookie'); |
|---|
| 44 | private $menu_admin = array('Main' => 'ListT', |
|---|
| 45 | 'Add Torrent' => 'AddT', |
|---|
| 46 | 'Feeds' => 'Feeds', |
|---|
| 47 | 'Cookie' => 'Cookie', |
|---|
| 48 | 'Admin' => 'Admin'); |
|---|
| 49 | private $submenu = array('General' => 'General&tpl=details', |
|---|
| 50 | 'Files' => 'Files&tpl=details', |
|---|
| 51 | 'Tracker' => 'Tracker&tpl=details'); |
|---|
| 52 | protected $admin; |
|---|
| 53 | |
|---|
| 54 | public function __construct( ) |
|---|
| 55 | { |
|---|
| 56 | parent::__construct( ); |
|---|
| 57 | if(isset($this->_request['logout'])) $this->logout(); |
|---|
| 58 | if(isset($this->_request['user_login'])) $this->login($this->_request['userf'], $this->_request['passwdf']); |
|---|
| 59 | } |
|---|
| 60 | private function checkError($result) |
|---|
| 61 | { |
|---|
| 62 | if($result->errno == '0') |
|---|
| 63 | return true; |
|---|
| 64 | else |
|---|
| 65 | return false; |
|---|
| 66 | } |
|---|
| 67 | protected function construct( ){} |
|---|
| 68 | |
|---|
| 69 | public function registrado( ) |
|---|
| 70 | { |
|---|
| 71 | return !is_null( $this->_sesion->id_user ); |
|---|
| 72 | } |
|---|
| 73 | public function compLogin($user, $passwd) |
|---|
| 74 | { |
|---|
| 75 | $passwd = md5($passwd); |
|---|
| 76 | $sql = "select id from tor_passwd where user = '$user' and passwd = '$passwd'"; |
|---|
| 77 | $result = $this->_db->query( $sql ); |
|---|
| 78 | if(is_object($result)) |
|---|
| 79 | { |
|---|
| 80 | $num = $result->numRows(); |
|---|
| 81 | |
|---|
| 82 | if($num > 0) |
|---|
| 83 | $return = $result->current(); |
|---|
| 84 | else |
|---|
| 85 | $return = false; |
|---|
| 86 | } else { |
|---|
| 87 | $return = false; |
|---|
| 88 | } |
|---|
| 89 | return $return; |
|---|
| 90 | } |
|---|
| 91 | public function getUser( ) |
|---|
| 92 | { |
|---|
| 93 | return $this->_sesion->user; |
|---|
| 94 | } |
|---|
| 95 | public function getIdUser( ) |
|---|
| 96 | { |
|---|
| 97 | return $this->_sesion->id_user; |
|---|
| 98 | } |
|---|
| 99 | public function getMenu() |
|---|
| 100 | { |
|---|
| 101 | if($this->_sesion->admin === true) |
|---|
| 102 | $return = $this->menu_admin; |
|---|
| 103 | else |
|---|
| 104 | $return = $this->menu; |
|---|
| 105 | |
|---|
| 106 | return $return; |
|---|
| 107 | } |
|---|
| 108 | public function getDetailsMenu() |
|---|
| 109 | { |
|---|
| 110 | return $this->submenu; |
|---|
| 111 | } |
|---|
| 112 | public function getWidth($total, $menu_items) |
|---|
| 113 | { |
|---|
| 114 | return floor($total/(count($menu_items)+1)) - 1; |
|---|
| 115 | } |
|---|
| 116 | public function Admin($id) |
|---|
| 117 | { |
|---|
| 118 | $sql = "select admin from tor_passwd where id = '" . $this->_sesion->id_user . "'"; |
|---|
| 119 | $result = $this->_db->query( $sql ); |
|---|
| 120 | $admin = $result->current(); |
|---|
| 121 | if($admin['admin'] == 1) |
|---|
| 122 | $return = true; |
|---|
| 123 | else |
|---|
| 124 | $return = false; |
|---|
| 125 | |
|---|
| 126 | return $return; |
|---|
| 127 | } |
|---|
| 128 | public function login( $user, $password ) |
|---|
| 129 | { |
|---|
| 130 | $id = $this->compLogin($user, $password); |
|---|
| 131 | if($id != false) |
|---|
| 132 | { |
|---|
| 133 | $this->_sesion->id_user = $id['id']; |
|---|
| 134 | $this->_sesion->user = $user; |
|---|
| 135 | $this->_sesion->admin = $this->Admin($id['id']); |
|---|
| 136 | $return = true; |
|---|
| 137 | } else { |
|---|
| 138 | $return = false; |
|---|
| 139 | $this->addMessage($this->_str['err_login']); |
|---|
| 140 | } |
|---|
| 141 | return $return; |
|---|
| 142 | } |
|---|
| 143 | public function isAdmin() |
|---|
| 144 | { |
|---|
| 145 | return $this->_sesion->admin; |
|---|
| 146 | } |
|---|
| 147 | public function logout( ) |
|---|
| 148 | { |
|---|
| 149 | if($this->registrado()) |
|---|
| 150 | { |
|---|
| 151 | $this->_sesion = null; |
|---|
| 152 | $this->addMessage($this->_str['info_logout']); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | private function getPrivate() |
|---|
| 156 | { |
|---|
| 157 | $tt = array(); |
|---|
| 158 | $sql = "select hash, user from torrents"; |
|---|
| 159 | $result = $this->_db->query($sql); |
|---|
| 160 | $torr = $result->fetchAll(); |
|---|
| 161 | foreach($torr as $torrent) |
|---|
| 162 | $tt[$torrent['hash']] = $torrent['user']; |
|---|
| 163 | |
|---|
| 164 | return $tt; |
|---|
| 165 | } |
|---|
| 166 | public function setClient() |
|---|
| 167 | { |
|---|
| 168 | $this->client = new xmlrpc_client(RT_DIR, RT_HOST, RT_PORT); |
|---|
| 169 | $this->multicall = new multicall($this->client, $this->data); |
|---|
| 170 | |
|---|
| 171 | if(RT_AUTH) |
|---|
| 172 | $this->client->setCredentials(RT_USER, RT_PASSWD); |
|---|
| 173 | |
|---|
| 174 | $this->client->return_type = 'phpvals'; |
|---|
| 175 | $this->client->no_multicall = NO_MULTICALL; |
|---|
| 176 | |
|---|
| 177 | $message = new xmlrpcmsg("system.pid"); |
|---|
| 178 | $result = $this->client->send($message); |
|---|
| 179 | |
|---|
| 180 | if($result->errno != 0) |
|---|
| 181 | { |
|---|
| 182 | $this->addMessage($this->_str['err_conn']); |
|---|
| 183 | return false; |
|---|
| 184 | } else { |
|---|
| 185 | $this->setTorrents(); |
|---|
| 186 | return true; |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | /* Initializes torrent objects */ |
|---|
| 190 | protected function setTorrents() { |
|---|
| 191 | $message = new xmlrpcmsg("d.multicall", array(new xmlrpcval($this->rtorrent_view, 'string'),new xmlrpcval('d.get_hash=', 'string'))); |
|---|
| 192 | $result = $this->client->send($message); |
|---|
| 193 | if(is_array($result->val)) |
|---|
| 194 | { |
|---|
| 195 | foreach($result->val as $hash) |
|---|
| 196 | { |
|---|
| 197 | $this->torrents[$hash[0]] = new torrent($hash[0], $this->client, $this->multicall, $this->data[$hash[0]]); |
|---|
| 198 | } |
|---|
| 199 | /* Mark torrents as public/private and assign owners */ |
|---|
| 200 | $private_torrents = $this->getPrivate(); |
|---|
| 201 | $hashes = $this->getHashes(); |
|---|
| 202 | if(!empty($hashes)) |
|---|
| 203 | { |
|---|
| 204 | foreach($hashes as $hash) |
|---|
| 205 | { |
|---|
| 206 | if(array_key_exists($hash, $private_torrents)) |
|---|
| 207 | { |
|---|
| 208 | $this->data[$hash]['private'] = true; |
|---|
| 209 | $this->data[$hash]['owner'] = $private_torrents[$hash]; |
|---|
| 210 | } else { |
|---|
| 211 | $this->data[$hash]['private'] = false; |
|---|
| 212 | $this->data[$hash]['owner'] = 0; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | protected function sortTorrentsBy($key, $order) |
|---|
| 219 | { |
|---|
| 220 | if (!in_array($order, self::$SORT_ORDERS)) |
|---|
| 221 | { |
|---|
| 222 | $order = self::$SORT_ORDERS[0]; |
|---|
| 223 | } |
|---|
| 224 | if (!in_array($key, self::$SORT_KEYS)) |
|---|
| 225 | { |
|---|
| 226 | $key = self::$SORT_KEYS[0]; |
|---|
| 227 | } |
|---|
| 228 | uasort( |
|---|
| 229 | $this->torrents, |
|---|
| 230 | array( |
|---|
| 231 | self, |
|---|
| 232 | sprintf( |
|---|
| 233 | 'compare%s%s', |
|---|
| 234 | ucfirst($key), |
|---|
| 235 | ucfirst($order) |
|---|
| 236 | ) |
|---|
| 237 | ) |
|---|
| 238 | ); |
|---|
| 239 | } |
|---|
| 240 | protected function getHashes() |
|---|
| 241 | { |
|---|
| 242 | if(!empty($this->torrents)) |
|---|
| 243 | { |
|---|
| 244 | $return = array_keys($this->torrents); |
|---|
| 245 | } |
|---|
| 246 | return $return; |
|---|
| 247 | } |
|---|
| 248 | protected function erase_db($hash) |
|---|
| 249 | { |
|---|
| 250 | $sql = "delete from torrents where hash = '" . $hash . "'"; |
|---|
| 251 | $this->_db->query($sql); |
|---|
| 252 | } |
|---|
| 253 | protected function setPerm() |
|---|
| 254 | { |
|---|
| 255 | $this->admin = false; |
|---|
| 256 | } |
|---|
| 257 | /* xmlrpc rtorrent admin functions (not torrent related ) */ |
|---|
| 258 | protected function set_download_rate($limit) |
|---|
| 259 | { |
|---|
| 260 | $message = new xmlrpcmsg("set_download_rate", array(new xmlrpcval($limit, 'int'))); |
|---|
| 261 | $result = $this->client->send($message); |
|---|
| 262 | |
|---|
| 263 | if($this->checkError($result)) |
|---|
| 264 | { |
|---|
| 265 | $return = true; |
|---|
| 266 | } else { |
|---|
| 267 | $return = false; |
|---|
| 268 | } |
|---|
| 269 | return $return; |
|---|
| 270 | } |
|---|
| 271 | protected function set_upload_rate($limit) |
|---|
| 272 | { |
|---|
| 273 | $message = new xmlrpcmsg("set_download_rate", array(new xmlrpcval($limit, 'int'))); |
|---|
| 274 | $result = $this->client->send($message); |
|---|
| 275 | |
|---|
| 276 | if($this->checkError($result)) |
|---|
| 277 | { |
|---|
| 278 | $return = true; |
|---|
| 279 | } else { |
|---|
| 280 | $return = false; |
|---|
| 281 | } |
|---|
| 282 | return $return; |
|---|
| 283 | } |
|---|
| 284 | /* User Interface methods */ |
|---|
| 285 | private function get_info_rtorrent($method, $multicall, $update) |
|---|
| 286 | { |
|---|
| 287 | if($update || !isset($this->data[$method])) |
|---|
| 288 | { |
|---|
| 289 | $message = new xmlrpcmsg($method); |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | if(isset($message)) |
|---|
| 293 | { |
|---|
| 294 | if($multicall === true) |
|---|
| 295 | { |
|---|
| 296 | $return = $this->multicall->add($message, $this->hash); |
|---|
| 297 | } |
|---|
| 298 | else |
|---|
| 299 | { |
|---|
| 300 | $result = $this->client->send($message); |
|---|
| 301 | $return = $this->checkError($result); |
|---|
| 302 | if($return) |
|---|
| 303 | { |
|---|
| 304 | $this->data[$method] = $result->val; |
|---|
| 305 | $return = $result->val; |
|---|
| 306 | } |
|---|
| 307 | } |
|---|
| 308 | } else { |
|---|
| 309 | $return = $this->data[$method]; |
|---|
| 310 | } |
|---|
| 311 | return $return; |
|---|
| 312 | } |
|---|
| 313 | public function view_list($multicall = false, $update = false) |
|---|
| 314 | { |
|---|
| 315 | return $this->get_info_rtorrent('view_list', $multicall, $update); |
|---|
| 316 | } |
|---|
| 317 | /* Upload/Download rate functions */ |
|---|
| 318 | public function get_down_rate($multicall = false, $update = false) |
|---|
| 319 | { |
|---|
| 320 | return $this->get_info_rtorrent('get_down_rate', $multicall, $update); |
|---|
| 321 | } |
|---|
| 322 | public function get_up_rate($multicall = false, $update = false) |
|---|
| 323 | { |
|---|
| 324 | return $this->get_info_rtorrent('get_up_rate', $multicall, $update); |
|---|
| 325 | } |
|---|
| 326 | /* rTorrent info functions */ |
|---|
| 327 | public function getDownload() |
|---|
| 328 | { |
|---|
| 329 | return round($this->get_down_rate()/1024,2) . 'KB/s'; |
|---|
| 330 | } |
|---|
| 331 | public function getUpload() |
|---|
| 332 | { |
|---|
| 333 | return round($this->get_up_rate()/1024,2) . 'KB/s'; |
|---|
| 334 | } |
|---|
| 335 | /* Disk related functions */ |
|---|
| 336 | public function getFreeSpace() |
|---|
| 337 | { |
|---|
| 338 | return $this->getCorrectUnits(disk_free_space(DIR_DOWNLOAD)); |
|---|
| 339 | } |
|---|
| 340 | public function getUsedSpace() |
|---|
| 341 | { |
|---|
| 342 | return $this->getCorrectUnits(disk_total_space(DIR_DOWNLOAD) - disk_free_space(DIR_DOWNLOAD)); |
|---|
| 343 | } |
|---|
| 344 | public function getTotalSpace() |
|---|
| 345 | { |
|---|
| 346 | return $this->getCorrectUnits(disk_total_space(DIR_DOWNLOAD)); |
|---|
| 347 | } |
|---|
| 348 | public function getUsedPercent() |
|---|
| 349 | { |
|---|
| 350 | return round((disk_total_space(DIR_DOWNLOAD) - disk_free_space(DIR_DOWNLOAD))/disk_total_space(DIR_DOWNLOAD)*100,0); |
|---|
| 351 | } |
|---|
| 352 | public function getCorrectUnits($size) |
|---|
| 353 | { |
|---|
| 354 | $i; |
|---|
| 355 | $e = sizeof(self::$UNITS); |
|---|
| 356 | for ($i = 0; $i < $e && $size > 900; ++$i) |
|---|
| 357 | { |
|---|
| 358 | $size = $size / 1024; |
|---|
| 359 | } |
|---|
| 360 | return sprintf('%.'.(self::$UNITS[$i][0]).'f %s', $size, self::$UNITS[$i][1]); |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | // Sort functions |
|---|
| 364 | static function compareNameAsc($a, $b) |
|---|
| 365 | { |
|---|
| 366 | return strnatcasecmp($a->get_name(), $b->get_name()); |
|---|
| 367 | } |
|---|
| 368 | static function compareNameDesc($a, $b) |
|---|
| 369 | { |
|---|
| 370 | return strnatcasecmp($b->get_name(), $a->get_name()); |
|---|
| 371 | } |
|---|
| 372 | static function compareDlAsc($a, $b) |
|---|
| 373 | { |
|---|
| 374 | return $a->get_down_rate() - $b->get_down_rate(); |
|---|
| 375 | } |
|---|
| 376 | static function compareDlDesc($a, $b) |
|---|
| 377 | { |
|---|
| 378 | return $b->get_down_rate() - $a->get_down_rate(); |
|---|
| 379 | } |
|---|
| 380 | static function compareUpAsc($a, $b) |
|---|
| 381 | { |
|---|
| 382 | return $a->get_up_rate() - $b->get_up_rate(); |
|---|
| 383 | } |
|---|
| 384 | static function compareUpDesc($a, $b) |
|---|
| 385 | { |
|---|
| 386 | return $b->get_up_rate() - $a->get_up_rate(); |
|---|
| 387 | } |
|---|
| 388 | static function compareDoneAsc($a, $b) |
|---|
| 389 | { |
|---|
| 390 | $an = $a->get_completed_chunks() * $a->get_chunk_size(); |
|---|
| 391 | $bn = $b->get_completed_chunks() * $b->get_chunk_size(); |
|---|
| 392 | |
|---|
| 393 | return $an - $bn; |
|---|
| 394 | } |
|---|
| 395 | static function compareDoneDesc($a, $b) |
|---|
| 396 | { |
|---|
| 397 | $an = $a->get_completed_chunks() * $a->get_chunk_size(); |
|---|
| 398 | $bn = $b->get_completed_chunks() * $b->get_chunk_size(); |
|---|
| 399 | |
|---|
| 400 | return $bn - $an; |
|---|
| 401 | } |
|---|
| 402 | static function compareSizeAsc($a, $b) |
|---|
| 403 | { |
|---|
| 404 | // possible int overflows here. |
|---|
| 405 | // hence the complicated calculation here ;) |
|---|
| 406 | $r = floatval($a->get_chunk_size()) / floatval($b->get_chunk_size()); |
|---|
| 407 | $an = $a->get_size_chunks() * $r; |
|---|
| 408 | $bn = $b->get_size_chunks(); |
|---|
| 409 | |
|---|
| 410 | // need to care that the result may be within [-1, 1] |
|---|
| 411 | $rv = ($an - $bn); |
|---|
| 412 | return $rv == 0 ? 0 : ($rv < 0 ? -1 : 1); |
|---|
| 413 | } |
|---|
| 414 | static function compareSizeDesc($a, $b) |
|---|
| 415 | { |
|---|
| 416 | $r = floatval($a->get_chunk_size()) / floatval($b->get_chunk_size()); |
|---|
| 417 | $an = $a->get_size_chunks() * $r; |
|---|
| 418 | $bn = $b->get_size_chunks(); |
|---|
| 419 | |
|---|
| 420 | $rv = ($bn - $an); |
|---|
| 421 | return $rv == 0 ? 0 : ($rv < 0 ? -1 : 1); |
|---|
| 422 | } |
|---|
| 423 | static function comparePercentAsc($a, $b) |
|---|
| 424 | { |
|---|
| 425 | $an = $a->get_completed_chunks() / $a->get_size_chunks(); |
|---|
| 426 | $bn = $b->get_completed_chunks() / $b->get_size_chunks(); |
|---|
| 427 | // Operating on raw percentages, hence scale by 100 or else we will be within (-1,1) and |
|---|
| 428 | // sort() doesn't like values |x| < 1 |
|---|
| 429 | return ($an - $bn) * 100; |
|---|
| 430 | } |
|---|
| 431 | static function comparePercentDesc($a, $b) |
|---|
| 432 | { |
|---|
| 433 | $an = $a->get_completed_chunks() / $a->get_size_chunks(); |
|---|
| 434 | $bn = $b->get_completed_chunks() / $b->get_size_chunks(); |
|---|
| 435 | return ($bn - $an) * 100; |
|---|
| 436 | } |
|---|
| 437 | static function compareRatioAsc($a, $b) |
|---|
| 438 | { |
|---|
| 439 | return $a->get_ratio() - $b->get_ratio(); |
|---|
| 440 | } |
|---|
| 441 | static function compareRatioDesc($a, $b) |
|---|
| 442 | { |
|---|
| 443 | return $b->get_ratio() - $a->get_ratio(); |
|---|
| 444 | } |
|---|
| 445 | } |
|---|
| 446 | ?> |
|---|