1<?php
2/**
3 * DokuWiki Plugin struct (Helper Component)
4 */
5
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die();
8
9class helper_plugin_structgroup_authgroup extends DokuWiki_Plugin {
10    protected $groupsloaded = false;
11    protected $groups = array();
12
13    protected function loadGroupsFromAcl() {
14        global $AUTH_ACL;
15        if (!isset($AUTH_ACL) || !is_array($AUTH_ACL)) return array();
16
17        $specials = array('ALL');
18        $groups = [];
19        foreach($AUTH_ACL as $line){
20            $line = trim(preg_replace('/#.*$/','',$line)); //ignore comments
21            if(!$line) continue;
22
23            $acl = preg_split('/[ \t]+/',$line);
24            //0 is pagename, 1 is user, 2 is acl
25
26            $grp = rawurldecode($acl[1]);
27            //it's not a group
28            if ($grp[0] != '@') continue;
29            $grp = substr($grp, 1);
30            if(in_array($grp,$specials)) continue;
31            $groups[] = $grp;
32        }
33
34        return array_unique($groups);
35    }
36
37    protected function loadGroups() {
38        /** @var \DokuWiki_Auth_Plugin $auth */
39        global $auth;
40
41        if(!$auth->canDo('getUsers')) {
42            msg('The user backend can not search for users', -1);
43            return false;
44        }
45
46        $groups = array_map(function($userinfo) { return $userinfo['grps']; }, $auth->retrieveUsers());
47        $groups = call_user_func_array('array_merge', $groups);
48        $groups = array_merge($groups, $this->loadGroupsFromAcl());
49
50        $this->groups = array_unique($groups);
51        $this->groupsloaded = true;
52    }
53
54    public function getGroups($length = NULL, $filter = '') {
55        if (!$this->groupsloaded) {
56            $this->loadGroups();
57        }
58
59        $groups = $this->groups;
60        if ($filter != '') $groups = array_filter($groups, function ($group) use ($filter) {
61            return strpos($group, $filter) === 0;
62        });
63
64        return array_slice($groups, 0, $length);
65    }
66
67}