Edit File by line
/home/barbar84/www/wp-conte.../plugins/worker/src/MWP/Parser
File: DefinitionTokenizer.php
<?php
[0] Fix | Delete
/*
[1] Fix | Delete
* This file is part of the ManageWP Worker plugin.
[2] Fix | Delete
*
[3] Fix | Delete
* (c) ManageWP LLC <contact@managewp.com>
[4] Fix | Delete
*
[5] Fix | Delete
* For the full copyright and license information, please view the LICENSE
[6] Fix | Delete
* file that was distributed with this source code.
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* A simple parser able to extract 'define' calls with their first constant string, from a PHP source code.
[11] Fix | Delete
* Helpful for configuration reading.
[12] Fix | Delete
*/
[13] Fix | Delete
class MWP_Parser_DefinitionTokenizer
[14] Fix | Delete
{
[15] Fix | Delete
/**
[16] Fix | Delete
* Returns an array of possible 'defined' values.
[17] Fix | Delete
*
[18] Fix | Delete
* @param string $content Content to parse, PHP code is expected.
[19] Fix | Delete
*
[20] Fix | Delete
* @return array All the definitions that could be found.
[21] Fix | Delete
*/
[22] Fix | Delete
public function getDefinitions($content)
[23] Fix | Delete
{
[24] Fix | Delete
$tokens = token_get_all($content);
[25] Fix | Delete
[26] Fix | Delete
$definitions = array();
[27] Fix | Delete
[28] Fix | Delete
// The parser has 4 phases:
[29] Fix | Delete
// 1 - found the 'define' keyword
[30] Fix | Delete
// 2 - found an open parentheses '('
[31] Fix | Delete
// 3 - found a constant string
[32] Fix | Delete
// 0 - (closing the circle) found a comma ','
[33] Fix | Delete
$phase = 0;
[34] Fix | Delete
$lastDefinition = '';
[35] Fix | Delete
[36] Fix | Delete
foreach ($tokens as $token) {
[37] Fix | Delete
if (is_array($token) && ($token[0] === T_WHITESPACE || $token[0] === T_COMMENT || $token[0] === T_DOC_COMMENT)) {
[38] Fix | Delete
// Skip whitespace and comment tokens.
[39] Fix | Delete
continue;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
if ($phase === 0) {
[43] Fix | Delete
// Look for a 'define' function call.
[44] Fix | Delete
if (is_array($token) && $token[0] === T_STRING && strtolower($token[1]) === 'define') {
[45] Fix | Delete
// This is a 'define' call, move to next phase.
[46] Fix | Delete
$phase = 1;
[47] Fix | Delete
}
[48] Fix | Delete
} elseif ($phase === 1 && $token === '(') {
[49] Fix | Delete
// Open parentheses found, move to next phase.
[50] Fix | Delete
$phase = 2;
[51] Fix | Delete
} elseif ($phase === 2 && is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) {
[52] Fix | Delete
// Constant string found, save it for later
[53] Fix | Delete
$lastDefinition = substr($token[1], 1, -1);
[54] Fix | Delete
$phase = 3;
[55] Fix | Delete
} elseif ($phase === 3 && $token === ',') {
[56] Fix | Delete
// Comma found, save the last found constant string, and reset the parser.
[57] Fix | Delete
$definitions[] = $lastDefinition;
[58] Fix | Delete
$phase = 0;
[59] Fix | Delete
} else {
[60] Fix | Delete
// Unsupported token found, reset the parser phase.
[61] Fix | Delete
$phase = 0;
[62] Fix | Delete
}
[63] Fix | Delete
}
[64] Fix | Delete
[65] Fix | Delete
return array_unique($definitions);
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
public function getDefinitionValues($content)
[69] Fix | Delete
{
[70] Fix | Delete
$tokens = token_get_all($content);
[71] Fix | Delete
[72] Fix | Delete
$definitions = array();
[73] Fix | Delete
[74] Fix | Delete
$phase = 0;
[75] Fix | Delete
$lastDefinition = '';
[76] Fix | Delete
$lastValue = '';
[77] Fix | Delete
[78] Fix | Delete
foreach ($tokens as $token) {
[79] Fix | Delete
if (is_array($token) && ($token[0] === T_WHITESPACE || $token[0] === T_COMMENT || $token[0] === T_DOC_COMMENT)) {
[80] Fix | Delete
// Skip whitespace and comment tokens.
[81] Fix | Delete
continue;
[82] Fix | Delete
}
[83] Fix | Delete
if ($phase === 0) {
[84] Fix | Delete
// Look for a 'define' function call.
[85] Fix | Delete
if (is_array($token) && $token[0] === T_STRING && strtolower($token[1]) === 'define') {
[86] Fix | Delete
// This is a 'define' call, move to next phase.
[87] Fix | Delete
$phase = 1;
[88] Fix | Delete
}
[89] Fix | Delete
} elseif ($phase === 1 && $token === '(') {
[90] Fix | Delete
// Open parentheses found, move to next phase.
[91] Fix | Delete
$phase = 2;
[92] Fix | Delete
} elseif ($phase === 2 && is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) {
[93] Fix | Delete
// Constant string found, save it for later
[94] Fix | Delete
$lastDefinition = trim($token[1], '"\'');
[95] Fix | Delete
$phase = 3;
[96] Fix | Delete
} elseif ($phase === 3 && $token === ',') {
[97] Fix | Delete
// Comma found, save the last found constant string.
[98] Fix | Delete
$phase = 4;
[99] Fix | Delete
} elseif ($phase === 4 && is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) {
[100] Fix | Delete
if (strlen($token[1]) === 2) {
[101] Fix | Delete
$lastValue = '';
[102] Fix | Delete
} else {
[103] Fix | Delete
$lastValue = stripslashes(substr($token[1], 1, -1));
[104] Fix | Delete
}
[105] Fix | Delete
$phase = 5;
[106] Fix | Delete
} elseif ($phase === 5 && $token === ')') {
[107] Fix | Delete
$definitions[$lastDefinition] = $lastValue;
[108] Fix | Delete
$phase = 0;
[109] Fix | Delete
} else {
[110] Fix | Delete
// Unsupported token found, reset the parser phase.
[111] Fix | Delete
$phase = 0;
[112] Fix | Delete
}
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
return $definitions;
[116] Fix | Delete
}
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function