/** * Wrapper around fopen('php://input', 'r'). * * This wrapper is necessary to facilitate chunked transfer encoding and * method spoofing (in case PUT requests). * @return resource filehandle */ public static function inputhandle() { if (self::$inputhandle === null) { if (isset($_SERVER['CONTENT_LENGTH'])) { self::$inputhandle = fopen('php://input', 'r'); } elseif ($_SERVER['HTTP_TRANSFER_ENCODING'] == 'chunked') { self::$inputhandle = tmpfile(); $input = fopen('php://input', 'r'); while (!feof($input)) { fwrite(self::$inputhandle, fgetc($input)); } fclose($input); fseek(self::$inputhandle, 0); } else { self::fatal(self::HTTP_LENGTH_REQUIRED); } } return self::$inputhandle; }
Topos::real_query("CALL `createTokens`({$escPool}, {$ntokens}, {$offset});"); REST::fatal(REST::HTTP_ACCEPTED); } elseif (isset($_POST['tokens'])) { $input = tmpfile(); fwrite($input, $_POST['tokens']); fseek($input, 0); $_SERVER['CONTENT_TYPE'] = 'text/plain; charset="UTF-8"'; } else { REST::fatal(REST::HTTP_BAD_REQUEST); } } // Handle a upload of a single text file, of which each line will be a token. if ($_SERVER['REQUEST_METHOD'] === 'POST' && strpos(@$_SERVER['CONTENT_TYPE'], 'text/') === 0) { $esccontenttype = Topos::escape_string($_SERVER['CONTENT_TYPE']); if (!$input) { $input = REST::inputhandle(); } $stmt1 = Topos::mysqli()->prepare(<<<EOS INSERT INTO `TokenValues` ( `tokenValue` ) VALUES (?); EOS ); $stmt2 = Topos::mysqli()->prepare(<<<EOS INSERT INTO `Tokens` ( `tokenId`, `poolId`, `tokenType`, `tokenLength`, `tokenCreated` ) VALUES (?, {$poolId}, {$esccontenttype}, ?, UNIX_TIMESTAMP()); EOS ); $bindTokenValue = $bindTokenId = $bindTokenLength = null; $stmt1->bind_param("s", $bindTokenValue);
*/ require_once 'include/global.php'; require_once 'topos.php'; REST::require_method('GET', 'HEAD', 'PUT', 'DELETE'); $user_id = Portal_User::current()->user_id(); $path_info = Portal::path_info(); $jobid = $path_info[0]; $escjobid = Portal_MySQL::escape_string($jobid); $escuserid = Portal_MySQL::escape_string($user_id); if ($_SERVER['REQUEST_METHOD'] == 'PUT') { if (strpos(@$_SERVER['CONTENT_TYPE'], 'text/plain') !== 0) { REST::fatal(REST::HTTP_UNSUPPORTED_MEDIA_TYPE); } // The job finished with an error and tries to inform us about it $errorstring = ''; while (($line = fread(REST::inputhandle(), 8192)) !== '') { $errorstring .= $line; } if (!strlen($errorstring)) { REST::fatal(REST::HTTP_BAD_REQUEST, 'No error string specified'); } $errorstring = Portal_MySQL::escape_string($errorstring); Portal_MySQL::real_query(<<<EOS UPDATE `Token` SET `token_error` = CONCAT(`token_error`, {$errorstring}) WHERE `token_id`={$escjobid} AND `user_id`={$escuserid}; EOS ); REST::header(array('status' => REST::HTTP_NO_CONTENT)); exit;
$user_id = Portal_User::current()->user_id(); $path_info = Portal::path_info(); $jobid = $path_info[0]; $escjobid = Portal_MySQL::escape_string($jobid); $escuserid = Portal_MySQL::escape_string($user_id); if ($_SERVER['REQUEST_METHOD'] == 'PUT') { if (strpos(@$_SERVER['CONTENT_TYPE'], 'application/x-compressed-tar') !== 0) { REST::fatal(REST::HTTP_UNSUPPORTED_MEDIA_TYPE); } // The job wants to put its results on the portal server $tmpfilename = tempnam('/tmp', 'portal_'); $tmpfile = fopen($tmpfilename, 'w'); while (($block = fread(REST::inputhandle(), 8192)) !== "") { fwrite($tmpfile, $block); } fclose(REST::inputhandle()); fclose($tmpfile); if (isset($_SERVER['CONTENT_LENGTH']) && $_SERVER['CONTENT_LENGTH'] != filesize($tmpfilename)) { unlink($tmpfilename); REST::fatal(REST::HTTP_BAD_REQUEST, "Content-Length header doesn't match actual content length."); } if (!rename($tmpfilename, Portal::JOBRESULTS_DIR . $jobid)) { unlink($tmpfilename); REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR, "Couldn't store uploaded file."); } chmod(Portal::JOBRESULTS_DIR . $jobid, 0660); REST::header(array('status' => REST::HTTP_NO_CONTENT)); exit; } // The user tries to get information about his jobs if (file_exists($fullfilename = Portal::JOBRESULTS_DIR . $jobid)) {
**************************************************************************/ require_once 'include/global.php'; $poolId = Topos::poolId($TOPOS_POOL); if ($_SERVER['REQUEST_METHOD'] === 'PUT') { $tokenType = Topos::escape_string(empty($_SERVER['CONTENT_TYPE']) ? 'application/octet-stream' : $_SERVER['CONTENT_TYPE']); $tokenName = ''; if (!empty($_SERVER['HTTP_CONTENT_DISPOSITION'])) { if (preg_match('/;\\s*filename\\s*=\\s*"((?:[^"\\\\]|\\\\.)+)"/', $_SERVER['HTTP_CONTENT_DISPOSITION'], $matches)) { $tokenName = $matches[1]; } } $tokenName = Topos::escape_string($tokenName); $stmt = Topos::mysqli()->prepare('INSERT INTO `TokenValues` (`tokenValue`) VALUES (?);'); $null = null; $stmt->bind_param("b", $null); $stream = REST::inputhandle(); while (!feof($stream)) { $stmt->send_long_data(0, fread($stream, 8192)); } fclose($stream); if (!$stmt->execute()) { REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR, $stmt->error); } $tokenId = Topos::mysqli()->insert_id; Topos::real_query(<<<EOS INSERT INTO `Tokens` (`tokenId`, `poolId`, `tokenType`, `tokenName`, `tokenCreated`, `tokenLength`) SELECT {$tokenId}, {$poolId}, {$tokenType}, {$tokenName}, UNIX_TIMESTAMP(), LENGTH(`tokenValue`) FROM `TokenValues` WHERE `tokenId` = {$tokenId};