} // Give me the unseen mails list $emails_list = imap_search($imap_client, 'UNSEEN'); if ($emails_list) { // One by one we fetch unseen mails foreach ($emails_list as $email_id) { do_debug('Open mail ' . $email_id); $header = imap_header($imap_client, $email_id); $body_text = imap_fetchbody($imap_client, $email_id, 1); $body_html = imap_fetchbody($imap_client, $email_id, 2); // Check if 'mail_prefix' if found in the TO field if (strpos($header->to[0]->mailbox, $mail_prefix) !== false) { // Yes, so this mail is for us. Now let's get the project identifier : $project_identifier = strtoupper(str_replace($mail_prefix, '', $header->to[0]->mailbox)); // If we found something we can now go and create the task : if (isset($projects[$project_identifier])) { $task['project_id'] = $projects[$project_identifier]['id']; $task['title'] = imap_utf8($header->subject); $task['description'] = $body_text; $response = $client->createTask($task); do_debug('Task "' . $header->subject . '" - ' . $response); } else { do_debug('Task "' . imap_utf8($header->subject) . '" not created : project identifier "' . $project_identifier . '" not found'); } } else { do_debug('Mail #' . $email_id . 'ignored : no project identifier'); } } } else { do_debug('No mails'); }
function handle_json_rpc($object) { /* if ($input == '') { $input = file_get_contents('php://input'); } */ $input = $GLOBALS['HTTP_RAW_POST_DATA']; $encoding = mb_detect_encoding($input, 'auto'); //convert to unicode if ($encoding != 'UTF-8') { $input = iconv($encoding, 'UTF-8', $input); } $input = json_decode($input); header('Content-Type: text/plain'); // handle Errors if (!$input) { if ($GLOBALS['HTTP_RAW_POST_DATA'] == "") { echo response(null, 0, array("code" => -32700, "message" => "Parse Error: no data")); } else { // json parse error $error = json_error(); $id = extract_id(); echo response(null, $id, array("code" => -32700, "message" => "Parse Error: {$error}")); } exit; } else { $method = get_field($input, 'method', null); $params = get_field($input, 'params', null); $id = get_field($input, 'id', null); // json rpc error if (!($method && $id)) { if (!$id) { $id = extract_id(); } if (!$method) { $error = "no method"; } else { if (!$id) { $error = "no id"; } else { $error = "unknown reason"; } } echo response(null, $id, array("code" => -32600, "message" => "Invalid Request: {$error}")); exit; } } // fix params (if params is null set it to empty array) if (!$params) { $params = array(); } // if params is object change it to array if (is_object($params)) { if (count(get_object_vars($params)) == 0) { $params = array(); } else { $params = get_object_vars($params); } } // call Service Method try { $class = get_class($object); $methods = get_class_methods($class); do_debug($methods); if (strcmp($method, 'help') == 0) { if (count($params) > 0) { if (!in_array($params[0], $methods)) { $no_method = 'There is no ' . $params[0] . ' method'; throw new Exception($no_method); } else { $static = get_class_vars($class); $help_str_name = $params[0] . "_documentation"; //throw new Exception(implode(", ", $static)); if (array_key_exists($help_str_name, $static)) { echo response($static[$help_str_name], $id, null); } else { throw new Exception($method . " method has no documentation"); } } } else { $url = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]; $msg = 'PHP JSON-RPC - in "' . $url . "\"\n"; $msg .= "class \"{$class}\" has methods: " . implode(", ", array_slice($methods, 0, -1)) . " and " . $methods[count($methods) - 1] . "."; echo response($msg, $id, null); } } else { if (!in_array($method, $methods)) { $msg = 'There is no ' . $method . ' method'; echo response(null, $id, array("code" => -32601, "message" => $msg)); } else { //throw new Exception('x -> ' . json_encode($params)); $result = call_user_func_array(array($object, $method), $params); echo response($result, $id, null); } } exit; } catch (Exception $e) { //catch all exeption from user code $msg = "Internal error: " . $e->getMessage(); echo response(null, $id, array("code" => -32603, "message" => $msg)); } }
} else { // Check for cookie... if (!empty($webcalendar_session)) { $encoded_login = $webcalendar_session; if (empty($encoded_login)) { // invalid session cookie $session_not_found = true; } else { $login_pw = split('\\|', decode_string($encoded_login)); $login = $login_pw[0]; $cryptpw = $login_pw[1]; // make sure we are connected to the database for password check $c = dbi_connect($db_host, $db_login, $db_password, $db_database); if (!$c) { echo "Error connecting to database:<BLOCKQUOTE>" . dbi_error() . "</BLOCKQUOTE>\n"; exit; } if (!user_valid_crypt($login, $cryptpw)) { do_debug("User not logged in; redirecting to login page"); if (empty($login_return_path)) { do_redirect("login.php"); } else { do_redirect("login.php?return_path={$login_return_path}"); } } do_debug("Decoded login from cookie: {$login}"); } } } } }
protected function do_command($token, $instance) { $input = explode(' ', $token); $method = $input[0]; unset($input[0]); $param = $input; if (in_array($method, array('describe', 'help'))) { if (is_array($param) and count($param)) { $return = get_describe(strtolower(get_class($instance)), current($param)); } else { $return = 'please input a valid method name after describe'; } } else { try { $return = call_user_func_array(array($instance, $method), $param); } catch (Exception $e) { $return = $e->getMessage(); do_debug($return); } $return = is_string($return) ? $return : json_encode($return); } return $return; }
function dump_globals() { foreach ($GLOBALS as $K => $V) { do_debug("GLOBALS[{$K}] => " . (strlen($V) < 70 ? $V : '(too long)')); } foreach ($GLOBALS['HTTP_POST_VARS'] as $K => $V) { do_debug("GLOBALS[{$_POST[$K]}] => " . (strlen($V) < 70 ? $V : '(too long)')); } }
} else { $exe_file = $task['exe_file']; $debug_exe_file = $task['debug_exe_file']; } // Run if ($task['run'] === "true") { $run_result = do_run($filelist, $exe_file, $task['running_params'], $compiler, $task['compiler_options'], $instance); // Debug if ($run_result['status'] == EXECUTION_CRASH && $task['debug'] === "true" && $debugger) { update_status("STA004", "Program crashed"); // Recompile with debug compiler_options $compile_result_debug = do_compile($filelist, $debug_exe_file, $compiler, $task['compiler_options_debug'], $instance); // If compiler failed with compiler_options_debug but succeeded with compiler_options, // most likely options are bad... so we'll skip debugging if ($compile_result_debug['status'] === COMPILE_SUCCESS) { $debug_result = do_debug($debug_exe_file, $debugger, $run_result['core'], $filelist, $instance); update_status("STA006", "Program crashed - debugging finished"); unlink($run_result['core']); } } else { update_status("STA005", "Program executed successfully"); } // Profile if ($run_result['status'] != EXECUTION_CRASH && $task['profile'] === "true" && $profiler) { // Recompile with debug compiler_options $compile_result_debug = do_compile($filelist, $debug_exe_file, $compiler, $task['compiler_options_debug'], $instance); if ($compile_result_debug['status'] === COMPILE_SUCCESS) { $profile_result = do_profile($debug_exe_file, $profiler, $filelist, $task['running_params'], $instance); update_status("STA007", "Program profiled"); } }
function do_test($filelist, $global_symbols, $test, $compiler, $debugger, $profiler, $task, $instance) { global $conf_max_program_output, $conf_verbosity; $test_result = array(); $test_result['status'] = TEST_SUCCESS; $test_result['run_result'] = array(); $test_result['debug_result'] = array(); $test_result['profile_result'] = array(); if ($conf_verbosity > 0) { print "Testing...\n"; } // Replacement strings $start_string = "====START_TEST_" . $test['id'] . "===="; $end_string = "====END_TEST_" . $test['id'] . "===="; $except_string = "====EXCEPTION_TEST_" . $test['id'] . "===="; // Find symbols that are required by this test $includes = array(); if (!empty($global_symbols)) { foreach ($test['require_symbols'] as $symbol) { $found = false; foreach ($global_symbols as $global => $file) { if ($symbol === $global) { array_push($includes, $file); $found = true; break; } } if (!$found) { $test_result['status'] = TEST_SYMBOL_NOT_FOUND; $test_result['status_object'] = $symbol; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - symbol not found ({$symbol})\n"; } return $test_result; } } } // TODO symbol renaming // Construct a new source file with embedded test $main_source_code = $main_filename = ""; if ($task['language'] == "C" || $task['language'] == "C++") { if (!array_key_exists("main", $global_symbols)) { // This is a bug in get_global_symbols, as a program without main wouldn't compile! $test_result['status'] = TEST_SYMBOL_NOT_FOUND; $test_result['status_object'] = "main"; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - main not found\n"; } return $test_result; } $main_filename = $global_symbols['main']; $main_source_code = file_get_contents($main_filename); // Rename main $newname = "_main"; while (array_key_exists($newname, $global_symbols)) { $newname = "_{$newname}"; } $newname = " {$newname}\${1}"; $main_source_code = preg_replace("/\\smain(\\W)/", $newname, $main_source_code); } // Include files containing symbols we need $includes_code = ""; foreach ($includes as $file) { if ($task['language'] == "C" || $task['language'] == "C++") { $includes_code .= "#include \"{$file}\"\n"; } if ($task['language'] == "Python") { $import_file = basename(preg_replace("/\\.py\$/", "", $file)); // Imported filenames must start with a letter in python if (!ctype_alpha($import_file[0])) { $import_file = "a" . $import_file; $new_file = dirname($file) . "/a" . basename($file); rename($file, $new_file); } $includes_code .= "from {$import_file} import *"; } } // Also include stdin/stdout libraries cause they will surely be used in test if ($task['language'] == "C") { $includes_code .= "#include <stdio.h>\n"; } if ($task['language'] == "C++") { $includes_code .= "#include <iostream>\nusing std::cin;\nusing std::cout;\nusing std::cerr;\nusing std::endl;\n"; } // Construct test code $test_code = ""; if ($task['language'] == "C") { $test_code .= "int main() {\nprintf(\"{$start_string}\");\n " . $test['code'] . "\n printf(\"{$end_string}\");\nreturn 0;\n}\n"; } else { if ($task['language'] == "C++") { $test_code .= "int main() {\ntry {\n std::cout<<\"{$start_string}\";\n " . $test['code'] . "\n std::cout<<\"{$end_string}\";\n } catch (...) {\n std::cout<<\"{$except_string}\";\n }\nreturn 0;\n}\n"; } else { if ($task['language'] == "Python") { $test_code .= "print(\"{$start_string}\")\n" . $test['code'] . "\nprint(\"{$end_string}\")\n"; } else { $test_code = $test['code']; } } } // Prevent cheating $main_source_code = str_replace($start_string, "====cheat_protection====", $main_source_code); $main_source_code = str_replace($end_string, "====cheat_protection====", $main_source_code); $main_source_code = str_replace($except_string, "====cheat_protection====", $main_source_code); // Construct whole file $main_length = substr_count($main_source_code, "\n"); $main_source_code = $includes_code . $test['global_top'] . "\n" . $main_source_code . "\n" . $test['global_above_main'] . "\n" . $test_code . "\n"; // Choose filename for test $test_filename = "bs_test_" . $test['id']; if ($task['language'] == "C") { $test_filename .= ".c"; } if ($task['language'] == "C++") { $test_filename .= ".cpp"; } if ($task['language'] == "Python") { $test_filename .= ".py"; } $test_path = instance_path($instance); if ($task['language'] == "C" || $task['language'] == "C++") { // Locate test file in the same path that mainfile used to be $test_path = dirname($main_filename); } while (in_array($test_path . "/" . $test_filename, $filelist)) { $test_filename = "_" . $test_filename; } $test_filename = $test_path . "/" . $test_filename; file_put_contents($test_filename, $main_source_code); // Add test file to filelist if ($task['language'] == "C" || $task['language'] == "C++") { for ($i = 0; $i < count($filelist); $i++) { if ($filelist[$i] === $global_symbols['main']) { $filelist[$i] = $test_filename; } } } else { if ($task['language'] == "Python") { // In python we execute just the test file and it includes everything else $filelist = array($test_filename); } else { array_push($filelist, $test_filename); } } // Calculate positions of original code and test code inside sources // that will be used to adjust output of compile/debug/profile to something user expects $adjustment_data = array(); $adjustment_data['orig_filename'] = $main_filename; $adjustment_data['new_filename'] = $test_filename; $adjustment_data['global_top_pos'] = substr_count($includes_code, "\n"); $adjustment_data['original_source_pos'] = $adjustment_data['global_top_pos'] + substr_count($test['global_top'], "\n") + 1; $adjustment_data['global_above_pos'] = $adjustment_data['original_source_pos'] + $main_length + 1; $adjustment_data['test_code_pos'] = $adjustment_data['global_above_pos'] + substr_count($test['global_above_main'], "\n") + 1; // number of lines added to main per language if ($task['language'] == "C") { $adjustment_data['test_code_pos'] += 2; } if ($task['language'] == "C++") { $adjustment_data['test_code_pos'] += 3; } if ($task['language'] == "Python") { $adjustment_data['test_code_pos'] += 1; } // === TESTING COMMENCE === // Compile test $test_exe_file = instance_path($instance) . "/bs_test_" . $test['id']; $compile_result = do_compile($filelist, $test_exe_file, $compiler, $task['compiler_options_debug'], $instance); $test_result['compile_result'] = $compile_result; if ($compile_result['status'] !== COMPILE_SUCCESS) { $test_result['status'] = TEST_COMPILE_FAILED; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - compile error\n"; } return $test_result; } // Execute test $run_result = do_run($filelist, $test_exe_file, $test['running_params'], $compiler, $task['compiler_options_debug'], $instance); $test_result['run_result'] = $run_result; $program_output = $run_result['output']; // Shortcut // Output was too long and it was cut off... let's pretend it finished ok if (strlen($program_output) >= $conf_max_program_output) { $program_output .= "\n{$end_string}\n"; } // Find marker strings in program output $start_pos = strpos($program_output, $start_string); if ($start_pos !== false) { $start_pos += strlen($start_string); } $end_pos = strpos($program_output, $end_string); $except_pos = strpos($program_output, $except_string); // Remove marker strings from output if ($end_pos !== false) { $program_output = substr($program_output, $start_pos, $end_pos - $start_pos); } else { if ($except_pos !== false) { $program_output = substr($program_output, $start_pos, $except_pos - $start_pos); } else { if ($start_pos !== false) { $program_output = substr($program_output, $start_pos); } } } $test_result['run_result']['output'] = $program_output; if ($run_result['status'] === EXECUTION_TIMEOUT) { $test_result['status'] = TEST_EXECUTION_TIMEOUT; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - execution timeout\n"; } // Profiler will simply execute even longer, so we don't go there return $test_result; } if ($run_result['status'] === EXECUTION_CRASH) { $test_result['status'] = TEST_EXECUTION_CRASH; // Use seldom: crashes are unreliable! if ($test['expected_crash'] === "true") { $test_result['status'] = TEST_SUCCESS; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " ok (crash)\n"; } } else { if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - crash\n"; } } // Debug in case of crash if ($debugger && $task['debug'] === "true") { $debug_result = do_debug($test_exe_file, $debugger, $run_result['core'], $filelist, $instance); // Adjust filenames and line numbers that were changed for the test foreach ($debug_result['parsed_output'] as &$msg) { if (instance_path($instance) . "/" . $msg['file'] === $adjustment_data['new_filename']) { test_adjust_lines($msg['file'], $msg['line'], $adjustment_data, $instance); } } $test_result['debug_result'] = $debug_result; } unlink($run_result['core']); // If crash is unexpected, we will go on to profiler cause it can give some more information if ($test['expected_crash'] === "true") { return $test_result; } } else { // === FINDING EXPECTED OUTPUT IN PROGRAM OUTPUT === // Remove invisible spaces in expected program output // Allow to specify newlines in expected output using \n foreach ($test['expected'] as &$ex) { $ex = str_replace("\r\n", "\n", $ex); $ex = str_replace("\\n", "\n", $ex); $ex = trim(preg_replace("/\\s+\n/", "\n", $ex)); } // Program finished normally if ($start_pos !== false && $end_pos !== false) { if ($test['expected_exception'] === "true") { $test_result['status'] = TEST_WRONG_OUTPUT; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - expected exception\n"; } return $test_result; } $test_result['run_result']['output'] = $program_output; // Don't fail test because of invisible spaces and empty lines $program_output = preg_replace("/\\s+\n/", "\n", $program_output); $program_output = trim(preg_replace("/\n+/", "\n", $program_output)); // Ignore whitespace if ($test['ignore_whitespace'] === "true") { $program_output = str_replace("\n", "", $program_output); $program_output = preg_replace("/\\s+/", "", $program_output); foreach ($test['expected'] as &$ex) { $ex = preg_replace("/\\s+/", "", $ex); } } // Look for expected outputs in program output $test_ok = false; $exnr = 1; foreach ($test['expected'] as &$ex) { // Why do we need a reference here??? if ($program_output == $ex) { if ($conf_verbosity > 0) { print "- test " . $test['id'] . " ok (exact match {$exnr})\n"; } $test_ok = true; break; } else { if ($test['substring'] === "true" && strstr($program_output, $ex)) { if ($conf_verbosity > 0) { print "- test " . $test['id'] . " ok (substring {$exnr})\n"; } $test_ok = true; break; } else { if ($test['regex'] === "true" && preg_match("/{$ex}/", $program_output)) { if ($conf_verbosity > 0) { print "- test " . $test['id'] . " ok (regex {$exnr})\n"; } $test_ok = true; break; } } } $exnr++; } if (!$test_ok) { $test_result['status'] = TEST_WRONG_OUTPUT; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed\n"; } if ($conf_verbosity > 2) { print "Output:\n{$program_output}\nExpected:\n" . $test['expected'][0]; } // We will continue to profiler as it may give us some explanation of result } } else { if ($start_pos !== false && $except_pos !== false) { // TODO check type of exception if ($test['expected_exception'] === "false") { $test_result['status'] = TEST_UNEXPECTED_EXCEPTION; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - exception not expected\n"; } // We won't continue to profiler because unexpected exception usually causes memleaks // which won't exist after the reason for this exception is removed. // Otherwise programmer might be confused that memleak caused the exception return $test_result; } if ($conf_verbosity > 0) { print "- test " . $test['id'] . " ok (exception)\n"; } } else { $test_result['status'] = TEST_OUTPUT_NOT_FOUND; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - output not found ({$start_pos}, {$end_pos}, {$except_pos})\n"; } } } } // if ($run_result['status'] === EXECUTION_CRASH) { ... } else { // Profile if ($profiler && $task['profile'] === "true") { $profile_result = do_profile($test_exe_file, $profiler, $filelist, $test['running_params'], $instance); // Adjust filenames and line numbers that were changed for the test foreach ($profile_result['parsed_output'] as &$msg) { // Valgrind always returns just the base file name if ($msg['file'] === basename($adjustment_data['new_filename'])) { test_adjust_lines($msg['file'], $msg['line'], $adjustment_data, $instance); } if (array_key_exists('file_alloced', $msg) && instance_path($instance) . "/" . $msg['file_alloced'] === $adjustment_data['new_filename']) { test_adjust_lines($msg['file_alloced'], $msg['line_alloced'], $adjustment_data, $instance); } } // If there are no errors, we will disregard profiler output if ($profile_result['status'] !== PROFILER_OK) { $test_result['profile_result'] = $profile_result; if ($test_result['status'] === TEST_SUCCESS) { $test_result['status'] = TEST_PROFILER_ERROR; if ($conf_verbosity > 0) { print "- test " . $test['id'] . " failed - profiler error\n"; } } } } return $test_result; }