xref: /web-bugs/www/fix.php (revision 8794eabf)
1<?php
2
3use App\Repository\BugRepository;
4use App\Repository\ReasonRepository;
5
6session_start();
7
8/* Admin interface for closing bug reports via direct link */
9
10// Obtain common includes
11require_once '../include/prepend.php';
12
13$bug_id = (int) $_REQUEST['id'];
14
15if (!$bug_id) {
16    redirect('index.php');
17}
18
19// Authenticate
20bugs_authenticate($user, $pw, $logged_in, $user_flags);
21
22// fetch info about the bug into $bug
23$bugRepository = $container->get(BugRepository::class);
24$bug = $bugRepository->findOneById($bug_id);
25
26if (!is_array($bug)) {
27    response_header('No Such Bug');
28    display_bug_error("No such bug #{$bug_id}");
29    response_footer();
30    exit;
31}
32
33// If bug exists, continue..
34$RESOLVE_REASONS = $FIX_VARIATIONS = $errors = [];
35
36if ($logged_in != 'developer') {
37    $errors[] = 'The username or password you supplied was incorrect.';
38}
39
40$project = !empty($_GET['project']) ? $_GET['project'] : false;
41
42$reasonRepository = $container->get(ReasonRepository::class);
43list($RESOLVE_REASONS, $FIX_VARIATIONS) = $reasonRepository->findByProject($site);
44
45// Handle reason / comments
46$reason = isset($_REQUEST['r']) ? filter_var($_REQUEST['r'], FILTER_SANITIZE_STRING) : '';
47$ncomment = isset($_POST['ncomment']) ? trim($_POST['ncomment']) : '';
48
49if (!$reason || !isset($RESOLVE_REASONS[$reason])) {
50    $errors[] = 'You have to use a valid reason to resolve this bug.';
51}
52
53if (isset($RESOLVE_REASONS[$reason]) && $RESOLVE_REASONS[$reason]['status'] == 'Not a bug' && $ncomment == '') {
54    $errors[] = 'You must provide a comment when marking a bug \'Not a bug\'';
55}
56
57// Handle errors
58if ($errors) {
59    response_header('Error in resolving bug');
60    display_bug_error($errors);
61?>
62
63<form method="post" action="fix.php">
64    <input type="hidden" name="id" value="<?php echo $bug_id; ?>">
65
66<?php // Note: same block is used also in bug.php!
67if ($logged_in == 'developer') {
68?>
69    <div class="explain">
70        Welcome back, <?php echo $user; ?>! (Not <?php echo $user; ?>?
71        <a href="logout.php">Log out.</a>)
72    </div>
73<?php } else { ?>
74    <div class="explain">
75        Welcome! If you don't have a Git account, you can't do anything here.<br>
76        If you reported this bug, you can <a href="bug.php?id=<?php echo $bug_id; ?>&amp;edit=2">edit this bug over here</a>.
77        <div class="details">
78            <label for="svnuser">php.net Username:</label>
79            <input type="text" id="svnuser" name="user" value="<?php echo htmlspecialchars($user) ?>" size="10" maxlength="20">
80            <label for="svnpw">php.net Password:</label>
81            <input type="password" id="svnpw" name="pw" value="<?php echo htmlspecialchars($pw) ?>" size="10">
82            <label for="save">Remember:</label><input style="vertical-align:middle;" type="checkbox" id="save" name="save" <?php echo !empty($_POST['save']) ? 'checked="checked"' : ''; ?>>
83        </div>
84    </div>
85<?php } ?>
86    <table>
87        <tr>
88            <th><a href="quick-fix-desc.php">Reason:</a></th>
89            <td colspan="5">
90                <select name="r">
91                    <?php echo show_reason_types($reason); ?>
92                </select>
93            </td>
94        </tr>
95        <tr>
96            <th>Note:</th>
97            <td colspan="5"><textarea cols="80" rows="8" name="ncomment" wrap="physical"><?php echo htmlspecialchars($ncomment); ?></textarea></td>
98        </tr>
99    </table>
100    <input type="submit" value="Resolve">
101</form>
102<?php
103    response_footer();
104    exit;
105}
106
107// Update bug
108$status = $RESOLVE_REASONS[$reason]['status'];
109if (isset($FIX_VARIATIONS[$reason][$bug['bug_type']])) {
110    $qftext = $FIX_VARIATIONS[$reason][$bug['bug_type']];
111} else {
112    $qftext = $RESOLVE_REASONS[$reason]['message'];
113}
114$ncomment = $qftext . (!empty($ncomment) ? "\n\n".$ncomment : "");
115
116// If the report already has the status of the resolution, bounce over to the main bug form
117// which shows the appropriate error message.
118if ($status == $bug['status']) {
119    redirect("bug.php?id={$bug_id}&edit=1&in[resolve]={$reason}");
120}
121
122// Standard items
123$in = [
124    'status' => $status,
125    'bug_type' => $bug['bug_type'],
126    'php_version' => $bug['php_version'],
127    'php_os' => $bug['php_os'],
128    'assign' => $bug['assign'],
129];
130
131// Assign automatically when closed
132if ($status == 'Closed' && $in['assign'] == '') {
133    $in['assign'] = $auth_user->handle;
134}
135
136try {
137    // Update bug
138    $dbh->prepare("
139        UPDATE bugdb
140        SET
141            status = ?,
142            assign = ?,
143            ts2 = NOW()
144        WHERE id = ?
145    ")->execute([
146        $status,
147        $in['assign'],
148        $bug_id,
149    ]);
150
151    // Add changelog entry
152    $changed = bug_diff($bug, $in);
153    if (!empty($changed)) {
154        $log_comment = bug_diff_render_html($changed);
155        if (!empty($log_comment)) {
156            $result = bugs_add_comment($bug_id, $auth_user->email, $auth_user->name, $log_comment, 'log');
157        }
158    }
159
160    // Add possible comment
161    if (!empty($ncomment)) {
162        $result = bugs_add_comment($bug_id, $auth_user->email, $auth_user->name, $ncomment, 'comment');
163    }
164
165    // Send emails
166    mail_bug_updates($bug, $in, $auth_user->email, $ncomment);
167    redirect("bug.php?id={$bug_id}&thanks=1");
168} catch (\Exception $e) {
169    // If we end up here, something went wrong.
170    response_header('Resolve Bug: Problem');
171    display_bug_error($e->getMessage());
172    response_footer();
173}
174