<?php
/**
 * Wiki farm manager
 *
 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
 * @author   Etienne MELEARD <etienne.meleard@cru.fr>
 * @desc     Process and renders virtualhost config related requests
 */

// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();

if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
if(!defined('DOKU_FARM_PLUGIN')) define('DOKU_FARM_PLUGIN', DOKU_PLUGIN.'farm/');

if(!defined('DOKU_FARMPLUGINLOADED')) define('DOKU_FARMPLUGINLOADED', true);

class dokuwiki_farm_virtualhostconfig {
	var $data = array();
	var $manager = null;
	var $errors = array();
	
	/**
	 * @param $manager object that must handle error(), success(), nicesize(), getLang() ... calls
	 */
	function __construct($manager) {
		$this->manager = & $manager;
	}
	
	/**
	 * Builds a link inside SOAP config tab
	 * @param $opts farm option array
	 * @return url string
	 */
	function wl($opts = array()) {
		return $this->manager->wl('virtualhostconfig', $opts);
	}
	
	/**
	 * Process requests
	 */
	function process() {
		// Parameters check
		if(!isset($this->manager->opt['save'])) return;
		
		// Security check
		if(!checkSecurityToken()) {
			$this->manager->error('system_errors', 'system_badtoken_failure');
			return; // any changes done by post
		}
		
		// Text mode save request
		if(isset($this->manager->opt['save'])) {
			if(!isset($_POST['virtualhost_config_advanced']) || empty($_POST['virtualhost_config_advanced'])) {
				$this->manager->error('system_errors', 'postparametermissing_failure');
				return;
			}
			if($fp = fopen(DOKU_FARM_PLUGIN.'virtual_hosts.php', 'w')) {
				fwrite($fp, '<?php exit(); ?>'."\n".$_POST['virtualhost_config_advanced']);
				fclose($fp);
			}else $this->manager->error('virtualhostconfig_errors', 'virtualhostconfig_save_failure');
			return;
		}
	}
	
	/**
	 * Renders
	 */
	function html() {
		global $ID;
		ptln('<div class="farm_cmd_title">'.$this->manager->getLang('virtualhostconfig_title').'</div>');
		ptln('<div class="farm_cmd_info">'.$this->manager->getLang('virtualhostconfig_info').'</div>');
		
		require_once DOKU_FARM_PLUGIN.'animal.class.php';
		$l = dokuwiki_farm_animal::listAnimals($this->manager);
		$vh = $this->getVH();
		
		$this->manager->formHead(array('farm_cmd' => 'virtualhostconfig'));
		ptln('<fieldset>');
		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;">');
		ptln('		<option value="">---</option>');
		foreach($l as $a) {
			if(!in_array($a->getName(), array_values($vh))) {
				ptln('		<option value="'.$a->getName().'">'.$a->getName().'</option>');
			}
		}
		ptln('	</select>');
		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>');
		ptln('</fieldset>');
		ptln('	<fieldset class="save">');
		ptln('		<input type="submit" class="button" name="farm_opt[save]" value="'.$this->manager->getLang('btn_save').'" />');
		ptln('	</fieldset>');
		ptln('</form>');
	}
	
	/**
	 * Returns the list of virtualhosts
	 * @return array of virtualhosts descriptors
	 */
	function getVH() {
		$vh = array();
		foreach(explode("\n", @file_get_contents(DOKU_FARM_PLUGIN.'virtual_hosts.php')) as $l) {
			if(preg_match('`^\s*([^\s]+)\s+([^\s#]+)`', $l, $m)) $vh[$m[1]] = $m[2];
		}
		return $vh;
	}
}
?>
