%%document-uplinkSend-member-popup%%
$Date $

%%preprocess%%
// new uplink loop
if ($_POST['uplinkid']=="create") {
	$this->encodedBaseUrl= urlencode("$this->baseUrl?method=uplinkSelect");
	$redirect= "$site->baseUrl/uplink-.popup?method=create&return=$this->encodedBaseUrl";
	bredirect($redirect);
	}
	
if ($_POST['edit']=="edit setting") {
	$this->encodedBaseUrl= urlencode("$this->baseUrl?method=uplinkSelect");
	$redirect= "$site->baseUrl/uplink-$_POST[uplinkid].popup?method=edit&return=$this->encodedBaseUrl";
	bredirect($redirect);
	}

// lookup uplink
$uplink= new Uplink;
$uplink->selectObjectById($_POST['uplinkid']);
if ($uplink->id=="") berror("Sorry, but uplink:$_POST[uplinkid] could not be found.",0);

// parse remoteurl
$url= parse_url($uplink->publishurl);
$rpchost= $url['host'];
$rpcport= $url['post'];
$rpcuri= $url['path'];
if ($url['query']!="") $rpcuri= $rpcuri.="?$url[query]";
if ($url['fragment']!="") $rpcuri= $rpcuri.="#$url[fragment]";
berror("document-uplinkSend: rpchost= $rpchost | rpcport= $rpcport | rpcuri= $rpcuri",1);

// is this a berylium xml-rpc implementation?
if (substr($rpcuri,0,10)=="/berylium/") $isBerylium= 1;
else $isBerylium= 0;

// format the object for display
if (!$isBerylium) {
	$this->beryliumToHtml();
	$folder->beryliumToHtml();
	}
$this->getRuntimeVars();

// title and location fixing
if ($this->title=="") $this->title= "Untitled ($this->created)";

// copyright
if ($this->copyright) {
	if ($this->copyright!="none") {
		$this->copyrightTag= "<div class='copyright'>$this->copyright</div>";
		}
	}

// folder copyright?
if ($this->copyright=="" && $folder->copyright!="") $this->copyrightTag= "<div class='copyright'>$folder->copyright</div>";

// FORMAT DESCRIPTION
if ($isBerylium) $this->descriptionTag= $this->description;
else $this->descriptionTag= "<blockquote>$this->description</blockquote>";

// BLOG ENTRY
if (!$isBerylium) {
	if ($uplink->flavor=="LiveJournal") {
		// LIVEJOURNAL has a civilised title field
		$this->blogentry="<div class='uplinked'>
$this->descriptionTag
$this->copyrightTag
<div class='site' style='clear: both;' />Uplinked from <a href='$folder->baseUrl'>$folder->title</a> | <a href='$this->htmlUrl'>Full $this->flavor</a></div></div>";
		}
	else {
		// BLOGGER needs everything in the entry
		$this->blogentry="<div class='uplinked'>
<div class='title'><a href='$this->htmlUrl'>$this->title</a></div>
$this->descriptionTag
$this->copyrightTag
<div class='site' style='clear: both;' />Uplinked from <a href='$folder->baseUrl'>$folder->title</a>. | <a href='$this->htmlUrl'>Full $this->flavor</a></div></div>";
		}
	$translation= array("\n"=>"", "\r"=>"");
	$this->blogentry=strtr($this->blogentry, $translation);
	}
else {
	// BERYLIUM uses bml
	$this->blogentry= $this->descriptionTag;
	$this->title= strip_tags(bparsebml($this->title));
	$properties= "title=$this->title^flavor=Uplinked $this->flavor^author=$this->author^copyright=$this->copyright^source=[$this->htmlUrl;$folder->title]";
	}
berror("This->blogentry= $this->blogentry",1);

// POST TO BLOG
include_once("$GLOBALS[beryliumroot]/external/xmlrpcutils.php");

// BERYLIUM APPKEY
$appkey= "95F16C6922351F7AAC3517B87863824A7CE14D53";

// very important, publish must be boolean!
settype($uplink->remotepublish, "boolean");

// unencode the password
$uplink->password= bgetword($uplink->remotepass);

// blogger.newPost
if ($uplink->flavor=="LiveJournal") {
	// see http://www.livejournal.com/doc/server/ljp.csp.xml-rpc.postevent.html
	if ($fotola->p_ljtype=="shared") $ljname= $uplink->remotename;
	$rpcresult = xu_rpc_http_concise( array(
							'method' => "LJ.XMLRPC.postevent",
							'args' => array("username"=>$uplink->remoteuser, "hpassword"=>md5($uplink->password), "event"=>$this->blogentry, "lineendings"=>"unix","subject"=>$this->title,"year"=>date("Y"),"mon"=>date("n"),"day"=>date("j"),"hour"=>date("G"),"min"=>date("i"), "usejournal"=>$ljname),
							'host' => $rpchost,
							'uri' => $rpcuri,
							'port' => $rpcport,
							'debug'=>0) );
	}
elseif ($uplink->flavor=="Berylium") {
	// berylium version
	$rpcresult = xu_rpc_http_concise( array(
							'method' => "blogger.newPost",
							'args' => array($appkey, $uplink->remoteid, $uplink->remoteuser, $uplink->password, $this->blogentry, $uplink->remotepublish, $properties),
							'host' => $rpchost,
							'uri' => $rpcuri,
							'port' => $rpcport,
							'debug'=>0) );
	$this->blogentry= bHtml("[$this->baseUrl;[b:$this->title]]\r\n\r\n".$this->blogentry);
	}
else {
	$rpcresult = xu_rpc_http_concise( array(
							'method' => "blogger.newPost",
							'args' => array($appkey, $uplink->remoteid, $uplink->remoteuser, $uplink->password, $this->blogentry, $uplink->remotepublish),
							'host' => $rpchost,
							'uri' => $rpcuri,
							'port' => $rpcport,
							'debug'=>0) );
	}
if (is_array($rpcresult) && $rpcresult['faultString']!="") {
	berror("Tried to post the entry via blogger.newPost to $uplink->publishurl, but got back the following error:<br />$rpcresult[faultString]",0);
	}
elseif (is_array($rpcresult) && $rpcresult['anum']!="") $this->postid= $rpcresult['anum'];
else $this->postid= $rpcresult;

// remoteurl and remoteblog
$this->remoteurl= $uplink->remoteurl;
$this->remotename= $uplink->remotename;

// UPDATE SITEMEMBER
$sitemember->p_lastuplink= $uplink->id;
$sitemember->applyPolicy();
$sitemember->updateObject();

%%header%%

%%css%%

%%template%%
<h1>Published to <a href="$this->remoteurl" target="_blank">$this->remotename</a></h1>
<p>Your weblog says this is post #$this->postid:<br />&nbsp;</p>
$this->blogentry
<p><a href='javascript: window.close()'>close window</a></p>

%%listrow%%

%%nullobject%%

%%footer%%

%%postprocess%%

%%end of context%%
