Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(RSS): Start Build rss feed
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Mar 11, 2019
1 parent b93d94c commit 4e38460
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 44 deletions.
5 changes: 4 additions & 1 deletion apps/config/http_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
'rules' => [
'GET tracker/{tracker_action}' => ['tracker','index'],
'GET captcha' => ['captcha', 'index'],
'GET rss' => ['rss', 'index','middleware' => [
apps\middleware\AuthByPasskeyMiddleware::class
]],
'api/v1/{controller}/{action}' => ['api/{controller}', '{action}', 'middleware' => [
apps\middleware\ApiMiddleware::class
]],
'{controller}/{action}' => ['{controller}', '{action}', 'middleware' => [
apps\middleware\AuthMiddleware::class
apps\middleware\AuthByCookiesMiddleware::class
]],
],
],
Expand Down
17 changes: 12 additions & 5 deletions apps/controllers/RssController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@

namespace apps\controllers;

use apps\models\Torrent;

class RssController
{
public function actionIndex() {
app()->user->loadUserFromPasskey();
use Rid\Http\Controller;

class RssController extends Controller
{
public function actionIndex()
{
// FIXME add torrent search
$fetch = app()->pdo->createCommand('SELECT `id` FROM torrents ORDER BY added_at DESC LIMIT 50;')->queryColumn();

$torrents = array_map(function ($id) {
return new Torrent($id);
}, $fetch);

return 'TODO';
return $this->render('rss_feed', ['torrents' => $torrents]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace apps\middleware;


class AuthMiddleware
class AuthByCookiesMiddleware
{

public function handle($callable, \Closure $next)
Expand Down
30 changes: 30 additions & 0 deletions apps/middleware/AuthByPasskeyMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/3/10
* Time: 17:25
*/

namespace apps\middleware;


class AuthByPasskeyMiddleware
{
public function handle($callable, \Closure $next)
{

// Check User
if (app()->request->get('passkey') === null) {
return 'missing passkey';
}

app()->user->loadUserFromPasskey();
if (app()->user->isAnonymous()) {
return 'passkey not exist';
}

// 执行下一个中间件
return $next();
}
}
57 changes: 57 additions & 0 deletions apps/views/rss_feed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* @link http://www.bittorrent.org/beps/bep_0036.html
*
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/3/10
* Time: 17:24
*
* @var array $torrents
* @var \apps\models\Torrent $torrent
*/

$url = (app()->request->isSecure() ? 'https://' : 'http://') . app()->config->get('base.site_url');
$site_name = app()->config->get('base.site_name');
$site_email = app()->config->get('base.site_email');
$yearfounded = 2019; // FIXME get it from dynamic config
$copyright = "Copyright (c) " . $site_name . " " . (date("Y") != $yearfounded ? $yearfounded . "-" : "") . date("Y") . ", all rights reserved";
?>
<?= '<?xml version="1.0" encoding="utf-8"?>' ?>

<rss version="2.0">
<channel>
<title><?= addslashes($site_name . ' Torrents') ?></title>
<link><?= $url ?></link>
<description><![CDATA[<?= addslashes('Latest torrents from ' . $site_name ) ?>]]></description>
<language>en</language>
<copyright><?= $copyright ?></copyright>
<managingEditor><?= $site_email . "(" . $site_name . " Admin)" ?></managingEditor>
<webMaster><?= $site_email . "(" . $site_name . " Webmaster)" ?></webMaster>
<pubDate><?= date('r') ?></pubDate>
<generator><?= app()->config->get('base.site_generator') ?> RSS Generator</generator>
<docs><![CDATA[http://www.rssboard.org/rss-specification]]></docs>
<ttl>120</ttl>
<image>
<url><![CDATA[<?= $url . '/favicon.ico' ?>]]></url>
<title><?= addslashes($site_name . 'Torrents') ?></title>
<link><![CDATA[<?= $url ?><]]></link>
<width>100</width>
<height>100</height>
<description><?= addslashes($site_name . ' Torrents') ?></description>
</image>
<?php foreach ($torrents as $torrent): ?>
<item>
<title><![CDATA[<?= $torrent->getTitle() ?>]]></title>
<link><?= $url.'/torrents/details?id=' . $torrent->getId() ?></link>
<description><?= $torrent->getDescr() ?></description>
<author><?= ($torrent->getUplver() == 'yes' ? 'Anonymous' : $torrent->getOwner()->getUsername()) . '@' . $site_name ?></author>
<category domain="<?= $url . '/torrents?cat='.$torrent->getCategoryId()?>">Movie</category>
<comments><![CDATA[<?= $url. '/torrents/details?id=' . $torrent->getId() . '&cmtpage=0#startcomments' ?>]]></comments>
<enclosure url="<?= $url . '/torrents/download?id=' . $torrent->getId() . ('') ?>" length="<?= $torrent->getTorrentSize() ?>" type="application/x-bittorrent" />
<guid isPermaLink="false"><?= $torrent->getInfoHash() ?></guid>
<pubDate><?= date('r',strtotime($torrent->getAddedAt())) ?></pubDate>
</item>
<?php endforeach; ?>
</channel>
</rss>
11 changes: 3 additions & 8 deletions apps/views/torrents/details.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* @var League\Plates\Template\Template $this
* @var \apps\models\Torrent $torrent
*/

include 'helper.php';
?>

<?= $this->layout('layout/base') ?>
Expand Down Expand Up @@ -37,14 +39,7 @@

<div class="row">
<div class="col-md-1">Uploader: </div>
<div class="col-md-5">
<?php if ($torrent->getUplver() == 'yes' and app()->user->getClass(true) < app()->config->get('authority.see_anonymous_uploader')): ?>
<i>Anonymous</i>
<?php else: ?>
<!--suppress HtmlUnknownTarget -->
<a class="text-default" href="/user/panel?id=<?= $torrent->getOwnerId() ?>" data-toggle="tooltip" title="User"><?= $torrent->getOwner()->getUsername() ?></a>
<?php endif; ?>
</div>
<div class="col-md-5"><?= get_torrent_uploader($torrent) ?></div>
<div class="col-md-1">Seeders:</div>
<div class="col-md-5"><span style="color: green;"><?= $torrent->getComplete() ?></span></div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions apps/views/torrents/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
*
* Common function to render torrent page
*
* Created by PhpStorm.
* User: Rhilip
* Date: 2019/3/11
* Time: 16:09
*
*/

/**
* @param \apps\models\Torrent $torrent
* @return string
*/
function get_torrent_uploader(\apps\models\Torrent $torrent)
{
if ($torrent->getUplver() == 'yes' and app()->user->getClass(true) < app()->config->get('authority.see_anonymous_uploader')) {
return '<i>Anonymous</i>';
} else {
return "<a class=\"text-default\" href=\"/user/panel?id={$torrent->getOwnerId()}\" data-toggle=\"tooltip\" title=\"User\">{$torrent->getOwner()->getUsername()}</a>";
}
}
58 changes: 30 additions & 28 deletions apps/views/torrents/list.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
* @var array $torrents
* @var \apps\models\Torrent $torrent
*/

include 'helper.php';
$time_now = time();
?>

<?= $this->layout('layout/base') ?>
Expand All @@ -19,42 +22,41 @@
<table class="layui-table">
<thead>
<tr>
<th class="text-center" style="width: 20px">Category</th>
<th>Torrent</th>
<th class="text-center" style="width: 5px">Link</th>
<th class="text-center" style="width: 50px">Size</th>
<th class="text-center" style="width: 100px">Date</th>
<th class="text-center" style="width: 15px"><i class="fas fa-arrow-up" title="Seeders"></th>
<th class="text-center" style="width: 15px"><i class="fas fa-arrow-down" title="Leechers"></i></th>
<th class="text-center" style="width: 15px"><i class="fas fa-check" title="Completed"></th>
<th class="text-center" style="width: 50px"><i class="fas fa-user" title="Owner"></i></th>
<th class="text-center" style="width: 20px" title="Category">Category</th>
<th class="text-center" title="Torrent">Torrents</th>
<th class="text-center" style="width: 5px" title="Comment"><i class="fas fa-comment-alt"></i></th>
<th class="text-center" style="width: 50px" title="Size">Size</th>
<th class="text-center" style="width: 100px" title="Date">Date</th>
<th class="text-center" style="width: 15px" title="Seeders"><i class="fas fa-arrow-up"></th>
<th class="text-center" style="width: 15px" title="Leechers"><i class="fas fa-arrow-down"></i></th>
<th class="text-center" style="width: 15px" title="Completed"><i class="fas fa-check"></th>
<th class="text-center" style="width: 50px" title="Owner"><i class="fas fa-user"></i></th>
</tr>
</thead>
<tbody>
<?php foreach ($torrents as $torrent): ?>
<tr>
<tr data-tid="<?= $torrent->getId() ?>">
<td class="text-center"><?= ($torrent->getCategory())->getName() ?></td>
<td>
<!--suppress HtmlUnknownTarget -->
<a href="/torrents/details?id=<?= $torrent->getId() ?>" target="_blank"><?= $torrent->getTitle() ?></a>
</td>
<td class="text-center">
<!--suppress HtmlUnknownTarget -->
<a href="/torrents/download?id=<?= $torrent->getId() ?>"><i class="fas fa-download"></i></a>
</td>
<td class="text-center" data-bytes-size="<?= $torrent->getTorrentSize() ?>"><?= $this->e($torrent->getTorrentSize(),'format_bytes') ?></td>
<td class="text-center" data-timestamp="<?= strtotime($torrent->getAddedAt()) ?>"><?= $torrent->getAddedAt() ?></td>
<td class="text-center"><?= $torrent->getComplete() ?></td>
<td class="text-center"><?= $torrent->getIncomplete() ?></td>
<td class="text-center"><?= $torrent->getDownloaded() ?></td>
<td class="text-center">
<?php if ($torrent->getUplver() == 'yes' and app()->user->getClass(true) < app()->config->get('authority.see_anonymous_uploader')): ?>
<i>Anonymous</i>
<?php else: ?>
<div class="pull-left">
<!--suppress HtmlUnknownTarget -->
<a class="text-default" href="/user/panel?id=<?= $torrent->getOwnerId() ?>" data-toggle="tooltip" title="User"><?= $torrent->getOwner()->getUsername() ?></a>
<?php endif; ?>
<a href="/torrents/details?id=<?= $torrent->getId() ?>" target="_blank"><?= $torrent->getTitle() ?></a>
</div>
<div class="pull-right">
<div class="text-center" style="width: 5px">
<!--suppress HtmlUnknownTarget -->
<a href="/torrents/download?id=<?= $torrent->getId() ?>"><i class="fas fa-download"></i></a>
<a href="#"><i class="<?= true ? 'far' : 'fas' // TODO ?> fa-star"></i></a>
</div>
</div>
</td>
<td class="text-center">0</td> <!-- TODO -->
<td class="text-center" data-bytes-size="<?= $torrent->getTorrentSize() ?>"><?= $this->e($torrent->getTorrentSize(),'format_bytes') ?></td>
<td class="text-center" data-timestamp="<?= strtotime($torrent->getAddedAt()) ?>" data-ttl="<?= $time_now - strtotime($torrent->getAddedAt()) ?>"><?= $torrent->getAddedAt() ?></td>
<td class="text-center"><?= number_format($torrent->getComplete()) ?></td>
<td class="text-center"><?= number_format($torrent->getIncomplete()) ?></td>
<td class="text-center"><?= number_format($torrent->getDownloaded()) ?></td>
<td class="text-center"><?= get_torrent_uploader($torrent) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down
3 changes: 2 additions & 1 deletion migration/ridpt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ INSERT INTO `site_config` (`name`, `value`) VALUES
('base.site_copyright', 'RidPT Group'),
('base.site_css_update_date', '201903100001'),
('base.site_description', 'A Private Tracker Site Demo powered by RidPT'),
('base.site_email', 'admin@ridpt.top'),
('base.site_generator', 'RidPT'),
('base.site_keywords', 'RidPT,Private Tracker'),
('base.site_muti_tracker_url', ''),
Expand All @@ -356,7 +357,7 @@ INSERT INTO `site_config` (`name`, `value`) VALUES
('buff.random_percent_free', '2'),
('register.by_green', '0'),
('register.by_invite', '1'),
('register.by_open', '1'),
('register.by_open', '0'),
('register.email_black_list', '@test.com'),
('register.email_white_list', '@gmail.com'),
('register.enabled_email_black_list', '1'),
Expand Down

0 comments on commit 4e38460

Please sign in to comment.