<?php

    include "misc.inc"       ;
    include "db_connect.inc" ;
    include "common.inc"     ;
    include "pilotdoc.inc"   ;

    // Check params: Format, Charset, Id

    if(!isset($Id) || !isset($Charset) || !isset($Format))
    {
        die("Missing parameters");
    }

    if(strcasecmp($Format,"text")==0)
    {
        $ctype = "text/plain";
        $ext   = ".txt";
        $crlf  = 0 ;
    } else if(strcasecmp($Format,"crlftext")==0)
    {
        $ctype = "text/plain" ;
        $ext   = ".txt" ;
        $crlf  = 1 ;
    } else if(strcasecmp($Format,"pilotdoc")==0)
    {
        $ctype="application/x-pilot";
        $ext   = ".prc";
    } else
    {
        die("Invalid format!");
    }

    if(strcasecmp($Charset,"koi8")==0)
    {
        $cset   = "koi8-r";
        $cscode = "k";
    } else if(strcasecmp($Charset,"cp1251")==0)
    {
        $cset="cp1251";
        $cscode = "w";
    } else if(strcasecmp($Charset,"cp866")==0)
    {
        $cset="cp866";
        $cscode = "d";
    } else if(strcasecmp($Charset,"mac")==0)
    {
        $cset="mac-cyrillic";
        $cscode = "m";
    } else
    {
        die("Charset!");
    }
    
    $sql="SELECT Texts.Title, Texts.Text, Texts.AuthorId, Authors.Name, Authors.Email, UNIX_TIMESTAMP(Texts.PublishedAt), Texts.Hidden FROM Texts, Authors WHERE Authors.Id = Texts.AuthorId AND Texts.Id = " . addslashes($Id);
    $result = mysql_db_query($db_database, $sql , $link);
    $row = mysql_fetch_array($result);
    mysql_free_result($result);

   if($row[6]=="Yes")
   {
        Header("Content-type: text/plain; charset=\"koi8-r\"");
        print("Автор запретил просмотр этого произведения.");
   } else 
   {
       Header("Content-type: " . $ctype . "; charset=\"" . $cset . "\"");
       //RFC1806 to suggest file name
       Header("Content-Disposition: attachment; filename=OBEC.PACTET.text" . $Id . $ext);
     
       if(strcmp($ctype,"text/plain")==0)
       {
            $str = "";
            $str .= "\"" . $row[0] . "\"\n";
            $str .= $row[3] . " <" . $row[4] . ">\n" ;
            $str .= "\n\n" ;
            $str .= $row[1] ;
            $str .= "\n\n" ;
            $str .= "----------------------------------------------\n" ;
            $str .= "Все права принадлежат автору произведения.\n" ;
            $str .= "Распостранение без разрешения автора запрещено!.\n" ;
            $str .= "Закачано с http://obec.crocodile.org/\n" ;
    
            if($crlf)
            {
                    print   str_replace("\n", "\r\n", 
                                    convert_cyr_string($str, k, $cscode)
                            );
            } else
            {
                    print convert_cyr_string($str, k, $cscode);
            }
       } else if(strcmp($ctype,"application/x-pilot")==0)
       {
            $str = "";
            $str .= "\"" . $row[0] . "\"\n";
            $str .= $row[3] . " <" . $row[4] . ">\n" ;
            $str .= "\n\n" ;
            $str .= $row[1] ;
            $str .= "\n\n" ;
            $str .= "----------------------------------------------\n" ;
            $str .= "Все права принадлежат автору произведения.\n" ;
            $str .= "Распостранение без разрешения автора запрещено!.\n" ;
            $str .= "Закачано с http://obec.crocodile.org/\n" ;
    
            $pdoc = new PilotDOCFile(convert_cyr_string($str    , k, $cscode), 
                                     convert_cyr_string($row[0] , k, $cscode)
            );
            $pdoc->write();
       }
    
       mysql_db_query($db_database, "UPDATE Texts SET Views = Views+1 WHERE Id = " . addslashes($Id) , $link);
   }
?>
