* 
 * 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();
$dbname = Portal_MySQL::escape_string($path_info[0]);
$dbversion = Portal_MySQL::escape_string($path_info[1]);
if (preg_match("/([^\\w\\-.~])/", $path_info[0] . $path_info[1])) {
    REST::fatal(REST::HTTP_FORBIDDEN, <<<EOS
Illegal characters found in database name or version.<br/>
Allowed characters are: A-Z a-z 0-9 _ - . ~
EOS
);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $user_id = Portal_User::current()->user_id();
    if (strpos(@$_SERVER['CONTENT_TYPE'], 'multipart/form-data') === 0) {
        if (empty($_POST['type'])) {
            REST::fatal(REST::HTTP_BAD_REQUEST, "No type specified");
        }
        if (!($typeId = (int) Portal_DB::databaseTypeIDByName($_POST['type']))) {
            REST::fatal(REST::HTTP_BAD_REQUEST, "Wrong type specified");
        }
        if (count($_FILES) == 0) {
            REST::fatal(REST::HTTP_BAD_REQUEST, "No file in multipart/form-data.");
        }
        if (count($_FILES) > 1) {
            REST::fatal(REST::HTTP_BAD_REQUEST, "Multiple files in multipart/form-data.");
        }
        $file = array_shift($_FILES);
        if (is_array($file['error'])) {
            REST::fatal(REST::HTTP_BAD_REQUEST, "Multiple files in multipart/form-data.");
        }
        if ($file['error'] !== UPLOAD_ERR_OK) {
            REST::fatal(REST::HTTP_BAD_REQUEST, htmlentities("Errno {$file['error']} occured during file upload."));
        }