Please read the comments, and check the video first.
include("xcloud_core/class.php");
// 1. New object of the clss JJX7Cloud
$X7Cloud = NEW JJX7Cloud();
// 2.1 - HEADERS DEFINITION :: USE CONSTANTS TO AVOID ANY SPELL PROBLEM
define('TEXT' , 'TEXT');
define('JSON' , 'JSON');
define('XML' , 'xml');
define('AUDIO' , 'AUDIO');
define('PDF' , 'pDF');
define('IMAGEN' , 'imagen');
define('CSV' , 'CSV');
// 2.2 - add_Header_Type( NAME OF THE NEW HEADER TYPE );
$X7Cloud->add_Header_Type( TEXT );
$X7Cloud->add_Header_Type( JSON );
$X7Cloud->add_Header_Type( XML );
$X7Cloud->add_Header_Type( AUDIO );
$X7Cloud->add_Header_Type( PDF );
$X7Cloud->add_Header_Type( IMAGEN );
$X7Cloud->add_Header_Type( CSV );
// 2.3 - WE SET THE NEW OVERLOAD FUNCTION NAME
$X7Cloud->set_Header_Function("header_type_logic");
// 2.4 - LOGIC
// This function new 2 parameters header_type_logic( DATA , TYPE ):
// THE MODE OF RETURN DATA MUST BE WITH "ECHO" NOT WITH "RETURN"
FUNCTION header_type_logic($data,$tipo){
//for a more faster performance use SWITCH
SWITCH (strtoupper($tipo)){
DEFAULT :
ECHO X7Cloud::ERROR_4;EXIT();
BREAK;
//add Headers Here ----!
CASE strtoupper("text");
ECHO $data;
BREAK;
CASE strtoupper("json"):
header('Content-Description: response');
//header('Content-Type: application/json');
header('Content-Disposition: inline; filename=respuesta.json');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
ECHO $data;
exit;
BREAK;
CASE strtoupper("xml"):
header('Content-Description: response');
header('Content-Type: application/xml');
header('Content-Disposition: inline; filename=respuesta.xml');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
ob_clean();
flush();
ECHO $data;
exit;
BREAK;
CASE strtoupper("audio"):
IF (file_exists($data)) {
$file = $data ;
$fp = fopen($file, 'r');
$content = fread($fp, filesize($file));
fclose($fp);
// Set the headers - header( 'Content-Type: audio/mpeg');
header('Content-Transfer-Encoding: binary');
header('Content-disposition: attachment; filename="'.basename($data)).'"';
header('Content-type: audio/mpeg');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
header('Content-Length: ' . filesize($file).';');
header_remove("Expires");
header_remove("Pragma");
echo $content;
EXIT;
} ELSE { ECHO "NOT FOUND";EXIT;}
BREAK;
CASE strtoupper("imagen"):
IF (file_exists($data)) {
header('Content-Description: imagen');
header('Content-Type: image/'.pathinfo($data,PATHINFO_EXTENSION).'');
header('Content-Disposition: inline; filename="'.basename($data)).'"';
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($data));
ob_clean();
flush();
echo readfile($data);
exit;
} ELSE { ECHO "NOT FOUND";EXIT;}
BREAK;
CASE strtoupper("Pdf"):
IF (file_exists($data)) {
header('Content-Description: Pdf');
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.basename($data)).'"';
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($data));
ob_clean();
flush();
readfile($data);
exit;
} ELSE { ECHO "NOT FOUND";EXIT;}
BREAK;
CASE strtoupper("csv"):
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename=report.xls');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-Type: application/force-download');
header('Content-Transfer-Encoding: binary');
ECHO $data;
exit;
BREAK;
}
}
// 3.1 - DATA TYPE DEFINITION :: USE CONSTANTS TO AVOID ANY SPELL PROBLEM
define('VVARCHAR' , 'VARCHAR'); // "TESTE" : LETRAS _ NUMEROS _ SIGNOS NORMALES EN COMILLAS
define('VINTEGER' , 'INTEGER');
define('HTML' , 'HTML');
define('URLS' , 'URLS');
define('DATE' , 'DATE');
define('NORMAL' , 'nORmal');
// 3.2 - add_Filter_Data_Type( NAME OF THE NEW DATA TYPE );
$X7Cloud->add_Filter_Data_Type( VVARCHAR ); //Only text , number and normal signs
$X7Cloud->add_Filter_Data_Type( VINTEGER ); //Only numbers and numeric simbols
$X7Cloud->add_Filter_Data_Type( HTML ); //VVARCHAR + SOME HTML TAGS
$X7Cloud->add_Filter_Data_Type( URLS ); //Only url chars
$X7Cloud->add_Filter_Data_Type( NORMAL ); //no sanitize
$X7Cloud->add_Filter_Data_Type( DATE ); //convert date function for sql
// 3.3 - WE SET THE NEW OVERLOAD FUNCTION NAME
$X7Cloud->set_Data_Type_Function("data_type_logic");
// 3.4 - LOGIC
// This function new 2 parameters data_type_logic( DATA , TYPE ):
// this function must return the data if correct else return FALSE
// note: if you want u can clean some characters from here but always
// THE MODE OF RETURN DATA MUST BE WITH "RETURN" NOT WITH "ECHO"
FUNCTION data_type_logic($data,$tipo,$validate){
SWITCH (strtoupper($tipo)){
DEFAULT :
ECHO X7Cloud::ERROR_18;EXIT();
BREAK;
CASE strtoupper("VARCHAR"):
IF ($validate==TRUE)IF (filter_var($data, FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => "#^[A-Za-z0-9\\-+.,_ ]+$#D")))==FALSE)RETURN FALSE;
$data = filter_var($data, FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES | FILTER_FLAG_ENCODE_AMP);
RETURN ($data != "") ? $data : FALSE;
BREAK;
CASE strtoupper("INTEGER"):
IF ($validate==TRUE) IF (filter_var($data , FILTER_VALIDATE_INT)==FALSE) RETURN FALSE;
$data = filter_var($data, FILTER_SANITIZE_NUMBER_INT );
RETURN ($data != "") ? intval($data) : FALSE;
BREAK;
CASE strtoupper("HTML"):
$data = filter_var($data, FILTER_SANITIZE_SPECIAL_CHARS);
RETURN ($data != "") ? $data : FALSE;
BREAK;
CASE strtoupper("URLS"):
$data = filter_var($data, FILTER_SANITIZE_URL);
RETURN ($data != "") ? $data : FALSE;
BREAK;
CASE strtoupper("nORmal"):
$data = filter_var($data, FILTER_UNSAFE_RAW );
RETURN ($data != "") ? $data : FALSE;
BREAK;
CASE strtoupper("DATE"):
$date_parse = explode("/",$data );
$data = $date_parse[2]."/".$date_parse[1]."/".$date_parse[0];
RETURN ($data != "") ? date($data) : FALSE;
BREAK;
}
}
// 4.1 - TEMPLATE DEFINITION :: USE CONSTANTS TO AVOID ANY SPELL PROBLEM
define('CRUDO' , 'CRUDO');
define('JSON_GRID' , 'json_grid');
define('JSON_FORM' , 'json_form');
define('JSON_MENSAJE' , 'json_mensaje');
define('XML_MENSAJE' , 'XML_mensaje');
define('XML_GRID' , 'xml_grid');
// 4.2 - add_Template_Type( NAME OF THE NEW HEADER TYPE );
$X7Cloud->add_Template_Type( CRUDO );
$X7Cloud->add_Template_Type( JSON_GRID );
$X7Cloud->add_Template_Type( JSON_FORM );
$X7Cloud->add_Template_Type( JSON_MENSAJE);
$X7Cloud->add_Template_Type( XML_MENSAJE );
$X7Cloud->add_Template_Type( XML_GRID );
// 4.3 - WE SET THE NEW OVERLOAD FUNCTION NAME
$X7Cloud->set_Template_Type_Function("template_type_logic");
// 4.4 - LOGIC
// This function new 3 parameters template_type_logic( DATA , TYPE ,PARAMS ):
FUNCTION template_type_logic($data,$type,array $params = null ){
GLOBAL $X7Cloud;
//For easy manange we put al libraries in libraries folder.
return include "libraries/default.php";
}
// 5 - BASIC CONFIGURATION
define('MODULE' , '0000'); //recomend not chnge this option nd use by defult
define('VERB' , '1111'); //recomend not chnge this option nd use by defult
define('HASH' , '7777'); //recomend not chnge this option nd use by defult
$X7Cloud->set_Module_Var(MODULE); //set the GET NAME VAR for filter modules
$X7Cloud->set_Verb_Var(VERB); //set the GET NAME VAR for filter verbs
$X7Cloud->set_LogMode(TRUE); //For loggin All , db calls are automatics
$X7Cloud->set_LogDir(""); //if empty string will save in this root
$X7Cloud->set_ActivateDocs(TRUE); //if you wAnt to show docs you need to set this first TRUE
$X7Cloud->set_Hash(FALSE,HASH); //set true if you want use hash security
$X7Cloud->SetTimeout(15); //MINUTes to timeout
$X7Cloud->set_Webservice_Url("http://localhost/ws/"); //Url of the cloud for docs porpouse
//$X7Cloud->set_database("mysql", "localhost:777" ,null, "agenda" , "root" , "root" ); //db config
$X7Cloud->set_database("sql", $hostname_sistema ,null, $database_sistema , $username_sistema , $password_sistema );
// 6 - VERBS/MODULES DEFINITION :: USE CONSTANTS TO AVOID ANY SPELL PROBLEM
// 6.1 VERBS
define('GET' , 0); //USE THIS FOR GET ACTIONS
define('LIS' , 1); //USE THIS FOR LIST ACTIONS
define('ADD' , 2); //USE THIS FOR ADD ACTIONS
define('UPD' , 3); //USE THIS FOR UPDATE ACTIONS
define('DEL' , 4); //USE THIS FOR DELETE ACTIONS
define('DOIT' , 5); //USER THIS FORM COMNDS WITH NO DATA RETURN
define('OPT' , 6); //USE THIS FOR OPTIONS
define('METADATA' , 7); //USE THIS FOR RETURN METADATA OF GRIDS/FORMS
define('RESOURCE' , 8); //USER THIS
// 6.2 MODULES
define('MOD_DOCS' , 0); //SMPLE OF LIGHT DOCUMENTTION
define('MOD_JSON' , 1); //SAMPLE OF DATA IN JSON STRCUTRE
define('MOD_XML' , 2); //SAMPLE OF DATA IN XML STRCUTRE
define('MOD_RESOURCES' , 3); //SAMPLE OF DATA IN DIFERENTES FORMTS
define('MOD_LOGIC' , 4); //SAMPLE OF LOGICS OF WEBSERVICE
// 6.3 LOGIC
// This function add_Module needs 2 parameters add_Module( NUMERIC ID , SHORT DESCRIPTION ):
// This function add_SubModule needs 4 parameters add_SubModul6e( NUMERIC PARENT ID , NUMERIC ID, FUNCTION, SHORT DESCRIPTION ):
$X7Cloud->add_Module ( MOD_DOCS , "MOD_DOCS" );
$X7Cloud->add_SubModule ( MOD_DOCS , GET , "SHOW_DOCS" , "Show docs" );
$X7Cloud->add_Module ( MOD_JSON , "MOD_JSON" );
$X7Cloud->add_SubModule ( MOD_JSON , GET , "SIMPLE_JSON" , "SIMPLE_JSON" );
$X7Cloud->add_SubModule ( MOD_JSON , GET ."1" , "COMPLEX_JSON" , "COMPLEX_JSON" );
$X7Cloud->add_SubModule ( MOD_JSON , GET ."2" , "DATABASE_JSON" , "DATABASE_JSON" );
$X7Cloud->add_Module ( MOD_XML , "MOD_XML" );
$X7Cloud->add_SubModule ( MOD_XML , GET , "SIMPLE_XML" , "SIMPLE_XML" );
$X7Cloud->add_SubModule ( MOD_XML , GET ."1" , "COMPLEX_XML" , "COMPLEX_XML" );
$X7Cloud->add_SubModule ( MOD_XML , GET ."2" , "DATABASE_XML" , "DATABASE_XML" );
$X7Cloud->add_Module ( MOD_RESOURCES , "MOD_RESOURCES" );
$X7Cloud->add_SubModule ( MOD_RESOURCES , GET , "RECURSO_MP3" , "RECURSO_MP3" );
$X7Cloud->add_SubModule ( MOD_RESOURCES , GET ."1" , "RECURSO_PDF" , "RECURSO_PDF" );
$X7Cloud->add_SubModule ( MOD_RESOURCES , GET ."2" , "RECURSO_IMG" , "RECURSO_IMG" );
$X7Cloud->add_Module ( MOD_LOGIC , "MOD_LOGIC" );
$X7Cloud->add_SubModule ( MOD_LOGIC , 1 , "FUNC_CREATE_SESSION" , "FUNC_CREATE_SESSION" );
$X7Cloud->add_SubModule ( MOD_LOGIC , 2 , "FUNC_SESSION_CHECK" , "FUNC_SESSION_CHECK" );
$X7Cloud->add_SubModule ( MOD_LOGIC , 3 , "FUNC_DELETE_SESSION" , "FUNC_DELETE_SESSION" );
$X7Cloud->add_SubModule ( MOD_LOGIC , 4 , "FUNC_GET_VALUES" , "FUNC_GET_VALUES" );
// 7 RUN WEBSERVICE
$X7Cloud->__init();
/*
$X7Cloud->set_DebugMode(TRUE);
$X7Cloud->_Debug_Show_Hash();
*/
// 8 ADD FUNCTIONS HERE
FUNCTION SHOW_DOCS (){
GLOBAL $X7Cloud;
//SHOW DOCUMENTTION
$X7Cloud->Show_HashDoc();
}
FUNCTION CRUDO_TEXT (){
GLOBAL $X7Cloud;
// This function Cloud_Response needs 4 params,lAst prmeter is optionl.
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
// in this exmple the response will be a RAW TEXT
$X7Cloud->Cloud_Response("hello world!" , TEXT ,CRUDO );
}
FUNCTION SIMPLE_JSON(){
GLOBAL $X7Cloud;
// This function Cloud_Response needs 4 params,lAst prmeter is optionl.
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple the response will be in JSON formt, and whit templte_type "JSON_MENSAJE" structure.
$X7Cloud->Cloud_Response("hello world!" , JSON ,JSON_MENSAJE );
}
FUNCTION COMPLEX_JSON (){
GLOBAL $X7Cloud;
$json_sample = '{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}';
// This function Cloud_Response needs 4 params,lAst prmeter is optionl.
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple we take a JSON STRING and output as JSON format , we use template_type "crudo" because
//we will not touch the json string, because is alredy in json formt
$X7Cloud->Cloud_Response($json_sample , JSON ,CRUDO );
}
FUNCTION DATABASE_JSON (){
GLOBAL $X7Cloud;
// This function Cloud_query need 1 params Cloud_query("sql sintx string")
// for X7CLOUD standrs the query most be a store procedure,why this? read security db section
// cloud_query will return Null if is empty result, Data if is data
// $json_sample = $X7Cloud->cloud_query("exec p_T_l_tree 1");
$json_sample = $X7Cloud->cloud_query("select * from modulos");//only store procedures
//also existe $X7Cloud->command(store procedure call); will just return true or false
$RESULT = array();
IF ( $json_sample == NULL ) $RESULT = array( 'RESULT' => NULL );
//MERGE result array in metada array
$metadata = array_merge ( array( 'parentnode' => 'DATABASE_JSON' ) , $RESULT );
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple the response will be in JSON formt, and whit templte_type "JSON_GRID" structure.
$X7Cloud->Cloud_Response($json_sample , JSON ,JSON_GRID , $metadata );
}
//XML NEEDS METADATA WITH AN FIELD "parentnode" DEFINED if not wil return xml strcuture error
FUNCTION SIMPLE_XML (){
GLOBAL $X7Cloud;
// This function Cloud_Response needs 4 params,lAst prmeter is optionl.
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple the response will be in XML formt, and whit templte_type "XML_MENSAJE" structure.
$metadata = array( 'parentnode' => 'SIMPLE_XML' );
$X7Cloud->Cloud_Response("hello world!" , XML ,XML_MENSAJE, $metadata );
}
FUNCTION COMPLEX_XML (){
GLOBAL $X7Cloud;
$metadata = array( 'parentnode' => 'COMPLEX_XML' );
$XML_sample = '';
// This function Cloud_Response needs 4 params,lAst prmeter is optionl.
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple we take a xml STRING and output as XML format , we use template_type "crudo" because
//we will not touch the XML string, because is alredy in XML formt
$X7Cloud->Cloud_Response($XML_sample , XML ,CRUDO );
}
FUNCTION DATABASE_XML (){
GLOBAL $X7Cloud;
// This function Cloud_query need 1 params Cloud_query("sql sintx string")
// for X7CLOUD standrs the query most be a store procedure,why this? read security db section
// cloud_query will return Null if is empty result, Data if is data
// $json_sample = $X7Cloud->cloud_query("exec p_T_l_tree 1");
$xml_sample = $X7Cloud->cloud_query("select * from modulos");
$RESULT = array();
IF ( $xml_sample == NULL ) $RESULT = array( 'RESULT' => NULL );
//MERGE result array in metada array
$metadata = array_merge ( array( 'parentnode' => 'DATABASE_XML' ) , $RESULT );
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple the response will be in XML formt, and whit templte_type "XML_GRID" structure.
$X7Cloud->Cloud_Response($xml_sample , XML ,XML_GRID , $metadata );
}
//RESOUCE FUNTIONCES
FUNCTION RECURSO_MP3 (){
GLOBAL $X7Cloud;
$url_sample = dirname(__FILE__)."/recursos/los cafres - aire.mp3";
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple the response will be in MP3 FILE formt, and whit templte_type "CRUDO".
// WE USE THE template_type in CRUDO because we dont modify the file
$X7Cloud->Cloud_Response($url_sample , AUDIO ,CRUDO );
}
FUNCTION RECURSO_PDF (){
GLOBAL $X7Cloud;
$url_sample = dirname(__FILE__)."/recursos/Curriculum_james_jara_En.pdf";
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple the response will be in PDF FILE formt, and whit templte_type "CRUDO".
// WE USE THE template_type in CRUDO because we dont modify the file
$X7Cloud->Cloud_Response($url_sample , PDF ,CRUDO );
}
FUNCTION RECURSO_IMG (){
GLOBAL $X7Cloud;
$url_sample = dirname(__FILE__)."/recursos/innosystem_jamesjara.jpg";
// Cloud_Response( data , header_type previus defined , template_type previus defined, aditional metainfo )
//in this exmple the response will be in jpg FILE formt, and whit templte_type "CRUDO".
// WE USE THE template_type in CRUDO because we dont modify the file
$X7Cloud->Cloud_Response($url_sample , IMAGEN ,CRUDO );
}
//LOGIC FUNCTIONS
FUNCTION FUNC_CREATE_SESSION (){
GLOBAL $X7Cloud;
IF ( $X7Cloud->HasSesion() ) die("session IS ALREADY created , destroy it");
//YOU CAN ADD OWN VALUES TO THE SESSION SET
$own_values = array (
'username'=> "JAMESJARA" ,
'owner_id'=> "1",
'custom_value'=> "hello world"
);
//CHEKC DOCUMENTATION TO SEE, DEFAULT STANDARS VALUES OF X7CLOUD
//NewSesion has optional parmeters, you can pass array with custum values
IF ( $X7Cloud->NewSesion( $own_values ) ) {
die("session CREATED");
}
}
FUNCTION FUNC_SESSION_CHECK (){
GLOBAL $X7Cloud;
// HasSesion will result true or false if a session exist
IF ( $X7Cloud->HasSesion() ){
//CHEKC DOCUMENTATION TO SEE, DEFAULT STANDARS VALUES OF X7CLOUD
ECHO $X7Cloud->GetSessionValue('username').""; //STANDART DEFAULT VALUE OF X7CLOUD USED FOR USERNAME , DEFUALT VALUE:NULL
ECHO $X7Cloud->GetSessionValue('owner_id').""; //STANDART DEFAULT VALUE OF X7CLOUD USED FOR USER ID , DEFUALT VALUE:NULL
ECHO $X7Cloud->GetSessionValue('webservice_hash').""; //STANDART DEFAULT VALUE OF X7CLOUD USED FOR UNIQUE HASH USER ,USE ONLY READONLY
ECHO $X7Cloud->GetSessionValue('secure_hash').""; //STANDART DEFAULT VALUE OF X7CLOUD USED FOR SECURITY ,USE ONLY READONLY
ECHO $X7Cloud->GetSessionValue('ultimo_acceso').""; //STANDART DEFAULT VALUE OF X7CLOUD USED FOR TIMEOUT ,USE ONLY READONLY
//the custom value if no exist return null
ECHO $X7Cloud->GetSessionValue('custom_value').""; //STANDART DEFAULT VALUE OF X7CLOUD USED FOR TIMEOUT ,USE ONLY READONLY
} ELSE ECHO "NO sesion";
}
FUNCTION FUNC_DELETE_SESSION (){
GLOBAL $X7Cloud;
//IF SESSION EXIST
IF ( $X7Cloud->HasSesion() ){
//CloseSesion WILL DESTROY SESSION
IF ($X7Cloud->CloseSesion()){
ECHO "SUCEFULL DESTRY SESSION";
} ELSE ECHO "ERROR DESTROYING SESSION";//THIS WILL NEVER HAPPEND
} ELSE ECHO "NO EXISTE ANY SESSION";
}
FUNCTION FUNC_GET_VALUES (){
GLOBAL $X7Cloud;
//NewValue FUNCTION needs 3 parameters , NewValue( VALUE, REGISTERED TYPE, OBLIGATORY FIELD , VALIDATION );
//NewValue will not return anthing, this is complement with can_continue
//can_continue will check validation returning true or false
//somenthing like in VINTEGER Type , the data will be cleaned, thats depende of you in
//data type section, but always must return true or false
ECHO " CanContinue : OBLIGATORY FIELDS
";
ECHO " obligatory var GET['1']
";
ECHO "
";
//OBLIGATORY
$var1 = $X7Cloud->NewValue( @$_GET['1'] , VVARCHAR , TRUE ); //ADD &1=TEST to the url
//SANITIZE
$var2 = $X7Cloud->NewValue( "test