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