<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides PLESK support
 *
 * @package    Host
 * @category   Plugins
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
abstract class Host_Plugin_Plesk extends Host_Plugin {
	// Service Object
	protected $protocol = '1.6.0.0';
	protected $path = 'enterprise/control/agent.php';
	protected $packet;
	protected $xml;
	protected $_loaded = FALSE;

	// Manage UI Login Attributes
	protected $url = 'login_up.php3';
	protected $login_acnt_field = '';
	protected $login_user_field = 'login_name';
	protected $login_pass_field = 'passwd';

	// Our required abstract classes
	public function serialize() {
		return (string)$this->_object;
	}
	public function unserialize($s) {
		$this->_object = XML::factory(NULL,NULL,$s);
	}

	public function __get($index) {
                // We need to find out the command key, so we can get the status result.
                if (count($a=array_keys($this->xml->packet->as_array())) != 1)
                        throw new Kohana_Exception('XML command malformed? :xml',array(':xml'=>(string)$this->xml));
                $key = array_shift($a);
                if (count($a=array_keys($this->xml->packet->$key->as_array())) != 1)
                        throw new Kohana_Exception('XML command malformed? :xml',array(':xml'=>(string)$this->xml));
                $get = array_shift($a);

		if ($index == '_object')
			return (! $this->_loaded OR $this->_object->$key->$get->result->status->value() != 'ok' ) ? $this->_object : $this->_object->$key->$get;
		elseif ($index == '_xml')
			return $this->xml;
		else
			return is_object($this->_object->$key->$get->result->$index) ? $this->_object->$key->$get->result->$index : NULL;
	}

	public function admin_update() {
		return View::factory('host/admin/plugin/plesk')
			->set('o',$this);
	}

	public function manage_button(Model_Service_Plugin_Host $spho,$t) {
		return $this->render_button($t,$spho->service_id,$spho->username_value(),substr(md5($spho->password_value()),0,8));
	}
	public function admin_manage_button(Model_Host_Server $hso,$t) {
		return $this->render_button($t,$hso->id,substr(md5($hso->manage_username),0,8),substr(md5($hso->manage_password),0,8));
	}

	protected function render_button($t,$sid,$u,$p) {
		$debug = FALSE;
		$output = '';

		$output .= Form::open(
			$debug ? 'debug/site' : sprintf('%s/%s',$this->hso->manage_url,$this->url),
			array('target'=>'w24','method'=>'post','id'=>sprintf('id_%s_%s',$sid,$t))
		);
		$output .= Form::input($this->login_user_field,$u,array('type'=>'hidden','id'=>sprintf('u_%s_%s',$sid,$t)));
		$output .= Form::input($this->login_pass_field,$p,array('type'=>'hidden','id'=>sprintf('p_%s_%s',$sid,$t)));
		$output .= Form::close();
		$output .= Form::button('submit',_('Manage'),array('class'=>'btn btn-default','value'=>sprintf('%s:%s',$sid,$t),'nocg'=>TRUE));

		return $output;
	}

	protected function init() {
		$this->_loaded = FALSE;
		$this->_object = XML::factory(NULL,'plesk');
		$this->xml = XML::factory(NULL,'plesk');
		$this->packet = $this->xml->add_node('packet','',array('version'=>$this->protocol));
	}

	protected function server_command(XML $xml) {
		$request = Request::factory(sprintf('%s/%s',$this->hso->manage_url,$this->path))
			->method('POST');

		$request->client()->options(Arr::merge($this->curlopts,array(
			CURLOPT_HTTPHEADER => array(
				'HTTP_AUTH_LOGIN: '.$this->hso->manage_username,
				'HTTP_AUTH_PASSWD: '.$this->hso->manage_password,
				'Content-Type: text/xml',
			),
			CURLOPT_POSTFIELDS => $this->render($xml),
			)));

		$response = $request->execute();

		$this->_object = XML::factory(NULL,'plesk',$response->body());

		if ($this->_object->status->value() == 'ok')
			$this->_loaded = TRUE;

		return $this;
	}

	public function loaded() {
		return $this->_loaded;
	}

	private function render(XML $xml) {
		return preg_replace('/<\/?plesk>/','',(string)$xml->render(FALSE));
	}
}
?>
