<?php
header
('Content-Type: text/html; charset=utf-8');
error_reporting(-1);

// read file contents
$data trim(file_get_contents('quote.data'));
$script basename($_SERVER["SCRIPT_FILENAME"]);
if (
$script == "quote.php") {
    
$channel "#linux";
} elseif (
$script == "quote-politics_open.php") {
    
$channel "#politics_open";
}

// Функция перекодирования url в русский язык
function my_url_decode($s)
{
    
$sstrtr ($s, array (
        
"%D0%B0"=>"а""%D0%90"=>"А""%D0%B1"=>"б""%D0%91"=>"Б""%D0%B2"=>"в""%D0%92"=>"В""%D0%B3"=>"г""%D0%93"=>"Г",
        
"%D0%B4"=>"д""%D0%94"=>"Д""%D0%B5"=>"е""%D0%95"=>"Е""%D1%91"=>"ё""%D0%81"=>"Ё""%D0%B6"=>"ж""%D0%96"=>"Ж",
        
"%D0%B7"=>"з""%D0%97"=>"З""%D0%B8"=>"и""%D0%98"=>"И""%D0%B9"=>"й""%D0%99"=>"Й""%D0%BA"=>"к""%D0%9A"=>"К",
        
"%D0%BB"=>"л""%D0%9B"=>"Л""%D0%BC"=>"м""%D0%9C"=>"М""%D0%BD"=>"н""%D0%9D"=>"Н""%D0%BE"=>"о""%D0%9E"=>"О",
        
"%D0%BF"=>"п""%D0%9F"=>"П""%D1%80"=>"р""%D0%A0"=>"Р""%D1%81"=>"с""%D0%A1"=>"С""%D1%82"=>"т""%D0%A2"=>"Т",
        
"%D1%83"=>"у""%D0%A3"=>"У""%D1%84"=>"ф""%D0%A4"=>"Ф""%D1%85"=>"х""%D0%A5"=>"Х""%D1%86"=>"ц""%D0%A6"=>"Ц",
        
"%D1%87"=>"ч""%D0%A7"=>"Ч""%D1%88"=>"ш""%D0%A8"=>"Ш""%D1%89"=>"щ""%D0%A9"=>"Щ""%D1%8A"=>"ъ""%D0%AA"=>"Ъ",
        
"%D1%8B"=>"ы""%D0%AB"=>"Ы""%D1%8C"=>"ь""%D0%AC"=>"Ь""%D1%8D"=>"э""%D0%AD"=>"Э""%D1%8E"=>"ю""%D0%AE"=>"Ю",
        
"%D1%8F"=>"я""%D0%AF"=>"Я""%20"=>" ")
    );
    return 
$s;
}


$filter "";
if (isset(
$_GET['filter'])) {
     
//Получаем $calc из url
    
$filter $_GET['filter']; //Получаем $calc из url
    
$filter my_url_decode($filter);
}


#print_r($_SERVER);

$len strlen($data);
$last 0;
$braces 0;
$backslash 0;

for (
$i 0$n 0$i $len$i++) {
        if (
$data[$i] == "\\") {
            if (
$backslash == 0) {
                
$backslash++;
            } else {
                
$backslash 0;
            }
            continue;
        } elseif (
$data[$i] == "{" && $backslash == 0) {
            if (
$braces == 0)
                
$last $i 1;
            
$braces++;
        } elseif (
$data[$i] == "}" && $backslash == 0) {
            
$braces--;
            if(
$braces == 0) {
                
$quotes[$n++] = substr($data$last$i $last 1);
                
$last $i 1;
            }
        }
        
$backslash 0;
}

// process messages data
foreach ($quotes as $k => $v)
{
    
$v trim($v);
    
// echo "<!-- $v -->\n";

    
$len strlen($v);
    
$braces 0;
    
$backslash 0;

    for (
$i 0$i $len$i++) {
        if (
$v[$i] == "\\") {
            if (
$backslash == 0) {
                
$backslash++;
            } else {
                
$backslash 0;
            }
            continue;
        } elseif (
$v[$i] == "{" && $backslash == 0)
            
$braces++;
        elseif (
$v[$i] == "}" && $backslash == 0)
            if(--
$braces == 0)
                break;
        
$backslash 0;
    }

    
$message substr($v1$i 1);
    
$info    substr($v$i 1$len);

    
$infos explode(' 'trim($info));
    if (isset(
$infos[1])) {
        
$nick explode('!'$infos[1]);
    } else {
        
# handle quites where $message not in {}, but string in format
        # "word\ word\ word\\ "
        
$infos preg_split("/[^\\\]\\\ (*SKIP)(*FAIL)|\s+/"$v);
        if (isset(
$infos[1])) {
            
# remove all \ from message
            
$message preg_replace("/\\\/"""$infos[0]);

            
# remove message from infos
            # \array_splice will remove 0 element and rearrange indexes
            # (what unset($infos[0]) not do
            
\array_splice($infos01);

            
$nick explode('!'$infos[1]);
            
#echo "<pre>";
            #print_r(preg_split("/[^\\\]\\\ (*SKIP)(*FAIL)|\s+/", $v));
            #print_r($infos);
            #print_r($nick);
            #print_r($infos[3]);
            #echo "</pre>";
        
} else {
            
$nick "???";
            
error_log("!!!! " $v);
        }
    }
    
$nick $nick[0];
    if (isset(
$infos[3])) {
        
$chan $infos[3];
    } else {
        
// if something wrong with quote - show it?!
        
$chan $channel// if use this, show a lot garbige. why??
        
$chan "???";
        
error_log(">>> " $v "<<<< " $message);
    }

    if (!isset(
$channel) || $chan == $channel) {
        
$quotes[$k] = array(
            
'message'   => $message,
            
'info'      => array(
                
'date'      => strftime('%e.%m.%Y в %T'$infos[0]),
                
'author'    => $infos[1],
                
'nick'      => $nick,
                
'channel'   => $chan
            
)
        );
    } else {
        unset(
$quotes[$k]);
        
#\array_splice($quotes, $k, $k);
    
}
}


// and show they all
?>
<html>
    <head>
        <title>Quotes <?php echo "$channel"?> @ Rusnet</title>
        <link rel="stylesheet" href="/style.css" type="text/css">
    </head>
    <body>
        <h1 align="center">Цитатник <?php echo "$channel"?>@RusNet</h1>
    <center>
    <form method='GET' action="/<?php echo $script?>" accept-charset="UTF-8">
            Search in quotes <input type='text' name='filter' value=''> <input type='submit'>
        </form>
    </center>
<?php
if (!empty($filter)) {
    echo 
"<p><a href='$script?filter='>Назад</a><br/><p>";
}
?>

        <ul class="quotes">
        <?php
            $i 
count($quotes) + 1;
            foreach (
array_reverse ($quotestrue) as $k => $quote):
            
$i--;
            if (!empty(
$filter)) {
                if (!
strpos($quote['info']['author'], $filter) &&
                    !
strpos($quote['info']['nick'], $filter) &&
                    !
strpos($quote['message'], $filter)) {
                
#echo "<p>".!str_contains($quote['info']['author'], $filter)."</p>". "<p>".!str_contains($quote['info']['nick'], $filter)  ."</p>". "<p>".!str_contains($quote['message'], $filter)       ."</p>";
                    
continue;
                }
            }
         
?>
            <li id="quote_<?php echo $i ?>" class="quote">
                <div class="date"><a href="#quote_<?php echo $i ?>">#<?php echo $i ?></a></div>
                <div class="author">Опубликовал <strong title="<?php echo htmlspecialchars($quote['info']['author']).htmlspecialchars($quote['info']['channel']); ?>"><?php echo htmlspecialchars($quote['info']['nick']); ?></strong> <?php echo $quote['info']['date']; ?></div>
                <p><?php
                    $msg 
trim(preg_replace('/[\x02\x0f]/'''$quote['message']));
                    
$msg trim(preg_replace('/((([(]|\[)?\d{1,2}:\d{2}(:\d{2})?([])]| PM[)])?)? ?(( \* \S{2,})|( ::: )|(<[^>]+> )?[^: ]+: ([^: ]+:)?|(?<=[^<] )\w+>|<))/'"\n$0"$msg));
                    echo 
nl2br(htmlspecialchars($msg)); ?>
                </p>
                
            </li>
        <?php endforeach; ?>
            
        </ul>

        <p align="center"><a href="http://heliosart.info">(c) helios</a></p>
    </body>
</html>