Skip to content

Instantly share code, notes, and snippets.

@TimVroom
Last active October 27, 2015 22:21
Show Gist options
  • Save TimVroom/8d3c644cce7addf02f79 to your computer and use it in GitHub Desktop.
Save TimVroom/8d3c644cce7addf02f79 to your computer and use it in GitHub Desktop.
Patch 6788 variable check (WIP)
<?php
// make sure the include path is correct
include "htdocs/app/Mage.php";
Mage::app();
// Set area to admin
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$resource = Mage::getSingleton('core/resource');
$cmsBlockTable = $resource->getTableName('cms/block');
$cmsPageTable = $resource->getTableName('cms/page');
$emailTemplate = $resource->getTableName('core/email_template');
$sql = "SELECT %s FROM %s WHERE %s LIKE '%%{{config %%' OR %s LIKE '%%{{block %%'";
$list = ['block' => [], 'variable' => []];
$cmsCheck = sprintf($sql, 'content', $cmsBlockTable, 'content', 'content');
$result = $resource->getConnection('core_read')->fetchAll($cmsCheck);
check($result, 'content', $list);
$cmsCheck = sprintf($sql, 'content', $cmsPageTable, 'content', 'content');
$result = $resource->getConnection('core_read')->fetchAll($cmsCheck);
check($result, 'content', $list);
$emailCheck = sprintf($sql, 'template_text', $emailTemplate, 'template_text', 'template_text');
$result = $resource->getConnection('core_read')->fetchAll($emailCheck);
check($result, 'template_text', $list);
$localeDir = Mage::getBaseDir('locale');
$scan = scandir($localeDir);
walkDir($scan, $localeDir, $list);
var_dump($list);
function walkDir(array $dir, $path = '', &$list) {
foreach ($dir as $subdir) {
if (strpos($subdir, '.') !== 0) {
if(is_dir($path . DS . $subdir)) {
walkDir(scandir($path . DS . $subdir), $path . DS . $subdir, $list);
} elseif (is_file($path . DS . $subdir) && pathinfo($subdir, PATHINFO_EXTENSION) !== 'csv') {
check([file_get_contents($path . DS . $subdir)], null, $list);
}
}
}
}
function check($result, $field = 'content', &$list) {
if ($result) {
$blockMatch = '/{{block[^}]*?type=["\'](.*?)["\']/i';
$varMatch = '/{{config[^}]*?path=["\'](.*?)["\']/i';
foreach ($result as $res) {
$target = ($field === null) ? $res: $res[$field];
if (preg_match_all($blockMatch, $target, $matches)) {
foreach ($matches[1] as $match) {
if (!in_array($match, $list['block'])) {
$list['block'][] = $match;
}
}
}
if (preg_match_all($varMatch, $target, $matches)) {
foreach ($matches[1] as $match) {
if (!in_array($match, $list['variable'])) {
$list['variable'][] = $match;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment