System_Launcher
[ class tree: System_Launcher ] [ index: System_Launcher ] [ all elements ]

Source for file Launcher.php

Documentation is available at Launcher.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Launch files with the associated application.
  7.  *
  8.  * usage:
  9.  *   require_once 'System/Launcher.php';
  10.  *   $launcher = new System_Launcher;
  11.  *   $launcher->launch('/data/docs/index.html', true);
  12.  *
  13.  *   Commands
  14.  *   --------
  15.  *   Windows:        start <filename>
  16.  *   Linux
  17.  *       KDE         kfmclient exec <filename>
  18.  *       Portland    xdg-open <filename>
  19.  *       Gnome       gnome-open <filename>
  20.  *   Mac OSX         open <filename>
  21.  *
  22.  * PHP version 5
  23.  * 
  24.  * @category System
  25.  * @package  System_Launcher
  26.  * @author   Christian Weiske <cweiske@php.net>
  27.  * @author   Olle Jonsson <olle.jonsson@gmail.com>
  28.  * @license  http://www.gnu.org/licenses/lgpl.html LGPL
  29.  * @link     http://github.com/olleolleolle/System_Launcher
  30.  * @since    File available since Release 0.1.0
  31.  */
  32.  
  33. /** Exception */
  34. require_once 'Launcher/Exception.php';
  35. /** Driver */
  36. require_once 'Launcher/Driver.php';
  37. /** Driver for GNOME */
  38. require_once 'Launcher/Driver/GNOME.php';
  39. /** Driver for KDE */
  40. require_once 'Launcher/Driver/KDE.php';
  41. /** Driver for Mac */
  42. require_once 'Launcher/Driver/Mac.php';
  43. /** Driver for Portland */
  44. require_once 'Launcher/Driver/Portland.php';
  45. /** Driver for Windows */
  46. require_once 'Launcher/Driver/Windows.php';
  47.  
  48. /**
  49.  * Launches files with the associated application.
  50.  *
  51.  * @category  System
  52.  * @package   System_Launcher
  53.  * @author    Christian Weiske <cweiske@php.net>
  54.  * @author    Olle Jonsson <olle.jonsson@gmail.com>
  55.  * @copyright 1997-2005 Christian Weiske
  56.  * @license   http://www.gnu.org/licenses/lgpl.html LGPL
  57.  * @version   Release: 0.5.1
  58.  * @link      http://github.com/olleolleolle/System_Launcher
  59.  */
  60. {
  61.     /**
  62.     * The driver for detected operating system.
  63.     * @var object 
  64.     */
  65.     protected $os;
  66.  
  67.     /**
  68.     * Ordered list of drivers to test.
  69.     * @var array 
  70.     */
  71.     protected $drivers;
  72.  
  73.     /**
  74.     * Command output string
  75.     * @var string 
  76.     */
  77.     protected $lastOutput;
  78.     /**
  79.      * Sets up a list of operating system checkers.
  80.      *
  81.      * @param array $drivers List of System_Launcher_Driver checkers.
  82.      */
  83.     public function __construct($drivers=null)
  84.     {
  85.         if ($drivers === null{
  86.             $this->drivers = array(
  87.                 new System_Launcher_Driver_Windows,
  88.                 new System_Launcher_Driver_Portland,
  89.                 new System_Launcher_Driver_KDE,
  90.                 new System_Launcher_Driver_GNOME,
  91.                 new System_Launcher_Driver_Mac
  92.             );
  93.         else {
  94.             $this->drivers = $drivers;
  95.         }
  96.     }
  97.  
  98.     /**
  99.      * Tries to detect the current operating system and set its driver.
  100.      *
  101.      * @return void 
  102.      */
  103.     protected function detectOS()
  104.     {
  105.         foreach ($this->drivers as $driver{
  106.             if ($driver->applies()) {
  107.                 $this->os = $driver;
  108.                 return;
  109.             }
  110.         }
  111.         throw new System_Launcher_Exception("Unsupported OS.");
  112.     }
  113.  
  114.     /**
  115.      * Returns the appropriate command to launch the given file name,
  116.      * depending on the operating system and the desktop environment.
  117.      *
  118.      * @param boolean $runInBackground True if the application should be run in the
  119.      *    background
  120.      *
  121.      *   @return string    The command to execute
  122.      */
  123.     protected function getCommand($runInBackground)
  124.     {
  125.         $this->detectOS();
  126.         $commandTemplate $this->os->getCommand($runInBackground);
  127.         if ($commandTemplate === ''{
  128.             throw new System_Launcher_Exception('Command must not be empty.');
  129.         }
  130.         return $commandTemplate;
  131.     }
  132.  
  133.     /**
  134.      * Launches a file or URL with its associated application.
  135.      *
  136.      * @param string  $fileName        The file or URL to open
  137.      * @param boolean $runInBackground True if the application should be run in the
  138.      *  background
  139.      *
  140.      * @return boolean   True if all was ok, false if there has been a problem
  141.      */
  142.     public function launch($fileName$runInBackground=false)
  143.     {
  144.         $command sprintf(
  145.             $this->getCommand($runInBackground)$this->cleanFileName($fileName)
  146.         );
  147.         exec($command$this->lastOutput$status);
  148.         return $status === 0;
  149.     }
  150.  
  151.     /**
  152.      * Removes initial dashes to avoid injection of command parameters.
  153.      *
  154.      * @param string $fileName Possibly malicious filename
  155.      *
  156.      * @return string Cleaned filename
  157.      */
  158.     protected function cleanFilename($fileName)
  159.     {
  160.         $fileName preg_replace('%^-+%'''$fileName);
  161.         return escapeshellarg($fileName);
  162.     }
  163.  
  164.     /**
  165.      * Launches a file or URL in the background.
  166.      *
  167.      * @param string $fileName The file or URL to open
  168.      *
  169.      * @return boolean True if all was ok, false if there has been a problem
  170.      */
  171.     public function launchInBackground($fileName)
  172.     {
  173.         return $this->launch($fileNametrue);
  174.     }
  175.  
  176.  
  177.     /**
  178.      * Returns last command output as lines of text.
  179.      *
  180.      * @return array 
  181.      */
  182.     public function getLastOutput()
  183.     {
  184.         return $this->lastOutput;
  185.     }
  186.  
  187.  
  188. }

Documentation generated on Wed, 02 Jan 2013 22:00:02 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.