PHP Classes

File: src/PHPVideoToolkit/ProcessUtils.php

Recommend this page to a friend!
  Classes of Oliver Lillie   PHP Video Toolkit   src/PHPVideoToolkit/ProcessUtils.php   Download  
File: src/PHPVideoToolkit/ProcessUtils.php
Role: Class source
Content type: text/plain
Description: Class source
Class: PHP Video Toolkit
Manipulate and convert videos with ffmpeg program
Author: By
Last change:
Date: 1 year ago
Size: 2,150 bytes
 

Contents

Class file image Download
<?php

namespace PHPVideoToolkit;

/**
 * ProcessUtils is a bunch of utility methods taken from Symphony.
 *
 * This class contains static methods only and is not meant to be instantiated.
 *
 * @author Martin HasoĊˆ <martin.hason@gmail.com>
 */
class ProcessUtils
{
   
/**
     * This class should not be instantiated.
     */
   
private function __construct()
    {
    }

   
/**
     * Escapes a string to be used as a shell argument.
     *
     * @param string $argument The argument that will be escaped
     *
     * @return string The escaped argument
     */
   
public static function escapeArgument($argument)
    {
       
//Fix for PHP bug #43784 escapeshellarg removes % from given string
        //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
        //@see https://bugs.php.net/bug.php?id=43784
        //@see https://bugs.php.net/bug.php?id=49446
       
if ('\\' === DIRECTORY_SEPARATOR) {
            if (
'' === $argument) {
                return
escapeshellarg($argument);
            }

           
$escapedArgument = '';
           
$quote = false;
            foreach (
preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
                if (
'"' === $part) {
                   
$escapedArgument .= '\\"';
                } elseif (
self::isSurroundedBy($part, '%')) {
                   
// Avoid environment variable expansion
                   
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
                } else {
                   
// escape trailing backslash
                   
if ('\\' === substr($part, -1)) {
                       
$part .= '\\';
                    }
                   
$quote = true;
                   
$escapedArgument .= $part;
                }
            }
            if (
$quote) {
               
$escapedArgument = '"'.$escapedArgument.'"';
            }

            return
$escapedArgument;
        }

        return
escapeshellarg($argument);
    }

    private static function
isSurroundedBy($arg, $char)
    {
        return
2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];
    }
}