/*
 * Displays a (Sub)-Thread. Is used in article.php
 *
 * $id:    Message-ID (not number!) of an article in the thread
 * $group: name of the newsgroup
 */
function message_thread($id,$group,$thread) {
  $current=$id;
  flush();
  while(isset($thread[$id]->references)) {
    foreach($thread[$id]->references as $reference) {
      if((trim($reference)!='') && (isset($thread[$reference]))) {
        $id=$reference;
        continue 2;
      }
    }
    break;
  }
  $liste=array();
  $liste[]=$id;
  $tmp=0;
  thread_show_head();
  echo thread_show_recursive($thread,$liste,1,"",$group,0,100,$tmp,$current);
  thread_show_tail();
}

/*
 * Print the header of a message to the webpage
 *
 * $head: the header of the message as an headerType
 * $group: the name of the newsgroup, is needed for the links to post.php3
 *         and the header.
 */
function show_header($head,$group) {
  global $article_show,$text_header,$file_article,$attachment_show;
  global $file_attachment,$anonym_address;
  echo '<div class="np_article_header">';
  if ($article_show["Subject"]) echo $text_header["subject"].htmlspecialchars($head->subject)."<br>";
  if ($article_show["From"]) {
    echo $text_header["from"];
    if($head->from==$anonym_address) {
      // this is the anonymous address, so only show the name
      echo htmlspecialchars($head->name);
    } else {
      if($article_show["From_link"])
        echo '<a href="mailto:'.htmlspecialchars($head->from).'">';
      if(isset($article_show["From_rewrite"]))
        echo eregi_replace($article_show["From_rewrite"][0],
                           $article_show["From_rewrite"][1],
                           htmlspecialchars($head->from));
      else
        echo htmlspecialchars($head->from);
      if($article_show["From_link"])
        echo '</a>';
      if ($head->name != "") echo ' ('.htmlspecialchars($head->name).')';
    }
    echo "<br>";
  }
  if ($article_show["Newsgroups"]) 
    echo $text_header["newsgroups"].htmlspecialchars(str_replace(',',', ',$head->newsgroups))."<br>\n";
  if (isset($head->followup) && ($article_show["Followup"]) && ($head->followup != "")) 
    echo $text_header["followup"].htmlspecialchars($head->followup)."<br>\n";
  if ((isset($head->organization)) && ($article_show["Organization"]) &&
     ($head->organization != ""))
    echo $text_header["organization"].
         html_parse(htmlspecialchars($head->organization))."<br>\n";
  if ($article_show["Date"])
    echo $text_header["date"].date($text_header["date_format"],$head->date)."<br>\n";
  if ($article_show["Message-ID"])
    echo $text_header["message-id"].htmlspecialchars($head->id)."<br>\n";
  if (($article_show["References"]) && (isset($head->references[0]))) {
    echo $text_header["references"];
    for ($i=0; $i<=count($head->references)-1; $i++) {
      $ref=$head->references[$i];
      echo ' '.'<a href="'.$file_article.'?group='.urlencode($group).
           '&id='.urlencode($ref).'">'.($i+1).'</a>';
    }
    echo "<br>";
  }
  if (isset($head->user_agent)) {
    if ((isset($article_show["User-Agent"])) &&
       ($article_show["User-Agent"])) {
      echo $text_header["user-agent"].htmlspecialchars($head->user_agent)."<br>\n";
    } else {
      echo "<!-- User-Agent: ".htmlspecialchars($head->user_agent)." -->\n";
    }
  }
  if ((isset($attachment_show)) && ($attachment_show==true) &&
      (isset($head->content_type[1]))) {
    echo $text_header["attachments"];
    for ($i=1; $i<count($head->content_type); $i++) {
      echo '<a href="'.$file_attachment.'?group='.urlencode($group).'&'.
           'id='.urlencode($head->number).'&'.
           'attachment='.$i.'">'.
           $head->content_type_name[$i].'</a> ('.
           $head->content_type[$i].')';
      if ($i<count($head->content_type)-1) echo ', ';
    }
  }
  echo '</div>';
}

/*
 * decodes a body. Splits the content of $body into an array of several
 * lines, respecting the special decoding issues of format=flowed
 * articles. Each returned line consists of two fields: text and
 * the quote depth (depth)
 */
function decode_textbody($body,$format="fixed") {
  $body=split("\n",$body);
  $nbody=array();
  $depth=0;
  $paragraph=""; // empty paragraph
  $lastline="";
  for($i=0; $i<count($body)+1; $i++) {
    // calculate the quote depth of the actual line
    $ndepth=0;
    $tdepth=0;
    for($j=0; $j<=strlen($body[$i]); $j++) {
      $tdepth=$j;
      if($body[$i][$j]=='>') {
        $ndepth++;
      } else {
        if(($body[$i][$j]!=' ') || ($body[$i][$j-1]==' ') || ($j==0)) {
          break;
        }
      }
    }
    // generate a new paragraph?
    if(($i>0) && (($ndepth!=$depth) || $format!="flowed" ||
       ($paragraph[strlen($paragraph)-1]!=' ')) || ($i==count($body))) {
      $tmp->text=$lastline=$paragraph;
      $tmp->depth=$depth;
      $paragraph="";
      $nbody[]=$tmp;
    }
    if($body[$i]=="-- " && $format=="flowed") $body[$i]="--";
    $paragraph.=substr($body[$i],$tdepth);
    $depth=$ndepth;
  }
  return $nbody;
}

/*
 * replaces multiple spaces in texts by &nbsp;es and convert special-chars
 * to their entities
 */
function text2html($text) {
  return eregi_replace("^ ","&nbsp;",
         str_replace("  ","&nbsp; ",
         str_replace("  ","&nbsp; ",
         str_replace("\n","<br>",
         htmlspecialchars($text)))));
}


/*
 * print an article to the webpage
 *
 * $group: The name of the newsgroup
 * $id: the ID of the article inside the group or the message-id
 * $attachment: The number of the attachment of the article.
 *              0 means the normal textbody.
 */
function message_show($group,$id,$attachment=0,$article_data=false) {
  global $file_article;
  global $text_header,$text_article,$article_showthread;
  global $block_xnoarchive,$article_graphicquotes;
  if ($article_data == false)
    $article_data=message_read($id,$attachment,$group);
  $head=$article_data->header;
  $body=$article_data->body[$attachment];
  if ($head) {
    if (($block_xnoarchive) && (isset($head->xnoarchive)) &&
        ($head->xnoarchive=="yes")) {
      echo $text_article["block-xnoarchive"];
    } else
    if (($head->content_type[$attachment]=="text/plain") &&
        ($attachment==0)) {
      show_header($head,$group);
      $body=decode_textbody($body,
             $article_data->header->content_type_format[$attachment]);
      $depth=0;
      echo '<div class="np_article_body">';
      for ($i=0; $i<=count($body); $i++) {
        // HTMLized Quotings instead of boring > ?
        if($article_graphicquotes) {
          // HTMLized Quotings
          for($j=$depth; $j<$body[$i]->depth; $j++)
            echo '<blockquote class="np_article_quote">';
          for($j=$body[$i]->depth; $j<$depth; $j++)
            echo '</blockquote>';
          echo html_parse(text2html($body[$i]->text)).'<br>';
          echo "\n";
          $depth=$body[$i]->depth;
        } else {
          // Boring old Quotings with >
          if($body[$i]->depth==0) {
            if(trim($body[$i]->text)=='')
              echo "<br>\n";
            else
              echo html_parse(text2html($body[$i]->text))."<br>\n";
          } else {
            echo '<i>'.str_repeat('&gt;',$body[$i]->depth).' '.
              html_parse(text2html(
                 textwrap($body[$i]->text,72-$body[$i]->depth,
                  "\n".str_repeat('>',$body[$i]->depth).' '))).
              "</i><br>\n";
          }
        }
      }
      echo '</div>';
    } else {
      echo $body;
    }
  }
}
