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