<?php

namespace Modules\Admin\Server\Checks\Connect;

class Controller extends \App\Mvc\Controller\Form\PopupAbstract
{

	/**
	 * {@inheritDoc}
	 * @see \App\Mvc\Controller\Form\PopupAbstract::initialize()
	 */
	protected function initialize()
	{
		parent::initialize();

		$this->actionsCfg['index'] = [ 'checkSection' => 'Modules/Admin/Server/Checks', 'checkOption' => false ];
	}

	/**
	 * Returns an array of data which is sent to the form in an identical format as the $_REQUEST.
	 * Is is used to fill in the default (previous) values of the form's fields.
	 * @param array $R
	 */
	protected function getDefaultFormData($R)
	{
		return [
			'ssl'=>1
		];
	}


	/**
	 * It uses the $formData (values collected form the form to attempt to save the info in the
	 * database/files (external storage).
	 * @param Array $formData
	 */
	protected function processFormData(Array $R, \App\MessagesCollector $msgs)
	{
		if ($R['save'] && !$R['address']) {
			$msgs->addWarning($this->lang['index']['emptyAddress']);
		}
		elseif ($R['save'] && $R['address']) {

			$curl_handle = curl_init();

			curl_setopt($curl_handle, CURLOPT_URL, htmlUnclean($R['address']));
			curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 0);
			curl_setopt($curl_handle, CURLOPT_TIMEOUT, 0);
			if ($R['ssl']) {
				curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, true);
				curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, true);
			} else {
				curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
			}
			curl_setopt($curl_handle, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
			curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);

			$response = 	curl_exec($curl_handle);
			$http_code = 	curl_getinfo($curl_handle);

			if ($response) {
				$msgs->addSuccess(sprintf($this->lang['index']['success'], $R['address']));
			}
			else {
				$msgs->addError(sprintf($this->lang['index']['error'], $R['address'], curl_error($curl_handle)));
			}
		}

		return $http_code;
	}

	/**
	 * (non-PHPdoc)
	 * @see \App\Mvc\Controller\Form\PopupAbstract::actionIndex()
	 */
	protected function actionIndex($R)
	{

		// collects messages
		$msgs = \App\MessagesCollector::create();

		$modelOutput = null;
		// SAVE THE FORM DATA /////////////////////////////////////////////////////////////////////
		if ($R[$this->formSubmitButtonName]) {
			$modelOutput = $this->processFormData($R, $msgs);
		}

		// INSTANTIATE AND RENDER VIEW ////////////////////////////////////////////////////////////
		// instantiate the view
		$view = new View_Index($this);

		// get form data for this operation
		if (!$R[$this->formSubmitButtonName]) {
			$formData=$this->getDefaultFormData($R);
		} else {
			$formData=$R;
			$formData['debugOutput'] = $modelOutput;
		}

		// send data to the view
		$view->addInput('formData', $formData);
		$view->getMessagesCollector()->appendMessages($msgs);
		$view->addInput('formSubmitButtonName', $this->formSubmitButtonName);

		// do some more stuff (if needed)
		$this->preRenderOperations($view, $formData);

		// render the view
		return new \Engine\Mvc\Response\Html($view->render());

	}


}