Skip to content

Commit

Permalink
Check is_array() before foreach() and in_array()
Browse files Browse the repository at this point in the history
  • Loading branch information
kinosang committed Nov 2, 2015
1 parent 09626c9 commit 9d3a8e1
Show file tree
Hide file tree
Showing 17 changed files with 239 additions and 112 deletions.
10 changes: 10 additions & 0 deletions cron/PwCronDoClearPeers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ class PwCronDoClearPeers extends AbstractCronBase
public function run($cronId)
{
$torrents = Wekit::load('EXT:torrent.service.PwTorrent')->fetchTorrent();

if (!is_array($torrents)) {
return;
}

foreach ($torrents as $torrent) {
$peers = Wekit::load('EXT:torrent.service.PwTorrentPeer')->getTorrentPeerByTorrent($torrent['id']);

if (!is_array($peers)) {
continue;
}

foreach ($peers as $peer) {
if (strtotime($peer['last_action']) < strtotime('-' . Wekit::C('site', 'app.torrent.cron.peertimeout') . ' minute')) {
Wekit::load('EXT:torrent.service.PwTorrentPeer')->deleteTorrentPeer($peer['id']);
Expand Down
7 changes: 6 additions & 1 deletion cron/PwCronDoClearTorrents.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ private function deleteThread($topic)
public function run($cronId)
{
$torrentimeout = Wekit::C('site', 'app.torrent.cron.torrentimeout');

if ($torrentimeout < 1) {
return null;
}

$torrents = Wekit::load('EXT:torrent.service.PwTorrent')->fetchTorrent();

if (!is_array($torrents)) {
return;
}

foreach ($torrents as $torrent) {
$topic = Wekit::load('forum.PwThread')->getThread($torrent['tid']);

if ($topic['disabled'] > 0) {
if (is_array($topic) && $topic['disabled'] > 0) {
continue;
}

Expand Down
69 changes: 44 additions & 25 deletions extensions/torrent/admin/ManageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ public function themeAction()
public function dorunAction()
{
list($showuserinfo, $titlegenifopen, $titlegendouban, $check, $deniedfts, $torrentnameprefix, $peertimeout, $torrentimeout) = $this->getInput(array('showuserinfo', 'titlegenifopen', 'titlegendouban', 'check', 'deniedfts', 'torrentnameprefix', 'peertimeout', 'torrentimeout'), 'post');
foreach ($deniedfts as $key => $value) {
if (empty($value)) {
continue;
}

$_deniedfts[$key] = $value;
if (is_array($deniedfts)) {
foreach ($deniedfts as $key => $value) {
if (empty($value)) {
continue;
}

$_deniedfts[$key] = $value;
}
}

if (empty($torrentnameprefix)) {
$torrentnameprefix = Wekit::C('site', 'info.name');
}
Expand All @@ -68,29 +72,35 @@ public function dorunAction()

$config = new PwConfigSet('site');
$config->set('app.torrent.showuserinfo', $showuserinfo)->set('app.torrent.titlegen.ifopen', $titlegenifopen)->set('app.torrent.titlegen.douban', $titlegendouban)->set('app.torrent.check', $check)->set('app.torrent.torrentnameprefix', $torrentnameprefix)->set('app.torrent.cron.peertimeout', intval($peertimeout))->set('app.torrent.cron.torrentimeout', intval($torrentimeout));

if (!empty($deniedfts)) {
$config->set('app.torrent.deniedfts', $_deniedfts);
}

$config->flush();

$this->showMessage('ADMIN:success');
}

public function docreditAction()
{
list($creditifopen, $credits) = $this->getInput(array('creditifopen', 'credits'), 'post');

$_credits = array();
!$credits && $credits = array();
foreach ($credits as $key => $credit) {
if (!$credit['enabled'] || empty($credit['func'])) {
continue;
}

$_credits[$key] = $credit;
if (is_array($credits)) {
foreach ($credits as $key => $credit) {
if (!$credit['enabled'] || empty($credit['func'])) {
continue;
}

$_credits[$key] = $credit;
}
}

$config = new PwConfigSet('site');
$config->set('app.torrent.creditifopen', intval($creditifopen))->set('app.torrent.credits', $_credits)->flush();

$this->showMessage('ADMIN:success');
}

Expand All @@ -101,26 +111,34 @@ public function doagentAction()
$PwTorrentAgentDs->deleteTorrentAgent($this->getInput('id', 'post'));
} else {
Wind::import('EXT:torrent.service.dm.PwTorrentAgentDm');

list($allowedClients, $newAllowedClients) = $this->getInput(array('allowedClients', 'newAllowedClients'), 'post');
foreach ($allowedClients as $key => $allowedClient) {
if (empty($allowedClient['family']) || empty($allowedClient['agent_pattern'])) {
continue;
}

$dm = new PwTorrentAgentDm($key);
$dm->setFamily($allowedClient['family'])->setPeeridPattern($allowedClient['peer_id_pattern'])->setAgentPattern($allowedClient['agent_pattern'])->setAllowHttps($allowedClient['allowhttps']);
$PwTorrentAgentDs->updateTorrentAgent($dm);
}
foreach ($newAllowedClients as $key => $allowedClient) {
if (empty($allowedClient['family']) || empty($allowedClient['agent_pattern'])) {
continue;
if (is_array($allowedClients)) {
foreach ($allowedClients as $key => $allowedClient) {
if (empty($allowedClient['family']) || empty($allowedClient['agent_pattern'])) {
continue;
}

$dm = new PwTorrentAgentDm($key);
$dm->setFamily($allowedClient['family'])->setPeeridPattern($allowedClient['peer_id_pattern'])->setAgentPattern($allowedClient['agent_pattern'])->setAllowHttps($allowedClient['allowhttps']);
$PwTorrentAgentDs->updateTorrentAgent($dm);
}
}

if (is_array($newAllowedClients)) {
foreach ($newAllowedClients as $key => $allowedClient) {
if (empty($allowedClient['family']) || empty($allowedClient['agent_pattern'])) {
continue;
}

$dm = new PwTorrentAgentDm();
$dm->setFamily($allowedClient['family'])->setPeeridPattern($allowedClient['peer_id_pattern'])->setAgentPattern($allowedClient['agent_pattern'])->setAllowHttps($allowedClient['allowhttps']);
$PwTorrentAgentDs->addTorrentAgent($dm);
$dm = new PwTorrentAgentDm();
$dm->setFamily($allowedClient['family'])->setPeeridPattern($allowedClient['peer_id_pattern'])->setAgentPattern($allowedClient['agent_pattern'])->setAllowHttps($allowedClient['allowhttps']);
$PwTorrentAgentDs->addTorrentAgent($dm);
}
}
}

$this->showMessage('ADMIN:success');
}

Expand All @@ -131,6 +149,7 @@ public function dothemeAction()
$config = new PwConfigSet('site');
$config->set('app.torrent.theme.showpeers', $showpeers);
$config->flush();

$this->showMessage('ADMIN:success');
}

Expand Down
78 changes: 47 additions & 31 deletions extensions/torrent/controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ public function updateInfoAction()
if ($w_type == 12) {
// 豆瓣
$url = 'https://api.douban.com/v2/movie/subject/' . $wikilink;

if (!empty(Wekit::C('site', 'app.torrent.titlegen.douban'))) {
$url .= '?apikey=' . Wekit::C('site', 'app.torrent.titlegen.douban');
}

$result = json_decode(PwUpdateInfo::curl($url));

$title = '[' . $result->countries[0] . ']'; // 国别
$title .= '[' . $result->year . ']'; // 年份
$title .= '[' . $result->title . ']'; // 影片中文名
Expand Down Expand Up @@ -209,18 +211,24 @@ public function announceAction()
Wind::import('EXT:torrent.service.srv.helper.PwAnnounce');

// Check if a BitTorrent client
$allowed = false;

$allowedClients = $this->_getTorrentAgentDS()->fetchTorrentAgent();
foreach ($allowedClients as $allowedClient) {
if (!preg_match($allowedClient['agent_pattern'], $agent)) {
continue;
}

if ($allowedClient['peer_id_pattern'] == '' || preg_match($allowedClient['peer_id_pattern'], $peerId)) {
$allowed = true;
}
if (is_array($allowedClients)) {
foreach ($allowedClients as $allowedClient) {
if (!preg_match($allowedClient['agent_pattern'], $agent)) {
continue;
}

if ($allowedClient['peer_id_pattern'] == '' || preg_match($allowedClient['peer_id_pattern'], $peerId)) {
$allowed = true;
}

break;
break;
}
}

if (!$allowed) {
PwAnnounce::showError('This a a bittorrent application and can\'t be loaded into a browser!');
}
Expand Down Expand Up @@ -343,14 +351,16 @@ public function announceAction()
$WindApi = WindidApi::api('user');
$pwUser = Wekit::load('user.PwUser');
$crdtits = $WindApi->getUserCredit($user['uid']);
$_credits = Wekit::C('site', 'app.torrent.credits');
$user_torrents = count($this->_getTorrentDS()->fetchTorrentByUid($user['uid']));
$histories = $this->_getTorrentHistoryDs()->fetchTorrentHistoryByUid($user['uid']);

foreach ($histories as $history) {
$downloaded_total += $history['downloaded'];
$uploaded_total += $history['uploaded'];
if (is_array($histories)) {
foreach ($histories as $history) {
$downloaded_total += $history['downloaded'];
$uploaded_total += $history['uploaded'];
}
}

unset($histories);

if ($downloaded_total != 0) {
Expand All @@ -371,15 +381,19 @@ public function announceAction()
$m->e('time = ' . intval($timeUsed));
$m->e('torrents = ' . intval($user_torrents));

foreach ($_credits as $key => $value) {
if ($value['enabled'] != '1') {
continue;
}
$_credits = Wekit::C('site', 'app.torrent.credits');

$m->e('credit = ' . intval($crdtits['credit' . $key]));
if (is_array($_credits)) {
foreach ($_credits as $key => $value) {
if ($value['enabled'] != '1') {
continue;
}

$m->e('credit = ' . intval($crdtits['credit' . $key]));

$changes[$key] = intval($m->e($exp));
$changed++;
$changes[$key] = intval($m->e($exp));
$changed++;
}
}

if ($changed) {
Expand Down Expand Up @@ -569,19 +583,21 @@ public function rssAction()

$torrents = $this->_getTorrentSubscribeDs()->getTorrentSubscribeByUid($this->loginUser->uid);

foreach ($torrents as $torrent) {
if ($torrent['disabled'] > 0 && !in_array($user['groupid'], array(3, 4, 5))) {
continue;
if (is_array($torrents)) {
foreach ($torrents as $torrent) {
if ($torrent['disabled'] > 0 && !in_array($user['groupid'], array(3, 4, 5))) {
continue;
}
echo '<item>';
echo '<title><![CDATA[' . $torrent['filename'] . ']]></title>';
echo '<link><![CDATA[' . WindUrlHelper::createUrl('/bbs/read/run?tid=' . $torrent['tid']) . ']]></link>';
echo '<pubDate>' . date('D, d M Y H:i:s O', $torrent['created_time']) . '</pubDate>';
echo '<description><![CDATA[' . $torrent['subject'] . ']]></description>';
echo '<enclosure type="application/x-bittorrent" length="' . $torrent['size'] . '" url="' . str_replace('&', '&amp;', WindUrlHelper::createUrl('/app/torrent/index/download?id=' . $torrent['torrent'] . '&passkey=' . $passkey)) . '" />';
echo '<author><![CDATA[' . $torrent['created_username'] . ']]></author>';
echo '<category domain="' . WindUrlHelper::createUrl('/bbs/thread/run?fid=' . $torrent['fid']) . '"><![CDATA[' . $torrent['name'] . ']]></category>';
echo '</item>';
}
echo '<item>';
echo '<title><![CDATA[' . $torrent['filename'] . ']]></title>';
echo '<link><![CDATA[' . WindUrlHelper::createUrl('/bbs/read/run?tid=' . $torrent['tid']) . ']]></link>';
echo '<pubDate>' . date('D, d M Y H:i:s O', $torrent['created_time']) . '</pubDate>';
echo '<description><![CDATA[' . $torrent['subject'] . ']]></description>';
echo '<enclosure type="application/x-bittorrent" length="' . $torrent['size'] . '" url="' . str_replace('&', '&amp;', WindUrlHelper::createUrl('/app/torrent/index/download?id=' . $torrent['torrent'] . '&passkey=' . $passkey)) . '" />';
echo '<author><![CDATA[' . $torrent['created_username'] . ']]></author>';
echo '<category domain="' . WindUrlHelper::createUrl('/bbs/thread/run?fid=' . $torrent['fid']) . '"><![CDATA[' . $torrent['name'] . ']]></category>';
echo '</item>';
}

echo '</channel>';
Expand Down
69 changes: 48 additions & 21 deletions extensions/torrent/service/srv/do/PwPostDoTorrent.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,49 +86,76 @@ public function check($postDm)
if (!count($flist)) {
return new PwError('种子不存在任何文件,请检查种子是否正确!');
}

$totalLength = 0;
foreach ($flist as $fn) {
list($ll, $ff) = $bencode->doDictionaryCheck($fn, 'length(integer):path(list)');
$totalLength += $ll;
$ffa = array();
foreach ($ff as $ffe) {
if ($ffe['type'] != 'string') {

if (is_array($flist)) {
foreach ($flist as $fn) {
list($ll, $ff) = $bencode->doDictionaryCheck($fn, 'length(integer):path(list)');

$totalLength += $ll;

$ffa = array();

if (is_array($ff)) {
foreach ($ff as $ffe) {
if ($ffe['type'] != 'string') {
return new PwError('种子存在文件名错误,请检查种子是否正确!');
}
$ffa[] = $ffe['value'];
}
}

if (!count($ffa)) {
return new PwError('种子存在文件名错误,请检查种子是否正确!');
}
$ffa[] = $ffe['value'];
}
if (!count($ffa)) {
return new PwError('种子存在文件名错误,请检查种子是否正确!');

$ffe = implode('/', $ffa);
$fileList[] = array($ffe, $ll);
}
$ffe = implode('/', $ffa);
$fileList[] = array($ffe, $ll);
}
$type = 'multi';
}
if (in_array('deniedfts', Wekit::C('site', 'app.torrent.check'))) {
$deniedfts = Wekit::C('site', 'app.torrent.deniedfts');
foreach ($fileList as $file) {
$ft = substr(strrchr($file[0], '.'), 1);
if (in_array($ft, $deniedfts)) {
return new PwError('种子内存在禁止发布的文件类型!');

$torrentcheck = Wekit::C('site', 'app.torrent.check');

if (is_array($torrentcheck)) {
if (in_array('deniedfts', $torrentcheck)) {
$deniedfts = Wekit::C('site', 'app.torrent.deniedfts');

if (is_array($deniedfts)) {
foreach ($fileList as $file) {
$ft = substr(strrchr($file[0], '.'), 1);
if (in_array($ft, $deniedfts)) {
return new PwError('种子内存在禁止发布的文件类型!');
}
}
}
}

if (in_array('source', $torrentcheck)) {
$dictionary['value']['info']['value']['source'] = $bencode->doDecode($bencode->doEncodeString(Wekit::C('site', 'info.name')));
}
}

$dictionary['value']['announce'] = $bencode->doDecode($bencode->doEncodeString(Wekit::C('site', 'info.url') . '/announce.php'));
$dictionary['value']['info']['value']['private'] = $bencode->doDecode('i1e');

if (in_array('source', Wekit::C('site', 'app.torrent.check'))) {
$dictionary['value']['info']['value']['source'] = $bencode->doDecode($bencode->doEncodeString(Wekit::C('site', 'info.name')));
}
unset($dictionary['value']['announce-list']);
unset($dictionary['value']['nodes']);

$dictionary = $bencode->doDecode($bencode->doEncode($dictionary));

list($announce, $info) = $bencode->doDictionaryCheck($dictionary, 'announce(string):info');

$infohash = pack('H*', sha1($info['string']));

$check = $this->_getTorrentDS()->getTorrentByInfoHash($infohash);

if ($check) {
return new PwError('不能发布重复种子资源');
}

$this->dictionary = $dictionary;
$this->infohash = $infohash;
$this->filename = $filename;
Expand Down

0 comments on commit 9d3a8e1

Please sign in to comment.