xref: /plugin/farmer/admin/new.php (revision 49f2871cf739ba9d87eb5b5878cdd7066d4018b8)
1*49f2871cSAndreas Gohr<?php
2*49f2871cSAndreas Gohr/**
3*49f2871cSAndreas Gohr * DokuWiki Plugin farmer (Admin Component)
4*49f2871cSAndreas Gohr *
5*49f2871cSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6*49f2871cSAndreas Gohr * @author  Michael Große <grosse@cosmocode.de>
7*49f2871cSAndreas Gohr */
8*49f2871cSAndreas Gohr
9*49f2871cSAndreas Gohr// must be run within Dokuwiki
10*49f2871cSAndreas Gohrif(!defined('DOKU_INC')) die();
11*49f2871cSAndreas Gohr
12*49f2871cSAndreas Gohrclass admin_plugin_farmer_new extends DokuWiki_Admin_Plugin {
13*49f2871cSAndreas Gohr
14*49f2871cSAndreas Gohr    /** @var helper_plugin_farmer $helper */
15*49f2871cSAndreas Gohr    protected $helper;
16*49f2871cSAndreas Gohr
17*49f2871cSAndreas Gohr    /**
18*49f2871cSAndreas Gohr     * @return bool true if only access for superuser, false is for superusers and moderators
19*49f2871cSAndreas Gohr     */
20*49f2871cSAndreas Gohr    public function forAdminOnly() {
21*49f2871cSAndreas Gohr        return true;
22*49f2871cSAndreas Gohr    }
23*49f2871cSAndreas Gohr
24*49f2871cSAndreas Gohr    /**
25*49f2871cSAndreas Gohr     * admin_plugin_farmer_new constructor.
26*49f2871cSAndreas Gohr     */
27*49f2871cSAndreas Gohr    public function __construct() {
28*49f2871cSAndreas Gohr        $this->helper = plugin_load('helper', 'farmer');
29*49f2871cSAndreas Gohr    }
30*49f2871cSAndreas Gohr
31*49f2871cSAndreas Gohr    /**
32*49f2871cSAndreas Gohr     * Should carry out any processing required by the plugin.
33*49f2871cSAndreas Gohr     */
34*49f2871cSAndreas Gohr    public function handle() {
35*49f2871cSAndreas Gohr        global $INPUT;
36*49f2871cSAndreas Gohr        global $ID;
37*49f2871cSAndreas Gohr        if(!$INPUT->has('farmer__submit')) return;
38*49f2871cSAndreas Gohr
39*49f2871cSAndreas Gohr        $data = $this->validateAnimalData();
40*49f2871cSAndreas Gohr        if(!$data) return;
41*49f2871cSAndreas Gohr        if($this->createNewAnimal($data['name'], $data['admin'], $data['pass'])){
42*49f2871cSAndreas Gohr            msg($this->getLang('animal creation success'), 1);
43*49f2871cSAndreas Gohr            $link = wl($ID, array('do'=>'admin', 'page'=>'farmer', 'sub'=>'new'), true, '&');
44*49f2871cSAndreas Gohr            send_redirect($link);
45*49f2871cSAndreas Gohr        }
46*49f2871cSAndreas Gohr    }
47*49f2871cSAndreas Gohr
48*49f2871cSAndreas Gohr    /**
49*49f2871cSAndreas Gohr     * Render HTML output, e.g. helpful text and a form
50*49f2871cSAndreas Gohr     */
51*49f2871cSAndreas Gohr    public function html() {
52*49f2871cSAndreas Gohr
53*49f2871cSAndreas Gohr        $form = new \dokuwiki\Form\Form();
54*49f2871cSAndreas Gohr        $form->addClass('plugin_farmer')->id('farmer__create_animal_form');
55*49f2871cSAndreas Gohr
56*49f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal configuration'));
57*49f2871cSAndreas Gohr        $form->addTextInput('animalname', $this->getLang('animal name'));
58*49f2871cSAndreas Gohr        $form->addFieldsetClose();
59*49f2871cSAndreas Gohr
60*49f2871cSAndreas Gohr        $form->addFieldsetOpen($this->getLang('animal administrator'));
61*49f2871cSAndreas Gohr        $form->addRadioButton('adminsetup', $this->getLang('importUsers'))->val('importUsers');
62*49f2871cSAndreas Gohr        $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))->val('currentAdmin');
63*49f2871cSAndreas Gohr        $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))->val('newAdmin')->attr('checked', 'checked');
64*49f2871cSAndreas Gohr        $form->addPasswordInput('adminPassword', $this->getLang('admin password'));
65*49f2871cSAndreas Gohr        $form->addFieldsetClose();
66*49f2871cSAndreas Gohr
67*49f2871cSAndreas Gohr        $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit')->val('newAnimal');
68*49f2871cSAndreas Gohr        echo $form->toHTML();
69*49f2871cSAndreas Gohr
70*49f2871cSAndreas Gohr        echo $this->locale_xhtml('tab_new_help');
71*49f2871cSAndreas Gohr    }
72*49f2871cSAndreas Gohr
73*49f2871cSAndreas Gohr    /**
74*49f2871cSAndreas Gohr     * Validate the data for a new animal
75*49f2871cSAndreas Gohr     *
76*49f2871cSAndreas Gohr     * @return array|bool false on errors, clean data otherwise
77*49f2871cSAndreas Gohr     */
78*49f2871cSAndreas Gohr    protected function validateAnimalData() {
79*49f2871cSAndreas Gohr        global $INPUT;
80*49f2871cSAndreas Gohr
81*49f2871cSAndreas Gohr        $animalname = $INPUT->filter('trim')->str('animalname');
82*49f2871cSAndreas Gohr        $adminsetup = $INPUT->str('adminsetup');
83*49f2871cSAndreas Gohr        $adminpass = $INPUT->filter('trim')->str('adminPassword');
84*49f2871cSAndreas Gohr
85*49f2871cSAndreas Gohr        $errors = array();
86*49f2871cSAndreas Gohr
87*49f2871cSAndreas Gohr        if($animalname === '') {
88*49f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_missing');
89*49f2871cSAndreas Gohr        } elseif(!$this->helper->validateAnimalName($animalname)) {
90*49f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_invalid');
91*49f2871cSAndreas Gohr        }
92*49f2871cSAndreas Gohr
93*49f2871cSAndreas Gohr        if($adminsetup === 'newAdmin' && $adminpass === '') {
94*49f2871cSAndreas Gohr            $errors[] = $this->getLang('adminPassword_empty');
95*49f2871cSAndreas Gohr        }
96*49f2871cSAndreas Gohr
97*49f2871cSAndreas Gohr        if($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) {
98*49f2871cSAndreas Gohr            $errors[] = $this->getLang('animalname_preexisting');
99*49f2871cSAndreas Gohr        }
100*49f2871cSAndreas Gohr
101*49f2871cSAndreas Gohr        if($errors) {
102*49f2871cSAndreas Gohr            foreach($errors as $error) {
103*49f2871cSAndreas Gohr                msg($error, -1);
104*49f2871cSAndreas Gohr            }
105*49f2871cSAndreas Gohr            return false;
106*49f2871cSAndreas Gohr        }
107*49f2871cSAndreas Gohr
108*49f2871cSAndreas Gohr        return array(
109*49f2871cSAndreas Gohr            'name' => $animalname,
110*49f2871cSAndreas Gohr            'admin' => $adminsetup,
111*49f2871cSAndreas Gohr            'pass' => $adminpass
112*49f2871cSAndreas Gohr        );
113*49f2871cSAndreas Gohr    }
114*49f2871cSAndreas Gohr
115*49f2871cSAndreas Gohr    /**
116*49f2871cSAndreas Gohr     * Create a new animal
117*49f2871cSAndreas Gohr     *
118*49f2871cSAndreas Gohr     * @param string $name name/title of the animal, will be the directory name for htaccess setup
119*49f2871cSAndreas Gohr     * @param string $adminSetup newAdmin, currentAdmin or importUsers
120*49f2871cSAndreas Gohr     * @param string $adminPassword required if $adminSetup is newAdmin
121*49f2871cSAndreas Gohr     * @return bool true if successful
122*49f2871cSAndreas Gohr     */
123*49f2871cSAndreas Gohr    protected function createNewAnimal($name, $adminSetup, $adminPassword) {
124*49f2871cSAndreas Gohr        $animaldir = DOKU_FARMDIR . '/' . $name;
125*49f2871cSAndreas Gohr
126*49f2871cSAndreas Gohr        // copy basic template
127*49f2871cSAndreas Gohr        $ok = $this->helper->io_copyDir(__DIR__ . '/../_animal', $animaldir);
128*49f2871cSAndreas Gohr        if(!$ok) {
129*49f2871cSAndreas Gohr            msg($this->getLang('animal creation error'), -1);
130*49f2871cSAndreas Gohr            return false;
131*49f2871cSAndreas Gohr        }
132*49f2871cSAndreas Gohr
133*49f2871cSAndreas Gohr        // append title to local config
134*49f2871cSAndreas Gohr        $ok &= io_saveFile($animaldir.'/conf/local.php', "\n".'$conf[\'title\'] = \''.$name.'\';'."\n", true);
135*49f2871cSAndreas Gohr
136*49f2871cSAndreas Gohr        // fixme add logo
137*49f2871cSAndreas Gohr
138*49f2871cSAndreas Gohr        // create admin user
139*49f2871cSAndreas Gohr        if($adminSetup === 'newAdmin') {
140*49f2871cSAndreas Gohr            $users = "# <?php exit()?>\n".$this->makeAdminLine($adminPassword)."\n";
141*49f2871cSAndreas Gohr        } elseif($adminSetup === 'currentAdmin') {
142*49f2871cSAndreas Gohr            $users = "# <?php exit()?>\n".$this->getAdminLine()."\n";
143*49f2871cSAndreas Gohr        } else {
144*49f2871cSAndreas Gohr            $users = io_readFile(DOKU_CONF . 'users.auth.php');
145*49f2871cSAndreas Gohr        }
146*49f2871cSAndreas Gohr        $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users);
147*49f2871cSAndreas Gohr
148*49f2871cSAndreas Gohr        /* FIXME handle deactivated plugins
149*49f2871cSAndreas Gohr        if($this->getConf('deactivated plugins') === '') {
150*49f2871cSAndreas Gohr            $deactivatedPluginsList = array('farmer',);
151*49f2871cSAndreas Gohr        } else {
152*49f2871cSAndreas Gohr            $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins'));
153*49f2871cSAndreas Gohr            array_push($deactivatedPluginsList, 'farmer');
154*49f2871cSAndreas Gohr        }
155*49f2871cSAndreas Gohr        foreach($deactivatedPluginsList as $plugin) {
156*49f2871cSAndreas Gohr            $this->helper->deactivatePlugin(trim($plugin), $animal);
157*49f2871cSAndreas Gohr        }
158*49f2871cSAndreas Gohr        */
159*49f2871cSAndreas Gohr
160*49f2871cSAndreas Gohr        return $ok;
161*49f2871cSAndreas Gohr    }
162*49f2871cSAndreas Gohr
163*49f2871cSAndreas Gohr    /**
164*49f2871cSAndreas Gohr     * Creates a new user line
165*49f2871cSAndreas Gohr     *
166*49f2871cSAndreas Gohr     * @param $password
167*49f2871cSAndreas Gohr     * @return string
168*49f2871cSAndreas Gohr     */
169*49f2871cSAndreas Gohr    protected function makeAdminLine($password) {
170*49f2871cSAndreas Gohr        $pass = auth_cryptPassword($password);
171*49f2871cSAndreas Gohr        $line = join("\t", array(
172*49f2871cSAndreas Gohr            'admin',
173*49f2871cSAndreas Gohr            $pass,
174*49f2871cSAndreas Gohr            'Administrator',
175*49f2871cSAndreas Gohr            'admin@example.org',
176*49f2871cSAndreas Gohr            'admin,user'
177*49f2871cSAndreas Gohr        ));
178*49f2871cSAndreas Gohr        return $line;
179*49f2871cSAndreas Gohr    }
180*49f2871cSAndreas Gohr
181*49f2871cSAndreas Gohr    /**
182*49f2871cSAndreas Gohr     * Copies the current user as new admin line
183*49f2871cSAndreas Gohr     *
184*49f2871cSAndreas Gohr     * @return string
185*49f2871cSAndreas Gohr     */
186*49f2871cSAndreas Gohr    protected function getAdminLine() {
187*49f2871cSAndreas Gohr        $currentAdmin = $_SERVER['REMOTE_USER'];
188*49f2871cSAndreas Gohr        $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php');
189*49f2871cSAndreas Gohr        $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":"));
190*49f2871cSAndreas Gohr        $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1);
191*49f2871cSAndreas Gohr        return $newAdmin;
192*49f2871cSAndreas Gohr    }
193*49f2871cSAndreas Gohr
194*49f2871cSAndreas Gohr
195*49f2871cSAndreas Gohr}
196*49f2871cSAndreas Gohr
197*49f2871cSAndreas Gohr// vim:ts=4:sw=4:et:
198