Advertisement
thenadz

PHP Find Ghostscript (cross-platform)

Jan 17th, 2014
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.07 KB | None | 0 0
  1. /**
  2.  * Checks whether we may call gs through exec().
  3.  *
  4.  * @return bool|str If available, returns exe path. False otherwise.
  5.  */
  6. private static function getGhostscriptExecutable() {
  7.    static $executable = null;
  8.  
  9.    if (is_null($executable)) {
  10.       // we must be able to exec()
  11.       $executable = self::isExecAvailable();
  12.       if (!$executable) return $executable;
  13.  
  14.       // find on Windows system
  15.       if ('WIN' === strtoupper(substr(PHP_OS, 0, 3))) {
  16.          // look for environment variable
  17.          $executable = getenv('GSC');
  18.          if($executable) return $executable;
  19.  
  20.          // hope GS in the path
  21.          $executable = exec('where gswin*c.exe');
  22.          if(!empty($executable)) return $executable;
  23.  
  24.          // look directly in filesystem
  25.          // 64- or 32-bit binary
  26.          $executable = exec('dir /o:n/s/b "C:\Program Files\gs\*gswin*c.exe"');
  27.          if (!empty($executable)) {
  28.             return $executable;
  29.          }
  30.  
  31.          // 32-bit binary on 64-bit OS
  32.          $executable = exec('dir /o:n/s/b "C:\Program Files (x86)\gs\*gswin32c.exe"');
  33.          $executable = empty($executable) ? false : $executable;
  34.          return $executable;
  35.       }
  36.  
  37.       // this is why I use Linux...
  38.       $executable = exec('which gs');
  39.       $executable = empty($executable) ? false : $executable;
  40.       return $executable;
  41.    }
  42.  
  43.    return $executable;
  44. }
  45.  
  46. /**
  47.  * Checks whether exec() may be used.
  48.  * Source: http://stackoverflow.com/a/12980534/866618
  49.  *
  50.  * @return bool Whether exec() is available.
  51.  */
  52. private static function isExecAvailable() {
  53.    static $available = null;
  54.  
  55.    if (is_null($available)) {
  56.       $available = true;
  57.  
  58.       if (ini_get('safe_mode')) {
  59.          $available = false;
  60.       } else {
  61.          $d = ini_get('disable_functions');
  62.          $s = ini_get('suhosin.executor.func.blacklist');
  63.          if ("$d$s") {
  64.             $array = preg_split('/,\s*/', "$d,$s");
  65.             if (in_array('exec', $array)) {
  66.                $available = false;
  67.             }
  68.          }
  69.       }
  70.    }
  71.  
  72.    return $available;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement