-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHelp.pm
454 lines (349 loc) · 15.4 KB
/
Help.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# ===============================================================================================================
# 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
#
# ===============================================================================================================
#
# Help.pm -- handler functions for SrvrPowerCtrl help pages..
#
package Plugins::SrvrPowerCtrl::Help;
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);
if ($^O =~ /^m?s?win/i) { ## Are we running windows?
eval {
require Win32;
import Win32;
} ;
}
#Global Variables..
use vars qw(%g);
*g = \%Plugins::SrvrPowerCtrl::Settings::g;
my $hOSs = ();
my $plugin; # the main plugin class
sub new {
my $class = shift;
$plugin = shift;
my $urlPrefix = "\\/";
my $urlRedirectHelp = 'plugins/SrvrPowerCtrl/html/helpredir.html';
my $urlMainHelp = 'plugins/SrvrPowerCtrl/html/help.html';
my $urlTSHelp = 'plugins/SrvrPowerCtrl/html/troubleshooting.html';
my $urlLogInfo = 'plugins/SrvrPowerCtrl/html/loginfo.html';
my $urlLog = 'plugins/SrvrPowerCtrl/html/log.html';
my $urlPrefs = 'plugins/SrvrPowerCtrl/html/prefs.html';
my $urlOSHelp;
#OSs we know how to setup..
my @aOSs = ('win', 'unix', 'mac');
#Flag the OSs we know how to setup up..
for (@aOSs) { $hOSs->{$_} = 1; }
#$g{log}->is_debug && $g{log}->debug("aOSs: " . Data::Dump::dump(@aOSs));
#Provide CSRF protection for the page and set the reference to the page's pre-processing handler..
$urlRedirectHelp = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlRedirectHelp);
$urlMainHelp = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlMainHelp);
$urlTSHelp = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlTSHelp);
$urlLogInfo = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlLogInfo);
$urlLog = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlLog);
$urlPrefs = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlPrefs);
#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($urlRedirectHelp, \&RenderRedirectHelpPage);
Slim::Web::Pages->addPageFunction($urlMainHelp, \&RenderHelpPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlMainHelp);
Slim::Web::Pages->addPageFunction($urlTSHelp, \&RenderTSHelpPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlTSHelp);
Slim::Web::Pages->addPageFunction($urlLogInfo, \&RenderLogInfoPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlLogInfo);
Slim::Web::Pages->addPageFunction($urlLog, \&RenderLogPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlLog);
Slim::Web::Pages->addPageFunction($urlPrefs, \&RenderPrefsPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlPrefs);
foreach my $szOS (@aOSs) {
$urlOSHelp = 'plugins/SrvrPowerCtrl/html/' . $szOS . '-help.html';
$urlOSHelp = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlOSHelp);
Slim::Web::Pages->addPageFunction($urlOSHelp, \&RenderOSHelpPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlOSHelp);
#$g{log}->is_debug && $g{log}->debug("$urlOSHelp");
}
# Unsupported OS..
$urlOSHelp = 'plugins/SrvrPowerCtrl/html/' . 'unsuppos-help.html';
$urlOSHelp = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlOSHelp);
Slim::Web::Pages->addPageFunction($urlOSHelp, \&RenderOSHelpPage);
Slim::Web::HTTP::CSRF->protect($urlPrefix . $urlOSHelp);
#for SC 7.3.4 and earlier..
} else {
Slim::Web::HTTP::addPageFunction($urlRedirectHelp, \&RenderRedirectHelpPage);
Slim::Web::HTTP::addPageFunction($urlMainHelp, \&RenderHelpPage);
Slim::Web::HTTP::protect($urlPrefix . $urlMainHelp);
Slim::Web::HTTP::addPageFunction($urlTSHelp, \&RenderTSHelpPage);
Slim::Web::HTTP::protect($urlPrefix . $urlTSHelp);
Slim::Web::HTTP::addPageFunction($urlLogInfo, \&RenderLogInfoPage);
Slim::Web::HTTP::protect($urlPrefix . $urlLogInfo);
Slim::Web::HTTP::addPageFunction($urlLog, \&RenderLogPage);
Slim::Web::HTTP::protect($urlPrefix . $urlLog);
Slim::Web::HTTP::addPageFunction($urlPrefs, \&RenderPrefsPage);
Slim::Web::HTTP::protect($urlPrefix . $urlPrefs);
# Supported OSs..
foreach my $szOS (@aOSs) {
$urlOSHelp = 'plugins/SrvrPowerCtrl/html/' . $szOS . '-help.html';
$urlOSHelp = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlOSHelp);
Slim::Web::HTTP::addPageFunction($urlOSHelp, \&RenderOSHelpPage);
Slim::Web::HTTP::protect($urlPrefix . $urlOSHelp);
#$g{log}->is_debug && $g{log}->debug("$urlOSHelp");
}
# Unsupported OS..
$urlOSHelp = 'plugins/SrvrPowerCtrl/html/' . 'unsuppos-help.html';
$urlOSHelp = Plugins::SrvrPowerCtrl::WebUI::EscapeURL($urlOSHelp);
Slim::Web::HTTP::addPageFunction($urlOSHelp, \&RenderOSHelpPage);
Slim::Web::HTTP::protect($urlPrefix . $urlOSHelp);
}
$class->SUPER::new($plugin);
$g{log}->is_debug && $g{log}->debug("HelpUI is activated!");
}
sub RenderRedirectHelpPage {
my ($client, $params) = @_;
my $szOSHelpFile;
$g{log}->is_debug && $g{log}->debug('Called!'); #client == ' . ( eval{$client->name()} || 'no client' ) . ", param == " . Data::Dump::dump($params));
if ( $hOSs->{"$g{szOS}"} ) {
$szOSHelpFile = 'plugins/SrvrPowerCtrl/html/' . $g{szOS} . '-help.html';
# Unsupported OS or distro..
} else {
$szOSHelpFile = 'plugins/SrvrPowerCtrl/html/unsuppos-help.html';
}
$params->{path} = $szOSHelpFile;
return RenderOSHelpPage($client, $params);
}
sub RenderHelpPage {
my ($client, $params) = @_;
my $szOSHelpLink;
my $szSBSUser;
my $szServerName;
my $szPluginPath;
#$g{log}->is_debug && $g{log}->debug('Called!'); # client == ' . ( eval{$client->name()} || 'no client' ) . ", param == " . Data::Dump::dump($params));
$szPluginPath = $g{szAppPath};
#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) {
# $szServerName = string('SQUEEZECENTER');
#} else {
# $szServerName = string('SQUEEZEBOX_SERVER');
#}
$szServerName = Plugins::SrvrPowerCtrl::Util::GetSCName();
$szSBSUser = $g{szSCUser};
if ( $hOSs->{"$g{szOS}"} ) {
$szOSHelpLink = "<a href=\"/plugins/SrvrPowerCtrl/html/$g{szOS}-help.html\" ><h4>" . string("PLUGIN_SRVRPOWERCTRL_" . uc($g{szOS}) . "_HELP_PAGE") . '</h4></a>' ;
# Unsupported OS or distro..
} else {
$szOSHelpLink = "<a href=\"/plugins/SrvrPowerCtrl/html/unsuppos-help.html\" ><h4>" . string('PLUGIN_SRVRPOWERCTRL_UNSUPPOS_HELP_PAGE') . '</h4></a>' ;
}
$params->{distro} = $g{szDistro}; # [% distro %]
$params->{setuplink} = $szOSHelpLink; # [% setuplink %]
$params->{sbsuser} = $szSBSUser; # [% sbsuser %]
$params->{sbsservername} = $szServerName; # [% sbsservername %]
$params->{pluginpath} = $szPluginPath; # [% pluginpath %]
#$g{log}->is_debug && $g{log}->debug('params == ' . Data::Dump::dump($params));
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
sub MyFindInPATH {
my $file = shift;
#return $file if -e $file;
foreach (split ';',$ENV{PATH}) {
return $_.'\\'.$file if -e $_.'/'.$file;
}
return undef;
}
sub IsHelperUtilInstalled {
my $szCheckFile;
#Check to see if we're already enabled by looking for file..
# Windows..
if ($g{szOS} eq 'win') {
$szCheckFile = "SCPowerTool.exe";
$szCheckFile = MyFindInPATH($szCheckFile);
$g{log}->is_debug && $g{log}->debug("MyFindInPATH found " . ($szCheckFile ? $szCheckFile : 'nada') );
# Various linux distros..
} elsif ( $g{szOS} eq 'unix' || $g{szOS} eq 'mac' ) {
#Path to one of our scripts..if we're already setup..
$szCheckFile = '/usr/local/sbin/spc-wakeup.sh';
} else {
return (0, undef);
}
if (-e $szCheckFile) {
return (1, $szCheckFile);
}
return (0, undef);
}
sub RenderUnsuppOSHelpPage {
my ($client, $params) = @_;
my $szDistro = $g{szDistro};
my $szWarning;
my $szServerName;
my $szSBSUser;
#Get the SC/SBS username..(NA for windows & mac)
#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) {
# $szServerName = string('SQUEEZECENTER');
#} else {
# $szServerName = string('SQUEEZEBOX_SERVER');
#}
$szServerName = Plugins::SrvrPowerCtrl::Util::GetSCName();
$szSBSUser = $g{szSCUser};
# Unsupported OS?
if ( ! $hOSs->{"$g{szOS}"} ) {
$szDistro = $g{szOS} . ':' . $g{szDistro};
$szWarning = sprintf('<H3>' . string('PLUGIN_SRVRPOWERCTRL_UNSUPPOS_HELP_MSG') . '</H3>', $szDistro);
}
#$g{log}->is_debug && $g{log}->debug("$message");
$params->{distro} = $g{szDistro}; # [% distro %]
$params->{warning} = $szWarning; # [% warning %]
$params->{sbsuser} = $szSBSUser; # [% sbsuser %]
$params->{sbsservername} = $szServerName; # [% sbsservername %]
#$g{log}->is_debug && $g{log}->debug('params == ' . Data::Dump::dump($params));
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
sub RenderOSHelpPage {
my ($client, $params) = @_;
my $szDistro = $g{szDistro};
my $szWarning;
my $szCheckFile = '';
my $bCheckFilePresent;
my $szPluginPath;
my $szSBSUser;
my $szServerName;
#$g{log}->is_debug && $g{log}->debug('Called!'); # client == ' . ( eval{$client->name()} || 'no client' ) . ", param == $params");
$szPluginPath = $g{szAppPath};
#Get the SC/SBS username..(NA for windows & mac)
#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) {
# $szServerName = string('SQUEEZECENTER');
#} else {
# $szServerName = string('SQUEEZEBOX_SERVER');
#}
$szServerName = Plugins::SrvrPowerCtrl::Util::GetSCName();
$szSBSUser = $g{szSCUser};
# Unsupported OS?
if ( ! $hOSs->{"$g{szOS}"} ) {
$szDistro = $g{szOS} . ':' . $g{szDistro};
$szWarning = sprintf('<H3>' . string('PLUGIN_SRVRPOWERCTRL_UNSUPPOS_HELP_MSG') . '</H3>', $szDistro);
}
#Check to see if we're already enabled by looking for a helper file..
($bCheckFilePresent, $szCheckFile) = IsHelperUtilInstalled();
$g{log}->is_debug && $g{log}->debug("Checkfile $szCheckFile " . ($bCheckFilePresent ? ' found.' : ' not found.') );
#we don't worry about different versions of windows..
if ($g{szOS} eq 'win') {
if ( index( $params->{path}, 'Windows' ) == -1 ) {
$szDistro = $g{szOS} . ':' . $g{szDistro};
$szWarning = sprintf('<H3>' . string('PLUGIN_SRVRPOWERCTRL_WRONGDISTRO_HELP_MSG') . '</H3>', $szDistro);
$szPluginPath = '/path/for/example/only';
} elsif ($bCheckFilePresent) {
#Already set up..
$szWarning = '<p>' . string('PLUGIN_SRVRPOWERCTRL_ALREADYSETUPWIN_HELP_MSG') . '</p>';
$szWarning = sprintf($szWarning, $szCheckFile);
} else {
$szWarning = '<p>' . string('PLUGIN_SRVRPOWERCTRL_NEEDSSETUP_HELP_MSG') . '</p>';
}
} else {
#Check to see if the help file being loaded matches our OS..
my $n = index( $params->{path}, $g{szOS} );
$g{log}->is_debug && $g{log}->debug("index( $params->{path}, $g{szOS} ) == $n");
if ( index( $params->{path}, $g{szOS} ) == -1 ) {
$szDistro = $g{szOS} . ':' . $g{szDistro};
$szWarning = sprintf('<H3>' . string('PLUGIN_SRVRPOWERCTRL_WRONGDISTRO_HELP_MSG') . '</H3>', $szDistro);
$szPluginPath = '/path/for/example/only';
} elsif ($bCheckFilePresent) {
#Already set up..
$szWarning = '<p>' . string('PLUGIN_SRVRPOWERCTRL_ALREADYSETUP_HELP_MSG') . '</p>';
} else {
#Need to set up..
$szWarning = '<p>' . string('PLUGIN_SRVRPOWERCTRL_NEEDSSETUP_HELP_MSG') . '</p>';
}
}
#$g{log}->is_debug && $g{log}->debug("$message");
$params->{distro} = $g{szDistro}; # [% distro %]
$params->{warning} = $szWarning; # [% warning %]
$params->{sbsuser} = $szSBSUser; # [% sbsuser %]
$params->{sbsservername} = $szServerName; # [% sbsservername %]
$params->{pluginpath} = $szPluginPath; # [% pluginpath %]
$params->{foundfile} = $szCheckFile; # [% foundfile %]
#$g{log}->is_debug && $g{log}->debug('params == ' . Data::Dump::dump($params));
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
sub RenderLogPage {
my ($client, $params) = @_;
$g{log}->is_debug && $g{log}->debug('Called!'); # . " params == " . Data::Dump::dump($params));
$params->{srvrpowerctrl_logdata} = Plugins::SrvrPowerCtrl::Util::ListOurLogEntries(1);
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
sub RenderPrefsPage {
my ($client, $params) = @_;
$g{log}->is_debug && $g{log}->debug('Called!'); # . " params == " . Data::Dump::dump($params));
$params->{srvrpowerctrl_prefsdata} = Plugins::SrvrPowerCtrl::Settings::ListPrefs(1);
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
sub RenderLogInfoPage {
my ($client, $params) = @_;
#$g{log}->is_debug && $g{log}->debug('Called!'); # . " params == " . Data::Dump::dump($params));
$params->{srvrpowerctrl_urlsuffix} = time();
$params->{srvrpowerctrl_logLevel} = Plugins::SrvrPowerCtrl::Settings::GetCurrentLogLevel();
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
#Trouble-shooting help page
sub RenderTSHelpPage {
my ($client, $params) = @_;
my $szCacheDir;
my $szPluginsDir;
my $szSBSUser;
my $szServerName;
#$g{log}->is_debug && $g{log}->debug('Called!'); # client == ' . ( eval{$client->name()} || 'no client' ) . ", param == $params");
#Get the SC/SBS username..(NA for windows & mac)
#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) {
# $szServerName = string('SQUEEZECENTER');
#} else {
# $szServerName = string('SQUEEZEBOX_SERVER');
#}
$szServerName = Plugins::SrvrPowerCtrl::Util::GetSCName();
$szSBSUser = $g{szSCUser};
$szCacheDir = Slim::Utils::OSDetect::dirsFor('cache');
$szPluginsDir = Slim::Utils::OSDetect::dirsFor('plugins');
$params->{os} = $g{szOS};
$params->{distro} = $g{szDistro};
$params->{macaddress} = $g{szServerMAC};
$params->{cachedir} = $szCacheDir;
$params->{pluginsdir} = $szPluginsDir;
$params->{sbsuser} = $szSBSUser;
$params->{sbsservername} = $szServerName; # [% sbsservername %]
$params->{srvrpowerctrl_urlsuffix} = time();
#$g{log}->is_debug && $g{log}->debug('params == ' . Data::Dump::dump($params));
return Slim::Web::HTTP::filltemplatefile($params->{path}, $params);
}
1;
__END__