<?php

/**
 *
 * srtool - a handy tool for doing stuff with .srt subtitle files
 * 
 * Copyright (C) 2005-2008 by sIX <six@aegis-corp.org>
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 *
 */

class SRTool {

    const 
VERSION "1.0.7";
    const 
CRLF "\r\n";
    
    public 
$srt = array();
    public 
$fn;

    public function 
__construct($fn) {

        
$this->Load($fn);
    
    }
    
    public function 
Load($fn) {

        
$this->fn $fn;
        
        if (!
file_exists($fn)) {

            throw new 
Exception("cannot find {$fn}");

        } else if (!
is_readable($fn)) {

            throw new 
Exception("cannot read {$fn}");

        }
        
        
$this->srt = array();

        if (!
$f = @fopen($fn"r")) throw new Exception("cannot open {$fn} for reading");
        
        
$ls fgets($f512);
        
        while (!
feof($f)) {

            
$idx = (int)$ls;
            
$ts rtrim(fgets($f512));

            
$cs $s "";
            
            do {

                if (
$cs !== ""$s .= $as $ls;
                
$cs $ls fgets($f512);
            
                if (((!
rtrim($as)) && ((int)$ls === ($idx 1))) || (feof($f))) break;
            
            } while (
true);

            if (
$idx >= 0$this->srt[$idx 1] = array($tsrtrim($s));

        }

        
fclose($f);

    }
    
    public function 
Save_As($fn) {

        if (!
$f = @fopen($fn"w+")) throw new Exception("cannot open {$fn} for writing");

        
$len 0;
        
        foreach (
$this->srt as $k => $se$len += fwrite($f, ($k 1) . self::CRLF $se[0] . self::CRLF $se[1] . self::CRLF self::CRLF);

        
fclose($f);

        return 
$len;

    }

    public function 
Save() {

        return 
$this->Save_As($this->fn);
    
    }

    public function 
Renumber() {

        
$this->srt array_values($this->srt);
    
    }
    
    public function 
Delete($idxs) {

        foreach (
$idxs as $idx) {

            if (isset(
$this->srt[$idx 1])) {

                unset(
$this->srt[$idx 1]);

            } else {

                throw new 
Exception("index not found : {$idx}");

            }
        
        }

        
$this->Renumber();
    
    }
    
    public function 
Wrap($cols 42) {

        foreach (
$this->srt as &$srt$srt[1] = wordwrap(preg_replace_callback("/([\r\n]+)(- )?/"create_function('$m''if ($m[2]) { return $m[1] . $m[2]; } else { return " " . $m[2]; }'), $srt[1]), $colsself::CRLFtrue);

    }

    public function 
Get_URL_Indexes() {

        
$ret = array();
        
        foreach (
$this->srt as $k => $srt) if (stripos($srt[1], "http://") !== false$ret[] = $k 1;

        return 
$ret;
    
    }

    public function 
Strip_Markup() {

        foreach (
$this->srt as &$srt) {
            
            
$tmp $srt[1];

            
// html-like tags
            
$tmp strip_tags($tmp);

            
// {\xxx} tags
            
$tmp preg_replace("/^{\\\[^}]+}/"""$tmp);
            
            
$srt[1] = $tmp;

        }
    
    }

    public function 
Replace_Chars() {

        
$repl = array(chr(0x9C) => "oe");
        
        foreach (
$this->srt as &$srt$srt[1] = strtr($srt[1], $repl);
    
    }

}

?>