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'])) { 420336ab2aSAndreas Gohr $url = $this->helper->getAnimalURL($data['name']); 430336ab2aSAndreas Gohr $link = '<a href="' . $url . '">' . hsc($data['name']) . '</a>'; 440336ab2aSAndreas Gohr 450336ab2aSAndreas Gohr msg(sprintf($this->getLang('animal creation success'), $link), 1); 4649f2871cSAndreas Gohr $link = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'new'), true, '&'); 4749f2871cSAndreas Gohr send_redirect($link); 4849f2871cSAndreas Gohr } 4949f2871cSAndreas Gohr } 5049f2871cSAndreas Gohr 5149f2871cSAndreas Gohr /** 5249f2871cSAndreas Gohr * Render HTML output, e.g. helpful text and a form 5349f2871cSAndreas Gohr */ 5449f2871cSAndreas Gohr public function html() { 55*1272da0cSAndreas Gohr $farmconfig = $this->helper->getConfig(); 5649f2871cSAndreas Gohr 5749f2871cSAndreas Gohr $form = new \dokuwiki\Form\Form(); 5849f2871cSAndreas Gohr $form->addClass('plugin_farmer')->id('farmer__create_animal_form'); 5949f2871cSAndreas Gohr 6049f2871cSAndreas Gohr $form->addFieldsetOpen($this->getLang('animal configuration')); 6149f2871cSAndreas Gohr $form->addTextInput('animalname', $this->getLang('animal name')); 6249f2871cSAndreas Gohr $form->addFieldsetClose(); 6349f2871cSAndreas Gohr 6449f2871cSAndreas Gohr $form->addFieldsetOpen($this->getLang('animal administrator')); 65*1272da0cSAndreas Gohr $btn = $form->addRadioButton('adminsetup', $this->getLang('noUsers'))->val('noUsers'); 66*1272da0cSAndreas Gohr if($farmconfig['inherit']['users']) { 67*1272da0cSAndreas Gohr $btn->attr('checked', 'checked'); // default when inherit available 68*1272da0cSAndreas Gohr } else { 69*1272da0cSAndreas Gohr $btn->attr('disabled', 'disabled'); 70*1272da0cSAndreas Gohr } 7149f2871cSAndreas Gohr $form->addRadioButton('adminsetup', $this->getLang('importUsers'))->val('importUsers'); 7249f2871cSAndreas Gohr $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))->val('currentAdmin'); 73*1272da0cSAndreas Gohr $btn = $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))->val('newAdmin'); 74*1272da0cSAndreas Gohr if(!$farmconfig['inherit']['users']) { 75*1272da0cSAndreas Gohr $btn->attr('checked', 'checked'); // default when inherit not available 76*1272da0cSAndreas Gohr } 7749f2871cSAndreas Gohr $form->addPasswordInput('adminPassword', $this->getLang('admin password')); 7849f2871cSAndreas Gohr $form->addFieldsetClose(); 7949f2871cSAndreas Gohr 8049f2871cSAndreas Gohr $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit')->val('newAnimal'); 8149f2871cSAndreas Gohr echo $form->toHTML(); 8249f2871cSAndreas Gohr } 8349f2871cSAndreas Gohr 8449f2871cSAndreas Gohr /** 8549f2871cSAndreas Gohr * Validate the data for a new animal 8649f2871cSAndreas Gohr * 8749f2871cSAndreas Gohr * @return array|bool false on errors, clean data otherwise 8849f2871cSAndreas Gohr */ 8949f2871cSAndreas Gohr protected function validateAnimalData() { 9049f2871cSAndreas Gohr global $INPUT; 9149f2871cSAndreas Gohr 9249f2871cSAndreas Gohr $animalname = $INPUT->filter('trim')->str('animalname'); 9349f2871cSAndreas Gohr $adminsetup = $INPUT->str('adminsetup'); 9449f2871cSAndreas Gohr $adminpass = $INPUT->filter('trim')->str('adminPassword'); 9549f2871cSAndreas Gohr 9649f2871cSAndreas Gohr $errors = array(); 9749f2871cSAndreas Gohr 9849f2871cSAndreas Gohr if($animalname === '') { 9949f2871cSAndreas Gohr $errors[] = $this->getLang('animalname_missing'); 10049f2871cSAndreas Gohr } elseif(!$this->helper->validateAnimalName($animalname)) { 10149f2871cSAndreas Gohr $errors[] = $this->getLang('animalname_invalid'); 10249f2871cSAndreas Gohr } 10349f2871cSAndreas Gohr 10449f2871cSAndreas Gohr if($adminsetup === 'newAdmin' && $adminpass === '') { 10549f2871cSAndreas Gohr $errors[] = $this->getLang('adminPassword_empty'); 10649f2871cSAndreas Gohr } 10749f2871cSAndreas Gohr 10849f2871cSAndreas Gohr if($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) { 10949f2871cSAndreas Gohr $errors[] = $this->getLang('animalname_preexisting'); 11049f2871cSAndreas Gohr } 11149f2871cSAndreas Gohr 11249f2871cSAndreas Gohr if($errors) { 11349f2871cSAndreas Gohr foreach($errors as $error) { 11449f2871cSAndreas Gohr msg($error, -1); 11549f2871cSAndreas Gohr } 11649f2871cSAndreas Gohr return false; 11749f2871cSAndreas Gohr } 11849f2871cSAndreas Gohr 11949f2871cSAndreas Gohr return array( 12049f2871cSAndreas Gohr 'name' => $animalname, 12149f2871cSAndreas Gohr 'admin' => $adminsetup, 12249f2871cSAndreas Gohr 'pass' => $adminpass 12349f2871cSAndreas Gohr ); 12449f2871cSAndreas Gohr } 12549f2871cSAndreas Gohr 12649f2871cSAndreas Gohr /** 12749f2871cSAndreas Gohr * Create a new animal 12849f2871cSAndreas Gohr * 12949f2871cSAndreas Gohr * @param string $name name/title of the animal, will be the directory name for htaccess setup 13049f2871cSAndreas Gohr * @param string $adminSetup newAdmin, currentAdmin or importUsers 13149f2871cSAndreas Gohr * @param string $adminPassword required if $adminSetup is newAdmin 13249f2871cSAndreas Gohr * @return bool true if successful 13349f2871cSAndreas Gohr */ 13449f2871cSAndreas Gohr protected function createNewAnimal($name, $adminSetup, $adminPassword) { 13549f2871cSAndreas Gohr $animaldir = DOKU_FARMDIR . '/' . $name; 13649f2871cSAndreas Gohr 13749f2871cSAndreas Gohr // copy basic template 13849f2871cSAndreas Gohr $ok = $this->helper->io_copyDir(__DIR__ . '/../_animal', $animaldir); 13949f2871cSAndreas Gohr if(!$ok) { 14049f2871cSAndreas Gohr msg($this->getLang('animal creation error'), -1); 14149f2871cSAndreas Gohr return false; 14249f2871cSAndreas Gohr } 14349f2871cSAndreas Gohr 14449f2871cSAndreas Gohr // append title to local config 14549f2871cSAndreas Gohr $ok &= io_saveFile($animaldir . '/conf/local.php', "\n" . '$conf[\'title\'] = \'' . $name . '\';' . "\n", true); 14649f2871cSAndreas Gohr 1474eba53bcSAndreas Gohr // create a random logo and favicon 1484eba53bcSAndreas Gohr if(!class_exists('\splitbrain\RingIcon\RingIcon', false)) { 1494eba53bcSAndreas Gohr require(__DIR__ . '/../3rdparty/RingIcon.php'); 1504eba53bcSAndreas Gohr } 1514eba53bcSAndreas Gohr if(!class_exists('\chrisbliss18\phpico\PHPIco', false)) { 1524eba53bcSAndreas Gohr require(__DIR__ . '/../3rdparty/PHPIco.php'); 1534eba53bcSAndreas Gohr } 1544eba53bcSAndreas Gohr try { 1554eba53bcSAndreas Gohr $ringicon = new \splitbrain\RingIcon\RingIcon(64); 1564eba53bcSAndreas Gohr $ringicon->createImage($animaldir, $animaldir . '/data/media/wiki/logo.png'); 1574eba53bcSAndreas Gohr $icongen = new \chrisbliss18\phpico\PHPIco($animaldir . '/data/media/wiki/logo.png'); 1584eba53bcSAndreas Gohr $icongen->save_ico($animaldir . '/data/media/wiki/favicon.ico'); 1594eba53bcSAndreas Gohr } catch(\Exception $ignore) { 1604eba53bcSAndreas Gohr // something went wrong, but we don't care. this is a nice to have feature only 1614eba53bcSAndreas Gohr } 16249f2871cSAndreas Gohr 16349f2871cSAndreas Gohr // create admin user 16449f2871cSAndreas Gohr if($adminSetup === 'newAdmin') { 16549f2871cSAndreas Gohr $users = "# <?php exit()?>\n" . $this->makeAdminLine($adminPassword) . "\n"; 16649f2871cSAndreas Gohr } elseif($adminSetup === 'currentAdmin') { 16749f2871cSAndreas Gohr $users = "# <?php exit()?>\n" . $this->getAdminLine() . "\n"; 168*1272da0cSAndreas Gohr } elseif($adminSetup === 'noUsers') { 169*1272da0cSAndreas Gohr $users = "# <?php exit()?>\n"; 17049f2871cSAndreas Gohr } else { 17149f2871cSAndreas Gohr $users = io_readFile(DOKU_CONF . 'users.auth.php'); 17249f2871cSAndreas Gohr } 17349f2871cSAndreas Gohr $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users); 17449f2871cSAndreas Gohr 17549f2871cSAndreas Gohr /* FIXME handle deactivated plugins 17649f2871cSAndreas Gohr if($this->getConf('deactivated plugins') === '') { 17749f2871cSAndreas Gohr $deactivatedPluginsList = array('farmer',); 17849f2871cSAndreas Gohr } else { 17949f2871cSAndreas Gohr $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins')); 18049f2871cSAndreas Gohr array_push($deactivatedPluginsList, 'farmer'); 18149f2871cSAndreas Gohr } 18249f2871cSAndreas Gohr foreach($deactivatedPluginsList as $plugin) { 18349f2871cSAndreas Gohr $this->helper->deactivatePlugin(trim($plugin), $animal); 18449f2871cSAndreas Gohr } 18549f2871cSAndreas Gohr */ 18649f2871cSAndreas Gohr 18749f2871cSAndreas Gohr return $ok; 18849f2871cSAndreas Gohr } 18949f2871cSAndreas Gohr 19049f2871cSAndreas Gohr /** 19149f2871cSAndreas Gohr * Creates a new user line 19249f2871cSAndreas Gohr * 19349f2871cSAndreas Gohr * @param $password 19449f2871cSAndreas Gohr * @return string 19549f2871cSAndreas Gohr */ 19649f2871cSAndreas Gohr protected function makeAdminLine($password) { 19749f2871cSAndreas Gohr $pass = auth_cryptPassword($password); 198*1272da0cSAndreas Gohr $line = join( 199*1272da0cSAndreas Gohr "\t", array( 20049f2871cSAndreas Gohr 'admin', 20149f2871cSAndreas Gohr $pass, 20249f2871cSAndreas Gohr 'Administrator', 20349f2871cSAndreas Gohr 'admin@example.org', 20449f2871cSAndreas Gohr 'admin,user' 205*1272da0cSAndreas Gohr ) 206*1272da0cSAndreas Gohr ); 20749f2871cSAndreas Gohr return $line; 20849f2871cSAndreas Gohr } 20949f2871cSAndreas Gohr 21049f2871cSAndreas Gohr /** 21149f2871cSAndreas Gohr * Copies the current user as new admin line 21249f2871cSAndreas Gohr * 21349f2871cSAndreas Gohr * @return string 21449f2871cSAndreas Gohr */ 21549f2871cSAndreas Gohr protected function getAdminLine() { 21649f2871cSAndreas Gohr $currentAdmin = $_SERVER['REMOTE_USER']; 21749f2871cSAndreas Gohr $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php'); 21849f2871cSAndreas Gohr $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":")); 21949f2871cSAndreas Gohr $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1); 22049f2871cSAndreas Gohr return $newAdmin; 22149f2871cSAndreas Gohr } 22249f2871cSAndreas Gohr 22349f2871cSAndreas Gohr} 22449f2871cSAndreas Gohr 22549f2871cSAndreas Gohr// vim:ts=4:sw=4:et: 226