xref: /plugin/approve/remote.php (revision 8a0fc108dcf4de0b5b5dff4a03a43b4a72cfba6f)
1*8a0fc108SAndreas Gohr<?php
2*8a0fc108SAndreas Gohr
3*8a0fc108SAndreas Gohruse dokuwiki\Extension\RemotePlugin;
4*8a0fc108SAndreas Gohruse dokuwiki\plugin\approve\PageRemoteResponse;
5*8a0fc108SAndreas Gohruse dokuwiki\Remote\RemoteException;
6*8a0fc108SAndreas Gohr
7*8a0fc108SAndreas Gohr/**
8*8a0fc108SAndreas Gohr * DokuWiki Plugin approve (Action Component)
9*8a0fc108SAndreas Gohr *
10*8a0fc108SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11*8a0fc108SAndreas Gohr * @author Andreas Gohr <dokuwiki@cosmocode.de>
12*8a0fc108SAndreas Gohr */
13*8a0fc108SAndreas Gohrclass remote_plugin_approve extends RemotePlugin
14*8a0fc108SAndreas Gohr{
15*8a0fc108SAndreas Gohr
16*8a0fc108SAndreas Gohr    /**
17*8a0fc108SAndreas Gohr     * Get wiki pages and their approval status
18*8a0fc108SAndreas Gohr     *
19*8a0fc108SAndreas Gohr     * Return all pages that are under control of the approval plugin, optionally filtered.
20*8a0fc108SAndreas Gohr     *
21*8a0fc108SAndreas Gohr     * @param string[] $states Return only pages matchin this state [approved, draft, ready_for_approval]
22*8a0fc108SAndreas Gohr     * @param string $namespace Only return pages within this namespace, empty for all
23*8a0fc108SAndreas Gohr     * @param string $filter Only return pages matching this regex, empty for all
24*8a0fc108SAndreas Gohr     * @param string $approver Only return pages to be approved by this user or group, empty for all
25*8a0fc108SAndreas Gohr     * @return PageRemoteResponse[]
26*8a0fc108SAndreas Gohr     * @throws RemoteException
27*8a0fc108SAndreas Gohr     */
28*8a0fc108SAndreas Gohr    public function getPages(
29*8a0fc108SAndreas Gohr        $states = ['approved', 'draft', 'ready_for_approval'], $namespace = '', $filter = '', $approver = ''
30*8a0fc108SAndreas Gohr    )
31*8a0fc108SAndreas Gohr    {
32*8a0fc108SAndreas Gohr        global $auth;
33*8a0fc108SAndreas Gohr
34*8a0fc108SAndreas Gohr        if (array_diff($states, ['approved', 'draft', 'ready_for_approval'])) {
35*8a0fc108SAndreas Gohr            throw new RemoteException('Invalid state(s) provided', 122);
36*8a0fc108SAndreas Gohr        }
37*8a0fc108SAndreas Gohr
38*8a0fc108SAndreas Gohr        $namespace = cleanID($namespace);
39*8a0fc108SAndreas Gohr
40*8a0fc108SAndreas Gohr        if (@preg_match('/' . $filter . '/', null) === false) {
41*8a0fc108SAndreas Gohr            throw new RemoteException('Invalid filter regex', 123);
42*8a0fc108SAndreas Gohr        }
43*8a0fc108SAndreas Gohr
44*8a0fc108SAndreas Gohr        $approver = $auth->cleanUser($approver);
45*8a0fc108SAndreas Gohr
46*8a0fc108SAndreas Gohr        /** @var helper_plugin_approve_db $db */
47*8a0fc108SAndreas Gohr        $db = plugin_load('helper', 'approve_db');
48*8a0fc108SAndreas Gohr        $pages = $db->getPages($approver, $states, $namespace, $filter);
49*8a0fc108SAndreas Gohr
50*8a0fc108SAndreas Gohr        return array_map(function ($data) {
51*8a0fc108SAndreas Gohr            return new PageRemoteResponse($data);
52*8a0fc108SAndreas Gohr        }, $pages);
53*8a0fc108SAndreas Gohr    }
54*8a0fc108SAndreas Gohr
55*8a0fc108SAndreas Gohr    /**
56*8a0fc108SAndreas Gohr     * Set the approval status of a page
57*8a0fc108SAndreas Gohr     *
58*8a0fc108SAndreas Gohr     * Mark a given page as approved or ready for approval
59*8a0fc108SAndreas Gohr     *
60*8a0fc108SAndreas Gohr     * @param string $page The page id
61*8a0fc108SAndreas Gohr     * @param string $status The new status [approved, ready_for_approval]
62*8a0fc108SAndreas Gohr     * @return true
63*8a0fc108SAndreas Gohr     * @throws RemoteException
64*8a0fc108SAndreas Gohr     */
65*8a0fc108SAndreas Gohr    public function setStatus($page, $status)
66*8a0fc108SAndreas Gohr    {
67*8a0fc108SAndreas Gohr        /** @var helper_plugin_approve_acl $acl */
68*8a0fc108SAndreas Gohr        $acl = plugin_load('helper', 'approve_acl');
69*8a0fc108SAndreas Gohr
70*8a0fc108SAndreas Gohr        /** @var helper_plugin_approve_db $db */
71*8a0fc108SAndreas Gohr        $db = plugin_load('helper', 'approve_db');
72*8a0fc108SAndreas Gohr
73*8a0fc108SAndreas Gohr        if (!page_exists($page)) {
74*8a0fc108SAndreas Gohr            throw new RemoteException('Page does not exist', 121);
75*8a0fc108SAndreas Gohr        }
76*8a0fc108SAndreas Gohr
77*8a0fc108SAndreas Gohr        if (!$acl->useApproveHere($page)) {
78*8a0fc108SAndreas Gohr            throw new RemoteException('This page is not under control of the approve plugin', 124);
79*8a0fc108SAndreas Gohr        }
80*8a0fc108SAndreas Gohr
81*8a0fc108SAndreas Gohr        global $INFO;
82*8a0fc108SAndreas Gohr        global $USERINFO;
83*8a0fc108SAndreas Gohr        $INFO['userinfo'] = $USERINFO;
84*8a0fc108SAndreas Gohr
85*8a0fc108SAndreas Gohr        if ($status == 'approved') {
86*8a0fc108SAndreas Gohr            if (!$acl->clientCanApprove($page)) {
87*8a0fc108SAndreas Gohr                throw new RemoteException('You are not allowed to approve this page', 111);
88*8a0fc108SAndreas Gohr            }
89*8a0fc108SAndreas Gohr            $db->setApprovedStatus($page);
90*8a0fc108SAndreas Gohr        } elseif ($status == 'ready_for_approval') {
91*8a0fc108SAndreas Gohr            if (!$acl->clientCanMarkReadyForApproval($page)) {
92*8a0fc108SAndreas Gohr                throw new RemoteException('You are not allowed to mark this page as ready for approval', 111);
93*8a0fc108SAndreas Gohr            }
94*8a0fc108SAndreas Gohr            $db->setReadyForApprovalStatus($page);
95*8a0fc108SAndreas Gohr        } else {
96*8a0fc108SAndreas Gohr            throw new RemoteException('Invalid status', 122);
97*8a0fc108SAndreas Gohr
98*8a0fc108SAndreas Gohr        }
99*8a0fc108SAndreas Gohr
100*8a0fc108SAndreas Gohr        return true;
101*8a0fc108SAndreas Gohr    }
102*8a0fc108SAndreas Gohr}
103