function ProcessFile($projectid, $filename, $md5) { unset($fp); if (!file_exists($filename)) { // check in parent dir also $filename = "../{$filename}"; } if (file_exists($filename)) { $fp = fopen($filename, 'r'); } if (@$fp) { global $PHP_ERROR_SUBMISSION_ID; do_submit($fp, $projectid, $md5, false, $PHP_ERROR_SUBMISSION_ID); $PHP_ERROR_SUBMISSION_ID = 0; @fclose($fp); unset($fp); global $CDASH_BACKUP_TIMEFRAME; if ($CDASH_BACKUP_TIMEFRAME != '0') { // Delete the temporary backup file since we now have a better-named one. cdash_unlink($filename); } $new_status = 2; // done, did call do_submit, finished normally } else { add_log("Cannot open file '" . $filename . "'", 'ProcessFile', LOG_ERR, $projectid); $new_status = 3; // done, did *NOT* call do_submit } return $new_status; }
public function doSubmit(DefaultMessage $message) { global $CDASH_BASE_URL, $CDASH_REMOTE_ADDR; // Since this could be running on a remote machine, spoof the IP // to appear as the IP that actually submitted the build $CDASH_REMOTE_ADDR = $message['submission_ip']; $result = do_submit($message['buildsubmissionid'], $message['projectid'], $message['expected_md5'], $message['do_checksum'], $message['submission_id']); // If the submission didn't explicitly fail, delete the submission XML to avoid // duplicate submissions if ($result !== false) { $client = new GuzzleHttp\Client(); $response = $client->request('DELETE', $CDASH_BASE_URL . '/api/v1/deleteBuildSubmissionXml.php', array('query' => array('buildsubmissionid' => $message['buildsubmissiondid']))); } }
$file_path = 'php://input'; $fp = fopen($file_path, 'r'); if ($CDASH_BERNARD_SUBMISSION) { // @todo what serializer should be used? $factory = new PersistentFactory($CDASH_BERNARD_DRIVER, new Serializer()); $producer = new Producer($factory, new EventDispatcher()); $buildSubmissionId = Uuid::uuid4()->toString(); $destinationFilename = $CDASH_BACKUP_DIRECTORY . '/' . $buildSubmissionId . '.xml'; if (copy('php://input', $destinationFilename)) { $producer->produce(new DefaultMessage('DoSubmit', array('buildsubmissionid' => $buildSubmissionId, 'filename' => $destinationFilename, 'projectid' => $projectid, 'expected_md5' => $expected_md5, 'do_checksum' => true, 'submission_id' => 0, 'submission_ip' => $_SERVER['REMOTE_ADDR']))); echo '<cdash version="' . $CDASH_VERSION . "\">\n"; echo " <status>OK</status>\n"; echo " <message>Build submitted successfully.</message>\n"; echo " <submissionId>{$buildSubmissionId}</submissionId>\n"; echo "</cdash>\n"; } else { add_log('Failed to copy build submission XML', 'global:submit.php', LOG_ERR); header('HTTP/1.1 500 Internal Server Error'); echo '<cdash version="' . $CDASH_VERSION . "\">\n"; echo " <status>ERROR</status>\n"; echo " <message>Failed to copy build submission XML.</message>\n"; echo "</cdash>\n"; } } elseif ($CDASH_ASYNCHRONOUS_SUBMISSION) { // If the submission is asynchronous we store in the database do_submit_asynchronous($fp, $projectid, $expected_md5); } else { do_submit($fp, $projectid, $expected_md5, true); } fclose($fp); unset($fp);
/** Function to deal with the external tool mechanism */ function put_submit_file() { include "models/buildfile.php"; // We expect GET to contain the following values: $vars = array('buildid', 'type'); foreach ($vars as $var) { if (!isset($_GET[$var]) || empty($_GET[$var])) { $response_array['status'] = 1; $response_array['description'] = 'Variable \'' . $var . '\' not set but required.'; echo json_encode($response_array); return; } } // Verify buildid. if (!is_numeric($_GET['buildid'])) { $response_array['status'] = 1; $response_array['description'] = 'Variable \'buildid\' is not numeric.'; echo json_encode($response_array); return; } // Abort early if we already have this file. $buildfile = new BuildFile(); $buildfile->BuildId = $_GET['buildid']; $buildfile->Type = htmlspecialchars(pdo_real_escape_string($_GET['type'])); $buildfile->md5 = htmlspecialchars(pdo_real_escape_string($_GET['md5'])); $buildfile->Filename = htmlspecialchars(pdo_real_escape_string($_GET['filename'])); if (!$buildfile->Insert()) { $response_array['status'] = 1; $response_array['description'] = 'Cannot insert buildfile into database. The file might already exist.'; echo json_encode($response_array); return; } // Get the ID of the project associated with this build. $row = pdo_single_row_query("SELECT projectid FROM build WHERE id = {$buildfile->BuildId}"); if (empty($row)) { $response_array['status'] = 1; $response_array['description'] = "Cannot find projectid for build #{$buildfile->BuildId}"; echo json_encode($response_array); return; } $projectid = $row[0]; // Begin writing this file to the backup directory. global $CDASH_BACKUP_DIRECTORY; $uploadDir = $CDASH_BACKUP_DIRECTORY; $ext = pathinfo($buildfile->Filename, PATHINFO_EXTENSION); $filename = $uploadDir . "/" . $buildfile->BuildId . "_" . $buildfile->md5 . ".{$ext}"; if (!($handle = fopen($filename, 'w'))) { $response_array['status'] = 1; $response_array['description'] = "Cannot open file ({$filename})"; echo json_encode($response_array); return; } // Read the data 1 KB at a time and write to the file. $putdata = fopen("php://input", "r"); while ($data = fread($putdata, 1024)) { fwrite($handle, $data); } // Close the streams. fclose($handle); fclose($putdata); // Check that the md5sum of the file matches what we were expecting. $md5sum = md5_file($filename); if ($md5sum != $buildfile->md5) { $response_array['status'] = 1; $response_array['description'] = "md5 mismatch. expected: {$buildfile->md5}, received: {$md5sum}"; unlink($filename); $buildfile->Delete(); echo json_encode($response_array); return; } global $CDASH_ASYNCHRONOUS_SUBMISSION; if ($CDASH_ASYNCHRONOUS_SUBMISSION) { // Create a new entry in the submission table for this file. $bytes = filesize($filename); $now_utc = gmdate(FMT_DATETIMESTD); pdo_query("INSERT INTO submission (filename,projectid,status,attempts,filesize,filemd5sum,created) " . "VALUES ('{$filename}','{$projectid}','0','0','{$bytes}','{$buildfile->md5}','{$now_utc}')"); // Trigger the processing loop in case it's not already running. trigger_process_submissions($projectid); } else { // synchronous processing. $handle = fopen($filename, 'r'); do_submit($handle, $projectid, $buildfile->md5, false); // The file is given a more appropriate name during do_submit, so we can // delete the old file now. unlink($filename); } // Returns the OK submission $response_array['status'] = 0; echo json_encode($response_array); }