shell() static public method

Execute a command inside a shell
static public shell ( $cmd, $input = null, $cwd = null, $env = null )
コード例 #1
0
ファイル: code-blocks.php プロジェクト: rsms/gitblog
 static function highlight($content, $lang, $extra_cssclass = null, $input_encoding = 'utf-8')
 {
     $cssclass = self::$conf['classname'];
     if (!$cssclass) {
         $cssclass = 'codeblock';
     }
     if ($extra_cssclass) {
         $cssclass .= ' ' . $extra_cssclass;
     }
     if (self::$previous_failure === true || $lang === 'text' || $lang === 'txt') {
         return self::dummy_block($content, $cssclass);
     }
     $cmd = self::$conf['pygmentize'] . ' ' . ($lang ? '-l ' . escapeshellarg($lang) : '-g') . ' -f html -O cssclass=' . escapeshellarg($cssclass) . ',encoding=' . $input_encoding . (self::$conf['tabsize'] ? ',tabsize=' . self::$conf['tabsize'] : '');
     $st = gb::shell($cmd, $content);
     if ($st === null || $st[0] !== 0 && strpos($st[2], 'command not found') !== false) {
         # probably no pygments installed.
         # remember failure in order to speed up subsequent calls.
         self::$previous_failure = true;
         gb::log(LOG_WARNING, 'unable to highlight code because %s can not be found', self::$conf['pygmentize']);
         return self::dummy_block($content, $cssclass);
     }
     # $st => array(int status, string out, string err)
     if ($st[0] !== 0) {
         if (strpos($st[2], 'guess_lexer') !== false) {
             gb::log(LOG_NOTICE, 'pygments failed to guess language');
         } else {
             gb::log(LOG_WARNING, 'pygments failed to highlight code: ' . $st[2]);
         }
         return self::dummy_block($content, $cssclass);
     }
     return $st[1];
 }
コード例 #2
0
ファイル: git.php プロジェクト: rsms/gitblog
 /** Execute a git command */
 static function exec($cmd, $input = null, $gitdir = null, $worktree = null, $allow_guess = false, $ignore_git_errors = false)
 {
     # build cmd
     if ($gitdir === null && !$allow_guess) {
         $gitdir = gb::$site_dir . '/.git';
     }
     if ($worktree === null && !$allow_guess) {
         $worktree = gb::$site_dir;
     }
     $cmd_prefix = 'git';
     if ($gitdir) {
         $cmd_prefix .= ' --git-dir=' . escapeshellarg($gitdir);
     }
     if ($worktree) {
         $cmd_prefix .= ' --work-tree=' . escapeshellarg($worktree);
     }
     $cmd = $cmd_prefix . ' ' . $cmd;
     #var_dump($cmd);
     gb::log(LOG_DEBUG, 'exec$ ' . $cmd);
     $r = gb::shell($cmd, $input, gb::$site_dir);
     git::$query_count++;
     # fail?
     if ($r === null) {
         return null;
     }
     # check for errors
     if ($r[0] != 0) {
         $msg = trim($r[1] . "\n" . $r[2]);
         if ($ignore_git_errors && strpos($msg, 'sh: ') !== 0) {
             return $msg;
         }
         if (strpos($r[2], 'Not a git repository') !== false) {
             throw new GitUninitializedRepoError($msg, $r[0], $cmd);
         } else {
             throw new GitError($msg, $r[0], $cmd);
         }
     }
     return $r[1];
 }