-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path50-unmenu-user_scripts.cgi
137 lines (125 loc) · 4.19 KB
/
50-unmenu-user_scripts.cgi
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
#ADD_ON_MENU User Scripts
#ADD_ON_URL user_scripts
#ADD_ON_VERSION .1 - Proof of concept version
#ADD_ON_VERSION .2 - Added unmenu_local.conf file
#ADD_ON_VERSION .3 - Fixed to allow single quotes in button labels
#ADD_ON_VERSION .4 - Added conditional tests to allow for dynamic button labels.
#ADD_ON_VERSION .5 - Added css for submit button - mvzdwaan
#UNMENU_RELEASE $Revision$ $Date$
function ReloadPage() {
echo "<script language=\"JavaScript\">"
echo "<!--"
echo "var sURL = unescape(window.location.pathname);"
echo "var p = sURL.indexOf('?');"
echo "sURL = sURL.substring(p);"
echo ""
echo "setTimeout( \"refresh()\", $1*1000 );"
echo ""
echo "function refresh()"
echo "{"
echo " window.location.href = sURL;"
echo "}"
echo "//-->"
echo "</script>"
echo ""
echo "<script language=\"JavaScript1.1\">"
echo "<!--"
echo " function refresh()"
echo "{"
echo " window.location.replace( sURL );"
echo "}"
echo "//-->"
echo "</script>"
}
if [ "$ScriptDirectory" = "" ]
then
ScriptDirectory="."
fi
if [ "$ConfigFile" = "" ]
then
ConfigFile="unmenu.conf"
fi
# Local config file variables will never be overwritten by a distributd file.
# It is here where local changes should be made without worry about them being
# lost when a new unmenu.conf file is distributed.
if [ "$LocalConfigFile" = "" ]
then
LocalConfigFile="unmenu_local.conf"
fi
# user scripts are normally in the $ScriptDirectory, but this directory can be
# overridden by a value in the ConfigFile.
UserScriptDir=`grep "^UserScriptDirectory" "$ScriptDirectory/$ConfigFile" 2>/dev/null | sed "s/=/ /" | cut -d" " -f2-`
if [ "$UserScriptDir" = "" ]
then
UserScriptDir="$ScriptDirectory"
fi
LocUserScriptDir=`grep "^UserScriptDirectory" "$ScriptDirectory/$LocalConfigFile" 2>/dev/null | sed "s/=/ /" | cut -d" " -f2-`
if [ "$LocUserScriptDir" != "" ]
then
UserScriptDir=$LocUserScriptDir
fi
PACKAGE_DIRECTORY=`grep "^PACKAGE_DIRECTORY" "$ScriptDirectory/$LocalConfigFile" 2>/dev/null | sed "s/ //g" | cut -d"=" -f2`
if [ "$PACKAGE_DIRECTORY" = "" ]
then
PACKAGE_DIRECTORY=`grep "^PACKAGE_DIRECTORY" "$ScriptDirectory/$ConfigFile" | sed "s/ //g" | cut -d"=" -f2`
fi
export PACKAGE_DIRECTORY
#echo "Importing user-scripts from ${UserScriptDir}/" >&2
c=0
scripts=`ls "$UserScriptDir"/[0-9]*unmenu_user_script* 2>/dev/null`
for a in $scripts
do
[ ! -x "$a" ] && continue;
let c=c+1
script[$c]="$a"
label[$c]=`grep "^#define USER_SCRIPT_LABEL" "$a" | cut -d" " -f3-`
descr[$c]=`grep "^#define USER_SCRIPT_DESCR" "$a" | cut -d" " -f3-`
conditional_test[$c]=`grep "^#define USER_SCRIPT_TEST" "$a" | cut -d" " -f3-`
reload_page[$c]=`grep "^#define USER_SCRIPT_RELOAD" "$a" | cut -d" " -f3-`
#echo "imported ${label[$c]} from ${script[$c]} ${descr[$c]}<br>"
done
PATH=$PATH:.
export PATH
# Now for some HTML, draw the buttons
echo "<hr><form method=GET><table>"
i=1
while test $i -le $c
do
if [ "${conditional_test[$i]}" != "" ]
then
new_label=`sh -c "${conditional_test[$i]}" 2>&1`
#echo "x${conditional_test[$i]}x ${label[$i]} = x${new_label}x" "<br>"
label[$i]="$new_label"
fi
if [ "${label[$i]}" != "" -a "${label[$i]}" != " " -a "${descr[$i]}" != "" ]
then
echo "<tr><td><input class=\"user_script_button\" type=submit value=\"${label[$i]}\" name=\"command\"></td><td>${descr[$i]}</td></tr>"
fi
let i=i+1
done
echo "</table></form><hr>"
# now, determine if the user pressed a button submitting a form
# this simple parse of $3 looks for only one parameter.
# It will be the label on the button pushed.
cgi_params=${3#*\?}
command=${3#*\=}
command=`echo "$command" | tr "+" " " | sed "s/%27/'/g"|sed "s/%2F/\//g"`
if [ "$command" != "" ]
then
i=1
while test $i -le $c
do
# echo "matching x${command}x against y${label[$i]}y <br>"
if [ "$command" = "${label[$i]}" ]
then
# here we execute the script and capture its output to be sent to the browser
${script[$i]} 2>&1
if [ "${reload_page[$i]}" != "" ]
then
ReloadPage ${reload_page[$i]}
fi
break;
fi
let i=i+1
done
fi