* 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: jobstates.php 2490 2009-08-26 10:44:52Z pieterb $
 **************************************************************************/
/**
 * File documentation.
 * @package Portal
 */
require_once 'include/global.php';
REST::require_method('GET', 'HEAD');
$user_id = Portal_User::current()->user_id();
$resultDir = opendir(Portal::JOBRESULTS_DIR);
$resultFiles = array();
while (($file = readdir($resultDir)) !== false) {
    if (preg_match('/^\\d+$/', $file)) {
        $resultFiles[(int) $file] = (int) $file;
    }
}
closedir($resultDir);
$result = Portal_MySQL::query(<<<EOS
SELECT `token_id`, `token_error` = '' FROM `Token`
 WHERE `user_id` = {$user_id};
EOS
);
$directory = RESTDir::factory();
while ($row = $result->fetch_row()) {
    $directory->line($row[0], array('status' => isset($resultFiles[$row[0]]) ? 'Done' : ($row[1] ? 'Queued' : 'Error')));
}
$directory->end();
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at <http://www.apache.org/licenses/LICENSE-2.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: databases.php 2459 2009-08-10 21:20:41Z pieterb $
 **************************************************************************/
/**
 * File documentation.
 * @package Portal
 */
require_once 'include/global.php';
REST::require_method('GET', 'HEAD');
$user_id = Portal_User::current()->user_id();
$result = Portal_MySQL::query(<<<EOS
SELECT DISTINCT `name` FROM `Database`
WHERE `is_shared` > 0
   OR `user_id` = {$user_id}
EOS
);
$action = REST::htmlspecialchars($_SERVER['REQUEST_URI']);
$directory = RESTDir::factory('Available databases (by name)');
while ($row = $result->fetch_row()) {
    $directory->line($row[0] . '/');
}
$directory->end();
    $user_dn_md5 = Portal_MySQL::escape_string(basename($fullfilename, '.pem'));
    $result = Portal_MySQL::query(<<<EOS
SELECT `proxy_server`, `proxy_username`, `proxy_password` FROM `User`
 WHERE `user_dn_md5` = {$user_dn_md5};
EOS
);
    if ($row = $result->fetch_row()) {
        $escusername = escapeshellarg($row[1]);
        $escpassword = escapeshellarg($row[2]);
        $escserver = escapeshellarg($row[0]);
        exec("echo {$escpassword} | myproxy-logon -v -l {$escusername} -s {$escserver} -S -o {$escfullfilename} 2>&1", $output, $returnval);
        if ($returnval) {
            unlink($fullfilename);
            Portal_MySQL::query(<<<EOS
UPDATE `User` SET `proxy_server` = NULL, `proxy_username` = NULL, `proxy_password` = NULL
 WHERE `user_dn_md5` = {$user_dn_md5};
EOS
);
        }
    }
    //  exec("grid-proxy-info -f {$escfullfilename} -timeleft", $output, $returnval);
    //  if ((int)$output[0] <= 0) { // The proxy has expired completely
    //    unlink($fullfilename);
    //    Portal_MySQL::query(<<<EOS
    //UPDATE `User` SET `proxy_server` = NULL, `proxy_username` = NULL, `proxy_password` = NULL
    // WHERE `user_dn_md5` = '{$user_dn_md5}';
    //EOS
    //    );
    //  }
}
REST::header(array('status' => REST::HTTP_NO_CONTENT));
    Portal_MySQL::real_query(<<<EOS
DELETE FROM `Token`
 WHERE `token_id`={$escjobid}
   AND `user_id`={$escuserid};
EOS
);
    if (!Portal_MySQL::mysql()->affected_rows) {
        REST::fatal(REST::HTTP_NOT_FOUND);
    }
    REST::header(array('status' => REST::HTTP_NO_CONTENT));
    exit;
}
// The user tries to get information about eir jobs
if (file_exists($fullfilename = Portal::JOBRESULTS_DIR . $jobid)) {
    REST::redirect(REST::HTTP_SEE_OTHER, Portal::portalURL() . "jobresults/{$jobid}");
}
$result = Portal_MySQL::query(<<<EOS
SELECT `token_error` 
  FROM `Token`
 WHERE `token_id`={$escjobid}
   AND `user_id`={$escuserid};
EOS
);
if (!($row = $result->fetch_row())) {
    // Can't find what the user is looking for
    REST::fatal(REST::HTTP_GONE);
}
if (empty($row[0])) {
    REST::fatal(REST::HTTP_NOT_FOUND, "<p>Your job hasn't been executed yet. Try again later.</p>");
}
REST::fatal(REST::HTTP_OK, '<p>Your job finished with the following error:</p><pre>' . REST::htmlspecialchars($row[0]) . '</pre>');
REST::header('text/plain; charset=UTF-8');
$TEMPNAM = tempnam('/tmp', 'portal');
register_shutdown_function('myRemoveTempFiles');
foreach (glob(Portal::PROXY_DIR . '*.pem') as $fullproxyfile) {
    #$escfilename = escapeshellarg($filename);
    $proxyfile = basename($fullproxyfile);
    if (!preg_match('/^([a-f0-9]{32})\\.pem$/', $proxyfile, $matches)) {
        Portal::debug("Strange proxy filename: {$proxyfile}");
        @unlink($fullproxyfile);
        continue;
    }
    $userdnmd5 = $matches[1];
    $escuserdnmd5 = Portal_MySQL::escape_string($userdnmd5);
    $result = Portal_MySQL::query(<<<EOS
SELECT `user_dn`, `proxy_server`, `proxy_username`, `proxy_password`
FROM `User`
WHERE `user_dn_md5` = {$escuserdnmd5};
EOS
);
    if (!($row = $result->fetch_row())) {
        Portal::debug("A proxy file {$proxyfile} was found, but there's no corresponding user in the database!");
        @unlink($fullproxyfile);
        continue;
    }
    $userdn = $row[0];
    $escproxyserver = escapeshellarg($proxyserver = $row[1]);
    $proxyusername = escapeshellarg($row[2]);
    $proxypassword = $row[3];
    $escfullproxyfile = escapeshellarg($fullproxyfile);
    $output = '';
    exec("grid-proxy-info -f {$escfullproxyfile} -exists -valid 0:30", $output, $returnval);
    if ($returnval && $proxypassword === null) {
        REST::fatal(REST::HTTP_BAD_REQUEST, 'Missing required parameter "status"');
    }
    $status = (int) $_POST['status'];
    Portal_MySQL::real_query(<<<EOS
INSERT INTO `Usage` (`user_id`, `usage_seconds`, `token_id`, `usage_status`)
VALUES ({$userid}, {$seconds}, {$token}, {$status});
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>
    /**
     * @param bool $required
     * @return Portal_User
     */
    public static function current()
    {
        if (self::$current === null) {
            switch ($_SERVER['SERVER_PORT']) {
                case Portal::PORT_PLAIN:
                    self::unauthorized();
                    break;
                    // strictly unnecessary, but syntactically nicer.
                // strictly unnecessary, but syntactically nicer.
                case Portal::PORT_SSL:
                    if (!isset($_SERVER['PHP_AUTH_USER'])) {
                        self::unauthorized();
                    }
                    $user_email = Portal_MySQL::escape_string($_SERVER['PHP_AUTH_USER']);
                    $user_password = md5($_SERVER['PHP_AUTH_PW']);
                    $result = Portal_MySQL::query(<<<EOS
SELECT `user_id`, `user_name`, `user_dn` FROM `User`
WHERE `user_email`   =  {$user_email}
  AND `user_password`= '{$user_password}';
EOS
);
                    if (!($row = $result->fetch_row())) {
                        self::unauthorized();
                    }
                    self::$current = new Portal_User((int) $row[0], $_SERVER['PHP_AUTH_USER'], $row[1], $row[2]);
                    break;
                case Portal::PORT_SSL_CSA:
                    $user_dn = self::csa_dn();
                    if (isset($_SERVER['PHP_AUTH_USER']) && (int) $_SERVER['PHP_AUTH_USER'] > 0 && preg_match('@^/O=dutchgrid/O=users/O=sara/CN=(?:Evert Lammerts|Pieter van Beek)@', $_SERVER['SSL_CLIENT_S_DN'])) {
                        $esc_user_id = (int) $_SERVER['PHP_AUTH_USER'];
                        $result = Portal_MySQL::query(<<<EOS
SELECT `user_email`, `user_name`, `user_dn` FROM `User`
WHERE `user_id` = {$esc_user_id};
EOS
);
                        if (!($row = $result->fetch_row())) {
                            REST::fatal(REST::HTTP_UNAUTHORIZED, "No such user id: {$esc_user_id}");
                        }
                        self::$current = new Portal_User($esc_user_id, $row[1], $row[0], $row[2], true);
                    } else {
                        $esc_user_dn = Portal_MySQL::escape_string($user_dn);
                        $result = Portal_MySQL::query(<<<EOS
SELECT `user_id`, `user_email`, `user_name` FROM `User`
WHERE `user_dn` =  {$esc_user_dn};
EOS
);
                        if (!($row = $result->fetch_row())) {
                            self::unauthorized();
                        }
                        self::$current = new Portal_User($row[0], $row[2], $row[1], $user_dn);
                    }
                    break;
                default:
                    REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR);
            }
        }
        return self::$current;
    }
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at <http://www.apache.org/licenses/LICENSE-2.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: databases_versions.php 2459 2009-08-10 21:20:41Z pieterb $
 **************************************************************************/
/**
 * File documentation.
 * @package Portal
 */
require_once 'include/global.php';
REST::require_method('GET', 'HEAD');
$user_id = Portal_User::current()->user_id();
$path_info = Portal::path_info();
$dbname = Portal_MySQL::escape_string($path_info[0]);
$result = Portal_MySQL::query(<<<EOS
SELECT DISTINCT(`version`) FROM `Database`
WHERE `name` = {$dbname}
  AND ( `is_shared` > 0 OR `user_id` = {$user_id} );
EOS
);
$directory = RESTDir::factory("{$path_info['0']}: available versions");
while ($row = $result->fetch_row()) {
    $directory->line($row[0] . '/');
}
$directory->end();
}
REST::require_method('GET', 'HEAD');
$path_info = Portal::path_info();
if (count($path_info) != 3) {
    REST::fatal(REST::HTTP_NOT_FOUND);
}
$file = explode('.', $path_info[2], 2);
if (!($database_id = (int) $file[0])) {
    REST::fatal(REST::HTTP_NOT_FOUND);
}
$user_id = Portal_User::current()->user_id();
$result = Portal_MySQL::query(<<<EOS
SELECT `d`.`name`,
       `d`.`version`,
       `d`.`type`,
       `d`.`checksum`,
       `u`.`user_name`
  FROM `Database` AS d LEFT JOIN `User` AS u USING(`user_id`)
 WHERE `d`.`database_id` = {$database_id}
   AND (`d`.`user_id` = {$user_id} OR `d`.`is_shared` = 1);
EOS
);
if (!($row = $result->fetch_row())) {
    REST::fatal(REST::HTTP_NOT_FOUND);
}
$fileinfo = @stat($realfilepath);
$filename = "{$row[0]}-{$row[1]}." . Portal_DB::databaseTypeExtension($row[2]);
REST::header(array('Content-Type' => Portal_DB::databaseTypeContentType($row[2]), 'Content-Encoding' => 'identity', 'Content-Disposition' => "attachment; filename=\"{$filename}\"", 'Last-Modified' => REST::http_date($fileinfo['mtime']), 'ETag' => "\"{$row[3]}\"", 'X-Creator-Name' => $row[4], 'Content-Length' => $fileinfo['size']));
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    readfile($realfilepath);
}
$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">
{$options}
</select><br />
<input type="submit" value="Upload" />
</form>
EOS
);
$user_id = Portal_User::current()->user_id();
$result = Portal_MySQL::query(<<<EOS
SELECT `user_name`, `database_id`, `type` FROM `Database` LEFT JOIN `User` USING(`user_id`)
WHERE `name` = {$dbname}
  AND `version` = {$dbversion}
  AND ( `is_shared` > 0 OR `Database`.`user_id` = {$user_id})
ORDER BY 3, 1;
EOS
);
while ($row = $result->fetch_array()) {
    $filesize = filesize(Portal_DB::DATABASE_DIR . $row[1]);
    $directory->line($row[1] . '.' . Portal_DB::databaseTypeExtension($row[2]), array('Size' => filesize(Portal_DB::DATABASE_DIR . $row[1]) . ' B', 'DBType' => Portal_DB::databaseTypeName($row[2]), 'Creator' => $row[0], 'Content-Type' => Portal_DB::databaseTypeContentType($row[2])));
}
$directory->end();
    /**
     * Get a list of available databases, given a set of database types.
     * @param $name... string the name(s) of the database types.
     * @return string an x fragment, to be put inside a select element.
     */
    public static function availableDatabases()
    {
        $dbTypes = func_get_args();
        if (empty($dbTypes)) {
            return array();
        }
        foreach ($dbTypes as $key => $value) {
            $dbTypes[$key] = self::databaseTypeIDByName($value);
        }
        $dbTypes = implode(',', $dbTypes);
        $user_id = Portal_User::current()->user_id();
        $result = Portal_MySQL::query(<<<EOS
SELECT `d`.`name`, `d`.`version`, `d`.`type`, `u`.`user_name`, `d`.`database_id`
  FROM `Database` AS d LEFT JOIN `User` AS u USING(`user_id`)
 WHERE (`d`.`is_shared` > 0 OR `d`.`user_id` = {$user_id})
   AND `d`.`type` IN({$dbTypes});
EOS
);
        $sorter = array();
        while ($row = $result->fetch_row()) {
            $extension = self::databaseTypeExtension($row[2]);
            $sorter[$row[3]]["{$row[0]}-{$row[1]}.{$extension}"] = REST::urlencode(Portal::portalURL() . 'databases/' . $row[0] . '/' . $row[1] . '/' . $row[4] . '.' . $extension);
        }
        $user_names = array_keys($sorter);
        natsort($user_names);
        $retval = '';
        foreach ($user_names as $user_name) {
            $retval .= "\n<optgroup label=\"" . htmlentities($user_name) . "\">";
            $dbnames = array_keys($sorter[$user_name]);
            natsort($dbnames);
            foreach ($dbnames as $dbname) {
                $retval .= "\n<option value=\"" . $sorter[$user_name][$dbname] . "\">{$dbname}</option>";
            }
            $retval .= "\n</optgroup>";
        }
        return $retval;
    }