<?php


function outright_create_simple_form( $form_array ){

	$form ='<table align="centre" width ="100%">';
	$form .='<form method="POST">';
	$form .='<input type="hidden" value="outr_outr_licence" name="module">';
	$form .='<input type="hidden" value="create_entrypoint" name="action">';
	$open_tr ='<tr><td>';
	$mid_tr ='</td><td>';
	$close_tr='</td></tr>';
	foreach( $form_array as $form_field){
	$form .=$open_tr.$form_field['label'].$mid_tr;
			if( $form_field['type'] =='text' ){
			$form .='<input type="text" name ="'.$form_field['name'].'">';
			 }
			 else if( $form_field['type'] =='textarea' ){		
			$form .= '<textarea name ="'.$form_field['name'].'" rows="10" cols="100">';
			$form .='outright_retrieve("Users","1");
			$sql ="select * from users where deleted =0";
			$result =outright_run_sql($sql);
			echo "<pre>";
			print_r($result);
			echo "</pre><br/>";
			</textarea>';
		 }
		  else if( $form_field['type'] =='select' ){
			$form .='<select name ="'.$form_field['name'].'">';
			  foreach( $form_field['options'] as $form_key=>$form_value){
				$form .='<option value="'.$form_key.'" label="'.$form_value.'"></option>';
			  }

			  $form .='</select>';
			$form .=$close_tr;
		 }
	}
		 	$form .='<tr><td colspan="2"><input type="submit" value="Create EntryPoint" name="create_entrypoint"></td></tr>';

	 $form .='</form>';

	$form .='</table>';
	
	return $form;
}
class outright_create_entrypoint{

var $enp_file_name;
var $enp_dir;
var $enp_ext_dir;
var $default_code;
var $entryPoint_location;


       public function __construct(){
		   	$this->enp_file_name =$_REQUEST['enp_name'].".php";
			$this->enp_dir ="custom/outright_products/entryPoints/";
			$this->enp_ext_dir ='custom/Extension/application/Ext/EntryPointRegistry/';
			$this->enp_file =$this->enp_dir.$this->enp_file_name;
			$this->entryPoint_location =$this->enp_ext_dir.$this->enp_file_name;


				if(!is_dir( $this->enp_dir)){
					if (!mkdir($this->enp_dir, 0777, true)) {
					die('Failed to create folders...'.$this->enp_dir);
					}
			}

				if(!is_dir( $this->enp_ext_dir)){
					if (!mkdir($this->enp_ext_dir, 0777, true)) {
					die('Failed to create folders...'.$this->enp_ext_dir);
					}
			}
		
		$this->default_code= ' echo "EntryPoint Succesfully worked for  '.$_REQUEST['enp_name'].'";
		echo "<hr/>";
		echo "<br/>Please edit your code at : '.$this->enp_file.'";';
				
	   }

		function outright_create_entrypoint_files(){
		
			$enp_text ='<?php

			$entry_point_registry["'.$_REQUEST['enp_name'].'"] = array(
			"file" => "'.$this->enp_file.'",
			"auth" => false
			);

			';
		
			file_put_contents( $this->entryPoint_location ,$enp_text);
			
			$default_code ='<?php
';
if($_REQUEST['enp_type']=='json'){
		$default_code .= 'ob_start();
		error_reporting(0);
		$return_data["entryPoint"] ="EntryPoint Succesfully Loaded";
		header("Content-Type: application/json");
		';
		if($_REQUEST['token_value'] && $_REQUEST['token_name']){
		$default_code .='$headers = getallheaders();

		$real_token ="'.$_REQUEST['token_value'].'";
		$token = !empty($headers["'.$_REQUEST[token_name].'"]) ? $headers["'.$_REQUEST[token_name].'"]: null;
		 if ($token != $real_token) {
				$return_data["success"] =  false;
				$return_data["message"][] = "Unauthorized";
				echo json_encode($return_data);
				die;
			}';
		}
		else{
		$default_code .='echo json_encode($return_data);';
		}
}
else{
			$default_code .=$this->default_code;
}
			file_put_contents( $this->enp_file ,$default_code);
			outright_do_repair();

		}
}
if($_REQUEST['enp_name']){
	echo "EntryPoint Created for ".$_REQUEST['enp_name'];
	echo '<hr/>';

	echo '<b>Go To  <a target="_blank" href="index.php?entryPoint='.$_REQUEST['enp_name'].'">click here</a></b>';
$enp_create = new outright_create_entrypoint();
$enp_create->outright_create_entrypoint_files();
}
else{
	$form_array =array(

		'enp_name'=>array(
			'name'=>'enp_name',
			'type'=>'text',
			'label'=>'EntryPoint Name :',
		),
		'enp_type'=>array(
		'name'=>'enp_type',
		'type'=>'select',
		'options'=>array('normal'=>'normal','json'=>'json','text'=>'text','xml'=>'xml','download'=>'download'),
		'label'=>'EntryPoint Type :',

		),
		'default_code'=>array(
		'name'=>'default_code',
		'type'=>'textarea',
		'label'=>'Default PHP Code to put in file :',

		),	
		'token_name'=>array(
		'name'=>'token_name',
		'type'=>'text',
		'label'=>'Token Field Name :',

		),	
		'token_value'=>array(
		'name'=>'token_value',
		'type'=>'text',
		'label'=>'Token Field Value :',

		),	
		
	);
echo outright_create_simple_form( $form_array );
}


