Example #1
0
 /**
  * @param $title string plain text
  * @return RESTDir
  */
 public static function factory($title = null)
 {
     if ($title === null) {
         preg_match('@^(.*)/@', $_SERVER['REQUEST_URI'], $matches);
         $title = 'Index for ' . htmlspecialchars($matches[1] . '/', ENT_QUOTES, 'UTF-8');
     }
     $best_xhtml_type = REST::best_xhtml_type();
     $type = REST::best_content_type(array($best_xhtml_type => 1.0, 'text/plain' => 0.3, 'text/tdv' => 0.5, 'text/csv' => 0.8, 'application/json' => 1.0), $best_xhtml_type);
     REST::header("{$type}; charset=UTF-8");
     switch ($type) {
         case 'application/xhtml+xml':
         case 'text/html':
             return new RESTDirHTML($title);
         case 'text/tdv':
         case 'text/plain':
             return new RESTDirPlain($title);
         case 'application/json':
             return new RESTDirJSON($title);
         case 'text/csv':
             return new RESTDirCSV($title);
     }
 }
Example #2
0
}
$result = Topos::query(<<<EOS
SELECT `tokenId`,
       `tokenName`,
       `tokenLockTimeout` - UNIX_TIMESTAMP(),
       `tokenLockDescription`
FROM `Tokens`
WHERE `tokenLockUUID` = {$escLockUUID}
  AND `tokenLockTimeout` > UNIX_TIMESTAMP();
EOS
);
if (!($row = $result->fetch_row())) {
    REST::fatal(REST::HTTP_NOT_FOUND);
}
$tokenURL = Topos::urlbase() . 'pools/' . REST::urlencode($TOPOS_POOL) . '/tokens/' . $row[0];
$xhtmltype = REST::best_xhtml_type();
$bct = REST::best_content_type(array($xhtmltype => 1, 'text/plain' => 1), $xhtmltype);
if ($bct === 'text/plain') {
    REST::header(array('Content-Type' => 'text/plain; charset=US-ASCII', 'Cache-Control' => 'no-cache'));
    if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
        exit;
    }
    echo <<<EOS
TokenId: {$row[0]}
TokenName: {$row[1]}
TokenURL: {$tokenURL}
Timeout: {$row[2]}
Description: {$row[3]}
EOS;
    exit;
}
     } elseif (!isset($handle->type[$index]) || $handle->type[$index] != 'URL') {
         REST::fatal(REST::HTTP_NOT_FOUND, 'No URL with that index');
     }
     // Finally, get the URL:
     $url = $handle->data[$index];
     // Append the URL with the current query string.
     // We leave out the "index" and "redirect" fields, though.
     unset($_GET['index']);
     unset($_GET['redirect']);
     if (!empty($_GET)) {
         $url .= '?' . http_build_query($_GET);
     }
     // Finally, perform the actual redirect:
     REST::redirect(REST::HTTP_TEMPORARY_REDIRECT, $url);
 }
 $xhtml_type = REST::best_xhtml_type() . '; charset=UTF-8';
 $content_type = REST::best_content_type(array($xhtml_type => 1.0, 'application/json' => 1.0, 'application/x-www-form-urlencoded' => 1.0, 'text/plain; charset=US-ASCII' => 0.5), $xhtml_type);
 // When was this handle last modified?
 $modified = 0;
 foreach ($handle->timestamp as $idx => $timestamp) {
     if ($timestamp > $modified) {
         $modified = $timestamp;
     }
 }
 REST::check_if_modified_since($modified);
 REST::header(array('status' => REST::HTTP_OK, 'Content-Type' => $content_type, 'Modified' => REST::http_date($modified)));
 // For a HEAD request, we can quit now:
 if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
     exit;
 }
 if ($content_type == $xhtml_type) {
<input type="text" name="total"/> Total number of tokens<br/>
<input type="submit" value="Show progress bar"/>
</form>
EOS
);
}
$bct = REST::best_content_type(array('text/html' => 1, 'application/xhtml+xml' => 1, 'text/plain' => 1), 'text/html');
if ($bct === 'text/plain') {
    REST::header(array('Content-Type' => 'text/plain; charset=US-ASCII', 'Refresh' => '60; ' . $_SERVER['REQUEST_URI'], 'Cache-Control' => 'no-cache'));
    if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
        exit;
    }
    echo $tokens / $total;
    exit;
}
REST::header(array('Content-Type' => REST::best_xhtml_type() . '; charset=UTF-8', 'Refresh' => '60; ' . $_SERVER['REQUEST_URI'], 'Cache-Control' => 'no-cache'));
if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
    exit;
}
echo REST::html_start('Progress');
?>
<table class="progress"><tbody>
<tr>
  <td style="width: <?php 
echo $width * $tokens / $total;
?>
pt;" class="done">
  <?php 
if ($percentage >= 50) {
    echo sprintf('%.1f%%', $percentage);
}
    /**
     * Shows a message screen to the user.
     * @param string $message HTML message
     * @param string $status HTTP status
     * @param string $redirect URL for automatic redirection
     * @param string $location Location of the created URL
     */
    public static function show_message($message, $status, $location)
    {
        REST::header(array('status' => $status, 'Content-Type' => REST::best_xhtml_type() . '; charset=UTF-8', 'Location' => REST::rel2url($location)));
        echo REST::html_start('Redirect') . <<<EOS
<p>{$message}</p>
<script type="text/javascript"><![CDATA[
  setTimeout(
    'window.location.href = "{$location}";',
    1000
  );
]]></script>
EOS;
        echo REST::html_end();
        exit;
    }
EOS
);
    REST::fatal(REST::HTTP_ACCEPTED);
}
REST::require_method('GET', 'HEAD');
$result = Portal_MySQL::query(<<<EOS
SELECT SUM(`usage_seconds`),
       DATE(`usage_timestamp`),
       `usage_status`
FROM `Usage`
WHERE `user_id` = {$userid}
GROUP BY 3,2
ORDER BY 3,2 ASC;
EOS
);
REST::header(REST::best_xhtml_type() . '; charset="UTF-8"');
echo REST::html_start('Usage statistics') . <<<EOS
<!--<form action="stats" method="post">
token: <input type="text" name="token" value=""/>
seconds: <input type="text" name="seconds" value=""/>
<input type="submit"/>
</form>-->
<table class="usagestats"><tbody>
<tr>
<th class="date">Date</th>
<th class="walltime">Walltime</th>
<th class="status">Status</th>
</tr>
EOS;
$STATUS_STRING = array(0 => 'OK', 1 => 'Transient error', 2 => 'Fatal error');
while ($row = $result->fetch_row()) {
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();
$form = <<<EOS
<h2>Forms</h2>
       (`tokenId`, `poolId`, `tokenType`, `tokenName`, `tokenCreated`, `tokenLength`)
SELECT {$tokenId}, {$poolId}, {$tokenType}, {$tokenName},
       UNIX_TIMESTAMP(), LENGTH(`tokenValue`)
FROM `TokenValues`
WHERE `tokenId` = {$tokenId};
EOS
);
        Topos::real_query('SET foreign_key_checks = 1;');
    } 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));
    }
    $type = REST::best_content_type(array('text/plain' => 1.0, REST::best_xhtml_type() => 1.0), REST::best_xhtml_type());
    $tokenURL = Topos::urlbase() . 'pools/' . REST::urlencode($TOPOS_POOL) . '/tokens/' . $tokenId;
    REST::created($tokenURL);
}
REST::require_method('HEAD', 'GET');
if (isset($_GET['token'])) {
    $escToken = Topos::escape_string(str_replace(array('%', '_', '*'), array('\\%', '\\_', '%'), $_GET['token']));
    $result = Topos::query(<<<EOS
SELECT `tokenId`, `tokenLeases`
FROM `Tokens`
WHERE `poolId` = {$poolId}
  AND `tokenName` LIKE {$escToken}
  AND `Tokens`.`tokenLockTimeout` <= UNIX_TIMESTAMP()
ORDER BY 2,1;
EOS
);
    if ($returnval) {
        REST::fatal(REST::HTTP_BAD_REQUEST, '<pre>' . htmlentities($output) . '</pre>');
    }
    $escserver = Portal_MySQL::escape_string($_POST['server']);
    $escusername = Portal_MySQL::escape_string($_POST['username']);
    $escpassword = Portal_MySQL::escape_string($_POST['password']);
    Portal_MySQL::real_query("UPDATE `User` SET `proxy_server` = {$escserver}, `proxy_username` = {$escusername}, `proxy_password` = {$escpassword} WHERE `user_dn_md5` = '{$userdnmd5}'");
    $best_xhtml_type = REST::best_xhtml_type();
    $type = REST::best_content_type(array($best_xhtml_type => 1.0, 'text/plain' => 1.0), $best_xhtml_type);
    $relurl = REST::urlencode(dirname($_SERVER['REDIRECT_URL'])) . '/proxy';
    REST::header(array('status' => REST::HTTP_CREATED, 'Location' => REST::urlbase() . $relurl, 'Content-Type' => "{$type}; charset=UTF-8"));
    if ($type == 'text/plain') {
        echo REST::urlbase() . $relurl;
    } else {
        echo Portal::html_start('Proxy created') . "<p><a href=\"proxy\">proxy</a></p>" . Portal::html_end();
    }
    exit;
}
REST::header(REST::best_xhtml_type() . "; charset=UTF-8");
$default_server = getenv('MYPROXY_SERVER');
echo Portal::html_start("myProxy") . <<<EOS
<form action="./myproxy" method="post">
<table border="0" cellpadding="0" cellspacing="0"><tbody>
  <tr><td>Username</td><td><input type="text" name="username" /></td></tr>
  <tr><td>Password</td><td><input type="password" name="password" /></td></tr>
  <tr><td>MyProxy server</td><td><input type="text" name="server" value="{$default_server}" /></td></tr>
  <tr><td>&nbsp;</td><td><input type="submit" value="Delegate" /></td></tr>
</tbody></table>
</form>
EOS
 . Portal::html_end();
EOS
);
    exec("cd '{$sandbox}'; find -mindepth 1 -maxdepth 1 -print0 | xargs -0 tar zcf {$TEMPNAM}.tgz", $output, $return_var);
    if ($return_var) {
        $output = implode("\n", $output);
        REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR, $output);
    }
    $tokenhandle = fopen("{$TEMPNAM}.tgz", 'r');
    try {
        $token_url = Topos::putTokenFile($tokenhandle, 'application/x-compressed-tar');
    } catch (Exception $e) {
        fclose($tokenhandle);
        throw $e;
    }
    fclose($tokenhandle);
    $token_id = basename($token_url);
    Portal_MySQL::real_query(<<<EOS
INSERT INTO `Token`
       ( `token_id`,  `user_id` )
VALUES ( {$token_id}, {$user_id} );
EOS
);
    $resultURL = REST::urlbase() . Portal::portalURL() . "jobstates/{$token_id}";
    REST::created($resultURL);
}
Portal_User::current();
REST::header(array('Content-Type' => REST::best_xhtml_type()));
echo Portal::html_start("{$appname}-{$appversion}") . '<form action="' . $appversion . '" method="post" enctype="multipart/form-data">';
$portlet->doGET();
echo '</form>' . Portal::html_end();
            unlink($tmpfilename);
            Portal_MySQL::real_query("DELETE FROM `Database` WHERE `database_id` = {$insert_id}");
            REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR, "Couldn't store uploaded file.");
        }
        chmod(Portal_DB::DATABASE_DIR . $insert_id, 0660);
    }
    $extension = Portal_DB::databaseTypeExtension($typeId);
    $htmlurl = "{$insert_id}.{$extension}";
    $fullurl = REST::urlbase() . $_SERVER['REDIRECT_URL'] . $htmlurl;
    $content_type = REST::best_content_type(array(REST::best_xhtml_type() => 1.0, 'text/plain' => 0.5), 'text/plain');
    if ($content_type == 'text/plain') {
        REST::header(array('status' => REST::HTTP_CREATED, 'Location' => $fullurl, 'Content-Type' => 'text/plain; charset=US-ASCII'));
        echo $fullurl;
        exit;
    }
    REST::header(array('status' => REST::HTTP_CREATED, 'Location' => $fullurl, 'Content-Type' => REST::best_xhtml_type() . '; charset=US-ASCII'));
    echo Portal::html_start('New database created') . "<a href=\"{$htmlurl}\" rel=\"child\" rev=\"index\">{$htmlurl}</a>" . Portal::html_end();
    exit;
}
REST::require_method('GET', 'HEAD');
$options = '';
foreach (Portal_DB::databaseTypeIDs() as $databaseTypeID) {
    $databaseTypeName = Portal_DB::databaseTypeName($databaseTypeID);
    $options .= "\n<option value=\"{$databaseTypeName}\">{$databaseTypeName}</option>";
}
$directory = RESTDir::factory("{$path_info[0]}, version {$path_info[1]}")->setForm(<<<EOS
<h1>Database upload</h1>
<form method="post" action="./" enctype="multipart/form-data">
<input type="file" name="dbfile" /><br />
<input type="checkbox" name="shared" value="1" /> Share this database with others<br />
Database type: <select name="type">