blob streaming code cleanup (work in progress)

This commit is contained in:
Marc Delisle
2010-07-22 13:15:27 -04:00
parent b6349cd626
commit 56ebe05af7
15 changed files with 381 additions and 391 deletions

View File

@@ -6,36 +6,38 @@
function initPBMSDatabase()
{
$query = "create database IF NOT EXISTS pbms;"; // If no other choice then try this.
/*
* The user may not have privileges to create the 'pbms' database
* so if it doesn't exist then we perform a select on a pbms system
* table in an already existing database which will cause the PBMS
* daemon to create the 'pbms' database.
*/
$db_array =PMA_DBI_fetch_result('SHOW DATABASES;');
if (!empty($db_array)) {
$target = "";
foreach ($db_array as $current_db) {
if ($current_db == 'pbms')
return TRUE;
if ($target == "") {
if (($current_db != 'pbxt') && ($current_db != 'mysql') && ($current_db != 'information_schema'))
$target = $current_db;
}
}
$query = "create database IF NOT EXISTS pbms;"; // If no other choice then try this.
/*
* The user may not have privileges to create the 'pbms' database
* so if it doesn't exist then we perform a select on a pbms system
* table in an already existing database which will cause the PBMS
* daemon to create the 'pbms' database.
*/
$db_array = PMA_DBI_fetch_result('SHOW DATABASES;');
if (! empty($db_array)) {
$target = "";
foreach ($db_array as $current_db) {
if ($current_db == 'pbms') {
return TRUE;
}
if ($target == "") {
if (($current_db != 'pbxt') && ($current_db != 'mysql') && ($current_db != 'information_schema')) {
$target = $current_db;
}
}
}
if ($target != "")
$query = "select * from $target.pbms_metadata_header"; // If it exists this table will not contain much
}
if ($target != "") {
$query = "select * from $target.pbms_metadata_header"; // If it exists this table will not contain much
}
}
$result = PMA_DBI_query($query );
if (!$result)
return FALSE;
return TRUE;
}
$result = PMA_DBI_query($query );
if (! $result) {
return FALSE;
}
return TRUE;
}
/**
* checks whether the necessary plugins for BLOBStreaming exist
@@ -95,23 +97,23 @@ function checkBLOBStreamingPlugins()
// Retrieve MySQL plugins
$existing_plugins = PMA_DBI_fetch_result('SHOW PLUGINS');
foreach ($existing_plugins as $one_existing_plugin) {
foreach ($existing_plugins as $one_existing_plugin) {
// check if required plugins exist
if ( strtolower($one_existing_plugin['Library']) == 'libpbms.so'
if ( strtolower($one_existing_plugin['Library']) == 'libpbms.so'
&& $one_existing_plugin['Status'] == "ACTIVE") {
$has_blobstreaming = true;
break;
}
$has_blobstreaming = true;
break;
}
}
unset($existing_plugins, $one_existing_plugin);
unset($existing_plugins, $one_existing_plugin);
}
// set variable indicating BS plugin existence
$PMA_Config->set('BLOBSTREAMING_PLUGINS_EXIST', $has_blobstreaming);
if ($has_blobstreaming) {
$bs_variables = PMA_BS_GetVariables();
$bs_variables = PMA_BS_GetVariables();
// if no BS variables exist, set plugin existence to false and return
if (count($bs_variables) <= 0) {
$PMA_Config->set('BLOBSTREAMING_PLUGINS_EXIST', FALSE);
@@ -136,7 +138,7 @@ function checkBLOBStreamingPlugins()
$PMA_Config->set('PHP_PBMS_EXISTS', FALSE);
$PMA_Config->set('FILEINFO_EXISTS', FALSE);
// check if PECL's fileinfo library exist
// check if PECL's fileinfo library exist
$finfo = NULL;
if (function_exists("finfo_open")) {
@@ -148,7 +150,7 @@ function checkBLOBStreamingPlugins()
$PMA_Config->set('FILEINFO_EXISTS', TRUE);
finfo_close($finfo);
} // end if (!empty($finfo))
} else {
PMA_cacheSet('skip_blobstreaming', true, true);
return FALSE;
@@ -223,8 +225,8 @@ function PMA_BS_GetVariables()
//========================
function PMA_BS_ReportPBMSError($msg)
{
$tmp_err = pbms_error();
PMA_showMessage("PBMS error, $msg $tmp_err");
$tmp_err = pbms_error();
PMA_showMessage("PBMS error, $msg $tmp_err");
}
//------------
@@ -238,20 +240,21 @@ function PMA_do_connect($db_name, $quiet)
// generate bs reference link
$pbms_host = $PMA_Config->get('BLOBSTREAMING_HOST');
$pbms_port = $PMA_Config->get('BLOBSTREAMING_PORT');
$pbms_port = $PMA_Config->get('BLOBSTREAMING_PORT');
if (pbms_connect($pbms_host, $pbms_port, $db_name) == FALSE) {
if ($quiet == FALSE)
PMA_BS_ReportPBMSError("PBMS Connectiuon failed: pbms_connect($pbms_host, $pbms_port, $db_name)");
return FALSE;
}
return TRUE;
if (pbms_connect($pbms_host, $pbms_port, $db_name) == FALSE) {
if ($quiet == FALSE) {
PMA_BS_ReportPBMSError("PBMS Connectiuon failed: pbms_connect($pbms_host, $pbms_port, $db_name)");
}
return FALSE;
}
return TRUE;
}
//------------
function PMA_do_disconnect()
{
pbms_close();
pbms_close();
}
//------------
@@ -261,76 +264,76 @@ function PMA_do_disconnect()
*/
function PMA_BS_IsPBMSReference($bs_reference, $db_name)
{
if (PMA_cacheGet('skip_blobstreaming', true))
return FALSE;
if (PMA_do_connect($db_name, TRUE) == FALSE) {
return FALSE;
}
if (PMA_cacheGet('skip_blobstreaming', true)) {
return FALSE;
}
if (PMA_do_connect($db_name, TRUE) == FALSE) {
return FALSE;
}
$ok = pbms_is_blob_reference($bs_reference);
PMA_do_disconnect();
return $ok ;
PMA_do_disconnect();
return $ok ;
}
//------------
function PMA_BS_CreateReferenceLink($bs_reference, $db_name)
{
if (PMA_do_connect($db_name, FALSE) == FALSE) {
return 'Error';
}
if (pbms_get_info($bs_reference) == FALSE) {
PMA_BS_ReportPBMSError("PBMS get BLOB info failed: pbms_get_info($bs_reference)");
PMA_do_disconnect();
return 'Error';
}
if (PMA_do_connect($db_name, FALSE) == FALSE) {
return 'Error';
}
if (pbms_get_info($bs_reference) == FALSE) {
PMA_BS_ReportPBMSError("PBMS get BLOB info failed: pbms_get_info($bs_reference)");
PMA_do_disconnect();
return 'Error';
}
$content_type = pbms_get_metadata_value("Content-type");
if ($content_type == FALSE) {
PMA_BS_ReportPBMSError("PMA_BS_CreateReferenceLink($bs_reference, $db_name): get BLOB Content-type failed: ");
}
PMA_do_disconnect();
if (!$content_type)
$content_type = "image/jpeg";
$bs_url = PMA_BS_getURL($bs_reference);
if (empty($bs_url)) {
PMA_BS_ReportPBMSError("No blob streaming server configured!");
return 'Error';
PMA_do_disconnect();
if (! $content_type) {
$content_type = "image/jpeg";
}
$bs_url = PMA_BS_getURL($bs_reference);
if (empty($bs_url)) {
PMA_BS_ReportPBMSError("No blob streaming server configured!");
return 'Error';
}
$output = "<a href=\"#\" onclick=\"requestMIMETypeChange('" . urlencode($db_name) . "', '" . urlencode($GLOBALS['table']) . "', '" . urlencode($bs_reference) . "', '" . urlencode($content_type) . "')\">$content_type</a>";
// specify custom HTML for various content types
switch ($content_type)
{
// no content specified
case NULL:
$output = "NULL";
break;
// image content
case 'image/jpeg':
case 'image/png':
$output .= ' (<a href="' . $bs_url . '" target="new">' . __('View image') . '</a>)';
break;
$output = "<a href=\"#\" onclick=\"requestMIMETypeChange('" . urlencode($db_name) . "', '" . urlencode($GLOBALS['table']) . "', '" . urlencode($bs_reference) . "', '" . urlencode($content_type) . "')\">$content_type</a>";
// specify custom HTML for various content types
switch ($content_type) {
// no content specified
case NULL:
$output = "NULL";
break;
// image content
case 'image/jpeg':
case 'image/png':
$output .= ' (<a href="' . $bs_url . '" target="new">' . __('View image') . '</a>)';
break;
// audio content
case 'audio/mpeg':
$output .= ' (<a href="#" onclick="popupBSMedia(\'' . PMA_generate_common_url() . '\',\'' . urlencode($bs_reference) . '\', \'' . urlencode($content_type) . '\',' . ($is_custom_type ? 1 : 0) . ', 640, 120)">' . __('Play audio'). '</a>)';
break;
// video content
case 'application/x-flash-video':
case 'video/mpeg':
$output .= ' (<a href="#" onclick="popupBSMedia(\'' . PMA_generate_common_url() . '\',\'' . urlencode($bs_reference) . '\', \'' . urlencode($content_type) . '\',' . ($is_custom_type ? 1 : 0) . ', 640, 480)">' . __('View video') . '</a>)';
break;
// unsupported content. specify download
default:
$output .= ' (<a href="' . $bs_url . '" target="new">' . __('Download file'). '</a>)';
}
case 'audio/mpeg':
$output .= ' (<a href="#" onclick="popupBSMedia(\'' . PMA_generate_common_url() . '\',\'' . urlencode($bs_reference) . '\', \'' . urlencode($content_type) . '\',' . ($is_custom_type ? 1 : 0) . ', 640, 120)">' . __('Play audio'). '</a>)';
break;
// video content
case 'application/x-flash-video':
case 'video/mpeg':
$output .= ' (<a href="#" onclick="popupBSMedia(\'' . PMA_generate_common_url() . '\',\'' . urlencode($bs_reference) . '\', \'' . urlencode($content_type) . '\',' . ($is_custom_type ? 1 : 0) . ', 640, 480)">' . __('View video') . '</a>)';
break;
// unsupported content. specify download
default:
$output .= ' (<a href="' . $bs_url . '" target="new">' . __('Download file'). '</a>)';
}
//PMA_showMessage("PMA_BS_CreateReferenceLink($bs_reference, $db_name): $output");
return $output;
return $output;
}
//------------
@@ -340,31 +343,36 @@ function PMA_BS_CreateReferenceLink($bs_reference, $db_name)
// they are not currently needed.
function PMA_BS_IsTablePBMSEnabled($db_name, $tbl_name, $tbl_type)
{
if (PMA_cacheGet('skip_blobstreaming', true))
return FALSE;
if ((isset($tbl_type) == FALSE) || (strlen($tbl_type) == 0))
return FALSE;
if (PMA_cacheGet('skip_blobstreaming', true)) {
return FALSE;
}
if ((isset($tbl_type) == FALSE) || (strlen($tbl_type) == 0)) {
return FALSE;
}
// load PMA configuration
$PMA_Config = $GLOBALS['PMA_Config'];
// return if unable to load PMA configuration
if (empty($PMA_Config))
if (empty($PMA_Config)) {
return FALSE;
if (!$PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST') )
}
if (! $PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST')) {
return FALSE;
}
// This information should be cached rather than selecting it each time.
//$query = "SELECT count(*) FROM information_schema.TABLES T, pbms.pbms_enabled E where T.table_schema = ". PMA_backquote($db_name) . " and T.table_name = ". PMA_backquote($tbl_name) . " and T.engine = E.name";
$query = "SELECT count(*) FROM pbms.pbms_enabled E where E.name = '". PMA_sqlAddslashes($tbl_type) . "'" ;
$result = PMA_DBI_query($query);
// This information should be cached rather than selecting it each time.
//$query = "SELECT count(*) FROM information_schema.TABLES T, pbms.pbms_enabled E where T.table_schema = ". PMA_backquote($db_name) . " and T.table_name = ". PMA_backquote($tbl_name) . " and T.engine = E.name";
$query = "SELECT count(*) FROM pbms.pbms_enabled E where E.name = '" . PMA_sqlAddslashes($tbl_type) . "'";
$result = PMA_DBI_query($query);
$data = PMA_DBI_fetch_row($result);
if ($data[0] == 1) {
return TRUE;
}
$data = PMA_DBI_fetch_row($result);
if ($data[0] == 1)
return TRUE;
return FALSE;
}
@@ -372,107 +380,97 @@ function PMA_BS_IsTablePBMSEnabled($db_name, $tbl_name, $tbl_type)
function PMA_BS_UpLoadFile($db_name, $tbl_name, $file_type, $file_name)
{
if (PMA_cacheGet('skip_blobstreaming', true))
return FALSE;
if (PMA_cacheGet('skip_blobstreaming', true)) {
return FALSE;
}
if (PMA_do_connect($db_name, FALSE) == FALSE) {
return FALSE;
}
$fh = fopen($file_name, 'r');
if (!$fh) {
PMA_do_disconnect();
PMA_showMessage("Could not open file: $file_name");
return FALSE;
}
pbms_add_metadata("Content-type", $file_type);
$pbms_blob_url = pbms_read_stream($fh, filesize($file_name), $tbl_name);
if (!$pbms_blob_url) {
PMA_BS_ReportPBMSError("pbms_read_stream() Failed");
if (PMA_do_connect($db_name, FALSE) == FALSE) {
return FALSE;
}
$fh = fopen($file_name, 'r');
if (! $fh) {
PMA_do_disconnect();
PMA_showMessage("Could not open file: $file_name");
return FALSE;
}
//PMA_showMessage(" PMA_BS_UpLoadFile($db_name, $tbl_name, $file_type, $file_name): $pbms_blob_url");
fclose($fh);
PMA_do_disconnect();
return $pbms_blob_url;
pbms_add_metadata("Content-type", $file_type);
$pbms_blob_url = pbms_read_stream($fh, filesize($file_name), $tbl_name);
if (! $pbms_blob_url) {
PMA_BS_ReportPBMSError("pbms_read_stream() Failed");
}
fclose($fh);
PMA_do_disconnect();
return $pbms_blob_url;
}
//------------
function PMA_BS_SetContentType($db_name, $bsTable, $blobReference, $contentType)
{
if (PMA_cacheGet('skip_blobstreaming', true))
return FALSE;
if (PMA_cacheGet('skip_blobstreaming', true)) {
return FALSE;
}
// This is a really ugly way to do this but currently there is nothing better.
// In a future version of PBMS the system tables will be redesigned to make this
// more eficient.
$query = "SELECT Repository_id, Repo_blob_offset FROM pbms_reference WHERE Blob_url='" . PMA_sqlAddslashes($blobReference) . "'";
//error_log(" PMA_BS_SetContentType: $query\n", 3, "/tmp/mylog");
$result = PMA_DBI_query($query);
//error_log(" $query\n", 3, "/tmp/mylog");
// This is a really ugly way to do this but currently there is nothing better.
// In a future version of PBMS the system tables will be redesigned to make this
// more eficient.
$query = "SELECT Repository_id, Repo_blob_offset FROM pbms_reference WHERE Blob_url='" . PMA_sqlAddslashes($blobReference) . "'";
//error_log(" PMA_BS_SetContentType: $query\n", 3, "/tmp/mylog");
$result = PMA_DBI_query($query);
//error_log(" $query\n", 3, "/tmp/mylog");
// if record exists
if ($data = PMA_DBI_fetch_assoc($result))
{
$where = "WHERE Repository_id=" . $data['Repository_id'] . " AND Repo_blob_offset=" . $data['Repo_blob_offset'] ;
$query = "SELECT name from pbms_metadata $where";
$result = PMA_DBI_query($query);
// if record exists
if ($data = PMA_DBI_fetch_assoc($result)) {
$where = "WHERE Repository_id=" . $data['Repository_id'] . " AND Repo_blob_offset=" . $data['Repo_blob_offset'] ;
$query = "SELECT name from pbms_metadata $where";
$result = PMA_DBI_query($query);
if (PMA_DBI_num_rows($result) == 0)
$query = "INSERT into pbms_metadata Values( ". $data['Repository_id'] . ", " . $data['Repo_blob_offset'] . ", 'Content_type', '" . PMA_sqlAddslashes($contentType) . "')";
else
$query = "UPDATE pbms_metadata SET name = 'Content_type', Value = '" . PMA_sqlAddslashes($contentType) . "' $where";
if (PMA_DBI_num_rows($result) == 0) {
$query = "INSERT into pbms_metadata Values( ". $data['Repository_id'] . ", " . $data['Repo_blob_offset'] . ", 'Content_type', '" . PMA_sqlAddslashes($contentType) . "')";
} else {
$query = "UPDATE pbms_metadata SET name = 'Content_type', Value = '" . PMA_sqlAddslashes($contentType) . "' $where";
}
//error_log("$query\n", 3, "/tmp/mylog");
PMA_DBI_query($query);
} else {
// if ($result == FALSE) {
// $err = PMA_DBI_getError();
// error_log("MySQL ERROR: $err\n", 3, "/tmp/mylog");
// } else
// error_log("No results: $query\n", 3, "/tmp/mylog");
return FALSE;
}
return TRUE;
PMA_DBI_query($query);
} else {
return FALSE;
}
return TRUE;
}
//------------
function PMA_BS_IsHiddenTable($table)
{
switch ($table) {
case 'pbms_repository' :
case 'pbms_reference' :
case 'pbms_metadata' :
case 'pbms_metadata_header' :
case 'pbms_dump' :
return TRUE;
}
return FALSE;
switch ($table) {
case 'pbms_repository' :
case 'pbms_reference' :
case 'pbms_metadata' :
case 'pbms_metadata_header' :
case 'pbms_dump' :
return TRUE;
}
return FALSE;
}
//------------
function PMA_BS_getURL($reference)
{
// load PMA configuration
$PMA_Config = $GLOBALS['PMA_Config'];
if (empty($PMA_Config))
return FALSE;
// load PMA configuration
$PMA_Config = $GLOBALS['PMA_Config'];
if (empty($PMA_Config)) {
return FALSE;
}
// retrieve BS server variables from PMA configuration
$bs_server = $PMA_Config->get('BLOBSTREAMING_SERVER');
if (empty($bs_server))
return FALSE;
// retrieve BS server variables from PMA configuration
$bs_server = $PMA_Config->get('BLOBSTREAMING_SERVER');
if (empty($bs_server)) {
return FALSE;
}
$bs_url = 'http://' . $bs_server . '/' . rtrim($reference);
//PMA_showMessage(" PMA_BS_getURL($reference): $bs_url");
return $bs_url;
$bs_url = 'http://' . $bs_server . '/' . rtrim($reference);
return $bs_url;
}
?>