}
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);
}
 * 
 * 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: database_types.php 2459 2009-08-10 21:20:41Z pieterb $
 **************************************************************************/
/**
 * File documentation.
 * @todo Implement retrieval of info about individual database types.
 * @package Portal
 */
require_once 'include/global.php';
REST::require_method('GET', 'HEAD');
$path_info = Portal::path_info();
if (!empty($path_info[0])) {
    $id = Portal_DB::databaseTypeIDByName($path_info[0]);
    if (empty($id)) {
        REST::fatal(REST::HTTP_NOT_FOUND);
    }
    REST::header(REST::best_xhtml_type() . '; charset="UTF-8"');
    echo REST::html_start("Database type \"{$path_info[0]}\"") . Portal_DB::databaseTypeDescription($id) . REST::html_end();
    exit;
}
$directory = RESTDir::factory();
foreach (Portal_DB::databaseTypeIDs() as $id) {
    $directory->line(Portal_DB::databaseTypeName($id), array('Content-Type' => Portal_DB::databaseTypeContentType($id), 'Extension' => Portal_DB::databaseTypeExtension($id)));
}
$directory->end();
$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();