<?PHP

/*
 *	W-AGORA 4.2
 *	-----------
 *	$Id: viewnote.php,v 1.46 2005/03/12 14:10:27 mdruilhe Exp $
 *	Usage:		include file - display a note 
 *	Authors:	Marc Druilhe <mdruilhe@w-agora.net>
 */

# set URLs to be displayed in the the note (or toolbar)
# -----------------------------------------------------

	// REPLY
	$reply_url="";
	$reply_link="";
	$reply_text = LABEL_REPLY_MESSAGE;
	if ($bn_thread && ($forum['state'] == '1') ) {
		if ($bn_view_reply) {
			$reply_url = "#FORM";
			$reply_link = anchor ($reply_url, "", $reply_text, ALT_REPLY_MESSAGE );
		} else {
			$reply_url = "post.$ext?bn=$bn&key=$key";
			$reply_link = anchorWindow ($reply_url, $reply_text, ALT_REPLY_MESSAGE, $bn_win_width, $bn_win_height);
		}
	}

	// EDIT
	$edit_url = "edit.$ext?bn=$bn&key=$viewkey";
	$edit_text = LABEL_EDIT_MESSAGE;
	if ($bn_modify) {
		if ($is_moderator || ($userid == 'guest') || ($auth->user["userid"] == $userid) ) {
			$edit_link = anchorWindow ($edit_url, $edit_text, ALT_EDIT_MESSAGE, $bn_win_width, $bn_win_height);	
		} else {
			$edit_link = "";
		}

	}

	// DELETE
	$delete_url = "delete.$ext?bn=$bn&key=$viewkey";
	$delete_text = LABEL_DELETE_MESSAGE;
	if ($bn_delete) {
		if ($is_moderator || ($userid == 'guest') || ($auth->user["userid"] == $userid) ) {
			$delete_link = anchor ($delete_url, "", $delete_text, ALT_DELETE_MESSAGE);
		} else {
			$delete_link = "";
		}
	}

	// MAIL
	$mail_text = LABEL_MAIL;
	$mail_url = "mail.$ext?site=$site&bn=$bn&key=$key&type=author&redirect=$bn_action";
	$mail_link = "";
	if ($bn_private_mail) {
		if ($bn_popup) {
			$mail_link = anchorWindow ($mail_url, $mail_text, ALT_PRIVATE_MAIL, $bn_win_width, $bn_win_height);
		} else {
			$mail_link = anchor ($mail_url, $target_frame, $mail_text, ALT_PRIVATE_MAIL);
		}
	}

	$mail_moder_text = LABEL_MAIL_MODER;
	$mail_moder_url = "mail.$ext?site=$site&bn=$bn&key=$key&type=moderator&redirect=$bn_action";
	if ($bn_popup) {
		$mail_moder_link = anchorWindow ($mail_moder_url, $mail_moder_text, ALT_MAIL_MODER, $bn_win_width, $bn_win_height);
	} else {
		$mail_moder_link = anchor ($mail_moder_url, $target_frame, $mail_moder_text, ALT_MAIL_MODER);
	}

	// SHOW PROFILE
	$show_profile_url = "profile.$ext?bn=$bn&showuser=$userid";
	$show_profile_text = msg(145, "<img src=\"$icon_dir/show_profile.gif\" width=15 height=15 border=0>");
	if ($userid == "guest") {
		$show_profile_link = "";
	} else {
		$show_profile_link = anchor ($show_profile_url, "", $show_profile_text, sprintf (ALT_SHOW_PROFILE, $userid));
	}
	
	// USER PRIVILEGES
	if (isset($user_userpriv) ) {
		$user_priv = ($userid=='guest') ? GUESTUSER_USERNAME : $GLOBALS['user_privs'][$user_userpriv];
	}

# Gets attachment if any
# ----------------------
	$att_size = "";
	$att_type = "";
	$att_name = "";
	$att_url = "";
	$att_link = "";
	$attachments = "";
	$att_icon = "";
	$num_cells = (isset($GLOBALS["att_num_cells"])) ? $GLOBALS["att_num_cells"] : 5; // number of cells per row

	$att = $db->getAttachments ($bn_db, $viewkey);
	if (is_array($att)) {
		$att_count = count($att);
		$attachments = "\n<table border=0 cellpadding=15 width=\"100%\">\n<tr>";
		$ncell = 0;
		for ($i=0; $i<$att_count; $i++) {
			$att_id        = $att[$i]["att_id"];
			$att_ids[$i]   = $att_id;
			$att_name      = $att[$i]["att_name"];
			$att_names[$i] = $att_name;
			$att_type      = $att[$i]["att_type"];
			$att_types[$i] = $att_type;
			$att_size      = (integer) $att[$i]["att_size"];
			$att_sizes[$i] = $att_size;
			if ($GLOBALS["use_path_info"]) {
				$att_url = "getfile.$ext/$bn/$att_id/$att_name";	
			} else {
				$att_url = "getfile.$ext?bn=$bn&att_id=$att_id";
			}
			$att_urls[$i]  = $att_url;
			$att_inline    = $att[$i]["inline"];
			$att_link      = getAttachmentUrl ($viewkey, $att_id, "$bn_dir_notes/$att_id.$att_name", $att_type, $att_size, $att_inline);
			$att_links[$i] = $att_link;
			// check for full row
			if (++$ncell > $num_cells)
			{
				$attachments .= "</tr>\n<tr>";
				$ncell = 1;
			}

			// If this attachment is literaly displayed, skip
			// rest of row and display the attachment in a row
			// by itself.

 			if (getAttDisplayMode($att_type, $att_inline) != ATT_DSP_LINK) {
				if ($ncell > 1)	{
					// fill up rest of row
					while ($ncell++ <= $num_cells)
						$attachments .= "<td>&nbsp;</td>";
					// and start new row
					$attachments .= "</tr>\n<tr>";
				}

				// force a new row after this attachment
				$ncell = $num_cells + 1;
				$td = "<td colspan=$num_cells>";
			} else {
				$td = "<td>";
			}
			$attachments .= $td.$att_link."</td>";

			$att_list[$att_id] = $att_name;
		}
		$attachments .= "</tr></table>";

		$att_icon = ($att_count>1) ? $GLOBALS['att_icon_multiple'] : att_icon($att_name);
		// Set drop down menu with attachments list
		$att_list_box = getListBox ("att_id", $att_list, "", 0, "window.location=('getfile.$ext?bn=$bn&att_id='+this.options[this.selectedIndex].value)");
		$att_list_form = "<form action=\"getfile.$ext\"><input type=\"hidden\" name=\"bn\" value=\"$bn\">".LABEL_ATTACHMENTS.$att_list_box.getSubmitButton('',LABEL_DOWNLOAD_FILE).'</form>';
	}

# Display message (static HTML file or dynamic according to the configuration
# ---------------------------------------------------------------------------
	$column_subject		= COLUMN_SUBJECT;
	$column_author		= COLUMN_AUTHOR;
	$label_posted		= LABEL_POSTED;
	$column_description	= COLUMN_DESCRIPTION;
	$label_add_reply	= LABEL_ADD_REPLY;

	$formated_date = formated_date ($unixdate);
	if (empty($show_profile_url)) {
		$label_posted_by_on = sprintf(LABEL_POSTED_BY_ON, "$username (".GUESTUSER_USERNAME.")", $formated_date);
	} else {
		$label_posted_by_on = sprintf(LABEL_POSTED_BY_ON, "<a href=\"$show_profile_url\">$userid</a>", $formated_date);
	}
	$label_hits			= sprintf(LABEL_HITS, $hits);
	$alt_email_user		= sprintf(ALT_SEND_EMAIL_TO, $username);

	if ($bn_doc_type == "static") {
		if (empty($filename) || $filename == "none") {
			$body = format_text($body);
			$doc=str_replace('"','\"', $bn_doc_var);
			eval ("echo stripSlashes(\"$doc\n\");");
		} elseif (file_exists ("$bn_dir_notes/$filename")) {
			readFile ("$bn_dir_notes/$filename");
		} else {
			MsgForm (sprintf(ERROR_NOTE_NOT_FOUND, $msg[0]), "list.$ext", "");
			display_footer ($header_footer);
		}
	} else {

		if ($mod_date>$unixdate) {
		  $last_edited = sprintf (NOTE_LAST_EDITED, formated_date($mod_date), $mod_userid);
		}

		if (isSet($body)) {
			$body = format_text($body);
		}

		if (!empty($pattern)) {
			$terms = split_search_terms(urldecode(stripslashes($pattern)));
			foreach($terms as $term) {
				if($term{0} != '-') {
					// $subject = preg_replace("/($term)/i", '<span class="hilite">\\1</span>', $subject);
					$body = preg_replace("/($term)/i", '<span class="hilite">\\1</span>', $body);
				}  
			}
		}

		// PICTURE / AVATAR
		$user_picture = trim($user_picture);
		if(!empty($user_picture)) {
			$user_picture = "<img src=\"$user_picture\" class=\"avatar\">";
		}

		$doc=str_replace('"','\"', $bn_doc_var);
		eval ("print stripSlashes(\"$doc\n\");");
	}

?>
