From 0445b1fb51996248fb5a5c77d04ca6e918fa255a Mon Sep 17 00:00:00 2001 From: Chanaka Indrajith Date: Thu, 14 Jun 2012 23:59:11 +0530 Subject: [PATCH] Some improvements to common.lib.php --- libraries/common.lib.php | 401 ++++++++++++++++++++++++++++----------- 1 file changed, 289 insertions(+), 112 deletions(-) diff --git a/libraries/common.lib.php b/libraries/common.lib.php index ffe3c18277..964e47fb38 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -25,6 +25,7 @@ function PMA_detectPow() } } + /** * Exponential expression / raise number into power * @@ -38,7 +39,7 @@ function PMA_pow($base, $exp, $use_function = false) { static $pow_function = null; - if (null == $pow_function) { + if ($pow_function == null) { $pow_function = PMA_detectPow(); } @@ -46,9 +47,10 @@ function PMA_pow($base, $exp, $use_function = false) $use_function = $pow_function; } - if ($exp < 0 && 'pow' != $use_function) { + if (($exp < 0) && ($use_function != 'pow')) { return false; } + switch ($use_function) { case 'bcpow' : // bcscale() needed for testing PMA_pow() with base values < 1 @@ -70,6 +72,7 @@ function PMA_pow($base, $exp, $use_function = false) return $pow; } + /** * Returns an HTML IMG tag for a particular icon from a theme, * which may be an actual file or an icon from a sprite. @@ -88,7 +91,7 @@ function PMA_getIcon($icon, $alternate = '', $force_text = false) $include_icon = ($GLOBALS['cfg']['PropertiesIconic'] !== false); // $cfg['PropertiesIconic'] is false or both // OR we have no $include_icon - $include_text = ($force_text || true !== $GLOBALS['cfg']['PropertiesIconic']); + $include_text = ($force_text || ($GLOBALS['cfg']['PropertiesIconic'] !== true)); // Always use a span (we rely on this in js/sql.js) $button = ''; @@ -106,6 +109,7 @@ function PMA_getIcon($icon, $alternate = '', $force_text = false) return $button; } + /** * Returns an HTML IMG tag for a particular image from a theme, * which may be an actual file or an icon from a sprite @@ -135,6 +139,7 @@ function PMA_getImage($image, $alternate = '', $attributes = array()) $sprites = array(); } } + // Check if we have the requested image as a sprite // and set $url accordingly $class = str_replace(array('.gif','.png'), '', $image); @@ -144,6 +149,7 @@ function PMA_getImage($image, $alternate = '', $attributes = array()) } else { $url = $GLOBALS['pmaThemeImage'] . $image; } + // set class attribute if ($is_sprite) { if (isset($attributes['class'])) { @@ -152,6 +158,7 @@ function PMA_getImage($image, $alternate = '', $attributes = array()) $attributes['class'] = "icon ic_$class"; } } + // set all other attributes $attr_str = ''; foreach ($attributes as $key => $value) { @@ -159,18 +166,21 @@ function PMA_getImage($image, $alternate = '', $attributes = array()) $attr_str .= " $key=\"$value\""; } } + // override the alt attribute if (isset($attributes['alt'])) { $alt = $attributes['alt']; } else { $alt = $alternate; } + // override the title attribute if (isset($attributes['title'])) { $title = $attributes['title']; } else { $title = $alternate; } + // generate the IMG tag $template = '%s'; $retval = sprintf($template, $url, $title, $alt, $attr_str); @@ -178,6 +188,7 @@ function PMA_getImage($image, $alternate = '', $attributes = array()) return $retval; } + /** * Returns the formatted maximum size for an upload * @@ -195,6 +206,7 @@ function PMA_getFormattedMaximumUploadSize($max_upload_size) return '(' . sprintf(__('Max: %s%s'), $max_size, $max_unit) . ')'; } + /** * Generates a hidden field which should indicate to the browser * the maximum size for upload @@ -210,6 +222,7 @@ function PMA_generateHiddenMaxFileSize($max_size) return ''; } + /** * Add slashes before "'" and "\" characters so a value containing them can * be used in a sql comparison. @@ -226,9 +239,10 @@ function PMA_generateHiddenMaxFileSize($max_size) * * @access public */ -function PMA_sqlAddSlashes($a_string = '', $is_like = false, $crlf = false, - $php_code = false +function PMA_sqlAddSlashes( + $a_string = '', $is_like = false, $crlf = false, $php_code = false ) { + if ($is_like) { $a_string = str_replace('\\', '\\\\\\\\', $a_string); } else { @@ -268,6 +282,7 @@ function PMA_escapeMysqlWildcards($name) return strtr($name, array('_' => '\\_', '%' => '\\%')); } // end of the 'PMA_escapeMysqlWildcards()' function + /** * removes slashes before "_" and "%" characters * Note: This function does not unescape backslashes! @@ -283,6 +298,7 @@ function PMA_unescapeMysqlWildcards($name) return strtr($name, array('\\_' => '_', '\\%' => '%')); } // end of the 'PMA_unescapeMysqlWildcards()' function + /** * removes quotes (',",`) from a quoted string * @@ -297,7 +313,7 @@ function PMA_unQuote($quoted_string, $quote = null) { $quotes = array(); - if (null === $quote) { + if ($quote === null) { $quotes[] = '`'; $quotes[] = '"'; $quotes[] = "'"; @@ -323,6 +339,7 @@ function PMA_unQuote($quoted_string, $quote = null) return $quoted_string; } + /** * format sql strings * @@ -352,7 +369,7 @@ function PMA_formatSql($parsed_sql, $unparsed_sql = '') // We don't so just return the input directly // This is intended to be used for when the SQL Parser is turned off $formatted_sql = "
\n";
-        if ($cfg['SQP']['fmtType'] == 'none' && $unparsed_sql != '') {
+        if (($cfg['SQP']['fmtType'] == 'none') && ($unparsed_sql != '')) {
             $formatted_sql .= $unparsed_sql;
         } else {
             $formatted_sql .= $parsed_sql;
@@ -404,6 +421,7 @@ function PMA_showDocLink($link, $target = 'documentation')
         . '';
 } // end of the 'PMA_showDocLink()' function
 
+
 /**
  * Displays a link to the official MySQL documentation
  *
@@ -417,12 +435,13 @@ function PMA_showDocLink($link, $target = 'documentation')
  *
  * @access  public
  */
-function PMA_showMySQLDocu($chapter, $link, $big_icon = false, $anchor = '',
-    $just_open = false
+function PMA_showMySQLDocu(
+    $chapter, $link, $big_icon = false, $anchor = '', $just_open = false
 ) {
+    
     global $cfg;
 
-    if ($cfg['MySQLManualType'] == 'none' || empty($cfg['MySQLManualBase'])) {
+    if (($cfg['MySQLManualType'] == 'none') || empty($cfg['MySQLManualBase'])) {
         return '';
     }
 
@@ -494,6 +513,7 @@ function PMA_showMySQLDocu($chapter, $link, $big_icon = false, $anchor = '',
     } else {
         return PMA_showDocLink(PMA_linkURL($url), 'mysql_doc');
     }
+    
 } // end of the 'PMA_showMySQLDocu()' function
 
 
@@ -511,6 +531,7 @@ function PMA_showDocu($anchor)
     return PMA_showDocLink('Documentation.html#' . $anchor);
 } // end of the 'PMA_showDocu()' function
 
+
 /**
  * Displays a link to the PHP documentation
  *
@@ -527,6 +548,7 @@ function PMA_showPHPDocu($target)
     return PMA_showDocLink($url);
 } // end of the 'PMA_showPHPDocu()' function
 
+
 /**
  * Returns HTML code for a tooltip
  *
@@ -538,15 +560,13 @@ function PMA_showPHPDocu($target)
  */
 function PMA_showHint($message)
 {
-    $retval  = '';
-    $retval .= PMA_getImage('b_help.png');
-    $retval .= '';
-    $retval .= $message;
-    $retval .= '';
-    $retval .= '';
-    return $retval;
+    return ''
+        . PMA_getImage('b_help.png')
+        . '' . $message . ''
+        . '';
 }
 
+
 /**
  * Displays a MySQL error message in the right frame.
  *
@@ -567,6 +587,7 @@ function PMA_mysqlDie(
     $error_message = '', $the_query = '',
     $is_modify_link = true, $back_url = '', $exit = true
 ) {
+    
     global $table, $db;
 
     $error_msg = '';
@@ -581,7 +602,7 @@ function PMA_mysqlDie(
     // --- Added to solve bug #641765
     if (! function_exists('PMA_SQP_isError') || PMA_SQP_isError()) {
         $formatted_sql = htmlspecialchars($the_query);
-    } elseif (empty($the_query) || trim($the_query) == '') {
+    } elseif (empty($the_query) || (trim($the_query) == '')) {
         $formatted_sql = '';
     } else {
         if (strlen($the_query) > $GLOBALS['cfg']['MaxCharactersInDisplayedSQL']) {
@@ -708,6 +729,7 @@ function PMA_mysqlDie(
     }
 } // end of the 'PMA_mysqlDie()' function
 
+
 /**
  * returns array with tables of given db with extended information and grouped
  *
@@ -723,7 +745,7 @@ function PMA_getTableList($db, $tables = null, $limit_offset = 0,
 ) {
     $sep = $GLOBALS['cfg']['LeftFrameTableSeparator'];
 
-    if (null === $tables) {
+    if ($tables === null) {
         $tables = PMA_DBI_get_tables_full(
             $db, false, false, null, $limit_offset, $limit_count
         );
@@ -747,7 +769,7 @@ function PMA_getTableList($db, $tables = null, $limit_offset = 0,
 
     foreach ($tables as $table_name => $table) {
         // check for correct row count
-        if (null === $table['Rows']) {
+        if ($table['Rows'] === null) {
             // Do not check exact row count here,
             // if row count is invalid possibly the table is defect
             // and this would break left frame;
@@ -774,24 +796,30 @@ function PMA_getTableList($db, $tables = null, $limit_offset = 0,
         if ($GLOBALS['cfg']['LeftFrameDBTree']
             && $sep && strstr($table_name, $sep)
         ) {
+            
             $parts = explode($sep, $table_name);
 
             $group =& $table_groups;
             $i = 0;
             $group_name_full = '';
             $parts_cnt = count($parts) - 1;
-            while ($i < $parts_cnt
-                && $i < $GLOBALS['cfg']['LeftFrameTableLevel']
+            
+            while (($i < $parts_cnt)
+                && ($i < $GLOBALS['cfg']['LeftFrameTableLevel'])
             ) {
+                
                 $group_name = $parts[$i] . $sep;
                 $group_name_full .= $group_name;
 
                 if (! isset($group[$group_name])) {
+                    
                     $group[$group_name] = array();
                     $group[$group_name]['is' . $sep . 'group'] = true;
                     $group[$group_name]['tab' . $sep . 'count'] = 1;
                     $group[$group_name]['tab' . $sep . 'group'] = $group_name_full;
+                    
                 } elseif (! isset($group[$group_name]['is' . $sep . 'group'])) {
+                    
                     $table = $group[$group_name];
                     $group[$group_name] = array();
                     $group[$group_name][$group_name] = $table;
@@ -799,12 +827,15 @@ function PMA_getTableList($db, $tables = null, $limit_offset = 0,
                     $group[$group_name]['is' . $sep . 'group'] = true;
                     $group[$group_name]['tab' . $sep . 'count'] = 1;
                     $group[$group_name]['tab' . $sep . 'group'] = $group_name_full;
+                    
                 } else {
                     $group[$group_name]['tab' . $sep . 'count']++;
                 }
+                
                 $group =& $group[$group_name];
                 $i++;
             }
+            
         } else {
             if (! isset($table_groups[$table_name])) {
                 $table_groups[$table_name] = array();
@@ -831,6 +862,7 @@ function PMA_getTableList($db, $tables = null, $limit_offset = 0,
     return $table_groups;
 }
 
+
 /* ----------------------- Set of misc functions ----------------------- */
 
 
@@ -855,6 +887,7 @@ function PMA_getTableList($db, $tables = null, $limit_offset = 0,
  */
 function PMA_backquote($a_name, $do_it = true)
 {
+    
     if (is_array($a_name)) {
         foreach ($a_name as &$data) {
             $data = PMA_backquote($data, $do_it);
@@ -876,8 +909,10 @@ function PMA_backquote($a_name, $do_it = true)
     } else {
         return $a_name;
     }
+    
 } // end of the 'PMA_backquote()' function
 
+
 /**
  * Defines the  value depending on the user OS.
  *
@@ -899,6 +934,7 @@ function PMA_whichCrlf()
     return $the_crlf;
 } // end of the 'PMA_whichCrlf()' function
 
+
 /**
  * Reloads navigation if needed.
  *
@@ -923,6 +959,7 @@ function PMA_getReloadNavigationScript($jsonly = false)
             '',
             '&'
         );
+        
         if (! $jsonly) {
             $retval .= '' . PHP_EOL;
         }
@@ -943,6 +981,7 @@ function PMA_getReloadNavigationScript($jsonly = false)
     return $retval;
 }
 
+
 /**
  * Prepare the message and the query
  * usually the message is the result of the query executed
@@ -957,11 +996,9 @@ function PMA_getReloadNavigationScript($jsonly = false)
  * @access  public
  */
 function PMA_getMessage(
-    $message,
-    $sql_query = null,
-    $type = 'notice',
-    $is_view = false
+    $message, $sql_query = null, $type = 'notice', $is_view = false
 ) {
+    
     global $cfg;
     $retval = '';
 
@@ -1294,8 +1331,7 @@ function PMA_getMessage(
         // in the tools div, only display the Inline link when not in ajax
         // mode because 1) it currently does not work and 2) we would
         // have two similar mechanisms on the page for the same goal
-        if ($is_select
-            || $GLOBALS['is_ajax_request'] === false
+        if ($is_select || ($GLOBALS['is_ajax_request'] === false)
             && ! $query_too_big
         ) {
             // see in js/functions.js the jQuery code attached to id inline_edit
@@ -1314,6 +1350,7 @@ function PMA_getMessage(
             . $refresh_link . $validate_link;
         $retval .= '';
     }
+    
     $retval .= '';
     if ($GLOBALS['is_ajax_request'] === false) {
         $retval .= '
'; @@ -1323,6 +1360,7 @@ function PMA_getMessage( } // end of the 'PMA_getMessage()' function + /** * Verifies if current MySQL server supports profiling * @@ -1337,7 +1375,7 @@ function PMA_profilingSupported() // (avoid a trip to the server for MySQL before 5.0.37) // and do not set a constant as we might be switching servers if (defined('PMA_MYSQL_INT_VERSION') - && PMA_MYSQL_INT_VERSION >= 50037 + && (PMA_MYSQL_INT_VERSION >= 50037) && PMA_DBI_fetch_value("SHOW VARIABLES LIKE 'profiling'") ) { PMA_cacheSet('profiling_supported', true, true); @@ -1349,6 +1387,7 @@ function PMA_profilingSupported() return PMA_cacheGet('profiling_supported', true); } + /** * Returns HTML for the form with the Profiling checkbox * @@ -1367,9 +1406,11 @@ function PMA_getProfilingForm($sql_query) $retval .= PMA_generate_common_hidden_inputs( $GLOBALS['db'], $GLOBALS['table'] ); + $retval .= '' . "\n"; - $retval .= '' . "\n"; + . htmlspecialchars($sql_query) . '" />' . "\n" + . '' . "\n"; + $retval .= PMA_getCheckbox( 'profiling', __('Profiling'), isset($_SESSION['profiling']), true ); @@ -1379,6 +1420,7 @@ function PMA_getProfilingForm($sql_query) return $retval; } + /** * Formats $value to byte view * @@ -1411,14 +1453,14 @@ function PMA_formatByteDown($value, $limes = 6, $comma = 0) __('PiB'), /* l10n: shortcuts for Exabyte */ __('EiB') - ); + ); $dh = PMA_pow(10, $comma); $li = PMA_pow(10, $limes); $unit = $byteUnits[0]; for ($d = 6, $ex = 15; $d >= 1; $d--, $ex-=3) { - if (isset($byteUnits[$d]) && $value >= $li * PMA_pow(10, $ex)) { + if (isset($byteUnits[$d]) && ($value >= $li * PMA_pow(10, $ex))) { // use 1024.0 to avoid integer overflow on 64-bit machines $value = round($value / (PMA_pow(1024, $d) / $dh)) /$dh; $unit = $byteUnits[$d]; @@ -1439,6 +1481,7 @@ function PMA_formatByteDown($value, $limes = 6, $comma = 0) return array(trim($return_value), $unit); } // end of the 'PMA_formatByteDown' function + /** * Changes thousands and decimal separators to locale specific values. * @@ -1455,11 +1498,12 @@ function PMA_localizeNumber($value) __(','), /* l10n: Decimal separator */ __('.'), - ), + ), $value ); } + /** * Formats $value to the given length and appends SI prefixes * with a $length of 0 no truncation occurs, number is only formated @@ -1490,6 +1534,7 @@ function PMA_formatNumber( $value, $digits_left = 3, $digits_right = 0, $only_down = false, $noTrailingZero = true ) { + if ($value == 0) { return '0'; } @@ -1498,7 +1543,7 @@ function PMA_formatNumber( //number_format is not multibyte safe, str_replace is safe if ($digits_left === 0) { $value = number_format($value, $digits_right); - if ($originalValue != 0 && floatval($value) == 0) { + if (($originalValue != 0) && (floatval($value) == 0)) { $value = ' <' . (1 / PMA_pow(10, $digits_right)); } @@ -1575,6 +1620,7 @@ function PMA_formatNumber( return $sign . $value . ' ' . $unit; } // end of the 'PMA_formatNumber' function + /** * Returns the number of bytes when a formatted size is given * @@ -1596,6 +1642,7 @@ function PMA_extractValueFromFormattedSize($formatted_size) return $return_value; }// end of the 'PMA_extractValueFromFormattedSize' function + /** * Writes localised date * @@ -1709,7 +1756,7 @@ function PMA_generateHtmlTab($tab, $url_params = array()) ) { $tab['class'] = 'active'; } elseif (is_null($tab['active']) && empty($GLOBALS['active_page']) - && basename($GLOBALS['PMA_PHP_SELF']) == $tab['link'] + && (basename($GLOBALS['PMA_PHP_SELF']) == $tab['link']) && empty($tab['warning'])) { $tab['class'] = 'active'; } @@ -1777,6 +1824,7 @@ function PMA_generateHtmlTab($tab, $url_params = array()) return $out; } // end of the 'PMA_generateHtmlTab()' function + /** * returns html-code for a tab navigation * @@ -1820,9 +1868,11 @@ function PMA_generateHtmlTabs($tabs, $url_params, $menu_id = 'topmenu') * * @return string the results to be echoed or saved in an array */ -function PMA_linkOrButton($url, $message, $tag_params = array(), +function PMA_linkOrButton( + $url, $message, $tag_params = array(), $new_form = true, $strip_img = false, $target = '' ) { + $url_length = strlen($url); // with this we should be able to catch case of image upload // into a (MEDIUM) BLOB; not worth generating even a form for these @@ -1856,14 +1906,14 @@ function PMA_linkOrButton($url, $message, $tag_params = array(), $displayed_message = ''; // Add text if not already added if (stristr($message, ''; + . htmlspecialchars( + preg_replace('/^.*\salt="([^"]*)".*$/si', '\1', $message) + ) + . ''; } // Suhosin: Check that each query parameter is not above maximum @@ -1881,12 +1931,15 @@ function PMA_linkOrButton($url, $message, $tag_params = array(), } } - if ($url_length <= $GLOBALS['cfg']['LinkLengthLimit'] && $in_suhosin_limits) { + if (($url_length <= $GLOBALS['cfg']['LinkLengthLimit']) && $in_suhosin_limits) { + // no whitespace within an else Safari will make it part of the link $ret = "\n" . '' . $message . $displayed_message . '' . "\n"; + } else { + // no spaces (linebreaks) at all // or after the hidden fields // IE will display them all @@ -1918,6 +1971,7 @@ function PMA_linkOrButton($url, $message, $tag_params = array(), $subname_close = ']'; $submit_link = '#usesubform[' . $GLOBALS['subform_counter'] . ']=1'; } + foreach ($query_parts as $query_pair) { list($eachvar, $eachval) = explode('=', $query_pair); $ret .= '' - . $message . ' ' . $displayed_message . '' . "\n"; + . implode(' ', $tag_params_strings) . '>' + . $message . ' ' . $displayed_message . '' . "\n"; if ($new_form) { $ret .= ''; @@ -1955,13 +2009,16 @@ function PMA_splitURLQuery($url) $url = str_replace(htmlentities('&'), $separator, $url); $url = str_replace('&', $separator, $url); } + $url = str_replace(htmlentities($separator), $separator, $url); // end decode $url_parts = parse_url($url); + return explode($separator, $url_parts['query']); } + /** * Returns a given timespan value in a readable format. * @@ -1975,20 +2032,24 @@ function PMA_timespanFormat($seconds) if ($days > 0) { $seconds -= $days * 86400; } + $hours = floor($seconds / 3600); if ($days > 0 || $hours > 0) { $seconds -= $hours * 3600; } + $minutes = floor($seconds / 60); if ($days > 0 || $hours > 0 || $minutes > 0) { $seconds -= $minutes * 60; } + return sprintf( __('%s days, %s hours, %s minutes and %s seconds'), (string)$days, (string)$hours, (string)$minutes, (string)$seconds ); } + /** * Takes a string and outputs each character on a line for itself. Used * mainly for horizontalflipped display mode. @@ -2036,6 +2097,7 @@ function PMA_flipstring($string, $Separator = "
\n") return $format_string; } + /** * Function added to avoid path disclosures. * Called by each script that needs parameters, it displays @@ -2067,7 +2129,7 @@ function PMA_checkParameters($params, $request = true) $error_message = ''; foreach ($params as $param) { - if ($request && $param != 'db' && $param != 'table') { + if ($request && ($param != 'db') && ($param != 'table')) { $checked_special = true; } @@ -2085,6 +2147,7 @@ function PMA_checkParameters($params, $request = true) } } // end function + /** * Function to generate unique condition for specified row. * @@ -2101,6 +2164,7 @@ function PMA_checkParameters($params, $request = true) function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, $force_unique = false ) { + $primary_key = ''; $unique_key = ''; $nonprimary_condition = ''; @@ -2111,6 +2175,7 @@ function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, $condition_array = array(); for ($i = 0; $i < $fields_cnt; ++$i) { + $condition = ''; $con_key = ''; $con_val = ''; @@ -2149,7 +2214,7 @@ function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, // (The isView() verification should not be costly in most cases // because there is some caching in the function). if (isset($meta->orgtable) - && $meta->table != $meta->orgtable + && ($meta->table != $meta->orgtable) && ! PMA_Table::isView($GLOBALS['db'], $meta->table) ) { $meta->table = $meta->orgtable; @@ -2172,17 +2237,22 @@ function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, if (! isset($row[$i]) || is_null($row[$i])) { $con_val = 'IS NULL'; } else { + // timestamp is numeric on some MySQL 4.1 // for real we use CONCAT above and it should compare to string if ($meta->numeric - && $meta->type != 'timestamp' - && $meta->type != 'real' + && ($meta->type != 'timestamp') + && ($meta->type != 'real') ) { + $con_val = '= ' . $row[$i]; - } elseif (($meta->type == 'blob' || $meta->type == 'string') + + } elseif ((($meta->type == 'blob') || ($meta->type == 'string')) // hexify only if this is a true not empty BLOB or a BINARY && stristr($field_flags, 'BINARY') - && ! empty($row[$i])) { + && ! empty($row[$i]) + ) { + // do not waste memory building a too big condition if (strlen($row[$i]) < 1000) { // use a CAST if possible, to avoid problems @@ -2192,31 +2262,40 @@ function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, // this blob won't be part of the final condition $con_val = null; } + } elseif (in_array($meta->type, PMA_getGISDatatypes()) && ! empty($row[$i]) ) { + // do not build a too big condition if (strlen($row[$i]) < 5000) { $condition .= '=0x' . bin2hex($row[$i]) . ' AND'; } else { $condition = ''; } + } elseif ($meta->type == 'bit') { + $con_val = "= b'" . PMA_printableBitValue($row[$i], $meta->length) . "'"; + } else { $con_val = '= \'' . PMA_sqlAddSlashes($row[$i], false, true) . '\''; - } + } } + if ($con_val != null) { + $condition .= $con_val . ' AND'; - if ($meta->primary_key > 0) { + + if ($meta->primary_key > 0) { $primary_key .= $condition; - $primary_key_array[$con_key] = $con_val; - } elseif ($meta->unique_key > 0) { + $primary_key_array[$con_key] = $con_val; + } elseif ($meta->unique_key > 0) { $unique_key .= $condition; - $unique_key_array[$con_key] = $con_val; + $unique_key_array[$con_key] = $con_val; } + $nonprimary_condition .= $condition; $nonprimary_condition_array[$con_key] = $con_val; } @@ -2226,22 +2305,26 @@ function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, // prefer primary or unique keys for condition, // but use conjunction of all values if no primary key $clause_is_unique = true; - if ($primary_key) { + + if ($primary_key) { $preferred_condition = $primary_key; $condition_array = $primary_key_array; - } elseif ($unique_key) { + + } elseif ($unique_key) { $preferred_condition = $unique_key; $condition_array = $unique_key_array; - } elseif (! $force_unique) { + + } elseif (! $force_unique) { $preferred_condition = $nonprimary_condition; $condition_array = $nonprimary_condition_array; - $clause_is_unique = false; + $clause_is_unique = false; } $where_clause = trim(preg_replace('|\s?AND$|', '', $preferred_condition)); return(array($where_clause, $clause_is_unique, $condition_array)); } // end function + /** * Generate a button or image tag * @@ -2256,13 +2339,15 @@ function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, * * @access public */ -function PMA_getButtonOrImage($button_name, $button_class, $image_name, $text, - $image, $value = '' +function PMA_getButtonOrImage( + $button_name, $button_class, $image_name, $text, $image, $value = '' ) { + if ($value == '') { $value = $text; } - if (false === $GLOBALS['cfg']['PropertiesIconic']) { + + if ($GLOBALS['cfg']['PropertiesIconic'] === false) { return ' ' . "\n"; @@ -2287,6 +2372,7 @@ function PMA_getButtonOrImage($button_name, $button_class, $image_name, $text, } } // end function + /** * Generate a pagination selector for browsing resultsets * @@ -2307,10 +2393,11 @@ function PMA_getButtonOrImage($button_name, $button_class, $image_name, $text, * * @access public */ -function PMA_pageselector($rows, $pageNow = 1, $nbTotalPage = 1, - $showAll = 200, $sliceStart = 5, $sliceEnd = 5, $percent = 20, - $range = 10, $prompt = '' +function PMA_pageselector( + $rows, $pageNow = 1, $nbTotalPage = 1, $showAll = 200, $sliceStart = 5, + $sliceEnd = 5, $percent = 20, $range = 10, $prompt = '' ) { + $increment = floor($nbTotalPage / $percent); $pageNowMinusRange = ($pageNow - $range); $pageNowPlusRange = ($pageNow + $range); @@ -2319,10 +2406,13 @@ function PMA_pageselector($rows, $pageNow = 1, $nbTotalPage = 1, if ($GLOBALS['cfg']['AjaxEnable']) { $gotopage .= ' class="ajax"'; } + $gotopage .= ' name="pos" >' . "\n"; if ($nbTotalPage < $showAll) { $pages = range(1, $nbTotalPage); + } else { + $pages = array(); // Always show first X pages @@ -2344,6 +2434,7 @@ function PMA_pageselector($rows, $pageNow = 1, $nbTotalPage = 1, $i = $sliceStart; $x = $nbTotalPage - $sliceEnd; $met_boundary = false; + while ($i <= $x) { if ($i >= $pageNowMinusRange && $i <= $pageNowPlusRange) { // If our pageselector comes near the current page, we use 1 @@ -2439,8 +2530,9 @@ function PMA_pageselector($rows, $pageNow = 1, $nbTotalPage = 1, * * @todo use $pos from $_url_params */ -function PMA_getListNavigator($count, $pos, $_url_params, $script, $frame, $max_count) -{ +function PMA_getListNavigator( + $count, $pos, $_url_params, $script, $frame, $max_count +) { $list_navigator_html = ''; @@ -2532,6 +2624,7 @@ function PMA_getListNavigator($count, $pos, $_url_params, $script, $frame, $max_ } + /** * replaces %u in given path with current user name * @@ -2555,6 +2648,7 @@ function PMA_userDir($dir) return str_replace('%u', $GLOBALS['cfg']['Server']['user'], $dir); } + /** * returns html code for db link to default db page * @@ -2582,6 +2676,7 @@ function PMA_getDbLink($database = null) . '">' . htmlspecialchars($database) . ''; } + /** * Prepare a lightbulb hint explaining a known external bug * that affects a functionality @@ -2596,7 +2691,7 @@ function PMA_getDbLink($database = null) function PMA_getExternalBug($functionality, $component, $minimum_version, $bugref) { $ext_but_html = ''; - if ($component == 'mysql' && PMA_MYSQL_INT_VERSION < $minimum_version) { + if (($component == 'mysql') && (PMA_MYSQL_INT_VERSION < $minimum_version)) { $ext_but_html .= PMA_showHint( sprintf( __('The %s functionality is affected by a known bug, see %s'), @@ -2608,6 +2703,7 @@ function PMA_getExternalBug($functionality, $component, $minimum_version, $bugre return $ext_but_html; } + /** * Returns a HTML checkbox * @@ -2620,14 +2716,13 @@ function PMA_getExternalBug($functionality, $component, $minimum_version, $bugre */ function PMA_getCheckbox($html_field_name, $label, $checked, $onclick) { - return ''; - } + /** * Generates a set of radio HTML fields * @@ -2647,25 +2742,31 @@ function PMA_getRadioFields($html_field_name, $choices, $checked_choice = '', $radio_html = ''; foreach ($choices as $choice_value => $choice_label) { + if (! empty($class)) { $radio_html .= '
'; } + $html_field_id = $html_field_name . '_' . $choice_value; $radio_html .= '' . ($escape_label ? htmlspecialchars($choice_label) : $choice_label) . ''; + if ($line_break) { $radio_html .= '
'; } + if (! empty($class)) { $radio_html .= '
'; } @@ -2676,6 +2777,7 @@ function PMA_getRadioFields($html_field_name, $choices, $checked_choice = '', } + /** * Generates and returns an HTML dropdown * @@ -2694,17 +2796,21 @@ function PMA_getDropdown($select_name, $choices, $active_choice, $id) { $result = ''; + return $result; } + /** * Generates a slider effect (jQjuery) * Takes care of generating the initial
and the link @@ -2739,6 +2845,7 @@ function PMA_getDivForSliderEffect($id, $message) } + /** * Creates an AJAX sliding toggle button * (or and equivalent form when AJAX is disabled) @@ -2757,6 +2864,7 @@ function PMA_toggleButton($action, $select_name, $options, $callback) $link = "$action&" . urlencode($select_name) . "="; $link_on = $link . urlencode($options[1]['value']); $link_off = $link . urlencode($options[0]['value']); + if ($options[1]['selected'] == true) { $state = 'on'; } else if ($options[0]['selected'] == true) { @@ -2764,45 +2872,46 @@ function PMA_toggleButton($action, $select_name, $options, $callback) } else { $state = 'on'; } + // Generate output - $retval = "\n"; - $retval .= "
\n"; - $retval .= "
\n"; - $retval .= "
\n"; - $retval .= " \n"; - $retval .= "
 
\n"; - $retval .= " \n"; - $retval .= " $link_off\n"; - $retval .= "
"; - $retval .= str_replace(' ', ' ', htmlspecialchars($options[0]['label'])); - $retval .= "\n" . "
\n"; - $retval .= " \n"; - $retval .= " \n"; - $retval .= " \n"; - $retval .= " "; - $retval .= htmlspecialchars($callback) . "\n"; - $retval .= " "; - $retval .= htmlspecialchars($GLOBALS['text_dir']) . "\n"; - $retval .= "
\n"; - $retval .= "
\n"; - $retval .= "
\n"; - $retval .= ""; - - return $retval; + return "\n" + . "
\n" + . "
\n" + . "
\n" + . " \n" + . " \n" + . " \n" + . " \n" + . " \n" + . " \n" + . " \n" + . " \n" + . "
\n" + . " $link_on\n" + . "
" + . str_replace(' ', ' ', htmlspecialchars($options[1]['label'])) + . "\n" . "
\n" + . "
 
\n" + . " $link_off\n" + . "
" + . str_replace(' ', ' ', htmlspecialchars($options[0]['label'])) + . "\n" . "
\n" + . "
\n" + . " " + . htmlspecialchars($callback) . "\n" + . " " + . htmlspecialchars($GLOBALS['text_dir']) . "\n" + . "
\n" + . "
\n" + . "
\n" + . ""; + } // end PMA_toggleButton() + /** * Clears cache content which needs to be refreshed on user change. * @@ -2813,6 +2922,7 @@ function PMA_clearUserCache() PMA_cacheUnset('is_superuser', true); } + /** * Verifies if something is cached in the session * @@ -2829,6 +2939,7 @@ function PMA_cacheExists($var, $server = 0) return isset($_SESSION['cache']['server_' . $server][$var]); } + /** * Gets cached information from the session * @@ -2849,6 +2960,7 @@ function PMA_cacheGet($var, $server = 0) } } + /** * Caches information in the session * @@ -2866,6 +2978,7 @@ function PMA_cacheSet($var, $val = null, $server = 0) $_SESSION['cache']['server_' . $server][$var] = $val; } + /** * Removes cached information from the session * @@ -2876,12 +2989,13 @@ function PMA_cacheSet($var, $val = null, $server = 0) */ function PMA_cacheUnset($var, $server = 0) { - if (true === $server) { + if ($server === true) { $server = $GLOBALS['server']; } unset($_SESSION['cache']['server_' . $server][$var]); } + /** * Converts a bit value to printable format; * in MySQL a BIT field can be from 1 to 64 bits so we need this @@ -2902,6 +3016,7 @@ function PMA_printableBitValue($value, $length) return $printable; } + /** * Verifies whether the value contains a non-printable character * @@ -2914,6 +3029,7 @@ function PMA_containsNonPrintableAscii($value) return preg_match('@[^[:print:]]@', $value); } + /** * Converts a BIT type default value * for example, b'010' becomes 010 @@ -2927,6 +3043,7 @@ function PMA_convertBitDefaultValue($bit_default_value) return strtr($bit_default_value, array("b" => "", "'" => "")); } + /** * Extracts the various parts from a column spec * @@ -2954,6 +3071,7 @@ function PMA_extractColumnSpec($columnspec) } if ('enum' == $type || 'set' == $type) { + // Define our working vars $enum_set_values = array(); $working = ""; @@ -2962,11 +3080,13 @@ function PMA_extractColumnSpec($columnspec) // While there is another character to process while (isset($columnspec[$index])) { + // Grab the char to look at $char = $columnspec[$index]; // If it is a single quote, needs to be handled specially if ($char == "'") { + // If we are not currently in a string, begin one if (! $in_string) { $in_string = true; @@ -2992,6 +3112,7 @@ function PMA_extractColumnSpec($columnspec) $index++; } } + } elseif ('\\' == $char && isset($columnspec[$index + 1]) && "'" == $columnspec[$index + 1] @@ -3003,15 +3124,19 @@ function PMA_extractColumnSpec($columnspec) // Otherwise, add it to our working string like normal $working .= $char; } + // Increment character index $index++; } // end while + $printtype = $type . '(' . str_replace("','", "', '", $spec_in_brackets) . ')'; $binary = false; $unsigned = false; $zerofill = false; + } else { + $enum_set_values = array(); /* Create printable type name */ @@ -3029,6 +3154,7 @@ function PMA_extractColumnSpec($columnspec) } else { $binary = false; } + $printtype = preg_replace('@zerofill@', '', $printtype, -1, $zerofill_cnt); $zerofill = ($zerofill_cnt > 0); $printtype = preg_replace('@unsigned@', '', $printtype, -1, $unsigned_cnt); @@ -3079,6 +3205,7 @@ function PMA_extractColumnSpec($columnspec) ); } + /** * Verifies if this table's engine supports foreign keys * @@ -3089,13 +3216,14 @@ function PMA_extractColumnSpec($columnspec) function PMA_isForeignKeySupported($engine) { $engine = strtoupper($engine); - if ('INNODB' == $engine || 'PBXT' == $engine) { + if (($engine == 'INNODB') || ($engine == 'PBXT')) { return true; } else { return false; } } + /** * Replaces some characters by a displayable equivalent * @@ -3113,6 +3241,7 @@ function PMA_replaceBinaryContents($content) return $result; } + /** * Converts GIS data to Well Known Text format * @@ -3129,17 +3258,21 @@ function PMA_asWKT($data, $includeSRID = false) if ($includeSRID) { $wktsql .= ", SRID(x'" . $hex . "')"; } + $wktresult = PMA_DBI_try_query($wktsql, null, PMA_DBI_QUERY_STORE); $wktarr = PMA_DBI_fetch_row($wktresult, 0); $wktval = $wktarr[0]; + if ($includeSRID) { $srid = $wktarr[1]; $wktval = "'" . $wktval . "'," . $srid; } @PMA_DBI_free_result($wktresult); + return $wktval; } + /** * If the string starts with a \r\n pair (0x0d0a) add an extra \n * @@ -3157,6 +3290,7 @@ function PMA_duplicateFirstNewline($string) return $string; } + /** * Get the action word corresponding to a script name * in order to display it as a title in navigation panel @@ -3185,6 +3319,7 @@ function PMA_getTitleForTarget($target) return $mapping[$target]; } + /** * Formats user string, expanding @VARIABLES@, accepting strftime format * string. @@ -3202,11 +3337,13 @@ function PMA_expandUserString($string, $escape = null, $updates = array()) $vars['http_host'] = PMA_getenv('HTTP_HOST'); $vars['server_name'] = $GLOBALS['cfg']['Server']['host']; $vars['server_verbose'] = $GLOBALS['cfg']['Server']['verbose']; + if (empty($GLOBALS['cfg']['Server']['verbose'])) { $vars['server_verbose_or_name'] = $GLOBALS['cfg']['Server']['host']; } else { $vars['server_verbose_or_name'] = $GLOBALS['cfg']['Server']['verbose']; } + $vars['database'] = $GLOBALS['db']; $vars['table'] = $GLOBALS['table']; $vars['phpmyadmin_version'] = 'phpMyAdmin ' . PMA_VERSION; @@ -3266,6 +3403,7 @@ function PMA_expandUserString($string, $escape = null, $updates = array()) return strtr(strftime($string), $replace); } + /** * Prepare the form used to browse anywhere on the local server for a file to * import @@ -3296,6 +3434,7 @@ function PMA_getBrowseUploadFileBlock($max_upload_size) return $block_html; } + /** * Prepare the form used to select a file to import from the server upload * directory @@ -3358,6 +3497,7 @@ function PMA_getSelectUploadFileBlock($import_list, $uploaddir) } + /** * Build titles and icons for action links * @@ -3384,9 +3524,11 @@ function PMA_buildActionTitles() $titles['NoExport'] = PMA_getIcon('bd_export.png', __('Export')); $titles['Execute'] = PMA_getIcon('b_nextpage.png', __('Execute')); $titles['NoExecute'] = PMA_getIcon('bd_nextpage.png', __('Execute')); + return $titles; } + /** * This function processes the datatypes supported by the DB, as specified in * PMA_Types->getColumns() and either returns an array (useful for quickly checking @@ -3401,12 +3543,17 @@ function PMA_buildActionTitles() function PMA_getSupportedDatatypes($html = false, $selected = '') { if ($html) { + // NOTE: the SELECT tag in not included in this snippet. $retval = ''; + foreach ($GLOBALS['PMA_Types']->getColumns() as $key => $value) { + if (is_array($value)) { + $retval .= ""; foreach ($value as $subvalue) { + if ($subvalue == $selected) { $retval .= sprintf( '', @@ -3426,7 +3573,9 @@ function PMA_getSupportedDatatypes($html = false, $selected = '') } } $retval .= ''; + } else { + if ($selected == $value) { $retval .= sprintf( '', @@ -3440,11 +3589,14 @@ function PMA_getSupportedDatatypes($html = false, $selected = '') $value ); } + } } + } else { $retval = array(); foreach ($GLOBALS['PMA_Types']->getColumns() as $value) { + if (is_array($value)) { foreach ($value as $subvalue) { if ($subvalue !== '-') { @@ -3456,12 +3608,14 @@ function PMA_getSupportedDatatypes($html = false, $selected = '') $retval[] = $value; } } + } } return $retval; } // end PMA_getSupportedDatatypes() + /** * Returns a list of datatypes that are not (yet) handled by PMA. * Used by: tbl_change.php and libraries/db_routines.inc.php @@ -3474,6 +3628,7 @@ function PMA_unsupportedDatatypes() return $no_support_types; } + /** * Return GIS data types * @@ -3501,6 +3656,7 @@ function PMA_getGISDatatypes($upper_case = false) return $gis_data_types; } + /** * Generates GIS data based on the string passed. * @@ -3522,6 +3678,7 @@ function PMA_createGISData($gis_string) } } + /** * Returns the names and details of the functions * that can be applied on geometry data typess. @@ -3595,6 +3752,7 @@ function PMA_getGISFunctions($geom_type = null, $binary = true, $display = false if ($display) { $funcs[] = array('display' => '--------'); } + if (PMA_MYSQL_INT_VERSION < 50601) { $funcs['Crosses'] = array('params' => 2, 'type' => 'int'); $funcs['Contains'] = array('params' => 2, 'type' => 'int'); @@ -3614,7 +3772,6 @@ function PMA_getGISFunctions($geom_type = null, $binary = true, $display = false $funcs['ST_Overlaps'] = array('params' => 2, 'type' => 'int'); $funcs['ST_Touches'] = array('params' => 2, 'type' => 'int'); $funcs['ST_Within'] = array('params' => 2, 'type' => 'int'); - } if ($display) { @@ -3632,6 +3789,7 @@ function PMA_getGISFunctions($geom_type = null, $binary = true, $display = false return $funcs; } + /** * Returns default function for a particular column. * @@ -3677,10 +3835,12 @@ function PMA_getDefaultFunctionForField($field, $insert_mode) ) { $default_function = $cfg['DefaultFunctions']['first_timestamp']; } + // Default for first timestamp field if ($field['first_timestamp']) { $default_function = $cfg['DefaultFunctions']['first_timestamp']; } + // For primary keys of type char(36) or varchar(36) UUID if the default // function // Only applies to insert mode, as it would silently trash data on updates. @@ -3690,6 +3850,7 @@ function PMA_getDefaultFunctionForField($field, $insert_mode) ) { $default_function = $cfg['DefaultFunctions']['FUNC_UUID']; } + // this is set only when appropriate and is always true if (isset($field['display_binary_as_hex'])) { $default_function = 'UNHEX'; @@ -3698,6 +3859,7 @@ function PMA_getDefaultFunctionForField($field, $insert_mode) return $default_function; } + /** * Creates a dropdown box with MySQL functions for a particular column. * @@ -3751,6 +3913,7 @@ function PMA_getFunctionsForField($field, $insert_mode) return $retval; } // end PMA_getFunctionsForField() + /** * Checks if the current user has a specific privilege and returns true if the * user indeed has that privilege or false if (s)he doesn't. This function must @@ -3778,15 +3941,18 @@ function PMA_currentUserHasPrivilege($priv, $db = null, $tbl = null) if ($user === false) { return false; } + $user = explode('@', $user); $username = "''"; $username .= str_replace("'", "''", $user[0]); $username .= "''@''"; $username .= str_replace("'", "''", $user[1]); $username .= "''"; + // Prepage the query $query = "SELECT `PRIVILEGE_TYPE` FROM `INFORMATION_SCHEMA`.`%s` " . "WHERE GRANTEE='%s' AND PRIVILEGE_TYPE='%s'"; + // Check global privileges first. $user_privileges = PMA_DBI_fetch_value( sprintf( @@ -3847,6 +4013,7 @@ function PMA_currentUserHasPrivilege($priv, $db = null, $tbl = null) return false; } + /** * Returns server type for current connection * @@ -3867,6 +4034,7 @@ function PMA_getServerType() return $server_type; } + /** * Analyzes the limit clause and return the start and length attributes of it. * @@ -3883,6 +4051,7 @@ function PMA_analyzeLimitClause($limit_clause) ); } + /** * Prepare HTML code for display button. * @@ -3896,6 +4065,7 @@ function PMA_getButton() . '

'; } + /** * Replace some html-unfriendly stuff * @@ -3922,7 +4092,8 @@ function PMA_mimeDefaultFunction($buffer) return $buffer; } -/* + +/** * Parses ENUM/SET values * * @param string $definition The definition of the column @@ -3939,9 +4110,12 @@ function PMA_parseEnumSetValues($definition) $values = array(); $in_string = false; $buffer = ''; + for ($i=0; $i 0) { // The leftovers in the buffer are the last value (if any) $values[] = $buffer; } + return $values; }