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