function diy_compile($payload, $storage)
{
global $app;
$result["controller"] = __FUNCTION__;
$result["function"] = substr($app->request()->getPathInfo(), 1);
$result["method"] = $app->request()->getMethod();
$params = loadParameters();
$result->function = substr($app->request()->getPathInfo(), 1);
$result->method = $app->request()->getMethod();
$params = loadParameters();
$srcfile = OAuth2\Request::createFromGlobals()->request["srcfile"];
$srclib = OAuth2\Request::createFromGlobals()->request["srclib"];
$device = OAuth2\Request::createFromGlobals()->request["device"];
$comp = OAuth2\Request::createFromGlobals()->request["comp"];
$filename = OAuth2\Request::createFromGlobals()->request["filename"];
$writedevice = OAuth2\Request::createFromGlobals()->request["writedevice"];
$up = json_decode(base64_decode($payload));
$client_id = $up->client_id;
$diy_error["post"]["device"] = $device;
$post["srcfile"] = $srcfile;
//organisation oauth_devices
$post["device"] = $device;
//organisation oauth_devices
$post["comp"] = $comp;
//organisation oauth_devices
$post["filename"] = $filename;
//organisation oauth_devices
$post["writedevice"] = $writedevice;
//organisation oauth_devices
$gump = new GUMP();
$gump->validation_rules(array('device' => 'required|alpha_numeric', 'filename' => 'required|alpha_numeric', 'comp' => 'required|alpha_numeric', 'writedevice' => 'required|alpha_numeric'));
$gump->filter_rules(array('device' => 'trim|sanitize_string', 'filename' => 'trim|sanitize_string', 'comp' => 'trim|sanitize_string', 'writedevice' => 'trim|sanitize_string'));
$validated = $gump->run($post);
if ($validated === false) {
$result["parse_errors"] = $gump->get_readable_errors(true);
$result["message"] = "[" . $result["method"] . "][" . $result["function"] . "]:" . $gump->get_readable_errors(true);
} else {
try {
$sourceWriteDir = __DIR__ . '/../../../data/sketches/' . $client_id . '/' . $device . '/' . $filename;
if (file_exists($sourceWriteDir)) {
throw new \Exception('Filename ' . $filename . ' for user ' . $client_id . ' and device ' . $device . ' already exists');
}
$stmt2 = $storage->prepare('SELECT * FROM oauth_devices WHERE device = :device');
$stmt2->execute(array('device' => trim($device)));
$row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
if ($row2["organisation"]) {
$org = trim($row2["organisation"]);
}
if ($row2["mode"]) {
$mode = trim($row2["mode"]);
}
if ($row2["status"]) {
$status = trim($row2["status"]);
}
if ($row2["client_id"]) {
$devclient_id = trim($row2["client_id"]);
}
$orgscopeadmin = "no";
$orgscopedevel = "no";
if ($mode == "devel" && $status == "org") {
$userscopes = explode(' ', trim($userscope));
$adminscope = $org . "_admin";
$develscope = $org . "_admin";
// o user aniki sto scope
for ($i = 0; $i <= count($userscopes); $i++) {
if (trim($userscopes[$i]) == $adminscope) {
$orgscopeadmin = "yes";
}
if (trim($userscopes[$i]) == $develscope) {
$orgscopedevel = "yes";
}
}
// einai o owner
if ($devclient_id == $client_id) {
$orgscopeadmin = "yes";
}
}
// einmai o owner
if ($mode == "devel" && $status == "private" && $devclient_id == $client_id) {
$orgscopeadmin = "yes";
}
$result["result"]["sketch1"] = $orgscopeadmin;
if ($orgscopeadmin == "yes" || $orgscopedevel == "yes") {
try {
$stmt2 = $storage->prepare('SELECT * FROM oauth_clients WHERE client_id = :device');
$stmt2->execute(array('device' => trim($device)));
$row2 = $stmt2->fetch(PDO::FETCH_ASSOC);
if ($row2["apiport"]) {
// *************************************** compiler *********************************
// srcfile echeis se base64 ton kodika
// compiler echeis ton compiler pou thelei o user mechri stigmis echoume gcc, ino
// filename to filename pou edosse o user
// o poros compilesketch
// afou kanei compile
// epistrefei
// error ta lathi h noerrors
// binfile to hex file
$compilerserver = diyConfig::read("compiler.host");
$compilerserver .= ":" . diyConfig::read("compiler.port");
$data1 = 'filename=' . $filename;
$data1 .= '&compiler=' . $comp;
$data1 .= '&srcfile=' . $srcfile;
$fixedFiles = array();
foreach ($srclib as $curName => $curFile) {
$fixedFiles[] = 'srclib[' . $curName . ']=' . $curFile;
}
$data1 .= '&' . implode('&', $fixedFiles);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$compilerserver}/api/compilesketch");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch, CURLOPT_POST, 1);
$or = curl_exec($ch);
if (!$or) {
$or = curl_error($ch);
}
$result["compiler"] = $or;
$result["message"] = "[" . $result["method"] . "][" . $result["function"] . "]: NoErrors";
$result["status"] = "200";
$r = json_decode($or, true);
if (!$r) {
$result["message"] = "[" . $result["method"] . "][" . $result["function"] . "]: CompilationError";
$result["compiler"] = $or;
$result["status"] = "500";
return $result;
}
if ($r['status'] != 200) {
$result["message"] = "[" . $result["method"] . "][" . $result["function"] . "]: CompilationError";
$result["status"] = "500";
return $result;
}
unset($result["compiler"]);
// No need to transfer this to the user
//$srcfilebase64encode = base64_encode($srcfile);
$apiport = trim($row2["apiport"]);
// *************************************** compiler *********************************
if ($r['status'] == 200 && $writedevice == "yes") {
$apiport = trim($row2["apiport"]);
$binfile = $r['hex'];
$data1 = 'file=base64';
$data1 .= '&binfile=' . $binfile;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1:{$apiport}/api/writesketch");
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data1);
curl_setopt($ch, CURLOPT_POST, 1);
$r = curl_exec($ch);
$result["sketch"] = $r;
$result["message"] = "[" . $result["method"] . "][" . $result["function"] . "]: NoErrors";
$result["status"] = "200";
//$result["result"]= $r;
}
// If we are here with no exceptions then everything went well. Lets save the sketch.
$ziptmp = tempnam(sys_get_temp_dir(), 'diytmpzip') . '.tgz';
file_put_contents($ziptmp, base64_decode($r['zip']));
$p = new PharData($ziptmp);
$p->decompress();
// creates /path/to/my.tar
$ziptmpextracted = str_replace('.tgz', '.tar', $ziptmp);
$phar = new PharData($ziptmpextracted);
$writeDir = __DIR__ . '/../../../data/sketches/' . $client_id . '/' . $filename;
$phar->extractTo($sourceWriteDir);
}
} catch (Exception $e) {
$diy_error["db"] = $e->getCode();
$result["status"] = $e->getCode();
$result["message"] = "[" . $result["method"] . "][" . $result["function"] . "]:" . $e->getMessage();
}
}
} catch (Exception $e) {
$diy_error["db"] = $e->getCode();
$result["status"] = $e->getCode();
$result["message"] = "[" . $result["method"] . "][" . $result["function"] . "]:" . $e->getMessage();
}
}
if (diyConfig::read('debug') == 1) {
$result["debug"] = $diy_error;
}
return $result;
}