Dotclear

source: plugins/antispam/index.php @ 3987:dcc501ff21f4

Revision 3987:dcc501ff21f4, 8.4 KB checked in by franck <carnet.franck.paul@…>, 6 years ago (diff)

Switching from inline JS variables to JSON script, antispam plugin

Line 
1<?php
2/**
3 * @brief antispam, a plugin for Dotclear 2
4 *
5 * @package Dotclear
6 * @subpackage Plugins
7 *
8 * @copyright Olivier Meunier & Association Dotclear
9 * @copyright GPL-2.0-only
10 */
11
12if (!defined('DC_CONTEXT_ADMIN')) {return;}
13dcPage::check('admin');
14
15dcAntispam::initFilters();
16$filters = dcAntispam::$filters->getFilters();
17
18$page_name   = __('Antispam');
19$filter_gui  = false;
20$default_tab = null;
21
22try
23{
24    # Show filter configuration GUI
25    if (!empty($_GET['f'])) {
26        if (!isset($filters[$_GET['f']])) {
27            throw new Exception(__('Filter does not exist.'));
28        }
29
30        if (!$filters[$_GET['f']]->hasGUI()) {
31            throw new Exception(__('Filter has no user interface.'));
32        }
33
34        $filter     = $filters[$_GET['f']];
35        $filter_gui = $filter->gui($filter->guiURL());
36    }
37
38    # Remove all spam
39    if (!empty($_POST['delete_all'])) {
40        $ts = dt::str('%Y-%m-%d %H:%M:%S', $_POST['ts'], $core->blog->settings->system->blog_timezone);
41
42        dcAntispam::delAllSpam($core, $ts);
43
44        dcPage::addSuccessNotice(__('Spam comments have been successfully deleted.'));
45        http::redirect($p_url);
46    }
47
48    # Update filters
49    if (isset($_POST['filters_upd'])) {
50        $filters_opt = [];
51        $i           = 0;
52        foreach ($filters as $fid => $f) {
53            $filters_opt[$fid] = [false, $i];
54            $i++;
55        }
56
57        # Enable active filters
58        if (isset($_POST['filters_active']) && is_array($_POST['filters_active'])) {
59            foreach ($_POST['filters_active'] as $v) {
60                $filters_opt[$v][0] = true;
61            }
62        }
63
64        # Order filters
65        if (!empty($_POST['f_order']) && empty($_POST['filters_order'])) {
66            $order = $_POST['f_order'];
67            asort($order);
68            $order = array_keys($order);
69        } elseif (!empty($_POST['filters_order'])) {
70            $order = explode(',', trim($_POST['filters_order'], ','));
71        }
72
73        if (isset($order)) {
74            foreach ($order as $i => $f) {
75                $filters_opt[$f][1] = $i;
76            }
77        }
78
79        # Set auto delete flag
80        if (isset($_POST['filters_auto_del']) && is_array($_POST['filters_auto_del'])) {
81            foreach ($_POST['filters_auto_del'] as $v) {
82                $filters_opt[$v][2] = true;
83            }
84        }
85
86        dcAntispam::$filters->saveFilterOpts($filters_opt);
87
88        dcPage::addSuccessNotice(__('Filters configuration has been successfully saved.'));
89        http::redirect($p_url);
90    }
91} catch (Exception $e) {
92    $core->error->add($e->getMessage());
93}
94?>
95<html>
96<head>
97  <title><?php echo ($filter_gui !== false ? sprintf(__('%s configuration'), $filter->name) . ' - ' : '') . $page_name; ?></title>
98  <?php
99echo dcPage::jsPageTabs($default_tab);
100$core->auth->user_prefs->addWorkspace('accessibility');
101if (!$core->auth->user_prefs->accessibility->nodragdrop) {
102    echo
103    dcPage::jsLoad('js/jquery/jquery-ui.custom.js') .
104    dcPage::jsLoad('js/jquery/jquery.ui.touch-punch.js');
105}
106echo
107    dcPage::jsJson('antispam', ['confirm_spam_delete' => __('Are you sure you want to delete all spams?')]) .
108    dcPage::jsLoad(dcPage::getPF('antispam/js/antispam.js')) .
109    dcPage::cssLoad(dcPage::getPF('antispam/style.css'));
110?>
111</head>
112<body>
113<?php
114
115if ($filter_gui !== false) {
116    echo dcPage::breadcrumb(
117        [
118            __('Plugins')                                         => '',
119            $page_name                                            => $p_url,
120            sprintf(__('%s filter configuration'), $filter->name) => ''
121        ]) .
122    dcPage::notices();
123
124    echo '<p><a href="' . $p_url . '" class="back">' . __('Back to filters list') . '</a></p>';
125
126    echo $filter_gui;
127
128    if ($filter->help) {
129        dcPage::helpBlock($filter->help);
130    }
131} else {
132    echo dcPage::breadcrumb(
133        [
134            __('Plugins') => '',
135            $page_name    => ''
136        ]) .
137    dcPage::notices();
138
139    # Information
140    $spam_count      = dcAntispam::countSpam($core);
141    $published_count = dcAntispam::countPublishedComments($core);
142    $moderationTTL   = $core->blog->settings->antispam->antispam_moderation_ttl;
143
144    echo
145    '<form action="' . $p_url . '" method="post" class="fieldset">' .
146    '<h3>' . __('Information') . '</h3>';
147
148    echo
149    '<ul class="spaminfo">' .
150    '<li class="spamcount"><a href="' . $core->adminurl->get('admin.comments', ['status' => '-2']) . '">' . __('Junk comments:') . '</a> ' .
151    '<strong>' . $spam_count . '</strong></li>' .
152    '<li class="hamcount"><a href="' . $core->adminurl->get('admin.comments', ['status' => '1']) . '">' . __('Published comments:') . '</a> ' .
153        $published_count . '</li>' .
154        '</ul>';
155
156    if ($spam_count > 0) {
157        echo
158        '<p>' . $core->formNonce() .
159        form::hidden('ts', time()) .
160        '<input name="delete_all" class="delete" type="submit" value="' . __('Delete all spams') . '" /></p>';
161    }
162    if ($moderationTTL != null && $moderationTTL >= 0) {
163        echo '<p>' . sprintf(__('All spam comments older than %s day(s) will be automatically deleted.'), $moderationTTL) . ' ' .
164        sprintf(__('You can modify this duration in the %s'), '<a href="' . $core->adminurl->get('admin.blog.pref') .
165            '#antispam_moderation_ttl"> ' . __('Blog settings') . '</a>') .
166            '.</p>';
167    }
168    echo '</form>';
169
170    # Filters
171    echo
172        '<form action="' . $p_url . '" method="post" id="filters-list-form">';
173
174    if (!empty($_GET['upd'])) {
175        dcPage::success(__('Filters configuration has been successfully saved.'));
176    }
177
178    echo
179    '<div class="table-outer">' .
180    '<table class="dragable">' .
181    '<caption class="as_h3">' . __('Available spam filters') . '</caption>' .
182    '<thead><tr>' .
183    '<th>' . __('Order') . '</th>' .
184    '<th>' . __('Active') . '</th>' .
185    '<th>' . __('Auto Del.') . '</th>' .
186    '<th class="nowrap">' . __('Filter name') . '</th>' .
187    '<th colspan="2">' . __('Description') . '</th>' .
188        '</tr></thead>' .
189        '<tbody id="filters-list" >';
190
191    $i = 1;
192    foreach ($filters as $fid => $f) {
193        $gui_link = '&nbsp;';
194        if ($f->hasGUI()) {
195            $gui_link =
196            '<a href="' . html::escapeHTML($f->guiURL()) . '">' .
197            '<img src="images/edit-mini.png" alt="' . __('Filter configuration') . '" ' .
198            'title="' . __('Filter configuration') . '" /></a>';
199        }
200
201        echo
202        '<tr class="line' . ($f->active ? '' : ' offline') . '" id="f_' . $fid . '">' .
203        '<td class="handle">' . form::number(['f_order[' . $fid . ']'], [
204            'min'        => 1,
205            'max'        => count($filters),
206            'default'    => $i,
207            'class'      => 'position',
208            'extra_html' => 'title="' . __('position') . '"'
209        ]) .
210        '</td>' .
211        '<td class="nowrap">' . form::checkbox(['filters_active[]'], $fid,
212            [
213                'checked'    => $f->active,
214                'extra_html' => 'title="' . __('Active') . '"'
215            ]
216        ) . '</td>' .
217        '<td class="nowrap">' . form::checkbox(['filters_auto_del[]'], $fid,
218            [
219                'checked'    => $f->auto_delete,
220                'extra_html' => 'title="' . __('Auto Del.') . '"'
221            ]
222        ) . '</td>' .
223        '<td class="nowrap" scope="row">' . $f->name . '</td>' .
224        '<td class="maximal">' . $f->description . '</td>' .
225            '<td class="status">' . $gui_link . '</td>' .
226            '</tr>';
227        $i++;
228    }
229    echo
230    '</tbody></table></div>' .
231    '<p>' . form::hidden('filters_order', '') .
232    $core->formNonce() .
233    '<input type="submit" name="filters_upd" value="' . __('Save') . '" /></p>' .
234        '</form>';
235
236    # Syndication
237    if (DC_ADMIN_URL) {
238        $ham_feed = $core->blog->url . $core->url->getURLFor(
239            'hamfeed',
240            $code = dcAntispam::getUserCode($core)
241        );
242        $spam_feed = $core->blog->url . $core->url->getURLFor(
243            'spamfeed',
244            $code = dcAntispam::getUserCode($core)
245        );
246
247        echo
248        '<h3>' . __('Syndication') . '</h3>' .
249        '<ul class="spaminfo">' .
250        '<li class="feed"><a href="' . $spam_feed . '">' . __('Junk comments RSS feed') . '</a></li>' .
251        '<li class="feed"><a href="' . $ham_feed . '">' . __('Published comments RSS feed') . '</a></li>' .
252            '</ul>';
253    }
254
255    dcPage::helpBlock('antispam', 'antispam-filters');
256}
257
258?>
259
260</body>
261</html>
Note: See TracBrowser for help on using the repository browser.

Sites map