function is_utf8($string)
{
return preg_match('%(?:
[\xC2-\xDF][\x80-\xBF]
|\xE0[\xA0-\xBF][\x80-\xBF]
|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
|\xED[\x80-\x9F][\x80-\xBF]
|\xF0[\x90-\xBF][\x80-\xBF]{2}
|[\xF1-\xF3][\x80-\xBF]{3}
|\xF4[\x80-\x8F][\x80-\xBF]{2}
)+%xs', $string);
}
setlocale(LC_ALL, 'fi_FI');
if(isset($_FILES['subtitle']['error']))
{
if($_FILES['subtitle']['error'] != UPLOAD_ERR_OK)
{
die('Tiedoston lähettämisessä tapahtui virhe.');
}
}
else
{
// filename from html form is not "subtitle"
// possible hacking attempt
die('Tiedoston lähettämisessä tapahtui virhe.');
}
$translators = trim($_POST['translators']);
if(strlen($translators) < 1)
{
die('Suomentajia on oltava vähintään yksi.');
}
$language = trim($_POST['language']);
$packsize = trim($_POST['packsize']);
if($packsize < 20)
{
$packsize = 20;
}
$include_subtitle = $_POST['include_subtitle'];
$remove_styling = $_POST['remove_styling'];
$filename = $_FILES['subtitle']['name'];
$location = $_FILES['subtitle']['tmp_name'];
// read the subtitle file into an array
$subtitle = file($location);
$rows = count($subtitle);
// clean up the file
for($i = 0; $i < $rows; $i++)
{
if(is_utf8($subtitle[$i]))
{
$subtitle[$i] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $subtitle[$i]);
}
$subtitle[$i] = trim($subtitle[$i]);
if($remove_styling)
{
$remove = array('<b>', '</b>', '<i>', '</i>', '<s>', '</s>', '<u>', '</u>', '<B>', '</B>', '<I>', '</I>', '<S>', '</S>', '<U>', '</U>');
$subtitle[$i] = str_replace($remove, '', $subtitle[$i]);
}
if(strlen($subtitle[$i]) == 0)
{
unset($subtitle[$i]);
}
}
// fix the array index
$subtitle = array_values($subtitle);
//detect subtitle format: sub / srt
if($subtitle[0] == "1" && strpos($subtitle[1], ' --> ') !== false)
{
// srt - subrip
$fps = $_POST['fps'];
$index = 1;
for($i = 0; $i < $rows; $i++)
{
if($subtitle[$i] == $index)
{
$position[$index - 1] = $i;
$index++;
}
}
// count the real number of rows
$rows = count($position);
// this fixes the last line
$position[$rows] = count($subtitle);
// counting
for($i = 0; $i < $rows; $i++)
{
$srt_times = explode(' --> ', $subtitle[$position[$i] + 1]);
$timearray = explode(':', $srt_times[0]);
$h = $timearray[0];
$m = $timearray[1];
$secarray = explode(',', $timearray[2]);
$s = $secarray[0];
$ms = $secarray[1];
// convert time format to frames
$show[$i] = round(($ms + $s * 1000 + $m * 60000 + $h * 3600000) / (1000 / $fps));
$timearray = explode(':', substr($srt_times[1], 0, 12));
$h = $timearray[0];
$m = $timearray[1];
$secarray = explode(',', $timearray[2]);
$s = $secarray[0];
$ms = $secarray[1];
// convert time format to frames
$hide[$i] = round(($ms + $s * 1000 + $m * 60000 + $h * 3600000) / (1000 / $fps));
for($j = $position[$i] + 2, $k = $position[$i + 1]; $j < $k; $j++)
{
if(strlen($text[$i]) > 0)
{
$text[$i] .= '|';
}
$text[$i] .= $subtitle[$j];
}
}
}
elseif(substr($subtitle[0], 0, 1) == '{' && substr($subtitle[1], 0, 1) == '{')
{
// sub - microdvd
$fps = end(explode('}', $subtitle[0]));
for($i = 1; $i < $rows; $i++)
{
$timearray = explode('}', $subtitle[$i]);
$show[$i - 1] = substr($timearray[0], 1);
$hide[$i - 1] = substr($timearray[1], 1);
// accommodate styling information
if(count($timearray) > 3)
{
// include styling only if that is wanted
if($remove_styling)
{
$text[$i - 1] = $timearray[3];
}
else
{
// include styling
$text[$i - 1] = $timearray[2].'}'.$timearray[3];
}
}
else
{
// no styling used
$text[$i - 1] = $timearray[2];
}
}
$rows--;
}
else
{
die('Tiedosto on väärässä muodossa.');
}
unset($subtitle);
// translator list parsing
if(strpos($translators, "\n") !== false)
{
// there are multiple lines
$nickname = explode("\n", $translators);
$nicks = count($nickname);
$line_index = true;
for($i = 1; $i <= $nicks; $i++)
{
// if any of the lines does not start with an index at zero position
// the list does not have index numbers in it
if(strpos($nickname[$i - 1], "$i".'. ') !== 0)
{
$line_index = false;
}
}
if($line_index)
{
for($i = 1; $i <= $nicks; $i++)
{
$nickname[$i - 1] = substr($nickname[$i - 1], strlen("$i".'. '));
}
}
}
else
{
// there is only one line
$nickname = explode(', ', $translators);
if(strpos(end($nickname), ' ja ') !== false)
{
$two_names = explode(' ja ', end($nickname));
$nickname[count($nickname) - 1] = $two_names[0];
$nickname[count($nickname)] = $two_names[1];
unset($two_names);
}
$nicks = count($nickname);
}
// add free packages according to max rows setting
if($rows / $nicks > $packsize)
{
$addition = ceil($rows / $packsize) - $nicks;
for($i = 0; $i < $addition; $i++)
{
$nickname[] = 'VAPAA';
}
$nicks = count($nickname);
}
// determine row count for packages
for($i = 0; $i < $nicks; $i++)
{
$row_count[$i] = floor($rows / $nicks);
// clean up the names
$nickname[$i] = trim($nickname[$i]);
}
$modulus = $rows % $nicks;
if($modulus !== 0)
{
for($i = 0; $i < $modulus; $i++)
{
$row_count[$i]++;
}
}
// split the subtitle into packages
$offset = 0;
for($i = 0; $i < $nicks; $i++)
{
$package[$i] = '{1}{1}'.$fps."\r\n";
for($j = $offset, $k = $offset + $row_count[$i]; $j < $k; $j++)
{
$package[$i] .= '{'.$show[$j].'}{'.$hide[$j].'}'.$text[$j]."\r\n";
}
$offset = $offset + $row_count[$i];
}
// create an archive that includes all of the packages
$fileinfo = pathinfo($filename);
$zipname = $fileinfo['filename'].'.paketit.zip';
$ziplocation = '/tmp/'.mt_rand().'.paketti';
// fix chars for buggy zip filename support
$search = array('å', 'Å', 'ä', 'Ä', 'ö', 'Ö');
$replace = array('a', 'A', 'a', 'A', 'o', 'O');
// create the actual archive
$zip = new ZipArchive;
$return = $zip->open($ziplocation, ZipArchive::CREATE);
if($return === true)
{
$zip->setArchiveComment('Mikropaketti 2011-02-26 © divxfinland.org');
for($i = 1; $i <= $nicks; $i++)
{
// fix chars for buggy zip filename support
$packname = $fileinfo['filename'].'.'.$language.'.P'.str_pad($i, strlen($nicks), '0', STR_PAD_LEFT).'.'.$nickname[$i - 1].'.sub';
$packname = str_replace($search, $replace, $packname);
$zip->addFromString($packname, $package[$i - 1]);
}
if($include_subtitle)
{
// fix chars for buggy zip filename support
$filename = str_replace($search, $replace, $filename);
$zip->addFile($location, $filename);
}
$zip->close();
}
else
{
die('Arkiston luominen ei onnistunut.');
}
// send the zip file to the user
header('Content-Description: File Transfer');
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename='.$zipname);
header('Content-Transfer-Encoding: binary');
//header('Expires: 0');
//header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
//header('Pragma: public');
//header('Content-Length: '.filesize($ziplocation));
ob_clean();
flush();
readfile($ziplocation);
unlink($ziplocation);
?>