Guest User

simple fireTV remote control PHP script

a guest
Aug 28th, 2016
1,336
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?
  2.  
  3. /*
  4.  * simple Amazon FireTV remote for any system running php (webserver)
  5.  * needs: a PHP environment and adb installed
  6.  * date: 2016-08-28
  7.  * contact: heyer@linuxnutzer.de
  8.  * idea from: http://www.aftvnews.com/how-to-remotely-control-an-amazon-fire-tv-or-fire-tv-stick-via-adb/
  9.  *
  10.  * instructions:
  11.  * - install adb
  12.  * - copy this script to your web-directory (e.g. /home/user/public_html/firetv)
  13.  * - optional: place a .htaccess file allowing only local connections (e.g. /home/user/public_html/firetv/.htaccess)
  14.  * - browse & set your bookmarks to e.g. http://localhost/~user/firetv/index.php?p=supersecret
  15.  */
  16.  
  17.  
  18. /*
  19.  * configure this:
  20.  */
  21.  $default_password = "supersecret";
  22.  $default_ip = "192.168.1.26";
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31. /*
  32.  * do not touch this:
  33.  */
  34.  
  35.  
  36. // error_reporting(E_ALL);
  37.  error_reporting(E_ALL ^ E_NOTICE);
  38.  ini_set("display_errors", 1);
  39.  
  40.  
  41. // password
  42.  $pass = "";
  43.  if (isset($_POST['pass'])) {
  44.   $pass = $_POST['pass'];
  45.  }
  46.  
  47.  if (isset($_GET['p'])) {
  48.   $pass = $_GET['p'];
  49.  }
  50.  
  51.  $correct_password = FALSE;
  52.  if ($pass == $default_password) {
  53.   $correct_password = TRUE;
  54.  }
  55.  
  56.  
  57.  
  58.  
  59. // ip
  60.  $ip = $default_ip;
  61.  if (isset($_POST['ip'])) {
  62.   $ip = $_POST['ip'];
  63.  }
  64.  if (isset($_POST['ip_new'])) {
  65.   $ip = $_POST['ip_new'];
  66.  }
  67.  
  68.  
  69.  
  70.  
  71. //firetv commands as on http://developer.android.com/reference/android/view/KeyEvent.html
  72.  $firetv_command = array(
  73.      array("id"=>100,   "name"=>"CONNECT",  "value"=>0, "y"=>0, "x"=>1),
  74.      array("id"=>102,   "name"=>"START-SERVER", "value"=>0, "y"=>0, "x"=>0),
  75.  
  76.      array("id"=>1, "name"=>"/\\",      "value"=>19,    "y"=>2, "x"=>1),
  77.      array("id"=>2, "name"=>"<",        "value"=>21,    "y"=>3, "x"=>0),
  78.      array("id"=>3, "name"=>"O",    "value"=>66,    "y"=>3, "x"=>1),
  79.      array("id"=>4, "name"=>">",    "value"=>22,    "y"=>3, "x"=>2),
  80.      array("id"=>5, "name"=>"\\/",      "value"=>20,    "y"=>4, "x"=>1),
  81.      
  82.      array("id"=>6, "name"=>"BACK",     "value"=>04,    "y"=>6, "x"=>0),
  83.      array("id"=>7, "name"=>"HOME",     "value"=>03,    "y"=>6, "x"=>1),
  84.      array("id"=>8, "name"=>"MENU",     "value"=>01,    "y"=>6, "x"=>2),
  85.      
  86.      array("id"=>9, "name"=>"PREVIOUS", "value"=>88,        "y"=>7, "x"=>0),
  87.      array("id"=>10,    "name"=>"PLAY/PAUSE",   "value"=>85,    "y"=>7, "x"=>1),
  88.      array("id"=>11,    "name"=>"NEXT",     "value"=>87,        "y"=>7, "x"=>2),
  89.  
  90.      array("id"=>101,   "name"=>"DISCONNECT",   "value"=>0, "y"=>9, "x"=>1),
  91.      array("id"=>103,   "name"=>"STOP-SERVER",  "value"=>0, "y"=>9, "x"=>2)
  92.  );
  93.  
  94.  $user_command_id = "";
  95.  if (isset($_POST['u'])) {
  96.   $user_command_id = key($_POST['u']);  //  _debug($user_command_id);
  97.  
  98.   $user_command = 0;
  99.     foreach ((array) $firetv_command as $key => $val) {
  100.         if ( ($val['id']==$user_command_id)) {
  101.             $user_command   = $key;
  102.         }
  103.     }
  104.   _execute_command($firetv_command[$user_command], $ip);
  105.  }
  106.  
  107.  
  108.  
  109. ?>
  110.  
  111. <HTML>
  112.  
  113. <HEAD>
  114. <TITLE>fireTV remote</TITLE>
  115. </HEAD>
  116. <BODY BGCOLOR="#ffffff" TEXT="#000000" LINK="#000000" ALINK="#000000" VLINK="#000000" TOPMARGIN=0 LEFTMARGIN=0 MARGINWIDTH=0 MARGINHEIGHT=0>
  117. <CENTER><TABLE BORDER=0 WIDTH="100%" HEIGHT="100%" BGCOLOR="#ffffff"><TR><TD><CENTER>
  118. <CENTER><TABLE BORDER=0 BGCOLOR="#aaccff"><TR><TD><CENTER>
  119. <H1>fireTV remote</H1>
  120.  
  121. <?
  122.  if ( $correct_password == FALSE) {
  123.   echo "wrong password";
  124.   exit(1);
  125.  }
  126.  
  127.  echo "<FORM ACTION=\"".$_SERVER['PHP_SELF']."\" enctype=\"multipart/form-data\" method=\"POST\">\n";
  128.  echo "<input type=\"hidden\" name=\"pass\" value=\"".$pass."\">\n";
  129.  echo "<input type=\"hidden\" name=\"ip\" value=\"".$ip."\">\n";
  130.  echo "fireTV ip: <input type=\"input\" name=\"ip_new\" value=\"".$ip."\"><BR>\n";
  131.  
  132.  $output = shell_exec("adb connect ".$ip); _debug($output);
  133. // if you want you can list all connected devices:
  134. // $output = shell_exec("adb devices"); _debug($output);
  135.  
  136.  _show_grid($firetv_command);   // I know I should use CSS here, but I am oldsk00l
  137.  
  138. ?>
  139.  
  140. </FORM>
  141.  
  142. </CENTER></TD></TR></TABLE></CENTER>
  143. </CENTER></TD></TR></TABLE></CENTER>
  144. </BODY>
  145. </HTML>
  146.  
  147. <?
  148.  
  149.  
  150. function _execute_command (
  151.     $command,
  152.     $ip
  153. ) {
  154. //  _debug($command);
  155.  
  156. //http://php.net/manual/de/function.shell-exec.php
  157.    
  158.     switch ($command['id']) {
  159.         case 100:
  160.             $output = shell_exec("adb connect ".$ip);
  161.         //  _debug($output);
  162.             break;
  163.         case 101:
  164.             $output = shell_exec("adb disconnect ".$ip);
  165.         //  _debug($output);
  166.             break;
  167.         case 102:
  168.             $output = shell_exec("adb start-server");
  169.         //  _debug($output);
  170.             break;
  171.         case 103:
  172.             $output = shell_exec("adb kill-server");
  173.         //  _debug($output);
  174.             break;
  175.         case ($command['id']<100):
  176.         //  _debug('normal: '.$command['name']);
  177.             $output = shell_exec("adb connect ".$ip);
  178.             $output = shell_exec("adb shell input keyevent ".$command['value']);
  179.         //  _debug($output);
  180.             break;
  181.            
  182.         default:
  183.             _debug('unknown command');
  184.             break;
  185.  
  186.     }   // switch
  187.     return 0;
  188. }
  189.  
  190. function _debug (
  191.     $my_array
  192. ) {
  193.     echo "<pre>\n";
  194.     print_r($my_array);
  195.     echo "</pre>\n";
  196.     return 0;
  197. }
  198.  
  199.  
  200. function _show_grid (
  201.     $my_array
  202. ) {
  203.     $HEIGHT = 10;
  204.     $WIDTH = 3;
  205.  
  206.     echo "<TABLE BORDER=0 WIDTH=100% COLS=".$WIDTH." ROWS=".$HEIGHT.">\n";
  207.  
  208.     for ($y=0; $y<$HEIGHT; $y++) {
  209.         echo "<TR>\n";
  210.         for ($x=0; $x<$WIDTH; $x++) {
  211.             $value = "&nbsp;";
  212.             foreach ((array) $my_array as $key => $val) {
  213.                 if ( ($val['y']==$y) &&  ($val['x']==$x)) {
  214.                     $value = "<input type=\"submit\" name=\"u[".$val['id']."]\" value=\"".$val['name']."\">\n";
  215.                 }
  216.             }
  217.             echo "<TD><CENTER>".$value."</CENTER></TD>\n";
  218.         }   // for x
  219.         echo "</TR>\n";
  220.     }   // for y
  221.     echo "</TABLE>\n";
  222.     return 0;
  223. }
  224.  
  225. ?>
RAW Paste Data