-
Notifications
You must be signed in to change notification settings - Fork 1
/
enigma2.php
111 lines (99 loc) · 2.94 KB
/
enigma2.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
<?php
/**
* Class to communicate with enigma2 devices using FTP protocol
*
* @todo there should be more classes to communicate
*
* @throws Exception
*/
class Enigma2Ftp
{
protected $_ftpStream;
protected $_host;
protected $_user;
protected $_passwd;
public function __construct($host, $user = "root", $passwd = "")
{
$this->_host = $host;
$this->_user = $user;
$this->_passwd = $passwd;
// connect FTP
$this->_ftpStream = ftp_connect($host);
// login to FTP server
$login = ftp_login($this->_ftpStream, $user, $passwd);
if (!$login) {
throw new Exception("FTP not connected");
} else {
ftp_pasv($this->_ftpStream, true);
}
}
public function __destruct()
{
if ($this->_ftpStream) {
// close FTP connection
@ftp_close($this->_ftpStream);
}
}
public function get($fn, $target = null)
{
if (is_string($target)) {
if (!ftp_get($this->_ftpStream, $target, $fn, FTP_ASCII)) {
$target = false;
}
} else {
if (is_null($target)) {
$target = tmpfile();
}
if (!ftp_fget($this->_ftpStream, $target, $fn, FTP_ASCII)) {
$target = false;
}
}
return $target;
}
/**
* @param $fn
* @param $target
* @return bool
*/
public function put($fn, $target)
{
return ftp_put($this->_ftpStream, $target, $fn, FTP_ASCII);
}
public function putBouquets($folderName)
{
// delete existing files
$files = ftp_nlist($this->_ftpStream, '/etc/enigma2/');
foreach ($files as $fn) {
if (pathinfo($fn, PATHINFO_EXTENSION) == 'tv') {
ftp_delete($this->_ftpStream, $fn);
}
}
// upload new files
foreach (glob("$folderName/*.tv") as $filename) {
$fn = pathinfo($filename, PATHINFO_BASENAME);
ftp_put($this->_ftpStream, '/etc/enigma2/' . $fn, $filename, FTP_ASCII);
}
}
public function getBouquets($folderName)
{
// TODO delete old files
$files = ftp_nlist($this->_ftpStream, '/etc/enigma2/');
foreach ($files as $fn) {
if (pathinfo($fn, PATHINFO_EXTENSION) == 'tv') {
$target = $folderName . DIRECTORY_SEPARATOR . pathinfo($fn, PATHINFO_BASENAME);
ftp_get($this->_ftpStream, $target, $fn, FTP_ASCII);
}
}
}
public function reload()
{
file_get_contents("http://" . $this->_host . "/web/servicelistreload?mode=2");
return true;
// TODO fallback to enigma2 restart
/*require_once "telnet.php";
$telnet = new Telnet($this->_host);
$telnet->connect();
$telnet->login($this->_user, $this->_passwd);
$telnet->exec("killall -9 enigma2");*/
}
}