-
Notifications
You must be signed in to change notification settings - Fork 2
/
class_transmissionremote.php
135 lines (109 loc) · 3.71 KB
/
class_transmissionremote.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
require_once "knj/strings.php";
require_once "knj/os.php";
/** This is a PHP-framework for the transmission-remote. */
class transmissionremote{
function setArgs($args){
foreach($args AS $key => $value){
if ($key == "user" || $key == "pass"){
$this->args[$key] = $value;
}else{
throw new Exception("Invalid argument: \"" . $key . "\".");
}
}
}
/** Returns the auth-string which should be used with transmission-remote. */
function getLoginString(){
if ($this->args["user"] && $this->args["pass"]){
return " --auth " . knj_string_unix_safe($this->args["user"]) . ":" . knj_string_unix_safe($this->args["pass"]);
}
return "";
}
/** Parses a list of downloads. */
function getList(){
$cmd = "transmission-remote" . $this->getLoginString() . " --list";
$exec = knj_os::shellCMD($cmd);
$this->errorCmd($exec);
if (preg_match("/Sum:\s+None/", $exec["result"], $match)){
return array();
}
if (!preg_match_all("/\s*([\d*]+)\s+(\d+)%\s+(Done|None|[\d\.]+\s+\S+)\s+(Unknown|Done|\d+ (min|hrs|days))\s+([\d\.]+)\s+([\d\.]+)\s+(\S+)\s+(Seeding|Up & Down|\S+)\s+(.+)\n/", $exec["result"], $matches)){
throw new Exception("Could not parse list.\n\n" . "Content:\n" . $exec["result"]);
}
$return = array();
foreach($matches[0] AS $key => $value){
$hash = $matches[1][$key];
$name = $matches[10][$key];
$percent = $matches[2][$key];
$done = $matches[4][$key];
/*$size = $matches[2][$key];
$size_mult = $matches[3][$key];
if ($size_mult == "GiB"){
$size_bytes = (($size * 1024) * 1024) * 1024;
}elseif($size_mult == "MiB"){
$size_bytes = ($size * 1024) * 1024;
}else{
$size_bytes = "[unknown]";
}
*/
$return[] = array(
"name" => $name,
"id" => intval($hash),
"percent" => $percent,
"done" => $done,
//"size" => $size,
//"size_mult" => $size_mult,
//"size_bytes" => $size_bytes
);
}
return $return;
}
function getFilesForID($id){
$cmd = "transmission-remote" . $this->getLoginString() . " --torrent " . $id . " --files";
$res = knj_os::shellCMD($cmd);
if (!preg_match_all("/\s+([0-9]+): ([0-9]+%)\s+(Low|Normal|High)\s+(Yes|No)\s+([0-9\.]+) (\S{2,6})\s+(.*)\n/U", $res["result"], $matches)){
throw new exception("Could not match files.");
}
$return = array();
foreach($matches[0] AS $key => $value){
$return[] = array(
"filename" => trim($matches[7][$key])
);
}
return $return;
}
/** Sets the limit of download and upload. */
function setLimit($mode, $limit = null){
$cmd = "transmission-remote" . $this->getLoginString();
if ($mode == "down" || $mode == "up"){
$cmd .= " --" . $mode . "limit " . knj_string_unix_safe($limit);
}elseif($mode == "nodown"){
$cmd .= " --no-downlimit";
}elseif($mode == "noup"){
$cmd .= " --no-uplimit";
}else{
throw new Exception("Invalid mode: \"" . $mode . "\".");
}
$exec = knj_os::shellCMD($cmd);
$this->errorCmd($exec);
return true;
}
/** Stops a torrent by its hash. */
function torrAction($id, $action){
$cmd = "transmission-remote" . $this->getLoginString() . " --torrent " . $id;
if ($action == "stop" || $action == "start" || $action == "remove"){
$cmd .= " --" . $action;
}else{
throw new Exception("Invalid action: \"" . $action . "\".");
}
$exec = knj_os::shellCMD($cmd);
$this->errorCmd($exec);
return true;
}
/** Throws exceptions based on output from knj_os::shellCMD(). */
function errorCmd($exec){
if (strlen(trim($exec["error"])) > 0){
throw new Exception("Transmission-error: " . trim($exec["error"]));
}
}
}