xref: /plugin/farmer/admin/new.php (revision 655fe6c1fa0e8eca0207e883f3a88be96a9a98a2)
149f2871cSAndreas Gohr<?php
21da41c8bSAndreas Gohr
31da41c8bSAndreas Gohruse chrisbliss18\phpico\PHPIco;
41da41c8bSAndreas Gohruse dokuwiki\Extension\AdminPlugin;
51da41c8bSAndreas Gohruse dokuwiki\Form\Form;
6*655fe6c1SAndreas Gohruse dokuwiki\Utf8\PhpString;
71da41c8bSAndreas Gohruse splitbrain\RingIcon\RingIcon;
81da41c8bSAndreas Gohr
949f2871cSAndreas Gohr/**
1049f2871cSAndreas Gohr * DokuWiki Plugin farmer (Admin Component)
1149f2871cSAndreas Gohr *
121da41c8bSAndreas Gohr * Create new animals
131da41c8bSAndreas Gohr *
1449f2871cSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
1549f2871cSAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
1649f2871cSAndreas Gohr */
171da41c8bSAndreas Gohrclass admin_plugin_farmer_new extends AdminPlugin
181da41c8bSAndreas Gohr{
1949f2871cSAndreas Gohr    /** @var helper_plugin_farmer $helper */
2049f2871cSAndreas Gohr    protected $helper;
2149f2871cSAndreas Gohr
2249f2871cSAndreas Gohr    /**
2349f2871cSAndreas Gohr     * admin_plugin_farmer_new constructor.
2449f2871cSAndreas Gohr     */
251da41c8bSAndreas Gohr    public function __construct()
261da41c8bSAndreas Gohr    {
2749f2871cSAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
2849f2871cSAndreas Gohr    }
2949f2871cSAndreas Gohr
30341a2d35SAndreas Gohr    /** @inheritdoc */
31341a2d35SAndreas Gohr    public function showInMenu()
32341a2d35SAndreas Gohr    {
33341a2d35SAndreas Gohr        return false;
34341a2d35SAndreas Gohr    }
35341a2d35SAndreas Gohr
3649f2871cSAndreas Gohr    /**
3749f2871cSAndreas Gohr     * Should carry out any processing required by the plugin.
3849f2871cSAndreas Gohr     */
391da41c8bSAndreas Gohr    public function handle()
401da41c8bSAndreas Gohr    {
4149f2871cSAndreas Gohr        global $INPUT;
4249f2871cSAndreas Gohr        global $ID;
4349f2871cSAndreas Gohr        if (!$INPUT->has('farmer__submit')) return;
4449f2871cSAndreas Gohr
4549f2871cSAndreas Gohr        $data = $this->validateAnimalData();
4649f2871cSAndreas Gohr        if (!$data) return;
471da41c8bSAndreas Gohr        if (
481da41c8bSAndreas Gohr            $this->createNewAnimal(
491da41c8bSAndreas Gohr                $data['name'],
501da41c8bSAndreas Gohr                $data['admin'],
511da41c8bSAndreas Gohr                $data['pass'],
521da41c8bSAndreas Gohr                $data['template'],
531da41c8bSAndreas Gohr                $data['aclpolicy'],
541da41c8bSAndreas Gohr                $data['allowreg']
551da41c8bSAndreas Gohr            )
561da41c8bSAndreas Gohr        ) {
570336ab2aSAndreas Gohr            $url = $this->helper->getAnimalURL($data['name']);
580336ab2aSAndreas Gohr            $link = '<a href="' . $url . '">' . hsc($data['name']) . '</a>';
590336ab2aSAndreas Gohr
600336ab2aSAndreas Gohr            msg(sprintf($this->getLang('animal creation success'), $link), 1);
611da41c8bSAndreas Gohr            $link = wl($ID, ['do' => 'admin', 'page' => 'farmer', 'sub' => 'new'], true, '&');
6249f2871cSAndreas Gohr            send_redirect($link);
6349f2871cSAndreas Gohr        }
6449f2871cSAndreas Gohr    }
6549f2871cSAndreas Gohr
6649f2871cSAndreas Gohr    /**
6749f2871cSAndreas Gohr     * Render HTML output, e.g. helpful text and a form
6849f2871cSAndreas Gohr     */
691da41c8bSAndreas Gohr    public function html()
701da41c8bSAndreas Gohr    {
716348ab5bSMichael Grosse        global $lang;
721272da0cSAndreas Gohr        $farmconfig = $this->helper->getConfig();
7349f2871cSAndreas Gohr
741da41c8bSAndreas Gohr        $form = new Form();
7549f2871cSAndreas Gohr        $form->addClass('plugin_farmer')->id('farmer__create_animal_form');
7649f2871cSAndreas Gohr
7749f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal configuration'));
7823164e01SAndreas Gohr        $form->addTextInput('animalname', $this->getLang('animal'));
7949f2871cSAndreas Gohr        $form->addFieldsetClose();
8049f2871cSAndreas Gohr
81801ebaa1SAndreas Gohr        $animals = $this->helper->getAllAnimals();
82801ebaa1SAndreas Gohr        array_unshift($animals, '');
83801ebaa1SAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal template'));
841da41c8bSAndreas Gohr        $form->addDropdown('animaltemplate', $animals)
851da41c8bSAndreas Gohr            ->addClass('farmer_chosen_animals');
86801ebaa1SAndreas Gohr        $form->addFieldsetClose();
87801ebaa1SAndreas Gohr
886348ab5bSMichael Grosse        $form->addFieldsetOpen($lang['i_policy'])->attr('id', 'aclPolicyFieldset');
891da41c8bSAndreas Gohr        $policyOptions = ['open' => $lang['i_pol0'], 'public' => $lang['i_pol1'], 'closed' => $lang['i_pol2']];
901da41c8bSAndreas Gohr        $form->addDropdown('aclpolicy', $policyOptions)
911da41c8bSAndreas Gohr            ->addClass('acl_chosen');
926348ab5bSMichael Grosse        if ($farmconfig['inherit']['main']) {
931da41c8bSAndreas Gohr            $form->addRadioButton('allowreg', $this->getLang('inherit user registration'))
941da41c8bSAndreas Gohr                ->val('inherit')
951da41c8bSAndreas Gohr                ->attr('checked', 'checked');
961da41c8bSAndreas Gohr            $form->addRadioButton('allowreg', $this->getLang('enable user registration'))
971da41c8bSAndreas Gohr                ->val('allow');
981da41c8bSAndreas Gohr            $form->addRadioButton('allowreg', $this->getLang('disable user registration'))
991da41c8bSAndreas Gohr                ->val('disable');
1006348ab5bSMichael Grosse        } else {
1011da41c8bSAndreas Gohr            $form->addCheckbox('allowreg', $lang['i_allowreg'])
1021da41c8bSAndreas Gohr                ->attr('checked', 'checked');
1036348ab5bSMichael Grosse        }
1046348ab5bSMichael Grosse
1056348ab5bSMichael Grosse        $form->addFieldsetClose();
1066348ab5bSMichael Grosse
10749f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal administrator'));
1081da41c8bSAndreas Gohr
1091da41c8bSAndreas Gohr        $btn = $form->addRadioButton('adminsetup', $this->getLang('noUsers'))
1101da41c8bSAndreas Gohr            ->val('noUsers');
1111272da0cSAndreas Gohr        if ($farmconfig['inherit']['users']) {
1121272da0cSAndreas Gohr            $btn->attr('checked', 'checked');  // default when inherit available
1131a130845SAndreas Gohr        } else {
1141a130845SAndreas Gohr            // no user copying when inheriting
1151da41c8bSAndreas Gohr            $form->addRadioButton('adminsetup', $this->getLang('importUsers'))
1161da41c8bSAndreas Gohr                ->val('importUsers');
1171da41c8bSAndreas Gohr            $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))
1181da41c8bSAndreas Gohr                ->val('currentAdmin');
1191a130845SAndreas Gohr        }
1201da41c8bSAndreas Gohr        $btn = $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))
1211da41c8bSAndreas Gohr            ->val('newAdmin');
1221272da0cSAndreas Gohr        if (!$farmconfig['inherit']['users']) {
1231272da0cSAndreas Gohr            $btn->attr('checked', 'checked'); // default when inherit not available
1241272da0cSAndreas Gohr        }
12549f2871cSAndreas Gohr        $form->addPasswordInput('adminPassword', $this->getLang('admin password'));
12649f2871cSAndreas Gohr        $form->addFieldsetClose();
12749f2871cSAndreas Gohr
1281da41c8bSAndreas Gohr        $form->addButton('farmer__submit', $this->getLang('submit'))
1291da41c8bSAndreas Gohr            ->attr('type', 'submit')
1301da41c8bSAndreas Gohr            ->val('newAnimal');
13149f2871cSAndreas Gohr        echo $form->toHTML();
13249f2871cSAndreas Gohr    }
13349f2871cSAndreas Gohr
13449f2871cSAndreas Gohr    /**
13549f2871cSAndreas Gohr     * Validate the data for a new animal
13649f2871cSAndreas Gohr     *
13749f2871cSAndreas Gohr     * @return array|bool false on errors, clean data otherwise
13849f2871cSAndreas Gohr     */
1391da41c8bSAndreas Gohr    protected function validateAnimalData()
1401da41c8bSAndreas Gohr    {
14149f2871cSAndreas Gohr        global $INPUT;
14249f2871cSAndreas Gohr
143*655fe6c1SAndreas Gohr        $animalname = $INPUT->filter('trim')->filter([PhpString::class, 'strtolower'])->str('animalname');
14449f2871cSAndreas Gohr        $adminsetup = $INPUT->str('adminsetup');
14549f2871cSAndreas Gohr        $adminpass = $INPUT->filter('trim')->str('adminPassword');
146801ebaa1SAndreas Gohr        $template = $INPUT->filter('trim')->str('animaltemplate');
1476348ab5bSMichael Grosse        $aclpolicy = $INPUT->filter('trim')->str('aclpolicy');
1486348ab5bSMichael Grosse        $allowreg = $INPUT->str('allowreg');
14949f2871cSAndreas Gohr
1501da41c8bSAndreas Gohr        $errors = [];
15149f2871cSAndreas Gohr
15249f2871cSAndreas Gohr        if ($animalname === '') {
15349f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_missing');
15449f2871cSAndreas Gohr        } elseif (!$this->helper->validateAnimalName($animalname)) {
15549f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_invalid');
15649f2871cSAndreas Gohr        }
15749f2871cSAndreas Gohr
15849f2871cSAndreas Gohr        if ($adminsetup === 'newAdmin' && $adminpass === '') {
15949f2871cSAndreas Gohr            $errors[] = $this->getLang('adminPassword_empty');
16049f2871cSAndreas Gohr        }
16149f2871cSAndreas Gohr
16249f2871cSAndreas Gohr        if ($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) {
16349f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_preexisting');
16449f2871cSAndreas Gohr        }
16549f2871cSAndreas Gohr
1661da41c8bSAndreas Gohr        if (!is_dir(DOKU_FARMDIR . $template) && !in_array($aclpolicy, ['open', 'public', 'closed'])) {
1676348ab5bSMichael Grosse            $errors[] = $this->getLang('aclpolicy missing/bad');
1686348ab5bSMichael Grosse        }
1696348ab5bSMichael Grosse
17049f2871cSAndreas Gohr        if ($errors) {
17149f2871cSAndreas Gohr            foreach ($errors as $error) {
17249f2871cSAndreas Gohr                msg($error, -1);
17349f2871cSAndreas Gohr            }
17449f2871cSAndreas Gohr            return false;
17549f2871cSAndreas Gohr        }
17649f2871cSAndreas Gohr
1778262a4cbSAndreas Gohr        if (!is_dir(DOKU_FARMDIR . $template)) {
178801ebaa1SAndreas Gohr            $template = '';
179801ebaa1SAndreas Gohr        }
1806348ab5bSMichael Grosse        if ($template != '') {
1816348ab5bSMichael Grosse            $aclpolicy = '';
1826348ab5bSMichael Grosse        }
183801ebaa1SAndreas Gohr
1841da41c8bSAndreas Gohr        return [
18549f2871cSAndreas Gohr            'name' => $animalname,
18649f2871cSAndreas Gohr            'admin' => $adminsetup,
187801ebaa1SAndreas Gohr            'pass' => $adminpass,
1886348ab5bSMichael Grosse            'template' => $template,
1896348ab5bSMichael Grosse            'aclpolicy' => $aclpolicy,
1906348ab5bSMichael Grosse            'allowreg' => $allowreg
1911da41c8bSAndreas Gohr        ];
19249f2871cSAndreas Gohr    }
19349f2871cSAndreas Gohr
19449f2871cSAndreas Gohr    /**
19549f2871cSAndreas Gohr     * Create a new animal
19649f2871cSAndreas Gohr     *
19749f2871cSAndreas Gohr     * @param string $name name/title of the animal, will be the directory name for htaccess setup
19849f2871cSAndreas Gohr     * @param string $adminSetup newAdmin, currentAdmin or importUsers
19949f2871cSAndreas Gohr     * @param string $adminPassword required if $adminSetup is newAdmin
200801ebaa1SAndreas Gohr     * @param string $template name of animal to copy
2016348ab5bSMichael Grosse     * @param $aclpolicy
2026348ab5bSMichael Grosse     * @param $userreg
20349f2871cSAndreas Gohr     * @return bool true if successful
2046348ab5bSMichael Grosse     * @throws Exception
20549f2871cSAndreas Gohr     */
2061da41c8bSAndreas Gohr    protected function createNewAnimal($name, $adminSetup, $adminPassword, $template, $aclpolicy, $userreg)
2071da41c8bSAndreas Gohr    {
2088262a4cbSAndreas Gohr        $animaldir = DOKU_FARMDIR . $name;
20949f2871cSAndreas Gohr
21049f2871cSAndreas Gohr        // copy basic template
2111da41c8bSAndreas Gohr        $ok = $this->helper->copyDir(__DIR__ . '/../_animal', $animaldir);
21249f2871cSAndreas Gohr        if (!$ok) {
21349f2871cSAndreas Gohr            msg($this->getLang('animal creation error'), -1);
21449f2871cSAndreas Gohr            return false;
21549f2871cSAndreas Gohr        }
21649f2871cSAndreas Gohr
217801ebaa1SAndreas Gohr        // copy animal template
218801ebaa1SAndreas Gohr        if ($template != '') {
2191da41c8bSAndreas Gohr            foreach (['conf', 'data/pages', 'data/media', 'data/meta', 'data/media_meta', 'index'] as $dir) {
2208262a4cbSAndreas Gohr                $templatedir = DOKU_FARMDIR . $template . '/' . $dir;
221801ebaa1SAndreas Gohr                if (!is_dir($templatedir)) continue;
222801ebaa1SAndreas Gohr                // do not copy changelogs in meta
223f64a85f7SAndreas Gohr                if (str_ends_with($dir, 'meta')) {
224801ebaa1SAndreas Gohr                    $exclude = '/\.changes$/';
225801ebaa1SAndreas Gohr                } else {
226801ebaa1SAndreas Gohr                    $exclude = '';
227801ebaa1SAndreas Gohr                }
2281da41c8bSAndreas Gohr                if (!$this->helper->copyDir($templatedir, $animaldir . '/' . $dir, $exclude)) {
229801ebaa1SAndreas Gohr                    msg(sprintf($this->getLang('animal template copy error'), $dir), -1);
230801ebaa1SAndreas Gohr                    // we go on anyway
231801ebaa1SAndreas Gohr                }
232801ebaa1SAndreas Gohr            }
233801ebaa1SAndreas Gohr        }
234801ebaa1SAndreas Gohr
23549f2871cSAndreas Gohr        // append title to local config
23649f2871cSAndreas Gohr        $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'title\'] = \'' . $name . '\';' . "\n", true);
23749f2871cSAndreas Gohr
2384eba53bcSAndreas Gohr        // create a random logo and favicon
2394eba53bcSAndreas Gohr        if (!class_exists('\splitbrain\RingIcon\RingIcon', false)) {
2404eba53bcSAndreas Gohr            require(__DIR__ . '/../3rdparty/RingIcon.php');
2414eba53bcSAndreas Gohr        }
2424eba53bcSAndreas Gohr        if (!class_exists('\chrisbliss18\phpico\PHPIco', false)) {
2434eba53bcSAndreas Gohr            require(__DIR__ . '/../3rdparty/PHPIco.php');
2444eba53bcSAndreas Gohr        }
2454eba53bcSAndreas Gohr        try {
246b263debeSAndreas Gohr            if (function_exists('imagecreatetruecolor')) {
247c3bd7eb3SAndreas Gohr                $logo = $animaldir . '/data/media/wiki/logo.png';
248c3bd7eb3SAndreas Gohr                if (!file_exists($logo)) {
2491da41c8bSAndreas Gohr                    $ringicon = new RingIcon(64);
250c3bd7eb3SAndreas Gohr                    $ringicon->createImage($animaldir, $logo);
251c3bd7eb3SAndreas Gohr                }
252c3bd7eb3SAndreas Gohr
253c3bd7eb3SAndreas Gohr                $icon = $animaldir . '/data/media/wiki/favicon.ico';
254c3bd7eb3SAndreas Gohr                if (!file_exists($icon)) {
2551da41c8bSAndreas Gohr                    $icongen = new PHPIco($logo);
2561da41c8bSAndreas Gohr                    $icongen->saveIco($icon);
257c3bd7eb3SAndreas Gohr                }
258b263debeSAndreas Gohr            }
2594eba53bcSAndreas Gohr        } catch (\Exception $ignore) {
2604eba53bcSAndreas Gohr            // something went wrong, but we don't care. this is a nice to have feature only
2614eba53bcSAndreas Gohr        }
26249f2871cSAndreas Gohr
26349f2871cSAndreas Gohr        // create admin user
26449f2871cSAndreas Gohr        if ($adminSetup === 'newAdmin') {
26549f2871cSAndreas Gohr            $users = "# <?php exit()?>\n" . $this->makeAdminLine($adminPassword) . "\n";
26649f2871cSAndreas Gohr        } elseif ($adminSetup === 'currentAdmin') {
26749f2871cSAndreas Gohr            $users = "# <?php exit()?>\n" . $this->getAdminLine() . "\n";
2681272da0cSAndreas Gohr        } elseif ($adminSetup === 'noUsers') {
269801ebaa1SAndreas Gohr            if (file_exists($animaldir . '/conf/users.auth.php')) {
270801ebaa1SAndreas Gohr                // a user file exists already, probably from animal template - don't overwrite
271801ebaa1SAndreas Gohr                $users = '';
272801ebaa1SAndreas Gohr            } else {
273801ebaa1SAndreas Gohr                // create empty user file
2741272da0cSAndreas Gohr                $users = "# <?php exit()?>\n";
275801ebaa1SAndreas Gohr            }
27649f2871cSAndreas Gohr        } else {
27749f2871cSAndreas Gohr            $users = io_readFile(DOKU_CONF . 'users.auth.php');
27849f2871cSAndreas Gohr        }
279801ebaa1SAndreas Gohr        if ($users) {
28049f2871cSAndreas Gohr            $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users);
281801ebaa1SAndreas Gohr        }
28249f2871cSAndreas Gohr
2836348ab5bSMichael Grosse        if ($aclpolicy != '') {
2846348ab5bSMichael Grosse            $aclfile = file($animaldir . '/conf/acl.auth.php');
2852b85f30dSAndreas Gohr            $aclfile = array_map('trim', $aclfile);
2866348ab5bSMichael Grosse            array_pop($aclfile);
2876348ab5bSMichael Grosse            switch ($aclpolicy) {
2886348ab5bSMichael Grosse                case 'open':
2896348ab5bSMichael Grosse                    $aclfile[] = "* @ALL 8";
2906348ab5bSMichael Grosse                    break;
2916348ab5bSMichael Grosse                case 'public':
2926348ab5bSMichael Grosse                    $aclfile[] = "* @ALL 1";
2936348ab5bSMichael Grosse                    $aclfile[] = "* @user 8";
2946348ab5bSMichael Grosse                    break;
2956348ab5bSMichael Grosse                case 'closed':
2966348ab5bSMichael Grosse                    $aclfile[] = "* @ALL 0";
2976348ab5bSMichael Grosse                    $aclfile[] = "* @user 8";
2986348ab5bSMichael Grosse                    break;
2996348ab5bSMichael Grosse                default:
3006348ab5bSMichael Grosse                    throw new Exception('Undefined aclpolicy given');
3016348ab5bSMichael Grosse            }
3021da41c8bSAndreas Gohr            $ok &= io_saveFile($animaldir . '/conf/acl.auth.php', implode("\n", $aclfile) . "\n");
3036348ab5bSMichael Grosse
3046348ab5bSMichael Grosse            global $conf;
3056348ab5bSMichael Grosse            switch ($userreg) {
3066348ab5bSMichael Grosse                case 'allow':
3071da41c8bSAndreas Gohr                    $disableactions = implode(',', array_diff(explode(',', $conf['disableactions']), ['register']));
3081da41c8bSAndreas Gohr                    $ok &= io_saveFile(
3091da41c8bSAndreas Gohr                        $animaldir . '/conf/local.php',
3101da41c8bSAndreas Gohr                        "\n" . '$conf[\'disableactions\'] = \'' . $disableactions . '\';' . "\n",
3111da41c8bSAndreas Gohr                        true
3121da41c8bSAndreas Gohr                    );
3136348ab5bSMichael Grosse                    break;
3146348ab5bSMichael Grosse                case 'disable':
3151da41c8bSAndreas Gohr                    $disableactions = implode(',', array_merge(explode(',', $conf['disableactions']), ['register']));
3161da41c8bSAndreas Gohr                    $ok &= io_saveFile(
3171da41c8bSAndreas Gohr                        $animaldir . '/conf/local.php',
3181da41c8bSAndreas Gohr                        "\n" . '$conf[\'disableactions\'] = \'' . $disableactions . '\';' . "\n",
3191da41c8bSAndreas Gohr                        true
3201da41c8bSAndreas Gohr                    );
3216348ab5bSMichael Grosse                    break;
3226348ab5bSMichael Grosse                case 'inherit':
3236348ab5bSMichael Grosse                case true:
3246348ab5bSMichael Grosse                    // nothing needs to be done
3256348ab5bSMichael Grosse                    break;
3266348ab5bSMichael Grosse                default:
3271da41c8bSAndreas Gohr                    $ok &= io_saveFile(
3281da41c8bSAndreas Gohr                        $animaldir . '/conf/local.php',
3291da41c8bSAndreas Gohr                        "\n" . '$conf[\'disableactions\'] = \'register\';' . "\n",
3301da41c8bSAndreas Gohr                        true
3311da41c8bSAndreas Gohr                    );
3326348ab5bSMichael Grosse            }
3336348ab5bSMichael Grosse        }
3346348ab5bSMichael Grosse
3354664a1d2SAndreas Gohr        // deactivate plugins by default FIXME this should be nicer
33649f2871cSAndreas Gohr        $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins'));
3374664a1d2SAndreas Gohr        $deactivatedPluginsList = array_map('trim', $deactivatedPluginsList);
3384664a1d2SAndreas Gohr        $deactivatedPluginsList = array_unique($deactivatedPluginsList);
3394664a1d2SAndreas Gohr        $deactivatedPluginsList = array_filter($deactivatedPluginsList);
34049f2871cSAndreas Gohr        foreach ($deactivatedPluginsList as $plugin) {
341af1c6dd8SAndreas Gohr            $this->helper->setPluginState(trim($plugin), $name, 0);
34249f2871cSAndreas Gohr        }
34349f2871cSAndreas Gohr
34449f2871cSAndreas Gohr        return $ok;
34549f2871cSAndreas Gohr    }
34649f2871cSAndreas Gohr
34749f2871cSAndreas Gohr    /**
34849f2871cSAndreas Gohr     * Creates a new user line
34949f2871cSAndreas Gohr     *
35049f2871cSAndreas Gohr     * @param $password
35149f2871cSAndreas Gohr     * @return string
35249f2871cSAndreas Gohr     */
3531da41c8bSAndreas Gohr    protected function makeAdminLine($password)
3541da41c8bSAndreas Gohr    {
35549f2871cSAndreas Gohr        $pass = auth_cryptPassword($password);
3561da41c8bSAndreas Gohr        $line = implode(
3571da41c8bSAndreas Gohr            ':',
3581da41c8bSAndreas Gohr            ['admin', $pass, 'Administrator', 'admin@example.org', 'admin,user']
3591272da0cSAndreas Gohr        );
36049f2871cSAndreas Gohr        return $line;
36149f2871cSAndreas Gohr    }
36249f2871cSAndreas Gohr
36349f2871cSAndreas Gohr    /**
36449f2871cSAndreas Gohr     * Copies the current user as new admin line
36549f2871cSAndreas Gohr     *
36649f2871cSAndreas Gohr     * @return string
36749f2871cSAndreas Gohr     */
3681da41c8bSAndreas Gohr    protected function getAdminLine()
3691da41c8bSAndreas Gohr    {
37049f2871cSAndreas Gohr        $currentAdmin = $_SERVER['REMOTE_USER'];
37149f2871cSAndreas Gohr        $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php');
37249f2871cSAndreas Gohr        $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":"));
3731da41c8bSAndreas Gohr
37449f2871cSAndreas Gohr        $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1);
37549f2871cSAndreas Gohr        return $newAdmin;
37649f2871cSAndreas Gohr    }
37749f2871cSAndreas Gohr}
378