<?php
/**
 * YANA library
 *
 * @package yana
 * @licence http://creativecommons.org/licenses/by-nc-sa/2.5/
 *
 * @ignore
 */
 
/**
 * This class is deprecated and may be removed in a future release
 *
 * @deprecated since version 2.8
 *
 * @access      public
 * @package     yana
 * @subpackage  io
 *
 * @ignore
 */
class TextFile extends DatFile
{

    /**#@+
     * class constant
     * @access  public
     */
    /** @var int */ var $UNTAINT_INPUT      = 0;
    /** @var int */ var $DONT_UNTAINT_INPUT = 1;
    /**#@-*/

    function get($line_nr, $untaint = null)
    {
        settype($line_nr, 'integer');
        $data      = $this->content[$line_nr];
        $REF       = $this->getRawData($line_nr);
        $TXT       = array();
        foreach ($REF as $key=>$element)
        {
            if ($untaint == $this->DONT_UNTAINT_INPUT) {
                $TXT[strtoupper($key)] = $element;
            } else if (is_string($element)) {
                $TXT[strtoupper($key)] = DisplayUtility::scanForAt(DisplayUtility::embeddedTags(DisplayUtility::smilies($element)));
            } else if (is_scalar($element)) {
                $TXT[strtoupper($key)] = $element;
            } else {
                trigger_error("Invalid input in file '".$this->getFilename()."' key '${key}'. Resource is expected to be a scalar value, found ".gettype($element)." instead.", E_USER_WARNING);
            }
        }
        return $TXT;
    }

    function getRawData($line_nr)
    {
        assert('is_int($line_nr);');
        settype($line_nr, 'int');
        $data      = $this->content[$line_nr];
        $REF       = array();
        while (preg_match("/<(.*)>(.*)<\/.*>/Ui", $data, $matches))
        {
            $matches[2]       = preg_replace("/(\S{80})(\S*)/i", "\\1<wbr>\\2", $matches[2]);
            $REF[$matches[1]] = $matches[2];
            $data             = preg_replace("/<".$matches[1].">.*<\/.*>/Ui", "", $data);
            $matches          = array();
        }
        return $REF;
    }

    function replace($line_nr, $REF, $untaint = null)
    {
        assert('is_int($line_nr);');
        settype($line_nr, 'int');
        $txt = "";
        if (gettype($REF)=="array" && gettype($line_nr)=="integer") {
            $ORIGINAL = $this->getRawData($line_nr);
            foreach ($REF as $key => $element)
            {
                  if ((empty($ORIGINAL[$key]) || $REF[$key] != $ORIGINAL[$key]) && $untaint != $this->DONT_UNTAINT_INPUT) {
                    $element = untaintInput($element, "string", 3000, YANA_ESCAPE_USERTEXT);
                  }
                  $txt    .= $this->makeData($key,$element);
            }
            $txt       = preg_replace("/>http:\/\//i",">",$txt);
            $txt       = nl2br($txt);
            $txt       = preg_replace("/\s/"," ",$txt);
            $txt      .= "\n";
            $this->content[$line_nr] = $txt;
            return true;
        } else {
            return false;
        }
    }

    function insert($REF, $untaint = null)
    {
        if (gettype($REF)=="array") {
            $txt       = "";
            foreach ($REF as $key => $element)
            {
                  if ((empty($ORIGINAL[$key]) || $REF[$key] != $ORIGINAL[$key]) && $untaint != $this->DONT_UNTAINT_INPUT){
                    $element = untaintInput($element, "string", 3000, YANA_ESCAPE_USERTEXT);
                  }
                  $txt    .= $this->makeData($key,$element);
            }
            $txt       = preg_replace("/>http:\/\//i",">",$txt);
            $txt       = nl2br($txt);
            $txt       = preg_replace("/\s/"," ",$txt);
            $txt      .= "\n";
            if (!$this->isEmpty()) {
                  array_unshift($this->content,$txt);
            } else {
                  $this->content[0] = $txt;
            }
            return true;
        } else {
            return false;
        }
    }

    function makeData($key,$element) 
    {
        settype($key,"string");
        settype($element,"string");
        return "<".strtoupper($key).">$element</".strtoupper($key).">";
    }

}

?>