xref: /web-bugs/www/rpc.php (revision 381dea42)
1<?php
2
3use App\Repository\BugRepository;
4
5/**
6 * This API page is used by https://svn.php.net/viewvc/SVNROOT/commit-bugs.php
7 * to manage bugs automatically.
8 */
9
10session_start();
11
12$bug_id = (isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0);
13
14if (!$bug_id) {
15    echo json_encode(['result' => ['error' => 'Missing bug id']]);
16    exit;
17}
18
19// Obtain common includes
20require_once '../include/prepend.php';
21
22if (!isset($_POST['MAGIC_COOKIE'])) {
23    echo json_encode(['result' => ['error' => 'Missing token']]);
24    exit;
25}
26
27if (sha1($_POST['MAGIC_COOKIE']) !== '8514f801cfba2ec74ec08264567ba291485f2765') {
28    echo json_encode(['result' => ['error' => 'Invalid token']]);
29    exit;
30}
31
32// fetch info about the bug into $bug
33$bugRepository = $container->get(BugRepository::class);
34$bug = $bugRepository->findOneById($bug_id);
35
36if (!is_array($bug)) {
37    echo json_encode(['result' => ['error' => 'No such bug']]);
38    exit;
39}
40
41// Be conservative: Do not allow access to private bugs.
42if ($bug['private'] === 'Y') {
43    echo json_encode(['result' => ['error' => 'No access to bug']]);
44    exit;
45}
46
47if (!empty($_POST['ncomment']) && !empty($_POST['user'])) {
48    $user = htmlspecialchars(trim($_POST['user']));
49    $ncomment = htmlspecialchars(trim($_POST['ncomment']));
50    $from = "{$user}@php.net";
51
52    try {
53        /* svn log comment */
54        bugs_add_comment($bug_id, $from, $user, $ncomment, 'svn');
55
56        /* Close the bug report as requested if it is not already closed */
57        if (!empty($_POST['status'])
58            && $bug['status'] !== 'Closed'
59            && $_POST['status'] === 'Closed') {
60            /* Change the bug status to Closed */
61            bugs_status_change($bug_id, 'Closed');
62
63            $in = $bug;
64            /* Just change the bug status */
65            $in['status'] = $_POST['status'];
66
67            $changed = bug_diff($bug, $in);
68            if (!empty($changed)) {
69                $log_comment = bug_diff_render_html($changed);
70                if (!empty($log_comment)) {
71                    /* Add a log of status change */
72                    bugs_add_comment($bug_id, $from, '', $log_comment, 'log');
73                }
74            }
75
76            /* Send a mail notification when automatically closing a bug */
77            mail_bug_updates($bug, $in, $from, $ncomment, 1, $bug_id);
78        }
79
80        echo json_encode(['result' => ['status' => $bug]]);
81        exit;
82    } catch (Exception $e) {
83        echo json_encode(['result' => ['error' => $e->getMessage()]]);
84        exit;
85    }
86} else if (!empty($_POST['getbug'])) {
87    echo json_encode(['result' => ['status' => $bug]]);
88    exit;
89}
90
91echo json_encode(['result' => ['error' => 'Nothing to do']]);
92