Skip to content

zimuyang/php-shlex

Repository files navigation

Shlex

Shlex is a PHP extension written in C. This extension implements the functionality of the shlex library in Python. In order to make users more familiar with the Shlex extension, the class implemented by the extension is basically the same as the python shlex library in terms of property and method names. The interface documentation is also modified from the python shlex library interface documentation.

The Shlex makes it easy to write lexical analyzers for simple syntaxes resembling that of the Unix shell. This will often be useful for writing minilanguages or for parsing quoted strings.

Table of contents

  1. Requirement
  2. Installation
  3. Functions
  4. Classes and methods

Requirement

  • PHP 7.0 +
  • Linux/OSX

Installation

Installation on Linux/OSX

phpize
./configure
make && make install

For windows

Windows system is currently not supported.

Functions

shlex_split

Split the string s using shell-like syntax.

Description
array shlex_split( string|resource|null $s [, bool $comments = false [, bool $posix = true ]] )
Parameters
s

    Split the string s using shell-like syntax.

Note:
Since the shlex_split() function instantiates a shlex instance, passing null for s will read the string to split from standard input.
comments

    If comments is false (the default), the parsing of comments in the given string will be disabled (setting the commenters attribute of the shlex instance to the empty string).

posix

    This function operates in POSIX mode by default, but uses non-POSIX mode if the posix argument is false.

Return Values

Returns an array of split strings.

Examples
<?php

$s = "foo#bar";
$ret = shlex_split($s, true);

var_dump($ret);

?>

The above example will output:

array(1) {
  [0] =>
  string(3) "foo"
}

shlex_quote

Return a shell-escaped version of the string s.

Description
string shlex_quote( string $s )
Parameters
s

    The string to be escaped.

Return Values

The returned value is a string that can safely be used as one token in a shell command line, for cases where you cannot use a list.

Examples
<?php

// If the output is executed, it will cause the index.php file to be deleted.
$filename = "somefile; rm -rf index.php";
$command = sprintf("ls -l %s", $filename);
echo $command;
echo "\n";

// shlex_quote() blocked the vulnerability
$command = sprintf("ls -l %s", shlex_quote($filename));
echo $command;
echo "\n";

// remote connection
$remoteCommand = sprintf("ssh home %s", shlex_quote($command));
echo $remoteCommand;
echo "\n";

?>

The above example will output:

ls -l somefile; rm -rf index.php
ls -l 'somefile; rm -rf index.php'
ssh home 'ls -l '"'"'somefile; rm -rf index.php'"'"''

Classes and methods

Shlex

Introduction

A Shlex instance or subclass instance is a lexical analyzer object.

Class synopsis
Shlex implements Iterator {
  
  /* Properties */
  public resource|null $instream = null;
    public string|null $infile = null;
    private bool|null $posix = null;
    public string|null $eof = null;
    public string $commenters = '#';
    public string $wordchars = 'abcdfeghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_';
    public string $whitespace = " \t\r\n";
    public bool $whitespaceSplit = false;
    public string $quotes = '\'"';
    public string $escape = '\\';
    public string $escapedquotes = '"';
    private string $state = ' ';
    private array $pushback = [];
    public int $lineno = 1;
    public int $debug = 0;
    public string $token = '';
    private array $filestack = [];
    public string|null $source = null;
    public string|null $punctuationChars = null;
    private array|null $_punctuationChars = null;
  
  /* Methods */
  public void function __construct( [ string|resource|null $instream = null [, string|null $infile = null [, bool $posix = false [, string|bool|null $punctuationChars = false ]]]]);

    public void function __destruct( void );

    public void function key( void );
    
    public void function next( void );
    
    public void function rewind( void );

    public string|null function current( void );

    public bool function valid( void );

    public void function pushToken( string $tok );

    public void function pushSource( string|resource $newstream, string|null $newfile = null );

    public void function popSource( void );

    public string|null|ShlexException function getToken( void );
    
    public string|null|ShlexException function readToken( void );
    
    public array function sourcehook( string $newfile );
    
    public string function errorLeader( string $infile = null, int|null $lineno = null );
}

Properties

instream

    The input stream from which this Shlex instance is reading characters.

infile

    The name of the current input file, as initially set at class instantiation time or stacked by later source requests. It may be useful to examine this when constructing error messages.

eof

    Token used to determine end of file. This will be set to the empty string (''), in non-POSIX mode, and to null in POSIX mode.

commenters

    The string of characters that are recognized as comment beginners. All characters from the comment beginner to end of line are ignored. Includes just '#' by default.

wordchars

    The string of characters that will accumulate into multi-character tokens. By default, includes all ASCII alphanumerics and underscore. In POSIX mode, the accented characters in the Latin-1 set are also included. If punctuationChars is not empty, the characters ~-./*?=, which can appear in filename specifications and command line parameters, will also be included in this attribute, and any characters which appear in punctuationChars will be removed from wordchars if they are present there.

whitespace

    Characters that will be considered whitespace and skipped. Whitespace bounds tokens. By default, includes space, tab, linefeed and carriage-return.

whitespaceSplit

    If true, tokens will only be split in whitespaces. This is useful, for example, for parsing command lines with Shlex, getting tokens in a similar way to shell arguments. If this attribute is true, punctuationChars will have no effect, and splitting will happen only on whitespaces. When using punctuationChars, which is intended to provide parsing closer to that implemented by shells, it is advisable to leave whitespaceSplit as false (the default value).

quotes

    Characters that will be considered string quotes. The token accumulates until the same quote is encountered again (thus, different quote types protect each other as in the shell.) By default, includes ASCII single and double quotes.

escape

    Characters that will be considered as escape. This will be only used in POSIX mode, and includes just '' by default.

escapedquotes

    Characters in quotes that will interpret escape characters defined in escape. This is only used in POSIX mode, and includes just '"' by default.

lineno

    Source line number (count of newlines seen so far plus one).

debug

    If this attribute is numeric and 1 or more, a Shlex instance will print verbose progress output on its behavior. If you need to use this, you can read the module source code to learn the details.

token

    The token buffer. It may be useful to examine this when catching exceptions.

source

    This attribute is null by default. If you assign a string to it, that string will be recognized as a lexical-level inclusion request similar to the source keyword in various shells. That is, the immediately following token will be opened as a filename and input will be taken from that stream until EOF, at which point the fclose() method of that stream will be called and the input source will again become the original input stream. Source requests may be stacked any number of levels deep.

punctuationChars

    Characters that will be considered punctuation. Runs of punctuation characters will be returned as a single token. However, note that no semantic validity checking will be performed: for example, ‘>>>’ could be returned as a token, even though it may not be recognised as such by shells.

Methods

Shlex::__construct

Constructor

Description
public void function Shlex::__construct( [ string|resource|null $instream = null [, string|null $infile = null [, bool $posix = false [, string|bool|null $punctuationChars = false ]]]])
Parameters
instream

    The instream argument, if present, specifies where to read characters from. It must be a resource type variable (can be read by fread( )), or a string. If no argument is given, input will be taken from php://stdin.

infile

     The second optional argument is a filename string, which sets the initial value of the infile attribute. If the instream argument is null, then this infile argument is always null.

posix

    The posix argument defines the operational mode: when posix is false (default), the Shlex instance will operate in compatibility mode. When operating in POSIX mode, Shlex will try to be as close as possible to the POSIX shell parsing rules.

punctuationChars

    The punctuationChars argument provides a way to make the behaviour even closer to how real shells parse. This can take a number of values: the default value, false. If set to true, then parsing of the characters ();<>|& is changed: any run of these characters (considered punctuation characters) is returned as a single token. If set to a non-empty string of characters, those characters will be used as the punctuation characters. Any characters in the wordchars attribute that appear in punctuationChars will be removed from wordchars.

Return Values

No value is returned.

Examples
<?php

$instance = new Shlex("a && b || c", null, false, "|");

$list = [];

foreach ($instance as $value) {
  $list[] = $value;
}

var_dump($list);

?>

The above example will output:

array(6) {
  [0] =>
  string(1) "a"
  [1] =>
  string(1) "&"
  [2] =>
  string(1) "&"
  [3] =>
  string(1) "b"
  [4] =>
  string(2) "||"
  [5] =>
  string(1) "c"
}

Shlex::__destruct

Destructor

Description
public void function Shlex::__destruct( void )

Used to release resource objects held by Shlex objects. Internally, fclose( ) is called to close the file handle.

Parameters

No parameters.

Return Values

No value is returned.

Examples

No examples.


Shlex::key

There is no practical use for the key method of the Iterator interface.

Description
public void function Shlex::key( void )
Parameters

No parameters.

Return Values

No value is returned.

Examples

No examples.


Shlex::next

There is no practical use for the next method of the Iterator interface.

Description
public void function Shlex::next( void )
Parameters

No parameters.

Return Values

No value is returned.

Examples

No examples.


Shlex::rewind

There is no practical use for the rewind method of the Iterator interface.

Description
public void function Shlex::rewind( void )
Parameters

No parameters.

Return Values

No value is returned.

Examples

No examples.


Shlex::current

Returns the token value read by Shlex this iteration.

Description
public string|null function Shlex::current( void )
Parameters

No parameters.

Return Values

Returns the token value read by Shlex this iteration.

Examples

No examples.


Shlex::valid

Determine if this iteration is valid.

Description
public bool function Shlex::valid( void )
Parameters

No parameters.

Return Values

Valid if true is returned, false is invalid.

Note:
Due to the implementation of this class, iteratively reading the next element is also called inside the method. So the next() method is invalid.
Examples

No examples.


Shlex::pushToken

Push the argument onto the token stack.

Description
public void function Shlex::pushToken( string $tok )
Parameters
tok

    The parameter being pushed.

Return Values

No value is returned.

Examples

No examples.


Shlex::pushSource

Push an input source stream onto the input stack.

Description
public void function Shlex::pushSource( string|resource $newstream, string|null $newfile = null );
Parameters
newstream

    The input source stream being pushed.

newfile

    If the filename argument is specified it will later be available for use in error messages. This is the same method used internally by the sourcehook() method.

Return Values

No value is returned.

Examples

No examples.


Shlex::popSource

Pop the last-pushed input source from the input stack. This is the same method used internally when the lexer reaches EOF on a stacked input stream.

Description
public void function Shlex::popSource( void )
Parameters

No parameters.

Return Values

No value is returned.

Examples

No examples.


Shlex::getToken

Return a token.

Description
public string|null|ShlexException function Shlex::getToken( void )
Parameters

No parameters.

Return Values

If tokens have been stacked using pushToken(), pop a token off the stack. Otherwise, read one from the input stream. If reading encounters an immediate end-of-file, eof is returned (the empty string ('') in non-POSIX mode, and null in POSIX mode).

Examples

No examples.


Shlex::readToken

Read a raw token.

Description
public string|null|ShlexException function Shlex::readToken( void )

Read a raw token. Ignore the pushback stack, and do not interpret source requests. (This is not ordinarily a useful entry point, and is documented here only for the sake of completeness.)

Parameters

No parameters.

Return Values

Return a raw token.

Examples

No examples.


Shlex::sourcehook

Description
public array function Shlex::sourcehook( string $newfile )

When Shlex detects a source request (see source below) this method is given the following token as argument, and expected to return a array of a filename and an open file-like object.

Normally, this method first strips any quotes off the argument. If the result is an absolute pathname, or there was no previous source request in effect, or the previous source was a stream (such as php://stdin), the result is left alone. Otherwise, if the result is a relative pathname, the directory part of the name of the file immediately before it on the source inclusion stack is prepended (this behavior is like the way the C preprocessor handles #include "file.h").

The result of the manipulations is treated as a filename, and returned as the first component of the tuple, with fopen() called on it to yield the second component. (Note: this is the reverse of the order of arguments in instance initialization!)

This hook is exposed so that you can use it to implement directory search paths, addition of file extensions, and other namespace hacks. There is no corresponding ‘close’ hook, but a shlex instance will call the fclose() method of the sourced input stream when it returns EOF.

For more explicit control of source stacking, use the pushSource() and popSource() methods.

Parameters
newfile

    file path.

Return Values

Return a array of a filename and an open file-like object.

Examples

No examples.


Shlex::errorLeader

Return an error message leader in the format of a Unix C compiler error label.

Description
public string function Shlex::errorLeader( string $infile = null, int|null $lineno = null )

This method generates an error message leader in the format of a Unix C compiler error label; the format is '"%s", line %d: ', where the %s is replaced with the name of the current source file and the %d with the current input line number (the optional arguments can be used to override these).

This convenience is provided to encourage Shlex users to generate error messages in the standard, parseable format understood by Emacs and other Unix tools.

Parameters
infile

    The name of the current source file.

lineno

    The current input line number.

Return Values

Return an error message leader in the format of a Unix C compiler error label.

Examples

No examples.


ShlexException

Shlex's exception class

Introduction

This class is primarily used for exceptions thrown when the Shlex class internally performs an error.

Class synopsis
ShlexException extends Exception {}
Examples
<?php

  throw new ShlexException('No escaped character');

?>