<?php

require_once('comp/file.php');

define('ROOT', 'drc');

function isBadName($name)
{
    if (!is_string($name))
        return "not a string name for Criteria: $name";
    if (!preg_match('/^[ABC]\.\d\.[a-z]/', $name))
        return "not a matched Criteria: $name";
//  if (preg_match('/[^0-9.A-Ba-b]/', $name))
//      return "illegals in Criteria: $name";
//  if (preg_match('/\.\./', $name))
//      return "dotdot in Criteria: $name";

    return '';
}

function skipEmpties(&$lines)
{
    while (count($lines[0]) > 0 && preg_match("/^ *$/", $lines[0]))
        array_shift($lines);
}

function trimEmpties(&$lines)
{
    while (($len = count($lines)) > 0 && preg_match("/^ *$/", $lines[$len-1]))
        array_pop($lines);
}

function &fixUpTerms($terms)
{
    if (!is_array($terms))
        $terms = array($terms);
    $terms2 = array();
    foreach ($terms as $i => $term)
    {
        $term = preg_replace('/\//', '\/', $term);
        $terms2[] = '/' . $term . '/';
    }
    return $terms2;
}

function &highlightterms($terms, $text)
{
    $high = array(
        '<span style="background-color: cyan">$0</span>',
        '<span style="background-color: pink">$0</span>',
        '<span style="background-color: green">$0</span>',
        '<span style="background-color: orange">$0</span>',
        '<span style="background-color: #8888FF;">$0</span>',
        '<span style="background-color: #00DD45;">$0</span>',
    );

// print_r($terms);
// print "<hr>";
// print_r($high);
// print "<br><i>$text</i> <br>\n";
    $terms = fixUpTerms($terms);
    $x = preg_replace($terms, $high, $text);
// print "<u>$x</u> <br>\n";
// print "<hr>";
    return $x;
}



class Criteria
{
    var $name;
    var $textname;
    var $book;
    var $dir;
    var $webtrust;
    var $requirement;
    var $comments;
    var $verified;
    var $mark_regexp;

    function Criteria($name, $loc = '')
    {
        if ($e = isBadName($name))
            exit("not a Criteria ($e): $name");

        $this->name = $name;
        $book = substr($name, 0, 1);
        $this->dir = "{$book}/{$this->name}";

        $this->webtrust = $this->requirement = $this->comments =  $this->verified =
             false;
        $mark_regexp = false;
    }

    function criteriaFile()     { return $this->dir . "/" . $this->name . ".html"; }
    function criteriaVerified() { return $this->dir . "/verified.html"; }
    function criteriaComments() { return $this->dir . "/comments.html"; }

    function &stripTable(&$data)
    {
        $lines = explode("\n", $data);
        if (preg_match('/^ *<TABLE.*/', $lines[0]))
            array_shift($lines);
        trimEmpties($lines);
        $len = count($lines);
        $line = $lines[$len-1];
        if (preg_match('/<\/TABLE>/', $line))
            array_pop($lines);
        return $lines;
    }

    function &getNextTD(&$lines)
    {
        $e1 = " <!-- failed to parse TD block --> ";
        $e2 = " <!-- failed to parse TD block, no end --> ";
        $block = array();
        skipEmpties($lines);
        $line = $lines[0];
        if (!preg_match('/^ *<TD>/', $line))
            return $e1 . "<!-- this: $line -->";

        $line = array_shift($lines);
        if (preg_match('/^ *<TD>(.*)<\/TD> *$/', $line, $matches))
        {
            return $matches[1];
        }

        $inblock = true;
        while ($inblock && count($lines) > 0)
        {
            $line = array_shift($lines);
            if (preg_match('/^ *<\/TD> *$/', $line))
                $inblock = false;
            else
                $block[] = $line;
        }
        if ($inblock)
            return $e2;

        $newdata = join("\n", $block);
        return $newdata;
    }

    function initCriteria()
    {
        $data = read_content_of_file($this->criteriaFile());
        if (!$data)
        {
            $this->textname = " <u> <i> {$this->name} </i> </u> ";
            $this->webtrust = "  ";
            $this->requirement = "<u> <i> file not found </i> </u>";
            return;
        }
        $lines = $this->stripTable($data);

        $this->textname = $this->getNextTD($lines);
        $this->webtrust = $this->getNextTD($lines);
        $this->requirement = $this->getNextTD($lines);
    }

    function initStraight($name)
    {
        $data = read_content_of_file($name);
        if (!$data)
        {
            $this->comments = "<!-- file not found -->";
            return;
        }
        $lines = $this->stripTable($data);
        $new_data = join("\n", $lines);
        return $new_data;
    }

    function initComments()
    {
        $this->comments = $this->initStraight($this->criteriaComments());
    }
    function initVerified()
    {
        $this->verified = $this->initStraight($this->criteriaVerified());
    }

    function getTextName()
    {
        if (!$this->textname)
            $this->initCriteria();
        return $this->textname;
    }
    function getWebTrust()
    {
        if (!$this->webtrust)
            $this->initCriteria();
        return $this->webtrust;
    }
    function getRequirement()
    {
        if (!$this->requirement)
            $this->initCriteria();
        return $this->requirement;
    }
    function getVerified()
    {
        if (!$this->verified)
            $this->initVerified();
        return $this->verified;
    }
    function getComments()
    {
        if (!$this->comments)
            $this->initComments();
        return $this->comments;
    }

    function _setup_mark_regexp()
    {
        if ($this->mark_regexp)
            return ;
        $this->mark_regexp = array();
        $this->mark_regexp['req'] =
           preg_replace('/<!--[^>]*>/', '', $this->getRequirement());
        $this->mark_regexp['ver'] =
           preg_replace('/<!--[^>]*>/', '', $this->getVerified());
        $this->mark_regexp['com'] =
           preg_replace('/<!--[^>]*>/', '', $this->getComments());
    }

    function testForMark($mark)
    {
        $this->_setup_mark_regexp();

        $match = '/"' . $mark . '"/';
        $all_fields = array('req', 'ver', 'com');
        foreach ($all_fields as $field)
        {
            if (preg_match($match, $this->mark_regexp[$field]))
                return 1;
        }
        return 0;
    }

    function hasMarks($marks)
    {
        if (!is_array($marks))
            $marks = array($marks);

        foreach ($marks as $i => $mark)
        {
            if ('other' == $mark)      // this one is logical, different
            {
                if (!$this->testForMark('pass') && !$this->testForMark('fail'))
                    return 1;
                continue ;
            }
            if ($this->testForMark($mark))
                return 1;
        }
        return 0;
    }

    function includes($terms, $embolden = 0)
    {
        if (!is_array($terms))
            $terms = array($terms);

        $req = preg_replace('/<[^>]*>/', '', $this->getRequirement());
        $ver = preg_replace('/<[^>]*>/', '', $this->getVerified());
        $com = preg_replace('/<[^>]*>/', '', $this->getComments());
        
        foreach ($terms as $i => $term)
        {
            $term = '/' . preg_replace('/\//', '\/', $term) . '/';
            if (preg_match($term, $req))
                return 1;
            if (preg_match($term, $ver))
                return 1;
            if (preg_match($term, $com))
                return 1;
        }
        return 0;
    }

    function toTR(&$lines)
    {
        $drc = 'bgcolor="yellow"';
        $lines[] = "<tr>";
        $lines[] = " <td $drc> " . $this->getTextName() . " </td>";
        $lines[] = " <td $drc> " . $this->getWebTrust() . " </td>";
        $lines[] = " <td $drc> " . $this->getRequirement() . "\n </td>";
        $lines[] = " <td> " . $this->getVerified() . "\n </td>";
        $lines[] = " <td> " . $this->getComments() . "\n </td>";
        $lines[] = "</tr>";
    }

    function specialMarks(&$text)
    {
        if (!preg_match('/<mark [^>]*\/>/i', $text))
        {
		return ;
        }

        // http://www.danshort.com/HTMLentities/index.php?w=dingb
        $marks = getMarkArray();
        foreach ($marks as $name => $replace)
        {
            $text = preg_replace("/img/", "IMG", $text);
            $text = preg_replace("/fail/", "FAIL", $text);
            $text = preg_replace("/<mark type=\"$name\".*>/i", $replace, $text);
        }
    }

    function toTR2(&$lines, $terms = '')
    {
        $drc = 'bgcolor="yellow"';
        $c2 = 'rowspan="2"';
        $req = $this->getRequirement();
        $ver = $this->getVerified();
        $com = $this->getComments();
        if ($terms)
        {
            $req = highlightterms($terms, $req);
            $ver = highlightterms($terms, $ver);
            $com = highlightterms($terms, $com);
        }
        $this->specialMarks($com);
        $this->specialMarks($ver);

        $lines[] = "<tr>";
        $lines[] = " <td $drc> " . $this->getTextName() . " </td>";
        $lines[] = " <td $drc $c2>\n  $req\n </td>";
        $lines[] = " <td $c2>\n   $ver\n </td>";
        $lines[] = " <td $c2>\n   $com\n </td>";
        $lines[] = "</tr>";
        $lines[] = "<tr>";
        $lines[] = " <td $drc> " . $this->getWebTrust() . " </td>";
        $lines[] = "</tr>";
    }
}



?>