static function execute_cmdline($app_id, $parameters) { $execInfo = EIConfig::get_appExecXML($app_id); $pAuxy = EIConfig::get_appParametersARRAY($app_id); $paramsArr = $pAuxy["parameters"]; // prefix to be attached to each parameter anme $param_prefix = "-"; if ($paramsArr["@attr"] && array_key_exists("prefix", $paramsArr["@attr"])) { $param_prefix = $paramsArr["@attr"]["prefix"]; } // boolean to check compatible parameters or not $param_check = false; if ($paramsArr["@attr"] && array_key_exists("check", $paramsArr["@attr"])) { $param_check = $paramsArr["@attr"]["check"]; } // the parameters template if ($execInfo->cmdlineapp) { $cmdline = (string) $execInfo->cmdlineapp; } else { throw new Exception("Could not find environment cmdlineapp for '" . $app_id . "'"); } // session ID // $sessionid_str = ""; // client ID // $clientid_str = ""; if (array_key_exists('_ei_clientid', $parameters)) { $clientid_str = $parameters['_ei_clientid']; unset($parameters['_ei_clientid']); } // files // $files_str = ""; $root_str = ""; $stream_str = ""; $execid_str = EIApps::token(5); // the temporal directory has the form // // easyinterface_XYZ/_ei_files // // where XYZ are some random characters generated by php. In // unix based systems, this directory is typically placed in // /tmp. In Windows it is placed in temp. // // **IMPORTANT** The use of _ei_files is essential, so clients // can reconstruct file names easily (i.e., whatever comes after // _ei_files is the name passed by the client) $aux = sys_get_temp_dir() . "/easyinterface_" . $execid_str; $dir = str_replace("\\", "/", $aux); unset($aux); mkdir($dir, 0755); if (array_key_exists('_ei_files', $parameters)) { $root_str = $dir . "/_ei_files"; mkdir($root_str, 0755); EIApps::build_directories($files_str, $root_str, $parameters); unset($parameters['_ei_files']); } $stream_str = $dir . "/_ei_stream"; mkdir($stream_str, 0755); // outline // $outline_str = ""; if (array_key_exists('_ei_outline', $parameters)) { $outline_str = implode(" ", $parameters['_ei_outline']); unset($parameters['_ei_outline']); } $typesofparams = array("selectone", "selectmany", "flag"); // other parameters // $parameters_str = ""; // first get what parameters we have to check // then check this. // finish write the rest if param_check is false $local; $localprefix; $localcheck; $localtype; $paramsdone = array(); foreach ($parameters as $key => $values) { $encontrado = false; foreach ($paramsArr as $typep => $paramsgroup) { //Careful! if (array_key_exists("@attr", $paramsgroup)) { //only exists one parameter of type "typep" //paramsgroup = parameter $actual = $paramsgroup; if (!array_key_exists("name", $actual["@attr"])) { throw new Exception("Missing param name in the app config file"); } else { if ($actual["@attr"]["name"] == $key) { //found parameter $local = $actual; //copy to check after. $localtype = $typep; $encontrado = true; break; } } } else { if ($typep != "@attr") { //exists more than one parameter of type "typep" //paramsgroup[N] = [N]parameter foreach ($paramsgroup as $k => $actual) { if (!array_key_exists("name", $actual["@attr"])) { throw new Exception("Missing param name in the app config file"); } else { if ($actual["@attr"]["name"] == $key) { $local = $actual; //copy to check after $localtype = $typep; $encontrado = true; break; } } } //end foreach paramsgroup if ($encontrado) { break; } } } } //end foreach paramsArr*/ if ($encontrado) { //THE PARAMETER IS IN THE VARIABLE local $localprefix = $param_prefix; if (array_key_exists("prefix", $local["@attr"])) { $localprefix = $local["@attr"]["prefix"]; } $localcheck = $param_check; if (array_key_exists("check", $local["@attr"])) { $localcheck = $local["@attr"]["check"]; } if ($localcheck == "true") { //check this parameter switch ($localtype) { case "selectone": if (count($values) != 1) { throw new Exception("This parameter (" . $local["@attr"]["name"] . ") only accept one value"); } if (!EIApps::checkvalue($local, $values[0])) { throw new Exception("Invalid parameter's value: " . $k . "in parameter: " . $local["@attr"]["name"]); } break; case "selectmany": if (count($values) < 1) { throw new Exception("This parameter (" . $local["@attr"]["name"] . ") requeire at least one value"); } foreach ($values as $v) { if (!EIApps::checkvalue($local, $v)) { throw new Exception("Invalid parameter's value: " . $v . " in parameter: " . $local["@attr"]["name"]); } } break; case "flag": $tval = "true"; $fval = "false"; if (array_key_exists("trueval", $local["@attr"])) { $tval = $local["@attr"]["trueval"]; } if (array_key_exists("falseval", $local["@attr"])) { $fval = $local["@attr"]["falseval"]; } if (count($values) > 1) { throw new Exception("This parameter (" . $local["@attr"]["name"] . ") only accept one value"); } else { if (count($values) == 0) { $values[0] = $fval; } } if ($values[0] != $tval && $values[0] != $fval) { throw new Exception("This flag parameter (" . $local["@attr"]["name"] . ") only accept " . $tval . " or " . $fval . ""); } break; case "textfield": break; default: throw new Exception("Unknown type param: " . $localtype); break; } //end switch localtype check } //end localcheck } else { // end encontrado //this parameter is not specificated on the config file throw new Exception("This app doesnt accept more parameters" . " than the especificated"); } //$key is the name of the param in the XML //$values is an array with all the values switch ($localtype) { case "selectone": $parameters_str .= " " . $localprefix . "" . $key; $parameters_str .= " " . $values[0]; break; case "selectmany": $parameters_str .= " " . $localprefix . "" . $key; foreach ($values as $val) { $parameters_str .= " " . $val; } break; case "flag": $explicit = "false"; if (array_key_exists("explicit", $local["@attr"])) { $explicit = $local["@attr"]["trueval"]; } $tval = "true"; $fval = "false"; if (array_key_exists("trueval", $local["@attr"])) { $tval = $local["@attr"]["trueval"]; } if (array_key_exists("falseval", $local["@attr"])) { $fval = $local["@attr"]["falseval"]; } if ($explicit == "false" && $values[0] == $tval) { $parameters_str .= " " . $localprefix . "" . $key; } else { if ($explicit == "true") { $parameters_str .= " " . $localprefix . "" . $key . " " . $values[0]; } } break; case "textfield": if (array_key_exists("passinfile", $local["@attr"])) { $name_tmpfile = tempnam($dir, $key . "_"); file_put_contents($name_tmpfile, $values[0]); chmod($name_tmpfile, 0755); $parameters_str .= " " . $localprefix . "" . $key; $parameters_str .= " '" . $name_tmpfile . "'"; } else { $parameters_str .= " " . $localprefix . "" . $key; $parameters_str .= " '" . $values[0] . "'"; } break; } //end switch localtype write str } //end parameters $replace_pairs = array("_ei_files" => $files_str, "_ei_root" => $root_str, "_ei_outline" => $outline_str, "_ei_stream" => $stream_str, "_ei_parameters" => $parameters_str, "_ei_clientid" => $clientid_str, "_ei_sessionid" => $sessionid_str, "_ei_execid" => $execid_str); $cmdline = strtr($cmdline, $replace_pairs); $cmdline = escapeshellcmd($cmdline); echo $cmdline; // TODO -- shoudl go into some tags $outputLines = array(); chdir("bin"); // we always execute in the bin directory exec($cmdline, $outputLines); $output = implode("\n", $outputLines); return $output; }