xref: /plugin/farmer/admin/new.php (revision 4eba53bcd5b3e3927139e95a0370dbb66eb44da1)
149f2871cSAndreas Gohr<?php
249f2871cSAndreas Gohr/**
349f2871cSAndreas Gohr * DokuWiki Plugin farmer (Admin Component)
449f2871cSAndreas Gohr *
549f2871cSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
649f2871cSAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
749f2871cSAndreas Gohr */
849f2871cSAndreas Gohr
949f2871cSAndreas Gohr// must be run within Dokuwiki
1049f2871cSAndreas Gohrif(!defined('DOKU_INC')) die();
1149f2871cSAndreas Gohr
1249f2871cSAndreas Gohrclass admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
1349f2871cSAndreas Gohr
1449f2871cSAndreas Gohr    /** @var helper_plugin_farmer $helper */
1549f2871cSAndreas Gohr    protected $helper;
1649f2871cSAndreas Gohr
1749f2871cSAndreas Gohr    /**
1849f2871cSAndreas Gohr     * @return bool true if only access for superuser, false is for superusers and moderators
1949f2871cSAndreas Gohr     */
2049f2871cSAndreas Gohr    public function forAdminOnly() {
2149f2871cSAndreas Gohr        return true;
2249f2871cSAndreas Gohr    }
2349f2871cSAndreas Gohr
2449f2871cSAndreas Gohr    /**
2549f2871cSAndreas Gohr     * admin_plugin_farmer_new constructor.
2649f2871cSAndreas Gohr     */
2749f2871cSAndreas Gohr    public function __construct() {
2849f2871cSAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
2949f2871cSAndreas Gohr    }
3049f2871cSAndreas Gohr
3149f2871cSAndreas Gohr    /**
3249f2871cSAndreas Gohr     * Should carry out any processing required by the plugin.
3349f2871cSAndreas Gohr     */
3449f2871cSAndreas Gohr    public function handle() {
3549f2871cSAndreas Gohr        global $INPUT;
3649f2871cSAndreas Gohr        global $ID;
3749f2871cSAndreas Gohr        if(!$INPUT->has('farmer__submit')) return;
3849f2871cSAndreas Gohr
3949f2871cSAndreas Gohr        $data = $this->validateAnimalData();
4049f2871cSAndreas Gohr        if(!$data) return;
4149f2871cSAndreas Gohr        if($this->createNewAnimal($data['name'], $data['admin'], $data['pass'])){
4249f2871cSAndreas Gohr            msg($this->getLang('animal creation success'), 1);
4349f2871cSAndreas Gohr            $link = wl($ID, array('do'=>'admin', 'page'=>'farmer', 'sub'=>'new'), true, '&');
4449f2871cSAndreas Gohr            send_redirect($link);
4549f2871cSAndreas Gohr        }
4649f2871cSAndreas Gohr    }
4749f2871cSAndreas Gohr
4849f2871cSAndreas Gohr    /**
4949f2871cSAndreas Gohr     * Render HTML output, e.g. helpful text and a form
5049f2871cSAndreas Gohr     */
5149f2871cSAndreas Gohr    public function html() {
5249f2871cSAndreas Gohr
5349f2871cSAndreas Gohr        $form = new \dokuwiki\Form\Form();
5449f2871cSAndreas Gohr        $form->addClass('plugin_farmer')->id('farmer__create_animal_form');
5549f2871cSAndreas Gohr
5649f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal configuration'));
5749f2871cSAndreas Gohr        $form->addTextInput('animalname', $this->getLang('animal name'));
5849f2871cSAndreas Gohr        $form->addFieldsetClose();
5949f2871cSAndreas Gohr
6049f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal administrator'));
6149f2871cSAndreas Gohr        $form->addRadioButton('adminsetup', $this->getLang('importUsers'))->val('importUsers');
6249f2871cSAndreas Gohr        $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))->val('currentAdmin');
6349f2871cSAndreas Gohr        $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))->val('newAdmin')->attr('checked', 'checked');
6449f2871cSAndreas Gohr        $form->addPasswordInput('adminPassword', $this->getLang('admin password'));
6549f2871cSAndreas Gohr        $form->addFieldsetClose();
6649f2871cSAndreas Gohr
6749f2871cSAndreas Gohr        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit')->val('newAnimal');
6849f2871cSAndreas Gohr        echo $form->toHTML();
6949f2871cSAndreas Gohr
7049f2871cSAndreas Gohr        echo $this->locale_xhtml('tab_new_help');
7149f2871cSAndreas Gohr    }
7249f2871cSAndreas Gohr
7349f2871cSAndreas Gohr    /**
7449f2871cSAndreas Gohr     * Validate the data for a new animal
7549f2871cSAndreas Gohr     *
7649f2871cSAndreas Gohr     * @return array|bool false on errors, clean data otherwise
7749f2871cSAndreas Gohr     */
7849f2871cSAndreas Gohr    protected function validateAnimalData() {
7949f2871cSAndreas Gohr        global $INPUT;
8049f2871cSAndreas Gohr
8149f2871cSAndreas Gohr        $animalname = $INPUT->filter('trim')->str('animalname');
8249f2871cSAndreas Gohr        $adminsetup = $INPUT->str('adminsetup');
8349f2871cSAndreas Gohr        $adminpass = $INPUT->filter('trim')->str('adminPassword');
8449f2871cSAndreas Gohr
8549f2871cSAndreas Gohr        $errors = array();
8649f2871cSAndreas Gohr
8749f2871cSAndreas Gohr        if($animalname === '') {
8849f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_missing');
8949f2871cSAndreas Gohr        } elseif(!$this->helper->validateAnimalName($animalname)) {
9049f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_invalid');
9149f2871cSAndreas Gohr        }
9249f2871cSAndreas Gohr
9349f2871cSAndreas Gohr        if($adminsetup === 'newAdmin' && $adminpass === '') {
9449f2871cSAndreas Gohr            $errors[] = $this->getLang('adminPassword_empty');
9549f2871cSAndreas Gohr        }
9649f2871cSAndreas Gohr
9749f2871cSAndreas Gohr        if($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) {
9849f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_preexisting');
9949f2871cSAndreas Gohr        }
10049f2871cSAndreas Gohr
10149f2871cSAndreas Gohr        if($errors) {
10249f2871cSAndreas Gohr            foreach($errors as $error) {
10349f2871cSAndreas Gohr                msg($error, -1);
10449f2871cSAndreas Gohr            }
10549f2871cSAndreas Gohr            return false;
10649f2871cSAndreas Gohr        }
10749f2871cSAndreas Gohr
10849f2871cSAndreas Gohr        return array(
10949f2871cSAndreas Gohr            'name' => $animalname,
11049f2871cSAndreas Gohr            'admin' => $adminsetup,
11149f2871cSAndreas Gohr            'pass' => $adminpass
11249f2871cSAndreas Gohr        );
11349f2871cSAndreas Gohr    }
11449f2871cSAndreas Gohr
11549f2871cSAndreas Gohr    /**
11649f2871cSAndreas Gohr     * Create a new animal
11749f2871cSAndreas Gohr     *
11849f2871cSAndreas Gohr     * @param string $name name/title of the animal, will be the directory name for htaccess setup
11949f2871cSAndreas Gohr     * @param string $adminSetup newAdmin, currentAdmin or importUsers
12049f2871cSAndreas Gohr     * @param string $adminPassword required if $adminSetup is newAdmin
12149f2871cSAndreas Gohr     * @return bool true if successful
12249f2871cSAndreas Gohr     */
12349f2871cSAndreas Gohr    protected function createNewAnimal($name, $adminSetup, $adminPassword) {
12449f2871cSAndreas Gohr        $animaldir = DOKU_FARMDIR . '/' . $name;
12549f2871cSAndreas Gohr
12649f2871cSAndreas Gohr        // copy basic template
12749f2871cSAndreas Gohr        $ok = $this->helper->io_copyDir(__DIR__ . '/../_animal', $animaldir);
12849f2871cSAndreas Gohr        if(!$ok) {
12949f2871cSAndreas Gohr            msg($this->getLang('animal creation error'), -1);
13049f2871cSAndreas Gohr            return false;
13149f2871cSAndreas Gohr        }
13249f2871cSAndreas Gohr
13349f2871cSAndreas Gohr        // append title to local config
13449f2871cSAndreas Gohr        $ok &= io_saveFile($animaldir.'/conf/local.php', "\n".'$conf[\'title\'] = \''.$name.'\';'."\n", true);
13549f2871cSAndreas Gohr
136*4eba53bcSAndreas Gohr        // create a random logo and favicon
137*4eba53bcSAndreas Gohr        if(!class_exists('\splitbrain\RingIcon\RingIcon', false)) {
138*4eba53bcSAndreas Gohr            require(__DIR__ . '/../3rdparty/RingIcon.php');
139*4eba53bcSAndreas Gohr        }
140*4eba53bcSAndreas Gohr        if(!class_exists('\chrisbliss18\phpico\PHPIco', false)) {
141*4eba53bcSAndreas Gohr            require(__DIR__ . '/../3rdparty/PHPIco.php');
142*4eba53bcSAndreas Gohr        }
143*4eba53bcSAndreas Gohr        try {
144*4eba53bcSAndreas Gohr            $ringicon = new \splitbrain\RingIcon\RingIcon(64);
145*4eba53bcSAndreas Gohr            $ringicon->createImage($animaldir, $animaldir . '/data/media/wiki/logo.png');
146*4eba53bcSAndreas Gohr            $icongen = new \chrisbliss18\phpico\PHPIco($animaldir . '/data/media/wiki/logo.png');
147*4eba53bcSAndreas Gohr            $icongen->save_ico($animaldir . '/data/media/wiki/favicon.ico');
148*4eba53bcSAndreas Gohr        } catch(\Exception $ignore) {
149*4eba53bcSAndreas Gohr            // something went wrong, but we don't care. this is a nice to have feature only
150*4eba53bcSAndreas Gohr        }
15149f2871cSAndreas Gohr
15249f2871cSAndreas Gohr        // create admin user
15349f2871cSAndreas Gohr        if($adminSetup === 'newAdmin') {
15449f2871cSAndreas Gohr            $users = "# <?php exit()?>\n".$this->makeAdminLine($adminPassword)."\n";
15549f2871cSAndreas Gohr        } elseif($adminSetup === 'currentAdmin') {
15649f2871cSAndreas Gohr            $users = "# <?php exit()?>\n".$this->getAdminLine()."\n";
15749f2871cSAndreas Gohr        } else {
15849f2871cSAndreas Gohr            $users = io_readFile(DOKU_CONF . 'users.auth.php');
15949f2871cSAndreas Gohr        }
16049f2871cSAndreas Gohr        $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users);
16149f2871cSAndreas Gohr
16249f2871cSAndreas Gohr        /* FIXME handle deactivated plugins
16349f2871cSAndreas Gohr        if($this->getConf('deactivated plugins') === '') {
16449f2871cSAndreas Gohr            $deactivatedPluginsList = array('farmer',);
16549f2871cSAndreas Gohr        } else {
16649f2871cSAndreas Gohr            $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins'));
16749f2871cSAndreas Gohr            array_push($deactivatedPluginsList, 'farmer');
16849f2871cSAndreas Gohr        }
16949f2871cSAndreas Gohr        foreach($deactivatedPluginsList as $plugin) {
17049f2871cSAndreas Gohr            $this->helper->deactivatePlugin(trim($plugin), $animal);
17149f2871cSAndreas Gohr        }
17249f2871cSAndreas Gohr        */
17349f2871cSAndreas Gohr
17449f2871cSAndreas Gohr        return $ok;
17549f2871cSAndreas Gohr    }
17649f2871cSAndreas Gohr
17749f2871cSAndreas Gohr    /**
17849f2871cSAndreas Gohr     * Creates a new user line
17949f2871cSAndreas Gohr     *
18049f2871cSAndreas Gohr     * @param $password
18149f2871cSAndreas Gohr     * @return string
18249f2871cSAndreas Gohr     */
18349f2871cSAndreas Gohr    protected function makeAdminLine($password) {
18449f2871cSAndreas Gohr        $pass = auth_cryptPassword($password);
18549f2871cSAndreas Gohr        $line = join("\t", array(
18649f2871cSAndreas Gohr            'admin',
18749f2871cSAndreas Gohr            $pass,
18849f2871cSAndreas Gohr            'Administrator',
18949f2871cSAndreas Gohr            'admin@example.org',
19049f2871cSAndreas Gohr            'admin,user'
19149f2871cSAndreas Gohr        ));
19249f2871cSAndreas Gohr        return $line;
19349f2871cSAndreas Gohr    }
19449f2871cSAndreas Gohr
19549f2871cSAndreas Gohr    /**
19649f2871cSAndreas Gohr     * Copies the current user as new admin line
19749f2871cSAndreas Gohr     *
19849f2871cSAndreas Gohr     * @return string
19949f2871cSAndreas Gohr     */
20049f2871cSAndreas Gohr    protected function getAdminLine() {
20149f2871cSAndreas Gohr        $currentAdmin = $_SERVER['REMOTE_USER'];
20249f2871cSAndreas Gohr        $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php');
20349f2871cSAndreas Gohr        $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":"));
20449f2871cSAndreas Gohr        $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1);
20549f2871cSAndreas Gohr        return $newAdmin;
20649f2871cSAndreas Gohr    }
20749f2871cSAndreas Gohr
20849f2871cSAndreas Gohr
20949f2871cSAndreas Gohr}
21049f2871cSAndreas Gohr
21149f2871cSAndreas Gohr// vim:ts=4:sw=4:et:
212