xref: /plugin/publish/helper.php (revision 7b3337ec7ebac2374133415202c5b1af1562161d)
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    /**
18     * checks if an id is within one of the namespaces in $namespace_list
19     *
20     * @param string $namespace_list
21     * @param string $id
22     *
23     * @return bool
24     */
25    function in_namespace($namespace_list, $id) {
26        // PHP apparantly does not have closures -
27        // so we will parse $valid ourselves. Wasteful.
28        $namespace_list = preg_split('/\s+/', $namespace_list);
29        //if(count($valid) == 0) { return true; }//whole wiki matches
30        if((count($namespace_list)==1) and ($namespace_list[0]=="")) { return true; }//whole wiki matches
31        $id = trim($id, ':');
32        $id = explode(':', $id);
33
34        // Check against all possible namespaces
35        foreach($namespace_list as $namespace) {
36            $namespace = explode(':', $namespace);
37            $current_ns_depth = 0;
38            $total_ns_depth = count($namespace);
39            $matching = true;
40
41            // Check each element, untill all elements of $v satisfied
42            while($current_ns_depth < $total_ns_depth) {
43                if($namespace[$current_ns_depth] != $id[$current_ns_depth]) {
44                    // not a match
45                    $matching = false;
46                    break;
47                }
48                $current_ns_depth += 1;
49            }
50            if($matching) { return true; } // a match
51        }
52        return false;
53    }
54
55    /**
56     * check if given $dir contains a valid namespace or is contained in a valid namespace
57     *
58     * @param $valid_namespaces_list
59     * @param $dir
60     *
61     * @return bool
62     */
63    function is_dir_valid($valid_namespaces_list, $dir) {
64        $valid_namespaces_list = preg_split('/\s+/', $valid_namespaces_list);
65        //if(count($valid) == 0) { return true; }//whole wiki matches
66        if((count($valid_namespaces_list)==1) && ($valid_namespaces_list[0]=="")) { return true; }//whole wiki matches
67        $dir = trim($dir, ':');
68        $dir = explode(':', $dir);
69
70        // Check against all possible namespaces
71        foreach($valid_namespaces_list as $valid_namespace) {
72            $valid_namespace = explode(':', $valid_namespace);
73            $current_depth = 0;
74            $dir_depth = count($dir); //this is what is different from above!
75            $matching = true;
76
77            // Check each element, untill all elements of $v satisfied
78            while($current_depth < $dir_depth) {
79                if (empty($valid_namespace[$current_depth])) {
80                    break;
81                }
82                if($valid_namespace[$current_depth] != $dir[$current_depth]) {
83                    // not a match
84                    $matching = false;
85                    break;
86                }
87                $current_depth += 1;
88            }
89            if($matching) { return true; } // a match
90        }
91        return false;
92    }
93
94    function canApprove() {
95        global $INFO;
96        global $ID;
97
98        if (!$this->in_namespace($this->getConf('apr_namespaces'), $ID)) {
99            return false;
100        }
101
102        return ($INFO['perm'] >= AUTH_DELETE);
103    }
104
105    function getRevision($id = null) {
106        global $REV;
107        if (isset($REV) && !empty($REV)) {
108            return $REV;
109        }
110        $meta = $this->getMeta($id);
111        if($meta){
112            if (isset($meta['last_change']['date'])) {
113                return $meta['last_change']['date'];
114            }
115            if (isset($meta['date']['modified'])) {
116                return $meta['date']['modified'];
117            }
118        }
119        return 0;
120    }
121
122    function getApprovals($id = null) {
123        $meta = $this->getMeta($id);
124        if (!isset($meta['approval'])) {
125            return array();
126        }
127        $approvals = $meta['approval'];
128        if (!is_array($approvals)) {
129            return array();
130        }
131        return $approvals;
132    }
133
134    function getMeta($id = null) {
135        global $ID;
136        global $INFO;
137
138        if ($id === null) $id = $ID;
139
140        if($ID === $id && $INFO['meta']) {
141            $meta = $INFO['meta'];
142        } else {
143            $meta = p_get_metadata($id);
144        }
145
146        $this->checkApprovalFormat($meta, $id);
147
148        return $meta;
149    }
150
151    function checkApprovalFormat($meta, $id) {
152        if (isset($meta['approval_version']) && $meta['approval_version'] >= 2) {
153            return;
154        }
155
156        if (!$this->hasApprovals($meta)) {
157            return;
158        }
159
160        $approvals = $meta['approval'];
161        foreach (array_keys($approvals) as $approvedId) {
162            $keys = array_keys($approvals[$approvedId]);
163
164            if (is_array($approvals[$approvedId][$keys[0]])) {
165                continue; // current format
166            }
167
168            $newEntry = $approvals[$approvedId];
169            if (count($newEntry) !== 3) {
170                //continue; // some messed up format...
171            }
172            $newEntry[] = intval($approvedId); // revision is the time of page edit
173
174            $approvals[$approvedId] = array();
175            $approvals[$approvedId][$newEntry[0]] = $newEntry;
176        }
177        p_set_metadata($id, array('approval' => $approvals), true, true);
178        p_set_metadata($id, array('approval_version' => 2), true, true);
179    }
180
181    function hasApprovals($meta) {
182        return isset($meta['approval']) && !empty($meta['approval']);
183    }
184
185    function getApprovalsOnRevision($revision) {
186        $approvals = $this->getApprovals();
187
188        if (isset($approvals[$revision])) {
189            return $approvals[$revision];
190        }
191        return array();
192    }
193
194    function getSortedApprovedRevisions($id = null) {
195        if ($id === null) {
196            global $ID;
197            $id = $ID;
198        }
199
200        static $sortedApprovedRevisions = array();
201        if (!isset($sortedApprovedRevisions[$id])) {
202            $approvals = $this->getApprovals($id);
203            krsort($approvals);
204            $sortedApprovedRevisions[$id] = $approvals;
205        }
206
207        return $sortedApprovedRevisions[$id];
208    }
209
210    function isRevisionApproved($revision, $id = null) {
211        $approvals = $this->getApprovals($id);
212        if (!isset($approvals[$revision])) {
213            return false;
214        }
215        return (count($approvals[$revision]) >= $this->getConf('number_of_approved'));
216    }
217
218    function isCurrentRevisionApproved($id = null) {
219        return $this->isRevisionApproved($this->getRevision($id), $id);
220    }
221
222    function getLatestApprovedRevision($id = null) {
223        $approvals = $this->getSortedApprovedRevisions($id);
224        foreach ($approvals as $revision => $ignored) {
225            if ($this->isRevisionApproved($revision, $id)) {
226                return $revision;
227            }
228        }
229        return 0;
230    }
231
232    function getLastestRevision() {
233        global $INFO;
234        return $INFO['meta']['date']['modified'];
235    }
236
237    function getApprovalDate() {
238        if (!$this->isCurrentRevisionApproved()) {
239            return -1;
240        }
241
242        $approvals = $this->getApprovalsOnRevision($this->getRevision());
243        uasort($approvals, array(&$this, 'cmpApprovals'));
244        $keys = array_keys($approvals);
245        return $approvals[$keys[$this->getConf('number_of_approved') -1]][3];
246
247    }
248
249    function cmpApprovals($left, $right) {
250        if ($left[3] == $right[3]) {
251            return 0;
252        }
253        return ($left[3] < $right[3]) ? -1 : 1;
254    }
255
256    function getApprovers() {
257        $approvers = $this->getApprovalsOnRevision($this->getRevision());
258        if (count($approvers) === 0) {
259            return;
260        }
261
262        $result = array();
263        foreach ($approvers as $approver) {
264            $result[] = editorinfo($this->getApproverName($approver));
265        }
266        return $result;
267    }
268
269    function getApproverName($approver) {
270        if ($approver[1]) {
271            return $approver[1];
272        }
273        if ($approver[2]) {
274            return $approver[2];
275        }
276        return $approver[0];
277    }
278
279    function getPreviousApprovedRevision() {
280        $currentRevision = $this->getRevision();
281        $approvals = $this->getSortedApprovedRevisions();
282        foreach ($approvals as $revision => $ignored) {
283            if ($revision >= $currentRevision) {
284                continue;
285            }
286            if ($this->isRevisionApproved($revision)) {
287                return $revision;
288            }
289        }
290        return 0;
291    }
292
293    function isHidden($id = null) {
294        if (!$this->getConf('hide drafts')) {
295            return false;
296        }
297
298        // needs to check if the actual namespace belongs to the apr_namespaces
299        if ($id == null) {
300            global $ID;
301            $id = $ID;
302        }
303        if (!$this->isActive($id)) {
304            return false;
305        }
306
307        if ($this->getLatestApprovedRevision($id)) {
308            return false;
309        }
310        return true;
311    }
312
313    function isHiddenForUser($id = null) {
314        if (!$this->isHidden($id)) {
315            return false;
316        }
317
318        if ($id == null) {
319            global $ID;
320            $id = $ID;
321        }
322
323        $allowedGroups = array_filter(explode(' ', trim($this->getConf('author groups'))));
324        if (empty($allowedGroups)) {
325            return auth_quickaclcheck($id) < AUTH_EDIT;
326        }
327
328        if (!isset($_SERVER['REMOTE_USER'])) {
329            return true;
330        }
331
332        global $USERINFO;
333        foreach ($allowedGroups as $allowedGroup) {
334            $allowedGroup = trim($allowedGroup);
335            if (in_array($allowedGroup, $USERINFO['grps'])) {
336                return false;
337            }
338        }
339        return true;
340    }
341
342    function isActive($id = null) {
343        if ($id == null) {
344            global $ID;
345            $id = $ID;
346        }
347        if (!$this->in_namespace($this->getConf('apr_namespaces'), $id)) {
348            return false;
349        }
350
351        $no_apr_namespaces = $this->getConf('no_apr_namespaces');
352        if (!empty($no_apr_namespaces)) {
353            if ($this->in_namespace($no_apr_namespaces, $id)) {
354                return false;
355            }
356        }
357        return true;
358    }
359
360    /**
361     * Create absolute diff-link between the two given revisions
362     *
363     * @param string $id
364     * @param int $rev1
365     * @param int $rev2
366     * @return string Diff-Link or empty string if $rev1 == $rev2
367     */
368    public function getDifflink($id, $rev1, $rev2) {
369        if($rev1 == $rev2) {
370            return '';
371        }
372        $params = 'do=diff,rev2[0]=' . $rev1 . ',rev2[1]=' . $rev2 . ',difftype=sidebyside';
373        $difflink = wl($id, $params,true,'&');
374        return $difflink;
375    }
376
377    function getPagesFromNamespace($namespace) {
378        global $conf;
379        $dir = $conf['datadir'] . '/' . str_replace(':', '/', $namespace);
380        $pages = array();
381        search($pages, $dir, array($this,'_search_helper'), array($namespace, $this->getConf('apr_namespaces'),
382                                                                  $this->getConf('no_apr_namespaces')));
383        return $pages;
384    }
385
386    /**
387     * search callback function
388     *
389     * filter out pages which can't be approved by the current user
390     * then check if they need approving
391     */
392    function _search_helper(&$data, $base, $file, $type, $lvl, $opts) {
393        $ns = $opts[0];
394        $valid_ns = $opts[1];
395        $invalid_ns = $opts[2];
396
397        if ($type == 'd') {
398            return $this->is_dir_valid($valid_ns, $ns . ':' . str_replace('/', ':', $file));
399        }
400
401        if (!preg_match('#\.txt$#', $file)) {
402            return false;
403        }
404
405        $id = pathID($ns . $file);
406        if (!empty($valid_ns) && !$this->in_namespace($valid_ns, $id)) {
407            return false;
408        }
409
410        if (!empty($invalid_ns) && $this->in_namespace($invalid_ns, $id)) {
411            return false;
412        }
413
414        if (auth_quickaclcheck($id) < AUTH_DELETE) {
415            return false;
416        }
417
418        $meta = $this->getMeta($id);
419        if ($this->isCurrentRevisionApproved($id)) {
420
421            // Already approved
422            return false;
423        }
424
425        $data[] = array($id, $meta['approval'], $meta['last_change']['date']);
426        return false;
427    }
428
429    public function removeSubnamespacePages ($pages, $namespace) {
430        $cleanpages = array();
431        foreach ($pages as $page) {
432            if (getNS($page[0]) == $namespace) {
433                $cleanpages[] = $page;
434            }
435        }
436        return $cleanpages;
437    }
438
439}
440