Esempio n. 1
0
 /**
  * Saves posts from enabled Source Feeds
  *
  * @return  void
  */
 public function RetrieveNewPostsTask()
 {
     $model = new Models\Feeds();
     $feeds = $model->loadAll();
     $model = new Models\Posts();
     $savedURLS = $model->loadURLs();
     foreach ($feeds as $feed) {
         if ($feed->enabled == 1 && filter_var($feed->url, FILTER_VALIDATE_URL) == TRUE) {
             try {
                 $ch = curl_init();
                 // set URL and other appropriate options
                 curl_setopt($ch, CURLOPT_URL, $feed->url);
                 curl_setopt($ch, CURLOPT_HEADER, 0);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                 $data = curl_exec($ch);
                 curl_close($ch);
                 if (!$data) {
                     $this->setError(Lang::txt('COM_FEEDAGGREGATOR_ERROR_READING_FEED', $feed->url));
                     continue;
                 }
                 $page = simplexml_load_string(utf8_encode($data));
                 if (isset($page->entry) == TRUE) {
                     $items = $page->entry;
                     $feedType = 'ATOM';
                 } else {
                     $items = $page->channel->item;
                     //gets the items of the channel
                     $feedType = 'RSS';
                 }
                 foreach ($items as $item) {
                     if ($feedType == 'ATOM') {
                         // get the href attribute of the orignal content link
                         foreach ($item->link->attributes() as $link) {
                             $link = $link;
                         }
                         if (in_array($link, $savedURLS) == FALSE) {
                             $post = new Models\Posts();
                             //create post object
                             $post->set('title', html_entity_decode(strip_tags($item->title)));
                             $post->set('feed_id', (int) $feed->id);
                             $post->set('status', 0);
                             //force new status
                             //ATOM original content link
                             $post->set('url', (string) $link);
                             if (isset($item->published) == TRUE) {
                                 $post->set('created', strtotime($item->published));
                             } else {
                                 $post->set('created', strtotime($item->updated));
                             }
                             $post->set('description', (string) html_entity_decode(strip_tags($item->content, '<img>')));
                             $post->store();
                             //save the post
                         }
                         // end check for prior existance
                     } else {
                         if ($feedType == 'RSS') {
                             if (in_array($item->link, $savedURLS) == FALSE) {
                                 $post = new Models\Posts();
                                 //create post object
                                 $post->set('title', (string) html_entity_decode(strip_tags($item->title)));
                                 $post->set('feed_id', (int) $feed->id);
                                 $post->set('status', 0);
                                 //force new status
                                 $post->set('created', strtotime($item->pubDate));
                                 $post->set('description', (string) html_entity_decode(strip_tags($item->description, '<img>')));
                                 $post->set('url', (string) $item->link);
                                 $post->store();
                                 //save the post
                             }
                         }
                     }
                 }
                 //end foreach
             } catch (Exception $e) {
                 $this->setError(Lang::txt('COM_FEEDAGGREGATOR_ERROR_READING_FEED', $feed->url));
                 continue;
             }
         }
         //end if
     }
     if ($this->getError()) {
         Notify::warning(implode('<br />', $this->getErrors()), $this->_option);
     }
     // Output messsage and redirect
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=posts&filterby=all', false), Lang::txt('COM_FEEDAGGREGATOR_GOT_NEW_POSTS'));
 }
Esempio n. 2
0
 /**
  * Save Source Feed form
  *
  * @return  void
  */
 public function saveTask()
 {
     //do a Request instead of a bind()
     $feed = new Models\Feeds();
     //get the URL first in order to validate
     $feed->set('url', Request::getVar('url'));
     $feed->set('name', Request::getVar('name'));
     $feed->set('id', Request::getVar('id'));
     $feed->set('enabled', Request::getVar('enabled'));
     $feed->set('description', Request::getVar('description'));
     //validate url
     if (!filter_var($feed->get('url'), FILTER_VALIDATE_URL)) {
         $this->feed = $feed;
         //redirect
         App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=feeds&task=new'), Lang::txt('COM_FEEDAGGREGATOR_ERROR_INVALID_URL'), 'warning');
     } else {
         if ($feed->store()) {
             // Output messsage and redirect
             App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller), Lang::txt('COM_FEEDAGGREGATOR_INFORMATION_UPDATED'));
         } else {
             App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller), Lang::txt('COM_FEEDAGGREGATOR_ERROR_UPDATE_FAILED'), 'warning');
         }
     }
 }