xref: /plugin/farmer/admin/new.php (revision 1da41c8bdb9ce0b590d5b69552c4ddb4d0aef860)
149f2871cSAndreas Gohr<?php
2*1da41c8bSAndreas Gohr
3*1da41c8bSAndreas Gohruse chrisbliss18\phpico\PHPIco;
4*1da41c8bSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
5*1da41c8bSAndreas Gohruse dokuwiki\Form\Form;
6*1da41c8bSAndreas Gohruse splitbrain\RingIcon\RingIcon;
7*1da41c8bSAndreas Gohr
849f2871cSAndreas Gohr/**
949f2871cSAndreas Gohr * DokuWiki Plugin farmer (Admin Component)
1049f2871cSAndreas Gohr *
11*1da41c8bSAndreas Gohr * Create new animals
12*1da41c8bSAndreas Gohr *
1349f2871cSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1449f2871cSAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
1549f2871cSAndreas Gohr */
16*1da41c8bSAndreas Gohrclass admin_plugin_farmer_new extends AdminPlugin
17*1da41c8bSAndreas Gohr{
1849f2871cSAndreas Gohr    /** @var helper_plugin_farmer $helper */
1949f2871cSAndreas Gohr    protected $helper;
2049f2871cSAndreas Gohr
2149f2871cSAndreas Gohr    /**
2249f2871cSAndreas Gohr     * @return bool true if only access for superuser, false is for superusers and moderators
2349f2871cSAndreas Gohr     */
24*1da41c8bSAndreas Gohr    public function forAdminOnly()
25*1da41c8bSAndreas Gohr    {
2649f2871cSAndreas Gohr        return true;
2749f2871cSAndreas Gohr    }
2849f2871cSAndreas Gohr
2949f2871cSAndreas Gohr    /**
3049f2871cSAndreas Gohr     * admin_plugin_farmer_new constructor.
3149f2871cSAndreas Gohr     */
32*1da41c8bSAndreas Gohr    public function __construct()
33*1da41c8bSAndreas Gohr    {
3449f2871cSAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
3549f2871cSAndreas Gohr    }
3649f2871cSAndreas Gohr
3749f2871cSAndreas Gohr    /**
3849f2871cSAndreas Gohr     * Should carry out any processing required by the plugin.
3949f2871cSAndreas Gohr     */
40*1da41c8bSAndreas Gohr    public function handle()
41*1da41c8bSAndreas Gohr    {
4249f2871cSAndreas Gohr        global $INPUT;
4349f2871cSAndreas Gohr        global $ID;
4449f2871cSAndreas Gohr        if (!$INPUT->has('farmer__submit')) return;
4549f2871cSAndreas Gohr
4649f2871cSAndreas Gohr        $data = $this->validateAnimalData();
4749f2871cSAndreas Gohr        if (!$data) return;
48*1da41c8bSAndreas Gohr        if (
49*1da41c8bSAndreas Gohr            $this->createNewAnimal(
50*1da41c8bSAndreas Gohr                $data['name'],
51*1da41c8bSAndreas Gohr                $data['admin'],
52*1da41c8bSAndreas Gohr                $data['pass'],
53*1da41c8bSAndreas Gohr                $data['template'],
54*1da41c8bSAndreas Gohr                $data['aclpolicy'],
55*1da41c8bSAndreas Gohr                $data['allowreg']
56*1da41c8bSAndreas Gohr            )
57*1da41c8bSAndreas Gohr        ) {
580336ab2aSAndreas Gohr            $url = $this->helper->getAnimalURL($data['name']);
590336ab2aSAndreas Gohr            $link = '<a href="' . $url . '">' . hsc($data['name']) . '</a>';
600336ab2aSAndreas Gohr
610336ab2aSAndreas Gohr            msg(sprintf($this->getLang('animal creation success'), $link), 1);
62*1da41c8bSAndreas Gohr            $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'new'], true, '&');
6349f2871cSAndreas Gohr            send_redirect($link);
6449f2871cSAndreas Gohr        }
6549f2871cSAndreas Gohr    }
6649f2871cSAndreas Gohr
6749f2871cSAndreas Gohr    /**
6849f2871cSAndreas Gohr     * Render HTML output, e.g. helpful text and a form
6949f2871cSAndreas Gohr     */
70*1da41c8bSAndreas Gohr    public function html()
71*1da41c8bSAndreas Gohr    {
726348ab5bSMichael Grosse        global $lang;
731272da0cSAndreas Gohr        $farmconfig = $this->helper->getConfig();
7449f2871cSAndreas Gohr
75*1da41c8bSAndreas Gohr        $form = new Form();
7649f2871cSAndreas Gohr        $form->addClass('plugin_farmer')->id('farmer__create_animal_form');
7749f2871cSAndreas Gohr
7849f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal configuration'));
7923164e01SAndreas Gohr        $form->addTextInput('animalname', $this->getLang('animal'));
8049f2871cSAndreas Gohr        $form->addFieldsetClose();
8149f2871cSAndreas Gohr
82801ebaa1SAndreas Gohr        $animals = $this->helper->getAllAnimals();
83801ebaa1SAndreas Gohr        array_unshift($animals, '');
84801ebaa1SAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal template'));
85*1da41c8bSAndreas Gohr        $form->addDropdown('animaltemplate', $animals)
86*1da41c8bSAndreas Gohr            ->addClass('farmer_chosen_animals');
87801ebaa1SAndreas Gohr        $form->addFieldsetClose();
88801ebaa1SAndreas Gohr
896348ab5bSMichael Grosse        $form->addFieldsetOpen($lang['i_policy'])->attr('id', 'aclPolicyFieldset');
90*1da41c8bSAndreas Gohr        $policyOptions = ['open' => $lang['i_pol0'], 'public' => $lang['i_pol1'], 'closed' => $lang['i_pol2']];
91*1da41c8bSAndreas Gohr        $form->addDropdown('aclpolicy', $policyOptions)
92*1da41c8bSAndreas Gohr            ->addClass('acl_chosen');
936348ab5bSMichael Grosse        if ($farmconfig['inherit']['main']) {
94*1da41c8bSAndreas Gohr            $form->addRadioButton('allowreg', $this->getLang('inherit user registration'))
95*1da41c8bSAndreas Gohr                ->val('inherit')
96*1da41c8bSAndreas Gohr                ->attr('checked', 'checked');
97*1da41c8bSAndreas Gohr            $form->addRadioButton('allowreg', $this->getLang('enable user registration'))
98*1da41c8bSAndreas Gohr                ->val('allow');
99*1da41c8bSAndreas Gohr            $form->addRadioButton('allowreg', $this->getLang('disable user registration'))
100*1da41c8bSAndreas Gohr                ->val('disable');
1016348ab5bSMichael Grosse        } else {
102*1da41c8bSAndreas Gohr            $form->addCheckbox('allowreg', $lang['i_allowreg'])
103*1da41c8bSAndreas Gohr                ->attr('checked', 'checked');
1046348ab5bSMichael Grosse        }
1056348ab5bSMichael Grosse
1066348ab5bSMichael Grosse        $form->addFieldsetClose();
1076348ab5bSMichael Grosse
10849f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal administrator'));
109*1da41c8bSAndreas Gohr
110*1da41c8bSAndreas Gohr        $btn = $form->addRadioButton('adminsetup', $this->getLang('noUsers'))
111*1da41c8bSAndreas Gohr            ->val('noUsers');
1121272da0cSAndreas Gohr        if ($farmconfig['inherit']['users']) {
1131272da0cSAndreas Gohr            $btn->attr('checked', 'checked');  // default when inherit available
1141a130845SAndreas Gohr        } else {
1151a130845SAndreas Gohr            // no user copying when inheriting
116*1da41c8bSAndreas Gohr            $form->addRadioButton('adminsetup', $this->getLang('importUsers'))
117*1da41c8bSAndreas Gohr                ->val('importUsers');
118*1da41c8bSAndreas Gohr            $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))
119*1da41c8bSAndreas Gohr                ->val('currentAdmin');
1201a130845SAndreas Gohr        }
121*1da41c8bSAndreas Gohr        $btn = $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))
122*1da41c8bSAndreas Gohr            ->val('newAdmin');
1231272da0cSAndreas Gohr        if (!$farmconfig['inherit']['users']) {
1241272da0cSAndreas Gohr            $btn->attr('checked', 'checked'); // default when inherit not available
1251272da0cSAndreas Gohr        }
12649f2871cSAndreas Gohr        $form->addPasswordInput('adminPassword', $this->getLang('admin password'));
12749f2871cSAndreas Gohr        $form->addFieldsetClose();
12849f2871cSAndreas Gohr
129*1da41c8bSAndreas Gohr        $form->addButton('farmer__submit', $this->getLang('submit'))
130*1da41c8bSAndreas Gohr            ->attr('type', 'submit')
131*1da41c8bSAndreas Gohr            ->val('newAnimal');
13249f2871cSAndreas Gohr        echo $form->toHTML();
13349f2871cSAndreas Gohr    }
13449f2871cSAndreas Gohr
13549f2871cSAndreas Gohr    /**
13649f2871cSAndreas Gohr     * Validate the data for a new animal
13749f2871cSAndreas Gohr     *
13849f2871cSAndreas Gohr     * @return array|bool false on errors, clean data otherwise
13949f2871cSAndreas Gohr     */
140*1da41c8bSAndreas Gohr    protected function validateAnimalData()
141*1da41c8bSAndreas Gohr    {
14249f2871cSAndreas Gohr        global $INPUT;
14349f2871cSAndreas Gohr
14449f2871cSAndreas Gohr        $animalname = $INPUT->filter('trim')->str('animalname');
14549f2871cSAndreas Gohr        $adminsetup = $INPUT->str('adminsetup');
14649f2871cSAndreas Gohr        $adminpass = $INPUT->filter('trim')->str('adminPassword');
147801ebaa1SAndreas Gohr        $template = $INPUT->filter('trim')->str('animaltemplate');
1486348ab5bSMichael Grosse        $aclpolicy = $INPUT->filter('trim')->str('aclpolicy');
1496348ab5bSMichael Grosse        $allowreg = $INPUT->str('allowreg');
15049f2871cSAndreas Gohr
151*1da41c8bSAndreas Gohr        $errors = [];
15249f2871cSAndreas Gohr
15349f2871cSAndreas Gohr        if ($animalname === '') {
15449f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_missing');
15549f2871cSAndreas Gohr        } elseif (!$this->helper->validateAnimalName($animalname)) {
15649f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_invalid');
15749f2871cSAndreas Gohr        }
15849f2871cSAndreas Gohr
15949f2871cSAndreas Gohr        if ($adminsetup === 'newAdmin' && $adminpass === '') {
16049f2871cSAndreas Gohr            $errors[] = $this->getLang('adminPassword_empty');
16149f2871cSAndreas Gohr        }
16249f2871cSAndreas Gohr
16349f2871cSAndreas Gohr        if ($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) {
16449f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_preexisting');
16549f2871cSAndreas Gohr        }
16649f2871cSAndreas Gohr
167*1da41c8bSAndreas Gohr        if (!is_dir(DOKU_FARMDIR . $template) && !in_array($aclpolicy, ['open', 'public', 'closed'])) {
1686348ab5bSMichael Grosse            $errors[] = $this->getLang('aclpolicy missing/bad');
1696348ab5bSMichael Grosse        }
1706348ab5bSMichael Grosse
17149f2871cSAndreas Gohr        if ($errors) {
17249f2871cSAndreas Gohr            foreach ($errors as $error) {
17349f2871cSAndreas Gohr                msg($error, -1);
17449f2871cSAndreas Gohr            }
17549f2871cSAndreas Gohr            return false;
17649f2871cSAndreas Gohr        }
17749f2871cSAndreas Gohr
1788262a4cbSAndreas Gohr        if (!is_dir(DOKU_FARMDIR . $template)) {
179801ebaa1SAndreas Gohr            $template = '';
180801ebaa1SAndreas Gohr        }
1816348ab5bSMichael Grosse        if ($template != '') {
1826348ab5bSMichael Grosse            $aclpolicy = '';
1836348ab5bSMichael Grosse        }
184801ebaa1SAndreas Gohr
185*1da41c8bSAndreas Gohr        return [
18649f2871cSAndreas Gohr            'name' => $animalname,
18749f2871cSAndreas Gohr            'admin' => $adminsetup,
188801ebaa1SAndreas Gohr            'pass' => $adminpass,
1896348ab5bSMichael Grosse            'template' => $template,
1906348ab5bSMichael Grosse            'aclpolicy' => $aclpolicy,
1916348ab5bSMichael Grosse            'allowreg' => $allowreg
192*1da41c8bSAndreas Gohr        ];
19349f2871cSAndreas Gohr    }
19449f2871cSAndreas Gohr
19549f2871cSAndreas Gohr    /**
19649f2871cSAndreas Gohr     * Create a new animal
19749f2871cSAndreas Gohr     *
19849f2871cSAndreas Gohr     * @param string $name name/title of the animal, will be the directory name for htaccess setup
19949f2871cSAndreas Gohr     * @param string $adminSetup newAdmin, currentAdmin or importUsers
20049f2871cSAndreas Gohr     * @param string $adminPassword required if $adminSetup is newAdmin
201801ebaa1SAndreas Gohr     * @param string $template name of animal to copy
2026348ab5bSMichael Grosse     * @param $aclpolicy
2036348ab5bSMichael Grosse     * @param $userreg
20449f2871cSAndreas Gohr     * @return bool true if successful
2056348ab5bSMichael Grosse     * @throws Exception
20649f2871cSAndreas Gohr     */
207*1da41c8bSAndreas Gohr    protected function createNewAnimal($name, $adminSetup, $adminPassword, $template, $aclpolicy, $userreg)
208*1da41c8bSAndreas Gohr    {
2098262a4cbSAndreas Gohr        $animaldir = DOKU_FARMDIR . $name;
21049f2871cSAndreas Gohr
21149f2871cSAndreas Gohr        // copy basic template
212*1da41c8bSAndreas Gohr        $ok = $this->helper->copyDir(__DIR__ . '/../_animal', $animaldir);
21349f2871cSAndreas Gohr        if (!$ok) {
21449f2871cSAndreas Gohr            msg($this->getLang('animal creation error'), -1);
21549f2871cSAndreas Gohr            return false;
21649f2871cSAndreas Gohr        }
21749f2871cSAndreas Gohr
218801ebaa1SAndreas Gohr        // copy animal template
219801ebaa1SAndreas Gohr        if ($template != '') {
220*1da41c8bSAndreas Gohr            foreach (['conf', 'data/pages', 'data/media', 'data/meta', 'data/media_meta', 'index'] as $dir) {
2218262a4cbSAndreas Gohr                $templatedir = DOKU_FARMDIR . $template . '/' . $dir;
222801ebaa1SAndreas Gohr                if (!is_dir($templatedir)) continue;
223801ebaa1SAndreas Gohr                // do not copy changelogs in meta
224801ebaa1SAndreas Gohr                if (substr($dir, -4) == 'meta') {
225801ebaa1SAndreas Gohr                    $exclude = '/\.changes$/';
226801ebaa1SAndreas Gohr                } else {
227801ebaa1SAndreas Gohr                    $exclude = '';
228801ebaa1SAndreas Gohr                }
229*1da41c8bSAndreas Gohr                if (!$this->helper->copyDir($templatedir, $animaldir . '/' . $dir, $exclude)) {
230801ebaa1SAndreas Gohr                    msg(sprintf($this->getLang('animal template copy error'), $dir), -1);
231801ebaa1SAndreas Gohr                    // we go on anyway
232801ebaa1SAndreas Gohr                }
233801ebaa1SAndreas Gohr            }
234801ebaa1SAndreas Gohr        }
235801ebaa1SAndreas Gohr
23649f2871cSAndreas Gohr        // append title to local config
23749f2871cSAndreas Gohr        $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'title\'] = \'' . $name . '\';' . "\n", true);
23849f2871cSAndreas Gohr
2394eba53bcSAndreas Gohr        // create a random logo and favicon
2404eba53bcSAndreas Gohr        if (!class_exists('\splitbrain\RingIcon\RingIcon', false)) {
2414eba53bcSAndreas Gohr            require(__DIR__ . '/../3rdparty/RingIcon.php');
2424eba53bcSAndreas Gohr        }
2434eba53bcSAndreas Gohr        if (!class_exists('\chrisbliss18\phpico\PHPIco', false)) {
2444eba53bcSAndreas Gohr            require(__DIR__ . '/../3rdparty/PHPIco.php');
2454eba53bcSAndreas Gohr        }
2464eba53bcSAndreas Gohr        try {
247b263debeSAndreas Gohr            if (function_exists('imagecreatetruecolor')) {
248c3bd7eb3SAndreas Gohr                $logo = $animaldir . '/data/media/wiki/logo.png';
249c3bd7eb3SAndreas Gohr                if (!file_exists($logo)) {
250*1da41c8bSAndreas Gohr                    $ringicon = new RingIcon(64);
251c3bd7eb3SAndreas Gohr                    $ringicon->createImage($animaldir, $logo);
252c3bd7eb3SAndreas Gohr                }
253c3bd7eb3SAndreas Gohr
254c3bd7eb3SAndreas Gohr                $icon = $animaldir . '/data/media/wiki/favicon.ico';
255c3bd7eb3SAndreas Gohr                if (!file_exists($icon)) {
256*1da41c8bSAndreas Gohr                    $icongen = new PHPIco($logo);
257*1da41c8bSAndreas Gohr                    $icongen->saveIco($icon);
258c3bd7eb3SAndreas Gohr                }
259b263debeSAndreas Gohr            }
2604eba53bcSAndreas Gohr        } catch (\Exception $ignore) {
2614eba53bcSAndreas Gohr            // something went wrong, but we don't care. this is a nice to have feature only
2624eba53bcSAndreas Gohr        }
26349f2871cSAndreas Gohr
26449f2871cSAndreas Gohr        // create admin user
26549f2871cSAndreas Gohr        if ($adminSetup === 'newAdmin') {
26649f2871cSAndreas Gohr            $users = "# <?php exit()?>\n" . $this->makeAdminLine($adminPassword) . "\n";
26749f2871cSAndreas Gohr        } elseif ($adminSetup === 'currentAdmin') {
26849f2871cSAndreas Gohr            $users = "# <?php exit()?>\n" . $this->getAdminLine() . "\n";
2691272da0cSAndreas Gohr        } elseif ($adminSetup === 'noUsers') {
270801ebaa1SAndreas Gohr            if (file_exists($animaldir . '/conf/users.auth.php')) {
271801ebaa1SAndreas Gohr                // a user file exists already, probably from animal template - don't overwrite
272801ebaa1SAndreas Gohr                $users = '';
273801ebaa1SAndreas Gohr            } else {
274801ebaa1SAndreas Gohr                // create empty user file
2751272da0cSAndreas Gohr                $users = "# <?php exit()?>\n";
276801ebaa1SAndreas Gohr            }
27749f2871cSAndreas Gohr        } else {
27849f2871cSAndreas Gohr            $users = io_readFile(DOKU_CONF . 'users.auth.php');
27949f2871cSAndreas Gohr        }
280801ebaa1SAndreas Gohr        if ($users) {
28149f2871cSAndreas Gohr            $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users);
282801ebaa1SAndreas Gohr        }
28349f2871cSAndreas Gohr
2846348ab5bSMichael Grosse        if ($aclpolicy != '') {
2856348ab5bSMichael Grosse            $aclfile = file($animaldir . '/conf/acl.auth.php');
2862b85f30dSAndreas Gohr            $aclfile = array_map('trim', $aclfile);
2876348ab5bSMichael Grosse            array_pop($aclfile);
2886348ab5bSMichael Grosse            switch ($aclpolicy) {
2896348ab5bSMichael Grosse                case 'open':
2906348ab5bSMichael Grosse                    $aclfile[] = "* @ALL 8";
2916348ab5bSMichael Grosse                    break;
2926348ab5bSMichael Grosse                case 'public':
2936348ab5bSMichael Grosse                    $aclfile[] = "* @ALL 1";
2946348ab5bSMichael Grosse                    $aclfile[] = "* @user 8";
2956348ab5bSMichael Grosse                    break;
2966348ab5bSMichael Grosse                case 'closed':
2976348ab5bSMichael Grosse                    $aclfile[] = "* @ALL 0";
2986348ab5bSMichael Grosse                    $aclfile[] = "* @user 8";
2996348ab5bSMichael Grosse                    break;
3006348ab5bSMichael Grosse                default:
3016348ab5bSMichael Grosse                    throw new Exception('Undefined aclpolicy given');
3026348ab5bSMichael Grosse            }
303*1da41c8bSAndreas Gohr            $ok &= io_saveFile($animaldir . '/conf/acl.auth.php', implode("\n", $aclfile) . "\n");
3046348ab5bSMichael Grosse
3056348ab5bSMichael Grosse            global $conf;
3066348ab5bSMichael Grosse            switch ($userreg) {
3076348ab5bSMichael Grosse                case 'allow':
308*1da41c8bSAndreas Gohr                    $disableactions = implode(',', array_diff(explode(',', $conf['disableactions']), ['register']));
309*1da41c8bSAndreas Gohr                    $ok &= io_saveFile(
310*1da41c8bSAndreas Gohr                        $animaldir . '/conf/local.php',
311*1da41c8bSAndreas Gohr                        "\n" . '$conf[\'disableactions\'] = \'' . $disableactions . '\';' . "\n",
312*1da41c8bSAndreas Gohr                        true
313*1da41c8bSAndreas Gohr                    );
3146348ab5bSMichael Grosse                    break;
3156348ab5bSMichael Grosse                case 'disable':
316*1da41c8bSAndreas Gohr                    $disableactions = implode(',', array_merge(explode(',', $conf['disableactions']), ['register']));
317*1da41c8bSAndreas Gohr                    $ok &= io_saveFile(
318*1da41c8bSAndreas Gohr                        $animaldir . '/conf/local.php',
319*1da41c8bSAndreas Gohr                        "\n" . '$conf[\'disableactions\'] = \'' . $disableactions . '\';' . "\n",
320*1da41c8bSAndreas Gohr                        true
321*1da41c8bSAndreas Gohr                    );
3226348ab5bSMichael Grosse                    break;
3236348ab5bSMichael Grosse                case 'inherit':
3246348ab5bSMichael Grosse                case true:
3256348ab5bSMichael Grosse                    // nothing needs to be done
3266348ab5bSMichael Grosse                    break;
3276348ab5bSMichael Grosse                default:
328*1da41c8bSAndreas Gohr                    $ok &= io_saveFile(
329*1da41c8bSAndreas Gohr                        $animaldir . '/conf/local.php',
330*1da41c8bSAndreas Gohr                        "\n" . '$conf[\'disableactions\'] = \'register\';' . "\n",
331*1da41c8bSAndreas Gohr                        true
332*1da41c8bSAndreas Gohr                    );
3336348ab5bSMichael Grosse            }
3346348ab5bSMichael Grosse        }
3356348ab5bSMichael Grosse
3364664a1d2SAndreas Gohr        // deactivate plugins by default FIXME this should be nicer
33749f2871cSAndreas Gohr        $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins'));
3384664a1d2SAndreas Gohr        $deactivatedPluginsList = array_map('trim', $deactivatedPluginsList);
3394664a1d2SAndreas Gohr        $deactivatedPluginsList = array_unique($deactivatedPluginsList);
3404664a1d2SAndreas Gohr        $deactivatedPluginsList = array_filter($deactivatedPluginsList);
34149f2871cSAndreas Gohr        foreach ($deactivatedPluginsList as $plugin) {
342af1c6dd8SAndreas Gohr            $this->helper->setPluginState(trim($plugin), $name, 0);
34349f2871cSAndreas Gohr        }
34449f2871cSAndreas Gohr
34549f2871cSAndreas Gohr        return $ok;
34649f2871cSAndreas Gohr    }
34749f2871cSAndreas Gohr
34849f2871cSAndreas Gohr    /**
34949f2871cSAndreas Gohr     * Creates a new user line
35049f2871cSAndreas Gohr     *
35149f2871cSAndreas Gohr     * @param $password
35249f2871cSAndreas Gohr     * @return string
35349f2871cSAndreas Gohr     */
354*1da41c8bSAndreas Gohr    protected function makeAdminLine($password)
355*1da41c8bSAndreas Gohr    {
35649f2871cSAndreas Gohr        $pass = auth_cryptPassword($password);
357*1da41c8bSAndreas Gohr        $line = implode(
358*1da41c8bSAndreas Gohr            ':',
359*1da41c8bSAndreas Gohr            ['admin', $pass, 'Administrator', 'admin@example.org', 'admin,user']
3601272da0cSAndreas Gohr        );
36149f2871cSAndreas Gohr        return $line;
36249f2871cSAndreas Gohr    }
36349f2871cSAndreas Gohr
36449f2871cSAndreas Gohr    /**
36549f2871cSAndreas Gohr     * Copies the current user as new admin line
36649f2871cSAndreas Gohr     *
36749f2871cSAndreas Gohr     * @return string
36849f2871cSAndreas Gohr     */
369*1da41c8bSAndreas Gohr    protected function getAdminLine()
370*1da41c8bSAndreas Gohr    {
37149f2871cSAndreas Gohr        $currentAdmin = $_SERVER['REMOTE_USER'];
37249f2871cSAndreas Gohr        $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php');
37349f2871cSAndreas Gohr        $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":"));
374*1da41c8bSAndreas Gohr
37549f2871cSAndreas Gohr        $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1);
37649f2871cSAndreas Gohr        return $newAdmin;
37749f2871cSAndreas Gohr    }
37849f2871cSAndreas Gohr}
379