<?php
require_once 'modules/admin/models/DashboardPlugin.php';
require_once 'modules/admin/models/Announcements.php';
require_once 'newedge/classes/NE_Network.php';

class PluginRssreader extends DashboardPlugin
{
    var $hasTab = true;
    var $hasPanel = true;
    var $configJSFile = "PluginRssreaderConfig.js";
    var $configFunction = "PopulateConfigWindow";
    var $name = "RSS Reader";
    var $description = "Mini RSS reader to list the news you care about in the dashboard.";
    var $default = true;
    var $pluginsettings = null;
    var $jsLibs = array(
        'plugins/dashboard/rssreader/PluginRssreader.js'
    );
    
    function getTab()
    {
        return $this->getPanel();
    }

    function getPanel()
    {
        $this->tpl->define(array('rssfeed' => '../../plugins/dashboard/rssreader/PluginRssreader.tpl'));

        $this->_loadSettings();
        $this->tpl->setKey('latestLink', $this->pluginsettings['rssurl']);
        $this->tpl->setKey('rssitemcount',$this->pluginsettings['rssitemcount']);
        $this->tpl->setKey('rssfeedname',$this->pluginsettings['rssname']);

        $this->tpl->parse('rssfeed_out', 'rssfeed');
        return $this->tpl->fetch('rssfeed_out');
    }

    function doAction()
    {
        if (!isset($_REQUEST['pluginAction'])) $_REQUEST['pluginAction'] = 'listitems';

        switch($_REQUEST['pluginAction']) {
            case 'listitems':
                //return $this->listitems($this->tasks);
                break;
            case 'feedproxy':

                include_once 'newedge/classes/NE_Cache.php';
                // We need to store each feed url as a seperate cached item
                // so we don't overwrite other cached feeds
                $cache = new NE_Cache("RSSFEED/".md5($_REQUEST['feed']));
                $content = $cache->getContent();
                $lastupdated = $cache->GetLastUpdated();
                //Cache online users for 18000 seconds.
                if ($content != '' && $lastupdated != '' && $lastupdated >= time() - 18000) {
                    header('Content-Type: text/xml');
                    echo $content;
                    return;
                }else{
                    $feed = $_REQUEST['feed'];
                    if($feed != '' && strpos($feed, 'http') === 0){
                        header('Content-Type: text/xml');
						$xml = NE_Network::curlRequest($this->settings, $feed, false, false, true);
                        $xml = str_replace('<content:encoded>', '<content>', $xml);
                        $xml = str_replace('</content:encoded>', '</content>', $xml);
                        $xml = str_replace('</dc:creator>', '</author>', $xml);
                        $xml = str_replace('<dc:creator', '<author', $xml);

                        $cache->SetContent($xml);
                        $cache->Update();
                        echo $xml;
                        return;
                    }
                }
                break;
            case 'editRssURL':
                if (!isset($_POST['url'])) break;
                //count,url,name
                $settings = array('rssurl'=>$_POST['url'],'rssitemcount'=>$_POST['count'], 'rssname'=>$_POST['name']);
                $this->storeData('settings', serialize($settings));
                include_once 'newedge/classes/NE_Cache.php';
                $cache = new NE_Cache("RSSFEED");
                $cache->deleteCache();
                break;
            case 'getFeeds':


                if (isset($_SERVER['SCRIPT_URI'])) {
                    $URI = $_SERVER['SCRIPT_URI'];
                } else {
                    $URI = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
                }
                $url = mb_substr($URI, 0, strpos($URI, "admin"));
                $companyURL = $url.'index.php?fuse=admin&view=RssAnnouncements';
                $rssArray[] = array("url"=>'https://www.clientexec.com/members/index.php?fuse=admin&view=RssAnnouncements',"text"=>'ClientExec Announcements');
                $rssArray[] = array("url"=>$companyURL,"text"=>'Your Company Announcements');
                $rssArray[] = array("url"=>'http://rss.news.yahoo.com/rss/mostviewed',"text"=>'Yahoo Most Viewed News');
                return json_encode(array("items"=>$rssArray));
                break;
        }
    }

    /*
    function _storeTasks()
    {
        $settings = array('rssurl'=>$this->rssurl,'rssitemcount'=>$this->rssitemcount, 'rssname'=>$this->rssname);
        $this->storeData('tasks', serialize($settings));
    }
    */

    function _loadSettings(){
        if ($this->storedDataExists('settings'))
        {
            $this->pluginsettings = unserialize($this->retrieveData('settings'));
        }else{
            //default settings
            $rssurl = "http://www.clientexec.com/members/index.php?fuse=admin&view=RssAnnouncements";
            $rssname = "ClientExec Announcements";
            $rssitemcount = 3;
            $this->pluginsettings = array('rssurl'=>$rssurl,'rssitemcount'=>$rssitemcount, 'rssname'=>$rssname);
        }
    }

}

?>
