<?php

class readerapp extends plugin {

	protected function load () {
		$this -> title = 'Opiner Reader';
		$this -> author = 'Ovalio';
		$this -> url = 'http://opiner-cms.net/';
		$this -> description = 'Sledujte novinky na iných stránkach pomocou RSS čítačky Opiner Reader.';
		$this -> modes = array ('application', 'widget');
		$this -> cache = false;
		$this -> redactors = true;
		$this -> values = array (
			'widarts'	=> array ('int', 2, 'Maximálny počet článkov (widget)'),
		);
		$this -> tables = array (
			'feeds'		=> '`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `url` text NOT NULL, `title` tinytext NOT NULL, `time` int(11) unsigned DEFAULT 0, PRIMARY KEY (`id`)',
			'posts'		=> '`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `url` text NOT NULL, `title` tinytext NOT NULL, `description` tinytext DEFAULT NULL, `home` bigint(20) unsigned DEFAULT 0, PRIMARY KEY (`id`)',
		);
	}
	
	protected function application () {



/*--- Úvodné HTML ---*/

global $_REQUEST, $tohead;
$tohead[] = '<style type="text/css"> td { vertical-align: top; padding: 5px; } td p, td h3 { padding: 0; margin: 0; } </style>';
if (ADMIN) {
$return = '<h2>Správa odberov</h2>
<form action="' . _SiteForm . '" method="post">
<input type="text" name="feed" size="50" maxlength="255" />
<input type="submit" value="Pridať" />
</form>';



/*--- Mazanie odberu ---*/

if (isset ($_REQUEST['drop'])
and $this -> query ("DELETE FROM `{prefix}posts` WHERE `home` = " . adjust ($_REQUEST['drop'], true))
and $this -> query ("DELETE FROM `{prefix}feeds` WHERE `id` = " . adjust ($_REQUEST['drop'], true)))
$return .= getIcon ('info', 'Odber úspešne odstránený!');



/*--- Pridávanie novej položky ---*/

if (isset ($_REQUEST['feed'])
and !mysql_fetch_row ($this -> query ("SELECT `id` FROM `{prefix}feeds` WHERE `url` = '" . adjust ($_REQUEST['feed']) . "' LIMIT 1"))
and false !== ($xml = @simplexml_load_file ($_REQUEST['feed']))
and isset ($xml -> channel -> title, $xml -> channel -> item)
and $this -> query ("INSERT INTO `{prefix}feeds` VALUES (0, '" . adjust ($_REQUEST['feed']) . "', '" . adjust ($xml -> channel -> title) . "', UNIX_TIMESTAMP());")
and $id = mysql_insert_id ()) {
	foreach ($xml->channel->item as $values) {
		if (is_object ($values) and isset ($values -> title, $values -> link, $values -> description)
		and !mysql_fetch_row ($this -> query ("SELECT `id` FROM `{prefix}posts` WHERE `url` = '" . adjust ($values -> link) . "' LIMIT 1"))) {
			$text = (strlen ($values -> description) < 250) ? $values -> description : substr ($values -> description, 0, strrpos (substr ($values -> description, 0, 250), ' ')) . ' (...)';
			$this -> query ("INSERT INTO `{prefix}posts` VALUES (0, '" . adjust ($values -> link) . "', '" . adjust ($values -> title) . "', '" . adjust ($text) . "', $id);");
		};
	};
	$return .= getIcon ('info', 'Odber bol úspešne pridaný!');
};
} else $return = '';



/*--- Aktualizácia indexu ---*/

if ($info = mysql_fetch_assoc ($this -> query ("SELECT `id`, `url` FROM `{prefix}feeds` WHERE `time` < UNIX_TIMESTAMP() - 600 ORDER BY `time` ASC  LIMIT 1"))
and false !== ($xml = @simplexml_load_file ($info['url']))
and isset ($xml -> channel -> title, $xml -> channel -> item)) {
	foreach ($xml -> channel -> item as $values) {
		if (is_object ($values) and isset ($values -> title, $values -> link, $values -> description)
		and !mysql_fetch_row ($this -> query ("SELECT `id` FROM `{prefix}posts` WHERE `url` = '" . adjust ($values -> link) . "' LIMIT 1"))) {
			$text = (strlen ($values -> description) < 250) ? $values -> description : substr ($values -> description, 0, strrpos (substr ($values -> description, 0, 250), ' ')) . ' (...)';
			$this -> query ("INSERT INTO `{prefix}posts` VALUES (0, '" . adjust ($values -> link) . "', '" . adjust ($values -> title) . "', '" . adjust ($text) . "', {$info['id']});");
		};
	};
	$this -> query ("UPDATE `{prefux}feeds` SET `time` = UNIX_TIMESTAMP() WHERE `id` = {$info['id']} LIMIT 1");
};



/*--- Stránkovanie & HTML ---*/

$return .= '<table id="admintable" cellspacing="3px">
<tr><th>Názov odberu</th><th width="70px">Akcia</th></tr>'.n;
$sql = $this -> query ("SELECT `id`, `title` FROM `{prefix}feeds` ORDER BY `id` ASC");
while ($tab = mysql_fetch_assoc ($sql)) $return .= '<tr><td>' . $tab['title'] . '</td><td><a href="admin.php?app=' . $_REQUEST['app'] . '&amp;drop=' . $tab['id'] . '">Odstrániť</a></td></tr>'.n;
$return .= '</table>
<h2>Najnovšie články</h2>
<table width="100%" cellspacing="10px"><tr>'.n;



/*--- Prechod databázou ---*/

$limit = 9;
$page = (isset ($_REQUEST['page'])) ? adjust ($_REQUEST['page'], true) : 1;
$where = (isset ($_REQUEST['filter'])) ? ' AND `a`.`home` = ' . adjust ($_REQUEST['filter'], true) : '';
$pageadd = (isset ($_REQUEST['filter'])) ? '&amp;filter=' . adjust ($_REQUEST['filter'], true) : '';
list ($count) = mysql_fetch_row ($this -> query ("SELECT COUNT(*) FROM `{prefix}posts` as `a` WHERE 1 = 1$where"));
if ($count > $limit) {
	$return .= '<div id="pagelinks">'.n;
	for ($i = 1; $i <= ceil ($count / $limit); ++$i) $return .= ' <a href="?app=' . $_REQUEST['app'] . '&amp;page=' . $i . $pageadd . '">' . $i . '</a>'.n;
	$return .= '</div>'.n;
};
$sql = $this -> query ("SELECT `b`.`title` as `feed`, `b`.`id`, `a`.`url`, `a`.`title`, `a`.`description` FROM `{prefix}posts` as `a`, `{prefix}feeds` as `b` WHERE `a`.`home` = `b`.`id`$where ORDER BY `a`.`id` DESC LIMIT " . (($page-1)*$limit) . ", $limit");
while ($tab = mysql_fetch_assoc ($sql)) {
	$return .= '<td><h3><a href="' . $tab['url'] . '" target="_blank">' . $tab['title'] . '</a></h3><p>' . $tab['description'] . ' <em>v <a href="?app=' . $_REQUEST['app'] . '&amp;filter=' . $tab['id'] . '">' . $tab['feed'] . '</a></em></p></td>'.n;
	if (@++$int % 3 == 0 and $int < $limit) $return .= '</tr><tr>'.n;
};
return $return . '</table>';
	}



	protected function widget () {
		if ($info = mysql_fetch_assoc ($this -> query ("SELECT `id`, `url` FROM `{prefix}feeds` WHERE `time` < UNIX_TIMESTAMP() - 600 ORDER BY `time` ASC LIMIT 1"))
		and false !== ($xml = @simplexml_load_file ($info['url']))
		and isset ($xml -> channel -> title, $xml -> channel -> item)) {
			foreach ($xml -> channel -> item as $values) {
				if (is_object ($values) and isset ($values -> title, $values -> link, $values -> description)
				and !mysql_fetch_row ($this -> query ("SELECT `id` FROM `{prefix}posts` WHERE `url` = '" . adjust ($values -> link) . "' LIMIT 1"))) {
					$text = (strlen ($values -> description) < 250) ? $values -> description : substr ($values -> description, 0, strrpos (substr ($values -> description, 0, 250), ' ')) . ' (...)';
					$this -> query ("INSERT INTO `{prefix}posts` VALUES (0, '" . adjust ($values -> link) . "', '" . adjust ($values -> title) . "', '" . adjust ($text) . "', {$info['id']});");
				};
			};
			$this -> query ("UPDATE `{prefux}feeds` SET `time` = UNIX_TIMESTAMP() WHERE `id` = {$info['id']} LIMIT 1");
		};
		$sql = $this -> query ("SELECT `url`, `title`, `description` FROM `{prefix}posts` ORDER BY `id` DESC LIMIT " . $this -> config ['widarts']);
		while ($info = mysql_fetch_assoc ($sql))
		@$return .= '<h3><a href="' . $info['url'] . '" target="_blank">' . $info['title'] . '</a></h3><p>' . $info['description'] . '</p>';
		if (!isset ($return)) return '<p>Neboli pridané zatiaľ žiadne feedy, prípadne ešte nemajú žiadne príspevky!</p>';
		return $return;
	}
};