HCE Project PHP language client API bindings  1.5.1
Hierarchical Cluster Engine PHP Client Interface API
 All Classes Namespaces Files Functions Variables Pages
hce_drce_api.inc.php
Go to the documentation of this file.
1 <?php
14 require_once 'hce_node_api.inc.php';
15 
19 defined('SPHINX_JSON_USE_BASE64') or define('SPHINX_JSON_USE_BASE64', 0);
23 defined('HCE_DRCE_EXEC_DEFAULT_TIMEOUT') or define('HCE_DRCE_EXEC_DEFAULT_TIMEOUT', 1000);
27 defined('HCE_DRCE_EXEC_DEFAULT_TTL') or define('HCE_DRCE_EXEC_DEFAULT_TTL', 1000);
31 defined('HCE_DRCE_ERROR_OK') or define('HCE_DRCE_ERROR_OK', 0);
32 defined('HCE_DRCE_ERROR_CREATE_MESSAGE') or define('HCE_DRCE_ERROR_CREATE_MESSAGE', -100);
33 defined('HCE_DRCE_ERROR_PARSE_RESPONSE') or define('HCE_DRCE_ERROR_PARSE_RESPONSE', -101);
34 defined('HCE_DRCE_ERROR_CREATE_CONNECTION') or define('HCE_DRCE_ERROR_CREATE_CONNECTION', -102);
35 defined('HCE_DRCE_ERROR_WRONG_PARAMETERS') or define('HCE_DRCE_ERROR_WRONG_PARAMETERS', -103);
36 
40 defined('HCE_DRCE_CMD_EXEC') or define('HCE_DRCE_CMD_EXEC', 'DRCE_EXEC');
41 
45 defined('HCE_DRCE_EXEC_FIELD_SESSION') or define('HCE_DRCE_EXEC_FIELD_SESSION', 'session');
46 defined('HCE_DRCE_EXEC_FIELD_COMMAND') or define('HCE_DRCE_EXEC_FIELD_COMMAND', 'command');
47 defined('HCE_DRCE_EXEC_FIELD_INPUT') or define('HCE_DRCE_EXEC_FIELD_INPUT', 'input');
48 defined('HCE_DRCE_EXEC_FIELD_FILES') or define('HCE_DRCE_EXEC_FIELD_FILES', 'files');
52 defined('HCE_DRCE_EXEC_FIELD_TYPE') or define('HCE_DRCE_EXEC_FIELD_TYPE', 'type');
53 defined('HCE_DRCE_EXEC_FIELD_PORT') or define('HCE_DRCE_EXEC_FIELD_PORT', 'port');
54 defined('HCE_DRCE_EXEC_FIELD_USER') or define('HCE_DRCE_EXEC_FIELD_USER', 'user');
55 defined('HCE_DRCE_EXEC_FIELD_PASSWD') or define('HCE_DRCE_EXEC_FIELD_PASSWD', 'password');
56 defined('HCE_DRCE_EXEC_FIELD_SHELL') or define('HCE_DRCE_EXEC_FIELD_SHELL', 'shell');
57 defined('HCE_DRCE_EXEC_FIELD_ENV') or define('HCE_DRCE_EXEC_FIELD_ENV', 'environment');
58 defined('HCE_DRCE_EXEC_FIELD_TIMEOUT') or define('HCE_DRCE_EXEC_FIELD_TIMEOUT', 'timeout');
59 defined('HCE_DRCE_EXEC_FIELD_TMODE') or define('HCE_DRCE_EXEC_FIELD_TMODE', 'tmode');
60 defined('HCE_DRCE_EXEC_FIELD_HOME_DIR') or define('HCE_DRCE_EXEC_FIELD_HOME_DIR', '');
61 defined('HCE_DRCE_EXEC_FIELD_TIME_MAX') or define('HCE_DRCE_EXEC_FIELD_TIME_MAX', 'time_max');
62 defined('HCE_DRCE_EXEC_FIELD_TIME_MAX_DEFAULT') or define('HCE_DRCE_EXEC_FIELD_TIME_MAX_DEFAULT', 30000);
63 defined('HCE_DRCE_EXEC_FIELD_TMODE_DEFAUL') or define('HCE_DRCE_EXEC_FIELD_TMODE_DEFAUL', 1);
64 defined('HCE_DRCE_EXEC_FIELD_CLEANUP') or define('HCE_DRCE_EXEC_FIELD_CLEANUP', 'cleanup');
65 defined('HCE_DRCE_EXEC_FIELD_CLEANUP_YES') or define('HCE_DRCE_EXEC_FIELD_CLEANUP_YES', 1);
66 defined('HCE_DRCE_EXEC_FIELD_CLEANUP_NO') or define('HCE_DRCE_EXEC_FIELD_CLEANUP_NO', 0);
70 defined('HCE_DRCE_EXEC_FIELD_LIMITS') or define('HCE_DRCE_EXEC_FIELD_LIMITS', 'limits');
74 defined('HCE_DRCE_EXEC_FIELD_NAME') or define('HCE_DRCE_EXEC_FIELD_NAME', 'name');
75 defined('HCE_DRCE_EXEC_FIELD_DATA') or define('HCE_DRCE_EXEC_FIELD_DATA', 'data');
76 defined('HCE_DRCE_EXEC_FIELD_ACTION') or define('HCE_DRCE_EXEC_FIELD_ACTION', 'action');
80 defined('HCE_DRCE_EXEC_FIELD_ERR_CODE') or define('HCE_DRCE_EXEC_FIELD_ERR_CODE', 'error_code');
81 defined('HCE_DRCE_EXEC_FIELD_ERR_MSG') or define('HCE_DRCE_EXEC_FIELD_ERR_MSG', 'error_message');
82 defined('HCE_DRCE_EXEC_FIELD_TIME') or define('HCE_DRCE_EXEC_FIELD_TIME', 'time');
83 defined('HCE_DRCE_EXEC_FIELD_RESULTS') or define('HCE_DRCE_EXEC_FIELD_RESULTS', 'results');
87 defined('HCE_DRCE_EXEC_FIELD_STDOUT') or define('HCE_DRCE_EXEC_FIELD_STDOUT', 'stdout');
88 defined('HCE_DRCE_EXEC_FIELD_STDERR') or define('HCE_DRCE_EXEC_FIELD_STDERR', 'stderror');
89 defined('HCE_DRCE_EXEC_FIELD_FILES') or define('HCE_DRCE_EXEC_FIELD_FILES', 'files');
90 defined('HCE_DRCE_EXEC_FIELD_NODE') or define('HCE_DRCE_EXEC_FIELD_NODE', 'node');
91 defined('HCE_DRCE_EXEC_FIELD_TIME') or define('HCE_DRCE_EXEC_FIELD_TIME', 'time');
95 defined('HCE_DRCE_REQUEST_TYPE_FIELD') or define('HCE_DRCE_REQUEST_TYPE_FIELD', 'type');
96 defined('HCE_DRCE_REQUEST_ID_FIELD') or define('HCE_DRCE_REQUEST_ID_FIELD', 'id');
97 defined('HCE_DRCE_REQUEST_DATA_FIELD') or define('HCE_DRCE_REQUEST_DATA_FIELD', 'data');
98 defined('HCE_DRCE_REQUEST_SUBTASKS_FIELD') or define('HCE_DRCE_REQUEST_SUBTASKS_FIELD', 'subtasks');
102 defined('HCE_DRCE_REQUEST_TYPE_SET') or define('HCE_DRCE_REQUEST_TYPE_SET', 0);
103 defined('HCE_DRCE_REQUEST_TYPE_CHECK') or define('HCE_DRCE_REQUEST_TYPE_CHECK', 1);
104 defined('HCE_DRCE_REQUEST_TYPE_TERMINATE') or define('HCE_DRCE_REQUEST_TYPE_TERMINATE', 2);
105 defined('HCE_DRCE_REQUEST_TYPE_GET') or define('HCE_DRCE_REQUEST_TYPE_GET', 3);
106 defined('HCE_DRCE_REQUEST_TYPE_DELETE') or define('HCE_DRCE_REQUEST_TYPE_DELETE', 4);
110 defined('HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE') or define('HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE', 1);
111 defined('HCE_DRCE_REQUEST_TYPE_CHECK_EXTENDED') or define('HCE_DRCE_REQUEST_TYPE_CHECK_EXTENDED', 2);
115 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_ALG') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_ALG', 'alg');
116 defined('HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE') or define('HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE', 1);
117 defined('HCE_DRCE_EXEC_TERMINATE_ALG_EXTENDED') or define('HCE_DRCE_EXEC_TERMINATE_ALG_EXTENDED', 2);
121 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY', 'delay');
122 defined('HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT') or define('HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT', 1000);
126 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT', 'repeat');
127 defined('HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT') or define('HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT', 3);
131 defined('HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL') or define('HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL', 'sinal');
132 defined('HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT') or define('HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT', 15);
136 defined('HCE_DRCE_REQUEST_TYPE_FETCH_DELETE') or define('HCE_DRCE_REQUEST_TYPE_FETCH_DELETE', 1);
137 defined('HCE_DRCE_REQUEST_TYPE_FETCH_NOT_DELETE') or define('HCE_DRCE_REQUEST_TYPE_FETCH_NOT_DELETE', 2);
141 defined('HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT') or define('HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT', 'READ_FROM_FILE:');
142 
143 /*
144  * @desc make DRCE request request json message
145  * @param $parameters_array - parameters array corresponds to the DRCE FO protocol Functional_object_message_format.docx
146  * array(HCE_DRCE_EXEC_FIELD_Q=>'query string',
147  * HCE_DRCE_EXEC_FIELD_PARAMETERS=>array(array(HCE_DRCE_EXEC_FIELD_QUERY_ID=>123), array(HCE_DRCE_EXEC_FIELD_JSON_TYPE=>15))
148  * )
149  *
150  * @return string json encoded message or negative value in case of error: HCE_DRCE_ERROR_WRONG_PARAMETERS - wrong parameters array structure
151  */
153  //Default DRCE request array initialization
154  switch($type){
155  case HCE_DRCE_REQUEST_TYPE_SET : {
156  $ret=array(HCE_DRCE_EXEC_FIELD_SESSION=>
157  array(HCE_DRCE_EXEC_FIELD_TYPE=>0,
158  HCE_DRCE_EXEC_FIELD_PORT=>0,
159  HCE_DRCE_EXEC_FIELD_USER=>'',
160  HCE_DRCE_EXEC_FIELD_PASSWD=>'',
161  HCE_DRCE_EXEC_FIELD_SHELL=>'',
162  HCE_DRCE_EXEC_FIELD_ENV=>array(),
163  HCE_DRCE_EXEC_FIELD_HOME_DIR=>'',
164  HCE_DRCE_EXEC_FIELD_TIMEOUT=>0,
165  HCE_DRCE_EXEC_FIELD_TMODE=>HCE_DRCE_EXEC_FIELD_TMODE_DEFAUL,
166  HCE_DRCE_EXEC_FIELD_TIME_MAX=>HCE_DRCE_EXEC_FIELD_TIME_MAX_DEFAULT,
167  HCE_DRCE_EXEC_FIELD_CLEANUP=>HCE_DRCE_EXEC_FIELD_CLEANUP_YES,
168  ),
169  HCE_DRCE_EXEC_FIELD_COMMAND=>'',
170  HCE_DRCE_EXEC_FIELD_INPUT=>'',
171  HCE_DRCE_EXEC_FIELD_FILES=>array()
172  );
173  break;
174  }
175  case HCE_DRCE_REQUEST_TYPE_CHECK : {
176  $ret=array(HCE_DRCE_EXEC_FIELD_TYPE=>HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE);
177  break;
178  }
179  case HCE_DRCE_REQUEST_TYPE_TERMINATE : {
180  $ret=array(
181  HCE_DRCE_EXEC_FIELD_TERMINATE_ALG=>HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE,
182  HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY=>HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT,
183  HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT=>HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT,
184  HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL=>HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT
185  );
186  break;
187  }
188  case HCE_DRCE_REQUEST_TYPE_GET : {
189  $ret=array(
190  HCE_DRCE_EXEC_FIELD_TYPE=>HCE_DRCE_REQUEST_TYPE_FETCH_DELETE
191  );
192  break;
193  }
194  case HCE_DRCE_REQUEST_TYPE_DELETE : {
195  $ret=array();
196  break;
197  }
198  }
199 
200  if(is_array($parameters)){
201  //Create json array from parameters array
202  //TODO:
203  }else{
204  //Create from json content string
205  $json_in=@json_decode($parameters, true);
206  if(is_array($json_in)){
207  //Fill json array fields according with protocol specification
208  switch($type){
209  case HCE_DRCE_REQUEST_TYPE_SET : {
210  //Fill session array
211  if(isset($json_in[HCE_DRCE_EXEC_FIELD_SESSION])){
212  foreach($ret[HCE_DRCE_EXEC_FIELD_SESSION] as $key=>$val){
213  if(isset($json_in[HCE_DRCE_EXEC_FIELD_SESSION][$key])){
214  $ret[HCE_DRCE_EXEC_FIELD_SESSION][$key]=$json_in[HCE_DRCE_EXEC_FIELD_SESSION][$key];
215  }
216  }
217  }
218  //Fill command
219  if(isset($json_in[HCE_DRCE_EXEC_FIELD_COMMAND])){
220  $ret[HCE_DRCE_EXEC_FIELD_COMMAND]=$json_in[HCE_DRCE_EXEC_FIELD_COMMAND];
221  }
222  //Fill input stream
223  if(isset($json_in[HCE_DRCE_EXEC_FIELD_INPUT])){
224  $ret[HCE_DRCE_EXEC_FIELD_INPUT]=$json_in[HCE_DRCE_EXEC_FIELD_INPUT];
225  }
226  //Fill files array
227  if(isset($json_in[HCE_DRCE_EXEC_FIELD_FILES]) && is_array($json_in[HCE_DRCE_EXEC_FIELD_FILES])){
228  foreach($json_in[HCE_DRCE_EXEC_FIELD_FILES] as $key=>$value){
229  if(is_array($value) && isset($value[HCE_DRCE_EXEC_FIELD_NAME]) && isset($value[HCE_DRCE_EXEC_FIELD_DATA]) && isset($value[HCE_DRCE_EXEC_FIELD_ACTION])){
230  if(strlen($value[HCE_DRCE_EXEC_FIELD_DATA])>15 && substr($value[HCE_DRCE_EXEC_FIELD_DATA], 0, 15)==HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT){
231  //Read content of file and replace data stream
232  $file_name=substr($value[HCE_DRCE_EXEC_FIELD_DATA], 15);
233  if(file_exists($file_name)){
234  $value[HCE_DRCE_EXEC_FIELD_DATA]=file_get_contents($file_name);
235  if(($value[HCE_DRCE_EXEC_FIELD_ACTION]+0) & 2147483648/*0x80000000*/){
236  $value[HCE_DRCE_EXEC_FIELD_DATA]=base64_encode($value[HCE_DRCE_EXEC_FIELD_DATA]);
237  }
238  }else{
239  $value[HCE_DRCE_EXEC_FIELD_DATA]='File not found: '.$file_name;
240  }
241  }
242  $ret[HCE_DRCE_EXEC_FIELD_FILES][]=array(
243  HCE_DRCE_EXEC_FIELD_NAME=>$value[HCE_DRCE_EXEC_FIELD_NAME],
244  HCE_DRCE_EXEC_FIELD_DATA=>$value[HCE_DRCE_EXEC_FIELD_DATA],
245  HCE_DRCE_EXEC_FIELD_ACTION=>($value[HCE_DRCE_EXEC_FIELD_ACTION]+0)
246  );
247  }
248  }
249  }
250  //Fill limits array
251  if(isset($json_in[HCE_DRCE_EXEC_FIELD_LIMITS])){
252  foreach($ret[HCE_DRCE_EXEC_FIELD_LIMITS] as $key=>$val){
253  if(isset($json_in[HCE_DRCE_EXEC_FIELD_LIMITS][$key])){
254  $ret[HCE_DRCE_EXEC_FIELD_LIMITS][$key]=$json_in[HCE_DRCE_EXEC_FIELD_LIMITS][$key];
255  }
256  }
257  }
258  break;
259  }
260  case HCE_DRCE_REQUEST_TYPE_CHECK : {
261  //Fill check type
262  if(isset($json_in[HCE_DRCE_EXEC_FIELD_TYPE])){
263  $ret[HCE_DRCE_EXEC_FIELD_TYPE]=$json_in[HCE_DRCE_EXEC_FIELD_TYPE];
264  }else{
265  $ret[HCE_DRCE_EXEC_FIELD_TYPE]=HCE_DRCE_REQUEST_TYPE_CHECK_SIMPLE;
266  }
267  break;
268  }
269  case HCE_DRCE_REQUEST_TYPE_TERMINATE : {
270  //Fill terminate algorithm
271  if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG])){
272  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG];
273  }else{
274  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_ALG]=HCE_DRCE_EXEC_TERMINATE_ALG_SIMPLE;
275  }
276  //Fill terminate delay
277  if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY])){
278  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY];
279  }else{
280  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_DELAY]=HCE_DRCE_EXEC_TERMINATE_DELAY_DEFAULT;
281  }
282  //Fill terminate repeat
283  if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT])){
284  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT];
285  }else{
286  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_REPEAT]=HCE_DRCE_EXEC_TERMINATE_REPEAT_DEFAULT;
287  }
288  //Fill terminate signal
289  if(isset($json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL])){
290  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL]=$json_in[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL];
291  }else{
292  $ret[HCE_DRCE_EXEC_FIELD_TERMINATE_SIGNAL]=HCE_DRCE_EXEC_TERMINATE_SIGNAL_DEFAULT;
293  }
294  break;
295  }
296  case HCE_DRCE_REQUEST_TYPE_GET : {
297  //Fill fetch type
298  if(isset($json_in[HCE_DRCE_EXEC_FIELD_TYPE])){
299  $ret[HCE_DRCE_EXEC_FIELD_TYPE]=$json_in[HCE_DRCE_EXEC_FIELD_TYPE];
300  }else{
301  $ret[HCE_DRCE_EXEC_FIELD_TYPE]=HCE_DRCE_REQUEST_TYPE_FETCH_DELETE;
302  }
303  break;
304  }
305  case HCE_DRCE_REQUEST_TYPE_DELETE : {
306  //According to protocol not exist fields for filling
307  }
308  break;
309  }
310  }else{
311  echo 'Parsing input json file error: '.json_last_error();
312  exit(1);
313  }
314  }
315 
316  return $ret;
317  }
318 
319 
320 /*
321  * @desc create DRCE exec request in json format for DRCE Cluster ($protocol==0) or HCE Admin ($protocol==1) interfaces protocol
322  * @param $parameters_array - array of items to construct json formatted exec request;
323  * @param $type - request action type;
324  * @param $id - request/task Id;
325  * @param $ttl - request TTL;
326  *
327  * @return DRCE exec request string in json format if success or null in case of error
328  */
329  function hce_drce_exec_prepare_request($parameters_array, $type, $id, $ttl, $subtasks=array()){
330  $ret=null;
331 
332  $drce_admin_protocol=hce_drce_exec_prepare_request_admin($parameters_array, $type, $id, $subtasks);
333 
334  if(SPHINX_JSON_USE_BASE64){
335  $drce_admin_protocol=base64_encode($drce_admin_protocol);
336  }
337 
338  $ret=json_encode(array(
339  HCE_HANDLER_COVER_FIELD_TYPE=>HCE_HANDLER_TYPE_DRCE,
340  HCE_HANDLER_COVER_FIELD_DATA=>$drce_admin_protocol,
341  HCE_HANDLER_COVER_FIELD_TTL=>$ttl
342  )
343  );
344 
345  return $ret;
346  }
347 
348 /*
349  * @desc create DRCE request in json format for HCE Admin interface protocol
350  * @param $parameters_array - array of items to construct json formatted exec request;
351  * @param $type - request action type;
352  * @param $id - request/task Id;
353  *
354  * @return DRCE exec request string in json format if success or null in case of error
355  */
356  function hce_drce_exec_prepare_request_admin($parameters_array, $type, $id, $subtasks=array()){
357  $ret=null;
358 
359  $ret=json_encode(
360  array(
361  HCE_DRCE_REQUEST_TYPE_FIELD=>$type,
362  HCE_DRCE_REQUEST_DATA_FIELD=>base64_encode(json_encode($parameters_array)),
363  HCE_DRCE_REQUEST_ID_FIELD=>$id,
364  HCE_DRCE_REQUEST_SUBTASKS_FIELD=>$subtasks
365  )
366  );
367  return $ret;
368  }
369 
370 
371 /*
372  * @desc parse DRCE admin response in json format
373  * @param $response_body - content of response json string
374  *
375  * @return ACN DRCE admin request response array
376  */
377  function hce_drce_exec_parse_response($response_body){
378  return json_decode($response_body, true);
379  }
380 
381 /*
382  * @desc parse DRCE request response in json format
383  * @param $response_body - content of response json string
384  *
385  * @return ACN DRCE request request response array
386  */
387  function hce_drce_exec_parse_response_json($response_body){
388  $ret=array('Error'=>'', 'Data'=>null);
389 
390  $tmp=json_decode($response_body, true);
391 
392  if(isset($tmp[HCE_DRCE_EXEC_FIELD_TYPE]) && $tmp[HCE_DRCE_EXEC_FIELD_TYPE]==HCE_HANDLER_TYPE_DRCE){
393  if(isset($tmp[HCE_DRCE_EXEC_FIELD_DATA])){
394  if(SPHINX_JSON_USE_BASE64){
395  $ret['Data']=json_decode(base64_decode($tmp[HCE_DRCE_EXEC_FIELD_DATA]), true);
396  }else{
397  $ret['Data']=json_decode($tmp[HCE_DRCE_EXEC_FIELD_DATA], true);
398  }
399  }else{
400  //Structure error
401  $ret['Error']='Structure error: `data` field not found!';
402  }
403  }else{
404  //Structure or type value error
405  $ret['Error']='Structure error: `type` field not found or type is not a proper value!';
406  }
407 
408  return $ret;
409  }
410 
411 
412 /*
413  * @desc parse DRCE subtasks file content in json format
414  * @param $subtasks - content of subtasks file json string
415  *
416  * @return subtasks json array
417  */
418  function hce_drce_create_subtasks_array($subtasks){
419  //echo 'subtasks3:'.cli_prettyPrintJson($subtasks, ' ').PHP_EOL;
420 
421  $json_in=json_decode($subtasks, true);
422 
423  $ret=array();
424 
425  //Fill subtasks array
426  if(is_array($json_in)){
427  foreach($json_in as $subtask_item){
428  //Chck 'data' field
429  if(is_array($subtask_item) && isset($subtask_item[HCE_DRCE_EXEC_FIELD_DATA]) && is_string($subtask_item[HCE_DRCE_EXEC_FIELD_DATA])){
430  if(strlen($subtask_item[HCE_DRCE_EXEC_FIELD_DATA])>15 && substr($subtask_item[HCE_DRCE_EXEC_FIELD_DATA], 0, 15)==HCE_DRCE_REQUEST_READ_FROM_FILE_STATEMENT){
431  $file_name=substr($subtask_item[HCE_DRCE_EXEC_FIELD_DATA], 15);
432  if(file_exists($file_name)){
433  $subtask_data=file_get_contents($file_name);
434  }else{
435  $subtask_data='file not found "'.$file_name.'"';
436  }
437  }else{
438  $subtask_data=$subtask_item[HCE_DRCE_EXEC_FIELD_DATA];
439  }
440  $subtask_item[HCE_DRCE_EXEC_FIELD_DATA]=base64_encode($subtask_data);
441  $ret[]=$subtask_item;
442  }elseif(is_array($subtask_item) && isset($subtask_item[HCE_DRCE_EXEC_FIELD_DATA]) && is_array($subtask_item[HCE_DRCE_EXEC_FIELD_DATA])){
443  $subtask_data=json_encode($subtask_item[HCE_DRCE_EXEC_FIELD_DATA]);
444  $subtask_item[HCE_DRCE_EXEC_FIELD_DATA]=base64_encode($subtask_data);
445  }
446  //Chck 'subtasks' field
447  if(is_array($subtask_item) && isset($subtask_item[HCE_DRCE_REQUEST_SUBTASKS_FIELD]) && is_array($subtask_item[HCE_DRCE_REQUEST_SUBTASKS_FIELD])){
448  $subtask_item[HCE_DRCE_REQUEST_SUBTASKS_FIELD]=hce_drce_create_subtasks_array(json_encode($subtask_item[HCE_DRCE_REQUEST_SUBTASKS_FIELD]));
449  }
450  $ret[]=$subtask_item;
451  }
452  }
453 
454  return $ret;
455  }
456 
457 ?>