Classes

File content/feed/Feed.class.php

File content/feed/Feed.class.php

  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
/**
 * This class could be used to export feeds
 * <div classs="message-helper notice">Do not use this class, but one of its children like RSS or ATOM</div>
 * @package     Content
 * @subpackage  Feed
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 10 23
 * @since       PHPBoost 2.0 - 2008 04 21
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

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';

    /**
     * @var int Module ID
     */
    private $module_id = '';
    /**
     *
     * @var int ID cat
     */
    private $id_cat = 0;
    /**
     *
     * @var string Feed Name
     */
    private $name = '';
    /**
     *
     * @var string The feed as a string
     */
    private $str = '';
    /**
     *
     * @var string The feed Template to use
     */
    protected $tpl = null;
    /**
     *
     * @var string The data structure
     */
    private $data = null;

    /**
     * Builds a new feed object
     * @param string $module_id its module_id
     * @param string $name the feeds name / type. default is DEFAULT_FEED_NAME
     * @param int $id_cat the feed category id
     */
    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;
    }

    /**
     * Loads a FeedData element
     * @param FeedData $data the element to load
     */
    public function load_data($data) { $this->data = $data; }
    /**
     * Loads a feed by its url
     * @param string $url the feed url
     */
    public function load_file($url) { }

    /**
     * Exports the feed as a string parsed by the <$tpl> template
     * @param mixed $template If false, uses de default tpl. If an associative array,
     * uses the default tpl but assigns it the array vars first.
     * It could also be a Template object
     * @param int $number the number of item to display
     * @param int $begin_at the first item to display
     * @return string The exported feed
     */
    public function export($template = false, $number = 10, $begin_at = 0)
    {
        if ($template === false)
        {    // A specific template is used
            $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();
    }

    /**
     * Loads the feed data in cache and export it
     * @return string the exported feed
     */
    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 '';
    }

    /**
     * Send the feed data in the cache
     */
    public function cache()
    {
        self::update_cache($this->module_id, $this->name, $this->data, $this->id_cat);
    }

    /**
     * Returns true if the feed data are in the cache
     * @return bool true if the feed data are in the cache
     */
    public function is_in_cache()
    {
        return file_exists($this->get_cache_file_name());
    }

    /**
     * Returns the feed data cache filename
     * @return string the feed data cache filename
     */
    public function get_cache_file_name()
    {
        return FEEDS_PATH . $this->module_id . '_' . $this->name . '_' . $this->id_cat . '.php';
    }

    /**
     * Clear the cache of the specified module_id.
     * @param mixed $module_id the module module_id or false. If false,
     * Clear all feeds data from the cache
     * @static
     */
    public static function clear_cache($module_id = false)
    {
        $folder = new Folder(FEEDS_PATH);
        $files = null;
        if ($module_id !== false)
        {   // Clear only this module cache
            $files = $folder->get_files('`' . $module_id . '_.*`');
            foreach ($files as $file)
            {
                $file->delete();
            }
        }
        else
        {   // Clear the whole cache
            AppContext::get_cache_service()->clear_syndication_cache();
        }
    }


    /**
     * Update the cache of the $module_id, $name, $idcat feed with $data
     * @param string $module_id the module id
     * @param string $name the feed name / type
     * @param &FeedData $data the data to put in the cache
     * @param int $idcat the feed data category
     * @static
     */
    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;
    }

    /**
     * Export a feed
     * @param string $module_id the module id
     * @param string $name the feed name / type
     * @param int $idcat the feed data category
     * @param mixed $tpl If false, uses de default tpl. If an associative array,
     * uses the default tpl but assigns it the array vars first.
     * It could also be a Template object
     * @param int $number the number of item to display
     * @param int $begin_at the first item to display
     * @return string The exported feed
     * @static
     */
    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;
        // Get the cache content or recreate it if not existing
        $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) )
            {   // If the module is not installed or doesn't have the get_feed_data_struct
                // functionality we break
                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 '';
        }
    }

    /**
     * @static
     * Generates the code which shows all the feeds formats.
     * @param string $feed_url Feed URL
     * @return string The HTML code to display.
     */
    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();
    }
}
?>