Skip to content

Instantly share code, notes, and snippets.

@Ibmurai Ibmurai/fixstuff.php
Created Sep 27, 2012

Embed
What would you like to do?
Fix all calls to htmlentities and htmlspecialchars, if you want to do ISO-8859-1 on PHP 5.4 or just be prepared.
#!/opt/local/bin/php
<?php
function getDirectoryList($dir, $skipExtensions = []) {
$dirList = $fileList = [];
if ($dfp = opendir($dir)) {
while (($entry = readdir($dfp)) !== false) {
$skip = false;
foreach ($skipExtensions as $extension) {
if (substr($entry, strlen($entry) - strlen($extension)) === $extension) {
$skip = true;
}
}
if (!$skip && $entry[0] != '.') {
$path = "$dir/$entry";
if (is_link($path)) {
} else if (is_file($path)) {
$fileList[] = $entry;
} else if (is_dir($path)) {
$dirList[$entry] = getDirectoryList($path, $skipExtensions);
}
}
}
closedir($dfp);
uksort($dirList, 'strnatcmp');
natsort($fileList);
}
return $dirList + $fileList;
}
function traverse($directoryList, $prepend = '') {
foreach ($directoryList as $name => $entry) {
if (is_array($entry)) {
traverse($entry, $prepend . $name . '/');
} else {
doWork($prepend . $entry);
}
}
}
$htmlEntitiesPattern = '/(?:(?:htmlentities)|(?:htmlspecialchars))
\(
(
(?:
(?:
\(
[^\)\(]+
\)
)
|
(?:
[^\)\(]+
)
)*
)
\)
/mx';
$paramsPattern = '/
(?:,|^)\s*
(
(?:
(?:[^\)\(,]+)?
\(
[^\)\(]+
\)
)
|
(?:
[^\)\(,]+
)
)
/mx';
function doFixHtmlEntities($file) {
global $htmlEntitiesPattern, $paramsPattern;
if (($input = file_get_contents($file)) !== FALSE) {
$matches = [];
if (preg_match_all($htmlEntitiesPattern, $input, $matches, PREG_SET_ORDER)) {
$res = '';
foreach ($matches as $match) {
$pMatches = [];
preg_match_all($paramsPattern, $match[1], $pMatches, PREG_SET_ORDER);
$args = [];
while ($arg = array_shift($pMatches)) {
$args[] = $arg[1];
}
$res .= 'func: ' . var_export($match[0], true) . "\n";
$newArgs = [];
if (isset($args[0])) {
$newArgs[0] = array_shift($args);
if (isset($args[0])) {
$newArgs[] = array_shift($args);
} else {
$newArgs[] = 'ENT_COMPAT';
}
if (isset($args[0])) {
$newArgs[] = array_shift($args);
} else {
$newArgs[] = "'ISO-8859-1'";
}
while ($nextArg = array_shift($args)) {
$newArgs[] = $nextArg;
}
$newCall = str_replace($match[1], implode(', ', $newArgs), $match[0]);
$res .= 'fixd: ' . var_export($newCall, true) . "\n";
$input = str_replace($match[0], $newCall, $input);
file_put_contents($file, $input);
} else {
$res .= "NO ARGS\n";
}
}
//return var_export($matches, true);
return $res;
}
} else {
return 'ERROR: Could not read file.';
}
}
function doWork($file) {
if ($output = doFixHtmlEntities($file)) {
echo $file . "\n";
echo "$output\n";
}
}
traverse(
getDirectoryList('.', array('.swf', '.ini', '.xsl', '.png', '.jpg', '.yaml', '.yml', '.xml', '.json', '.txt', '.ico', '.sql', '.js', '.jar', '.java', '.css', '.gif', '.less'))
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.