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