streams/Code/Module/Sources.php

203 lines
7.2 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
2021-12-03 03:01:39 +00:00
2022-02-16 04:08:28 +00:00
namespace Code\Module;
2016-04-19 03:38:38 +00:00
use App;
2022-02-16 04:08:28 +00:00
use Code\Web\Controller;
use Code\Lib\Features;
use Code\Render\Theme;
2022-02-12 20:43:29 +00:00
2016-04-19 03:38:38 +00:00
2021-12-02 23:02:31 +00:00
class Sources extends Controller
{
public function post()
{
if (!local_channel()) {
return;
}
2022-01-25 23:20:02 +00:00
if (!Features::enabled(local_channel(), 'channel_sources')) {
2021-12-02 23:02:31 +00:00
return;
}
$source = intval($_REQUEST['source']);
$xchan = escape_tags($_REQUEST['xchan']);
$abook = intval($_REQUEST['abook']);
$words = escape_tags($_REQUEST['words']);
$resend = intval($_REQUEST['resend']);
$frequency = $_REQUEST['frequency'];
$name = escape_tags($_REQUEST['name']);
$tags = escape_tags($_REQUEST['tags']);
$channel = App::get_channel();
if ($name == '*') {
$xchan = '*';
}
if ($abook) {
2021-12-03 03:01:39 +00:00
$r = q(
"select abook_xchan from abook where abook_id = %d and abook_channel = %d limit 1",
2021-12-02 23:02:31 +00:00
intval($abook),
intval(local_channel())
);
if ($r) {
$xchan = $r[0]['abook_xchan'];
}
}
if (!$xchan) {
notice(t('Failed to create source. No channel selected.') . EOL);
return;
}
set_abconfig(local_channel(), $xchan, 'system', 'rself', $resend);
if (!$source) {
2021-12-03 03:01:39 +00:00
$r = q(
"insert into source ( src_channel_id, src_channel_xchan, src_xchan, src_patt, src_tag )
values ( %d, '%s', '%s', '%s', '%s' ) ",
2021-12-02 23:02:31 +00:00
intval(local_channel()),
dbesc($channel['channel_hash']),
dbesc($xchan),
dbesc($words),
dbesc($tags)
);
if ($r) {
info(t('Source created.') . EOL);
}
goaway(z_root() . '/sources');
} else {
2021-12-03 03:01:39 +00:00
$r = q(
"update source set src_xchan = '%s', src_patt = '%s', src_tag = '%s' where src_channel_id = %d and src_id = %d",
2021-12-02 23:02:31 +00:00
dbesc($xchan),
dbesc($words),
dbesc($tags),
intval(local_channel()),
intval($source)
);
if ($r) {
info(t('Source updated.') . EOL);
}
}
}
public function get()
{
if (!local_channel()) {
notice(t('Permission denied.') . EOL);
return EMPTY_STR;
}
2022-01-25 23:20:02 +00:00
if (!Features::enabled(local_channel(), 'channel_sources')) {
2021-12-02 23:02:31 +00:00
return EMPTY_STR;
}
// list sources
if (argc() == 1) {
2021-12-03 03:01:39 +00:00
$r = q(
"select source.*, xchan.* from source left join xchan on src_xchan = xchan_hash where src_channel_id = %d",
2021-12-02 23:02:31 +00:00
intval(local_channel())
);
if ($r) {
for ($x = 0; $x < count($r); $x++) {
if ($r[$x]['src_xchan'] == '*') {
$r[$x]['xchan_name'] = t('*');
}
$r[$x]['src_patt'] = htmlspecialchars($r[$x]['src_patt'], ENT_COMPAT, 'UTF-8');
}
}
2022-02-12 20:43:29 +00:00
$o = replace_macros(Theme::get_template('sources_list.tpl'), [
2021-12-02 23:02:31 +00:00
'$title' => t('Channel Sources'),
'$desc' => t('Manage remote sources of content for your channel.'),
'$new' => t('New Source'),
'$sources' => $r
]);
return $o;
}
if (argc() == 2 && argv(1) === 'new') {
// TODO add the words 'or RSS feed' and corresponding code to manage feeds and frequency
2022-02-12 20:43:29 +00:00
$o = replace_macros(Theme::get_template('sources_new.tpl'), [
2021-12-02 23:02:31 +00:00
'$title' => t('New Source'),
'$desc' => t('Import all or selected content from the following channel into this channel and distribute it according to your channel settings.'),
'$words' => ['words', t('Only import content with these words (one per line)'), '', t('Leave blank to import all public content')],
'$name' => ['name', t('Channel Name'), '', '', '', 'autocomplete="off"'],
'$tags' => ['tags', t('Add the following categories to posts imported from this source (comma separated)'), '', t('Optional')],
'$resend' => ['resend', t('Resend posts with this channel as author'), 0, t('Copyrights may apply'), [t('No'), t('Yes')]],
'$submit' => t('Submit')
]);
return $o;
}
if (argc() == 2 && intval(argv(1))) {
// edit source
2021-12-03 03:01:39 +00:00
$r = q(
"select source.*, xchan.* from source left join xchan on src_xchan = xchan_hash where src_id = %d and src_channel_id = %d limit 1",
2021-12-02 23:02:31 +00:00
intval(argv(1)),
intval(local_channel())
);
if ($r) {
2021-12-03 03:01:39 +00:00
$x = q(
"select abook_id from abook where abook_xchan = '%s' and abook_channel = %d limit 1",
2021-12-02 23:02:31 +00:00
dbesc($r[0]['src_xchan']),
intval(local_channel())
);
}
if (!$r) {
notice(t('Source not found.') . EOL);
return '';
}
$r[0]['src_patt'] = htmlspecialchars($r[0]['src_patt'], ENT_QUOTES, 'UTF-8');
2022-02-12 20:43:29 +00:00
$o = replace_macros(Theme::get_template('sources_edit.tpl'), array(
2021-12-02 23:02:31 +00:00
'$title' => t('Edit Source'),
'$drop' => t('Delete Source'),
'$id' => $r[0]['src_id'],
'$desc' => t('Import all or selected content from the following channel into this channel and distribute it according to your channel settings.'),
'$words' => array('words', t('Only import content with these words (one per line)'), $r[0]['src_patt'], t('Leave blank to import all public content')),
'$xchan' => $r[0]['src_xchan'],
'$abook' => $x[0]['abook_id'],
'$tags' => array('tags', t('Add the following categories to posts imported from this source (comma separated)'), $r[0]['src_tag'], t('Optional')),
'$resend' => ['resend', t('Resend posts with this channel as author'), get_abconfig(local_channel(), $r[0]['xchan_hash'], 'system', 'rself'), t('Copyrights may apply'), [t('No'), t('Yes')]],
'$name' => array('name', t('Channel Name'), $r[0]['xchan_name'], ''),
'$submit' => t('Submit')
));
return $o;
}
if (argc() == 3 && intval(argv(1)) && argv(2) === 'drop') {
2021-12-03 03:01:39 +00:00
$r = q(
"select * from source where src_id = %d and src_channel_id = %d limit 1",
2021-12-02 23:02:31 +00:00
intval(argv(1)),
intval(local_channel())
);
if (!$r) {
notice(t('Source not found.') . EOL);
return '';
}
2021-12-03 03:01:39 +00:00
$r = q(
"delete from source where src_id = %d and src_channel_id = %d",
2021-12-02 23:02:31 +00:00
intval(argv(1)),
intval(local_channel())
);
2021-12-03 03:01:39 +00:00
if ($r) {
2021-12-02 23:02:31 +00:00
info(t('Source removed') . EOL);
2021-12-03 03:01:39 +00:00
} else {
2021-12-02 23:02:31 +00:00
notice(t('Unable to remove source.') . EOL);
2021-12-03 03:01:39 +00:00
}
2021-12-02 23:02:31 +00:00
goaway(z_root() . '/sources');
}
// shouldn't get here.
}
2016-04-19 03:38:38 +00:00
}