1da0ae2c0SAndreas Gohr<?php 2da0ae2c0SAndreas Gohr/** 3da0ae2c0SAndreas Gohr * DokuWiki Plugin farmer (Admin Component) 4da0ae2c0SAndreas Gohr * 5da0ae2c0SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 6da0ae2c0SAndreas Gohr * @author Michael Große <grosse@cosmocode.de> 7da0ae2c0SAndreas Gohr */ 8da0ae2c0SAndreas Gohr 9da0ae2c0SAndreas Gohr// must be run within Dokuwiki 10da0ae2c0SAndreas Gohruse dokuwiki\Form\Form; 11da0ae2c0SAndreas Gohr 12da0ae2c0SAndreas Gohrif(!defined('DOKU_INC')) die(); 13da0ae2c0SAndreas Gohr 14da0ae2c0SAndreas Gohrclass admin_plugin_farmer_config extends DokuWiki_Admin_Plugin { 15da0ae2c0SAndreas Gohr 16b96c66ccSAndreas Gohr /** @var helper_plugin_farmer */ 17b96c66ccSAndreas Gohr protected $helper; 18b96c66ccSAndreas Gohr 19da0ae2c0SAndreas Gohr /** 20da0ae2c0SAndreas Gohr * @return bool admin only! 21da0ae2c0SAndreas Gohr */ 22da0ae2c0SAndreas Gohr public function forAdminOnly() { 23da0ae2c0SAndreas Gohr return false; 24da0ae2c0SAndreas Gohr } 25da0ae2c0SAndreas Gohr 26da0ae2c0SAndreas Gohr /** 27b96c66ccSAndreas Gohr * admin_plugin_farmer_config constructor. 28b96c66ccSAndreas Gohr */ 29b96c66ccSAndreas Gohr public function __construct() { 30b96c66ccSAndreas Gohr $this->helper = plugin_load('helper', 'farmer'); 31b96c66ccSAndreas Gohr } 32b96c66ccSAndreas Gohr 33b96c66ccSAndreas Gohr /** 34da0ae2c0SAndreas Gohr * Should carry out any processing required by the plugin. 35da0ae2c0SAndreas Gohr */ 36da0ae2c0SAndreas Gohr public function handle() { 37da0ae2c0SAndreas Gohr global $INPUT; 38da0ae2c0SAndreas Gohr global $ID; 39da0ae2c0SAndreas Gohr if(!$INPUT->has('farmconf')) return; 40da0ae2c0SAndreas Gohr if(!checkSecurityToken()) return; 41da0ae2c0SAndreas Gohr 42b96c66ccSAndreas Gohr $farmconf = $this->helper->getConfig(); 43*c4c8e953SAndreas Gohr $farmdir = $farmconf['base']['farmdir']; 44a646d519SAndreas Gohr $farmconf = array_merge($farmconf, $INPUT->arr('farmconf')); 45*c4c8e953SAndreas Gohr $farmconf['base']['farmdir'] = $farmdir; 46*c4c8e953SAndreas Gohr 47*c4c8e953SAndreas Gohr $farmconf['base']['basedomain'] = trim(trim($farmconf['base']['basedomain'],'.')); 48a646d519SAndreas Gohr 49da0ae2c0SAndreas Gohr $ini = DOKU_INC . 'conf/farm.ini'; 50da0ae2c0SAndreas Gohr $data = "; Farm config created by the farmer plugin\n"; 51a646d519SAndreas Gohr $data .= $this->createIni($farmconf); 52da0ae2c0SAndreas Gohr io_saveFile($ini, $data); 53da0ae2c0SAndreas Gohr 54da0ae2c0SAndreas Gohr $self = wl($ID, array('do' => 'admin', 'page' => 'farmer', 'sub' => 'config'), true, '&'); 55da0ae2c0SAndreas Gohr send_redirect($self); 56da0ae2c0SAndreas Gohr } 57da0ae2c0SAndreas Gohr 58da0ae2c0SAndreas Gohr /** 59da0ae2c0SAndreas Gohr * Render HTML output, e.g. helpful text and a form 60da0ae2c0SAndreas Gohr */ 61da0ae2c0SAndreas Gohr public function html() { 62b96c66ccSAndreas Gohr $farmconf = $this->helper->getConfig(); 63da0ae2c0SAndreas Gohr 64da0ae2c0SAndreas Gohr $form = new Form(array('method' => 'post')); 65*c4c8e953SAndreas Gohr 66*c4c8e953SAndreas Gohr $form->addFieldsetOpen($this->getLang('base')); 67*c4c8e953SAndreas Gohr $form->addHTML('<label><span>'.$this->getLang('farm dir').'</span>'.DOKU_FARMDIR); 68*c4c8e953SAndreas Gohr $form->addTextInput('farmconf[base][farmhost]', $this->getLang('farm host'))->val($farmconf['base']['farmhost']); 69*c4c8e953SAndreas Gohr $form->addTextInput('farmconf[base][basedomain]', $this->getLang('base domain'))->val($farmconf['base']['basedomain']); 70*c4c8e953SAndreas Gohr $form->addFieldsetClose(); 71*c4c8e953SAndreas Gohr 72*c4c8e953SAndreas Gohr 73da0ae2c0SAndreas Gohr $form->addFieldsetOpen($this->getLang('conf_inherit')); 74da0ae2c0SAndreas Gohr foreach($farmconf['inherit'] as $key => $val) { 75da0ae2c0SAndreas Gohr $form->setHiddenField("farmconf[inherit][$key]", 0); 76da0ae2c0SAndreas Gohr $chk = $form->addCheckbox("farmconf[inherit][$key]", $this->getLang('conf_inherit_' . $key))->useInput(false); 77da0ae2c0SAndreas Gohr if($val) $chk->attr('checked', 'checked'); 78da0ae2c0SAndreas Gohr } 79da0ae2c0SAndreas Gohr $form->addFieldsetClose(); 80da0ae2c0SAndreas Gohr 81da0ae2c0SAndreas Gohr $form->addFieldsetOpen($this->getLang('conf_notfound')); 82bd48c676SAndreas Gohr $form->addTagOpen('label'); 83bd48c676SAndreas Gohr $form->addTagOpen('span'); 84bd48c676SAndreas Gohr $form->addHTML($this->getLang('conf_notfound')); 85bd48c676SAndreas Gohr $form->addTagClose('span'); 86da0ae2c0SAndreas Gohr $form->addTagOpen('select')->attr('name', 'farmconf[notfound][show]'); 87da0ae2c0SAndreas Gohr foreach(array('farmer', '404', 'list', 'redirect') as $key) { 88da0ae2c0SAndreas Gohr $opt = $form->addTagOpen('option')->attr('value', $key); 89da0ae2c0SAndreas Gohr if($farmconf['notfound']['show'] == $key) $opt->attr('selected', 'selected'); 90da0ae2c0SAndreas Gohr $form->addHTML($this->getLang('conf_notfound_' . $key)); 91da0ae2c0SAndreas Gohr $form->addTagClose('option'); 92da0ae2c0SAndreas Gohr } 93da0ae2c0SAndreas Gohr $form->addTagClose('select'); 94bd48c676SAndreas Gohr $form->addTagClose('label'); 95bd48c676SAndreas Gohr 96da0ae2c0SAndreas Gohr $form->addTextInput('farmconf[notfound][url]', $this->getLang('conf_notfound_url'))->val($farmconf['notfound']['url']); 97da0ae2c0SAndreas Gohr $form->addFieldsetClose(); 98da0ae2c0SAndreas Gohr 99da0ae2c0SAndreas Gohr $form->addButton('save', $this->getLang('save')); 100da0ae2c0SAndreas Gohr echo $form->toHTML(); 101da0ae2c0SAndreas Gohr } 102da0ae2c0SAndreas Gohr 103da0ae2c0SAndreas Gohr /** 104da0ae2c0SAndreas Gohr * Simple function to create an ini file 105da0ae2c0SAndreas Gohr * 106da0ae2c0SAndreas Gohr * Does no escaping, but should suffice for our use case 107da0ae2c0SAndreas Gohr * 108da0ae2c0SAndreas Gohr * @link http://stackoverflow.com/a/5695202/172068 109da0ae2c0SAndreas Gohr * @param array $data The data to transform 110da0ae2c0SAndreas Gohr * @return string 111da0ae2c0SAndreas Gohr */ 112da0ae2c0SAndreas Gohr public function createIni($data) { 113da0ae2c0SAndreas Gohr $res = array(); 114da0ae2c0SAndreas Gohr foreach($data as $key => $val) { 115da0ae2c0SAndreas Gohr if(is_array($val)) { 116da0ae2c0SAndreas Gohr $res[] = ''; 117da0ae2c0SAndreas Gohr $res[] = "[$key]"; 118da0ae2c0SAndreas Gohr foreach($val as $skey => $sval) { 119da0ae2c0SAndreas Gohr $res[] = "$skey = " . (is_numeric($sval) ? $sval : '"' . $sval . '"'); 120da0ae2c0SAndreas Gohr } 121da0ae2c0SAndreas Gohr } else { 122da0ae2c0SAndreas Gohr $res[] = "$key = " . (is_numeric($val) ? $val : '"' . $val . '"'); 123da0ae2c0SAndreas Gohr } 124da0ae2c0SAndreas Gohr } 125da0ae2c0SAndreas Gohr $res[] = ''; 126da0ae2c0SAndreas Gohr return join("\n", $res); 127da0ae2c0SAndreas Gohr } 128da0ae2c0SAndreas Gohr} 129da0ae2c0SAndreas Gohr 130da0ae2c0SAndreas Gohr// vim:ts=4:sw=4:et: 131