<?php

class seozavr
{
	var $version = '12';
	var $code = 'мой айдишник';
	var $encoding = 'windows-1251';
	var $affiliateLink = '<a href="http://seozavr.ru/index.php?id=1910">Купи прямые ссылки в статьях</a>';
	var $dbnum = '';
	var $catalogURL = '';
	var $catalogTitle = '';
	var $enableAffiliateLink = true;

	function getHead()
	{
		$content = '';
		$empty = true;
		if ($articles = @file_get_contents(dirname(__FILE__) . "/db{$this->dbnum}.txt"))
		{
			if (($articles = @unserialize($articles)) && count($articles) > 0 && $articles['expire'] >= time())
			{
				$code = isset($articles['code']) ? $articles['code'] : 'articleId';
				$empty = false;
				if (isset($_GET[$code]) && array_key_exists($_GET[$code], $articles))
				{
					$content .= "<title>{$articles[$_GET[$code]]['articleTitle']}</title>\n";
					if (isset($articles[$_GET[$code]]['articleDescription']) && !empty($articles[$_GET[$code]]['articleDescription']))
					$content .= "<meta name=\"description\" content=\"{$articles[$_GET[$code]]['articleDescription']}\"/>\n";
					if (isset($articles[$_GET[$code]]['articleKeywords']) && !empty($articles[$_GET[$code]]['articleKeywords']))
					$content .= "<meta name=\"keywords\" content=\"{$articles[$_GET[$code]]['articleKeywords']}\"/>\n";
				}
				else
				{
					foreach ($articles as $article) if (!empty($article['articleThemeName'])) $articleThemes[] = $article['articleThemeName'];
					if (isset($articleThemes) && is_array($articleThemes)) $articleThemes = @array_unique($articleThemes);
					if (isset($articleThemes) && is_array($articleThemes)) $title = 'Список статей на тему ' . implode(', ', $articleThemes);
					else $title = 'Список статей';
					$content .= "<title>$title</title>\n";
				}
			}
		}
		if ($empty)
		{
			if (empty($this->catalogTitle)) $content .= "<title>Здесь будет список статей</title>\n";
			else $content .= "<title>{$this->catalogTitle}</title>\n";
		}
		if ($this->encoding == 'windows-1251') return $content;
		else return iconv('windows-1251', $this->encoding, $content);
	}
	
	function getTitle()
	{
		$title = '';
		$empty = true;
		if ($articles = @file_get_contents(dirname(__FILE__) . "/db{$this->dbnum}.txt"))
		{
			if (($articles = @unserialize($articles)) && count($articles) > 0 && $articles['expire'] >= time())
			{
				$code = isset($articles['code']) ? $articles['code'] : 'articleId';
				$empty = false;
				if (isset($_GET[$code]) && array_key_exists($_GET[$code], $articles))
				{
					$title = "{$articles[$_GET[$code]]['articleTitle']}";
				}
				else
				{
					foreach ($articles as $article) if (!empty($article['articleThemeName'])) $articleThemes[] = $article['articleThemeName'];
					if (isset($articleThemes) && is_array($articleThemes)) $articleThemes = @array_unique($articleThemes);
					if (isset($articleThemes) && is_array($articleThemes)) $title = 'Список статей на тему ' . implode(', ', $articleThemes);
					else $title = 'Список статей';
				}
			}
		}
		if ($empty)
		{
			if (empty($this->catalogTitle)) $title = "Здесь будет список статей";
			else $title = $this->catalogTitle;			
		}
		if ($this->encoding == 'windows-1251') return $title;
		else return iconv('windows-1251', $this->encoding, $title);		
	}
	
	function getKeywords()
	{
		$keywords = '';
		if ($articles = @file_get_contents(dirname(__FILE__) . "/db{$this->dbnum}.txt"))
		{
			if (($articles = @unserialize($articles)) && count($articles) > 0 && $articles['expire'] >= time())
			{
				$code = isset($articles['code']) ? $articles['code'] : 'articleId';
				if (isset($_GET[$code]) && array_key_exists($_GET[$code], $articles))
				{
					if (isset($articles[$_GET[$code]]['articleKeywords']) && !empty($articles[$_GET[$code]]['articleKeywords']))
					$keywords = $articles[$_GET[$code]]['articleKeywords'];
				}
			}
		}
		if ($this->encoding == 'windows-1251') return $keywords;
		else return iconv('windows-1251', $this->encoding, $keywords);
	}

	function getDescription()
	{
		$description = '';
		if ($articles = @file_get_contents(dirname(__FILE__) . "/db{$this->dbnum}.txt"))
		{
			if (($articles = @unserialize($articles)) && count($articles) > 0 && $articles['expire'] >= time())
			{
				$code = isset($articles['code']) ? $articles['code'] : 'articleId';
				if (isset($_GET[$code]) && array_key_exists($_GET[$code], $articles))
				{
					if (isset($articles[$_GET[$code]]['articleDescription']) && !empty($articles[$_GET[$code]]['articleDescription']))
					$description = $articles[$_GET[$code]]['articleDescription'];
				}
			}
		}
		if ($this->encoding == 'windows-1251') return $description;
		else return iconv('windows-1251', $this->encoding, $description);
	}	
	
	function showHead()
	{
		echo $this->getHead();
	}

	function getBody()
	{
		$content = '';
		$empty = true;
		if ($articles = @file_get_contents(dirname(__FILE__) . "/db{$this->dbnum}.txt"))
		{
			if (($articles = @unserialize($articles)) && count($articles) > 0 && $articles['expire'] >= time())
			{
				$empty = false;
				$code = isset($articles['code']) ? $articles['code'] : 'articleId';
				if (isset($_GET[$code]) && array_key_exists($_GET[$code], $articles))
				{
					$content .= "<h1 class=\"articleName\">{$articles[$_GET[$code]]['articleName']}</h1>\n";
					$content .= "<div class=\"articleText\">{$articles[$_GET[$code]]['articleText']}</div>";
				}
				else
				{
					$themes = array();
					foreach ($articles as $articleId => $article)
					{
						if (is_numeric($articleId) && !empty($article['articleThemeName'])) $themes[$article['articleThemeName']][$articleId] = $article;
					}
					foreach ($themes as $theme => $articles)
					{
						$content .= "<div class=\"paragraph\">";
						$content .= "<h2 class=\"articleThemeName\">$theme</h2>";						
						foreach ($articles as $articleId => $article)
						{							
							if (strpos($_SERVER['REQUEST_URI'], '?'))
							$content .= "<p class=\"articleLink\"><a href=\"&$code=$articleId\">{$article['articleName']}</a></p>";
							else
							$content .= "<p class=\"articleLink\"><a href=\"?$code=$articleId\">{$article['articleName']}</a></p>";
						}
						$content .= "</div>";
					}
					if ($this->enableAffiliateLink) $content .= '<br /><p class="promoLink">' . $this->affiliateLink . '</p>';
				}
			}
		}
		if ($empty)
		{
			$content = "<!--seozavr_catalog-->";
			if ($this->enableAffiliateLink) $content .= '<p class="promoLink">' . $this->affiliateLink . '</p>';
		}
		if ($this->encoding == 'windows-1251') return $content;
		else return iconv('windows-1251', $this->encoding, $content);
	}
	
	function showBody()
	{
		echo $this->getBody();
	}

	function getList()
	{
		$content = '';
		$empty = true;
		if ($articles = @file_get_contents(dirname(__FILE__) . "/db{$this->dbnum}.txt"))
		{
			if (($articles = @unserialize($articles)) && count($articles) > 0 && $articles['expire'] >= time())
			{
				$code = isset($articles['code']) ? $articles['code'] : 'articleId';
				$empty = false;
				$themes = array();
				foreach ($articles as $articleId => $article)
				{
					if (!is_numeric($articleId)) continue;
					if (strpos($this->catalogURL, '?'))
					$content .= "<p class=\"articleLink\"><a href=\"{$this->catalogURL}&$code=$articleId\">{$article['articleName']}</a></p>";
					else
					$content .= "<p class=\"articleLink\"><a href=\"{$this->catalogURL}?$code=$articleId\">{$article['articleName']}</a></p>";
				}
			}
		}
		if ($empty) 
		{
			if ($this->enableAffiliateLink) $content = '<p class="promoLink">' . $this->affiliateLink . '</p>';
			$content .= '<!--seozavr_top-->';
		}
		if ($this->encoding == 'windows-1251') return $content;
		else return iconv('windows-1251', $this->encoding, $content);
	}
	
	function showList()
	{
		echo $this->getList(); 
	}

	function postArticles($articles)
	{
		$filename = "db{$this->dbnum}.txt";
		if(get_magic_quotes_gpc()) $articles = stripslashes($articles);
		if (@unserialize($articles))
		{
			if ($fp = @fopen($filename, 'wb'))
			{
				if (@fwrite($fp, $articles))
				{
					@fclose($fp);
					@chmod($filename, 438);
					return 0;
				}
				else return 3;
			}
			else return 2;
		}
		else return 1;
	}

	function getParams()
	{
		$params['version'] = $this->version;
		$params['affiliateLink'] = $this->affiliateLink;
		return serialize($params);
	}
}

if (isset($_POST['articles']))
{
	$seozavr = new seozavr();
	echo $seozavr->postArticles($_POST['articles']);
}

if (isset($_GET['params']))
{
	$seozavr = new seozavr();
	echo $seozavr->getParams();
}
?>