2009-05-04 Esteban Sánchez <estebans@artica.es>

* 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.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@1674 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
Esteban Sanchez 2009-05-04 13:11:16 +00:00
parent 7749e201de
commit 0aef184a94
3 changed files with 75 additions and 85 deletions

View File

@ -1,3 +1,11 @@
2009-05-04 Esteban Sánchez <estebans@artica.es>
* 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 <estebans@artica.es> 2009-05-04 Esteban Sánchez <estebans@artica.es>
* include/functions_agents.php: Added create_agent(), * include/functions_agents.php: Added create_agent(),

View File

@ -16,19 +16,7 @@
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
function dbmanager_query ($sql, &$error) {
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") {
global $config; global $config;
$retval = array(); $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. // see with a simple echo and mysql reject it, so dont forget to do this.
$sql = unsafe_string ($sql); $sql = unsafe_string ($sql);
$sql = htmlspecialchars_decode ($sql, ENT_QUOTES ); $sql = htmlspecialchars_decode ($sql, ENT_QUOTES);
$result = mysql_query ($sql); $result = mysql_query ($sql);
if ($result === false) { if ($result === false) {
$backtrace = debug_backtrace (); $backtrace = debug_backtrace ();
$error = sprintf ('%s (\'%s\') in <strong>%s</strong> on line %d', $error = mysql_error ();
mysql_error (), $sql, $backtrace[0]['file'], $backtrace[0]['line']);
set_error_handler ('sql_error_handler');
trigger_error ($error);
restore_error_handler ();
return false; return false;
} elseif ($result === true) { }
if ($result === true) {
if ($rettype == "insert_id") { if ($rettype == "insert_id") {
return mysql_insert_id (); return mysql_insert_id ();
} elseif ($rettype == "info") { } elseif ($rettype == "info") {
return mysql_info (); return mysql_info ();
} }
return mysql_affected_rows (); //This happens in case the statement was executed but didn't need a resource return mysql_affected_rows ();
} else {
while ($row = mysql_fetch_array ($result)) {
array_push ($retval, $row);
}
mysql_free_result ($result);
} }
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
array_push ($retval, $row);
}
mysql_free_result ($result);
if (! empty ($retval)) if (! empty ($retval))
return $retval; return $retval;
//Return false, check with === or !== //Return false, check with === or !==
@ -73,66 +60,61 @@ function dbmanager_query ($sql, $rettype = "affected_rows") {
function dbmgr_extension_main () { function dbmgr_extension_main () {
require_css_file ('dbmanager', 'extensions/dbmanager/');
echo '<link rel="stylesheet" href="extensions/dbmanager/dbmanager.css" type="text/css" />'; $sql = (string) get_parameter ('sql');
$sqlcode = get_parameter ("sqlcode", ""); echo "<h1>Database interface</h1>";
echo '<div class="notify">';
echo "This is an advanced extension to interface with Pandora FMS database directly from WEB console using native SQL sentences. Please note that <b>you can damage</b> your Pandora FMS installation if you don't know </b>exactly</b> what are you doing, this means that you can severily damage your setup using this extension. This extension is intended to be used <b>only by experienced users</b> with a depth knowledgue of Pandora FMS internals.";
echo '</div>';
echo "<h1>Database Interface</h1>"; echo "<br />";
echo "<p>This is an advanced extension to interface with Pandora FMS database directly from WEB console using native SQL sentences. Please note that <b>you can damage</b> your Pandora FMS installation if you don't know </b>exactly</b> what are you doing, this means that you can severily damage your setup using this extension. This extension is intended to be used <b>only by experienced users</b> with a depth knowledgue of Pandora FMS internals.</p>"; echo "Some samples of usage: <blockquote><em>SHOW STATUS;<br />DESCRIBE tagente<br />SELECT * FROM tserver<br />UPDATE tagente SET id_grupo = 15 WHERE nombre LIKE '%194.179%'</em></blockquote>";
echo "<br>";
echo "Some samples of usage: <i><blockquote>SHOW STATUS;<br>DESCRIBE tagente<br>SELECT * FROM tserver<br>UPDATE tagente SET id_grupo = 15 WHERE nombre LIKE '%194.179%'</blockquote></i>";
echo "<br><br>"; echo "<br /><br />";
echo "<form method='post' action=''>"; echo "<form method='post' action=''>";
echo "<textarea class='dbmanager' name='sqlcode'>"; print_textarea ('sql', 5, 50, unsafe_string ($sql));
echo unsafe_string ($sqlcode); echo '<br />';
echo "</textarea>"; echo '<div class="action-buttons" style="width: 100%">';
echo "<br><br>"; print_submit_button (__('Execute SQL'), '', false, 'class="sub next"');
print_submit_button (__('Execute SQL'), '', false, 'class="sub next"',false); echo '</div>';
echo "</form>"; echo "</form>";
// Processing SQL Code // Processing SQL Code
if ($sqlcode != ""){ if ($sql == '')
echo "<br>"; return;
echo "<hr>";
echo "<br>"; echo "<br />";
$result = dbmanager_query ($sqlcode); echo "<hr />";
if (!is_array($result)){ echo "<br />";
echo "<b>Result: <b>".$result;
} $error = '';
else { $result = dbmanager_query ($sql, $error);
$header = "";
$header_printed = 0; if ($result === false) {
echo '<table width=90% class="dbmanager">'; echo '<strong>An error has occured when querying the database.</strong><br />';
foreach ($result as $item => $value){ echo $error;
$data = ""; return;
foreach ($value as $row => $value2){ }
if ($header_printed ==0)
if (!is_numeric($row)) if (! is_array ($result)) {
$header .= "<th class='dbmanager'>" . $row; echo "<strong>Output: <strong>".$result;
if (!is_numeric($row)){ return;
$data .= "<td class='dbmanager'>" . $value2; }
}
} $table->width = '90%';
if ($header_printed == 0){ $table->class = 'dbmanager';
echo $header; $table->head = array_keys ($result[0]);
echo "<tr class='dbmanager'>";
$header_printed = 1; $table->data = $result;
}
echo $data; print_table ($table);
echo "<tr class='dbmanager'>";
}
echo "</table>";
}
}
} }
/* This adds a option in the operation menu */ /* 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 */ /* This sets the function to be called when the extension is selected in the operation menu */
add_extension_godmode_function ('dbmgr_extension_main'); add_extension_godmode_function ('dbmgr_extension_main');

View File

@ -2,29 +2,29 @@
Plain old table styles Plain old table styles
written by Chris Heilmann http://wait-till-i.com 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:1px solid #888;
border-collapse:collapse; border-collapse: collapse;
margin:0; margin: 0;
padding:0; padding: 0;
} }
td.dbmanager,th.dbmanager{
table.dbmanager td, table.dbmanager th {
padding:.2em .5em; padding:.2em .5em;
vertical-align:top; vertical-align: top;
font-weight:normal; font-weight: normal;
background:#fafafa; background: #fafafa;
color:#000; color: #000;
font-size: 9px; font-size: 9px;
} }
th.dbmanager { table.dbmanager th {
color: #fff; color: #fff;
text-transform: uppercase; text-transform: uppercase;
font-size: 8px; font-size: 8px;
background: #888; background: #888;
} }
textarea.dbmanager { textarea.dbmanager {
min-height: 50px; min-height: 50px;
height: 50px; height: 50px;