Esempio n. 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);
     }
 }
$total = (int) $_GET['total'];
if ($total === 0) {
    $total = 1;
}
$percentage = 100 * $tokens / $total;
if ($percentage > 100) {
    REST::fatal(REST::HTTP_BAD_REQUEST, <<<EOS
<p>The total number of tokens cannot be smaller than the number of tokens in this pool.</p>
<form action="progress" method="get">
<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>
Esempio n. 3
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;
}
REST::header(array('Content-Type' => $xhtmltype . '; charset=UTF-8', 'Cache-Control' => 'no-cache'));
         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) {
     echo REST::html_start('Metadata for handle ' . htmlspecialchars($handle->handle(), ENT_COMPAT, 'UTF-8'));
        $r_handles[$r_handle] = 1;
    }
    if ($handles === null) {
        $handles = $r_handles;
    } else {
        $handles = array_intersect_key($handles, $r_handles);
    }
}
ksort($handles);
$handles = array_keys($handles);
if ($p_max > 0) {
    $handles = array_slice($handles, $p_page * $p_max, $p_max);
}
//... And print everything
$xhtml_type = REST::best_xhtml_type() . '; charset=UTF-8';
$content_type = REST::best_content_type(array($xhtml_type => 1.0, 'application/json' => 1.0), $xhtml_type);
REST::header(array('status' => REST::HTTP_OK, 'Content-Type' => $content_type));
// For a HEAD request, we can quit now:
if ($_SERVER['REQUEST_METHOD'] === 'HEAD') {
    exit;
}
if ($content_type == $xhtml_type) {
    echo REST::html_start('Searchresults');
    echo <<<EOS
<table class="searchresults"><tbody><tr>
<th class="handle">Handle</th>
</tr>
EOS;
    $num_rows = 0;
    while ($search_stmt->fetch()) {
        $num_rows++;
       (`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
);
    $server = escapeshellarg(trim($_POST['server']));
    $filename = escapeshellarg(Portal::PROXY_DIR . $userdnmd5 . '.pem');
    exec("echo {$password} | myproxy-logon -v -l {$username} -s {$server} -S -o {$filename} 2>&1", $output, $returnval);
    $output = implode("\n", $output);
    if (preg_match('/^(?:invalid pass phrase|No credentials exist for username .*)$/m', $output)) {
        REST::fatal(REST::HTTP_UNAUTHORIZED, 'Invalid username and/or pass phrase');
    }
    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>
        } catch (Portal_MySQL_Exception $e) {
            unlink($tmpfilename);
            REST::fatal(REST::HTTP_CONFLICT, "Can't overwrite existing file: name={$dbname}, version={$dbversion}, type={$_POST['type']}");
        }
        $insert_id = Portal_MySQL::mysql()->insert_id;
        if (!rename($tmpfilename, Portal_DB::DATABASE_DIR . $insert_id)) {
            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>";
}