diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 6146adc87f..c1af73bbae 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,11 @@ +2009-05-04 Esteban Sánchez + + * extensions/dbmanager.php: Code cleanup, making it easier an without + unneded loops. Added error message support when querying. + + * extensions/dbmanager/dbmanager.css: Use CSS inheritance for cells and + header of the table. + 2009-05-04 Esteban Sánchez * include/functions_agents.php: Added create_agent(), diff --git a/pandora_console/extensions/dbmanager.php b/pandora_console/extensions/dbmanager.php index 44b64d3b2c..cd3bc73688 100644 --- a/pandora_console/extensions/dbmanager.php +++ b/pandora_console/extensions/dbmanager.php @@ -16,19 +16,7 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -function string_decompose ($mystring){ - - $output = ""; - for ($a=0; $a < strlen($mystring); $a++){ - $output .= substr($mystring, $a, 1)."|"; -// $output .= ord(substr($mystring, $a, 1)).":".substr($mystring, $a, 1)."|"; -// $output .= ord(substr($mystring, $a, 1))."|"; - } - return $output; -} - -function dbmanager_query ($sql, $rettype = "affected_rows") { +function dbmanager_query ($sql, &$error) { global $config; $retval = array(); @@ -41,30 +29,29 @@ function dbmanager_query ($sql, $rettype = "affected_rows") { // see with a simple echo and mysql reject it, so dont forget to do this. $sql = unsafe_string ($sql); - $sql = htmlspecialchars_decode ($sql, ENT_QUOTES ); + $sql = htmlspecialchars_decode ($sql, ENT_QUOTES); $result = mysql_query ($sql); if ($result === false) { $backtrace = debug_backtrace (); - $error = sprintf ('%s (\'%s\') in %s on line %d', - mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']); - set_error_handler ('sql_error_handler'); - trigger_error ($error); - restore_error_handler (); + $error = mysql_error (); return false; - } elseif ($result === true) { + } + + if ($result === true) { if ($rettype == "insert_id") { return mysql_insert_id (); } elseif ($rettype == "info") { return mysql_info (); } - return mysql_affected_rows (); //This happens in case the statement was executed but didn't need a resource - } else { - while ($row = mysql_fetch_array ($result)) { - array_push ($retval, $row); - } - mysql_free_result ($result); + return mysql_affected_rows (); } + + while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { + array_push ($retval, $row); + } + mysql_free_result ($result); + if (! empty ($retval)) return $retval; //Return false, check with === or !== @@ -73,66 +60,61 @@ function dbmanager_query ($sql, $rettype = "affected_rows") { function dbmgr_extension_main () { + require_css_file ('dbmanager', 'extensions/dbmanager/'); - echo ''; + $sql = (string) get_parameter ('sql'); - $sqlcode = get_parameter ("sqlcode", ""); + echo "

Database interface

"; + echo '
'; + echo "This is an advanced extension to interface with Pandora FMS database directly from WEB console using native SQL sentences. Please note that you can damage your Pandora FMS installation if you don't know exactly what are you doing, this means that you can severily damage your setup using this extension. This extension is intended to be used only by experienced users with a depth knowledgue of Pandora FMS internals."; + echo '
'; - echo "

Database Interface

"; - echo "

This is an advanced extension to interface with Pandora FMS database directly from WEB console using native SQL sentences. Please note that you can damage your Pandora FMS installation if you don't know exactly what are you doing, this means that you can severily damage your setup using this extension. This extension is intended to be used only by experienced users with a depth knowledgue of Pandora FMS internals.

"; - - echo "
"; - echo "Some samples of usage:
SHOW STATUS;
DESCRIBE tagente
SELECT * FROM tserver
UPDATE tagente SET id_grupo = 15 WHERE nombre LIKE '%194.179%'
"; + echo "
"; + echo "Some samples of usage:
SHOW STATUS;
DESCRIBE tagente
SELECT * FROM tserver
UPDATE tagente SET id_grupo = 15 WHERE nombre LIKE '%194.179%'
"; - echo "

"; + echo "

"; echo "
"; - echo ""; - echo "

"; - print_submit_button (__('Execute SQL'), '', false, 'class="sub next"',false); + print_textarea ('sql', 5, 50, unsafe_string ($sql)); + echo '
'; + echo '
'; + print_submit_button (__('Execute SQL'), '', false, 'class="sub next"'); + echo '
'; echo "
"; // Processing SQL Code - if ($sqlcode != ""){ - echo "
"; - echo "
"; - echo "
"; - $result = dbmanager_query ($sqlcode); - if (!is_array($result)){ - echo "Result: ".$result; - } - else { - $header = ""; - $header_printed = 0; - echo ''; - foreach ($result as $item => $value){ - $data = ""; - foreach ($value as $row => $value2){ - if ($header_printed ==0) - if (!is_numeric($row)) - $header .= ""; - $header_printed = 1; - } - echo $data; - echo ""; - } - echo "
" . $row; - if (!is_numeric($row)){ - $data .= "" . $value2; - } - } - if ($header_printed == 0){ - echo $header; - echo "
"; - } - } - + if ($sql == '') + return; + + echo "
"; + echo "
"; + echo "
"; + + $error = ''; + $result = dbmanager_query ($sql, $error); + + if ($result === false) { + echo 'An error has occured when querying the database.
'; + echo $error; + return; + } + + if (! is_array ($result)) { + echo "Output: ".$result; + return; + } + + $table->width = '90%'; + $table->class = 'dbmanager'; + $table->head = array_keys ($result[0]); + + $table->data = $result; + + print_table ($table); } /* This adds a option in the operation menu */ -add_godmode_menu_option (__('DB Interface'), 'PM'); +add_godmode_menu_option (__('DB interface'), 'PM'); /* This sets the function to be called when the extension is selected in the operation menu */ add_extension_godmode_function ('dbmgr_extension_main'); diff --git a/pandora_console/extensions/dbmanager/dbmanager.css b/pandora_console/extensions/dbmanager/dbmanager.css index 017f26c52d..36fda7fd9e 100644 --- a/pandora_console/extensions/dbmanager/dbmanager.css +++ b/pandora_console/extensions/dbmanager/dbmanager.css @@ -2,29 +2,29 @@ Plain old table styles written by Chris Heilmann http://wait-till-i.com */ -table.dbmanager,td.dbmanager,th.dbmanager{ +table.dbmanager, table.dbmanager td, table.dbmanager th { border:1px solid #888; - border-collapse:collapse; - margin:0; - padding:0; + border-collapse: collapse; + margin: 0; + padding: 0; } -td.dbmanager,th.dbmanager{ + +table.dbmanager td, table.dbmanager th { padding:.2em .5em; - vertical-align:top; - font-weight:normal; - background:#fafafa; - color:#000; + vertical-align: top; + font-weight: normal; + background: #fafafa; + color: #000; font-size: 9px; } -th.dbmanager { +table.dbmanager th { color: #fff; text-transform: uppercase; font-size: 8px; background: #888; } - textarea.dbmanager { min-height: 50px; height: 50px;