Maslosoft Whitelist API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
namespace Maslosoft\Whitelist\Tokenizer;
use Maslosoft\Whitelist\Interfaces\TokenInterface;
use Maslosoft\Whitelist\Tokenizer\Composite\FunctionCall;
use Maslosoft\Whitelist\Tokenizer\Tokens\SimpleToken;
use Maslosoft\Whitelist\Tokenizer\Tokens\Token;
/**
* Tokenizer
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class Tokenizer
{
private $rawTokens = [];
/**
*
* @var TokenInterface[]
*/
private $tokens = [];
public function __construct($code)
{
$this->rawTokens = token_get_all($code);
foreach ($this->rawTokens as $index => $token)
{
if (is_array($token))
{
$this->tokens[$index] = new Token($token, $this->tokens, $index);
}
else
{
$this->tokens[$index] = new SimpleToken($token, $this->tokens, $index);
}
$this->tokens[$index]->name = token_name($this->tokens[$index]->type);
}
}
/**
*
* @return TokenInterface[];
*/
public function getTokens()
{
return $this->tokens;
}
/**
* Get function tokens. This includes:
*
* * Simple functions
* * eval
* * echo
* * print
*
* This does not include:
*
* * Method calls
* * Variable name function calls
* * Requires and includes
*
* @return TokenInterface[]
*/
public function getFunctions()
{
$result = [];
foreach ($this->tokens as $index => $token)
{
// Check for eval first
if ($token->is(T_EVAL))
{
$result[] = $token;
continue;
}
if ($token->is(T_ECHO))
{
$result[] = $token;
continue;
}
if ($token->is(T_PRINT))
{
$result[] = $token;
continue;
}
// Looks not looks like a function then skip
if (!$token->valIs('('))
{
continue;
}
// Get actual token rather than `(`
$token = $token->prev();
// If not string or variable, might be control operator, skip
// Variable might be used as variable function name, ie $name()
// This could be also `]` when it's array member
if ($token->not(T_STRING) && $token->not(T_VARIABLE) && !$token->valIs(']'))
{
continue;
}
// Get token before function name
$prev = $token->prev();
// Check if is simple function
// By checking what's before name
// Exclude array members calls here, as it is special case
if (
$prev->not(T_DOUBLE_COLON) &&
$prev->not(T_NEW) &&
$prev->not(T_OBJECT_OPERATOR) &&
!$token->valIs(']')
)
{
$result[] = $token;
continue;
}
// Check for array member call
if ($token->valIs(']'))
{
$arr = $token;
$isClosed = false;
$name = [];
// Search back to find variable name of this array
// And whole expression value
// Need to check for `[$variable]` too
//
// If could not find variablie, bogus code? Break loop.
// This would be code like only `]()`
while ($arr->not(TokenInterface::TypeEmpty))
{
$name[] = $arr->value;
if ($arr->valIs(']'))
{
$isClosed = false;
}
if ($arr->valIs('['))
{
$isClosed = true;
}
$arr = $arr->prev();
if ($arr->is(T_VARIABLE) && $isClosed)
{
$name[] = $arr->value;
break;
}
}
$tmp = implode('', array_reverse($name));
$result[] = new FunctionCall($tmp, $this->tokens, $index);
}
}
return $result;
}
}
API documentation generated by ApiGen