<commit_before><?


// BUILDSERVICE.PHP - serverska strana skripte za buildservice

require_once("../lib/config.php");
require_once("../lib/dblayer.php");
require_once("../lib/zamger.php");
require_once("../lib/student_predmet.php"); // update_komponente
require_once("../lib/utility.php"); // ends_with, rm_minus_r
require_once("../lib/session.php"); // check_cookie


db_connect($conf_dbhost,$conf_dbuser,$conf_dbpass,$conf_dbdb);


// Čekamo 15 minuta na buildhost
$conf_buildhost_timeout = 60*15;

$result = array();

$result['success'] = "true";
$result['data'] = array();

$autotest_basepath = "/tmp/autotest";


// Lists of source filename extensions per language
$conf_extensions = array(
	"C"    => array( ".c", ".h" ),
	"C++"  => array( ".cpp", ".h", ".cxx", ".hxx" ),
	"C++11"  => array( ".cpp", ".h", ".cxx", ".hxx" ),
	"Java" => array( ".java" ),
);



check_cookie();

if ($userid!=0)
	$priv = odredi_privilegije_korisnika();
  
if ($userid == 0) {
	$result['success'] = "false";
	$result['code'] = "ERR001";
	$result["message"] = "Session expired";
  
} else if ($priv === false) {
	$result['success'] = "false";
	$result['code'] = "ERR002";
	$result["message"] = "User has no privileges";
}


else if ($_REQUEST['action'] == "nextTask") {
	if (!$user_autotester) {
		$result['success'] = "false";
		$result['code'] = "ERR004";
		$result["message"] = "Only autotester has access to nextTask";
	} else {
		$task = $previousTask = false;
		$q = db_query("SELECT id, zadataka FROM zadaca WHERE automatsko_testiranje=1 ORDER BY id");
		while ($r = db_fetch_row($q)) {
			for ($i=1; $i<=$r[1]; $i++)
				if (dajZadatak($r[0], $i)) {
					$tmpTask = $r[0] * 100 + $i;
					if (!isset($_REQUEST['previousTask']) || $_REQUEST['previousTask'] == $previousTask) {
						$task = $tmpTask;
						break;
					} else if ($task == false) {
						$task = $tmpTask;
					}
					$previousTask = $tmpTask;
				}
		}
		if ($task == false)
			$result['data']['id'] = "false";
		else
			$result['data']['id'] = $task;
	}
}


else if ($_REQUEST['action'] == "assignProgram") {
	$task = intval($_REQUEST['task']);
	$zadaca = intval($task/100);
	$zadatak = $task % 100;
	if (!$user_autotester && bs_nastavnik_pravo_pristupa($zadaca) != "nastavnik" && bs_nastavnik_pravo_pristupa($zadaca) != "super_asistent") {
		$result['success'] = "false";
		$result['code'] = "ERR004";
		$result["message"] = "You don't have permission to assignProgram";
	} else {
		$buildhost_id = db_escape($_REQUEST['buildhost']);
		$q99 = db_query("LOCK TABLES buildservice_tracking WRITE, zadatak READ");
		$zadatak = dajZadatak($zadaca, $zadatak);
		if ($zadatak) {
			$q100 = db_query("DELETE FROM buildservice_tracking WHERE zadatak=$zadatak");
			$q110 = db_query("INSERT INTO buildservice_tracking SET zadatak=$zadatak, buildhost='$buildhost_id'");
			$result['data']['id'] = $zadatak;
		} else
			$result['data']['id'] = "false";
		$q111 = db_query("UNLOCK TABLES");
	}
}

else if ($_REQUEST['action'] == "getTaskData") {
	$task = intval($_REQUEST['task']);
	$zadaca = intval($task/100);
	$zadatak = $task % 100;
	
	if (!$user_siteadmin && !$user_autotester && !pravo_pristupa($zadaca,0)) {
		$result['success'] = "false";
		$result['code'] = "ERR003";
		$result["message"] = "Access denied";

	} else {
		$q = db_query("SELECT z.naziv, p.naziv, ag.naziv, pj.naziv, pj.kompajler, pj.opcije_kompajlera, pj.opcije_kompajlera_debug, z.zadataka FROM zadaca as z, predmet as p, akademska_godina as ag, programskijezik as pj WHERE z.id=$zadaca AND z.predmet=p.id AND z.akademska_godina=ag.id AND z.programskijezik=pj.id");
		if (db_num_rows($q) == 0) {
			$result['success'] = "false";
			$result['code'] = "ERR005";
			$result["message"] = "Unknown task ID $task";
		}
		
		$r = db_fetch_row($q);
		$result['data']['id'] = $task;
		$result['data']['name'] = $r[1]." (".$r[2]."), ".$r[0];
		if ($r[7] > 1) $result['data']['name'] .= ", zadatak $zadatak";
		$result['data']['language'] = $r[3];

		// FIXME:
		if ($r[3] == "C++11") $result['data']['language'] = "C++";

		// FIXME:
		$result['data']['required_compiler']  = $r[4];
		$result['data']['preferred_compiler'] = $r[4];
		$result['data']['compiler_features']  = array();
		$result['data']['compiler_options']   = $r[5];
		$result['data']['compiler_options_debug'] = $r[6];

		// Šta sve treba raditi sa zadaćom
		$result['data']['compile'] = "true";
		$result['data']['run']     = "false";
		$result['data']['test']    = "true";
		if ($r[3] == "Python") {
			$result['data']['debug']   = "false";
			$result['data']['profile'] = "false";
		} else {
			$result['data']['debug']   = "true";
			$result['data']['profile'] = "true";
		}
		if ($zadaca == 2890) $result['data']['profile'] = "false";

		// Tests
		$result['data']['test_specifications'] = array();
		
		// require symbols HACK
		$q3 = db_query("SELECT tip, specifikacija, zamijeni FROM autotest_replace WHERE zadaca=$zadaca AND zadatak=$zadatak");
		$replace_symbols = array();
		$require_symbols = array();
		while ($r3 = db_fetch_row($q3)) {
			if ($r3[2] === "")
				array_push($require_symbols, $r3[1]);
			else {
				$replace = array();
				$replace['type'] = $r3[0];
				$replace['match'] = $r3[1];
				$replace['replace'] = $r3[2];
			}
		}
		
		$q2 = db_query("SELECT id, kod, rezultat, alt_rezultat, fuzzy, global_scope, pozicija_globala, stdin, partial_match, sakriven FROM autotest WHERE zadaca=$zadaca AND zadatak=$zadatak AND aktivan=1");
		while ($r2 = db_fetch_row($q2)) {
			// Studentima ne prikazujemo sakrivene testove?
			if ($r2[9]==1 && !$user_siteadmin && !$user_autotester && !bs_nastavnik_pravo_pristupa($zadaca))
//			if ($r2[9]==1)
				continue;
			
			$test = array();
			$test['id'] = $r2[0];
			$test['require_symbols'] = $require_symbols;
			$test['replace_symbols'] = $replace_symbols;
			$test['code'] = $r2[1];
			
			if ($r2[6] === 'prije_maina') {
				$test['global_above_main'] = $r2[5];
				$test['global_top'] = "";
			} else {
				$test['global_top'] = $r2[5];
				$test['global_above_main'] = "";
			}
			
			$test['running_params'] = array();
			$test['running_params']['timeout'] = 10; // TODO hardcodirano 10 sekundi
			$test['running_params']['vmem'] = 1000000; // TODO hardcodirano ~200 MB
			$test['running_params']['stdin'] = $r2[7];<commit_msg>buildservice: Parametar "replace symbols" je bio ignorisan<commit_after><?


// BUILDSERVICE.PHP - serverska strana skripte za buildservice

require_once("../lib/config.php");
require_once("../lib/dblayer.php");
require_once("../lib/zamger.php");
require_once("../lib/student_predmet.php"); // update_komponente
require_once("../lib/utility.php"); // ends_with, rm_minus_r
require_once("../lib/session.php"); // check_cookie


db_connect($conf_dbhost,$conf_dbuser,$conf_dbpass,$conf_dbdb);


// Čekamo 15 minuta na buildhost
$conf_buildhost_timeout = 60*15;

$result = array();

$result['success'] = "true";
$result['data'] = array();

$autotest_basepath = "/tmp/autotest";


// Lists of source filename extensions per language
$conf_extensions = array(
	"C"    => array( ".c", ".h" ),
	"C++"  => array( ".cpp", ".h", ".cxx", ".hxx" ),
	"C++11"  => array( ".cpp", ".h", ".cxx", ".hxx" ),
	"Java" => array( ".java" ),
);



check_cookie();

if ($userid!=0)
	$priv = odredi_privilegije_korisnika();
  
if ($userid == 0) {
	$result['success'] = "false";
	$result['code'] = "ERR001";
	$result["message"] = "Session expired";
  
} else if ($priv === false) {
	$result['success'] = "false";
	$result['code'] = "ERR002";
	$result["message"] = "User has no privileges";
}


else if ($_REQUEST['action'] == "nextTask") {
	if (!$user_autotester) {
		$result['success'] = "false";
		$result['code'] = "ERR004";
		$result["message"] = "Only autotester has access to nextTask";
	} else {
		$task = $previousTask = false;
		$q = db_query("SELECT id, zadataka FROM zadaca WHERE automatsko_testiranje=1 ORDER BY id");
		while ($r = db_fetch_row($q)) {
			for ($i=1; $i<=$r[1]; $i++)
				if (dajZadatak($r[0], $i)) {
					$tmpTask = $r[0] * 100 + $i;
					if (!isset($_REQUEST['previousTask']) || $_REQUEST['previousTask'] == $previousTask) {
						$task = $tmpTask;
						break;
					} else if ($task == false) {
						$task = $tmpTask;
					}
					$previousTask = $tmpTask;
				}
		}
		if ($task == false)
			$result['data']['id'] = "false";
		else
			$result['data']['id'] = $task;
	}
}


else if ($_REQUEST['action'] == "assignProgram") {
	$task = intval($_REQUEST['task']);
	$zadaca = intval($task/100);
	$zadatak = $task % 100;
	if (!$user_autotester && bs_nastavnik_pravo_pristupa($zadaca) != "nastavnik" && bs_nastavnik_pravo_pristupa($zadaca) != "super_asistent") {
		$result['success'] = "false";
		$result['code'] = "ERR004";
		$result["message"] = "You don't have permission to assignProgram";
	} else {
		$buildhost_id = db_escape($_REQUEST['buildhost']);
		$q99 = db_query("LOCK TABLES buildservice_tracking WRITE, zadatak READ");
		$zadatak = dajZadatak($zadaca, $zadatak);
		if ($zadatak) {
			$q100 = db_query("DELETE FROM buildservice_tracking WHERE zadatak=$zadatak");
			$q110 = db_query("INSERT INTO buildservice_tracking SET zadatak=$zadatak, buildhost='$buildhost_id'");
			$result['data']['id'] = $zadatak;
		} else
			$result['data']['id'] = "false";
		$q111 = db_query("UNLOCK TABLES");
	}
}

else if ($_REQUEST['action'] == "getTaskData") {
	$task = intval($_REQUEST['task']);
	$zadaca = intval($task/100);
	$zadatak = $task % 100;
	
	if (!$user_siteadmin && !$user_autotester && !pravo_pristupa($zadaca,0)) {
		$result['success'] = "false";
		$result['code'] = "ERR003";
		$result["message"] = "Access denied";

	} else {
		$q = db_query("SELECT z.naziv, p.naziv, ag.naziv, pj.naziv, pj.kompajler, pj.opcije_kompajlera, pj.opcije_kompajlera_debug, z.zadataka FROM zadaca as z, predmet as p, akademska_godina as ag, programskijezik as pj WHERE z.id=$zadaca AND z.predmet=p.id AND z.akademska_godina=ag.id AND z.programskijezik=pj.id");
		if (db_num_rows($q) == 0) {
			$result['success'] = "false";
			$result['code'] = "ERR005";
			$result["message"] = "Unknown task ID $task";
		}
		
		$r = db_fetch_row($q);
		$result['data']['id'] = $task;
		$result['data']['name'] = $r[1]." (".$r[2]."), ".$r[0];
		if ($r[7] > 1) $result['data']['name'] .= ", zadatak $zadatak";
		$result['data']['language'] = $r[3];

		// FIXME:
		if ($r[3] == "C++11") $result['data']['language'] = "C++";

		// FIXME:
		$result['data']['required_compiler']  = $r[4];
		$result['data']['preferred_compiler'] = $r[4];
		$result['data']['compiler_features']  = array();
		$result['data']['compiler_options']   = $r[5];
		$result['data']['compiler_options_debug'] = $r[6];

		// Šta sve treba raditi sa zadaćom
		$result['data']['compile'] = "true";
		$result['data']['run']     = "false";
		$result['data']['test']    = "true";
		if ($r[3] == "Python") {
			$result['data']['debug']   = "false";
			$result['data']['profile'] = "false";
		} else {
			$result['data']['debug']   = "true";
			$result['data']['profile'] = "true";
		}
		if ($zadaca == 2890) $result['data']['profile'] = "false";

		// Tests
		$result['data']['test_specifications'] = array();
		
		// require symbols HACK
		$q3 = db_query("SELECT tip, specifikacija, zamijeni FROM autotest_replace WHERE zadaca=$zadaca AND zadatak=$zadatak");
		$replace_symbols = array();
		$require_symbols = array();
		while ($r3 = db_fetch_row($q3)) {
			if ($r3[2] === "")
				array_push($require_symbols, $r3[1]);
			else {
				$replace = array();
				$replace['type'] = $r3[0];
				$replace['match'] = $r3[1];
				$replace['replace'] = $r3[2];
				array_push($replace_symbols, $replace);
			}
		}
		
		$q2 = db_query("SELECT id, kod, rezultat, alt_rezultat, fuzzy, global_scope, pozicija_globala, stdin, partial_match, sakriven FROM autotest WHERE zadaca=$zadaca AND zadatak=$zadatak AND aktivan=1");
		while ($r2 = db_fetch_row($q2)) {
			// Studentima ne prikazujemo sakrivene testove?
			if ($r2[9]==1 && !$user_siteadmin && !$user_autotester && !bs_nastavnik_pravo_pristupa($zadaca))
//			if ($r2[9]==1)
				continue;
			
			$test = array();
			$test['id'] = $r2[0];
			$test['require_symbols'] = $require_symbols;
			$test['replace_symbols'] = $replace_symbols;
			$test['code'] = $r2[1];
			
			if ($r2[6] === 'prije_maina') {
				$test['global_above_main'] = $r2[5];
				$test['global_top'] = "";
			} else {
				$test['global_top'] = $r2[5];
				$test['global_above_main'] = "";
			}
			
			$test['running_params'] = array();
			$test['running_params']['timeout'] = 10; // TODO hardcodirano 10 sekundi
			$test['running_params']['vmem'] = 1000000; // TODO hardcodirano ~200 MB
			$test['running_params']['stdin'] = $r2[7];
{'fraction_non_alphanumeric': 0.22356662712534597, 'fraction_numerical': 0.020245156188216686, 'mean_word_length': 4.481577806675336, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6513163', 'n_tokens_mistral': 5521, 'n_tokens_neox': 4956, 'n_words': 925}