function runTask($task, $source) { $hash = randomString(); while (file_exists("sandbox/run_{$hash}/")) { $hash = randomString(); } $hash = 'run_' . $hash; $sandboxDir = "sandbox/{$hash}/"; mkdir($sandboxDir); $task_dir = "tasks/{$task}/"; $source_file = $sandboxDir . 'user.sql'; file_put_contents($source_file, $source); $output_file = $sandboxDir . 'output'; $error_file = $sandboxDir . 'error'; $diff_file = $sandboxDir . 'diff'; $db_init_file = $sandboxDir . 'db_init.sql'; $db_destroy_file = $sandboxDir . 'db_destroy.sql'; $answer_file = $task_dir . $task . '.ans'; $init_file = $task_dir . $task . '.sql'; $config = getTaskConfig(); foreach ($config as $pattern => $def) { $matched = @preg_match($pattern, $task); if ($matched) { if (array_key_exists('init', $def)) { $init_file = 'tasks/' . $def['init']; } } else { if ($matched === false) { abort('invalid regex pattern in task config: ' . $pattern); } } } $sql_admin = "mysql --user=task_runner --password=task_runner --local-infile=1"; $sql_jail = "mysql --user={$hash} --password={$hash}"; file_put_contents($db_init_file, join(";\n", array("create database {$hash}", "create user '{$hash}'@'localhost' identified by '{$hash}'", "grant all privileges on {$hash}.* to '{$hash}'@'localhost'", "flush privileges")) . ';'); exec("{$sql_admin} < {$db_init_file} 2> {$error_file}"); parseError($error_file, true); exec("{$sql_admin} {$hash} < {$init_file} 2> {$error_file}"); parseError($error_file, true); exec("{$sql_jail} {$hash} < {$source_file} > {$output_file} 2> {$error_file}"); exec("diff -q --strip-trailing-cr {$output_file} {$answer_file} > {$diff_file}"); $output = file_get_contents($output_file); $error = parseError($error_file, false); $diff = file_get_contents($diff_file); if (!empty($diff)) { exec("diff -y --strip-trailing-cr {$output_file} {$answer_file} > {$diff_file}"); $diff = file_get_contents($diff_file); } file_put_contents($db_destroy_file, join(";\n", array("revoke all privileges on {$hash}.* from '{$hash}'@'localhost'", "drop user '{$hash}'@'localhost'", "drop database {$hash}")) . ';'); exec("{$sql_admin} < {$db_destroy_file} 2> {$error_file}"); parseError($error_file, true); // Do not remove $sandboxDir if history is wanted. //exec("rm -r $sandboxDir"); $result = array('error' => $error, 'diff' => $diff, 'output' => $output); return $result; }
/* Error handler: Called if there was a parse error. Retrieves and returns information about the error. */ function parseError( $parser ) { $error = xml_error_string( xml_get_error_code( $parser ) ); $errorLine = xml_get_current_line_number( $parser ); $errorColumn = xml_get_current_column_number( $parser ); return "<b>Error: $error at line $errorLine column $errorColumn</b>"; } // Create the parser and set options $parser = xml_parser_create(); xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false ); // Register the event handlers with the parser xml_set_element_handler( $parser, "startElementHandler", "endElementHandler" ); xml_set_character_data_handler( $parser, "characterDataHandler" ); // Read and parse the XML document $xml = file_get_contents( "./stock_list.xml" ); xml_parse( $parser, $xml ) or die( parseError( $parser ) ); xml_parser_free( $parser ); ?> </pre> </body> </html>
private static function getFunction($s) { $tmp = explode(OPERATOR, $s); $operation = trim($tmp[0]); if ($operation != DEFINER_FUNCTION) { parseError("{$s}: Not a function."); return null; } $ms = explode(MODIFIER_SPLITTER, $tmp[1]); $ret = new CPLFunction(); $ret->Name = $ms[0]; foreach ($ms as $i => $m) { if ($i == 0) { continue; } $ret->Modifiers[] = $m; } return $ret; }
function UCloud_Client_Do($req) { $ch = curl_init(); $url = $req->URL; $options = array(CURLOPT_USERAGENT => $req->UA, CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HEADER => true, CURLOPT_NOBODY => false, CURLOPT_CUSTOMREQUEST => $req->METHOD, CURLOPT_URL => $url['host'] . "/" . rawurlencode($url['path']), CURLOPT_TIMEOUT => $req->Timeout, CURLOPT_CONNECTTIMEOUT => $req->Timeout); $httpHeader = $req->Header; if (!empty($httpHeader)) { $header = array(); foreach ($httpHeader as $key => $parsedUrlValue) { $header[] = "{$key}: {$parsedUrlValue}"; } $options[CURLOPT_HTTPHEADER] = $header; } $body = $req->Body; if (!empty($body)) { $options[CURLOPT_POSTFIELDS] = $body; } else { $options[CURLOPT_POSTFIELDS] = ""; } curl_setopt_array($ch, $options); $result = curl_exec($ch); $ret = curl_errno($ch); if ($ret !== 0) { $err = new UCloud_Error(0, $ret, curl_error($ch)); curl_close($ch); return array(null, $err); } $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); curl_close($ch); $responseArray = explode("\r\n\r\n", $result); $responseArraySize = sizeof($responseArray); $headerString = $responseArray[$responseArraySize - 2]; $respBody = $responseArray[$responseArraySize - 1]; $headers = parseHeaders($headerString); $resp = new HTTP_Response($code, $respBody); $resp->Header = $headers; $err = null; if (floor($resp->StatusCode / 100) != 2) { list($r, $m) = parseError($respBody); $err = new UCloud_Error($resp->StatusCode, $r, $m); } return array($resp, $err); }