title: execute program in php
body: <p>Is there a way to execute a program on the server, Example: Firefox, Gimp, Etc.</p>
  
  <p>My concept is to be able to log into my local LAMP server on my phone, type in a command on a text box (eg: /usr/bin/firefox) and press a button which sends the command string using the post method to another PHP script that starts the program on my server. This would be good so when I'm on the road and want to start "firefox <a href="http://www.blahblah.net" rel="nofollow">http://www.blahblah.net</a>" and come back to my computer with firefox opened up. Yes, I am aware of the security issues but it is just a proof-of-concept.</p>
  
  <p>I've tried the exec() and system() commands but they don't seem to work.... Am I doing something wrong?</p>
  
  <pre><code>#!/usr/bin/env ruby
  
  require 'rubygems'
  require 'sinatra'
  
  disable :protection # not needed on something this simple
  set :port, 11111    # think 1APPX
  
  get '/' do
  # this handles both apps (via ?app=...) and files (via ?file=...)
  
  if params[:app] then
      # as a side effect, this also happens to actually run the app, which is
      # pretty much what we wanted in the first place
      @fn = params[:app][1..-2];
      `/usr/bin/env #{@fn}`
  elsif params[:file] then
      # bugfix, remove quotes...
      @fn = params[:file][1..-2];
      # xdg-open anyone?
      puts "DEBUG /usr/bin/env xdg-open #{@fn}";
      `"/usr/bin/env xdg-open #{@fn}"`
  else
      # nothing...
      404
  end
  
  end
  
  not_found do
  status 404
  "Application #{@fn} not found. Usage: /?app=\"[appname]\" or /?file=\"[filename]\""
  end
  </code></pre>