REST::fatal(REST::HTTP_OK, 'Lock destroyed successfully'); } REST::fatal(REST::HTTP_NOT_FOUND); } REST::require_method('HEAD', 'GET'); if (isset($_GET['timeout'])) { $timeout = (int) $_GET['timeout']; if ($timeout < 1) { REST::fatal(REST::HTTP_BAD_REQUEST, 'Bad value for parameter "timeout"'); } $description = isset($_GET['description']) ? ', `tokenLockDescription` = ' . Topos::escape_string((string) $_GET['description']) : ''; Topos::real_query(<<<EOS UPDATE `Tokens` SET `tokenLockTimeout` = UNIX_TIMESTAMP() + {$timeout}, `tokenLockCounter` = `tokenLockCounter` + 1 {$description} WHERE `tokenLockUUID` = {$escLockUUID} AND `tokenLockTimeout` > UNIX_TIMESTAMP(); EOS ); if (!Topos::mysqli()->affected_rows) { REST::fatal(REST::HTTP_NOT_FOUND); } } $result = Topos::query(<<<EOS SELECT `tokenId`, `tokenName`, `tokenLockTimeout` - UNIX_TIMESTAMP(), `tokenLockDescription` FROM `Tokens` WHERE `tokenLockUUID` = {$escLockUUID}
* distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Id$ **************************************************************************/ require_once 'include/global.php'; $poolId = Topos::poolId($TOPOS_POOL); if ($_SERVER['REQUEST_METHOD'] === 'DELETE') { $query = <<<EOS DELETE FROM `Tokens` WHERE `Tokens`.`tokenId` = {$TOPOS_TOKEN} AND `poolId` = {$poolId}; EOS; Topos::real_query($query); if (Topos::mysqli()->affected_rows) { REST::fatal(REST::HTTP_OK, 'Token destroyed'); } else { REST::fatal(REST::HTTP_NOT_FOUND); } } REST::require_method('HEAD', 'GET'); if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { REST::fatal(REST::HTTP_NOT_MODIFIED); } $poolId = Topos::poolId($TOPOS_POOL); $result = Topos::query(<<<EOS SELECT `tokenLength`, `tokenType`, `tokenCreated`, `tokenName`, IF(`tokenLockTimeout` > UNIX_TIMESTAMP(), `tokenLockUUID`, NULL) AS 'tokenLockUUID', `tokenLeases` FROM `Tokens`
} $stream = fopen($file['tmp_name'][$key], 'r'); while (!feof($stream)) { $stmt1->send_long_data(0, fread($stream, 8192)); } fclose($stream); if (!$stmt1->execute()) { REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR, 'stmt1: ' . $stmt1->error); } $t_upload_map[$paramname][$key] = $stmt1->insert_id; } // foreach ( $file['name'] as $key => $filename ) } // foreach ($_FILES as $paramname => $file) // For this operation, we need MySQL transactions. Topos::real_query('START TRANSACTION;'); try { foreach ($_FILES as $paramname => $file) { foreach ($file['name'] as $key => $filename) { if (isset($t_upload_map[$paramname][$key])) { $bindTokenId = $t_upload_map[$paramname][$key]; $bindTokenName = empty($filename) ? '' : $filename; $bindTokenType = empty($file['type'][$key]) ? 'application/octet-stream' : $file['type'][$key]; $bindTokenLength = $file['size'][$key]; if (!$stmt2->execute()) { Topos::mysqli()->rollback(); REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR, 'stmt2: ' . $stmt2->error); } // if ( !$stmt2->execute() ) } // if (isset($t_upload_map[$paramname][$key]))
EOS ); Topos::real_query(<<<EOS INSERT INTO `OrphanValues` SELECT `TV`.`tokenId` FROM `TokenValues` AS `TV` NATURAL LEFT JOIN `Tokens` AS `T` WHERE `T`.`tokenId` IS NULL; EOS ); Topos::real_query(<<<EOS DELETE `TokenValues` FROM `OrphanValues` NATURAL JOIN `TokenValues`; EOS ); Topos::real_query(<<<EOS DROP TABLE `OrphanValues`; EOS ); REST::header(array('Content-Type' => REST::best_xhtml_type() . '; charset=UTF-8')); echo REST::html_start('Pool'); echo '<p>Pool destroyed successfully.</p>'; echo REST::html_end(); exit; } REST::require_method('HEAD', 'GET'); // Fetch number of tokens $query = <<<EOS SELECT COUNT(`tokenId`), SUM(UNIX_TIMESTAMP() < `tokenLockTimeout`) FROM `Pools` NATURAL JOIN `Tokens` WHERE `poolName` = {$escPool}; EOS; list($ntokens, $nlocks) = Topos::query($query)->fetch_row();
$row = $result->fetch_row(); if ($row[0] === null) { REST::fatal(REST::HTTP_NOT_FOUND, 'No token available'); } Topos::real_query(<<<EOS UPDATE `Pools` SET `minLeases` = {$row[0]} WHERE `poolId` = {$poolId}; EOS ); } // while while ($row = $result->fetch_row()) { Topos::real_query(<<<EOS UPDATE `Tokens` SET `tokenLeases` = {$row[1]} + 1 {$timeout} WHERE `tokenId` = {$row[0]} AND `tokenLeases` = {$row[1]}; EOS ); if (Topos::mysqli()->affected_rows) { break 2; } } // while } // while $url = Topos::urlbase() . 'pools/' . REST::urlencode($TOPOS_POOL) . '/tokens/' . $row[0]; if ($lockUUID) { header("X-Topos-OpaqueLockToken: opaquelocktoken:{$lockUUID}"); header('X-Topos-LockURL: ' . Topos::urlbase() . 'pools/' . REST::urlencode($TOPOS_POOL) . '/locks/' . $lockUUID); } REST::redirect(REST::HTTP_SEE_OTHER, $url);
if (empty($_GET['timeout']) || (int) $_GET['timeout'] < 1) { Topos::real_query(<<<EOS UPDATE `Tokens` SET `tokenLeases` = {$tokenLeases} + 1 WHERE `tokenId` = {$tokenId} AND `tokenLeases` = {$tokenLeases}; EOS ); } else { $lockUUID = Topos::uuid(); $timeout = (int) $_GET['timeout']; $description = isset($_GET['description']) ? $_GET['description'] : ''; $description = Topos::escape_string($description); Topos::real_query(<<<EOS UPDATE `Tokens` SET `tokenLeases` = {$tokenLeases} + 1, `tokenLockTimeout` = UNIX_TIMESTAMP() + {$timeout}, `tokenLockUUID` = '{$lockUUID}', `tokenLockDescription` = {$description} WHERE `tokenId` = {$tokenId} AND `tokenLeases` = {$tokenLeases}; EOS ); } if (Topos::mysqli()->affected_rows) { break 2; } } // while } // while } // if (isset($_GET['token'])) $url = Topos::urlbase() . 'pools/' . REST::urlencode($TOPOS_POOL) . '/tokens/' . $row[0];
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Id$ **************************************************************************/ require_once 'include/global.php'; $escPool = Topos::escape_string($TOPOS_POOL); // TODO: the DELETE handler was written by Evert, using a subquery. I'm used // to doing this with a single JOIN query... if ($_SERVER['REQUEST_METHOD'] === 'DELETE') { Topos::real_query(<<<EOS UPDATE `Tokens` SET `tokenLockTimeout` = 0, `tokenLockUUID` = null WHERE `poolId` = (SELECT `poolId` FROM `Pools` WHERE `Pools`.`poolName` = {$escPool}); EOS ); REST::header(array('Content-Type' => REST::best_xhtml_type() . '; charset=UTF-8')); echo REST::html_start('Locks'); echo '<p>Locks destroyed successfully.</p>'; echo REST::html_end(); exit; } REST::require_method('HEAD', 'GET'); $result = Topos::query(<<<EOS SELECT `tokenId`, `tokenName`, `tokenLockUUID`, `tokenLockTimeout` - UNIX_TIMESTAMP(), `tokenLockDescription` FROM `Pools` NATURAL JOIN `Tokens` WHERE `poolName` = {$escPool} AND `tokenLockTimeout` > UNIX_TIMESTAMP()
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Id$ **************************************************************************/ require_once 'include/global.php'; $poolId = Topos::poolId($TOPOS_POOL); // TODO: the DELETE handler was written by Evert, using a subquery. I'm used // to doing this with a single JOIN query... if ($_SERVER['REQUEST_METHOD'] === 'DELETE') { Topos::real_query(<<<EOS UPDATE `Tokens` SET `tokenLockTimeout` = 0, `tokenLockUUID` = null WHERE `poolId` = {$poolId}; EOS ); REST::fatal(REST::HTTP_OK, 'Locks destroyed successfully'); } REST::require_method('HEAD', 'GET'); $result = Topos::query(<<<EOS SELECT `tokenId`, `tokenName`, `tokenLockUUID`, `tokenLockTimeout` - UNIX_TIMESTAMP(), `tokenLockDescription` FROM `Tokens` WHERE `poolId` = {$poolId} AND `tokenLockTimeout` > UNIX_TIMESTAMP() ORDER BY 1; EOS ); $directory = RESTDir::factory();
} $escPoolName = Topos::escape_string($pool); Topos::real_query("CALL `createTokens`({$escRealm}, {$escPoolName}, {$tokens});"); Topos::log('populate', array('realmName' => $TOPOS_REALM, 'poolName' => $TOPOS_POOL, 'tokens' => $tokens)); REST::header(array('Content-Type' => REST::best_xhtml_type() . '; charset=UTF-8')); echo REST::html_start('Realm'); echo '<p>Pool populated successfully.</p>' . '<p><a href="./" rel="index">Back</a></p>'; echo REST::html_end(); exit; } if ($_SERVER['REQUEST_METHOD'] === 'DELETE') { Topos::real_query('START TRANSACTION;'); try { Topos::real_query(<<<EOS DELETE `Tokens`.* FROM `Tokens` NATURAL JOIN `Pools` WHERE `Pools`.`realmName` = {$escRealm}; EOS ); Topos::log('delete', array('realm' => $TOPOS_REALM, 'tokens' => Topos::mysqli()->affected_rows)); } catch (Topos_MySQL $e) { Topos::mysqli()->rollback(); throw $e; } if (!Topos::mysqli()->commit()) { REST::fatal(REST::HTTP_SERVICE_UNAVAILABLE, 'Transaction failed: ' . htmlentities(Topos::mysqli()->error)); } REST::header(array('Content-Type' => REST::best_xhtml_type() . '; charset=UTF-8')); echo REST::html_start('Pool'); echo '<p>Realm destroyed successfully.</p>'; echo REST::html_end(); exit;
* * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Id$ **************************************************************************/ require_once 'include/global.php'; $escPool = Topos::escape_string($TOPOS_POOL); //Deprecated: if ($_SERVER['REQUEST_METHOD'] === 'DELETE') { Topos::real_query(<<<EOS DELETE `Pools`, `Tokens` FROM `Pools` NATURAL LEFT JOIN `Tokens` WHERE `Pools`.`poolName` = {$escPool}; EOS ); REST::fatal(REST::HTTP_OK, 'Pool destroyed successfully.'); } REST::require_method('HEAD', 'GET'); // Fetch number of tokens $query = <<<EOS SELECT COUNT(`tokenId`), SUM(UNIX_TIMESTAMP() < `tokenLockTimeout`) FROM `Pools` NATURAL JOIN `Tokens` WHERE `poolName` = {$escPool}; EOS; list($ntokens, $nlocks) = Topos::query($query)->fetch_row(); $form = <<<EOS <h2>Forms</h2> <h3>Getting the next token</h3>