forked from brainfoolong/omxwebgui
/
index.php
193 lines (186 loc) · 8.84 KB
/
index.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
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
<?php
/**
* Simple Web GUI for Omxplayer on a Raspberry Pi
*
* @link https://github.com/brainfoolong/omxwebgui
* @author BrainFooLong
* @license GPL v3
*/
error_reporting(E_ALL);
ini_set("display_errors", 1);
set_time_limit(15);
if(version_compare(PHP_VERSION, "5.4", "<")) die("You need PHP 5.4 or higher");
header("X-UA-Compatible: IE=edge");
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
require(__DIR__."/lib/functions.php");
require(__DIR__."/lib/Translations.class.php");
require(__DIR__."/lib/OMX.class.php");
try{
$optionsFile = __DIR__."/options.json";
$options = file_exists($optionsFile) ? json_decode(file_get_contents($optionsFile), true) : array();
$folders = isset($options["folders"]) ? $options["folders"] : array();
Translations::$language = isset($options["language"]) ? $options["language"] : "en";
# json requests
if(isset($_GET["json"])){
$data = NULL;
switch($_GET["action"]){
case "get-status":
$data = array("status" => "stopped");
$output = $return = "";
exec('sh '.escapeshellcmd(__DIR__."/omx-status.sh"), $output, $return);
if($return){
$data["status"] = "playing";
if(file_exists(OMX::$fifoStatusFile)){
$json = json_decode(file_get_contents(OMX::$fifoStatusFile), true);
$data["path"] = $json["path"];
# view cache
$hash = getPathHash($data["path"]);
if(!isset($options["viewed"][$hash])) {
$options["viewed"][$hash] = true;
file_put_contents($optionsFile, json_encode($options));
}
}
}
break;
}
echo json_encode($data);
exit;
}
# ajax requests
if(isset($_POST["action"])){
switch($_POST["action"]){
# getting the filelist
case "get-filelist":
$files = array();
foreach($folders as $folder){
$files = array_merge($files, isStreamUrl($folder) ? array($folder) : getVideoFiles($folder));
}
foreach($files as $file){
$classes = array("file");
if(isset($options["viewed"][getPathHash($file)])) $classes[] = "viewed";
echo '<div class="'.implode(" ", $classes).'" data-path="'.$file.'"><span class="eye"></span> <span class="path">'.$file.'</span></div>';
}
exit;
break;
# save options
case "save-options":
$folders = explode("\n", trim($_POST["folders"]));
$error = false;
foreach($folders as $key => $folder){
$folder = trim(str_replace("\\", "/", $folder));
$folders[$key] = $folder;
if(isStreamUrl($folder)) continue;
if(!is_dir($folder) || !is_readable($folder) || !$folder || $folder == "/"){
$error = true;
unset($folders[$key]);
}
}
$options["folders"] = $folders;
$options = array_merge($options, $_POST["option"]);
file_put_contents($optionsFile, json_encode($options));
echo t("saved");
if($error) echo t("error.folders");
echo "<br/>".t("reload.page");
break;
# key/mouse click commands
case "shortcut":
$startCmd = escapeshellarg($_POST["path"])." ".(isset($options["speedfix"]) && $options["speedfix"] ? "1" : "0");
switch($_POST["shortcut"]){
case "start":
file_put_contents(OMX::$fifoStatusFile, json_encode(array("path" => $_POST["path"])));
OMX::sendCommand($startCmd, "start");
break;
case "p":
if(!file_exists(OMX::$fifoFile)){
OMX::sendCommand($startCmd, "start");
}else{
OMX::sendCommand("p", "pipe");
}
break;
default:
$key = OMX::$hotkeys[$_POST["shortcut"]];
OMX::sendCommand(isset($key["shortcut"]) ? $key["shortcut"] : $_POST["shortcut"], "pipe");
}
break;
}
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=670, initial-scale=0.8">
<link rel="stylesheet" type="text/css" href="css/site.css">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
var omxHotkeys = <?=json_encode(OMX::$hotkeys)?>;
var options = <?=json_encode($options)?>;
var language = <?=json_encode(Translations::$language)?>;
var translations = <?=json_encode(Translations::$translations)?>;
</script>
<script type="text/javascript" src="js/scripts.js"></script>
<title>OMX Web GUI - By BrainFooLong</title>
</head>
<body>
<div class="page">
<div class="ajax-error"><div></div></div>
<div class="ajax-success"><div></div></div>
<div class="header">
<a href="https://github.com/brainfoolong/omxwebgui" target="_blank"><img src="images/logo.png" alt="" class="logo"/></a>
<div class="box">
<form name="opt" method="post" action="">
<input type="hidden" name="action" value="save-options"/>
<p><?=nl2br(t("folders.desc"))?></p>
<textarea cols="45" rows="3" name="folders"><?=htmlentities(implode("\n", $folders))?></textarea><br/>
<?=t("ui.language")?>: <select name="option[language]">
<?php foreach(Translations::$languages as $lang => $label){
echo '<option value="'.$lang.'"';
if(isset($options["language"]) && $options["language"] == $lang) echo ' selected="selected"';
echo '>'.$label.'</option>';
}?>
</select><br/>
<?php displayYesNoOption("speedfix", $options) ?><br/>
<?php displayYesNoOption("autoplay-next", $options) ?><br/>
<br/>
<input type="button" class="action button" data-action="save-options" value="<?=t("save")?>"/>
</form>
</div>
</div>
<div class="files">
<div class="status-line"><b><?=t("status")?>:</b> <span id="status"><?=t("loading")?>...</span></div>
<div class="results">
<input type="text" class="search" value="<?=t("search.input")?>" autocomplete="off"/>
<div id="filelist"><?=t("loading")?>...</div>
</div>
</div>
<div class="omx-buttons">
<?php foreach(OMX::$hotkeys as $key => $value){
$keyValue = $key;
switch($key){
case "left": $keyValue = "←"; break;
case "right": $keyValue = "→"; break;
case "up": $keyValue = "↑"; break;
case "down": $keyValue = "↓"; break;
}
echo '<div class="button" data-action="shortcut" data-shortcut="'.$key.'"><span class="shortcut">'.$keyValue.'</span><span class="label">'.nl2br(t("shortcut-$key")).'</span></div>';
}?>
<div class="clear"></div>
</div>
<div class="footer">
Powered by BrainFooLong - Contribute on GitHub <a href="https://github.com/brainfoolong/omxwebgui" target="_blank">omxwebgui</a>
</div>
</div>
</body>
</html>
<?php
}catch(Exception $e){
header("HTTP/1.0 500 Internal Server Error");
echo $e->getMessage()."\n\n";
echo $e->getTraceAsString();
}