Example #1
0
 /**
  * Save Source Feed form
  *
  * @return  void
  */
 public function saveTask()
 {
     // get the URL first in order to validate
     $feed = Feed::blank();
     $feed->set('id', Request::getVar('id'));
     $feed->set('url', Request::getVar('url'));
     $feed->set('name', Request::getVar('name'));
     $feed->set('enabled', Request::getVar('enabled'));
     $feed->set('description', Request::getVar('description'));
     //validate url
     if (!filter_var($feed->get('url'), FILTER_VALIDATE_URL)) {
         Notify::error(Lang::txt('COM_FEEDAGGREGATOR_ERROR_INVALID_URL'));
         return $this->editTask($feed);
     }
     if (!$feed->save()) {
         Notify::error(Lang::txt('COM_FEEDAGGREGATOR_ERROR_UPDATE_FAILED'));
         return $this->editTask($feed);
     }
     // Output messsage and redirect
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller), Lang::txt('COM_FEEDAGGREGATOR_INFORMATION_UPDATED'));
 }
Example #2
0
 /**
  * Saves posts from enabled Source Feeds
  *
  * @return  void
  */
 public function RetrieveNewPostsTask()
 {
     $feeds = Feed::all()->rows();
     $savedURLS = array();
     $urls = Post::all()->select('url')->rows();
     foreach ($urls as $url) {
         $savedURLS[] = $url->url;
     }
     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 Post();
                             //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', Date::of($item->published)->toSql());
                             } else {
                                 $post->set('created', Date::of($item->updated)->toSql());
                             }
                             $post->set('description', (string) html_entity_decode(strip_tags($item->content, '<img>')));
                             $post->save();
                             //save the post
                         }
                         // end check for prior existance
                     } else {
                         if ($feedType == 'RSS') {
                             if (in_array($item->link, $savedURLS) == FALSE) {
                                 $post = new Post();
                                 //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', Date::of($item->pubDate)->toSql());
                                 $post->set('description', (string) html_entity_decode(strip_tags($item->description, '<img>')));
                                 $post->set('url', (string) $item->link);
                                 $post->save();
                                 //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'));
 }