xref: /plugin/publish/helper.php (revision 4e55e2a150e6f7573f375cf59f24812ce94643ec)
1<?php
2/**
3 * DokuWiki Plugin publish (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Jarrod Lowe <dokuwiki@rrod.net>
7 * @author  Andreas Gohr <gohr@cosmocode.de>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13class helper_plugin_publish extends DokuWiki_Plugin {
14
15    private $sortedApprovedRevisions = null;
16
17    // FIXME find out what this is supposed to do and how it can be done better
18    function in_namespace($valid, $check) {
19        // PHP apparantly does not have closures -
20        // so we will parse $valid ourselves. Wasteful.
21        $valid = preg_split('/\s+/', $valid);
22        //if(count($valid) == 0) { return true; }//whole wiki matches
23        if((count($valid)==1) and ($valid[0]=="")) { return true; }//whole wiki matches
24        $check = trim($check, ':');
25        $check = explode(':', $check);
26
27        // Check against all possible namespaces
28        foreach($valid as $v) {
29            $v = explode(':', $v);
30            $n = 0;
31            $c = count($v);
32            $matching = 1;
33
34            // Check each element, untill all elements of $v satisfied
35            while($n < $c) {
36                if($v[$n] != $check[$n]) {
37                    // not a match
38                    $matching = 0;
39                    break;
40                }
41                $n += 1;
42            }
43            if($matching == 1) { return true; } // a match
44        }
45        return false;
46    }
47
48    // FIXME find out what this is supposed to do and how it can be done better
49    function in_sub_namespace($valid, $check) {
50        // is check a dir which contains any valid?
51        // PHP apparantly does not have closures -
52        // so we will parse $valid ourselves. Wasteful.
53        $valid = preg_split('/\s+/', $valid);
54        //if(count($valid) == 0) { return true; }//whole wiki matches
55        if((count($valid)==1) and ($valid[0]=="")) { return true; }//whole wiki matches
56        $check = trim($check, ':');
57        $check = explode(':', $check);
58
59        // Check against all possible namespaces
60        foreach($valid as $v) {
61            $v = explode(':', $v);
62            $n = 0;
63            $c = count($check); //this is what is different from above!
64            $matching = 1;
65
66            // Check each element, untill all elements of $v satisfied
67            while($n < $c) {
68                if($v[$n] != $check[$n]) {
69                    // not a match
70                    $matching = 0;
71                    break;
72                }
73                $n += 1;
74            }
75            if($matching == 1) { return true; } // a match
76        }
77        return false;
78    }
79
80    function canApprove() {
81        global $INFO;
82        global $ID;
83
84        if (!$this->in_namespace($this->getConf('apr_namespaces'), $ID)) {
85            return false;
86        }
87
88        return ($INFO['perm'] >= AUTH_DELETE);
89    }
90
91    function getRevision($id = null) {
92        global $REV;
93        if (isset($REV) && !empty($REV)) {
94            return $REV;
95        }
96        $meta = $this->getMeta($id);
97        if (isset($meta['last_change']['date'])) {
98            return $meta['last_change']['date'];
99        }
100        return $meta['date']['modified'];
101    }
102
103    function getApprovals($id = null) {
104        $meta = $this->getMeta($id);
105        if (!isset($meta['approval'])) {
106            return array();
107        }
108        $approvals = $meta['approval'];
109        if (!is_array($approvals)) {
110            return array();
111        }
112        return $approvals;
113    }
114
115    function getMeta($id = null) {
116        global $ID;
117        if ($id === null || $ID === $id) {
118            global $INFO;
119            $meta = $INFO['meta'];
120            $id = $ID;
121        } else {
122            $meta = p_get_metadata($id);
123        }
124
125        $this->checkApprovalFormat($meta, $id);
126
127        return $meta;
128    }
129
130    function checkApprovalFormat($meta, $id) {
131        if (isset($meta['approval_version']) && $meta['approval_version'] >= 2) {
132            return;
133        }
134
135        if (!$this->hasApprovals($meta)) {
136            return;
137        }
138
139        $approvals = $meta['approval'];
140        foreach (array_keys($approvals) as $approvedId) {
141            $keys = array_keys($approvals[$approvedId]);
142
143            if (is_array($approvals[$approvedId][$keys[0]])) {
144                continue; // current format
145            }
146
147            $newEntry = $approvals[$approvedId];
148            if (count($newEntry) !== 3) {
149                //continue; // some messed up format...
150            }
151            $newEntry[] = intval($approvedId); // revision is the time of page edit
152
153            $approvals[$approvedId] = array();
154            $approvals[$approvedId][$newEntry[0]] = $newEntry;
155        }
156        p_set_metadata($id, array('approval' => $approvals), true, true);
157        p_set_metadata($id, array('approval_version' => 2), true, true);
158    }
159
160    function hasApprovals($meta) {
161        return isset($meta['approval']) && !empty($meta['approval']);
162    }
163
164    function getApprovalsOnRevision($revision) {
165        $approvals = $this->getApprovals();
166
167        if (isset($approvals[$revision])) {
168            return $approvals[$revision];
169        }
170        return array();
171    }
172
173    function getSortedApprovedRevisions() {
174        if ($this->sortedApprovedRevisions === null) {
175            $approvals = $this->getApprovals();
176            krsort($approvals);
177            $this->sortedApprovedRevisions = $approvals;
178        }
179        return $this->sortedApprovedRevisions;
180    }
181
182    function isRevisionApproved($revision, $id = null) {
183        $approvals = $this->getApprovals($id);
184        if (!isset($approvals[$revision])) {
185            return false;
186        }
187        return (count($approvals[$revision]) >= $this->getConf('number_of_approved'));
188    }
189
190    function isCurrentRevisionApproved($id = null) {
191        return $this->isRevisionApproved($this->getRevision($id), $id);
192    }
193
194    function getLatestApprovedRevision() {
195        $approvals = $this->getSortedApprovedRevisions();
196        foreach ($approvals as $revision => $ignored) {
197            if ($this->isRevisionApproved($revision)) {
198                return $revision;
199            }
200        }
201        return 0;
202    }
203
204    function getLastestRevision() {
205        global $INFO;
206        return $INFO['meta']['date']['modified'];
207    }
208
209    function getApprovalDate() {
210        if (!$this->isCurrentRevisionApproved()) {
211            return -1;
212        }
213
214        $approvals = $this->getApprovalsOnRevision($this->getRevision());
215        uasort($approvals, array(&$this, 'cmpApprovals'));
216        $keys = array_keys($approvals);
217        return $approvals[$keys[$this->getConf('number_of_approved') -1]][3];
218
219    }
220
221    function cmpApprovals($left, $right) {
222        if ($left[3] == $right[3]) {
223            return 0;
224        }
225        return ($left[3] < $right[3]) ? -1 : 1;
226    }
227
228    function getApprovers() {
229        $approvers = $this->getApprovalsOnRevision($this->getRevision());
230        if (count($approvers) === 0) {
231            return;
232        }
233
234        $result = array();
235        foreach ($approvers as $approver) {
236            $result[] = editorinfo($this->getApproverName($approver));
237        }
238        return $result;
239    }
240
241    function getApproverName($approver) {
242        if ($approver[1]) {
243            return $approver[1];
244        }
245        if ($approver[2]) {
246            return $approver[2];
247        }
248        return $approver[0];
249    }
250
251    function getPreviousApprovedRevision() {
252        $currentRevision = $this->getRevision();
253        $approvals = $this->getSortedApprovedRevisions();
254        foreach ($approvals as $revision => $ignored) {
255            if ($revision >= $currentRevision) {
256                continue;
257            }
258            if ($this->isRevisionApproved($revision)) {
259                return $revision;
260            }
261        }
262        return 0;
263    }
264
265    function isHidden() {
266        global $ID;
267
268        if (!$this->getConf('hide drafts')) {
269            return false;
270        }
271
272        if ($this->getLatestApprovedRevision()) {
273            return false;
274        }
275        return true;
276    }
277
278    function isHiddenForUser() {
279        if (!$this->isHidden()) {
280            return false;
281        }
282
283        global $ID;
284        $allowedGroups = array_filter(explode(' ', trim($this->getConf('author groups'))));
285        if (empty($allowedGroups)) {
286            return auth_quickaclcheck($ID) < AUTH_EDIT;
287        }
288
289        if (!$_SERVER['REMOTE_USER']) {
290            return true;
291        }
292
293        global $USERINFO;
294        foreach ($allowedGroups as $allowedGroup) {
295            $allowedGroup = trim($allowedGroup);
296            if (in_array($allowedGroup, $USERINFO['grps'])) {
297                return false;
298            }
299        }
300        return true;
301    }
302}
303