<?php

/*
	[Discuz!] (C)2001-2006 Comsenz Inc.
	This is NOT a freeware, use is subject to license terms

	$RCSfile: supesite.func.php,v $
	$Revision: 1.9.6.1 $
	$Date: 2006/09/01 06:15:01 $
*/

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

function supe_xspace2forum($itemid, &$subject, &$message, &$special, &$iconid, &$trade) {
	global $db, $tablepre, $timestamp, $timeoffset, $dateformat, $timeformat, $supe_siteurl, $supe_tablepre;
	include_once language('misc');

	$query = $db->query("SELECT * FROM {$supe_tablepre}spaceitems WHERE itemid='$itemid'");
	if(!$item = $db->fetch_array($query)) {
		return array();
	}

	$subject .= str_replace('"', '&quot;', $item['subject']);

	switch($item['type']) {
		case 'blog':
			$special = 0;
			$query = $db->query("SELECT message, postip, weather, mood, customfieldid, customfieldtext FROM {$supe_tablepre}spaceblogs WHERE itemid='$itemid'");
			$item += $db->fetch_array($query);
			$message .= supe_html2bbcode($item['message']);
			break;
		case 'image':
			$special = 0;
			$query = $db->query("SELECT message, postip, customfieldid, customfieldtext FROM {$supe_tablepre}spaceimages WHERE itemid='$itemid'");
			$item += $db->fetch_array($query);
			$message .= "[b]$language[supe_picture_story]:[/b]  \r\n ".supe_html2bbcode($item[message]);
			break;
		case 'goods':
			$special = 2;
			$query = $db->query("SELECT * FROM {$supe_tablepre}spacegoods WHERE itemid='$itemid'");
			$item += $db->fetch_array($query);
			$message .= supe_html2bbcode($item['message']);

			$trade['trade_expiration'] = $timestamp + 30 * 86400;
			$trade['seller'] = $item['alipay'];
			$trade['item_name'] = $item['subject'];
			$trade['item_price'] = $item['price'];
			$trade['item_number'] = $item['salednum'] ? $item['salednum'] : 1;
			$trade['item_quality'] = $item['quality'] == 'new' ? 1 : 2;
			$trade['item_locus'] = $item['province'].' '.$item['city'];
			$trade['item_transport'] = $item['chargemode'] == 'buy' ? 2 : 1;
			$trade['postage_mail'] = $item['chargemail'];
			$trade['postage_express'] = $item['chargeexpress'];
			$trade['postage_ems'] = $item['chargeems'];
			$trade['item_type'] = 1;
			break;
		case 'file':
			$special = 0;
			$query = $db->query("SELECT message, relativetags, postip, relativeitemids, customfieldid, customfieldtext, includetags, filesize, filesizeunit, version, producer, downfrom, language, permission, system, remoteurl FROM {$supe_tablepre}spacefiles WHERE itemid='$itemid'");
			$item += $db->fetch_array($query);
			$message .= '[list]';
			$message .= "[*][b]$language[supe_software_size]:[/b] $item[filesize]\r\n";
			$message .= "[*][b]$language[supe_software_version]:[/b] $item[version]\r\n";
			$message .= "[*][b]$language[supe_software_producer]:[/b] $item[producer]\r\n";
			$message .= "[*][b]$language[supe_software_downfrom]:[/b] $item[downfrom]\r\n";
			$message .= "[*][b]$language[supe_software_language]:[/b] $item[language]\r\n";
			$message .= "[*][b]$language[supe_software_permission]:[/b] $item[permission] \r\n";
			$message .= "[*][b]$language[supe_software_system]:[/b] $item[system]\r\n";
			$item['digest'] = $language['supe_digest_'.intval($item['digest'])];
			$message .= "[*][b]$language[supe_digest_level]:[/b] $item[digest] \r\n";
			$message .= "[*][b]$language[supe_software_introduce]:[/b]\r\n".supe_html2bbcode($item[message])."\r\n\r\n";
			if($item['remoteurl'] = unserialize($item['remoteurl'])) {
				$message .= "[*][b]$language[supe_download_from_remote]:[/b]\r\n";
				foreach($item['remoteurl'] as $val) {
					$message .= "[url=$val[remoteurl]][/b]{$val[remoteurlname]}[/b][/url]\r\n";
				}
			}
			$message .= '[/list]';
			break;
		case 'link':
			$special = 0;
			$query = $db->query("SELECT url, message, postip, customfieldid, customfieldtext FROM {$supe_tablepre}spacelinks WHERE itemid='$itemid'");
			$item += $db->fetch_array($query);
			$message .= '[list]';
			$message .= "[*][b]$language[supe_linkurl]:[/b] [url]{$item[url]}[/url]\r\n";
			$message .= "[*][b]$language[supe_snapshot]:[/b] [url=$supe_siteurl/batch.snapshot.php?itemid=$item[itemid]]$language[supe_viewsnapshot][/url] \r\n";
			$message .= "[*][b]$language[supe_urldescription]:[/b]\r\n";
			$message .= supe_html2bbcode($item['message']);
			$message .= '[/list]';
			break;
		default:
			$special = 0;
	}
	$message_customfield = '';
	if($item['customfieldid'] && $item['customfieldtext']) {

		$customfielddata = unserialize($item['customfieldtext']);
		$query = $db->query("SELECT name, customfieldtext FROM {$supe_tablepre}customfields WHERE customfieldid='$item[customfieldid]'");

		$querydata = $db->fetch_array($query);
		$customfieldname = $querydata['name'];
		$customfieldstruct = unserialize($querydata['customfieldtext']);
		unset($querydata);
		$len = count($customfieldstruct);
		$message_customfield .= "[quote][b]{$customfieldname}[/b]:\r\n";

		for($i = 0; $i < $len; $i++) {
			switch($customfieldstruct[$i]['type']) {
				case 'input':
				case 'textarea':
				case 'select':
					$message_customfield .= "[*][b]{$customfieldstruct[$i][name]}[/b] : {$customfielddata[$i]} \r\n";
					break;
				case 'checkbox':
					$message_customfield .= "[*][b]{$customfieldstruct[$i][name]}[/b] : ".join(',', $customfielddata[$i])."\r\n";
					break;
			}
		}
		unset($customfieldstruct, $customfieldname, $len);
		$message_customfield .= "[/quote]\r\n";
	}

	$message_attachments = '';
	if($item['haveattach']) {

		$message_attachments = $item['type'] != 'file' ? "\r\n[b]$language[attach]:[/b]\r\n" : "[b]$language[supe_donwload_from_local]:[/b]  \r\n";
		$query = $db->query("SELECT aid, dateline, filename, subject, attachtype, isimage, size, filepath, thumbpath, downloads FROM {$supe_tablepre}attachments WHERE hash='$item[hash]'");
		while($attach = $db->fetch_array($query)) {

			if($attach['isimage']) {
				$attach['dateline'] = gmdate("$dateformat $timeformat", $attach['dateline'] + $timeoffset * 3600);

				if($item['message'] && !preg_match("/src=\"[^\"]*".preg_quote($attach[filepath], '/')."\"/is", $item['message'])) {
					$message_attachments .= "[url=$supe_siteurl/attachments/$attach[filepath]][img]$supe_siteurl/attachments/{$attach[thumbpath]}[/img][/url]\r\n";
					$message_attachments .= "[b]{$attach[subject]}[/b]  [$language[supe_dateline]:$attach[dateline]]\r\n";
				}

			} else {
				require_once DISCUZ_ROOT.'./include/attachment.func.php';

				$attach['dateline'] = gmdate("$dateformat $timeformat", $attach['dateline'] + $timeoffset * 3600);
				$attach['filesize'] = sizecount($attach['filesize']);
				$attach['filetype'] = supe_html2bbcode(attachtype(fileext($attach['attachment'])."\t".$attach['filetype']));
				$message_attachments .= "$attach[filetype] [url=$supe_siteurl/batch.download.php?aid=$attach[aid]][b]{$attach[filename]}[/b][/url]\r\n";
				$message_attachments .= "[$language[supe_dateline]:$attach[dateline] - $language[supe_download_count]:$attach[downloads]]\r\n";
			}
		}
	}
	$message .= $message_customfield.$message_attachments;
	return $item;
}

function supe_html2bbcode($text) {
	global $forum, $allowhtml;
	require_once DISCUZ_ROOT.'./include/editor.func.php';

	return $forum['allowhtml'] || $allowhtml ? $text : html2bbcode($text);
}

?>