xref: /plugin/farmer/admin/new.php (revision dfdaf33e5be77ffde44c74599bccb56fea95f9ea)
1<?php
2/**
3 * DokuWiki Plugin farmer (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <grosse@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
13
14    /** @var helper_plugin_farmer $helper */
15    protected $helper;
16
17    /**
18     * @return bool true if only access for superuser, false is for superusers and moderators
19     */
20    public function forAdminOnly() {
21        return true;
22    }
23
24    /**
25     * admin_plugin_farmer_new constructor.
26     */
27    public function __construct() {
28        $this->helper = plugin_load('helper', 'farmer');
29    }
30
31    /**
32     * Should carry out any processing required by the plugin.
33     */
34    public function handle() {
35        global $INPUT;
36        global $ID;
37        if(!$INPUT->has('farmer__submit')) return;
38
39        $data = $this->validateAnimalData();
40        if(!$data) return;
41        if($this->createNewAnimal($data['name'], $data['admin'], $data['pass'], $data['template'])) {
42            $url = $this->helper->getAnimalURL($data['name']);
43            $link = '<a href="' . $url . '">' . hsc($data['name']) . '</a>';
44
45            msg(sprintf($this->getLang('animal creation success'), $link), 1);
46            $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'new'), true, '&');
47            send_redirect($link);
48        }
49    }
50
51    /**
52     * Render HTML output, e.g. helpful text and a form
53     */
54    public function html() {
55        $farmconfig = $this->helper->getConfig();
56
57        $form = new \dokuwiki\Form\Form();
58        $form->addClass('plugin_farmer')->id('farmer__create_animal_form');
59
60        $form->addFieldsetOpen($this->getLang('animal configuration'));
61        $form->addTextInput('animalname', $this->getLang('animal'));
62        $form->addFieldsetClose();
63
64        $animals = $this->helper->getAllAnimals();
65        array_unshift($animals, '');
66        $form->addFieldsetOpen($this->getLang('animal template'));
67        $form->addDropdown('animaltemplate', $animals)->addClass('farmer_choosen_animals');
68        $form->addFieldsetClose();
69
70        $form->addFieldsetOpen($this->getLang('animal administrator'));
71        $btn = $form->addRadioButton('adminsetup', $this->getLang('noUsers'))->val('noUsers');
72        if($farmconfig['inherit']['users']) {
73            $btn->attr('checked', 'checked');  // default when inherit available
74        }
75        $form->addRadioButton('adminsetup', $this->getLang('importUsers'))->val('importUsers');
76        $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))->val('currentAdmin');
77        $btn = $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))->val('newAdmin');
78        if(!$farmconfig['inherit']['users']) {
79            $btn->attr('checked', 'checked'); // default when inherit not available
80        }
81        $form->addPasswordInput('adminPassword', $this->getLang('admin password'));
82        $form->addFieldsetClose();
83
84        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit')->val('newAnimal');
85        echo $form->toHTML();
86    }
87
88    /**
89     * Validate the data for a new animal
90     *
91     * @return array|bool false on errors, clean data otherwise
92     */
93    protected function validateAnimalData() {
94        global $INPUT;
95
96        $animalname = $INPUT->filter('trim')->str('animalname');
97        $adminsetup = $INPUT->str('adminsetup');
98        $adminpass = $INPUT->filter('trim')->str('adminPassword');
99        $template = $INPUT->filter('trim')->str('animaltemplate');
100
101        $errors = array();
102
103        if($animalname === '') {
104            $errors[] = $this->getLang('animalname_missing');
105        } elseif(!$this->helper->validateAnimalName($animalname)) {
106            $errors[] = $this->getLang('animalname_invalid');
107        }
108
109        if($adminsetup === 'newAdmin' && $adminpass === '') {
110            $errors[] = $this->getLang('adminPassword_empty');
111        }
112
113        if($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) {
114            $errors[] = $this->getLang('animalname_preexisting');
115        }
116
117        if($errors) {
118            foreach($errors as $error) {
119                msg($error, -1);
120            }
121            return false;
122        }
123
124        if(!is_dir(DOKU_FARMDIR . '/' . $template)) {
125            $template = '';
126        }
127
128        return array(
129            'name' => $animalname,
130            'admin' => $adminsetup,
131            'pass' => $adminpass,
132            'template' => $template
133        );
134    }
135
136    /**
137     * Create a new animal
138     *
139     * @param string $name name/title of the animal, will be the directory name for htaccess setup
140     * @param string $adminSetup newAdmin, currentAdmin or importUsers
141     * @param string $adminPassword required if $adminSetup is newAdmin
142     * @param string $template name of animal to copy
143     * @return bool true if successful
144     */
145    protected function createNewAnimal($name, $adminSetup, $adminPassword, $template) {
146        $animaldir = DOKU_FARMDIR . '/' . $name;
147
148        // copy basic template
149        $ok = $this->helper->io_copyDir(__DIR__ . '/../_animal', $animaldir);
150        if(!$ok) {
151            msg($this->getLang('animal creation error'), -1);
152            return false;
153        }
154
155        // copy animal template
156        if($template != '') {
157            foreach(array('conf', 'data/pages', 'data/media', 'data/meta', 'data/media_meta', 'index') as $dir) {
158                $templatedir = DOKU_FARMDIR . '/' . $template . '/' . $dir;
159                if(!is_dir($templatedir)) continue;
160                // do not copy changelogs in meta
161                if(substr($dir, -4) == 'meta') {
162                    $exclude = '/\.changes$/';
163                } else {
164                    $exclude = '';
165                }
166                if(!$this->helper->io_copyDir($templatedir, $animaldir . '/' . $dir, $exclude)) {
167                    msg(sprintf($this->getLang('animal template copy error'), $dir), -1);
168                    // we go on anyway
169                }
170            }
171        }
172
173        // append title to local config
174        $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'title\'] = \'' . $name . '\';' . "\n", true);
175
176        // create a random logo and favicon
177        if(!class_exists('\splitbrain\RingIcon\RingIcon', false)) {
178            require(__DIR__ . '/../3rdparty/RingIcon.php');
179        }
180        if(!class_exists('\chrisbliss18\phpico\PHPIco', false)) {
181            require(__DIR__ . '/../3rdparty/PHPIco.php');
182        }
183        try {
184            $ringicon = new \splitbrain\RingIcon\RingIcon(64);
185            $ringicon->createImage($animaldir, $animaldir . '/data/media/wiki/logo.png');
186            $icongen = new \chrisbliss18\phpico\PHPIco($animaldir . '/data/media/wiki/logo.png');
187            $icongen->save_ico($animaldir . '/data/media/wiki/favicon.ico');
188        } catch(\Exception $ignore) {
189            // something went wrong, but we don't care. this is a nice to have feature only
190        }
191
192        // create admin user
193        if($adminSetup === 'newAdmin') {
194            $users = "# <?php exit()?>\n" . $this->makeAdminLine($adminPassword) . "\n";
195        } elseif($adminSetup === 'currentAdmin') {
196            $users = "# <?php exit()?>\n" . $this->getAdminLine() . "\n";
197        } elseif($adminSetup === 'noUsers') {
198            if(file_exists($animaldir . '/conf/users.auth.php')) {
199                // a user file exists already, probably from animal template - don't overwrite
200                $users = '';
201            } else {
202                // create empty user file
203                $users = "# <?php exit()?>\n";
204            }
205        } else {
206            $users = io_readFile(DOKU_CONF . 'users.auth.php');
207        }
208        if($users) {
209            $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users);
210        }
211
212        /* FIXME handle deactivated plugins
213        if($this->getConf('deactivated plugins') === '') {
214            $deactivatedPluginsList = array('farmer',);
215        } else {
216            $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins'));
217            array_push($deactivatedPluginsList, 'farmer');
218        }
219        foreach($deactivatedPluginsList as $plugin) {
220            $this->helper->deactivatePlugin(trim($plugin), $animal);
221        }
222        */
223
224        return $ok;
225    }
226
227    /**
228     * Creates a new user line
229     *
230     * @param $password
231     * @return string
232     */
233    protected function makeAdminLine($password) {
234        $pass = auth_cryptPassword($password);
235        $line = join(
236            "\t", array(
237                    'admin',
238                    $pass,
239                    'Administrator',
240                    'admin@example.org',
241                    'admin,user'
242                )
243        );
244        return $line;
245    }
246
247    /**
248     * Copies the current user as new admin line
249     *
250     * @return string
251     */
252    protected function getAdminLine() {
253        $currentAdmin = $_SERVER['REMOTE_USER'];
254        $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php');
255        $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":"));
256        $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1);
257        return $newAdmin;
258    }
259
260}
261
262// vim:ts=4:sw=4:et:
263