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: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198:
<?php
if (php_sapi_name() !== 'cli')
die('This tool is to be ran via CLI');
prepareCLIhandler();
$buildFiles = array(
'index.php' => 'SMF',
'SSI.php' => 'Sources',
'subscriptions.php' => 'Sources',
'Sources/*.php' => 'Sources',
'Sources/Tasks/*.php' => 'Tasks',
'Themes/default/*.php' => 'Default',
'Themes/*/*.php' => 'Template',
'Themes/default/languages/*.english.php' => 'Languages',
);
$readLengths = array(
'SMF' => 4096,
'Sources' => 4096,
'Tasks' => 4096,
'Default' => 768,
'Template' => 768,
'Languages' => 768
);
$searchStrings = array(
'SMF' => '~\*\s@version\s+(.+)[\s]{2}~i',
'Sources' => '~\*\s@version\s+(.+)[\s]{2}~i',
'Tasks' => '~\*\s@version\s+(.+)[\s]{2}~i',
'Default' => '~\*\s@version\s+(.+)[\s]{2}~i',
'Template' => '~\*\s@version\s+(.+)[\s]{2}~i',
'Languages' => '~(?://|/\*)\s*Version:\s+(.+?);\s*~i'
);
$ignoreFiles = array(
'|\./*.php~|i',
'|\./*.txt|i',
);
if ($cliparams['smf'] == '20')
unset($buildFiles['Sources/Tasks/*.php']);
if (!isset($cliparams['include-languages']))
unset($buildFiles['Themes/default/languages/*.english.php']);
if (!isset($_SERVER['argv'], $_SERVER['argv'][1]))
die('Error: No SMF root specified' . "\n");
$smfRoot = $_SERVER['argv'][1];
if (!file_exists($smfRoot))
die('Error: SMF Root does not exist' . "\n");
$smfRoot = realpath(rtrim($smfRoot, '/')) . '/';
$version_info = array();
$count = 0;
foreach ($buildFiles as $globPath => $location)
{
$files = glob($smfRoot . $globPath);
if (!isset($version_info[$location]))
$version_info[$location] = array();
foreach ($files as $file)
{
$basename = basename($file);
if ($basename == 'index.php' && $location != 'SMF')
continue;
foreach ($ignoreFiles as $if)
if (preg_match($if, $basename))
continue;
++$count;
$fp = fopen($file, 'r');
$header = fread($fp, $readLengths[$location]);
fclose($fp);
if (preg_match($searchStrings[$location], $header, $match) == 1)
$version_info[$location][$basename] = $match[1];
else
$version_info[$location][$basename] = '???';
}
}
foreach ($version_info as $location => $files)
ksort($version_info[$location]);
if (isset($cliparams['include-languages']) && $cliparams['output'] == 'raw')
var_dump($version_info);
else
{
foreach ($version_info as $location => $files)
{
if ($location === 'SMF')
echo "window.smfVersions = {\n";
elseif ($location === 'Languages')
echo "};\n\nwindow.smfLanguageVersions = {\n";
foreach ($files as $file => $version)
{
++$i;
$thislocation = $location === 'SMF' ? 'SMF' : ($location === 'Languages' ? str_replace('.english.php', '', $file) : $location . $file);
if ($thislocation === 'SMF')
$version = 'SMF ' . $version;
echo "\t'", $thislocation, "': '" . $version . "'";
if ($count != $i)
echo ',';
echo "\n";
}
if ($location === 'Languages')
echo "};\n";
}
}
function prepareCLIhandler()
{
global $cliparams;
$params = $_SERVER['argv'];
array_shift($params);
$cliparams = array();
foreach($params AS $param)
{
if (strpos($param, '=') !== false)
{
list ($var, $val) = explode('=', $param);
$cliparams[ltrim($var, '-')] = $val;
}
else
$cliparams[ltrim($param, '-')] = true;
}
unset($params);
if (empty($cliparams) || isset($cliparams['help']) || isset($cliparams['h']))
{
echo "SMF Generate Detailed Versions Tool". "\n"
. '$ php ' . basename(__FILE__) . " path/to/smf/ [-h] [--output=raw] [--languages] [--smf=[20|21]] \n"
. "--include-languages Include Languages Versions.". "\n"
. "--smf=[21|20] What Version of SMF. This defaults to SMF 21.". "\n"
. "-h, --help This help file.". "\n"
. "--output=raw Raw output.". "\n"
. "\n";
die;
}
if (!isset($cliparams['smf']))
$cliparams['smf'] = '21';
}