public static function getInstance() { if (self::$_instance === null) { self::$_instance = new self(); } return self::$_instance; }
public function addAction() { error_reporting(E_ALL); ini_set('display_errors', '0'); $this->_helper->layout->disableLayout(); $request = $this->getRequest(); $form = new Application_Form_FeadAdd(); if ($this->getParam('url')) { $feads = Application_Service_Fead::getInstance()->searchFead($this->getParam('url')); // no fead found if (is_null($feads)) { echo json_encode(['error' => $this->_translate->_('add_fead_error')]); // one fead found (add it) } else { if (1 === sizeof($feads)) { $userId = $this->_session->getSessionId(); $fead = new Application_Model_Entity_Fead($feads[0]); $id = Application_Model_FeadRepository::getInstance()->addFead($fead); echo json_encode(['success' => $id]); // more feads found (select one) } else { echo json_encode(['feads' => $feads]); } } } else { $this->view->form = $form; } }
public function addFead(Application_Model_Entity_Fead $fead) { $id = $this->urlExists($fead->getUrl()); if (!$id) { $this->_db->insert($this->_table, ['title' => $fead->getTitle(), 'url' => $fead->getUrl()]); $id = $this->_db->lastInsertId($this->_table); Application_Service_Fead::getInstance()->updateFead($fead->getUrl(), $id); } Application_Model_UserFeadRepository::getInstance()->add($id); return $id; }
public function addAction() { $this->_helper->layout->disableLayout(); $url = $this->getParam('u'); $status = 2; if (is_null($this->_session->getSessionId())) { $status = 3; } else { if ($url) { $feads = Application_Service_Fead::getInstance()->searchFead($url); if (is_null($feads) || 1 !== sizeof($feads)) { $status = 2; } else { $fead = new Application_Model_Entity_Fead($feads[0]); $id = Application_Model_FeadRepository::getInstance()->addFead($fead); $status = 1; } } } $this->redirect("/img/webimporter/{$status}.png"); }
public function updateFead($id, $debug = null) { $frontendOptions = ['lifetime' => 60, 'automatic_serialization' => true]; $backendOptions = ['cache_dir' => substr(APPLICATION_PATH, 0, strrpos(APPLICATION_PATH, '/')) . '/data/cache/']; $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); $output = ''; Zend_Registry::set('Zend_Locale', new Zend_Locale('de')); Zend_Locale::setCache($cache); $repository = Application_Model_ArticleRepository::getInstance(); $feadData = Application_Model_FeadRepository::getInstance()->get($id); $date = $repository->getLatestDateForFead($id); // close db connections Application_Model_ArticleRepository::getInstance()->closeConnection(); try { $fead = Application_Service_Fead::import(strtolower($feadData->getUrl())); } catch (Exception $e) { if ($debug) { $output .= $feadData->getTitle() . ': ' . $e->getMessage() . '<br>'; } try { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $feadData->getUrl()); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); $data = curl_exec($curl); curl_close($curl); $fead = Application_Service_Fead::importString($data); } catch (Exception $e) { $output .= $feadData->getTitle() . ': ' . $e->getMessage() . '<br>'; echo $feadData->getTitle() . ': ' . $e->getMessage(); return; } } if ($debug) { $output .= $feadData->getTitle() . '<br>'; } $articles = []; foreach ($fead as $i => $article) { $articleDate = $article->getDateModified(); $now = new Zend_Date(); $now = $now->toString('YYYY-MM-dd HH:mm:ss'); if (!is_null($articleDate) && $now > $articleDate->toString('YYYY-MM-dd HH:mm:ss')) { $articleDate = $articleDate->toString('YYYY-MM-dd HH:mm:ss'); } else { $articleDate = $now; } if ($debug) { $output .= $articleDate . ' > ' . $date . '?<br>'; } if ($articleDate <= $date) { break; } if ($debug) { $output .= '.'; } $content = trim($article->getDescription(), ['script']); $preview = substr(trim(strip_tags($content, ['a', 'img', 'script'])), 0, 255); $url = $article->getLink(); $thumb = strpos($article->getContent(), '<img'); if ($thumb) { $thumb = strpos($article->getContent(), 'src="http', $thumb); $end = strpos($article->getContent(), '"', $thumb + 5); $thumb = substr($article->getContent(), $thumb + 5, $end - $thumb - 5); } else { $thumb = null; } $articles[$i] = new Application_Model_Entity_Article(['feadId' => $feadData->getId(), 'title' => substr($article->getTitle(), 0, 255), 'preview' => $preview, 'url' => $url, 'thumb' => $thumb, 'dateCreated' => $article->getDateCreated()->toString('YYYY-MM-dd HH:mm:ss'), 'dateModified' => $articleDate, 'content' => $content]); } Application_Model_ArticleRepository::getInstance()->openConnection(); // import from oldest to youngest for ($i = sizeof($articles) - 1; $i >= 0; $i--) { Application_Model_ArticleRepository::getInstance()->addArticle($articles[$i]); } if ($debug) { echo $output; } }