From d428db6641acfd81fb4a6c04e1e213efb6909c65 Mon Sep 17 00:00:00 2001 From: Hugues Peccatte Date: Wed, 18 Dec 2013 22:19:13 +0100 Subject: [PATCH 1/4] Reduce nested levels. Signed-off-by: Hugues Peccatte --- libraries/config/FormDisplay.class.php | 60 ++++---- libraries/gis/pma_gis_multipolygon.php | 77 +++++----- libraries/navigation/NavigationTree.class.php | 12 +- libraries/rte/rte_routines.lib.php | 139 +++++++++--------- 4 files changed, 150 insertions(+), 138 deletions(-) diff --git a/libraries/config/FormDisplay.class.php b/libraries/config/FormDisplay.class.php index 4ead3c2414..e29fd1375f 100644 --- a/libraries/config/FormDisplay.class.php +++ b/libraries/config/FormDisplay.class.php @@ -642,37 +642,41 @@ class FormDisplay } // save forms - if ($allow_partial_save || empty($this->_errors)) { - foreach ($to_save as $work_path => $path) { - // TrustedProxies requires changes before saving - if ($path == 'TrustedProxies') { - $proxies = array(); - $i = 0; - foreach ($values[$path] as $value) { - $matches = array(); - $match = preg_match( - "/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches - ); - if ($match) { - // correct 'IP: HTTP header' pair - $ip = trim($matches[1]); - $proxies[$ip] = trim($matches[2]); - } else { - // save also incorrect values - $proxies["-$i"] = $value; - $i++; - } + if (!$allow_partial_save && !empty($this->_errors)) { + // don't look for non-critical errors + $this->_validate(); + return $result; + } + + foreach ($to_save as $work_path => $path) { + // TrustedProxies requires changes before saving + if ($path == 'TrustedProxies') { + $proxies = array(); + $i = 0; + foreach ($values[$path] as $value) { + $matches = array(); + $match = preg_match( + "/^(.+):(?:[ ]?)(\\w+)$/", $value, $matches + ); + if ($match) { + // correct 'IP: HTTP header' pair + $ip = trim($matches[1]); + $proxies[$ip] = trim($matches[2]); + } else { + // save also incorrect values + $proxies["-$i"] = $value; + $i++; } - $values[$path] = $proxies; } - $this->_configFile->set($work_path, $values[$path], $path); - } - if ($is_setup_script) { - $this->_configFile->set( - 'UserprefsDisallow', - array_keys($this->_userprefsDisallow) - ); + $values[$path] = $proxies; } + $this->_configFile->set($work_path, $values[$path], $path); + } + if ($is_setup_script) { + $this->_configFile->set( + 'UserprefsDisallow', + array_keys($this->_userprefsDisallow) + ); } // don't look for non-critical errors diff --git a/libraries/gis/pma_gis_multipolygon.php b/libraries/gis/pma_gis_multipolygon.php index 7afbff386e..eca3889df8 100644 --- a/libraries/gis/pma_gis_multipolygon.php +++ b/libraries/gis/pma_gis_multipolygon.php @@ -409,20 +409,23 @@ class PMA_GIS_Multipolygon extends PMA_GIS_Geometry // Classify inner rings to their respective outer rings. foreach ($row_data['parts'] as $j => $ring1) { - if (! $ring1['isOuter']) { - foreach ($row_data['parts'] as $k => $ring2) { - if ($ring2['isOuter']) { - // If the pointOnSurface of the inner ring - // is also inside the outer ring - if (PMA_GIS_Polygon::isPointInsidePolygon( - $ring1['pointOnSurface'], $ring2['points'] - )) { - if (! isset($ring2['inner'])) { - $row_data['parts'][$k]['inner'] = array(); - } - $row_data['parts'][$k]['inner'][] = $j; - } + if ($ring1['isOuter']) { + continue; + } + foreach ($row_data['parts'] as $k => $ring2) { + if (!$ring2['isOuter']) { + continue; + } + + // If the pointOnSurface of the inner ring + // is also inside the outer ring + if (PMA_GIS_Polygon::isPointInsidePolygon( + $ring1['pointOnSurface'], $ring2['points'] + )) { + if (! isset($ring2['inner'])) { + $row_data['parts'][$k]['inner'] = array(); } + $row_data['parts'][$k]['inner'][] = $j; } } } @@ -430,30 +433,32 @@ class PMA_GIS_Multipolygon extends PMA_GIS_Geometry $wkt = 'MULTIPOLYGON('; // for each polygon foreach ($row_data['parts'] as $ring) { - if ($ring['isOuter']) { - $wkt .= '('; // start of polygon - - $wkt .= '('; // start of outer ring - foreach ($ring['points'] as $point) { - $wkt .= $point['x'] . ' ' . $point['y'] . ','; - } - $wkt = substr($wkt, 0, strlen($wkt) - 1); - $wkt .= ')'; // end of outer ring - - // inner rings if any - if (isset($ring['inner'])) { - foreach ($ring['inner'] as $j) { - $wkt .= ',('; // start of inner ring - foreach ($row_data['parts'][$j]['points'] as $innerPoint) { - $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ','; - } - $wkt = substr($wkt, 0, strlen($wkt) - 1); - $wkt .= ')'; // end of inner ring - } - } - - $wkt .= '),'; // end of polygon + if (!$ring['isOuter']) { + continue; } + + $wkt .= '('; // start of polygon + + $wkt .= '('; // start of outer ring + foreach ($ring['points'] as $point) { + $wkt .= $point['x'] . ' ' . $point['y'] . ','; + } + $wkt = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= ')'; // end of outer ring + + // inner rings if any + if (isset($ring['inner'])) { + foreach ($ring['inner'] as $j) { + $wkt .= ',('; // start of inner ring + foreach ($row_data['parts'][$j]['points'] as $innerPoint) { + $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ','; + } + $wkt = substr($wkt, 0, strlen($wkt) - 1); + $wkt .= ')'; // end of inner ring + } + } + + $wkt .= '),'; // end of polygon } $wkt = substr($wkt, 0, strlen($wkt) - 1); diff --git a/libraries/navigation/NavigationTree.class.php b/libraries/navigation/NavigationTree.class.php index d386f22a06..a23c79af91 100644 --- a/libraries/navigation/NavigationTree.class.php +++ b/libraries/navigation/NavigationTree.class.php @@ -648,10 +648,10 @@ class PMA_NavigationTree $children = $this->_tree->children; usort($children, array('PMA_NavigationTree', 'sortNode')); $this->_setVisibility(); - for ($i=0; $i_renderNode($children[0], true, 'first'); - } else if ($i + 1 != count($children)) { + } else if ($i + 1 != $nbChildren) { $retval .= $this->_renderNode($children[$i], true); } else { $retval .= $this->_renderNode($children[$i], true, 'last'); @@ -680,8 +680,8 @@ class PMA_NavigationTree $retval .= $this->_getPageSelector($node); $children = $node->children; usort($children, array('PMA_NavigationTree', 'sortNode')); - for ($i=0; $i_renderNode($children[$i], true); } else { $retval .= $this->_renderNode($children[$i], true, 'last'); @@ -959,8 +959,8 @@ class PMA_NavigationTree $children = $node->children; usort($children, array('PMA_NavigationTree', 'sortNode')); $buffer = ''; - for ($i=0; $i_renderNode( $children[$i], true, diff --git a/libraries/rte/rte_routines.lib.php b/libraries/rte/rte_routines.lib.php index 67433335ea..e2b68dd22c 100644 --- a/libraries/rte/rte_routines.lib.php +++ b/libraries/rte/rte_routines.lib.php @@ -656,76 +656,79 @@ function PMA_RTN_getDataFromName($name, $type, $all = true) $retval['item_param_opts_text'] = $params['opts']; // Get extra data - if ($all) { - if ($retval['item_type'] == 'FUNCTION') { - $retval['item_type_toggle'] = 'PROCEDURE'; - } else { - $retval['item_type_toggle'] = 'FUNCTION'; - } - $retval['item_returntype'] = ''; - $retval['item_returnlength'] = ''; - $retval['item_returnopts_num'] = ''; - $retval['item_returnopts_text'] = ''; - if (! empty($routine['DTD_IDENTIFIER'])) { - if (strlen($routine['DTD_IDENTIFIER']) > 63) { - // If the DTD_IDENTIFIER string from INFORMATION_SCHEMA is - // at least 64 characters, then it may actually have been - // chopped because that column is a varchar(64), so we will - // parse the output of SHOW CREATE query to get accurate - // information about the return variable. - $dtd = ''; - $fetching = false; - for ($i=0; $i<$parsed_query['len']; $i++) { - if ($parsed_query[$i]['type'] == 'alpha_reservedWord' - && strtoupper($parsed_query[$i]['data']) == 'RETURNS' - ) { - $fetching = true; - } else if ($fetching == true - && $parsed_query[$i]['type'] == 'alpha_reservedWord' - ) { - // We will not be looking for options such as UNSIGNED - // or ZEROFILL because there is no way that a numeric - // field's DTD_IDENTIFIER can be longer than 64 - // characters. We can safely assume that the return - // datatype is either ENUM or SET, so we only look - // for CHARSET. - $word = strtoupper($parsed_query[$i]['data']); - if ($word == 'CHARSET' - && ($parsed_query[$i+1]['type'] == 'alpha_charset' - || $parsed_query[$i+1]['type'] == 'alpha_identifier') - ) { - $dtd .= $word . ' ' . $parsed_query[$i+1]['data']; - } - break; - } else if ($fetching == true) { - $dtd .= $parsed_query[$i]['data'] . ' '; - } - } - $routine['DTD_IDENTIFIER'] = $dtd; - } - $returnparam = PMA_RTN_parseOneParameter($routine['DTD_IDENTIFIER']); - $retval['item_returntype'] = $returnparam[2]; - $retval['item_returnlength'] = $returnparam[3]; - $retval['item_returnopts_num'] = $returnparam[4]; - $retval['item_returnopts_text'] = $returnparam[4]; - } - $retval['item_definer'] = PMA_RTN_parseRoutineDefiner($parsed_query); - $retval['item_definition'] = $routine['ROUTINE_DEFINITION']; - $retval['item_isdeterministic'] = ''; - if ($routine['IS_DETERMINISTIC'] == 'YES') { - $retval['item_isdeterministic'] = " checked='checked'"; - } - $retval['item_securitytype_definer'] = ''; - $retval['item_securitytype_invoker'] = ''; - if ($routine['SECURITY_TYPE'] == 'DEFINER') { - $retval['item_securitytype_definer'] = " selected='selected'"; - } else if ($routine['SECURITY_TYPE'] == 'INVOKER') { - $retval['item_securitytype_invoker'] = " selected='selected'"; - } - $retval['item_sqldataaccess'] = $routine['SQL_DATA_ACCESS']; - $retval['item_comment'] = $routine['ROUTINE_COMMENT']; + if (!$all) { + return $retval; } + if ($retval['item_type'] == 'FUNCTION') { + $retval['item_type_toggle'] = 'PROCEDURE'; + } else { + $retval['item_type_toggle'] = 'FUNCTION'; + } + $retval['item_returntype'] = ''; + $retval['item_returnlength'] = ''; + $retval['item_returnopts_num'] = ''; + $retval['item_returnopts_text'] = ''; + if (! empty($routine['DTD_IDENTIFIER'])) { + if (strlen($routine['DTD_IDENTIFIER']) > 63) { + // If the DTD_IDENTIFIER string from INFORMATION_SCHEMA is + // at least 64 characters, then it may actually have been + // chopped because that column is a varchar(64), so we will + // parse the output of SHOW CREATE query to get accurate + // information about the return variable. + $dtd = ''; + $fetching = false; + for ($i=0; $i<$parsed_query['len']; $i++) { + if ($parsed_query[$i]['type'] == 'alpha_reservedWord' + && strtoupper($parsed_query[$i]['data']) == 'RETURNS' + ) { + $fetching = true; + } else if ($fetching == true + && $parsed_query[$i]['type'] == 'alpha_reservedWord' + ) { + // We will not be looking for options such as UNSIGNED + // or ZEROFILL because there is no way that a numeric + // field's DTD_IDENTIFIER can be longer than 64 + // characters. We can safely assume that the return + // datatype is either ENUM or SET, so we only look + // for CHARSET. + $word = strtoupper($parsed_query[$i]['data']); + if ($word == 'CHARSET' + && ($parsed_query[$i+1]['type'] == 'alpha_charset' + || $parsed_query[$i+1]['type'] == 'alpha_identifier') + ) { + $dtd .= $word . ' ' . $parsed_query[$i+1]['data']; + } + break; + } else if ($fetching == true) { + $dtd .= $parsed_query[$i]['data'] . ' '; + } + } + $routine['DTD_IDENTIFIER'] = $dtd; + } + $returnparam = PMA_RTN_parseOneParameter($routine['DTD_IDENTIFIER']); + $retval['item_returntype'] = $returnparam[2]; + $retval['item_returnlength'] = $returnparam[3]; + $retval['item_returnopts_num'] = $returnparam[4]; + $retval['item_returnopts_text'] = $returnparam[4]; + } + + $retval['item_definer'] = PMA_RTN_parseRoutineDefiner($parsed_query); + $retval['item_definition'] = $routine['ROUTINE_DEFINITION']; + $retval['item_isdeterministic'] = ''; + if ($routine['IS_DETERMINISTIC'] == 'YES') { + $retval['item_isdeterministic'] = " checked='checked'"; + } + $retval['item_securitytype_definer'] = ''; + $retval['item_securitytype_invoker'] = ''; + if ($routine['SECURITY_TYPE'] == 'DEFINER') { + $retval['item_securitytype_definer'] = " selected='selected'"; + } else if ($routine['SECURITY_TYPE'] == 'INVOKER') { + $retval['item_securitytype_invoker'] = " selected='selected'"; + } + $retval['item_sqldataaccess'] = $routine['SQL_DATA_ACCESS']; + $retval['item_comment'] = $routine['ROUTINE_COMMENT']; + return $retval; } // PMA_RTN_getDataFromName() From c5839569f83d686a327d15e5c353931520a5b328 Mon Sep 17 00:00:00 2001 From: Hugues Peccatte Date: Fri, 20 Dec 2013 23:12:03 +0100 Subject: [PATCH 2/4] Reduce nested levels. Signed-off-by: Hugues Peccatte --- libraries/server_privileges.lib.php | 254 ++++++++++++++-------------- libraries/zip_extension.lib.php | 119 +++++++------ 2 files changed, 191 insertions(+), 182 deletions(-) diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index bde4809bc6..5f0c437b12 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2020,133 +2020,135 @@ function PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db, $table = n { $html_output = ''; $odd_row = true; - if (! empty($privMap)) { - foreach ($privMap as $current_user => $val) { - foreach ($val as $current_host => $current_privileges) { - $nbPrivileges = count($current_privileges); - $html_output .= ''; - - // user - $html_output .= ' 1) { - $html_output .= ' rowspan="' . $nbPrivileges . '"'; - } - $html_output .= '>'; - if (empty($current_user)) { - $html_output .= '' - . __('Any') . ''; - } else { - $html_output .= htmlspecialchars($current_user); - } - $html_output .= ''; - - // host - $html_output .= ' 1) { - $html_output .= ' rowspan="' . $nbPrivileges . '"'; - } - $html_output .= '>'; - $html_output .= htmlspecialchars($current_host); - $html_output .= ''; - - for ($i = 0; $i < $nbPrivileges; $i++) { - $current = $current_privileges[$i]; - - // type - $html_output .= ''; - if ($current['Type'] == 'g') { - $html_output .= __('global'); - } elseif ($current['Type'] == 'd') { - if ($current['Db'] == PMA_Util::escapeMysqlWildcards($db)) { - $html_output .= __('database-specific'); - } else { - $html_output .= __('wildcard'). ': ' - . '' - . htmlspecialchars($current['Db']) - . ''; - } - } elseif ($current['Type'] == 't') { - $html_output .= __('table-specific'); - } - $html_output .= ''; - - // privileges - $html_output .= ''; - if (isset($current['Table_name'])) { - $privList = explode(',', $current['Table_priv']); - $privs = array(); - $grantsArr = PMA_getTableGrantsArray(); - foreach ($grantsArr as $grant) { - $privs[$grant[0]] = 'N'; - foreach ($privList as $priv) { - if ($grant[0] == $priv) { - $privs[$grant[0]] = 'Y'; - } - } - } - $html_output .= '' - . join( - ',', - PMA_extractPrivInfo($privs, true, true) - ) - . ''; - } else { - $html_output .= '' - . join( - ',', - PMA_extractPrivInfo($current, true, false) - ) - . ''; - } - $html_output .= ''; - - // grant - $html_output .= ''; - $containsGrant = false; - if (isset($current['Table_name'])) { - $privList = explode(',', $current['Table_priv']); - foreach ($privList as $priv) { - if ($priv == 'Grant') { - $containsGrant = true; - } - } - } else { - $containsGrant = $current['Grant_priv'] == 'Y'; - } - $html_output .= ($containsGrant ? __('Yes') : __('No')); - $html_output .= ''; - - // action - $html_output .= ''; - $specific_db = (isset($current['Db']) && $current['Db'] != '*') - ? $current['Db'] : ''; - $specific_table = (isset($current['Table_name']) - && $current['Table_name'] != '*') - ? $current['Table_name'] : ''; - $html_output .= PMA_getUserEditLink( - $current_user, - $current_host, - $specific_db, - $specific_table - ); - $html_output .= ''; - - $html_output .= ''; - if (($i + 1) < $nbPrivileges) { - $html_output .= ''; - } - } - $odd_row = ! $odd_row; - } - } - } else { + if (empty($privMap)) { $html_output .= '' - . '' - . __('No user found.') - . '' - . ''; + . '' + . __('No user found.') + . '' + . '' + . ''; + return $html_output; + } + + foreach ($privMap as $current_user => $val) { + foreach ($val as $current_host => $current_privileges) { + $nbPrivileges = count($current_privileges); + $html_output .= ''; + + // user + $html_output .= ' 1) { + $html_output .= ' rowspan="' . $nbPrivileges . '"'; + } + $html_output .= '>'; + if (empty($current_user)) { + $html_output .= '' + . __('Any') . ''; + } else { + $html_output .= htmlspecialchars($current_user); + } + $html_output .= ''; + + // host + $html_output .= ' 1) { + $html_output .= ' rowspan="' . $nbPrivileges . '"'; + } + $html_output .= '>'; + $html_output .= htmlspecialchars($current_host); + $html_output .= ''; + + for ($i = 0; $i < $nbPrivileges; $i++) { + $current = $current_privileges[$i]; + + // type + $html_output .= ''; + if ($current['Type'] == 'g') { + $html_output .= __('global'); + } elseif ($current['Type'] == 'd') { + if ($current['Db'] == PMA_Util::escapeMysqlWildcards($db)) { + $html_output .= __('database-specific'); + } else { + $html_output .= __('wildcard'). ': ' + . '' + . htmlspecialchars($current['Db']) + . ''; + } + } elseif ($current['Type'] == 't') { + $html_output .= __('table-specific'); + } + $html_output .= ''; + + // privileges + $html_output .= ''; + if (isset($current['Table_name'])) { + $privList = explode(',', $current['Table_priv']); + $privs = array(); + $grantsArr = PMA_getTableGrantsArray(); + foreach ($grantsArr as $grant) { + $privs[$grant[0]] = 'N'; + foreach ($privList as $priv) { + if ($grant[0] == $priv) { + $privs[$grant[0]] = 'Y'; + } + } + } + $html_output .= '' + . join( + ',', + PMA_extractPrivInfo($privs, true, true) + ) + . ''; + } else { + $html_output .= '' + . join( + ',', + PMA_extractPrivInfo($current, true, false) + ) + . ''; + } + $html_output .= ''; + + // grant + $html_output .= ''; + $containsGrant = false; + if (isset($current['Table_name'])) { + $privList = explode(',', $current['Table_priv']); + foreach ($privList as $priv) { + if ($priv == 'Grant') { + $containsGrant = true; + } + } + } else { + $containsGrant = $current['Grant_priv'] == 'Y'; + } + $html_output .= ($containsGrant ? __('Yes') : __('No')); + $html_output .= ''; + + // action + $html_output .= ''; + $specific_db = (isset($current['Db']) && $current['Db'] != '*') + ? $current['Db'] : ''; + $specific_table = (isset($current['Table_name']) + && $current['Table_name'] != '*') + ? $current['Table_name'] : ''; + $html_output .= PMA_getUserEditLink( + $current_user, + $current_host, + $specific_db, + $specific_table + ); + $html_output .= ''; + + $html_output .= ''; + if (($i + 1) < $nbPrivileges) { + $html_output .= ''; + } + } + $odd_row = ! $odd_row; + } } $html_output .= ''; diff --git a/libraries/zip_extension.lib.php b/libraries/zip_extension.lib.php index 0c4db27f07..967dc39c98 100644 --- a/libraries/zip_extension.lib.php +++ b/libraries/zip_extension.lib.php @@ -23,65 +23,72 @@ function PMA_getZipContents($file, $specific_entry = null) $error_message = ''; $file_data = ''; $zip_handle = zip_open($file); - if (is_resource($zip_handle)) { - $first_zip_entry = zip_read($zip_handle); - if (false === $first_zip_entry) { - $error_message = __('No files found inside ZIP archive!'); - } else { - /* Is the the zip really an ODS file? */ - $read = zip_entry_read($first_zip_entry); - $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet'; - if (!strcmp($ods_mime, $read)) { - $specific_entry = '/^content\.xml$/'; - } - - if (isset($specific_entry)) { - /* Return the correct contents, not just the first entry */ - for ( ; ; ) { - $entry = zip_read($zip_handle); - if (is_resource($entry)) { - if (preg_match($specific_entry, zip_entry_name($entry))) { - zip_entry_open($zip_handle, $entry, 'r'); - $file_data = zip_entry_read( - $entry, - zip_entry_filesize($entry) - ); - zip_entry_close($entry); - break; - } - } else { - /** - * Either we have reached the end of the zip and still - * haven't found $specific_entry or there was a parsing - * error that we must display - */ - if ($entry === false) { - $error_message = __('Error in ZIP archive:') - . ' Could not find "' . $specific_entry . '"'; - } else { - $error_message = __('Error in ZIP archive:') - . ' ' . PMA_getZipError($zip_handle); - } - - break; - } - } - } else { - zip_entry_open($zip_handle, $first_zip_entry, 'r'); - /* File pointer has already been moved, - * so include what was read above */ - $file_data = $read; - $file_data .= zip_entry_read( - $first_zip_entry, - zip_entry_filesize($first_zip_entry) - ); - zip_entry_close($first_zip_entry); - } - } - } else { + if (!is_resource($zip_handle)) { $error_message = __('Error in ZIP archive:') . ' ' . PMA_getZipError($zip_handle); + zip_close($zip_handle); + return (array('error' => $error_message, 'data' => $file_data)); } + + $first_zip_entry = zip_read($zip_handle); + if (false !== $first_zip_entry) { + $error_message = __('No files found inside ZIP archive!'); + zip_close($zip_handle); + return (array('error' => $error_message, 'data' => $file_data)); + } + + /* Is the the zip really an ODS file? */ + $read = zip_entry_read($first_zip_entry); + $ods_mime = 'application/vnd.oasis.opendocument.spreadsheet'; + if (!strcmp($ods_mime, $read)) { + $specific_entry = '/^content\.xml$/'; + } + + if (!isset($specific_entry)) { + zip_entry_open($zip_handle, $first_zip_entry, 'r'); + /* File pointer has already been moved, + * so include what was read above */ + $file_data = $read; + $file_data .= zip_entry_read( + $first_zip_entry, + zip_entry_filesize($first_zip_entry) + ); + zip_entry_close($first_zip_entry); + zip_close($zip_handle); + return (array('error' => $error_message, 'data' => $file_data)); + } + + /* Return the correct contents, not just the first entry */ + for ( ; ; ) { + $entry = zip_read($zip_handle); + if (is_resource($entry)) { + if (preg_match($specific_entry, zip_entry_name($entry))) { + zip_entry_open($zip_handle, $entry, 'r'); + $file_data = zip_entry_read( + $entry, + zip_entry_filesize($entry) + ); + zip_entry_close($entry); + break; + } + } else { + /** + * Either we have reached the end of the zip and still + * haven't found $specific_entry or there was a parsing + * error that we must display + */ + if ($entry === false) { + $error_message = __('Error in ZIP archive:') + . ' Could not find "' . $specific_entry . '"'; + } else { + $error_message = __('Error in ZIP archive:') + . ' ' . PMA_getZipError($zip_handle); + } + + break; + } + } + zip_close($zip_handle); return (array('error' => $error_message, 'data' => $file_data)); } From efd206f1dc91b82babbe9b37aaded10e5132e190 Mon Sep 17 00:00:00 2001 From: Hugues Peccatte Date: Sat, 21 Dec 2013 00:06:04 +0100 Subject: [PATCH 3/4] Reduce nested levels. Signed-off-by: Hugues Peccatte --- libraries/import.lib.php | 332 +++++++++++++++------------- libraries/plugin_interface.lib.php | 211 +++++++++--------- libraries/server_privileges.lib.php | 199 +++++++++-------- 3 files changed, 397 insertions(+), 345 deletions(-) diff --git a/libraries/import.lib.php b/libraries/import.lib.php index c8bfacf8bf..1f8ba75d3d 100644 --- a/libraries/import.lib.php +++ b/libraries/import.lib.php @@ -91,179 +91,205 @@ function PMA_importRunQuery($sql = '', $full = '', $controluser = false, $skip_queries, $executed_queries, $max_sql_len, $read_multiply, $cfg, $sql_query_disabled, $db, $run_query, $is_superuser; $read_multiply = 1; - if (isset($import_run_buffer)) { - // Should we skip something? - if ($skip_queries > 0) { - $skip_queries--; + if (!isset($import_run_buffer)) { + // Do we have something to push into buffer? + $import_run_buffer = PMA_importRunQuery_post( + $import_run_buffer, $sql, $full + ); + return; + } + + // Should we skip something? + if ($skip_queries > 0) { + $skip_queries--; + // Do we have something to push into buffer? + $import_run_buffer = PMA_importRunQuery_post( + $import_run_buffer, $sql, $full + ); + return; + } + + if (! empty($import_run_buffer['sql']) + && trim($import_run_buffer['sql']) != '' + ) { + + // USE query changes the database, son need to track + // while running multiple queries + $is_use_query + = (stripos($import_run_buffer['sql'], "use ") !== false) + ? true + : false; + + $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql'])); + if (! $sql_query_disabled) { + $sql_query .= $import_run_buffer['full']; + } + $pattern = '@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?' + . 'DATABASE @i'; + if (! $cfg['AllowUserDropDatabase'] + && ! $is_superuser + && preg_match($pattern, $import_run_buffer['sql']) + ) { + $GLOBALS['message'] = PMA_Message::error( + __('"DROP DATABASE" statements are disabled.') + ); + $error = true; } else { - if (! empty($import_run_buffer['sql']) - && trim($import_run_buffer['sql']) != '' + + $executed_queries++; + + $pattern = '/^[\s]*(SELECT|SHOW|HANDLER)/i'; + if ($run_query + && $GLOBALS['finished'] + && empty($sql) + && ! $error + && ((! empty($import_run_buffer['sql']) + && preg_match($pattern, $import_run_buffer['sql'])) + || ($executed_queries == 1)) ) { - - // USE query changes the database, son need to track - // while running multiple queries - $is_use_query - = (stripos($import_run_buffer['sql'], "use ") !== false) - ? true - : false; - - $max_sql_len = max($max_sql_len, strlen($import_run_buffer['sql'])); + $go_sql = true; if (! $sql_query_disabled) { - $sql_query .= $import_run_buffer['full']; - } - $pattern = '@^[[:space:]]*DROP[[:space:]]+(IF EXISTS[[:space:]]+)?' - . 'DATABASE @i'; - if (! $cfg['AllowUserDropDatabase'] - && ! $is_superuser - && preg_match($pattern, $import_run_buffer['sql']) - ) { - $GLOBALS['message'] = PMA_Message::error(__('"DROP DATABASE" statements are disabled.')); - $error = true; + $complete_query = $sql_query; + $display_query = $sql_query; } else { + $complete_query = ''; + $display_query = ''; + } + $sql_query = $import_run_buffer['sql']; + $sql_data['valid_sql'][] = $import_run_buffer['sql']; + if (! isset($sql_data['valid_queries'])) { + $sql_data['valid_queries'] = 0; + } + $sql_data['valid_queries']++; - $executed_queries++; + // If a 'USE ' SQL-clause was found, + // set our current $db to the new one + list($db, $reload) = PMA_lookForUse( + $import_run_buffer['sql'], + $db, + $reload + ); + } elseif ($run_query) { - $pattern = '/^[\s]*(SELECT|SHOW|HANDLER)/i'; - if ($run_query - && $GLOBALS['finished'] - && empty($sql) - && ! $error - && ((! empty($import_run_buffer['sql']) - && preg_match($pattern, $import_run_buffer['sql'])) - || ($executed_queries == 1)) - ) { - $go_sql = true; - if (! $sql_query_disabled) { - $complete_query = $sql_query; - $display_query = $sql_query; - } else { - $complete_query = ''; - $display_query = ''; - } - $sql_query = $import_run_buffer['sql']; + if ($controluser) { + $result = PMA_queryAsControlUser( + $import_run_buffer['sql'] + ); + } else { + $result = $GLOBALS['dbi'] + ->tryQuery($import_run_buffer['sql']); + } + + $msg = '# '; + if ($result === false) { // execution failed + if (! isset($my_die)) { + $my_die = array(); + } + $my_die[] = array( + 'sql' => $import_run_buffer['full'], + 'error' => $GLOBALS['dbi']->getError() + ); + + $msg .= __('Error'); + + if (! $cfg['IgnoreMultiSubmitErrors']) { + $error = true; + return; + } + } else { + $a_num_rows = (int)@$GLOBALS['dbi']->numRows($result); + $a_aff_rows = (int)@$GLOBALS['dbi']->affectedRows(); + if ($a_num_rows > 0) { + $msg .= __('Rows'). ': ' . $a_num_rows; + $last_query_with_results = $import_run_buffer['sql']; + } elseif ($a_aff_rows > 0) { + $message = PMA_Message::getMessageForAffectedRows( + $a_aff_rows + ); + $msg .= $message->getMessage(); + } else { + $msg .= __( + 'MySQL returned an empty result set (i.e. zero ' + . 'rows).' + ); + } + + if (($a_num_rows > 0) || $is_use_query) { $sql_data['valid_sql'][] = $import_run_buffer['sql']; if (! isset($sql_data['valid_queries'])) { $sql_data['valid_queries'] = 0; } $sql_data['valid_queries']++; - - // If a 'USE ' SQL-clause was found, - // set our current $db to the new one - list($db, $reload) = PMA_lookForUse( - $import_run_buffer['sql'], - $db, - $reload - ); - } elseif ($run_query) { - - if ($controluser) { - $result = PMA_queryAsControlUser( - $import_run_buffer['sql'] - ); - } else { - $result = $GLOBALS['dbi'] - ->tryQuery($import_run_buffer['sql']); - } - - $msg = '# '; - if ($result === false) { // execution failed - if (! isset($my_die)) { - $my_die = array(); - } - $my_die[] = array( - 'sql' => $import_run_buffer['full'], - 'error' => $GLOBALS['dbi']->getError() - ); - - $msg .= __('Error'); - - if (! $cfg['IgnoreMultiSubmitErrors']) { - $error = true; - return; - } - } else { - $a_num_rows = (int)@$GLOBALS['dbi']->numRows($result); - $a_aff_rows = (int)@$GLOBALS['dbi']->affectedRows(); - if ($a_num_rows > 0) { - $msg .= __('Rows'). ': ' . $a_num_rows; - $last_query_with_results = $import_run_buffer['sql']; - } elseif ($a_aff_rows > 0) { - $message = PMA_Message::getMessageForAffectedRows( - $a_aff_rows - ); - $msg .= $message->getMessage(); - } else { - $msg .= __( - 'MySQL returned an empty result set (i.e. zero ' - . 'rows).' - ); - } - - if (($a_num_rows > 0) || $is_use_query) { - $sql_data['valid_sql'][] = $import_run_buffer['sql']; - if (! isset($sql_data['valid_queries'])) { - $sql_data['valid_queries'] = 0; - } - $sql_data['valid_queries']++; - } - - } - if (! $sql_query_disabled) { - $sql_query .= $msg . "\n"; - } - - // If a 'USE ' SQL-clause was found and the query - // succeeded, set our current $db to the new one - if ($result != false) { - list($db, $reload) = PMA_lookForUse( - $import_run_buffer['sql'], - $db, - $reload - ); - } - - $pattern = '@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)' - . '?(TABLE|DATABASE)[[:space:]]+(.+)@im'; - if ($result != false - && preg_match($pattern, $import_run_buffer['sql']) - ) { - $reload = true; - } - } // end run query - } // end if not DROP DATABASE - // end non empty query - } elseif (! empty($import_run_buffer['full'])) { - if ($go_sql) { - $complete_query .= $import_run_buffer['full']; - $display_query .= $import_run_buffer['full']; - } else { - if (! $sql_query_disabled) { - $sql_query .= $import_run_buffer['full']; } + } - } - // check length of query unless we decided to pass it to sql.php - // (if $run_query is false, we are just displaying so show - // the complete query in the textarea) - if (! $go_sql && $run_query) { - if (! empty($sql_query)) { - if (strlen($sql_query) > 50000 - || $executed_queries > 50 - || $max_sql_len > 1000 - ) { - $sql_query = ''; - $sql_query_disabled = true; - } + if (! $sql_query_disabled) { + $sql_query .= $msg . "\n"; } + + // If a 'USE ' SQL-clause was found and the query + // succeeded, set our current $db to the new one + if ($result != false) { + list($db, $reload) = PMA_lookForUse( + $import_run_buffer['sql'], + $db, + $reload + ); + } + + $pattern = '@^[\s]*(DROP|CREATE)[\s]+(IF EXISTS[[:space:]]+)' + . '?(TABLE|DATABASE)[[:space:]]+(.+)@im'; + if ($result != false + && preg_match($pattern, $import_run_buffer['sql']) + ) { + $reload = true; + } + } // end run query + } // end if not DROP DATABASE + // end non empty query + } elseif (! empty($import_run_buffer['full'])) { + if ($go_sql) { + $complete_query .= $import_run_buffer['full']; + $display_query .= $import_run_buffer['full']; + } else { + if (! $sql_query_disabled) { + $sql_query .= $import_run_buffer['full']; } - } // end do query (no skip) - } // end buffer exists + } + } + // check length of query unless we decided to pass it to sql.php + // (if $run_query is false, we are just displaying so show + // the complete query in the textarea) + if (! $go_sql && $run_query) { + if (! empty($sql_query)) { + if (strlen($sql_query) > 50000 + || $executed_queries > 50 + || $max_sql_len > 1000 + ) { + $sql_query = ''; + $sql_query_disabled = true; + } + } + } // Do we have something to push into buffer? - if (! empty($sql) || ! empty($full)) { + $import_run_buffer = PMA_importRunQuery_post($import_run_buffer, $sql, $full); +} + +/** + * @param $import_run_buffer + * @param $sql + * @param $full + * @return array + */ +function PMA_importRunQuery_post($import_run_buffer, $sql, $full) +{ + if (!empty($sql) || !empty($full)) { $import_run_buffer = array('sql' => $sql, 'full' => $full); + return $import_run_buffer; } else { unset($GLOBALS['import_run_buffer']); + return $import_run_buffer; } } diff --git a/libraries/plugin_interface.lib.php b/libraries/plugin_interface.lib.php index 326adfefac..335969865c 100644 --- a/libraries/plugin_interface.lib.php +++ b/libraries/plugin_interface.lib.php @@ -304,116 +304,117 @@ function PMA_pluginGetOneOption( $propertyItem, true ); - } else { - // single property item - switch ($property_class) { - case "BoolPropertyItem": - $ret .= '
  • ' . "\n"; - $ret .= 'getName() - ); + continue; + } - if ($propertyItem->getForce() != null) { - // Same code is also few lines lower, update both if needed - $ret .= ' onclick="if (!this.checked && ' - . '(!document.getElementById(\'checkbox_' . $plugin_name - . '_' . $propertyItem->getForce() . '\') ' - . '|| !document.getElementById(\'checkbox_' - . $plugin_name . '_' . $propertyItem->getForce() - . '\').checked)) ' - . 'return false; else return true;"'; - } - $ret .= ' />'; - $ret .= ''; - break; - case "DocPropertyItem": - echo "DocPropertyItem"; - break; - case "HiddenPropertyItem": - $ret .= '
  • '; - break; - case "MessageOnlyPropertyItem": - $ret .= '
  • ' . "\n"; - $ret .= '

    ' . PMA_getString($propertyItem->getText()) . '

    '; - break; - case "RadioPropertyItem": - $default = PMA_pluginGetDefault( + // single property item + switch ($property_class) { + case "BoolPropertyItem": + $ret .= '
  • ' . "\n"; + $ret .= 'getName() ); - foreach ($propertyItem->getValues() as $key => $val) { - $ret .= '
  • ' - . PMA_getString($val) . '
  • '; - } - break; - case "SelectPropertyItem": - $ret .= '
  • ' . "\n"; - $ret .= ''; - $ret .= ''; - break; - case "TextPropertyItem": - case "NumberPropertyItem": - $ret .= '
  • ' . "\n"; - $ret .= ''; - $ret .= 'getSize() != null - ? ' size="' . $propertyItem->getSize() . '"' - : '') - . ($propertyItem->getLen() != null - ? ' maxlength="' . $propertyItem->getLen() . '"' - : '') - . ' />'; - break; - default:; + + if ($propertyItem->getForce() != null) { + // Same code is also few lines lower, update both if needed + $ret .= ' onclick="if (!this.checked && ' + . '(!document.getElementById(\'checkbox_' . $plugin_name + . '_' . $propertyItem->getForce() . '\') ' + . '|| !document.getElementById(\'checkbox_' + . $plugin_name . '_' . $propertyItem->getForce() + . '\').checked)) ' + . 'return false; else return true;"'; } + $ret .= ' />'; + $ret .= ''; + break; + case "DocPropertyItem": + echo "DocPropertyItem"; + break; + case "HiddenPropertyItem": + $ret .= '
  • '; + break; + case "MessageOnlyPropertyItem": + $ret .= '
  • ' . "\n"; + $ret .= '

    ' . PMA_getString($propertyItem->getText()) . '

    '; + break; + case "RadioPropertyItem": + $default = PMA_pluginGetDefault( + $section, + $plugin_name . '_' . $propertyItem->getName() + ); + foreach ($propertyItem->getValues() as $key => $val) { + $ret .= '
  • ' + . PMA_getString($val) . '
  • '; + } + break; + case "SelectPropertyItem": + $ret .= '
  • ' . "\n"; + $ret .= ''; + $ret .= ''; + break; + case "TextPropertyItem": + case "NumberPropertyItem": + $ret .= '
  • ' . "\n"; + $ret .= ''; + $ret .= 'getSize() != null + ? ' size="' . $propertyItem->getSize() . '"' + : '') + . ($propertyItem->getLen() != null + ? ' maxlength="' . $propertyItem->getLen() . '"' + : '') + . ' />'; + break; + default:; } } } diff --git a/libraries/server_privileges.lib.php b/libraries/server_privileges.lib.php index 5f0c437b12..3d8c22dd9c 100644 --- a/libraries/server_privileges.lib.php +++ b/libraries/server_privileges.lib.php @@ -2059,94 +2059,11 @@ function PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db, $table = n $html_output .= htmlspecialchars($current_host); $html_output .= ''; - for ($i = 0; $i < $nbPrivileges; $i++) { - $current = $current_privileges[$i]; + $html_output .= PMA_getHtmlListOfPrivs( + $db, $current_privileges, $current_user, + $current_host, $odd_row + ); - // type - $html_output .= ''; - if ($current['Type'] == 'g') { - $html_output .= __('global'); - } elseif ($current['Type'] == 'd') { - if ($current['Db'] == PMA_Util::escapeMysqlWildcards($db)) { - $html_output .= __('database-specific'); - } else { - $html_output .= __('wildcard'). ': ' - . '' - . htmlspecialchars($current['Db']) - . ''; - } - } elseif ($current['Type'] == 't') { - $html_output .= __('table-specific'); - } - $html_output .= ''; - - // privileges - $html_output .= ''; - if (isset($current['Table_name'])) { - $privList = explode(',', $current['Table_priv']); - $privs = array(); - $grantsArr = PMA_getTableGrantsArray(); - foreach ($grantsArr as $grant) { - $privs[$grant[0]] = 'N'; - foreach ($privList as $priv) { - if ($grant[0] == $priv) { - $privs[$grant[0]] = 'Y'; - } - } - } - $html_output .= '' - . join( - ',', - PMA_extractPrivInfo($privs, true, true) - ) - . ''; - } else { - $html_output .= '' - . join( - ',', - PMA_extractPrivInfo($current, true, false) - ) - . ''; - } - $html_output .= ''; - - // grant - $html_output .= ''; - $containsGrant = false; - if (isset($current['Table_name'])) { - $privList = explode(',', $current['Table_priv']); - foreach ($privList as $priv) { - if ($priv == 'Grant') { - $containsGrant = true; - } - } - } else { - $containsGrant = $current['Grant_priv'] == 'Y'; - } - $html_output .= ($containsGrant ? __('Yes') : __('No')); - $html_output .= ''; - - // action - $html_output .= ''; - $specific_db = (isset($current['Db']) && $current['Db'] != '*') - ? $current['Db'] : ''; - $specific_table = (isset($current['Table_name']) - && $current['Table_name'] != '*') - ? $current['Table_name'] : ''; - $html_output .= PMA_getUserEditLink( - $current_user, - $current_host, - $specific_db, - $specific_table - ); - $html_output .= ''; - - $html_output .= ''; - if (($i + 1) < $nbPrivileges) { - $html_output .= ''; - } - } $odd_row = ! $odd_row; } } @@ -2155,6 +2072,114 @@ function PMA_getHtmlTableBodyForSpecificDbOrTablePrivs($privMap, $db, $table = n return $html_output; } +/** + * Get HTML to display privileges + * + * @param string $db Database name + * @param array $current_privileges List of privileges + * @param string $current_user Current user + * @param string $current_host Current host + * @param boolean $odd_row Current row is odd + * + * @return string HTML to display privileges + */ +function PMA_getHtmlListOfPrivs( + $db, $current_privileges, $current_user, + $current_host, $odd_row +) { + $nbPrivileges = count($current_privileges); + $html_output = null; + for ($i = 0; $i < $nbPrivileges; $i++) { + $current = $current_privileges[$i]; + + // type + $html_output .= ''; + if ($current['Type'] == 'g') { + $html_output .= __('global'); + } elseif ($current['Type'] == 'd') { + if ($current['Db'] == PMA_Util::escapeMysqlWildcards($db)) { + $html_output .= __('database-specific'); + } else { + $html_output .= __('wildcard') . ': ' + . '' + . htmlspecialchars($current['Db']) + . ''; + } + } elseif ($current['Type'] == 't') { + $html_output .= __('table-specific'); + } + $html_output .= ''; + + // privileges + $html_output .= ''; + if (isset($current['Table_name'])) { + $privList = explode(',', $current['Table_priv']); + $privs = array(); + $grantsArr = PMA_getTableGrantsArray(); + foreach ($grantsArr as $grant) { + $privs[$grant[0]] = 'N'; + foreach ($privList as $priv) { + if ($grant[0] == $priv) { + $privs[$grant[0]] = 'Y'; + } + } + } + $html_output .= '' + . join( + ',', + PMA_extractPrivInfo($privs, true, true) + ) + . ''; + } else { + $html_output .= '' + . join( + ',', + PMA_extractPrivInfo($current, true, false) + ) + . ''; + } + $html_output .= ''; + + // grant + $html_output .= ''; + $containsGrant = false; + if (isset($current['Table_name'])) { + $privList = explode(',', $current['Table_priv']); + foreach ($privList as $priv) { + if ($priv == 'Grant') { + $containsGrant = true; + } + } + } else { + $containsGrant = $current['Grant_priv'] == 'Y'; + } + $html_output .= ($containsGrant ? __('Yes') : __('No')); + $html_output .= ''; + + // action + $html_output .= ''; + $specific_db = (isset($current['Db']) && $current['Db'] != '*') + ? $current['Db'] : ''; + $specific_table = (isset($current['Table_name']) + && $current['Table_name'] != '*') + ? $current['Table_name'] : ''; + $html_output .= PMA_getUserEditLink( + $current_user, + $current_host, + $specific_db, + $specific_table + ); + $html_output .= ''; + + $html_output .= ''; + if (($i + 1) < $nbPrivileges) { + $html_output .= ''; + } + } + return $html_output; +} + /** * Returns edit link for a user. * From 6104e9557eedcb1634b4841ba619cd42feb3e898 Mon Sep 17 00:00:00 2001 From: Hugues Peccatte Date: Sat, 21 Dec 2013 22:10:06 +0100 Subject: [PATCH 4/4] Fix wrong refactoring. Signed-off-by: Hugues Peccatte --- libraries/zip_extension.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/zip_extension.lib.php b/libraries/zip_extension.lib.php index 967dc39c98..e8a45d9c16 100644 --- a/libraries/zip_extension.lib.php +++ b/libraries/zip_extension.lib.php @@ -31,7 +31,7 @@ function PMA_getZipContents($file, $specific_entry = null) } $first_zip_entry = zip_read($zip_handle); - if (false !== $first_zip_entry) { + if (false === $first_zip_entry) { $error_message = __('No files found inside ZIP archive!'); zip_close($zip_handle); return (array('error' => $error_message, 'data' => $file_data));