| 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 | Created by Roger Pau Monné |
|---|
| 19 | */ |
|---|
| 20 | class torrent |
|---|
| 21 | { |
|---|
| 22 | private $hash; // saves the hash of the torrent (needed for init) |
|---|
| 23 | private $client; |
|---|
| 24 | private $multicall; |
|---|
| 25 | private $data; |
|---|
| 26 | |
|---|
| 27 | /* Save the hash, don't call any methods by default */ |
|---|
| 28 | public function __construct($hash, &$client, &$multicall, &$data) |
|---|
| 29 | { |
|---|
| 30 | $this->hash = $hash; |
|---|
| 31 | $this->client = &$client; |
|---|
| 32 | $this->multicall = &$multicall; |
|---|
| 33 | $this->data = &$data; |
|---|
| 34 | } |
|---|
| 35 | /** |
|---|
| 36 | * Check return array for errors |
|---|
| 37 | **/ |
|---|
| 38 | private function checkError($result) |
|---|
| 39 | { |
|---|
| 40 | if($result->errno == '0') |
|---|
| 41 | return true; |
|---|
| 42 | else |
|---|
| 43 | return false; |
|---|
| 44 | } |
|---|
| 45 | /** |
|---|
| 46 | * General functions to prevent duplicate code |
|---|
| 47 | */ |
|---|
| 48 | /* Command Execution (no return value) */ |
|---|
| 49 | private function command($command, $multicall) |
|---|
| 50 | { |
|---|
| 51 | $message = new xmlrpcmsg($command, array(new xmlrpcval($this->hash, 'string'))); |
|---|
| 52 | |
|---|
| 53 | if($multicall === true) |
|---|
| 54 | { |
|---|
| 55 | $return = $this->multicall->add($message, $this->hash); |
|---|
| 56 | } |
|---|
| 57 | else |
|---|
| 58 | { |
|---|
| 59 | $result = $this->client->send($message); |
|---|
| 60 | $return = $this->checkError($result); |
|---|
| 61 | } |
|---|
| 62 | return $return; |
|---|
| 63 | } |
|---|
| 64 | /* Info request function */ |
|---|
| 65 | private function get_info($method, $multicall, $update) |
|---|
| 66 | { |
|---|
| 67 | if($update || !isset($this->data[$method])) |
|---|
| 68 | { |
|---|
| 69 | $message = new xmlrpcmsg($method, array(new xmlrpcval($this->hash, 'string'))); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | if(isset($message)) |
|---|
| 73 | { |
|---|
| 74 | if($multicall === true) |
|---|
| 75 | { |
|---|
| 76 | $return = $this->multicall->add($message, $this->hash); |
|---|
| 77 | } |
|---|
| 78 | else |
|---|
| 79 | { |
|---|
| 80 | $result = $this->client->send($message); |
|---|
| 81 | $return = $this->checkError($result); |
|---|
| 82 | if($return) |
|---|
| 83 | { |
|---|
| 84 | $this->data[$method] = $result->val; |
|---|
| 85 | $return = $result->val; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } else { |
|---|
| 89 | $return = $this->data[$method]; |
|---|
| 90 | } |
|---|
| 91 | return $return; |
|---|
| 92 | } |
|---|
| 93 | /* Info request function */ |
|---|
| 94 | private function get_info_object($method, $param, $multicall, $update) |
|---|
| 95 | { |
|---|
| 96 | if($update || !isset($this->data[$method][$param])) |
|---|
| 97 | { |
|---|
| 98 | $message = new xmlrpcmsg($method, array(new xmlrpcval($this->hash, 'string'), new xmlrpcval($param, 'int'))); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | if(isset($message)) |
|---|
| 102 | { |
|---|
| 103 | if($multicall === true) |
|---|
| 104 | { |
|---|
| 105 | $return = $this->multicall->add($message, $this->hash); |
|---|
| 106 | } |
|---|
| 107 | else |
|---|
| 108 | { |
|---|
| 109 | $result = $this->client->send($message); |
|---|
| 110 | $return = $this->checkError($result); |
|---|
| 111 | if($return) |
|---|
| 112 | { |
|---|
| 113 | $this->data[$method][$param] = $result->val; |
|---|
| 114 | $return = $result->val; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | } else { |
|---|
| 118 | $return = $this->data[$method][$param]; |
|---|
| 119 | } |
|---|
| 120 | return $return; |
|---|
| 121 | } |
|---|
| 122 | /** |
|---|
| 123 | * Commands wrappers |
|---|
| 124 | */ |
|---|
| 125 | public function start($multicall = false) |
|---|
| 126 | { |
|---|
| 127 | return $this->command('d.start', $multicall); |
|---|
| 128 | } |
|---|
| 129 | public function stop($multicall = false) |
|---|
| 130 | { |
|---|
| 131 | return $this->command('d.stop', $multicall); |
|---|
| 132 | } |
|---|
| 133 | public function close($multicall = false) |
|---|
| 134 | { |
|---|
| 135 | return $this->command('d.close', $multicall); |
|---|
| 136 | } |
|---|
| 137 | public function erase($multicall = false) |
|---|
| 138 | { |
|---|
| 139 | return $this->command('d.erase', $multicall); |
|---|
| 140 | } |
|---|
| 141 | public function check_hash($multicall = false) |
|---|
| 142 | { |
|---|
| 143 | return $this->command('d.check_hash', $multicall); |
|---|
| 144 | } |
|---|
| 145 | public function update_priorities($multicall = false) |
|---|
| 146 | { |
|---|
| 147 | return $this->command('d.update_priorities', $multicall); |
|---|
| 148 | } |
|---|
| 149 | /** |
|---|
| 150 | * Info request wrappers |
|---|
| 151 | */ |
|---|
| 152 | /* Download methods */ |
|---|
| 153 | public function get_name($multicall = false, $update = false) |
|---|
| 154 | { |
|---|
| 155 | return $this->get_info('d.get_name', $multicall, $update); |
|---|
| 156 | } |
|---|
| 157 | public function get_down_rate($multicall = false, $update = false) |
|---|
| 158 | { |
|---|
| 159 | return $this->get_info('d.get_down_rate', $multicall, $update); |
|---|
| 160 | } |
|---|
| 161 | public function get_up_rate($multicall = false, $update = false) |
|---|
| 162 | { |
|---|
| 163 | return $this->get_info('d.get_up_rate', $multicall, $update); |
|---|
| 164 | } |
|---|
| 165 | public function get_chunk_size($multicall = false, $update = false) |
|---|
| 166 | { |
|---|
| 167 | return $this->get_info('d.get_chunk_size', $multicall, $update); |
|---|
| 168 | } |
|---|
| 169 | public function get_completed_chunks($multicall = false, $update = false) |
|---|
| 170 | { |
|---|
| 171 | return $this->get_info('d.get_completed_chunks', $multicall, $update); |
|---|
| 172 | } |
|---|
| 173 | public function get_size_chunks($multicall = false, $update = false) |
|---|
| 174 | { |
|---|
| 175 | return $this->get_info('d.get_size_chunks', $multicall, $update); |
|---|
| 176 | } |
|---|
| 177 | public function get_state($multicall = false, $update = false) |
|---|
| 178 | { |
|---|
| 179 | return $this->get_info('d.get_state', $multicall, $update); |
|---|
| 180 | } |
|---|
| 181 | public function get_peers_accounted($multicall = false, $update = false) |
|---|
| 182 | { |
|---|
| 183 | return $this->get_info('d.get_peers_accounted', $multicall, $update); |
|---|
| 184 | } |
|---|
| 185 | public function get_peers_complete($multicall = false, $update = false) |
|---|
| 186 | { |
|---|
| 187 | return $this->get_info('d.get_peers_complete', $multicall, $update); |
|---|
| 188 | } |
|---|
| 189 | public function is_hash_checking($multicall = false, $update = false) |
|---|
| 190 | { |
|---|
| 191 | return $this->get_info('d.is_hash_checking', $multicall, $update); |
|---|
| 192 | } |
|---|
| 193 | public function get_ratio($multicall = false, $update = false) |
|---|
| 194 | { |
|---|
| 195 | return $this->get_info('d.get_ratio', $multicall, $update); |
|---|
| 196 | } |
|---|
| 197 | public function get_tracker_size($multicall = false, $update = false) |
|---|
| 198 | { |
|---|
| 199 | return $this->get_info('d.get_tracker_size', $multicall, $update); |
|---|
| 200 | } |
|---|
| 201 | public function is_active($multicall = false, $update = false) |
|---|
| 202 | { |
|---|
| 203 | return $this->get_info('d.is_active', $multicall, $update); |
|---|
| 204 | } |
|---|
| 205 | public function is_open($multicall = false, $update = false) |
|---|
| 206 | { |
|---|
| 207 | return $this->get_info('d.is_open', $multicall, $update); |
|---|
| 208 | } |
|---|
| 209 | public function get_message($multicall = false, $update = false) |
|---|
| 210 | { |
|---|
| 211 | return $this->get_info('d.get_message', $multicall, $update); |
|---|
| 212 | } |
|---|
| 213 | public function get_creation_date($multicall = false, $update = false) |
|---|
| 214 | { |
|---|
| 215 | return $this->get_info('d.get_creation_date', $multicall, $update); |
|---|
| 216 | } |
|---|
| 217 | public function get_size_files($multicall = false, $update = false) |
|---|
| 218 | { |
|---|
| 219 | return $this->get_info('d.get_size_files', $multicall, $update); |
|---|
| 220 | } |
|---|
| 221 | public function get_tied_to_file($multicall = false, $update = false) |
|---|
| 222 | { |
|---|
| 223 | return $this->get_info('d.get_tied_to_file', $multicall, $update); |
|---|
| 224 | } |
|---|
| 225 | public function get_base_path($multicall = false, $update = false) |
|---|
| 226 | { |
|---|
| 227 | return $this->get_info('d.get_base_path', $multicall, $update); |
|---|
| 228 | } |
|---|
| 229 | public function get_peers_max($multicall = false, $update = false) |
|---|
| 230 | { |
|---|
| 231 | return $this->get_info('d.get_peers_max', $multicall, $update); |
|---|
| 232 | } |
|---|
| 233 | public function get_peers_min($multicall = false, $update = false) |
|---|
| 234 | { |
|---|
| 235 | return $this->get_info('d.get_peers_min', $multicall, $update); |
|---|
| 236 | } |
|---|
| 237 | public function d_get_priority($multicall = false, $update = false) |
|---|
| 238 | { |
|---|
| 239 | return $this->get_info('d.get_priority', $multicall, $update); |
|---|
| 240 | } |
|---|
| 241 | public function d_set_priority($priority, $multicall = false) |
|---|
| 242 | { |
|---|
| 243 | $message = new xmlrpcmsg("d.set_priority", array(new xmlrpcval($this->hash, 'string'), new xmlrpcval($priority, 'int'))); |
|---|
| 244 | |
|---|
| 245 | if($multicall === true) |
|---|
| 246 | { |
|---|
| 247 | $return = $this->multicall->add($message, $this->hash); |
|---|
| 248 | } |
|---|
| 249 | else |
|---|
| 250 | { |
|---|
| 251 | $result = $this->client->send($message); |
|---|
| 252 | $return = $this->checkError($result); |
|---|
| 253 | } |
|---|
| 254 | return $return; |
|---|
| 255 | } |
|---|
| 256 | /* Tracker methods */ |
|---|
| 257 | public function t_set_enabled($tracker, $enabled, $multicall = false) |
|---|
| 258 | { |
|---|
| 259 | $message = new xmlrpcmsg('t.set_enabled', array(new xmlrpcval($this->hash, 'string'), new xmlrpcval($tracker, 'int'), new xmlrpcval($enabled, 'int'))); |
|---|
| 260 | |
|---|
| 261 | if($multicall === true) |
|---|
| 262 | { |
|---|
| 263 | $return = $this->multicall->add($message, $this->hash); |
|---|
| 264 | } |
|---|
| 265 | else |
|---|
| 266 | { |
|---|
| 267 | $result = $this->client->send($message); |
|---|
| 268 | $return = $this->checkError($result); |
|---|
| 269 | } |
|---|
| 270 | return $return; |
|---|
| 271 | } |
|---|
| 272 | public function t_get_url($tracker, $multicall = false, $update = false) |
|---|
| 273 | { |
|---|
| 274 | return $this->get_info_object('t.get_url', $tracker, $multicall, $update); |
|---|
| 275 | } |
|---|
| 276 | public function t_get_scrape_complete($tracker, $multicall = false, $update = false) |
|---|
| 277 | { |
|---|
| 278 | return $this->get_info_object('t.get_scrape_complete', $tracker, $multicall, $update); |
|---|
| 279 | } |
|---|
| 280 | public function t_get_scrape_incomplete($tracker, $multicall = false, $update = false) |
|---|
| 281 | { |
|---|
| 282 | return $this->get_info_object('t.get_scrape_incomplete', $tracker, $multicall, $update); |
|---|
| 283 | } |
|---|
| 284 | public function t_is_enabled($tracker, $multicall = false, $update = false) |
|---|
| 285 | { |
|---|
| 286 | return $this->get_info_object('t.is_enabled', $tracker, $multicall, $update); |
|---|
| 287 | } |
|---|
| 288 | /* Files methods */ |
|---|
| 289 | public function f_set_priority($file, $priority, $multicall = false) |
|---|
| 290 | { |
|---|
| 291 | $message = new xmlrpcmsg('f.set_priority', array(new xmlrpcval($this->hash, 'string'), new xmlrpcval($file, 'int'), new xmlrpcval($priority, 'int'))); |
|---|
| 292 | |
|---|
| 293 | if($multicall === true) |
|---|
| 294 | { |
|---|
| 295 | $return = $this->multicall->add($message, $this->hash); |
|---|
| 296 | } |
|---|
| 297 | else |
|---|
| 298 | { |
|---|
| 299 | $result = $this->client->send($message); |
|---|
| 300 | $return = $this->checkError($result); |
|---|
| 301 | } |
|---|
| 302 | return $return; |
|---|
| 303 | } |
|---|
| 304 | public function f_get_path($param, $multicall = false, $update = false) |
|---|
| 305 | { |
|---|
| 306 | return $this->get_info_object('f.get_path', $param, $multicall, $update); |
|---|
| 307 | } |
|---|
| 308 | public function f_get_completed_chunks($param, $multicall = false, $update = false) |
|---|
| 309 | { |
|---|
| 310 | return $this->get_info_object('f.get_completed_chunks', $param, $multicall, $update); |
|---|
| 311 | } |
|---|
| 312 | public function f_get_size_chunks($param, $multicall = false, $update = false) |
|---|
| 313 | { |
|---|
| 314 | return $this->get_info_object('f.get_size_chunks',$param, $multicall, $update); |
|---|
| 315 | } |
|---|
| 316 | public function f_get_priority($param, $multicall = false, $update = false) |
|---|
| 317 | { |
|---|
| 318 | return $this->get_info_object('f.get_priority', $param, $multicall, $update); |
|---|
| 319 | } |
|---|
| 320 | /* wTorrent specific atributes */ |
|---|
| 321 | public function get_private() |
|---|
| 322 | { |
|---|
| 323 | return $this->data['private']; |
|---|
| 324 | } |
|---|
| 325 | public function get_owner() |
|---|
| 326 | { |
|---|
| 327 | return $this->data['owner']; |
|---|
| 328 | } |
|---|
| 329 | } |
|---|
| 330 | ?> |
|---|