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'])){ 42*0336ab2aSAndreas Gohr $url = $this->helper->getAnimalURL($data['name']); 43*0336ab2aSAndreas Gohr $link = '<a href="'.$url.'">'.hsc($data['name']).'</a>'; 44*0336ab2aSAndreas Gohr 45*0336ab2aSAndreas 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() { 5549f2871cSAndreas Gohr 5649f2871cSAndreas Gohr $form = new \dokuwiki\Form\Form(); 5749f2871cSAndreas Gohr $form->addClass('plugin_farmer')->id('farmer__create_animal_form'); 5849f2871cSAndreas Gohr 5949f2871cSAndreas Gohr $form->addFieldsetOpen($this->getLang('animal configuration')); 6049f2871cSAndreas Gohr $form->addTextInput('animalname', $this->getLang('animal name')); 6149f2871cSAndreas Gohr $form->addFieldsetClose(); 6249f2871cSAndreas Gohr 6349f2871cSAndreas Gohr $form->addFieldsetOpen($this->getLang('animal administrator')); 6449f2871cSAndreas Gohr $form->addRadioButton('adminsetup', $this->getLang('importUsers'))->val('importUsers'); 6549f2871cSAndreas Gohr $form->addRadioButton('adminsetup', $this->getLang('currentAdmin'))->val('currentAdmin'); 6649f2871cSAndreas Gohr $form->addRadioButton('adminsetup', $this->getLang('newAdmin'))->val('newAdmin')->attr('checked', 'checked'); 6749f2871cSAndreas Gohr $form->addPasswordInput('adminPassword', $this->getLang('admin password')); 6849f2871cSAndreas Gohr $form->addFieldsetClose(); 6949f2871cSAndreas Gohr 7049f2871cSAndreas Gohr $form->addButton('farmer__submit', $this->getLang('submit'))->attr('type', 'submit')->val('newAnimal'); 7149f2871cSAndreas Gohr echo $form->toHTML(); 7249f2871cSAndreas Gohr 7349f2871cSAndreas Gohr echo $this->locale_xhtml('tab_new_help'); 7449f2871cSAndreas Gohr } 7549f2871cSAndreas Gohr 7649f2871cSAndreas Gohr /** 7749f2871cSAndreas Gohr * Validate the data for a new animal 7849f2871cSAndreas Gohr * 7949f2871cSAndreas Gohr * @return array|bool false on errors, clean data otherwise 8049f2871cSAndreas Gohr */ 8149f2871cSAndreas Gohr protected function validateAnimalData() { 8249f2871cSAndreas Gohr global $INPUT; 8349f2871cSAndreas Gohr 8449f2871cSAndreas Gohr $animalname = $INPUT->filter('trim')->str('animalname'); 8549f2871cSAndreas Gohr $adminsetup = $INPUT->str('adminsetup'); 8649f2871cSAndreas Gohr $adminpass = $INPUT->filter('trim')->str('adminPassword'); 8749f2871cSAndreas Gohr 8849f2871cSAndreas Gohr $errors = array(); 8949f2871cSAndreas Gohr 9049f2871cSAndreas Gohr if($animalname === '') { 9149f2871cSAndreas Gohr $errors[] = $this->getLang('animalname_missing'); 9249f2871cSAndreas Gohr } elseif(!$this->helper->validateAnimalName($animalname)) { 9349f2871cSAndreas Gohr $errors[] = $this->getLang('animalname_invalid'); 9449f2871cSAndreas Gohr } 9549f2871cSAndreas Gohr 9649f2871cSAndreas Gohr if($adminsetup === 'newAdmin' && $adminpass === '') { 9749f2871cSAndreas Gohr $errors[] = $this->getLang('adminPassword_empty'); 9849f2871cSAndreas Gohr } 9949f2871cSAndreas Gohr 10049f2871cSAndreas Gohr if($animalname !== '' && file_exists(DOKU_FARMDIR . '/' . $animalname)) { 10149f2871cSAndreas Gohr $errors[] = $this->getLang('animalname_preexisting'); 10249f2871cSAndreas Gohr } 10349f2871cSAndreas Gohr 10449f2871cSAndreas Gohr if($errors) { 10549f2871cSAndreas Gohr foreach($errors as $error) { 10649f2871cSAndreas Gohr msg($error, -1); 10749f2871cSAndreas Gohr } 10849f2871cSAndreas Gohr return false; 10949f2871cSAndreas Gohr } 11049f2871cSAndreas Gohr 11149f2871cSAndreas Gohr return array( 11249f2871cSAndreas Gohr 'name' => $animalname, 11349f2871cSAndreas Gohr 'admin' => $adminsetup, 11449f2871cSAndreas Gohr 'pass' => $adminpass 11549f2871cSAndreas Gohr ); 11649f2871cSAndreas Gohr } 11749f2871cSAndreas Gohr 11849f2871cSAndreas Gohr /** 11949f2871cSAndreas Gohr * Create a new animal 12049f2871cSAndreas Gohr * 12149f2871cSAndreas Gohr * @param string $name name/title of the animal, will be the directory name for htaccess setup 12249f2871cSAndreas Gohr * @param string $adminSetup newAdmin, currentAdmin or importUsers 12349f2871cSAndreas Gohr * @param string $adminPassword required if $adminSetup is newAdmin 12449f2871cSAndreas Gohr * @return bool true if successful 12549f2871cSAndreas Gohr */ 12649f2871cSAndreas Gohr protected function createNewAnimal($name, $adminSetup, $adminPassword) { 12749f2871cSAndreas Gohr $animaldir = DOKU_FARMDIR . '/' . $name; 12849f2871cSAndreas Gohr 12949f2871cSAndreas Gohr // copy basic template 13049f2871cSAndreas Gohr $ok = $this->helper->io_copyDir(__DIR__ . '/../_animal', $animaldir); 13149f2871cSAndreas Gohr if(!$ok) { 13249f2871cSAndreas Gohr msg($this->getLang('animal creation error'), -1); 13349f2871cSAndreas Gohr return false; 13449f2871cSAndreas Gohr } 13549f2871cSAndreas Gohr 13649f2871cSAndreas Gohr // append title to local config 13749f2871cSAndreas Gohr $ok &= io_saveFile($animaldir.'/conf/local.php', "\n".'$conf[\'title\'] = \''.$name.'\';'."\n", true); 13849f2871cSAndreas Gohr 1394eba53bcSAndreas Gohr // create a random logo and favicon 1404eba53bcSAndreas Gohr if(!class_exists('\splitbrain\RingIcon\RingIcon', false)) { 1414eba53bcSAndreas Gohr require(__DIR__ . '/../3rdparty/RingIcon.php'); 1424eba53bcSAndreas Gohr } 1434eba53bcSAndreas Gohr if(!class_exists('\chrisbliss18\phpico\PHPIco', false)) { 1444eba53bcSAndreas Gohr require(__DIR__ . '/../3rdparty/PHPIco.php'); 1454eba53bcSAndreas Gohr } 1464eba53bcSAndreas Gohr try { 1474eba53bcSAndreas Gohr $ringicon = new \splitbrain\RingIcon\RingIcon(64); 1484eba53bcSAndreas Gohr $ringicon->createImage($animaldir, $animaldir . '/data/media/wiki/logo.png'); 1494eba53bcSAndreas Gohr $icongen = new \chrisbliss18\phpico\PHPIco($animaldir . '/data/media/wiki/logo.png'); 1504eba53bcSAndreas Gohr $icongen->save_ico($animaldir . '/data/media/wiki/favicon.ico'); 1514eba53bcSAndreas Gohr } catch(\Exception $ignore) { 1524eba53bcSAndreas Gohr // something went wrong, but we don't care. this is a nice to have feature only 1534eba53bcSAndreas Gohr } 15449f2871cSAndreas Gohr 15549f2871cSAndreas Gohr // create admin user 15649f2871cSAndreas Gohr if($adminSetup === 'newAdmin') { 15749f2871cSAndreas Gohr $users = "# <?php exit()?>\n".$this->makeAdminLine($adminPassword)."\n"; 15849f2871cSAndreas Gohr } elseif($adminSetup === 'currentAdmin') { 15949f2871cSAndreas Gohr $users = "# <?php exit()?>\n".$this->getAdminLine()."\n"; 16049f2871cSAndreas Gohr } else { 16149f2871cSAndreas Gohr $users = io_readFile(DOKU_CONF . 'users.auth.php'); 16249f2871cSAndreas Gohr } 16349f2871cSAndreas Gohr $ok &= io_saveFile($animaldir . '/conf/users.auth.php', $users); 16449f2871cSAndreas Gohr 16549f2871cSAndreas Gohr /* FIXME handle deactivated plugins 16649f2871cSAndreas Gohr if($this->getConf('deactivated plugins') === '') { 16749f2871cSAndreas Gohr $deactivatedPluginsList = array('farmer',); 16849f2871cSAndreas Gohr } else { 16949f2871cSAndreas Gohr $deactivatedPluginsList = explode(',', $this->getConf('deactivated plugins')); 17049f2871cSAndreas Gohr array_push($deactivatedPluginsList, 'farmer'); 17149f2871cSAndreas Gohr } 17249f2871cSAndreas Gohr foreach($deactivatedPluginsList as $plugin) { 17349f2871cSAndreas Gohr $this->helper->deactivatePlugin(trim($plugin), $animal); 17449f2871cSAndreas Gohr } 17549f2871cSAndreas Gohr */ 17649f2871cSAndreas Gohr 17749f2871cSAndreas Gohr return $ok; 17849f2871cSAndreas Gohr } 17949f2871cSAndreas Gohr 18049f2871cSAndreas Gohr /** 18149f2871cSAndreas Gohr * Creates a new user line 18249f2871cSAndreas Gohr * 18349f2871cSAndreas Gohr * @param $password 18449f2871cSAndreas Gohr * @return string 18549f2871cSAndreas Gohr */ 18649f2871cSAndreas Gohr protected function makeAdminLine($password) { 18749f2871cSAndreas Gohr $pass = auth_cryptPassword($password); 18849f2871cSAndreas Gohr $line = join("\t", array( 18949f2871cSAndreas Gohr 'admin', 19049f2871cSAndreas Gohr $pass, 19149f2871cSAndreas Gohr 'Administrator', 19249f2871cSAndreas Gohr 'admin@example.org', 19349f2871cSAndreas Gohr 'admin,user' 19449f2871cSAndreas Gohr )); 19549f2871cSAndreas Gohr return $line; 19649f2871cSAndreas Gohr } 19749f2871cSAndreas Gohr 19849f2871cSAndreas Gohr /** 19949f2871cSAndreas Gohr * Copies the current user as new admin line 20049f2871cSAndreas Gohr * 20149f2871cSAndreas Gohr * @return string 20249f2871cSAndreas Gohr */ 20349f2871cSAndreas Gohr protected function getAdminLine() { 20449f2871cSAndreas Gohr $currentAdmin = $_SERVER['REMOTE_USER']; 20549f2871cSAndreas Gohr $masterUsers = file_get_contents(DOKU_CONF . 'users.auth.php'); 20649f2871cSAndreas Gohr $masterUsers = ltrim(strstr($masterUsers, "\n" . $currentAdmin . ":")); 20749f2871cSAndreas Gohr $newAdmin = substr($masterUsers, 0, strpos($masterUsers, "\n") + 1); 20849f2871cSAndreas Gohr return $newAdmin; 20949f2871cSAndreas Gohr } 21049f2871cSAndreas Gohr 21149f2871cSAndreas Gohr 21249f2871cSAndreas Gohr} 21349f2871cSAndreas Gohr 21449f2871cSAndreas Gohr// vim:ts=4:sw=4:et: 215