Skip to content
This repository was archived by the owner on Oct 30, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion files/class.app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to mention this one, it bugged the logs out.


// Connect to database
$this->connectDB($this->config['db'], false);
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion files/class.articles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
6 changes: 5 additions & 1 deletion files/class.forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
178 changes: 178 additions & 0 deletions files/class.rss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php
abstract class enumBase {
final public function __construct($value) {
$refClass = new ReflectionClass($this);

if (!in_array($value, $refClass->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 = '<?xml version="1.0" encoding="UTF-8" ?>';
$data .= '<?xml-stylesheet type="text/css" href="../files/css/rss.css" ?>';
$data .= '<rss version="2.0">';
$data .= '<channel>';
$data .= '<title>HackThis!! RSS</title>';
$data .= '<link>https://www.hackthis.co.uk/</link>';
$data .= '<description>Want to learn about hacking, hackers and network security. Try our hacking challenges or join our community to discuss the latest software and cracking tools.</description>';
$data .= '<language>en-gb</language>';

foreach ($result as $row) {
$data .= '<item>';
$data .= '<title>'.$row->title.'</title>';
$data .= '<guid>'.$row->unique_id.'</guid>';
$data .= '<link>'.$row->link.'</link>';
$data .= '<description>'.$row->description.'</description>';
$data .= '<category>'.$row->category.'</category>';
$data .= '<pubDate>'.$row->pubDate.'</pubDate>';
$data .= '</item>';
}

$data .= '</channel>';
$data .= '</rss> ';
} elseif ($type == feedType::ATOM) {
$objDateTime = new DateTime('NOW');
$data = '<?xml version="1.0" encoding="utf-8" ?>';
$data .= '<?xml-stylesheet type="text/css" href="../files/css/rss.css" ?>';
$data .= '<feed xmlns="http://www.w3.org/2005/Atom">';
$data .= '<id>https://www.hackthis.co.uk/</id>';
$data .= '<title>HackThis!! ATOM</title>';
$data .= '<updated>'.$objDateTime->format(DateTime::ATOM).'</updated>';
$data .= '<link href="https://www.hackthis.co.uk/" />';
$data .= '<subtitle>Want to learn about hacking, hackers and network security. Try our hacking challenges or join our community to discuss the latest software and cracking tools.</subtitle>';

foreach ($result as $row) {
$data .= '<entry>';
$data .= '<title>'.$row->title.'</title>';
$data .= '<id>'.$row->unique_id.'</id>';
$data .= '<link href="'.$row->link.'" />';
$data .= '<updated>'.$row->pubDate.'</updated>';
$data .= '<summary>'.$row->description.'</summary>';
$data .= '<category term=">'.$row->category.'" />';
$data .= '</entry>';
}

$data .= '</feed> ';
} else {
$data = null;
throw IllegalArgumentException();
}

return $data;
}
}
?>
4 changes: 4 additions & 0 deletions files/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<li><a href='https://www.facebook.com/hackthisuk'><i class='icon-facebook'></i> Facebook</a></li>
<li><a href='https://twitter.com/hackthisuk'><i class='icon-twitter'></i> Twitter</a></li>
<li><a href='https://github.com/HackThis'><i class='icon-github'></i> GitHub</a></li>
<li>
<a href='/rss/rss.php'><img class='icon-rss' src='../files/images/rss.png' /></a>&nbsp;
<a href='/rss/atom.php'><img class='icon-atom' src='../files/images/atom.png' /></a>
</li>
</ul>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion html/files/css/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ div.image {
a {
color: $text;

.icon-facebook, .icon-twitter, .icon-feed {
.icon-facebook, .icon-twitter, .icon-feed, .icon-rss, .icon-atom {
padding-right: 3px;
}

Expand Down
83 changes: 83 additions & 0 deletions html/files/css/rss.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
rss, feed {
background-color: #000;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 0.7em;
line-height: 130%;
margin: 1em;
}
/* HEADER */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
channel {
display: block;
background-color: #1e1e1e;
padding-bottom: 0.3em;
}
feed>id {
display: block;
color: #0C8200;
font-size: 130%;
font-weight: bold;
margin: 0.3em 0.3em 1em 0.5em;
}
channel>title, feed>title {
display: block;
padding: 0.4em 0.2em;
color: #0C8200;
border-bottom: 1px solid black;
font-weight: bold;
font-size: 140%;
background-color: #141414;
}
channel>link, feed>link {
display: block;
color: #aaa;
font-size: 130%;
font-weight: bold;
margin: 0.5em;
}
channel>description, feed>subtitle, feed>updated {
display: block;
color: #aaa;
font-size: 130%;
font-weight: bold;
margin: 0.3em 0.5em 1em 0.5em;
}
language, feed>entry>link , feed>entry>category {
display: none;
}
/* CONTENT */
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
channel>item, feed>entry {
background-color: #383838;
border: 1px solid #094802;
clear: both;
display: block;
padding: 0 0 0.5em;
margin: 1em;
}
channel>item>title, feed>entry>title {
background-color: #141414;
color: #FFF;
display: block;
font-size: 110%;
font-weight: bold;
padding: 0.3em 0.5em;
}
channel>item>description, channel>item>category, channel>item>guid, feed>entry>summary, feed>entry>id{
display: block;
float: none;
text-align: left;
padding: 0.2em 0.5em 0.4em;
color: #aaa;
}
channel>item>link {
color: #fff;
display: block;
margin: 0.5em 0.3em 1em 0.3em;
}
channel>item>pubDate, feed>entry>updated {
color: #aaa;
display: block;
font-size: 86%;
padding: 0 0.5em;
}
Binary file added html/files/images/atom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added html/files/images/rss.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions html/rss/atom.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
include_once 'class.rss.php';

$feedClass = new rss();
$rssFeed = $feedClass->generateRSS(feedType::ATOM);

header('Content-Type: application/xml');
echo $rssFeed;
?>
9 changes: 9 additions & 0 deletions html/rss/rss.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
include_once 'class.rss.php';

$feedClass = new rss();
$rssFeed = $feedClass->generateRSS(feedType::RSS);

header('Content-Type: application/xml');
echo $rssFeed;
?>
13 changes: 13 additions & 0 deletions sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,19 @@ CREATE TABLE IF NOT EXISTS `irc_logs` (
PRIMARY KEY (`log_id`)
) ENGINE=MyISAM;

/*
RSS FEED
*/
CREATE TABLE IF NOT EXISTS `rss_feed` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`unique_id` varchar(36) NOT NULL,
`title` varchar(200) NOT NULL,
`link` varchar(200) NOT NULL,
`description` text NOT NULL,
`category` varchar(20) NOT NULL,
`pubDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;

/*
TRIGGERS
Expand Down
Loading