-
Notifications
You must be signed in to change notification settings - Fork 1
/
SqueezeplayConfig.php
151 lines (126 loc) · 5.23 KB
/
SqueezeplayConfig.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Copyright Enna Team
*
* Handle Squeezeplay config
* How to use:
*
* $c = new SqueezeplayConfig();
* $value = $c−>getServerInfo('ip');
* $c->setPlayerName("test");
* $c->commitChanges();
*/
if (!defined('ENNA_WWW')) define('ENNA_WWW', 1);
include_once('common.php');
if (!defined('PAGE_NAV')) define('PAGE_NAV', 0);
require_once('ConfigParser.php');
define('SQUEEZEPLAY_PLAYBACK_CONFIG', 'settings = {enableAudio=1,squeezeNetwork=false,serverUuid="%s",powerState="on",volume=40,serverInit={%sip="%s",},serverName="%s",playerInit={name="%s",model="squeezeplay",},captureVolume=40,}');
class SqueezeplayConfig
{
protected $playerName = "";
protected $serverInfos = array();
public function __construct()
{
global $config;
$filename = $config['sq_config_path'] . $config['sq_playback_file'];
try
{
if (!is_readable($filename))
throw new Exception('File not found');
$luaconf = file_get_contents($filename);
//Clean file
$luaconf = str_replace(array("\r\n", "\n", "\r"), "", $luaconf);
//reformat lua config with newlines
$luaconf = str_replace(array("{", ","), array("{\n", ",\n"), $luaconf);
$parser = new ConfigParser($luaconf);
$c = $parser->toArray();
$this->playerName = $c['settings']['playerInit']['name'];
$this->serverInfos['uuid'] = $c['settings']['serverUuid'];
if (array_key_exists('mac', $c['settings']['serverInit']))
$this->serverInfos['mac'] = $c['settings']['serverInit']['mac'];
$this->serverInfos['ip'] = $c['settings']['serverInit']['ip'];
$this->serverInfos['name'] = $c['settings']['serverName'];
}
catch (Exception $e)
{
//Can't parse lua config, reset it
@unlink($filename);
$this->serverInfos['uuid'] = "Unknown";
$this->serverInfos['ip'] = "None";
$this->serverInfos['name'] = "None";
}
//Write setupdone into lua config to tell squeezeplay setup is done and don't bother with that
$filename = $config['sq_config_path'] . $config['sq_welcome_file'];
$fp = fopen($filename, 'w');
fwrite($fp, 'settings = {setupDone=true,}');
fclose($fp);
}
public function commitChanges()
{
global $config;
//write changes to config file
$mac = "";
if (array_key_exists('mac', $this->serverInfos))
$mac = sprintf("mac=\"%s\",\n", $this->serverInfos['mac']);
if ($this->playerName == "")
$this->playerName = $config['product_name'];
$conf = sprintf(SQUEEZEPLAY_PLAYBACK_CONFIG, $this->serverInfos['uuid'],
$mac,
$this->serverInfos['ip'],
$this->serverInfos['name'],
$this->playerName);
$filename = $config['sq_config_path'] . $config['sq_playback_file'];
if (file_exists($filename) && !is_writable($filename))
return false;
try
{
$fp = fopen($filename, 'w');
fwrite($fp, $conf);
fclose($fp);
}
catch(Exception $e)
{
return false;
}
//restart jive service
exec('systemctl restart jive.service', $ret);
return true;
}
public function setPlayerName($name)
{
$this->playerName = $name;
}
public function getPlayerName()
{
return $this->playerName;
}
public function setServerInfo($info, $value)
{
$this->serverInfos[$info] = $value;
}
public function getServerInfo($info)
{
if (!array_key_exists($info, $this->serverInfos))
return false;
return $this->serverInfos[$info];
}
public function getServerInfoAll()
{
return $this->serverInfos;
}
public function setServerInfoAll($infos)
{
$this->serverInfos = $infos;
if ($this->getServerInfo('name') === false)
$this->setServerInfo('name', 'Unknown');
if ($this->getServerInfo('uuid') === false)
$this->setServerInfo('uuid', '');
}
}
/*
$a = new SqueezeplayConfig();
print_r($a->getPlayerName());
print("\n");
print_r($a->getServerInfoAll());
*/
?>