<?php defined('SYSPATH') or die('No direct access allowed.');
/**
 * This class provides Host Server functions
 *
 * @package    OSB
 * @subpackage HostServer
 * @category   Controllers/Task
 * @author     Deon George
 * @copyright  (c) 2010 Deon George
 * @license    http://dev.leenooks.net/license.html
 */
class Controller_Task_Host extends Controller_Task {
	// Host Server Object
	private $hs;
	private $so;

	public function __construct(Request $request, Response $response) {
		parent::__construct($request,$response);

		// To make it easy for some methods, we'll load our service here
		switch (Request::current()->action()) {
			case 'getclient':
			case 'getdomain':
			case 'gettraffic':
			case 'provision':
				$this->so = ORM::factory('service',$this->request->param('id'));

				if (! $this->so->loaded())
					throw new Kohana_Exception('Unknown service :sid',array(':sid'=>$this->request->param('id')));

				$hso = $this->so->plugin()->host_server;
				$this->hs = new $hso->provision_plugin($hso);

				break;
		}
	}

	public function save($index,$result) {
		$p = $this->so->plugin();

		// We need to use a new var to avoid Indirect modification of overloaded property errors.
		$x = is_null($p->server_data) ? array() : $p->server_data;
		$x[$p->host_server_id][$index] = serialize($result);
		$p->server_data = $x;

		$x = is_null($p->server_data_date) ? array() : $p->server_data_date;
		$x[$p->host_server_id][$index] = time();
		$p->server_data_date = $x;

		return $p->save();
	}

	/**
	 * Get Client Details from Host Server
	 */
	public function action_getclient() {
		$result = $this->hs->cmd_getclient($this->so);
		$p = $this->so->plugin();

		if ($result->loaded()) {
			if (! isset($p->server_data[$p->host_server_id]['c']) OR (md5($p->server_data[$p->host_server_id]['c']) != md5(serialize($result)))) {
				echo "WARNING: data changed on server";
				$this->save('c',$result);
			} else
				echo "NOTE: data same on server";

		} else
			print_r($result);
	}

	/**
	 * Get Client Details from Host Server
	 */
	public function action_getdomain() {
		$result = $this->hs->cmd_getdomain($this->so);
		$p = $this->so->plugin();

		if ($result->loaded()) {
			if (! isset($p->server_data[$p->host_server_id]['d']) OR (md5($p->server_data[$p->host_server_id]['d']) != md5(serialize($result)))) {
				echo "WARNING: data changed on server";
				$this->save('d',$result);
			} else
				echo "NOTE: data same on server";

		} else
			print_r($result);
	}

	/**
	 * Get Domain Traffic
	 */
	public function action_gettraffic() {
		$sid = $this->request->param('id');

		$result = $this->hs->cmd_gettraffic(ORM::factory('service',$sid));
		if ($result->gen_info)
			print_r($result->gen_info->as_array());
		else
			print_r((string)$result);
	}

	/**
	 * List services that need to be provisioned
	 */
	public function action_provisionlist() {
		$mode = $this->request->param('id');
		$cats = array();

		if ($mode)
			$cats = ORM::factory('product_category')->list_bylistgroup($mode);

		foreach (ORM::Factory('service')->list_provision()->find_all() as $so) {
			$pc = array();

			// Limit to show only those by the requested category.
			if ($cats) {
				if (! $so->product->avail_category OR ! preg_match('/^a:/',$so->product->avail_category))
					continue;

                        	$pc = unserialize($so->product->avail_category);

				if (! array_intersect($pc,array_keys($cats)))
					continue;
			}

			echo $so->id();

			switch ($mode) {
				case 'host':
					printf(' %s %s %s',$so->plugin(),$so->plugin()->host_server->name,$so->service_name());
					break;

				default:
			}
			echo "\n";
		}
	}

	/**
	 * Add a domain for the client
	 *
	 * @param int $id Hosting ID (in OSB)
	 * @return unknown_type
	 */
	public function action_provision() {
		$sid = $this->request->param('id');
		$so = ORM::factory('service',$sid);

		// Provision Account
		// @todo Need a test to see if an account alerady exists.
/*
		$result = $this->hs->add_client($so);
		print_r((string)$result);

		// Next need to get the ID from the account call to set the IP
		$result = $this->hs->setip(35); // @todo change this number
		print_r((string)$result);

		// Provision Domain
		$result = $this->hs->add_service($so);
		print_r((string)$result);

		// Set Limits
		$result = $this->hs->setlimits($so);
		print_r((string)$result);

		// Next need to get the ID for the domain to disable mail
		$result = $this->hs->disablemail(43);
		print_r((string)$result);
*/
	}
}
?>
