1<?php
2
3use dokuwiki\Extension\RemotePlugin;
4use dokuwiki\Remote\AccessDeniedException;
5
6class remote_plugin_farmer extends RemotePlugin {
7    /** @var helper_plugin_farmer hlp */
8    protected $helper;
9
10    /**
11     * remote_plugin_struct constructor.
12     */
13    public function __construct()
14    {
15        parent::__construct();
16
17        $this->helper = plugin_load('helper', 'farmer');
18    }
19
20    /**
21     * Get the configured farm host
22     *
23     * @return string
24     * @throws AccessDeniedException
25     */
26    public function getFarmhost(): string
27    {
28        $this->ensureAdmin();
29        return $this->helper->getConfig()['base']['farmhost'];
30    }
31
32    /**
33     * Get the configured base domain of the farmer
34     * This could be an empty string, then farmhost will be used to determine an animal url
35     *
36     * @return string
37     * @throws AccessDeniedException
38     */
39    public function getBaseDomain(): string
40    {
41        $this->ensureAdmin();
42        return $this->helper->getConfig()['base']['basedomain'];
43    }
44
45    /**
46     * Get a list of all animal names
47     *
48     * @return array
49     * @throws AccessDeniedException
50     */
51    public function listAnimals(): array
52    {
53        $this->ensureAdmin();
54        return $this->helper->getAllAnimals();
55    }
56
57    /**
58     * Get a list of all animal urls
59     *
60     * @return array
61     * @throws AccessDeniedException
62     */
63    public function listAnimalUrls(): array
64    {
65        $this->ensureAdmin();
66        foreach($this->helper->getAllAnimals() as $animal) {
67            $animalUrls[] = $this->helper->getAnimalURL($animal);
68        }
69        return $animalUrls;
70    }
71
72    /**
73     * Get configuration details of farmer plugin enriched by list of animals
74     *
75     * @return array
76     * @throws AccessDeniedException
77     */
78    public function getFarmerConfig(): array
79    {
80        $this->ensureAdmin();
81        $farmerConfig = $this->helper->getConfig();
82        foreach($this->helper->getAllAnimals() as $index=>$animal) {
83            $farmerConfig['animals'][$index]["name"] =$animal;
84            $farmerConfig['animals'][$index]["url"] = $this->helper->getAnimalURL($animal);
85        }
86        return $farmerConfig;
87    }
88
89    /**
90     * @throws AccessDeniedException
91     */
92    private function ensureAdmin() {
93        if (!auth_isadmin()) {
94            throw new AccessDeniedException(
95                'You are not allowed to access farmer configuration, superuser permission is required',
96                114
97            );
98        }
99    }
100}
101