forked from gharris999/SrvrPowerCtrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebUI.pm
349 lines (267 loc) · 10.6 KB
/
WebUI.pm
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# ===============================================================================================================
# SrvrPowerCtrl - a plugin for SqueezeCenter 7.3.x / Squeezebox Server 7.4.x
# Allows shutdown/restart/suspend/hibernation of your Squeezebox Server
# hardware via SBS's web interface, your Squeezebox's IR remote
# or via a SBC / Touch / SqueezePlay.
#
# Version 20160501.151506
#
# Copyright (C) 2008, 2009 Gordon Harris
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 2, which should be included with this software.
#
# Based on Adrian C's (aka tenwiseman) Server Power Control plugin:
# http://adrianonline.wordpress.com/
# http://forums.slimdevices.com/showthread.php?t=32674
# Copyright AdrianOnline.Net Feb 2007. Initially modeled on Max Spicer's Max2.pm
#
# Also includes code from PeterW's AllQuiet plugin: http://www.tux.org/~peterw/slim/slim7/AllQuiet/
# AllQuiet copyright (c) 2007 by Peter Watkins ([email protected])
#
#
# See a duscussion of this plugin at http://forums.slimdevices.com/showthread.php?t=48521
#
# ===============================================================================================================
#
# Webmenu.pm -- handler for SrvrPowerCtrl actions initiated via the SC web UI..
# Thanks to indifference_engine for suggesting this.
#
package Plugins::SrvrPowerCtrl::WebUI;
use strict;
use base qw(Slim::Web::Settings);
use Slim::Utils::Prefs;
use Slim::Utils::Validate;
use Slim::Utils::OSDetect;
use File::Spec::Functions qw(:ALL);
use Slim::Utils::Strings qw(string);
use URI::Escape;
#Global Variables..
use vars qw(%g);
*g = \%Plugins::SrvrPowerCtrl::Settings::g;
my $plugin; # the main plugin class
sub new {
my $class = shift;
$plugin = shift;
#$g{log}->is_debug && $g{log}->debug("Called!");
$class->SUPER::new($plugin);
}
# ----------------------------------------------------------------------------------------
# Show the SrvrPowerCtrl menu on the SC web UI home 'Extras' menu
sub EscapeURL {
my ($url, $addendum) = @_;
#$g{log}->is_debug && $g{log}->debug("url before: $url");
#$url = uri_escape($url);
#escape the '/' elements..
$url =~ s!\/!\\/!g;
#escape the dot..
$url =~ s!([.])!\\\.!g;
#add on any parameters
$url = "${url}\.*";
#$g{log}->is_debug && $g{log}->debug("url after: $url");
return $url;
}
my $bWebUIIsActive = 0;
sub ActivateWebUI {
my $bEnable = shift;
my $urlWebUI = 'plugins/SrvrPowerCtrl/webui.html';
my $urlAction = 'plugins/SrvrPowerCtrl/action.html';
my $urlActionDone = 'plugins/SrvrPowerCtrl/completed.html';
my $urlActionCanceled = 'plugins/SrvrPowerCtrl/canceled.html';
my $urlPrefix = "\\/";
#my $urlSuffix = "\\.*";
if ($bEnable) {
#Don't re-activate if already active..
if (!$bWebUIIsActive) {
#Tack our menu onto the Extras menu..
Slim::Web::Pages->addPageLinks("plugins", { $plugin->getDisplayName() => $urlWebUI });
Slim::Web::Pages->addPageLinks("icons", { $plugin->getDisplayName() => $plugin->_pluginDataFor('icon') });
#Provide CSRF protection for the page and set the reference to the page's pre-processing handler..
$urlWebUI = EscapeURL($urlWebUI);
$urlAction = EscapeURL($urlAction, ".*");
$urlActionDone = EscapeURL($urlActionDone, "\.*");
$urlActionCanceled = EscapeURL($urlActionCanceled, ".*");
#for SBS 7.4 and later..
#if ( $g{nSCVersion} >= 7.4 ) {
#compareVersions Returns: 1 if $left > $right, 0 if $left == $right, -1 if $left < $right
if (Slim::Utils::Versions::compareVersions($::VERSION , '7.4') >= 0) {
Slim::Web::Pages->addPageFunction($urlWebUI, \&RenderWebUIMenu);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlWebUI);
Slim::Web::Pages->addPageFunction( $urlAction, \&RenderActionPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlAction);
Slim::Web::Pages->addPageFunction( $urlActionDone, \&RenderActionDonePage);
#Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlActionDone);
Slim::Web::Pages->addPageFunction($urlActionCanceled, \&RenderActionCanceledPage);
#Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlActionCanceled);
} else { #for SC 7.3.x and earlier..
Slim::Web::HTTP::addPageFunction($urlWebUI, \&RenderWebUIMenu);
Slim::Web::HTTP::protect($urlPrefix . $urlWebUI);
Slim::Web::HTTP::addPageFunction( $urlAction, \&RenderActionPage);
Slim::Web::HTTP::protect($urlPrefix . $urlAction);
Slim::Web::HTTP::addPageFunction( $urlActionDone, \&RenderActionDonePage);
#Slim::Web::HTTP::protect($urlPrefix . $urlActionDone);
Slim::Web::HTTP::addPageFunction($urlActionCanceled, \&RenderActionCanceledPage);
#Slim::Web::HTTP::protect($urlPrefix . $urlActionCanceled);
}
$bWebUIIsActive = 1;
}
} else {
if ($bWebUIIsActive) {
#Kill our menu on the Extras menu..
Slim::Web::Pages->addPageLinks("plugins", { $plugin->getDisplayName() => undef });
$bWebUIIsActive = 0;
}
}
$g{log}->is_debug && $g{log}->debug('WebUI ' . (($bEnable && $bWebUIIsActive) ? '' : 'de-') . 'activated!');
return ($bEnable && $bWebUIIsActive);
}
# Draws the plugin's web ui page off the extras menu..
sub RenderWebUIMenu {
my ($client, $params) = @_;
my @actions = ();
my $action;
#$g{log}->is_debug && $g{log}->debug("Called! params == " . Data::Dump::dump($params));
#Rebuild the action items every time?
Plugins::SrvrPowerCtrl::Menu::initActionItems();
foreach my $item (@{$g{aActions}}) {
if ($item->{menuindex} ge 0) {
$action = ();
$action = {
'action' => uc($item->{'action'}),
'menutext' => $item->{'menutext'},
};
push (@actions, $action);
}
}
$params->{actionlist} = \@actions;
$params->{srvrpowerctrl_stats} = Plugins::SrvrPowerCtrl::Util::SrvrPowerCtrlStats();
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
# Draws the action page..
sub RenderActionPage {
my ($client, $params) = @_;
my $n;
my $action;
my $item;
my $nDeferTime;
my $timeoutmsg;
my $message;
#$g{log}->is_debug && $g{log}->debug("Called! client == " . $client->name() || 'no client' . ", param == " . Data::Dump::dump($params));
$nDeferTime = 0;
#add sleep-playing time to defer time??
$action = $params->{url_query};
$action = lc(substr($action, 7));
$n = index($action, "&");
if ($n > 0) {
$action = substr($action, 0, $n);
}
$g{log}->is_debug && $g{log}->debug("action == $action");
$item = Plugins::SrvrPowerCtrl::Menu::findActionItem($action);
if (defined($item)) {
#Check to see if there is already a pending action..
if (!$g{tPendingActionTimer}) {
#cue up the action..
$g{log}->is_debug && $g{log}->debug("Cueing up action $action");
#Auto-add sleep defer time...
$nDeferTime = Plugins::SrvrPowerCtrl::SleepButton::GetSleepTime();
#if we are sleep-playing..
if ($nDeferTime > 0) {
$g{log}->is_debug && $g{log}->debug("Deferring action " . $item->{action} . " for $nDeferTime while sleep-playing..");
$item->{isSleepDefered} = 1;
#Activate the sleep monitor
Plugins::SrvrPowerCtrl::Watchdog::ActivateSleepRequestMonitor(1);
}
$nDeferTime += $item->{cancelwait};
# Prepare the action for execution...the action is only cancelable from the web page..
if (Plugins::SrvrPowerCtrl::Plugin::prepareAction($client, $item, 1)) {
$timeoutmsg = string( 'PLUGIN_SRVRPOWERCTRL_TIMEOUT_MSG' );
$timeoutmsg = sprintf($timeoutmsg, $nDeferTime);
$params->{refreshurl} = "plugins/SrvrPowerCtrl/completed.html";
$message = $item->{'message'} . ' ' . $timeoutmsg;
} else {
#we're blocked...why?
$params->{refreshurl} = "plugins/SrvrPowerCtrl/canceled.html";
$message = Plugins::SrvrPowerCtrl::Block::GetBlockReasonMessage($action);
$nDeferTime = $g{prefs}->nRegretDelay;
}
} else {
# Already a pending action...just report on it..
$nDeferTime = $g{hPendingAction}->{exeTime} - time();
$message = Plugins::SrvrPowerCtrl::Plugin::GetPendingActionMessage($action);
$timeoutmsg = string( 'PLUGIN_SRVRPOWERCTRL_TIMEOUT_MSG' );
$timeoutmsg = sprintf($timeoutmsg, $nDeferTime);
$params->{refreshurl} = "plugins/SrvrPowerCtrl/completed.html";
$message = $message . ' ' . $timeoutmsg;
}
} else {
# We don't know about this action....
$params->{refreshurl} = "plugins/SrvrPowerCtrl/canceled.html";
$nDeferTime += $g{prefs}->nRegretDelay;
$message = "Bad action: $action";
}
$g{log}->is_debug && $g{log}->debug("$message");
$params->{refreshtime} = $nDeferTime;
$params->{actionmessage} = $message;
$params->{action} = $action;
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
sub RenderActionCanceledPage {
my ($client, $params) = @_;
my $message;
my $nDeferTime;
#$g{log}->is_debug && $g{log}->debug("Called! client == " . $client->name() || 'no client' . ", param == " . Data::Dump::dump($params));
$nDeferTime = $g{prefs}->nRegretDelay;
if (!$g{tPendingActionTimer}) {
$message = string('PLUGIN_SRVRPOWERCTRL_BLOCKED_MSG');
} else {
$message = string('PLUGIN_SRVRPOWERCTRL_CANCELED_MSG');
Plugins::SrvrPowerCtrl::Plugin::cancelAction($client);
}
$g{log}->is_debug && $g{log}->debug("$message");
$params->{actionmessage} = $message;
$params->{refreshtime} = $nDeferTime;
$params->{refreshurl} = "home.html";
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
# Draws the action done page..
sub RenderActionDonePage {
my ($client, $params) = @_;
my $n;
my $action;
my $item;
my $message;
my $confmessage;
my $nDeferTime;
#$g{log}->is_debug && $g{log}->debug("Called! client == " . $client->name() || 'no client' . ", param == " . Data::Dump::dump($params));
$params->{refreshurl} = "";
$action = $params->{url_query};
$action = lc(substr($action, 7));
#strip out any other parameters..
#suspend2AS&player=00%3a04%3a20%3a06%3a9e%3a70
$n = index($action, "&");
if ($n > 0) {
$action = substr($action, 0, $n);
}
$g{log}->is_debug && $g{log}->debug("action == $action");
$item = Plugins::SrvrPowerCtrl::Menu::findActionItem($action);
if (defined($item)) {
$message = $item->{'message'} . "..";
#$confmessage = string('PLUGIN_SRVRPOWERCTRL_' . uc($action) . '_DONE_MSG') . "..";
$confmessage = $item->{'messagedone'} . "..";
$nDeferTime = $item->{refreshwait};
$params->{refreshurl} = "home.html";
} else {
$message = "Bad action: $action";
$confmessage = $message;
$params->{refreshurl} = "home.html";
$nDeferTime = 15;
}
$params->{refreshtime} = $nDeferTime;
$params->{actionmessage} = $message;
$params->{confirmedmessage} = $confmessage;
#$g{log}->is_debug && $g{log}->debug("params == " . Data::Dump::dump(($params));
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
1;
__END__