Your IP : 216.73.216.240


Current Path : /home/juvelize/www/components/com_myrssreader/js/
Upload File :
Current File : /home/juvelize/www/components/com_myrssreader/js/checker.php

<?php
/**
* @package      My RSS Reader
* @copyright    Copyright (C) 2010 FalsinSoft. All rights reserved.
* @license      GNU/GPL
* @website      http://falsinsoft-joomla.blogspot.com
* @email        falsinsoft@gmail.com
* 
*/

require_once(JPATH_BASE.DS.'components'.DS.'com_myrssreader'.DS.'simplepie'.DS.'autoloader.php');
require_once(JPATH_BASE.DS.'components'.DS.'com_myrssreader'.DS.'simplepie'.DS.'idn'.DS.'idna_convert.class.php');

class CheckerEngine
{
	var $_sp = null;
	
	function __construct()
	{
		$this->_sp = new SimplePie();

		$strip_htmltags = $this->_sp->strip_htmltags;
		unset($strip_htmltags[array_search('object', $strip_htmltags)]);
		unset($strip_htmltags[array_search('param', $strip_htmltags)]);
		unset($strip_htmltags[array_search('embed', $strip_htmltags)]);
		unset($strip_htmltags[array_search('iframe', $strip_htmltags)]);
		unset($strip_htmltags[array_search('style', $strip_htmltags)]);
		$strip_htmltags = array_values($strip_htmltags); 

		$this->_sp->strip_htmltags($strip_htmltags);

		$strip_attributes = $this->_sp->strip_attributes;
		unset($strip_attributes[array_search('style', $strip_attributes)]);
		unset($strip_attributes[array_search('class', $strip_attributes)]);
		unset($strip_attributes[array_search('id', $strip_attributes)]);
		$strip_attributes = array_values($strip_attributes); 

		$this->_sp->strip_attributes($strip_attributes);

		$this->_sp->enable_cache(false);
		$this->_sp->enable_order_by_date(true);
		$this->_sp->set_timeout(10);
	}
	
	function __destruct() 
	{
		if($this->_sp != null)
		{
			$this->_sp->__destruct();
			unset($this->_sp);
			$this->_sp = null;
		}
	}
	
	function CheckRSS($rss_id, $max_feeds, $max_errors)
	{
		$db =& JFactory::getDBO();
		
		$db->setQuery("SELECT rss_url FROM #__myrssreader WHERE id = ".$rss_id);
		$rss_url = $db->loadResult();
		if($rss_url == null) return false;

		$this->_sp->set_feed_url($rss_url);
		$this->_sp->init();
		$this->_sp->handle_content_type();

		if($this->_sp->error() == false)
		{
			$feeds_number = ($max_feeds <= $this->_sp->get_item_quantity()) ? $max_feeds : $this->_sp->get_item_quantity();
			
			for($feed_idx = 0; $feed_idx < $feeds_number; $feed_idx++)
			{
				$item = $this->_sp->get_item($feed_idx);
				$new_feeds_hash_array[$feed_idx] = md5($item->get_link());
			}
			
			$db->setQuery("SELECT hash FROM #__myrssreader_cache WHERE rss_id = ".$rss_id." AND hash IN('".implode("','", $new_feeds_hash_array)."')");
			
			$current_feeds_hash_array = $db->loadResultArray();
			
			for($old_feed_found = false, $new_feeds_number = 0, $feed_idx = 0; $feed_idx < $feeds_number; $feed_idx++)
			{			
				if(in_array($new_feeds_hash_array[$feed_idx], $current_feeds_hash_array) == false)
				{
					$item = $this->_sp->get_item($feed_idx);
					
					$item_title   = $item->get_title();
					$item_date    = $item->get_date("Y-m-d H:i:s");
					$item_content = $item->get_content();
					$item_link 	  = $item->get_link();
					
					if($old_feed_found == true)
						$created = "'".$item_date."'";
					else
						$created = "NOW()";
						
					$item_title = trim($item_title);
					$item_title = str_replace(chr(10), "", $item_title);
					$item_title = str_replace(chr(13), "", $item_title);
					$item_title = addslashes($item_title);
					
					$item_content = trim($item_content);
					$item_content = addslashes($item_content);
				
					$query = "INSERT INTO #__myrssreader_cache "
							."(rss_id, hash, created, item_date, item_title, item_content, item_link) "
							."VALUES ("
							.$rss_id.", "
							."'".$new_feeds_hash_array[$feed_idx]."', "
							.$created.", "
							."'".$item_date."', "
							."'".$item_title."', "
							."'".$item_content."', "
							."'".$item_link."'"
							.")";
							
					$db->setQuery($query);
					
					if($db->query()) $new_feeds_number++;
				}
				else
				{
					$old_feed_found = true;
				}
			}
			
			$db->setQuery("DELETE FROM #__myrssreader_cache WHERE rss_id = ".$rss_id." AND hash NOT IN('".implode("','", $new_feeds_hash_array)."')");	
			$db->query();
			
			$query = "UPDATE #__myrssreader SET "
					."last_check = NOW(), check_errors = 0, "
					."last_feeds_number = ".$new_feeds_number.", checks_counter = (checks_counter + 1), "
					."new_feeds_average = ((new_feeds_average + ".$new_feeds_number.") / (checks_counter + 1)) "
					."WHERE id = ".$rss_id;
					
			$db->setQuery($query);
			$db->query();
			
			return true;
		}
		else
		{
			$db->setQuery("SELECT check_errors FROM #__myrssreader WHERE id = ".$rss_id);

			$check_errors = $db->loadResult();

			$query = "UPDATE #__myrssreader SET last_check = NOW(), ";
			
			if(($check_errors+1) >= $max_errors) 
				$query .= "published = 0";
			else
				$query .= "check_errors = (check_errors + 1)";
			
			$query .= " WHERE id = ".$rss_id;
			
			$db->setQuery($query);
			$db->query();	
			
			return false;
		}
	}	
}

?>