#!/usr/bin/php
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
$progname = "pophorator";
$progver = "0.51";
ini_set( "memory_limit", "-1");
assert_options( ASSERT_BAIL, 1);
error_reporting(E_ALL);
// Output the PHP errors into STDERR as the output is written into STDOUT
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
switch ($errno)
{
case E_USER_ERROR:
fwrite(STDERR,
"** INTERNAL FATAL ERROR [$errno] $errstr
\n" .
" Fatal error in line $errline of file $errfile" .
", PHP " . PHP_VERSION . " (" . PHP_OS . ")\n");
exit(1);
break;
case E_USER_WARNING:
fwrite(STDERR, "** INTERNAL ERROR: [$errno, line $errline] $errstr\n");
exit(1);
break;
case E_USER_NOTICE:
case E_NOTICE:
fwrite(STDERR, "** INTERNAL WARNING: [$errno, line $errline] $errstr\n");
break;
default:
fwrite(STDERR, "** INTERNAL UNKOWN ERROR: [$errno, line $errline] $errstr\n");
exit(1);
break;
}
}
set_error_handler("myErrorHandler");
// TODO: support different character encodings
function Find_I10N_Strings( $file )
{
$file_len = strlen($file);
// Make a line num / col LUT
$idx_to_row_col = array();
$row = $col = 0;
for ( $i=0; $i<$file_len; ++$i )
{
if ( $file[$i] == "\n" )
{ $row++; $col=0; }
$idx_to_row_col[$i] = array($row, $col++);
}
// Find the PHP snippets
$split = array();
$php_matches = array();
preg_match_all( '/<[?]php\s+(.*)\s+[?]>/Usi',
$file, &$php_matches, PREG_OFFSET_CAPTURE|PREG_SET_ORDER);
// Find the Javascript snippets
$javascript_matches = array();
preg_match_all( '/<\s*script[^>]*>(.*)<\s*[\/]\s*script[^>]*>/Usi', $file, &$javascript_matches, PREG_OFFSET_CAPTURE|PREG_SET_ORDER);
// Append PHP and Javascript snippets and sort them by occurrence index
$script_matches = array_merge($php_matches, $javascript_matches);
usort($script_matches, create_function('$a,$b', 'return $a[0][1]>$b[0][1];'));
// Merge overlapping script snippets (there might be, e.g. PHP blocks
// inside