<?php

class friendsapp extends plugin {

	protected function load () {
		$this -> title = 'Opiner Friends';
		$this -> author = 'Ovalio';
		$this -> url = 'http://friends.opiner-cms.net/';
		$this -> description = 'Spravujte svoj profil, čítajte novinky a mnoho iného z portálu Opiner Friends, komunity okolo Opiner CMS.';
		$this -> modes = array ('application', 'widget');
		$this -> cache = true;
		$this -> redactors = false;
		$this -> values = array (
			'ofhn'	=> array ('string', '', 'OFHN kľúč na prepojenie s Vaším profilom (Opiner Friends &raquo; Nastavenia)'),
		);
		$this -> tables = array ();
	}

	public function application () {

		// Úvodne "opatrenia"
		global $_CONFIG, $prefix, $tohead;
		if (strlen ($this -> config ['ofhn']) == 32
		and false !== ($xml = @simplexml_load_file ('http://friends.opiner-cms.net/cofr.php?mode=application&ofhn=' . $this -> config ['ofhn']))
		and isset ($xml['protocol'], $xml -> post, $xml -> profile) and $xml['protocol'] == '1.0') {
			$tohead[] = '<link rel="stylesheet" href="admin/remote/friends.css" type="text/css" />';
			$tohead[] = '<script src="admin/remote/friends.js" type="text/javascript" charset="utf-8"></script>';
			$out = '<h2>Opiner Friends</h2>
<div id="friendsPanel">
	<img src="' . $xml -> profile -> image . '" alt="' . $xml -> profile -> name . '" id="friendsImage" />
	<h4><a href="' . $xml -> profile -> url . '">' . $xml -> profile -> name . '</a></h4>
	<p><a href="' . $xml -> profile -> url . '">Profil</a> | <a href="mailto:' . $xml -> profile -> mail . '">Email</a> | <a href="' . $xml -> profile -> web . '" target="_blank">Web</a></p>
	<div id="status">' . $xml -> profile -> status . ' (<a href="http://friends.opiner-cms.net/index.php" onclick="return changeStatusForm(\'' . $this -> config['ofhn'] . '\');">Zmeniť</a>)</div>' . n;



	// Prijaté správy
	if (isset ($xml -> message)) {
		$out .= '	<h3>Neprečítaná pošta</h3>
	<div id="readMessage"></div>'.n;
		foreach ($xml -> message as $values) @$out .= '	<table class="newMessage" id="message' . $values -> id . '">
		<tr>
			<td rowspan="3"><img src="' . $values -> sender -> image . '" alt="' . $values -> sender -> name . '" /></td>
			<td><strong><a href="' . $values -> url . '" onclick="return readMessage(' . $values -> id . ', \'' . $values -> hash . '\');">' . $values -> subject . '</a></strong></td>
		</tr>
		<tr><td>od <a href="' . $values -> sender -> url . '">' . $values -> sender -> name . '</a></td></tr>
		<tr><td>' . date ('d.m.Y @H:i', adjust ($values -> time, true)) . '</td></tr>
	</table>'.n;
	};
	
	$out .= '</div>
<div id="friendsBoard">'.n;



			// Čítanie Opiner News
			foreach ($xml -> post as $values) {
				$out .= '<div class="cofrNew">
	<p>' . $values -> description . '</p>
	<p class="added">' . date ('d.m.Y H:i', adjust ($values -> time, true)) . '</p>
</div>'.n;
			};
			return $out . '</div>';
		} else return '<h2>Registrácia</h2>
<p>Ak ste registrovaný, vyplňte správny OFHN kľúč v nastaveniach tejto aplikácie!</p>
<form action="http://friends.opiner-cms.net/signup.php" method="post">
<fieldset>
	<input type="hidden" name="serial" value="' . _SerialNumber . '" />
	<input type="hidden" name="site" value="' . _SiteLink . '" />

	<strong>Nové heslo:</strong>
	<input type="password" name="password" /><br />
	<em>Heslo, pomocou ktorého sa budete prihlasovať na službu Opiner Friends</em>

	<strong>Vaše meno:</strong>
	<input type="text" name="name" value="' . $_CONFIG['author'] . '" /><br />
	<em>Vaše zobrazované meno, nemá súvis na prihlasovanie</em>

	<strong>Váš email:</strong>
	<input type="text" name="mail" value="' . $_CONFIG['mail'] . '" /><br />
	<em>Emailová adresa, pomocou ktorej bude prebiehať prihlasovanie</em><br />
</fieldset>
<input type="submit" value="Registrovať" />
</form>';
	}





	public function widget () {
		if (strlen ($this -> config ['ofhn']) == 32) {
			if (false !== ($xml = @simplexml_load_file ('http://friends.opiner-cms.net/cofr.php?mode=widget&ofhn=' . $this -> config ['ofhn']))
			and isset ($xml['protocol'], $xml -> profile) and $xml['protocol'] == '1.0') {
				$out = '<img src="' . $xml -> profile -> image . '" alt="' . $xml -> profile -> name . '" id="profilImage" />
<h3><a href="' . $xml -> profile -> url . '" title="Váš profil">' . $xml -> profile -> name . '</a></h3>
<p><em>' . $xml -> profile -> status . '</em><br /><small><a href="admin.php?app=' . $this -> apphash . '">Zmeniť status</a> | <a href="admin.php?app=' . $this -> apphash . '">Nástenka</a> | <a href="' . $xml -> profile -> url . '">Profil</a></small></p>'.n;



/*--- Prijaté správy ---*/

if (isset ($xml -> message)) {
	$out .= '<h3>Neprečítaná pošta</h3>'.n;
	foreach ($xml -> message as $values) @$out .= '<table class="newMessage">
	<tr>
		<td rowspan="3"><img src="' . $values -> sender -> image . '" alt="' . $values -> sender -> name . '" /></td>
		<td><strong><a href="' . $values -> url . '">' . $values -> subject . '</a></strong></td>
	</tr>
	<tr><td>od <a href="' . $values -> sender -> url . '">' . $values -> sender -> name . '</a></td></tr>
	<tr><td>' . date ('d.m.Y @H:i', adjust ($values -> time, true)) . '</td></tr>
</table>'.n;
};



/*--- Priatelia ---*/

if (isset ($xml -> friend)) {
	$out .= '<h3>Priatelia</h3>'.n;
	foreach ($xml -> friend as $friend) @$out .= '<table class="newMessage">
	<tr>
		<td rowspan="3"><img src="' . $friend -> image . '" alt="' . $friend -> name . '" /></td>
		<td><strong><a href="' . $friend -> url . '">' . $friend -> name . '</a></strong></td>
	</tr>
	<tr><td><a href="' . $friend -> web . '" target="_blank">' . $friend -> web . '</a></td></tr>
	<tr><td><a href="' . $friend -> send . '">napísať poštu</a></td></tr>
</table>'.n;
};
				return $out;
			} else return '<p>Spojenie so serverom Opiner Friends neúspešné!</p>';
		} else return '<p>Je potrebné najprv nastaviť OFHN kľúč v nastaveniach tejto aplikácie</p>';
	}
};