Refresh .po files
[phpmyadmin.git] / libraries / blobstreaming.lib.php
blob8d416d862649b59bc0d5ec42b633d18b85e6a175
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * @package BLOBStreaming
5 */
7 /**
8 * Initializes PBMS database
10 * @return bool
12 function initPBMSDatabase()
14 // If no other choice then try this.
15 $query = "create database IF NOT EXISTS pbms;";
17 * The user may not have privileges to create the 'pbms' database
18 * so if it doesn't exist then we perform a select on a pbms system
19 * table in an already existing database which will cause the PBMS
20 * daemon to create the 'pbms' database.
22 $db_array = PMA_DBI_fetch_result('SHOW DATABASES;');
23 if (! empty($db_array)) {
24 $target = "";
25 foreach ($db_array as $current_db) {
26 if ($current_db == 'pbms') {
27 return true;
29 if ($target == "") {
30 if ($current_db != 'pbxt'
31 && ! PMA_is_system_schema($current_db, true)
32 ) {
33 $target = $current_db;
38 if ($target != "") {
39 // If it exists this table will not contain much
40 $query = "select * from $target.pbms_metadata_header";
44 $result = PMA_DBI_query($query);
45 if (! $result) {
46 return false;
48 return true;
51 /**
52 * checks whether the necessary plugins for BLOBStreaming exist
54 * @access public
55 * @return boolean
57 function checkBLOBStreamingPlugins()
59 if (PMA_cacheGet('skip_blobstreaming', true) === true) {
60 return false;
63 // load PMA configuration
64 $PMA_Config = $GLOBALS['PMA_Config'];
66 // return if unable to load PMA configuration
67 if (empty($PMA_Config)) {
68 return false;
71 // If we don't know that we can skip blobstreaming, we continue
72 // verifications; anyway, in case we won't skip blobstreaming,
73 // we still need to set some variables in non-persistent settings,
74 // which is done via $PMA_Config->set().
76 /** Retrieve current server configuration;
77 * at this point, $PMA_Config->get('Servers') contains the server parameters
78 * as explicitely defined in config.inc.php, so it cannot be used; it's
79 * better to use $GLOBALS['cfg']['Server'] which contains the explicit
80 * parameters merged with the default ones
83 $serverCfg = $GLOBALS['cfg']['Server'];
85 // return if unable to retrieve current server configuration
86 if (! $serverCfg) {
87 return false;
90 // if PHP extension in use is 'mysql', specify element 'PersistentConnections'
91 if ($serverCfg['extension'] == "mysql") {
92 $serverCfg['PersistentConnections'] = $PMA_Config->settings['PersistentConnections'];
95 // if connection type is TCP, unload socket variable
96 if (strtolower($serverCfg['connect_type']) == "tcp") {
97 $serverCfg['socket'] = "";
100 $has_blobstreaming = PMA_cacheGet('has_blobstreaming', true);
102 if ($has_blobstreaming === null) {
103 if (! PMA_DRIZZLE && PMA_MYSQL_INT_VERSION >= 50109) {
105 // Retrieve MySQL plugins
106 $existing_plugins = PMA_DBI_fetch_result('SHOW PLUGINS');
108 foreach ($existing_plugins as $one_existing_plugin) {
109 // check if required plugins exist
110 if ( strtolower($one_existing_plugin['Library']) == 'libpbms.so'
111 && $one_existing_plugin['Status'] == "ACTIVE"
113 $has_blobstreaming = true;
114 break;
117 unset($existing_plugins, $one_existing_plugin);
118 } else if (PMA_DRIZZLE) {
119 $has_blobstreaming = (bool) PMA_DBI_fetch_result(
120 "SELECT 1
121 FROM data_dictionary.plugins
122 WHERE module_name = 'PBMS'
123 AND is_active = true
124 LIMIT 1"
127 PMA_cacheSet('has_blobstreaming', $has_blobstreaming, true);
130 // set variable indicating BS plugin existence
131 $PMA_Config->set('BLOBSTREAMING_PLUGINS_EXIST', $has_blobstreaming);
133 if (! $has_blobstreaming) {
134 PMA_cacheSet('skip_blobstreaming', true, true);
135 return false;
138 if ($has_blobstreaming) {
139 $bs_variables = PMA_BS_GetVariables();
141 // if no BS variables exist, set plugin existence to false and return
142 if (count($bs_variables) == 0) {
143 $PMA_Config->set('BLOBSTREAMING_PLUGINS_EXIST', false);
144 PMA_cacheSet('skip_blobstreaming', true, true);
145 PMA_cacheSet('has_blobstreaming', false, true);
146 return false;
147 } // end if (count($bs_variables) <= 0)
149 // Check that the required pbms functions exist:
150 if (function_exists("pbms_connect") == false
151 || function_exists("pbms_error") == false
152 || function_exists("pbms_close") == false
153 || function_exists("pbms_is_blob_reference") == false
154 || function_exists("pbms_get_info") == false
155 || function_exists("pbms_get_metadata_value") == false
156 || function_exists("pbms_add_metadata") == false
157 || function_exists("pbms_read_stream") == false
160 // We should probably notify the user that they need to install
161 // the pbms client lib and PHP extension to make use of blob streaming.
162 $PMA_Config->set('BLOBSTREAMING_PLUGINS_EXIST', false);
163 PMA_cacheSet('skip_blobstreaming', true, true);
164 PMA_cacheSet('has_blobstreaming', false, true);
165 return false;
168 if (function_exists("pbms_connection_pool_size")) {
169 if ( isset($PMA_Config->settings['pbms_connection_pool_size'])) {
170 $pool_size = $PMA_Config->settings['pbms_connection_pool_size'];
171 if ($pool_size == "") {
172 $pool_size = 1;
174 } else {
175 $pool_size = 1;
177 pbms_connection_pool_size($pool_size);
180 // get BS server port
181 $BS_PORT = $bs_variables['pbms_port'];
183 // if no BS server port or 'pbms' database exists,
184 // set plugin existance to false and return
185 if ((! $BS_PORT) || (! initPBMSDatabase())) {
186 $PMA_Config->set('BLOBSTREAMING_PLUGINS_EXIST', false);
187 PMA_cacheSet('skip_blobstreaming', true, true);
188 return false;
189 } // end if (!$BS_PORT)
191 // Ping PBMS: the database doesn't need to exist for this to work.
192 if (pbms_connect($serverCfg['host'], $BS_PORT, "anydb") == false) {
193 $PMA_Config->set('BLOBSTREAMING_PLUGINS_EXIST', false);
194 PMA_cacheSet('skip_blobstreaming', true, true);
195 return false;
197 pbms_close();
199 if (function_exists("pbms_pconnect")) {
200 $PMA_Config->set('PBMS_PCONNECT_EXISTS', true);
201 } else {
202 $PMA_Config->set('PBMS_PCONNECT_EXISTS', false);
205 // add selected BS, CURL and fileinfo library variables to PMA configuration
206 $PMA_Config->set('BLOBSTREAMING_PORT', $BS_PORT);
207 $PMA_Config->set('BLOBSTREAMING_HOST', $serverCfg['host']);
208 $PMA_Config->set('BLOBSTREAMING_SERVER', $serverCfg['host'] . ':' . $BS_PORT);
209 $PMA_Config->set('PHP_PBMS_EXISTS', false);
210 $PMA_Config->set('FILEINFO_EXISTS', false);
212 // check if PECL's fileinfo library exist
213 $finfo = null;
215 if (function_exists("finfo_open")) {
216 $finfo = finfo_open(FILEINFO_MIME);
219 // fileinfo library exists, set necessary variable and close resource
220 if (! empty($finfo)) {
221 $PMA_Config->set('FILEINFO_EXISTS', true);
222 finfo_close($finfo);
223 } // end if (!empty($finfo))
225 } else {
226 PMA_cacheSet('skip_blobstreaming', true, true);
227 return false;
228 } // end if ($has_blobstreaming)
230 return true;
234 * returns a list of BLOBStreaming variables used by MySQL
236 * @access public
237 * @return array - list of BLOBStreaming variables
239 function PMA_BS_GetVariables()
241 // load PMA configuration
242 $PMA_Config = $GLOBALS['PMA_Config'];
244 // return if unable to load PMA configuration
245 if (empty($PMA_Config)) {
246 return null;
248 // run query to retrieve BS variables
249 $query = "SHOW VARIABLES LIKE '%pbms%'";
250 $result = PMA_DBI_query($query);
252 $BS_Variables = array();
254 // while there are records to retrieve
255 while ($data = @PMA_DBI_fetch_assoc($result)) {
256 $BS_Variables[$data['Variable_name']] = $data['Value'];
258 // return BS variables
259 return $BS_Variables;
263 * Retrieves and shows PBMS error.
265 * @param sting $msg error message
267 * @return nothing
269 function PMA_BS_ReportPBMSError($msg)
271 $tmp_err = pbms_error();
272 PMA_showMessage(__('PBMS error') . " $msg $tmp_err");
276 * Tries to connect to PBMS server.
278 * @param string $db_name Database name
279 * @param bool $quiet Whether to report errors
281 * @return bool Connection status.
283 function PMA_do_connect($db_name, $quiet)
285 $PMA_Config = $GLOBALS['PMA_Config'];
287 // return if unable to load PMA configuration
288 if (empty($PMA_Config)) {
289 return false;
292 // generate bs reference link
293 $pbms_host = $PMA_Config->get('BLOBSTREAMING_HOST');
294 $pbms_port = $PMA_Config->get('BLOBSTREAMING_PORT');
296 if ($PMA_Config->get('PBMS_PCONNECT_EXISTS')) {
297 // Open a persistent connection.
298 $ok = pbms_pconnect($pbms_host, $pbms_port, $db_name);
299 } else {
300 $ok = pbms_connect($pbms_host, $pbms_port, $db_name);
303 if ($ok == false) {
304 if ($quiet == false) {
305 PMA_BS_ReportPBMSError(
306 __('PBMS connection failed:')
307 . " pbms_connect($pbms_host, $pbms_port, $db_name)"
310 return false;
312 return true;
316 * Disconnects from PBMS server.
318 * @return nothing
320 function PMA_do_disconnect()
322 pbms_close();
326 * Checks whether the BLOB reference looks valid
328 * @param string $bs_reference BLOB reference
329 * @param string $db_name Database name
331 * @return bool True on success.
333 function PMA_BS_IsPBMSReference($bs_reference, $db_name)
335 if (PMA_cacheGet('skip_blobstreaming', true)) {
336 return false;
339 // You do not really need a connection to the PBMS Daemon
340 // to check if a reference looks valid but unfortunalty the API
341 // requires one at this point so until the API is updated
342 // we need to epen one here. If you use pool connections this
343 // will not be a performance problem.
344 if (PMA_do_connect($db_name, false) == false) {
345 return false;
348 $ok = pbms_is_blob_reference($bs_reference);
349 return $ok ;
352 //------------
353 function PMA_BS_CreateReferenceLink($bs_reference, $db_name)
355 if (PMA_do_connect($db_name, false) == false) {
356 return __('Error');
359 if (pbms_get_info(trim($bs_reference)) == false) {
360 PMA_BS_ReportPBMSError(
361 __('PBMS get BLOB info failed:')
362 . " pbms_get_info($bs_reference)"
364 PMA_do_disconnect();
365 return __('Error');
368 $content_type = pbms_get_metadata_value("Content-Type");
369 if ($content_type == false) {
370 $br = trim($bs_reference);
371 PMA_BS_ReportPBMSError(
372 "PMA_BS_CreateReferenceLink('$br', '$db_name'): "
373 . __('PBMS get BLOB Content-Type failed')
377 PMA_do_disconnect();
379 if (! $content_type) {
380 $content_type = "image/jpeg";
383 $bs_url = PMA_BS_getURL($bs_reference);
384 if (empty($bs_url)) {
385 PMA_BS_ReportPBMSError(__('No blob streaming server configured!'));
386 return 'Error';
389 $output = $content_type;
391 // specify custom HTML for various content types
392 switch ($content_type) {
393 // no content specified
394 case null:
395 $output = "NULL";
396 break;
397 // image content
398 case 'image/jpeg':
399 case 'image/png':
400 $output .= ' (<a href="' . $bs_url . '" target="new">'
401 . __('View image') . '</a>)';
402 break;
403 // audio content
404 case 'audio/mpeg':
405 $output .= ' (<a href="#" onclick="popupBSMedia(\''
406 . PMA_generate_common_url() . '\',\'' . urlencode($bs_reference)
407 . '\', \'' . urlencode($content_type) . '\','
408 . ($is_custom_type ? 1 : 0) . ', 640, 120)">' . __('Play audio')
409 . '</a>)';
410 break;
411 // video content
412 case 'application/x-flash-video':
413 case 'video/mpeg':
414 $output .= ' (<a href="#" onclick="popupBSMedia(\''
415 . PMA_generate_common_url() . '\',\'' . urlencode($bs_reference)
416 . '\', \'' . urlencode($content_type) . '\','
417 . ($is_custom_type ? 1 : 0) . ', 640, 480)">' . __('View video')
418 . '</a>)';
419 break;
420 // unsupported content. specify download
421 default:
422 $output .= ' (<a href="' . $bs_url . '" target="new">'
423 . __('Download file') . '</a>)';
426 return $output;
430 * In the future there may be server variables to turn on/off PBMS
431 * BLOB streaming on a per table or database basis. So in anticipation of this
432 * PMA_BS_IsTablePBMSEnabled() passes in the table and database name even though
433 * they are not currently needed.
435 * @param string $db_name database name
436 * @param string $tbl_name table name
437 * @param string $tbl_type table type
439 * @return bool
441 function PMA_BS_IsTablePBMSEnabled($db_name, $tbl_name, $tbl_type)
443 if (PMA_cacheGet('skip_blobstreaming', true)) {
444 return false;
447 if ((isset($tbl_type) == false) || (strlen($tbl_type) == 0)) {
448 return false;
451 // load PMA configuration
452 $PMA_Config = $GLOBALS['PMA_Config'];
454 // return if unable to load PMA configuration
455 if (empty($PMA_Config)) {
456 return false;
459 if (! $PMA_Config->get('BLOBSTREAMING_PLUGINS_EXIST')) {
460 return false;
463 // This information should be cached rather than selecting it each time.
464 // $query = "SELECT count(*) FROM information_schema.TABLES T,
465 // pbms.pbms_enabled E where T.table_schema = ". PMA_backquote($db_name) . "
466 // and T.table_name = ". PMA_backquote($tbl_name) . " and T.engine = E.name";
467 $query = "SELECT count(*) FROM pbms.pbms_enabled E where E.name = '"
468 . PMA_sqlAddSlashes($tbl_type) . "'";
469 $result = PMA_DBI_query($query);
471 $data = PMA_DBI_fetch_row($result);
472 if ($data[0] == 1) {
473 return true;
476 return false;
479 //------------
480 function PMA_BS_UpLoadFile($db_name, $tbl_name, $file_type, $file_name)
483 if (PMA_cacheGet('skip_blobstreaming', true)) {
484 return false;
487 if (PMA_do_connect($db_name, false) == false) {
488 return false;
491 $fh = fopen($file_name, 'r');
492 if (! $fh) {
493 PMA_do_disconnect();
494 PMA_showMessage(sprintf(__('Could not open file: %s'), $file_name));
495 return false;
498 pbms_add_metadata("Content-Type", $file_type);
500 $pbms_blob_url = pbms_read_stream($fh, filesize($file_name), $tbl_name);
501 if (! $pbms_blob_url) {
502 PMA_BS_ReportPBMSError("pbms_read_stream()");
505 fclose($fh);
506 PMA_do_disconnect();
507 return $pbms_blob_url;
510 //------------
511 function PMA_BS_SetContentType($db_name, $bsTable, $blobReference, $contentType)
513 if (PMA_cacheGet('skip_blobstreaming', true)) {
514 return false;
517 // This is a really ugly way to do this but currently there is nothing better.
518 // In a future version of PBMS the system tables will be redesigned to make this
519 // more efficient.
520 $query = "SELECT Repository_id, Repo_blob_offset FROM pbms_reference"
521 . " WHERE Blob_url='" . PMA_sqlAddSlashes($blobReference) . "'";
522 //error_log(" PMA_BS_SetContentType: $query\n", 3, "/tmp/mylog");
523 $result = PMA_DBI_query($query);
524 //error_log(" $query\n", 3, "/tmp/mylog");
526 // if record exists
527 if ($data = PMA_DBI_fetch_assoc($result)) {
528 $where = "WHERE Repository_id=" . $data['Repository_id']
529 . " AND Repo_blob_offset=" . $data['Repo_blob_offset'] ;
530 $query = "SELECT name from pbms_metadata $where";
531 $result = PMA_DBI_query($query);
533 if (PMA_DBI_num_rows($result) == 0) {
534 $query = "INSERT into pbms_metadata Values( ". $data['Repository_id']
535 . ", " . $data['Repo_blob_offset'] . ", 'Content_type', '"
536 . PMA_sqlAddSlashes($contentType) . "')";
537 } else {
538 $query = "UPDATE pbms_metadata SET name = 'Content_type', Value = '"
539 . PMA_sqlAddSlashes($contentType) . "' $where";
541 //error_log("$query\n", 3, "/tmp/mylog");
542 PMA_DBI_query($query);
543 } else {
544 return false;
546 return true;
549 //------------
550 function PMA_BS_IsHiddenTable($table)
552 if ($table === 'pbms_repository'
553 || $table === 'pbms_reference'
554 || $table === 'pbms_metadata'
555 || $table === 'pbms_metadata_header'
556 || $table === 'pbms_dump'
558 return true;
560 return false;
563 //------------
564 function PMA_BS_getURL($reference)
566 // load PMA configuration
567 $PMA_Config = $GLOBALS['PMA_Config'];
568 if (empty($PMA_Config)) {
569 return false;
572 // retrieve BS server variables from PMA configuration
573 $bs_server = $PMA_Config->get('BLOBSTREAMING_SERVER');
574 if (empty($bs_server)) {
575 return false;
578 $bs_url = PMA_linkURL('http://' . $bs_server . '/' . rtrim($reference));
579 return $bs_url;