1<?php
2/**
3 * Wiki farm manager
4 *
5 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author   Etienne MELEARD <etienne.meleard@cru.fr>
7 * @desc     Process and renders virtualhost config related requests
8 */
9
10// must be run within Dokuwiki
11if(!defined('DOKU_INC')) die();
12
13if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
14if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', DOKU_PLUGIN.'farm/');
15
16if(!defined('DOKU_FARMPLUGINLOADED')) define('DOKU_FARMPLUGINLOADED', true);
17
18class dokuwiki_farm_virtualhostconfig {
19	var $data = array();
20	var $manager = null;
21	var $errors = array();
22
23	/**
24	 * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls
25	 */
26	function __construct($manager) {
27		$this->manager = & $manager;
28	}
29
30	/**
31	 * Builds a link inside SOAP config tab
32	 * @param $opts farm option array
33	 * @return url string
34	 */
35	function wl($opts = array()) {
36		return $this->manager->wl('virtualhostconfig', $opts);
37	}
38
39	/**
40	 * Process requests
41	 */
42	function process() {
43		// Parameters check
44		if(!isset($this->manager->opt['save'])) return;
45
46		// Security check
47		if(!checkSecurityToken()) {
48			$this->manager->error('system_errors', 'system_badtoken_failure');
49			return; // any changes done by post
50		}
51
52		// Text mode save request
53		if(isset($this->manager->opt['save'])) {
54			if(!isset($_POST['virtualhost_config_advanced']) || empty($_POST['virtualhost_config_advanced'])) {
55				$this->manager->error('system_errors', 'postparametermissing_failure');
56				return;
57			}
58			if($fp = fopen(DOKU_FARM_PLUGIN.'virtual_hosts.php', 'w')) {
59				fwrite($fp, '<?php exit(); ?>'."\n".$_POST['virtualhost_config_advanced']);
60				fclose($fp);
61			}else $this->manager->error('virtualhostconfig_errors', 'virtualhostconfig_save_failure');
62			return;
63		}
64	}
65
66	/**
67	 * Renders
68	 */
69	function html() {
70		global $ID;
71		ptln('<div class="farm_cmd_title">'.$this->manager->getLang('virtualhostconfig_title').'</div>');
72		ptln('<div class="farm_cmd_info">'.$this->manager->getLang('virtualhostconfig_info').'</div>');
73
74		require_once DOKU_FARM_PLUGIN.'animal.class.php';
75		$l = dokuwiki_farm_animal::listAnimals($this->manager);
76		$vh = $this->getVH();
77
78		$this->manager->formHead(array('farm_cmd' => 'virtualhostconfig'));
79		ptln('<fieldset>');
80		ptln('	'.$this->manager->getLang('virtualhost_config_for').' <select onchange="if(!this.value) return; var vh = prompt(\''.$this->manager->getLang('virtualhost_config_value').'\', \'\'); if(vh) this.form.elements[\'virtualhost_config_advanced\'].value += vh + \' \' + this.value; this.selectedIndex = 0;">');
81		ptln('		<option value="">---</option>');
82		foreach($l as $a) {
83			if(!in_array($a->getName(), array_values($vh))) {
84				ptln('		<option value="'.$a->getName().'">'.$a->getName().'</option>');
85			}
86		}
87		ptln('	</select>');
88		ptln('	<textarea rows="20" style="width:100%;overflow:auto;" name="virtualhost_config_advanced">'.preg_replace('`<\?php\s*exit\(\);\s*\?>`', '', @file_get_contents(DOKU_FARM_PLUGIN.'virtual_hosts.php')).'</textarea>');
89		ptln('</fieldset>');
90		ptln('	<fieldset class="save">');
91		ptln('		<input type="submit" class="button" name="farm_opt[save]" value="'.$this->manager->getLang('btn_save').'" />');
92		ptln('	</fieldset>');
93		ptln('</form>');
94	}
95
96	/**
97	 * Returns the list of virtualhosts
98	 * @return array of virtualhosts descriptors
99	 */
100	function getVH() {
101		$vh = array();
102		foreach(explode("\n", @file_get_contents(DOKU_FARM_PLUGIN.'virtual_hosts.php')) as $l) {
103			if(preg_match('`^\s*([^\s]+)\s+([^\s#]+)`', $l, $m)) $vh[$m[1]] = $m[2];
104		}
105		return $vh;
106	}
107}
108?>
109