Classes

File content/feed/ATOM.class.php

File content/feed/ATOM.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: 
<?php
/**
 * This class could load a feed by its url or by a FeedData element and
 * export it to the ATOM format
 * @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: 2016 11 14
 * @since       PHPBoost 2.0 - 2008 04 21
 * @contributor mipel <mipel@phpboost.com>
*/

class ATOM extends Feed
{
    const DEFAULT_ATOM_TEMPLATE = 'framework/content/syndication/atom.tpl';

    ## Public Methods ##
    /**
     * Builds a new ATOM object
     * @param string $module_id its module_id
     * @param string $feed_name the feeds name / type. default is Feed::DEFAULT_FEED_NAME
     * @param int $id_cat the feed category id
     */
    public function __construct($module_id, $feed_name = Feed::DEFAULT_FEED_NAME, $id_cat = 0)
    {
        parent::__construct($module_id, $feed_name, $id_cat);
        $this->tpl = new FileTemplate(self::DEFAULT_ATOM_TEMPLATE);
    }

    /**
     * Loads a feed by its url
     * @param string $url the feed url
     */
    public function load_file($url)
    {
        if (($file = @file_get_contents($url)) !== false)
        {
            $this->data = new FeedData();
            if (preg_match('`<entry>(.*)</entry>`isu', $file))
            {
                $expParsed = explode('<entry>', $file);
                $nbItems = (count($expParsed) - 1) > $nbItems ? $nbItems : count($expParsed) - 1;

                $this->data->set_date(preg_match('`<updated>(.*)</updated>`isu', $expParsed[0], $var) ? $var[1] : '');
                $this->data->set_title(preg_match('`<title>(.*)</title>`isu', $expParsed[0], $var) ? $var[1] : '');
                $this->data->set_link(preg_match('`<link href="(.*)"/>`isu', $expParsed[0], $var) ? $var[1] : '');
                $this->data->set_host(preg_match('`<link href="(.*)"/>`isu', $expParsed[0], $var) ? $var[1] : '');

                for ($i = 1; $i <= $nbItems; $i++)
                {
                    $item = new FeedItem();
                    $item->set_title(preg_match('`<title>(.*)</title>`isu', $expParsed[$i], $title) ? $title[1] : '');
                    $item->set_link(preg_match('`<link href="(.*)"/>`isu', $expParsed[$i], $url) ? $url[1] : '');
                    $item->set_guid(preg_match('`<id>(.*)</id>`isu', $expParsed[$i], $guid) ? $guid[1] : '');
                    $item->set_desc(preg_match('`<summary>(.*)</summary>`isu', $expParsed[$i], $desc) ? $desc[1] : '');
                    $item->set_date(preg_match('`<updated>(.*)</updated>`isu', $expParsed[$i], $date) ? new Date(strtotime($date[1]), Timezone::SERVER_TIMEZONE) : null);

                    $enclosure = preg_match('`<enclosure rel="enclosure" url="(.*)" length="(.*)" type="(.*)" />`isu', $expParsed[$i]);
                    if ($enclosure)
                    {
                        $enclosure_item = new FeedItemEnclosure();
                        $enclosure_item->set_lenght($enclosure[2]);
                        $enclosure_item->set_type($enclosure[3]);
                        $enclosure_item->set_url($enclosure[1]);
                        $item->set_enclosure($enclosure_item);
                    }

                    $this->data->add_item($item);
                }
                return true;
            }
            return false;
        }
        return false;
    }
}
?>