Starting XBMC via HTTP
I have the Yatse Android remote for my XBMC server, but I failed to find a start button there.
Leveraging the apache server runing on the XBMC machine, I created a web page that allows to stop/start XBMC by displaying and ON/OFF button:
<?php if ( (0 === strpos($_SERVER['REMOTE_ADDR'], '192.168.')) || (0 === strpos($_SERVER['REMOTE_ADDR'], '127.0.0.')) ) { } else { die(""); } $xbmc_start_cmd = "sudo /usr/local/bin/xbmc-start-as-jc"; $xbmc_stop_cmd = "sudo /usr/local/bin/xbmc-stop-as-jc"; if ($_GET["action"]!= null && $_GET["action"]=="stop") { shell_exec($xbmc_stop_cmd); header("Refresh: 5"); } else if ($_GET["action"]!= null && $_GET["action"]=="start") { shell_exec($xbmc_start_cmd); }; header('Content-Type: text/html; charset=utf-8'); ?> <html> <style> #xbmc_start { background-color:#5F8; color:#080; border-color:#080; } .xbmc_control { width:50%; height:100px; font-size:50px; border-width:5px; margin-top:50px; } #xbmc_stop { background-color:#F58; color:#800; border-color:#800; } </style> <body> <center> <?php $url=(!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; if (strlen(shell_exec('pidof xbmc.bin'))>0) { #echo "xbmc is RUNNING"; ?> <FORM method="get"> <input type="hidden" name="action" value="stop"> <input type="submit" class="xbmc_control" id="xbmc_stop" value="stopper XBMC" /> </form> <? } else { #echo "xbmc is OFF"; ?> <FORM method="get"> <input type="hidden" name="action" value="start"> <input type="submit" class="xbmc_control" id="xbmc_start" value="démarrer XBMC" /> </form> <? } ?> </center> </body> </html>
There is nothing too fancy about the page, however I struggled to get the right configuration to get xbmc started as the right user. Indeed, my linux machine has constantly a X session opened, but not as the same user as the one apache runs as.
Instead of having the php script call the xbmc binaries directly, I wrapped it into a script launched via sudo
: /usr/local/bin/xbmc-start-as-jc
. The content is the following:
#!/bin/bash export DISPLAY=:0.0 if pidof xbmc.bin then echo "XBMC is already running" else /bin/su - jc -c "nohup /usr/bin/xbmc > /dev/null 2>&1 &" fi
You can see that the script is using the su
command to switch the user it runs as.
You need to make sure that the apache user has the priviledge to run this script as sudo. In /etc/sudoers.d
:
www-data ALL=NOPASSWD:/usr/local/bin/xbmc-start-as-jc
One could also directly give the priviledge to the apache user to launch su
via sudo
, but this is a security risk I don’t want to take since the HTTP server is accessible from the outside : an exploit could be rather damaging…