diff --git a/files/class.app.php b/files/class.app.php
index 03eb937..000767c 100644
--- a/files/class.app.php
+++ b/files/class.app.php
@@ -11,7 +11,7 @@ function __construct($minimal = false) {
$this->config = $config;
$this->config['cache'] = $this->config['path'] . "/files/cache/";
- $this->config['log'] = $this->config['path'] . "/files/log/";
+ $this->config['log'] = $this->config['path'] . "/files/logs/";
// Connect to database
$this->connectDB($this->config['db'], false);
@@ -69,6 +69,8 @@ function __construct($minimal = false) {
$this->articles = new articles($this);
// Create forum object
$this->forum = new forum($this);
+ // Create RSS object
+ $this->rss = new rss();
if (!is_array($custom_css))
$custom_css = Array();
diff --git a/files/class.articles.php b/files/class.articles.php
index 3672f4e..61c2ba2 100644
--- a/files/class.articles.php
+++ b/files/class.articles.php
@@ -439,19 +439,24 @@ public function acceptArticle($article_id) {
if ($result->category_id == 0) {
$slug = '/news/' . $result->slug;
$type = 'news';
+ $cat_id = 0;
} else {
$slug = '/articles/' . $result->slug;
$type = 'article';
+ $cat_id = 1;
}
$this->app->feed->call($result->username, $type, $result->title, $slug);
// Award medal to user
$this->app->user->awardMedal('writer', 2, $result->user_id);
+ // Add to RSS
+ if(!$this->app->rss->storeRSS($result->title, $slug, substr($result->body, 0, 200).'...', $cat_id))
+ $this->app->log->add('rss', 'Failed to add an item to the feed.');
+
return true;
}
-
/*
* COMMENTS
*/
diff --git a/files/class.forum.php b/files/class.forum.php
index 26eaf8c..700331d 100644
--- a/files/class.forum.php
+++ b/files/class.forum.php
@@ -102,7 +102,7 @@ public function getSections($parent=null) {
}
public function printThreadPost($post, $first=false, $last=false, $admin=false) {
- if (!$post) return;
+ if (!$post) return;
$post->first = $first;
$post->last = $last;
@@ -454,6 +454,10 @@ public function newThread($section, $title, $body) {
$this->app->ssga->set_event('forum', 'thread.new', '/forum/' . $slug, $this->app->user->uid);
$this->app->ssga->send();
+ // Add to RSS
+ if(!$this->app->rss->storeRSS($title, $slug, substr($body, 0, 200).'...', 2))
+ $this->app->log->add('rss', 'Failed to add an item to the feed.');
+
return '/forum/' . $slug;
}
diff --git a/files/class.rss.php b/files/class.rss.php
new file mode 100644
index 0000000..5a07905
--- /dev/null
+++ b/files/class.rss.php
@@ -0,0 +1,178 @@
+getConstants()))
+ throw IllegalArgumentException();
+
+ $this->value = $value;
+ }
+
+ final public function __toString() {
+ return $this->value;
+ }
+ }
+
+ class feedType extends enumBase {
+ const ATOM = "atom";
+ const RSS = "rss";
+ }
+
+ class feedCategory extends enumBase {
+ const ARTICLE = "Article";
+ const FORUM = "Forum";
+ const NEWS = "News";
+ }
+
+ class rss {
+ public function __construct() {
+ //load configuration file
+ require('config.php');
+
+ if (!isset($config) || !is_array($config))
+ throw new Exception('Config error');
+
+ $this->config = $config;
+ $this->connectDB($this->config['db'], false);
+ }
+
+ /**
+ * Create database connection
+ *
+ * @param object $config Databse connection config
+ * @param boolean $debug Should the connection ignore errors or throw exceptions
+ *
+ * @return void
+ *
+ * @todo Create site config option that is passed in to the debug param
+ */
+ protected function connectDB($config, $debug=true) {
+ // Connect to database
+ try {
+ $dsn = "{$config['driver']}:host={$config['host']}";
+ $dsn .= (!empty($config['port'])) ? ';port=' . $config['port'] : '';
+ $dsn .= ";dbname={$config['database']}";
+ $this->db = new PDO($dsn, $config['username'], $config['password']);
+
+ if ($debug)
+ $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
+
+ $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
+ $this->db->setAttribute(PDO::MYSQL_ATTR_FOUND_ROWS, true);
+ } catch(PDOException $e) {
+ die($e->getMessage());
+ }
+ }
+
+ /**
+ * Store a new feed item in the DB
+ *
+ * @param string $title Feed title
+ * @param string $slug Link to the thread/news item/article
+ * @param string $description A short descriptive text
+ * @param integer $catID To allow RSS readers to cathegorize the feeds
+ *
+ * @return Boolean
+ *
+ * @todo Create everything, pubDate will be done using SQL Now()
+ */
+ public function storeRSS($title, $slug, $description, $catID) {
+ $category = feedCategory::ARTICLE;
+
+ switch ($catID) {
+ case 0:
+ $category = feedCategory::NEWS;
+ break;
+ case 1:
+ $category = feedCategory::ARTICLE;
+ break;
+ case 2:
+ $category = feedCategory::FORUM;
+ break;
+ }
+
+ $link = $this->config['domain'] . $slug;
+
+ try {
+ // Insert article
+ $st = $this->db->prepare('INSERT INTO `rss_feed` (`unique_id`,`title`,`link`,`description`,`category`)
+ VALUES (UUID(),:title,:link,:description,:category)');
+ $st->execute(array(':title' => $title, ':link' => $link, ':description' => $description, ':category' => $category));
+
+ $this->db->commit();
+ } catch(PDOException $e) {
+ $this->db->rollBack();
+ return False;
+ }
+ return True;
+ }
+
+ /**
+ * Create and parse RSS/ATOM feed
+ *
+ * @param feedType $type To determine output method
+ *
+ * @return array
+ */
+ public function generateRSS($type) {
+ $sql = 'SELECT * FROM `rss_feed` ORDER BY `id` DESC';
+ $st = $this->db->prepare($sql);
+ $st->execute();
+ $result = $st->fetchAll();
+
+ if ($type == feedType::RSS) {
+ $data = '';
+ $data .= '';
+ $data .= '