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

Commit

Permalink
feat(torrent/upload): Add more config key to control behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jul 21, 2019
1 parent b74982f commit bb01120
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 45 deletions.
11 changes: 9 additions & 2 deletions apps/libraries/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,25 @@ public static function ruleCategory(): array
}, 86400);
}

public static function ruleCanUsedCategory(): array
{
return array_filter(static::ruleCategory(), function ($cat) {
return $cat['enabled'] = 1;
});
}

public static function ruleQuality($quality): array
{
if (!in_array($quality, array_keys(self::getQualityTableList()))) throw new \RuntimeException('Unregister quality : ' . $quality);
return static::getStaticCacheValue('enabled_quality_' . $quality, function () use ($quality) {
return app()->pdo->createCommand("SELECT * FROM `quality_$quality` WHERE `enabled` = 1 ORDER BY `sort_index`,`id`")->queryAll();
return app()->pdo->createCommand("SELECT * FROM `quality_$quality` WHERE `id` > 0 AND `enabled` = 1 ORDER BY `sort_index`,`id`")->queryAll();
}, 86400);
}

public static function ruleTeam(): array
{
return static::getStaticCacheValue('enabled_teams', function () {
return app()->pdo->createCommand('SELECT * FROM `teams` WHERE `enabled` = 1 ORDER BY `sort_index`,`id`')->queryAll();
return app()->pdo->createCommand('SELECT * FROM `teams` WHERE `id` > 0 AND `enabled` = 1 ORDER BY `sort_index`,`id`')->queryAll();
}, 86400);
}

Expand Down
100 changes: 67 additions & 33 deletions apps/models/form/TorrentUploadForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ class TorrentUploadForm extends Validator

/** @var \Rid\Http\UploadFile */
public $file;
/** @var \Rid\Http\UploadFile */
public $nfo;

public $category;
public $title;
public $subtitle = '';
public $descr;
public $uplver = 0; // If user upload this torrent Anonymous

public $anonymous = 0; // If user upload this torrent Anonymous
public $hr = 0; // TODO This torrent require hr check

// Quality
Expand Down Expand Up @@ -61,42 +63,43 @@ public static function inputRules()
{
$categories_id_list = array_map(function ($cat) {
return $cat['id'];
}, Site::ruleCategory());
}, Site::ruleCanUsedCategory());

$rules = [
'title' => 'required',
'file' => [
['required'],
['Upload\Required'],
['Upload\Extension', ['allowed' => 'torrent']],
['Upload\Size', ['size' => config('torrent.max_file_size') . 'B']]
['Upload\Size', ['size' => config('upload.max_torrent_file_size') . 'B']]
],
'category' => [
['required'], ['Integer'],
['InList', ['list' => $categories_id_list]]
],
'descr' => 'required',
'uplver' => [
['InList', ['list' => [0, 1]]]
],
'hr' => [
['InList', ['list' => [0, 1]]]
],
];

if (app()->request->post('nfo')) {
if (config('torrent_upload.enable_upload_nfo') && // Enable nfo upload
app()->user->isPrivilege('upload_nfo_file') && // This user can upload nfo
app()->request->post('nfo') // Nfo file upload
) {
$rules['nfo'] = [
['Upload\Extension', ['allowed' => ['nfo', 'txt']]],
['Upload\Size', ['size' => config('torrent.max_nfo_size') . 'B']]
['Upload\Size', ['size' => config('upload.max_nfo_file_size') . 'B']]
];
}

// Add Quality Valid
foreach (Site::getQualityTableList() as $quality => $title) {
// TODO add config key
$quality_id_list = array_map(function ($cat) {
return $cat['id'];
}, Site::ruleQuality($quality));
$quality_id_list = [0];
// IF enabled this quality field , then load it value list from setting
// Else we just allow the default value 0 to prevent cheating
if (config('torrent_upload.enable_quality_' . $quality)) {
$quality_id_list += array_map(function ($cat) {
return $cat['id'];
}, Site::ruleQuality($quality));
}

$rules[$quality] = [
['Integer'],
Expand All @@ -105,14 +108,33 @@ public static function inputRules()
}

// Add Team id Valid
$team_id_list = array_map(function ($team) {
return $team['id'];
}, Site::ruleCanUsedTeam());
$team_id_list = [0];
if (config('torrent_upload.enable_teams')) {
$team_id_list += array_map(function ($team) {
return $team['id'];
}, Site::ruleCanUsedTeam());
}

$rules['team'] = [
['Integer'],
['InList', ['list' => $team_id_list]]
];

// Add Flag Valid
// Notice: we don't valid if user have privilege to use this value,
// Un privilege flag will be rewrite in rewriteFlags() when call flush()
if (config('torrent_upload.enable_anonymous')) {
$rules['uplver'] = [
['InList', ['list' => [0, 1]]]
];
}
if (config('torrent_upload.enable_hr')) {
$rules['hr'] = [
['InList', ['list' => [0, 1]]]
];
}


return $rules;
}

Expand Down Expand Up @@ -244,11 +266,11 @@ public function flush()
'team' => $this->team,
'descr' => $this->descr,
'nfo' => $nfo_blob,
'uplver' => $this->uplver, 'hr' => $this->hr
'uplver' => $this->anonymous, 'hr' => $this->hr
])->execute();
$this->id = app()->pdo->getLastInsertId();

$this->insertTags();
if (config('torrent_upload.enable_tags')) $this->insertTags();
$this->setBuff();

// Save this torrent
Expand All @@ -269,13 +291,24 @@ public function flush()
throw $e;
}

Site::writeLog("Torrent {$this->id} ({$this->title}) was uploaded by " . ($this->uplver ? 'Anonymous' : app()->user->getUsername()));
Site::writeLog("Torrent {$this->id} ({$this->title}) was uploaded by " . ($this->anonymous ? 'Anonymous' : app()->user->getUsername()));
}

// TODO Check and rewrite torrent flags if user don't reach flags privilege
// Check and rewrite torrent flags based on site config and user's privilege of upload flags
private function rewriteFlags()
{

foreach (['anonymous','hr'] as $flag) {
$config = config('torrent_upload.enable_' . $flag);
if ($config == 2) { // if global config force enabled this flag
$this->$flag = 1;
} elseif ($config == 0) { // if global config disabled this flag
$this->$flag = 0;
} else { // check if user can use this flag
if (!app()->user->isPrivilege('upload_flag_' . $flag)) {
$this->$flag = 0;
}
}
}
}

private function insertTags()
Expand All @@ -298,16 +331,17 @@ private function insertTags()
if (strlen($tag) > 0) {
$tag_id = app()->redis->zScore('site:torrents_all_tags', $tag); // check if it is exist tag in cache
if ($tag_id == 0) { // un-exist tag
// TODO Check if allow user to add un-exist tag
try { // insert tag to database and cache
app()->pdo->createCommand('INSERT INTO `tags`(`tag`) VALUES (:tag)')->bindParams([
'tag' => $tag
])->execute();
$tag_id = app()->pdo->getLastInsertId();
app()->redis->zAdd('site:torrents_all_tags', $tag_id, $tag);
$tag_id_list[] = ['tag_id' => $tag_id, 'torrent_id' => $this->id];
} catch (\Exception $e) {
continue;
if (config('torrent_upload.allow_new_custom_tags')) {
try { // insert tag to database and cache
app()->pdo->createCommand('INSERT INTO `tags`(`tag`) VALUES (:tag)')->bindParams([
'tag' => $tag
])->execute();
$tag_id = app()->pdo->getLastInsertId();
app()->redis->zAdd('site:torrents_all_tags', $tag_id, $tag);
$tag_id_list[] = ['tag_id' => $tag_id, 'torrent_id' => $this->id];
} catch (\Exception $e) {
continue;
}
}
} else {
$tag_id_list[] = ['tag_id' => $tag_id, 'torrent_id' => $this->id];
Expand Down
15 changes: 10 additions & 5 deletions apps/views/torrent/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
<select id="category" name="category" class="form-control">
<option value="0" selected>[Select a category]</option>
<?php foreach (Site::ruleCategory() as $category) : ?>
<?php if ($category['id'] == 0) { continue; } ?>
<option value="<?= $category['id'] ?>"><?= $category['full_path'] ?></option>
<?php endforeach; ?>
</select>
Expand Down Expand Up @@ -59,30 +58,33 @@
<div class="help-block">You should obey our upload rules. **LINK**</div>
</td> <!-- FIXME link url -->
</tr>
<?php if (config('torrent_upload.enable_subtitle')): ?>
<tr>
<td class="nowrap"><label for="subtitle">Sub Title</label></td>
<td><input id="subtitle" name="subtitle" class="form-control" type="text"
placeholder="The subtitle of Your upload torrent">
<div class="help-block">You should obey our upload rules. **LINK**</div>
</td> <!-- FIXME link url -->
</tr>
<?php endif; ?>
<tr>
<td class="nowrap"><label>Quality</label></td>
<td>
<div class="row">
<?php foreach (Site::getQualityTableList() as $quality => $title): ?>
<?php if (config('torrent_upload.enable_quality_' . $quality)) : ?>
<div class="col-md-3">
<div class="input-group">
<span class="input-group-addon"><label for="<?= $quality ?>"><?= $title ?></label></span>
<select class="form-control" id="<?= $quality ?>" name="<?= $quality ?>">
<option value="0">[Choose One]</option>
<?php foreach (Site::ruleQuality($quality) as $q): ?>
<?php if ($q['id'] == 0) { continue; } ?>
<option value="<?= $q['id']; ?>"><?= $q['name']; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
</td> <!-- FIXME link url -->
Expand All @@ -97,7 +99,6 @@
<select id="team" name="team" class="form-control">
<option value="0" selected>[Choose One]</option>
<?php foreach (Site::ruleCanUsedTeam() as $team) : ?>
<?php if ($team['id'] == 0){ continue; } ?>
<option value="<?= $team['id'] ?>"><?= $team['name'] ?></option>
<?php endforeach; ?>
</select>
Expand All @@ -106,6 +107,7 @@
</div>
</td> <!-- FIXME link url -->
</tr>
<?php if (config('torrent_upload.enable_upload_nfo') && app()->user->isPrivilege('upload_nfo_file')): ?>
<tr>
<td class="nowrap"><label for="nfo">NFO File</label></td>
<td>
Expand All @@ -116,13 +118,15 @@
</div>
</div>
</tr>
<?php endif ?>
<tr>
<td class="nowrap"><label for="descr" class="required">Description</label></td>
<td>
<textarea id="descr" name="descr" class="form-control" style="width: 99%"
cols="100" rows="10" required="required"></textarea>
</td>
</tr>
<?php if (config('torrent_upload.enable_tags')):?>
<tr>
<td class="nowrap"><label for="tags">Tags</label></td>
<td><input id="tags" name="tags" class="form-control" type="text">
Expand All @@ -132,15 +136,16 @@
<a href="javascript:" class="add-tag label label-outline <?= $tag['class_name'] ?>"><?= $tag['tag'] ?></a>
<?php endforeach; ?>
</div>
</td> <!-- FIXME link url -->
</td>
</tr>
<?php endif; ?>
<tr>
<td class="nowrap"><label for="descr">Flags</label></td>
<td>
<div class="row">
<div class="col-md-3">
<div class="switch<?= app()->user->getClass(true) > config('authority.upload_anonymous') ? '' : ' disabled' ?>">
<input type="checkbox" id="uplver" name="uplver" value="1"><label for="uplver">Anonymous Upload</label>
<input type="checkbox" id="anonymous" name="anonymous" value="1"><label for="anonymous">Anonymous Upload</label>
</div>
</div>
<div class="col-md-3">
Expand Down
23 changes: 18 additions & 5 deletions migration/ridpt.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jul 21, 2019 at 02:19 PM
-- Generation Time: Jul 21, 2019 at 03:15 PM
-- Server version: 8.0.16
-- PHP Version: 7.3.7

Expand Down Expand Up @@ -619,7 +619,9 @@ INSERT INTO `site_config` (`name`, `value`) VALUES
('authority.see_banned_torrent', '40'),
('authority.see_extend_debug_log', '90'),
('authority.see_pending_torrent', '40'),
('authority.upload_anonymous', '5'),
('authority.upload_flag_anonymous', '5'),
('authority.upload_flag_hr', '40'),
('authority.upload_nfo_file', '5'),
('base.enable_extend_debug', '1'),
('base.enable_invite_system', '1'),
('base.enable_register_system', '1'),
Expand Down Expand Up @@ -679,8 +681,17 @@ INSERT INTO `site_config` (`name`, `value`) VALUES
('route.admin_index', '60'),
('route.admin_service', '90'),
('security.max_login_attempts', '10'),
('torrent.max_file_size', '3145728'),
('torrent.max_nfo_size', '65535'),
('torrent_upload.allow_new_custom_tags', '0'),
('torrent_upload.enable_anonymous', '1'),
('torrent_upload.enable_hr', '1'),
('torrent_upload.enable_quality_audio', '1'),
('torrent_upload.enable_quality_codec', '1'),
('torrent_upload.enable_quality_medium', '1'),
('torrent_upload.enable_quality_resolution', '1'),
('torrent_upload.enable_subtitle', '1'),
('torrent_upload.enable_tags', '1'),
('torrent_upload.enable_teams', '1'),
('torrent_upload.enable_upload_nfo', '1'),
('tracker.cheater_check', '1'),
('tracker.enable_announce', '1'),
('tracker.enable_maxdlsystem', '1'),
Expand All @@ -691,7 +702,9 @@ INSERT INTO `site_config` (`name`, `value`) VALUES
('tracker.min_interval', '60'),
('tracker.retry_interval', '120'),
('tracker.user_max_leech', '1'),
('tracker.user_max_seed', '3');
('tracker.user_max_seed', '3'),
('upload.max_nfo_file_size', '65535'),
('upload.max_torrent_file_size', '3145728');

-- --------------------------------------------------------

Expand Down

0 comments on commit bb01120

Please sign in to comment.