diff options
author | Bernhard Posselt <nukeawhale@gmail.com> | 2013-01-27 07:15:53 +0400 |
---|---|---|
committer | Bernhard Posselt <nukeawhale@gmail.com> | 2013-01-27 07:15:53 +0400 |
commit | ae7393db3d99a7ac223ae917129cccd9f49888e3 (patch) | |
tree | 7f54b72b0d01c38afd1378365a67e4f192922423 /lib | |
parent | 483784caa38bd6131405ac474347a215584e30a5 (diff) |
merged the angularjs branch
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api.php | 163 | ||||
-rw-r--r-- | lib/backgroundjob.php | 12 | ||||
-rw-r--r-- | lib/collection.php | 34 | ||||
-rw-r--r-- | lib/controller.php | 108 | ||||
-rw-r--r-- | lib/feed.php | 67 | ||||
-rw-r--r-- | lib/feedmapper.php | 271 | ||||
-rw-r--r-- | lib/feedtypes.php | 22 | ||||
-rw-r--r-- | lib/folder.php | 76 | ||||
-rw-r--r-- | lib/foldermapper.php | 198 | ||||
-rw-r--r-- | lib/item.php | 194 | ||||
-rw-r--r-- | lib/itemmapper.php | 297 | ||||
-rw-r--r-- | lib/request.php | 83 | ||||
-rw-r--r-- | lib/response.php | 218 | ||||
-rw-r--r-- | lib/search.php | 16 | ||||
-rw-r--r-- | lib/security.php | 104 | ||||
-rw-r--r-- | lib/utils.php | 4 |
16 files changed, 697 insertions, 1170 deletions
diff --git a/lib/api.php b/lib/api.php new file mode 100644 index 000000000..d3d0e1f1b --- /dev/null +++ b/lib/api.php @@ -0,0 +1,163 @@ +<?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){ + \OCP\Util::addScript($this->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 index 9495086f0..098ff8393 100644 --- a/lib/backgroundjob.php +++ b/lib/backgroundjob.php @@ -27,10 +27,10 @@ namespace OCA\News; */ class Backgroundjob { static public function sortFeeds( $a, $b ) { - if( $a['id'] == $b['id'] ) { + if( $a->getId() == $b->getId() ) { return 0; } - elseif( $a['id'] < $b['id'] ) { + elseif( $a->getId() < $b->getId() ) { return -1; } else{ @@ -67,9 +67,9 @@ class Backgroundjob { $done = false; foreach( $feeds as $feed ) { - if( $feed['id'] > $lastid ) { + if( $feed->getId() > $lastid ) { // set lastid BEFORE updating feed! - \OCP\Config::setAppValue('news', 'backgroundjob_lastid',$feed['id']); + \OCP\Config::setAppValue('news', 'backgroundjob_lastid',$feed->getId()); $done = true; self::updateFeed( $feedmapper, $feed ); } @@ -82,10 +82,10 @@ class Backgroundjob { static private function updateFeed( $feedmapper, $feed ) { $newfeed = null; - $newfeed = Utils::fetch( $feed['url'] ); + $newfeed = Utils::fetch( $feed->getUrl() ); if( $newfeed !== null ) { $feedmapper = new FeedMapper(); - $newfeedid = $feedmapper->save($newfeed, $feed['folderid'] ); + $newfeedid = $feedmapper->save($newfeed, $feed->getFolderId() ); } } } diff --git a/lib/collection.php b/lib/collection.php deleted file mode 100644 index 30b926c00..000000000 --- a/lib/collection.php +++ /dev/null @@ -1,34 +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; - -/** - * This class models a collection, which is either a feed or a folder. - */ -class Collection { - - private $id; - - public function __construct($id) { - $this->id = $id; - } - - public function getId() { - return $this->id; - } - - public function setId($id) { - $this->id = $id; - } - -} diff --git a/lib/controller.php b/lib/controller.php new file mode 100644 index 000000000..292e25a18 --- /dev/null +++ b/lib/controller.php @@ -0,0 +1,108 @@ +<?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/feed.php b/lib/feed.php deleted file mode 100644 index e99ba3c75..000000000 --- a/lib/feed.php +++ /dev/null @@ -1,67 +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; - - -/** - * This class models a feed. - */ -class Feed extends Collection { - - private $title; - private $url; - private $items; //array that contains all the items of the feed - private $favicon; - - // if $items = null, it means that feed has not been fetched yet - // if $id = null, it means that the feed has not been stored in the db yet - public function __construct($url, $title, $items = null, $id = null) { - $this->url = $url; - $this->title = $title; - if ($items !== null) { - $this->items = $items; - } - if ($id !== null) { - parent::__construct($id); - } - } - - public function getUrl() { - return $this->url; - } - - public function getTitle() { - return $this->title; - } - - public function setTitle($title) { - $this->title = $title; - } - - public function getFavicon() { - return $this->favicon; - } - - public function setFavicon($favicon) { - $this->favicon = $favicon; - } - - public function setItems($items) { - $this->items = $items; - } - - public function getItems() { - return $this->items; - } - -} diff --git a/lib/feedmapper.php b/lib/feedmapper.php deleted file mode 100644 index a8e6d3b03..000000000 --- a/lib/feedmapper.php +++ /dev/null @@ -1,271 +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; - -/** - * This class maps a feed to an entry in the feeds table of the database. - */ -class FeedMapper { - - const tableName = '*PREFIX*news_feeds'; - private $userid; - - public function __construct($userid = null) { - if ($userid !== null) { - $this->userid = $userid; - } - else { - $this->userid = \OCP\USER::getUser(); - } - } - - /** - * @brief - * @param row a row from the feeds table of the database - * @returns an object of the class OCA\News\Feed - */ - public function fromRow($row) { - $url = $row['url']; - $title = $row['title']; - $id = $row['id']; - $feed = new Feed($url, $title, null, $id); - $favicon = $row['favicon_link']; - $feed->setFavicon($favicon); - - return $feed; - } - - /** - * @brief as a list that can be easily parsed using JSON - * @returns - */ - public function findAll() { - $query = 'SELECT * FROM ' . self::tableName; - $params = array(); - if( $this->userid ) { - $query = $query.' WHERE user_id = ?'; - $params[] = $this->userid; - } - - $stmt = \OCP\DB::prepare( $query ); - $result = $stmt->execute( $params ); - $feeds = array(); - while ($row = $result->fetchRow()) { - $url = $row['url']; - $id = $row['id']; - $folderid = $row['folder_id']; - $userid = $row['user_id']; - $title = $row['title']; - $feeds[] = array("url" => $url, "id" => $id, "folderid" => $folderid, - 'userid' => $userid, 'title' => $title ); - } - - return $feeds; - } - - /** - * @brief returns the number of feeds that a user has - * @returns the number of feeds that a user has - */ - public function feedCount() { - $query = 'SELECT COUNT(*) AS size FROM ' . self::tableName . ' WHERE user_id = ?'; - $stmt = \OCP\DB::prepare($query); - $result = $stmt->execute(array($this->userid))->fetchRow(); - return $result['size']; - } - - - /** - * @brief Retrieve a feed from the database - * @param id The id of the feed in the database table. - * @returns - */ - public function findById($id) { - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); - $result = $stmt->execute(array($id)); - if(!$row = $result->fetchRow()) - return null; - $feed = self::fromRow($row); - return $feed; - } - - /** - * @brief Retrieve all the feeds contained in the folder $folderid - * @param folderid The id of the folder in the database table. - * @returns a list of feeds - */ - public function findByFolderId($folderid) { - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE user_id = ? AND folder_id = ?'); - $result = $stmt->execute(array($this->userid, $folderid)); - $feeds = array(); - while ($row = $result->fetchRow()) { - $feed = self::fromRow($row); - $feeds[] = $feed; - } - return $feeds; - } - - - /** - * @brief Retrieve a feed and all its items from the database - * @param id The id of the feed in the database table. - * @returns an instance of OCA\News\Feed - */ - public function findWithItems($id) { - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); - $result = $stmt->execute(array($id)); - $row = $result->fetchRow(); - - $feed = self::fromRow($row); - $itemMapper = new ItemMapper(); - $items = $itemMapper->findById($id); - $feed->setItems($items); - - return $feed; - } - - /** - * @brief Find the id of a feed and all its items from the database - * @param url url of the feed - * @return id of the feed corresponding to the url passed as parameters - * null - if there is no such feed - */ - public function findIdFromUrl($url) { - $url_hash = md5($url); - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE url_hash = ?'); - $result = $stmt->execute(array($url_hash)); - $row = $result->fetchRow(); - $id = null; - if ($row != null) { - $id = $row['id']; - } - return $id; - } - - public function mostRecent() { - //FIXME: does something like SELECT TOP 1 * exists in pear/mdb2 ?? - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' ORDER BY lastmodified'); - $result = $stmt->execute(); - $row = $result->fetchRow(); - $id = null; - if ($row != null) { - $id = $row['id']; - } - return $id; - } - - /** - * @brief Save the feed and all its items into the database - * @param feed the feed to be saved - * @returns The id of the feed in the database table. - */ - //TODO: handle error case - public function save(Feed $feed, $folderid) { - $title = $feed->getTitle(); - $url = $feed->getUrl(); - $url_hash = md5($url); - - if(empty($title)) { - $l = \OC_L10N::get('news'); - $title = $l->t('no title'); - } - - $favicon = $feed->getFavicon(); - - //FIXME: Detect when feed contains already a database id - $feedid = $this->findIdFromUrl($url); - if ($feedid === null) { - $query = \OCP\DB::prepare(" - INSERT INTO " . self::tableName . - "(url, url_hash, title, favicon_link, folder_id, user_id, added, lastmodified) - VALUES (?, ?, ?, ?, ?, ?, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()) - "); - - $params=array( - $url, - $url_hash, - $title, - $favicon, - $folderid, - $this->userid - ); - $query->execute($params); - - $feedid = \OCP\DB::insertid(self::tableName); - } - else { - //update the db. it needs to be done, since it might be the first save after a full fetch - $stmt = \OCP\DB::prepare(' - UPDATE ' . self::tableName . - ' SET favicon_link = ? , lastmodified = UNIX_TIMESTAMP() , folder_id = ? - WHERE id = ? - '); - - $params=array( - $favicon, - $folderid, - $feedid - ); - $stmt->execute($params); - } - $feed->setId($feedid); - - $itemMapper = new ItemMapper(); - - $items = $feed->getItems(); - if ($items !== null) { - foreach($items as $item) { - $itemMapper->save($item, $feedid); - } - } - - return $feedid; - } - - - public function deleteById($id) { - if ($id == null) { - return false; - } - $stmt = \OCP\DB::prepare('DELETE FROM ' . self::tableName .' WHERE id = ? AND user_id = ?'); - - $result = $stmt->execute(array($id, $this->userid)); - - $itemMapper = new ItemMapper(); - //TODO: handle the value that the execute returns - $itemMapper->deleteAll($id); - - return true; - } - - public function delete(Feed $feed) { - $id = $feed->getId(); - return deleteById($id); - } - - public function deleteAll($folderid) { - if ($folderid == null) { - return false; - } - - $stmt = \OCP\DB::prepare('SELECT id FROM ' . self::tableName . ' WHERE folder_id = ? AND user_id = ?'); - - $result = $stmt->execute(array($folderid, $this->userid)); - while ($row = $result->fetchRow()) { - if(!self::deleteById($row['id'])) - return false; - } - - return true; - } -} diff --git a/lib/feedtypes.php b/lib/feedtypes.php deleted file mode 100644 index d330a5b2a..000000000 --- a/lib/feedtypes.php +++ /dev/null @@ -1,22 +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 FeedType { - const FEED = 0; - const FOLDER = 1; - const STARRED = 2; - const SUBSCRIPTIONS = 3; - const SHARED = 4; -};
\ No newline at end of file diff --git a/lib/folder.php b/lib/folder.php deleted file mode 100644 index 2e3c96a7c..000000000 --- a/lib/folder.php +++ /dev/null @@ -1,76 +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; - -/** - * This class models a folder that contains feeds. - */ -class Folder extends Collection { - - private $name; - private $children; - private $parent; - private $opened; - - public function __construct($name, $id = null, Collection $parent = null) { - $this->name = $name; - if ($id !== null) { - parent::__construct($id); - } - $this->children = array(); - if ($parent !== null) { - $this->parent = $parent; - } - if($this->opened === null){ - $this->opened = true; - } - } - - public function getName() { - return $this->name; - } - - public function setName($name) { - $this->name = $name; - } - - public function getOpened() { - return $this->opened; - } - - public function setOpened($opened) { - $this->opened = $opened; - } - - public function getParentId() { - if ($this->parent === null) { - return 0; - } - return $this->parent->getId(); - } - - public function addChild(Collection $child) { - $this->children[] = $child; - } - - public function addChildren($children) { - $this->children = $children; - } - - public function getChildren() { - return $this->children; - } - - - -}
\ No newline at end of file diff --git a/lib/foldermapper.php b/lib/foldermapper.php deleted file mode 100644 index 5d7145176..000000000 --- a/lib/foldermapper.php +++ /dev/null @@ -1,198 +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; - -/** - * This class maps a feed to an entry in the feeds table of the database. - */ -class FolderMapper { - - const tableName = '*PREFIX*news_folders'; - - private $userid; - - public function __construct($userid = null) { - if ($userid !== null) { - $this->userid = $userid; - } - else { - $this->userid = \OCP\USER::getUser(); - } - } - - - /** - * @brief Returns the forest (list of trees) of folders children of $parentid - * @param - * @returns - */ - public function childrenOf($parentid) { - $folderlist = array(); - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . - ' WHERE user_id = ? AND parent_id = ?'); - $result = $stmt->execute(array($this->userid, $parentid)); - - while( $row = $result->fetchRow()) { - $folderid = $row['id']; - $folder = new Folder($row['name'], $folderid); - $folder->setOpened($row['opened']); - $children = self::childrenOf($folderid); - $folder->addChildren($children); - $folderlist[] = $folder; - } - - return $folderlist; - } - - /** - * @brief Returns the forest (list of trees) of folders children of $parentid, - * including the feeds that they contain - * @param - * @returns - */ - public function childrenOfWithFeeds($parentid) { - - $feedmapper = new FeedMapper(); - $collectionlist = $feedmapper->findByFolderId($parentid); - - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . - ' WHERE user_id = ? AND parent_id = ?'); - $result = $stmt->execute(array($this->userid, $parentid)); - - while( $row = $result->fetchRow()) { - $folderid = $row['id']; - $folder = new Folder($row['name'], $folderid); - $folder->setOpened($row['opened']); - $children = self::childrenOfWithFeeds($folderid); - $folder->addChildren($children); - $collectionlist[] = $folder; - } - - return $collectionlist; - } - - - /** - * This is being used for consistency - */ - public function findById($id){ - return $this->find($id); - } - - - /** - * @brief Retrieve a folder from the database - * @param id The id of the folder in the database table. - * @returns an instance of OC_News_Folder - */ - public function find($id) { - $stmt = \OCP\DB::prepare('SELECT * - FROM ' . self::tableName . - ' WHERE user_id = ? AND id = ?'); - $result = $stmt->execute(array($this->userid, $id)); - - $row = $result->fetchRow(); - $folder = new Folder($row['name'], $row['id']); - $folder->setOpened($row['opened']); - - return $folder; - } - - /** - * @brief Store the folder and all its feeds into the database - * @param folder the folder to be saved - * @returns The id of the folder in the database table. - */ - public function save(Folder $folder) { - $query = \OCP\DB::prepare(' - INSERT INTO ' . self::tableName . - '(name, parent_id, user_id, opened) - VALUES (?, ?, ?, ?) - '); - - $name = $folder->getName(); - - if(empty($name)) { - $l = \OC_L10N::get('news'); - $name = $l->t('no name'); - } - - $parentid = $folder->getParentId(); - - $params=array( - $name, - $parentid, - $this->userid, - $folder->getOpened() - ); - $query->execute($params); - $folderid = \OCP\DB::insertid(self::tableName); - - $folder->setId($folderid); - return $folderid; - } - - - /** - * @brief Updates the folder - * @param folder the folder to be updated - */ - public function update(Folder $folder) { - $query = \OCP\DB::prepare('UPDATE ' . self::tableName - . ' SET name = ?, opened = ?' . ' WHERE id = ?'); - - $params = array($folder->getName(), $folder->getOpened(), $folder->getId()); - $query->execute($params); - return true; - } - - /** - * @brief Delete the folder and all its feeds from the database - * @param folder the folder to be deleted (an instance of OCA\News\Folder) - * @returns true if the folder has been deleted, false if an error occurred - */ - public function delete(Folder $folder) { - $folderid = $folder->getId(); - return deleteById(folderid); - } - - /** - * @brief Delete the folder and all its feeds from the database - * @param folder the folder to be deleted (an instance of OCA\News\Folder) - * @returns true if the folder has been deleted, false if an error occurred - */ - public function deleteById($folderid) { - if ($folderid == null) { - return false; - } - - // delete child folders - $stmt = \OCP\DB::prepare('SELECT id FROM ' . self::tableName .' WHERE parent_id = ? AND user_id = ?'); - $result = $stmt->execute(array($folderid, $this->userid)); - while ($row = $result->fetchRow()) { - if (!self::deleteById($row['id'])) - return false; - } - - $stmt = \OCP\DB::prepare('DELETE FROM ' . self::tableName .' WHERE id = ? AND user_id = ?'); - $result = $stmt->execute(array($folderid, $this->userid)); - - $feedMapper = new FeedMapper($this->userid); - //TODO: handle the value that the execute returns - if(!$feedMapper->deleteAll($folderid)) - return false; - - return true; - } - -}
\ No newline at end of file diff --git a/lib/item.php b/lib/item.php deleted file mode 100644 index 3d63e24f6..000000000 --- a/lib/item.php +++ /dev/null @@ -1,194 +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 StatusFlag{ - const UNREAD = 0x02; - const IMPORTANT = 0x04; - const DELETED = 0x08; - const UPDATED = 0x16; -} - -/** - * This class models an item. - * - * It encapsulate a SimplePie_Item object and adds a status flag to it - */ -class Item { - - private $url; - private $title; - private $guid; - private $body; - private $status; //a bit-field set with status flags - private $id; //id of the item in the database table - private $author; - private $date; //date is stored in the Unix format - private $feedTitle; - private $enclosure; // Item_Enclosure object containing media attachment information - - public function __construct($url, $title, $guid, $body, $id = null) { - $this->title = $title; - $this->url = $url; - $this->guid = $guid; - $this->body = $body; - if ($id == null) { - $this->status |= StatusFlag::UNREAD; - } - else { - $this->id = $id; - } - } - - public function getFeedId() { - return $this->feedId; - } - - public function setFeedId($feedId) { - $this->feedId = $feedId; - } - - public function getGuid() { - return $this->guid; - } - - public function setGuid($guid) { - $this->guid = $guid; - } - - public function getId() { - return $this->id; - } - - public function setId($id) { - $this->id = $id; - } - - public function setRead() { - $this->status &= ~StatusFlag::UNREAD; - } - - public function setUnread() { - $this->status |= StatusFlag::UNREAD; - } - - public function isRead() { - return !($this->status & StatusFlag::UNREAD); - } - - public function setImportant() { - $this->status |= StatusFlag::IMPORTANT; - } - - public function setUnimportant() { - $this->status &= ~StatusFlag::IMPORTANT; - } - - public function isImportant() { - return ($this->status & StatusFlag::IMPORTANT); - } - - /** - * NOTE: this is needed to store items in the database, otherwise - * the status of an item should be retrieved with methods: isRead(), isImportant(), ... - */ - public function getStatus() { - return $this->status; - } - - public function setStatus($status) { - $this->status = $status; - } - - /* change the following method with set/get magic methods - * http://www.php.net/manual/en/language.oop5.overloading.php#object.get - */ - - public function getTitle() { - return $this->title; - } - - public function setTitle($title) { - $this->title = $title; - } - - public function getFeedTitle() { - return $this->feedTitle; - } - - public function setFeedTitle($feedtitle) { - $this->feedTitle = $feedtitle; - } - - public function getUrl() { - return $this->url; - } - - public function setUrl($url) { - $this->url = $url; - } - - public function getBody() { - return $this->body; - } - - public function setBody($body) { - $this->body = $body; - } - - public function getAuthor() { - return $this->author; - } - - public function setAuthor($author) { - $this->author = $author; - } - - public function getDate() { - return $this->date; - } - - //TODO: check if the parameter is in the Unix format - public function setDate($date) { - $this->date = $date; - } - - public function getEnclosure() { - return $this->enclosure; - } - - public function setEnclosure(Item_Enclosure $enclosure) { - $this->enclosure = $enclosure; - } -} - -class Item_Enclosure { - private $mimetype; - private $link; - - public function getMimeType() { - return $this->mimetype; - } - - public function setMimeType($mimetype) { - $this->mimetype = $mimetype; - } - - public function getLink() { - return $this->link; - } - - public function setLink($link) { - $this->link = $link; - } -} diff --git a/lib/itemmapper.php b/lib/itemmapper.php deleted file mode 100644 index 9aac95a37..000000000 --- a/lib/itemmapper.php +++ /dev/null @@ -1,297 +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; - -/** - * This class maps an item to a row of the items table in the database. - * It follows the Data Mapper pattern (see http://martinfowler.com/eaaCatalog/dataMapper.html). - */ -class ItemMapper { - - const tableName = '*PREFIX*news_items'; - private $userid; - - public function __construct($userid = null) { - if ($userid !== null) { - $this->userid = $userid; - } - else { - $this->userid = \OCP\USER::getUser(); - } - } - - /** - * @brief - * @param row a row from the items table of the database - * @returns an object of the class OC_News_Item - */ - public function fromRow($row) { - $url = $row['url']; - $title = $row['title']; - $guid = $row['guid']; - $body = $row['body']; - $id = $row['id']; - - $item = new Item($url, $title, $guid, $body, $id); - $item->setStatus($row['status']); - $item->setAuthor($row['author']); - $item->setFeedId($row['feed_id']); - $item->setDate(Utils::dbtimestampToUnixtime($row['pub_date'])); - - if($row['enclosure_mime'] !== null && $row['enclosure_link'] !== null) { - $enclosure = new Item_Enclosure(); - $enclosure->setMimeType($row['enclosure_mime']); - $enclosure->setLink($row['enclosure_link']); - $item->setEnclosure($enclosure); - } - - return $item; - } - - /** - * @brief Retrieve all the item corresponding to a feed from the database - * @param feedid The id of the feed in the database table. - */ - public function findByFeedId($feedid) { - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE feed_id = ? ORDER BY pub_date DESC'); - $result = $stmt->execute(array($feedid)); - $feedmapper = new FeedMapper($this->userid); - $feed = $feedmapper->findById($feedid); - $items = array(); - while ($row = $result->fetchRow()) { - $item = $this->fromRow($row); - $item->setFeedTitle($feed->getTitle()); - $items[] = $item; - } - - return $items; - } - - - /** - * @brief Retrieve all the items corresponding to a feed from the database with a particular status - * @param feedid The id of the feed in the database table. - * @param status one of the constants defined in OCA\News\StatusFlag - */ - public function findAllStatus($feedid, $status) { - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' - WHERE feed_id = ? - AND ((status & ?) > 0) - ORDER BY pub_date DESC'); - $result = $stmt->execute(array($feedid, $status)); - - $items = array(); - while ($row = $result->fetchRow()) { - $item = $this->fromRow($row); - $items[] = $item; - } - - return $items; - } - - /* - * @brief Retrieve all the items from the database with a particular status - * @param status one of the constants defined in OCA\News\StatusFlag - */ - public function findEveryItemByStatus($status) { - $stmt = \OCP\DB::prepare('SELECT ' . self::tableName . '.* FROM ' . self::tableName . ' - JOIN '. FeedMapper::tableName .' ON - '. FeedMapper::tableName .'.id = ' . self::tableName . '.feed_id - WHERE '. FeedMapper::tableName .'.user_id = ? - AND ((' . self::tableName . '.status & ?) > 0) - ORDER BY ' . self::tableName . '.pub_date DESC'); - $result = $stmt->execute(array($this->userid, $status)); - - $items = array(); - while ($row = $result->fetchRow()) { - $item = $this->fromRow($row); - $items[] = $item; - } - - return $items; - } - - public function countAllStatus($feedid, $status) { - $stmt = \OCP\DB::prepare('SELECT COUNT(*) as size FROM ' . self::tableName . ' - WHERE feed_id = ? - AND ((status & ?) > 0)'); - $result=$stmt->execute(array($feedid, $status))->fetchRow(); - return $result['size']; - } - - /** - * @brief Count all the items from the database with a particular status - * @param status one of the constants defined in OCA\News\StatusFlag - */ - public function countEveryItemByStatus($status) { - $stmt = \OCP\DB::prepare('SELECT COUNT(*) as size FROM ' . self::tableName . ' - JOIN '. FeedMapper::tableName .' ON - '. FeedMapper::tableName .'.id = ' . self::tableName . '.feed_id - WHERE '. FeedMapper::tableName .'.user_id = ? - AND ((' . self::tableName . '.status & ?) > 0)'); - $result = $stmt->execute(array($this->userid, $status))->fetchRow();; - - return $result['size']; - } - - public function findIdFromGuid($guid_hash, $guid, $feedid) { - $stmt = \OCP\DB::prepare(' - SELECT * FROM ' . self::tableName . ' - WHERE guid_hash = ? - AND feed_id = ? - '); - $result = $stmt->execute(array($guid_hash, $feedid)); - //TODO: if there is more than one row, falling back to comparing $guid - $row = $result->fetchRow(); - $id = null; - if ($row != null) { - $id = $row['id']; - } - return $id; - } - - /** - * @brief Update the item after its status has changed - * @returns The item whose status has changed. - */ - public function update(Item $item) { - - $itemid = $item->getId(); - $status = $item->getStatus(); - - $stmt = \OCP\DB::prepare(' - UPDATE ' . self::tableName . - ' SET status = ? - WHERE id = ? - '); - - $params=array( - $status, - $itemid - ); - - $result = $stmt->execute($params); - - - return true; - } - - /** - * @brief Save the feed and all its items into the database - * @returns The id of the feed in the database table. - */ - public function save(Item $item, $feedid) { - $guid = $item->getGuid(); - $guid_hash = md5($guid); - - $status = $item->getStatus(); - - $itemid = $this->findIdFromGuid($guid_hash, $guid, $feedid); - - if ($itemid == null) { - $title = $item->getTitle(); - $body = $item->getBody(); - $author = $item->getAuthor(); - $enclosure_mime = null; - $enclosure_link = null; - - if($enclosure = $item->getEnclosure()) { - $enclosure_mime = $enclosure->getMimeType(); - $enclosure_link = $enclosure->getLink(); - } - - $stmt = \OCP\DB::prepare(' - INSERT INTO ' . self::tableName . - '(url, title, body, author, guid, guid_hash, pub_date, enclosure_mime, enclosure_link, feed_id, status) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - '); - - if(empty($title)) { - $l = \OC_L10N::get('news'); - $title = $l->t('no title'); - } - - if(empty($body)) { - $l = \OC_L10N::get('news'); - $body = $l->t('no body'); - } - - $pub_date = Utils::unixtimeToDbtimestamp($item->getDate()); - - $params=array( - $item->getUrl(), - $title, - $body, - $author, - $guid, - $guid_hash, - $pub_date, - $enclosure_mime, - $enclosure_link, - $feedid, - $status - ); - - $stmt->execute($params); - - $itemid = \OCP\DB::insertid(self::tableName); - } - else { - $this->update($item); - } - $item->setId($itemid); - return $itemid; - } - - /** - * @brief Retrieve an item from the database - * @param id The id of the item in the database table. - */ - public function findById($id) { - - $stmt = \OCP\DB::prepare('SELECT ' . self::tableName . '.id AS id, ' . self::tableName . - '.url AS url, ' . self::tableName . '.title AS title, guid, body, status, author, feed_id, pub_date, enclosure_mime, enclosure_link' . - ' FROM ' . self::tableName . ' JOIN ' . FeedMapper::tableName . - ' ON ' . self::tableName . '.feed_id = ' . FeedMapper::tableName . '.id WHERE (' . self::tableName . - '.id = ? AND ' . FeedMapper::tableName . '.user_id = ? )'); - $result = $stmt->execute(array($id, $this->userid)); - - /* - $stmt = \OCP\DB::prepare('SELECT * FROM ' . self::tableName . ' WHERE id = ?'); - $result = $stmt->execute(array($id)); - */ - $row = $result->fetchRow(); - - $item = $this->fromRow($row); - - return $item; - - } - - - /** - * @brief Permanently delete all items belonging to a feed from the database - * @param feedid The id of the feed that we wish to delete - * @return - */ - public function deleteAll($feedid) { - if ($feedid == null) { - return false; - } - $stmt = \OCP\DB::prepare('DELETE FROM ' . self::tableName .' WHERE feed_id = ?'); - - $result = $stmt->execute(array($feedid)); - - return $result; - } -}
\ No newline at end of file diff --git a/lib/request.php b/lib/request.php new file mode 100644 index 000000000..013c9b80e --- /dev/null +++ b/lib/request.php @@ -0,0 +1,83 @@ +<?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 new file mode 100644 index 000000000..993d061d3 --- /dev/null +++ b/lib/response.php @@ -0,0 +1,218 @@ +<?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 index af450ba9f..d9cf1d294 100644 --- a/lib/search.php +++ b/lib/search.php @@ -1,4 +1,14 @@ <?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{ @@ -16,9 +26,9 @@ class OC_Search_Provider_News extends OC_Search_Provider{ $l = new OC_l10n('news'); foreach($allFeeds as $feed) { - if(substr_count(strtolower($feed['title']), strtolower($query)) > 0) { - $link = OCP\Util::linkTo('news', 'index.php').'?feedid='.urlencode($feed['id']); - $results[]=new OC_Search_Result($feed['title'], '', $link, (string)$l->t('News')); + 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')); } } } diff --git a/lib/security.php b/lib/security.php new file mode 100644 index 000000000..99258285c --- /dev/null +++ b/lib/security.php @@ -0,0 +1,104 @@ +<?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/utils.php b/lib/utils.php index 52e71b092..560114ca7 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -14,7 +14,7 @@ namespace OCA\News; // load SimplePie library //TODO: is this a suitable place for the following require? - require_once 'news/3rdparty/SimplePie/autoloader.php'; +require_once 'news/3rdparty/SimplePie/autoloader.php'; class Utils { @@ -93,7 +93,7 @@ class Utils { $enclosureType = $itemEnclosure->get_type(); $enclosureLink = $itemEnclosure->get_link(); if(stripos($enclosureType, "audio/") !== FALSE) { - $enclosure = new Item_Enclosure(); + $enclosure = new Enclosure(); $enclosure->setMimeType($enclosureType); $enclosure->setLink($enclosureLink); $item->setEnclosure($enclosure); |