/**
  * test add new message
  */
 public function testAdd()
 {
     $feedModel = new application_models_feeds();
     $currentDatetime = date('Y-m-d H:i:s');
     $this->model->add($feedModel->find(1)->current(), 'testmessage no 1');
     // error in feed set
     $this->assertEquals('1', $feedModel->find(1)->current()->error);
     // correct error message added
     $this->assertGreaterThan(0, $this->model->fetchAll()->count());
     $result = $this->model->fetchAll($this->model->select()->order('id DESC'))->current();
     $this->assertEquals($currentDatetime, $result->datetime);
     $this->assertEquals('testmessage no 1', $result->message);
     $this->assertEquals('1', $result->feed);
 }
 /**
  * updates a given feed
  * returns an error or true on success
  *
  * @return int timestamp for this refresh
  * @param mixed $feed current feed model
  */
 public function feed($feed)
 {
     @set_time_limit(Zend_Registry::get('config')->rss->timelimit);
     @error_reporting(E_ERROR);
     // logging
     $logger = Zend_Registry::get('logger');
     $logger->log('start feed fetching "' . $feed->name . '"', Zend_Log::DEBUG);
     // get feed plugin
     $logger->log('load feed plugin', Zend_Log::DEBUG);
     $messagesModel = new application_models_messages();
     $plugin = Zend_Controller_Action_HelperBroker::getStaticHelper('pluginloader')->getPlugin($feed->source);
     if ($plugin === false) {
         $logger->log('error loading feed plugin ' . $feed->source, Zend_Log::ERR);
         return $messagesModel->add($feed, 'unknown plugin');
     }
     // receive new content
     $logger->log('load feed content', Zend_Log::DEBUG);
     try {
         $plugin->load($feed->url);
     } catch (Exception $e) {
         $logger->log('error loading feed content: ' . $e->getMessage(), Zend_Log::ERR);
         return $messagesModel->add($feed, $e->getMessage());
     }
     // update html url of the feed
     $feed->htmlurl = $plugin->getHtmlUrl();
     $feed->save();
     // current date
     $now = Zend_Date::now();
     $now->sub(Zend_Registry::get('session')->deleteItems, Zend_Date::DAY);
     $logger->log('current date ' . $now, Zend_Log::DEBUG);
     // include htmLawed
     if (!function_exists('htmLawed')) {
         require Zend_Registry::get('config')->includePaths->library . '/htmLawed.php';
     }
     // insert new items in database
     $logger->log('start item fetching', Zend_Log::DEBUG);
     $itemsModel = new application_models_items();
     foreach ($plugin as $item) {
         // test date: continue with next if item too old
         $date = new Zend_Date($item->getDate(), Zend_Date::ISO_8601);
         if ($now->compare($date) == 1 && Zend_Registry::get('session')->deleteItems != 0) {
             $logger->log('item "' . $item->getTitle() . '" (' . $date . ') older than ' . Zend_Registry::get('session')->deleteItems . ' days', Zend_Log::DEBUG);
             continue;
         }
         // filter match?
         try {
             if ($this->filter($feed, $item) === false) {
                 continue;
             }
         } catch (Exception $e) {
             $messagesModel->add($feed, 'filter error');
             continue;
         }
         // item already in database?
         if ($this->itemExists($item) === true) {
             continue;
         }
         // insert new item
         $logger->log('---', Zend_Log::DEBUG);
         $logger->log('start insertion of new item "' . $item->getTitle() . '"', Zend_Log::DEBUG);
         // sanitize content html
         $content = htmLawed($item->getContent(), array("safe" => 1, "deny_attribute" => Zend_Registry::get('config')->rss->allowed->deniedattribs, "keep_bad" => 0, "comment" => 1, "cdata" => 1, "elements" => Zend_Registry::get('config')->rss->allowed->tags));
         $title = htmLawed($item->getTitle(), array("deny_attribute" => "*", "elements" => "-*"));
         $logger->log('item content sanitized', Zend_Log::DEBUG);
         $nitem = array('title' => $title, 'content' => $content, 'feed' => $feed->id, 'unread' => 1, 'starred' => 0, 'datetime' => $item->getDate(), 'uid' => $item->getId(), 'link' => htmLawed($item->getLink(), array("deny_attribute" => "*", "elements" => "-*")));
         $logger->log('item in database inserted', Zend_Log::DEBUG);
         // multimedia item: get and save thumbnail
         if ($plugin->multimedia) {
             try {
                 // download and generate thumbnail
                 $thumbnail = $this->generateThumbnail($item->getThumbnail());
                 $logger->log('thumbnail "' . $thumbnail . '" generated', Zend_Log::DEBUG);
                 // set thumbnailpath as content
                 $nitem = array_merge($nitem, array('content' => $thumbnail));
             } catch (Exception $e) {
                 $logger->log('thumbnail error ' . $e->getMessage(), Zend_Log::ERR);
                 $messagesModel->add($feed, $e->getMessage());
                 continue;
             }
         }
         // insert new item
         $itemsModel->insert($nitem);
         $logger->log('item inserted', Zend_Log::DEBUG);
     }
     // success: set lastrefresh
     $feed->lastrefresh = Zend_Date::now()->get(Zend_Date::TIMESTAMP);
     $feed->error = 0;
     $feed->save();
     // cleanup old items
     $logger->log('cleanup old items', Zend_Log::DEBUG);
     $this->cleanupOldItems();
     // cleanup old message items
     $messagesModel->cleanup();
     // destroy feed object (prevent memory issues)
     $logger->log('destroy feed object', Zend_Log::DEBUG);
     $plugin->destroy();
     return $feed->lastrefresh;
 }