1<?php
2
3require_once dirname(__FILE__).'/accesscheck.php';
4
5if (isset($_GET['id'])) {
6    $hash = '#'.sprintf('%d', $_GET['id']);
7    $id = sprintf('%d', $_GET['id']);
8} else {
9    $hash = '';
10    $id = 0;
11}
12
13if (isset($_GET['action']) && $_GET['action'] == 'next') {
14    if (isset($_GET['del'])) {
15        Sql_Query(sprintf('delete from %s where id = %d', $GLOBALS['tables']['bounceregex'], $_GET['del']));
16    } elseif (isset($_GET['activate'])) {
17        Sql_Query(sprintf('update %s set status = "active" where id = %d', $GLOBALS['tables']['bounceregex'],
18            $_GET['activate']));
19    } else {
20        Redirect('bouncerules');
21    }
22    $next = Sql_Fetch_Row_Query(sprintf('select id from %s where status = "candidate"',
23        $GLOBALS['tables']['bounceregex']));
24    if (!empty($next[0])) {
25        Redirect('bouncerule&id='.$next[0]);
26    } else {
27        $_SESSION['action_result'] = s('No more candidate rules');
28        Redirect('bouncerules');
29    }
30}
31
32if (isset($_POST['save']) && $_POST['save']) {
33    Sql_Query(sprintf('update %s set regex = "%s", regexhash = "%s", action="%s", comment="%s",status = "%s" where id= %d',
34        $GLOBALS['tables']['bounceregex'], trim($_POST['regex']), md5(trim($_POST['regex'])), sql_escape($_POST['action']),
35        sql_escape($_POST['comment']), sql_escape($_POST['status']), $_GET['id']), 1);
36    $num = Sql_Affected_Rows();
37    if ($num < 0) {
38        echo $GLOBALS['I18N']->get('Updating the regular expression of this rule caused an Sql conflict<br/>This is probably because there is already a rule like that. Do you want to delete this rule instead?');
39        echo '<p>'.PageLink2('bouncerules&del='.$id, $GLOBALS['I18N']->get('Yes')).'&nbsp;';
40        echo PageLink2('bouncerules', $GLOBALS['I18N']->get('No')).'</p>';
41
42        return;
43    }
44    Redirect('bouncerules'.$hash);
45}
46
47echo '<p>'.PageLinkButton('bouncerules'.$hash, $GLOBALS['I18N']->get('back to list of bounce rules')).'</p>';
48$data = Sql_Fetch_Array_Query(sprintf('select * from %s where id = %d',
49    $GLOBALS['tables']['bounceregex'], $id));
50
51echo '<div class="actions">';
52echo PageLinkButton('bouncerule', s('delete and next'), 'del='.$id.'&action=next', '',
53    s('delete this rule and go to the next candidate'));
54echo PageLinkButton('bouncerule', s('activate and next'), 'activate='.$id.'&action=next', '',
55    s('activate this rule and go to the next candidate'));
56echo '</div>';
57
58echo formStart();
59echo '<table>';
60printf('<tr><td colspan="2">%s</td></tr><tr><td colspan="2"><input type=text name="regex" size=60 value="%s"></td></tr>',
61    $GLOBALS['I18N']->get('Regular Expression'), htmlspecialchars($data['regex']));
62printf('<tr><td>%s</td><td>%s</td></tr>',
63    $GLOBALS['I18N']->get('Created By'), adminName($data['admin']));
64printf('<tr><td>%s</td><td><select name="action">', $GLOBALS['I18N']->get('Action'));
65foreach ($GLOBALS['bounceruleactions'] as $action => $desc) {
66    printf('<option value="%s" %s>%s</option>', $action, $data['action'] == $action ? 'selected' : '', $desc);
67}
68echo '</select></td></tr>';
69printf('<tr><td>%s</td><td><select name="status">', $GLOBALS['I18N']->get('Status'));
70printf('<option value="none">[%s]</option>', $GLOBALS['I18N']->get('Select Status'));
71foreach (array('active', 'candidate') as $type) {
72    printf('<option value="%s" %s>%s</option>', $type, $data['status'] == $type ? 'selected' : '',
73        $GLOBALS['I18N']->get($type));
74}
75echo '</select></td></tr>';
76printf('<tr><td colspan=2>%s</td></tr><tr><td colspan=2>
77  <textarea name="comment" rows=10 cols=65>%s</textarea></td></tr>',
78    $GLOBALS['I18N']->get('Memo for this rule'), htmlspecialchars($data['comment']));
79echo '<tr><td colspan=2><input type=submit name="save" value="'.$GLOBALS['I18N']->get('Save Changes').'"></td></tr>';
80echo '</table></form>';
81
82$req = Sql_Query(sprintf('select * from %s where regex = %d', $GLOBALS['tables']['bounceregex_bounce'], $_GET['id']));
83$num = Sql_affected_Rows();
84if ($num) {
85    echo '<p>'.$GLOBALS['I18N']->get('related bounces').'</p><p>';
86} else {
87    echo '<p>'.$GLOBALS['I18N']->get('no related bounces found').'</p>';
88}
89$c = 0;
90while ($row = Sql_Fetch_Array($req)) {
91    echo PageLink2('bounce&id='.$row['bounce'], $row['bounce']).' ';
92    ++$c;
93    if ($c > 100) {
94        break;
95    }
96}
97if ($c < $num) {
98    printf(' '.$GLOBALS['I18N']->get('and more, %d in total'), $num);
99}
100