1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312:
<?php
define('FEEDS_PATH', PATH_TO_ROOT . '/cache/syndication/');
define('ERROR_GETTING_CACHE', 'Error regenerating and / or retrieving the syndication cache of the %s (%s)');
class Feed
{
const DEFAULT_FEED_NAME = 'master';
private $module_id = '';
private $id_cat = 0;
private $name = '';
private $str = '';
protected $tpl = null;
private $data = null;
public function __construct($module_id, $name = self::DEFAULT_FEED_NAME, $id_cat = 0)
{
$this->module_id = $module_id;
$this->name = $name;
$this->id_cat = $id_cat;
}
public function load_data($data) { $this->data = $data; }
public function load_file($url) { }
public function export($template = false, $number = 10, $begin_at = 0)
{
if ($template === false)
{
$tpl = clone $this->tpl;
}
else
{
$tpl = clone $template;
}
if (!empty($this->data))
{
$tpl->put_all(array(
'DATE' => $this->data->get_date(),
'DATE_RFC822' => $this->data->get_date_rfc2822(),
'DATE_RFC3339' => $this->data->get_date_iso8601(),
'DATE_TEXT' => $this->data->get_date_text(),
'THIS_YEAR' => date('Y'),
'TITLE' => $this->data->get_title(),
'RAW_TITLE' => TextHelper::htmlspecialchars($this->data->get_title()),
'U_LINK' => $this->data->get_link(),
'HOST' => $this->data->get_host(),
'DESC' => ContentSecondParser::export_html_text($this->data->get_desc()),
'RAW_DESC' => TextHelper::htmlspecialchars($this->data->get_desc()),
'LANG' => $this->data->get_lang()
));
$items = $this->data->subitems($number, $begin_at);
foreach ($items as $item)
{
$enclosure = $item->get_enclosure();
$tpl->assign_block_vars('item', array(
'TITLE' => $item->get_title(),
'RAW_TITLE' => TextHelper::htmlspecialchars($item->get_title()),
'U_LINK' => $item->get_link(),
'U_GUID' => $item->get_guid(),
'DESC' => ContentSecondParser::export_html_text($item->get_desc()),
'RAW_DESC' => TextHelper::htmlspecialchars($item->get_desc()),
'DATE' => $item->get_date(),
'DATE_RFC822' => $item->get_date_rfc2822(),
'DATE_RFC3339' => $item->get_date_iso8601(),
'DATE_HOUR' => $item->get_hours(),
'DATE_MINUTES' => $item->get_minutes(),
'DATE_TEXT' => $item->get_date_text(),
'C_IMG' => ($item->get_image_url() != ''),
'U_IMG' => $item->get_image_url(),
'C_ENCLOSURE' => $enclosure !== null,
'ENCLOSURE_LENGHT' => $enclosure !== null ? $enclosure->get_lenght() : '',
'ENCLOSURE_TYPE' => $enclosure !== null ? $enclosure->get_type() : '',
'ENCLOSURE_URL' => $enclosure !== null ? $enclosure->get_url() : ''
));
}
}
return $tpl->render();
}
public function read()
{
if ($this->is_in_cache())
{
$include = include($this->get_cache_file_name());
if ($include && isset($__feed_object) && !empty($__feed_object))
{
$this->data = $__feed_object;
return $this->export();
}
}
return '';
}
public function cache()
{
self::update_cache($this->module_id, $this->name, $this->data, $this->id_cat);
}
public function is_in_cache()
{
return file_exists($this->get_cache_file_name());
}
public function get_cache_file_name()
{
return FEEDS_PATH . $this->module_id . '_' . $this->name . '_' . $this->id_cat . '.php';
}
public static function clear_cache($module_id = false)
{
$folder = new Folder(FEEDS_PATH);
$files = null;
if ($module_id !== false)
{
$files = $folder->get_files('`' . $module_id . '_.*`');
foreach ($files as $file)
{
$file->delete();
}
}
else
{
AppContext::get_cache_service()->clear_syndication_cache();
}
}
private static function update_cache($module_id, $name, $data, $idcat = 0)
{
if ($data instanceof FeedData)
{
$file = new File(FEEDS_PATH . $module_id . '_' . $name . '_' . $idcat . '.php');
$file->write('<?php $__feed_object = TextHelper::unserialize(' . var_export($data->serialize(), true) . '); ?>');
$file->close();
return true;
}
return false;
}
public static function get_parsed($module_id, $name = self::DEFAULT_FEED_NAME, $idcat = 0, $template = false, $number = 10, $begin_at = 0)
{
if (!($template instanceof Template))
{
$template = new FileTemplate('framework/content/syndication/feed.tpl');
if (gettype($template) == 'array')
{
$template->put_all($template);
}
}
$feed_data_cache_file_exists = true;
$feed_data_cache_file = FEEDS_PATH . $module_id . '_' . $name . '_' . $idcat . '.php';
if (!file_exists($feed_data_cache_file))
{
$extension_provider_service = AppContext::get_extension_provider_service();
$provider = $extension_provider_service->get_provider($module_id);
if (!$provider->has_extension_point(FeedProvider::EXTENSION_POINT) )
{
return '';
}
$feed_provider = $provider->get_extension_point(FeedProvider::EXTENSION_POINT);
$data = $feed_provider->get_feed_data_struct($idcat);
$feed_data_cache_file_exists = self::update_cache($module_id, $name, $data, $idcat);
}
if ($feed_data_cache_file_exists)
{
include $feed_data_cache_file;
if (isset($__feed_object) && !empty($__feed_object))
{
$feed = new Feed($module_id, $name);
$feed->load_data($__feed_object);
return $feed->export($template, $number, $begin_at);
}
return '';
}
else
{
MessageHelper::display(sprintf(ERROR_GETTING_CACHE, $module_id, $idcat), MessageHelper::WARNING);
return '';
}
}
public static function get_feed_menu($module_id, $id_cat = 0)
{
$feed_menu = new FileTemplate('framework/content/syndication/menu.tpl');
$feed_menu->put_all(array(
'U_FEED_RSS' => SyndicationUrlBuilder::rss($module_id, $id_cat)->absolute(),
'U_FEED_ATOM' => SyndicationUrlBuilder::atom($module_id, $id_cat)->absolute()
));
return $feed_menu->render();
}
}
?>