summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBernhard Posselt <nukeawhale@gmail.com>2013-03-21 16:32:36 +0100
committerBernhard Posselt <nukeawhale@gmail.com>2013-03-21 16:32:36 +0100
commitac84b27965f5a1aec859e389f099fb844e33de46 (patch)
treec98d7aace90fcb442208349918a3e71cd9a7691a /lib
parentf475d882d0a76908400e9857f7e8a4ae8ad8a752 (diff)
reorganize folder
Diffstat (limited to 'lib')
-rw-r--r--lib/api.php166
-rw-r--r--lib/backgroundjob.php91
-rw-r--r--lib/controller.php108
-rw-r--r--lib/request.php83
-rw-r--r--lib/response.php218
-rw-r--r--lib/search.php38
-rw-r--r--lib/security.php104
-rw-r--r--lib/share/item.php44
-rw-r--r--lib/utils.php208
9 files changed, 0 insertions, 1060 deletions
diff --git a/lib/api.php b/lib/api.php
deleted file mode 100644
index 28f471207..000000000
--- a/lib/api.php
+++ /dev/null
@@ -1,166 +0,0 @@
-<?php
-
-/**
-* ownCloud - App Template Example
-*
-* @author Bernhard Posselt
-* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
-*
-* This library is free software; you can redistribute it and/or
-* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
-* License as published by the Free Software Foundation; either
-* version 3 of the License, or any later version.
-*
-* This library is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
-*
-* You should have received a copy of the GNU Affero General Public
-* License along with this library. If not, see <http://www.gnu.org/licenses/>.
-*
-*/
-
-
-namespace OCA\News;
-
-/**
- * This is used to wrap the owncloud static api calls into an object to make the
- * code better abstractable for use in the dependency injection container
- *
- * Extend this to your needs
- */
-class API {
-
- private $appName;
-
- /**
- * @param string $appName: the name of your application
- */
- public function __construct($appName){
- $this->appName = $appName;
- }
-
-
- /**
- * @return the name of your application
- */
- public function getAppName(){
- return $this->appName;
- }
-
-
- /**
- * @return: the user id of the current user
- */
- public function getUserId(){
- return \OCP\USER::getUser();
- }
-
-
- /**
- * Sets the current navigation entry to the currently running app
- */
- public function activateNavigationEntry(){
- \OCP\App::setActiveNavigationEntry($this->appName);
- }
-
-
- /**
- * Adds a new javascript file
- * @param string $scriptName: the name of the javascript in js/
- * without the suffix
- */
- public function addScript($scriptName, $appName=null){
- if($appName === null ){
- $appName = $this->appName;
- }
- \OCP\Util::addScript($appName, $scriptName);
- }
-
-
- /**
- * Adds a new css file
- * @param string $styleName: the name of the css file in css/
- * without the suffix
- */
- public function addStyle($styleName){
- \OCP\Util::addStyle($this->appName, $styleName);
- }
-
-
- /**
- * @brief shorthand for addScript for files in the 3rdparty directory
- * @param string $name: the name of the file without the suffix
- */
- public function add3rdPartyScript($name){
- \OCP\Util::addScript($this->appName . '/3rdparty', $name);
- }
-
-
- /**
- * @brief shorthand for addStyle for files in the 3rdparty directory
- * @param string $name: the name of the file without the suffix
- */
- public function add3rdPartyStyle($name){
- \OCP\Util::addStyle($this->appName . '/3rdparty', $name);
- }
-
- /**
- * Looks up a systemwide defined value
- * @param string $key: the key of the value, under which it was saved
- * @return the saved value
- */
- public function getSystemValue($key){
- return \OCP\Config::getSystemValue($key, '');
- }
-
-
- /**
- * Sets a new systemwide value
- * @param string $key: the key of the value, under which will be saved
- * @param $value: the value that should be stored
- */
- public function setSystemValue($key, $value){
- return \OCP\Config::setSystemValue($key, $value);
- }
-
-
- /**
- * Shortcut for setting a user defined value
- * @param $key the key under which the value is being stored
- * @param $value the value that you want to store
- */
- public function setUserValue($key, $value){
- \OCP\Config::setUserValue($this->getUserId(), $this->appName, $key, $value);
- }
-
-
- /**
- * Shortcut for getting a user defined value
- * @param $key the key under which the value is being stored
- */
- public function getUserValue($key){
- return \OCP\Config::getUserValue($this->getUserId(), $this->appName, $key);
- }
-
-
- /**
- * Returns the translation object
- * @return the translation object
- */
- public function getTrans(){
- return \OC_L10N::get($this->appName);
- }
-
-
- public function getLocalFilePath($path){
- return \OC_Filesystem::getLocalFile($path);
- }
-
-
- public function openEventSource(){
- return new \OC_EventSource();
- }
-
-} \ No newline at end of file
diff --git a/lib/backgroundjob.php b/lib/backgroundjob.php
deleted file mode 100644
index 098ff8393..000000000
--- a/lib/backgroundjob.php
+++ /dev/null
@@ -1,91 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Jakob Sack
-* @copyright 2012 Jakob Sack owncloud@jakobsack.de
-*
-* This library is free software; you can redistribute it and/or
-* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
-* License as published by the Free Software Foundation; either
-* version 3 of the License, or any later version.
-*
-* This library is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
-*
-* You should have received a copy of the GNU Affero General Public
-* License along with this library. If not, see <http://www.gnu.org/licenses/>.
-*
-*/
-
-namespace OCA\News;
-
-/**
- * This class maps a feed to an entry in the feeds table of the database.
- */
-class Backgroundjob {
- static public function sortFeeds( $a, $b ) {
- if( $a->getId() == $b->getId() ) {
- return 0;
- }
- elseif( $a->getId() < $b->getId() ) {
- return -1;
- }
- else{
- return 1;
- }
- }
-
- static public function run() {
- if( \OC::$CLI ) {
- self::cliStep();
- }
- else{
- self::webStep();
- }
- }
-
- static private function cliStep() {
- $feedmapper = new FeedMapper();
-
- // Iterate over all feeds
- $feeds = $feedmapper->findAll();
- foreach( $feeds as $feed ) {
- self::updateFeed( $feedmapper, $feed );
- }
- }
-
- static private function webStep() {
- // Iterate over all users
- $lastid = \OCP\Config::getAppValue('news', 'backgroundjob_lastid',0);
-
- $feedmapper = new FeedMapper();
- $feeds = $feedmapper->findAll();
- usort( $feeds, array( 'OCA\News\Backgroundjob', 'sortFeeds' ));
-
- $done = false;
- foreach( $feeds as $feed ) {
- if( $feed->getId() > $lastid ) {
- // set lastid BEFORE updating feed!
- \OCP\Config::setAppValue('news', 'backgroundjob_lastid',$feed->getId());
- $done = true;
- self::updateFeed( $feedmapper, $feed );
- }
- }
-
- if( !$done ) {
- \OCP\Config::setAppValue('news', 'backgroundjob_lastid',0);
- }
- }
-
- static private function updateFeed( $feedmapper, $feed ) {
- $newfeed = null;
- $newfeed = Utils::fetch( $feed->getUrl() );
- if( $newfeed !== null ) {
- $feedmapper = new FeedMapper();
- $newfeedid = $feedmapper->save($newfeed, $feed->getFolderId() );
- }
- }
-}
diff --git a/lib/controller.php b/lib/controller.php
deleted file mode 100644
index 292e25a18..000000000
--- a/lib/controller.php
+++ /dev/null
@@ -1,108 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Bernhard Posselt
-* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
-*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
-*
-*/
-
-namespace OCA\News;
-
-class Controller {
-
- protected $userId;
- protected $appName;
- protected $request;
- protected $api;
- protected $trans;
-
- public function __construct($request, $api){
- $this->api = $api;
- $this->userId = $api->getUserId();
- $this->appName = $api->getAppName();
- $this->request = $request;
- $this->trans = $api->getTrans();
- }
-
-
- /**
- * @brief lets you access post and get parameters by the index
- * @param string $key: the key which you want to access in the $_POST or
- * $_GET array. If both arrays store things under the same
- * key, return the value in $_POST
- * @param $default: the value that is returned if the key does not exist
- * @return: the content of the array
- */
- protected function params($key, $default=null){
- $postValue = $this->request->getPOST($key);
- $getValue = $this->request->getGET($key);
-
- if($postValue !== null){
- return $postValue;
- }
-
- if($getValue !== null){
- return $getValue;
- }
-
- return $default;
- }
-
- /**
- * Shortcut for accessing an uploaded file through the $_FILES array
- * @param string $key: the key that will be taken from the $_FILES array
- * @return the file in the $_FILES element
- */
- protected function getUploadedFile($key){
- return $this->request->getFILES($key);
- }
-
-
- /**
- * Binds variables to the template and prints it
- * The following values are always assigned: userId, trans
- * @param $templateName the name of the template
- * @param $arguments an array with arguments in $templateVar => $content
- * @param string $renderAs: admin, user or blank: admin renders the page on
- * the admin settings page, user renders a normal
- * owncloud page, blank renders the template alone
- */
- protected function render($templateName, $arguments=array(),
- $renderAs='user'){
- $response = new TemplateResponse($this->appName, $templateName);
- $response->setParams($arguments);
- $response->renderAs($renderAs);
- return $response;
- }
-
-
- /**
- * @brief renders a json success
- * @param array $params an array which will be converted to JSON
- */
- protected function renderJSON($params=array()){
- $response = new JSONResponse($this->appName);
- $response->setParams($params);
- return $response;
- }
-
-
- /**
- * @brief renders a json error
- * @param string $msg: the error message
- * @param string $file: the file that it occured in
- * @param array $params an array which will be converted to JSON
- */
- protected function renderJSONError($msg, $file="", $params=array()){
- $response = new JSONResponse($this->appName);
- $response->setParams($params);
- $response->setErrorMessage($msg, $file);
- return $response;
- }
-
-
-}
diff --git a/lib/request.php b/lib/request.php
deleted file mode 100644
index 013c9b80e..000000000
--- a/lib/request.php
+++ /dev/null
@@ -1,83 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Bernhard Posselt
-* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
-*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
-*
-*/
-
-namespace OCA\News;
-
-
-/**
- * Encapsulates user id, $_GET and $_POST arrays for better testability
- */
-class Request {
-
- private $get;
- private $post;
- private $userId;
- private $files;
-
- /**
- * @param string $userId: the id of the current user
- * @param array $get: the $_GET array
- * @param array $post: the $_POST array
- * @param array $files the $_FILES array
- */
- public function __construct($userId, $get=array(), $post=array(), $files=array()) {
- $this->get = $get;
- $this->post = $post;
- $this->userId = $userId;
- $this->files = $files;
- }
-
-
- /**
- * Returns the get value or the default if not found
- * @param string $key: the array key that should be looked up
- * @param string $default: if the key is not found, return this value
- * @return the value of the stored array
- */
- public function getGET($key, $default=null){
- if(isset($this->get[$key])){
- return $this->get[$key];
- } else {
- return $default;
- }
- }
-
-
- /**
- * Returns the get value of the files array
- * @param string $key: the array key that should be looked up
- * @return the value of the stored array
- */
- public function getFILES($key){
- if(isset($this->files[$key])){
- return $this->files[$key];
- } else {
- return null;
- }
- }
-
-
- /**
- * Returns the get value or the default if not found
- * @param string $key: the array key that should be looked up
- * @param string $default: if the key is not found, return this value
- * @return the value of the stored array
- */
- public function getPOST($key, $default=null){
- if(isset($this->post[$key])){
- return $this->post[$key];
- } else {
- return $default;
- }
- }
-
-}
diff --git a/lib/response.php b/lib/response.php
deleted file mode 100644
index 993d061d3..000000000
--- a/lib/response.php
+++ /dev/null
@@ -1,218 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Bernhard Posselt
-* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
-*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
-*
-*/
-
-namespace OCA\News;
-
-
-abstract class Response {
-
- private $headers;
-
- protected function __construct(){
- $this->headers = array();
- }
-
- /**
- * Adds a new header to the response that will be called before the render
- * function
- * @param string header: the string that will be used in the header() function
- */
- public function addHeader($header){
- array_push($this->headers, $header);
- }
-
-
- /**
- * Renders all headers
- */
- public function render(){
- foreach($this->headers as $value) {
- header($value);
- }
- }
-
-
-}
-
-
-/**
- * Prompts the user to download the a textfile
- */
-class TextDownloadResponse extends Response {
-
- private $content;
- private $filename;
- private $contentType;
-
- /**
- * Creates a response that prompts the user to download the file
- * @param string $content: the content that should be written into the file
- * @param string $filename: the name that the downloaded file should have
- * @param string $contentType: the mimetype that the downloaded file should have
- */
- public function __construct($content, $filename, $contentType){
- parent::__construct();
- $this->content = $content;
- $this->filename = $filename;
- $this->contentType = $contentType;
-
- $this->addHeader('Content-Disposition: attachment; filename="' . $filename . '"');
- $this->addHeader('Content-Type: ' . $contentType);
- }
-
-
- /**
- * Simply sets the headers and returns the file contents
- * @return the file contents
- */
- public function render(){
- parent::render();
- return $this->content;
- }
-
-
-}
-
-
-/**
- * Response for a normal template
- */
-class TemplateResponse extends Response {
-
- private $templateName;
- private $params;
- private $appName;
- private $renderAs;
-
- /**
- * @param string $appName: the name of your app
- * @param string $templateName: the name of the template
- */
- public function __construct($appName, $templateName) {
- parent::__construct();
- $this->templateName = $templateName;
- $this->appName = $appName;
- $this->params = array();
- $this->renderAs = 'user';
- }
-
-
- /**
- * @brief sets template parameters
- * @param array $params: an array with key => value structure which sets template
- * variables
- */
- public function setParams($params){
- $this->params = $params;
- }
-
-
- /**
- * @brief sets the template page
- * @param string $renderAs: admin, user or blank: admin renders the page on
- * the admin settings page, user renders a normal
- * owncloud page, blank renders the template alone
- */
- public function renderAs($renderAs='user'){
- $this->renderAs = $renderAs;
- }
-
-
- /**
- * Returns the rendered html
- * @return the rendered html
- */
- public function render(){
- parent::render();
-
- if($this->renderAs === 'blank'){
- $template = new \OCP\Template($this->appName, $this->templateName);
- } else {
- $template = new \OCP\Template($this->appName, $this->templateName,
- $this->renderAs);
- }
-
- foreach($this->params as $key => $value){
- $template->assign($key, $value, false);
- }
-
- return $template->fetchPage();
- }
-
-}
-
-
-/**
- * A renderer for JSON calls
- */
-class JSONResponse extends Response {
-
- private $name;
- private $data;
- private $appName;
-
- /**
- * @param string $appName: the name of your app
- */
- public function __construct($appName) {
- parent::__construct();
- $this->appName = $appName;
- $this->data = array();
- $this->error = false;
- }
-
- /**
- * @brief sets values in the data json array
- * @param array $params: an array with key => value structure which will be
- * transformed to JSON
- */
- public function setParams($params){
- $this->data['data'] = $params;
- }
-
-
- /**
- * @brief in case we want to render an error message, also logs into the
- * owncloud log
- * @param string $message: the error message
- * @param string $file: the file where the error occured, use __FILE__ in
- * the file where you call it
- */
- public function setErrorMessage($msg, $file){
- $this->error = true;
- $this->data['msg'] = $msg;
- \OCP\Util::writeLog($this->appName, $file . ': ' . $msg, \OCP\Util::ERROR);
- }
-
-
- /**
- * Returns the rendered json
- * @return the rendered json
- */
- public function render(){
- parent::render();
-
- ob_start();
-
- if($this->error){
- \OCP\JSON::error($this->data);
- } else {
- \OCP\JSON::success($this->data);
- }
-
- $result = ob_get_contents();
- ob_end_clean();
-
- return $result;
- }
-
-} \ No newline at end of file
diff --git a/lib/search.php b/lib/search.php
deleted file mode 100644
index d9cf1d294..000000000
--- a/lib/search.php
+++ /dev/null
@@ -1,38 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
-*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
-*
-*/
-
-class OC_Search_Provider_News extends OC_Search_Provider{
-
- function search($query) {
- if (!OCP\App::isEnabled('news')) {
- return array();
- }
-
- $feedMapper = new OCA\News\FeedMapper(OCP\USER::getUser());
- $results=array();
-
- if($feedMapper->feedCount() > 0) {
- $allFeeds = $feedMapper->findAll();
-
- $l = new OC_l10n('news');
-
- foreach($allFeeds as $feed) {
- if(substr_count(strtolower($feed->getTitle()), strtolower($query)) > 0) {
- $link = \OC_Helper::linkToRoute('news_index_feed', array('feedid' => $feed->getId()));
- $results[]=new OC_Search_Result($feed->getTitle(), '', $link, (string)$l->t('News'));
- }
- }
- }
- return $results;
-
- }
-}
diff --git a/lib/security.php b/lib/security.php
deleted file mode 100644
index 99258285c..000000000
--- a/lib/security.php
+++ /dev/null
@@ -1,104 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Bernhard Posselt
-* Copyright (c) 2012 - Bernhard Posselt <nukeawhale@gmail.com>
-*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
-*
-*/
-
-
-namespace OCA\News;
-
-
-/**
- * This class is a simple object with getters and setters and allows
- * finegrained controll over security checks
- * All security checks are enabled by default
- */
-class Security {
-
- private $csrfCheck;
- private $loggedInCheck;
- private $appEnabledCheck;
- private $isAdminCheck;
- private $appName;
-
- /**
- * @param string $appName: the name of the app
- */
- public function __construct($appName){
- $this->appName = $appName;
-
- // enable all checks by default
- $this->csrfCheck = true;
- $this->loggedInCheck = true;
- $this->appEnabledCheck = true;
- $this->isAdminCheck = true;
- }
-
-
- public function setCSRFCheck($csrfCheck){
- $this->csrfCheck = $csrfCheck;
- }
-
- public function setLoggedInCheck($loggedInCheck){
- $this->loggedInCheck = $loggedInCheck;
- }
-
- public function setAppEnabledCheck($appEnabledCheck){
- $this->appEnabledCheck = $appEnabledCheck;
- }
-
- public function setIsAdminCheck($isAdminCheck){
- $this->isAdminCheck = $isAdminCheck;
- }
-
-
- /**
- * Runs all security checks
- */
- public function runChecks() {
-
- if($this->loggedInCheck){
- \OCP\JSON::checkLoggedIn();
- }
-
- if($this->appEnabledCheck){
- \OCP\JSON::checkAppEnabled($this->appName);
- }
-
- if($this->isAdminCheck){
- \OCP\JSON::checkAdminUser();
- }
-
- }
-
-
- /**
- * Runs all the security checks for AJAX requests
- */
- public function runAjaxChecks(){
- if($this->csrfCheck){
- \OCP\JSON::callCheck();
- }
-
- if($this->loggedInCheck){
- \OCP\JSON::checkLoggedIn();
- }
-
- if($this->appEnabledCheck){
- \OCP\JSON::checkAppEnabled($this->appName);
- }
-
- if($this->isAdminCheck){
- \OCP\JSON::checkAdminUser();
- }
-
- }
-
-
-} \ No newline at end of file
diff --git a/lib/share/item.php b/lib/share/item.php
deleted file mode 100644
index db9ab0f48..000000000
--- a/lib/share/item.php
+++ /dev/null
@@ -1,44 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
-*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
-*
-*/
-
-namespace OCA\News;
-
-class Share_Backend_News_Item implements \OCP\Share_Backend {
-
- const FORMAT_ITEM = 0;
-
- private static $item;
-
- public function isValidSource($itemSource, $uidOwner) {
- $itemMapper = new ItemMapper($uidOwner);
- $this->item = $itemMapper->findById($itemSource);
- if ($this->item !== null) {
- return true;
- }
- return false;
- }
-
- public function generateTarget($itemSource, $shareWith, $exclude = null) {
- return $this->item->getTitle();
- }
-
- public function formatItems($items, $format, $parameters = null) {
- $formattedItems = array();
- foreach ($items as $item) {
- $itemMapper = new ItemMapper($item['uid_owner']);
- $formattedItem = $itemMapper->findById($item['item_source']);
- $formattedItems[] = $formattedItem;
- }
- return $formattedItems;
- }
-
-} \ No newline at end of file
diff --git a/lib/utils.php b/lib/utils.php
deleted file mode 100644
index 560114ca7..000000000
--- a/lib/utils.php
+++ /dev/null
@@ -1,208 +0,0 @@
-<?php
-/**
-* ownCloud - News app
-*
-* @author Alessandro Cosentino
-* Copyright (c) 2012 - Alessandro Cosentino <cosenal@gmail.com>
-*
-* This file is licensed under the Affero General Public License version 3 or later.
-* See the COPYING-README file
-*
-*/
-
-namespace OCA\News;
-
-// load SimplePie library
-//TODO: is this a suitable place for the following require?
-require_once 'news/3rdparty/SimplePie/autoloader.php';
-
-class Utils {
-
- /**
- * @brief Transform a date from UNIX timestamp format to MDB2 timestamp format
- * @param dbtimestamp a date in the UNIX timestamp format
- * @returns a date in the MDB2 timestamp format, or NULL if an error occurred
- */
- public static function unixtimeToDbtimestamp($unixtime) {
- if ($unixtime === null) {
- return null;
- }
- $dt = \DateTime::createFromFormat('U', $unixtime);
- if ($dt === false) {
- return null;
- }
- return $dt->format('Y-m-d H:i:s');
- }
-
- /**
- * @brief Transform a date from MDB2 timestamp format to UNIX timestamp format
- * @param dbtimestamp a date in the MDB2 timestamp format
- * @returns a date in the UNIX timestamp format, or NULL if an error occurred
- */
- public static function dbtimestampToUnixtime($dbtimestamp) {
- if ($dbtimestamp === null) {
- return null;
- }
- $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $dbtimestamp);
- if ($dt === false) {
- return null;
- }
- return $dt->format('U');
- }
-
- /**
- * @brief Fetch a feed from remote
- * @param url remote url of the feed
- * @returns an instance of OC_News_Feed
- */
- public static function fetch($url) {
- $spfeed = new \SimplePie_Core();
- $spfeed->set_feed_url( $url );
- $spfeed->enable_cache( false );
-
- if (!$spfeed->init()) {
- return null;
- }
-
- //temporary try-catch to bypass SimplePie bugs
- try {
- $spfeed->handle_content_type();
- $title = $spfeed->get_title();
-
- $items = array();
- if ($spitems = $spfeed->get_items()) {
- foreach($spitems as $spitem) {
- $itemUrl = $spitem->get_permalink();
- $itemTitle = $spitem->get_title();
- $itemGUID = $spitem->get_id();
- $itemBody = $spitem->get_content();
- $item = new Item($itemUrl, $itemTitle, $itemGUID, $itemBody);
-
- $spAuthor = $spitem->get_author();
- if ($spAuthor !== null) {
- $item->setAuthor($spAuthor->get_name());
- }
-
- //date in Item is stored in UNIX timestamp format
- $itemDate = $spitem->get_date('U');
- $item->setDate($itemDate);
-
- // associated media file, for podcasts
- $itemEnclosure = $spitem->get_enclosure();
- if($itemEnclosure !== null) {
- $enclosureType = $itemEnclosure->get_type();
- $enclosureLink = $itemEnclosure->get_link();
- if(stripos($enclosureType, "audio/") !== FALSE) {
- $enclosure = new Enclosure();
- $enclosure->setMimeType($enclosureType);
- $enclosure->setLink($enclosureLink);
- $item->setEnclosure($enclosure);
- }
- }
-
- $items[] = $item;
- }
- }
-
- $feed = new Feed($url, $title, $items);
-
- $favicon = $spfeed->get_image_url();
-
- if ($favicon !== null && self::checkFavicon($favicon)) { // use favicon from feed
- $feed->setFavicon($favicon);
- }
- else { // try really hard to find a favicon
- $webFavicon = self::discoverFavicon($url);
- if ($webFavicon !== null) {
- $feed->setFavicon($webFavicon);
- }
- }
- return $feed;
- }
- catch (Exception $e) {
- return null;
- }
- }
-
- /**
- * Perform a "slim" fetch of a feed from remote.
- * Differently from Utils::fetch(), it doesn't retrieve items nor a favicon
- *
- * @param url remote url of the feed
- * @returns an instance of OC_News_Feed
- */
- public static function slimFetch($url) {
- $spfeed = new \SimplePie_Core();
- $spfeed->set_feed_url( $url );
- $spfeed->enable_cache( false );
- $spfeed->set_stupidly_fast( true );
-
- if (!$spfeed->init()) {
- return null;
- }
-
- //temporary try-catch to bypass SimplePie bugs
- try {
- $title = $spfeed->get_title();
-
- $feed = new Feed($url, $title);
-
- return $feed;
- }
- catch (Exception $e) {
- return null;
- }
- }
-
- public static function checkFavicon($favicon) {
- if ($favicon === null || $favicon == false)
- return false;
-
- $file = new \SimplePie_File($favicon);
- // size in bytes
- $filesize = strlen($file->body);
-
- if($file->success && $filesize > 0 && $filesize < 50000) { //bigger files are not considered favicons
- $sniffer = new \SimplePie_Content_Type_Sniffer($file);
- if(substr($sniffer->get_type(), 0, 6) === 'image/') {
- $imgsize = getimagesize($favicon);
- if ($imgsize['0'] <= 32 && $imgsize['1'] <= 32) { //bigger images are not considered favicons
- return true;
- }
- }
- }
- return false;
- }
-
- public static function discoverFavicon($url) {
- //try webroot favicon
- $favicon = \SimplePie_Misc::absolutize_url('/favicon.ico', $url);
-
- if(self::checkFavicon($favicon))
- return $favicon;
-
- //try to extract favicon from web page
- $absoluteUrl = \SimplePie_Misc::absolutize_url('/', $url);
-
- $handle = curl_init ( );
- curl_setopt ( $handle, CURLOPT_URL, $absoluteUrl );
- curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, 1 );
- curl_setopt ( $handle, CURLOPT_FOLLOWLOCATION, TRUE );
- curl_setopt ( $handle, CURLOPT_MAXREDIRS, 10 );
-
- if ( FALSE!==($page=curl_exec($handle)) ) {
- preg_match ( '/<[^>]*link[^>]*(rel=["\']icon["\']|rel=["\']shortcut icon["\']) .*href=["\']([^>]*)["\'].*>/iU', $page, $match );
- if (1<sizeof($match)) {
- // the specified uri might be an url, an absolute or a relative path
- // we have to turn it into an url to be able to display it out of context
- $favicon = htmlspecialchars_decode ( $match[2] );
- // test for an url
- if (parse_url($favicon,PHP_URL_SCHEME)) {
- if(self::checkFavicon($favicon))
- return $favicon;
- }
- }
- }
- return null;
- }
-} \ No newline at end of file