From 7c4571ac2533d81df91231f52a43cf0a75b7192c Mon Sep 17 00:00:00 2001 From: mdtrooper Date: Mon, 2 Apr 2012 15:57:48 +0000 Subject: [PATCH] 2012-04-02 Miguel de Dios * include/functions_html.php: added into function "html_debug_print" the equal output to log file, this is with more data. * include/functions_db.php: cleaned source code style. * extensions/update_pandora/functions.ajax.php, extensions/update_pandora/functions.php, extensions/update_manager/lib/libupdate_manager.php, extensions/update_pandora.php: added extension to update pandora from the targz of Sourceforge. Merged from the branch pandora_4.0 git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@5874 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f --- pandora_console/ChangeLog | 15 + pandora_console/extensions/update_pandora.php | 184 ++++++++ .../update_pandora/functions.ajax.php | 233 +++++++++++ .../extensions/update_pandora/functions.php | 393 ++++++++++++++++++ pandora_console/include/functions_db.php | 2 +- pandora_console/include/functions_html.php | 33 +- 6 files changed, 845 insertions(+), 15 deletions(-) create mode 100644 pandora_console/extensions/update_pandora.php create mode 100644 pandora_console/extensions/update_pandora/functions.ajax.php create mode 100644 pandora_console/extensions/update_pandora/functions.php diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 63b3e94747..2c13076113 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,18 @@ +2012-04-02 Miguel de Dios + + * include/functions_html.php: added into function "html_debug_print" the + equal output to log file, this is with more data. + + * include/functions_db.php: cleaned source code style. + + * extensions/update_pandora/functions.ajax.php, + extensions/update_pandora/functions.php, + extensions/update_manager/lib/libupdate_manager.php, + extensions/update_pandora.php: added extension to update pandora from the + targz of Sourceforge. + + Merged from the branch pandora_4.0 + 2012-04-02 Dario Rodriguez * operation/menu.php: Fixed an error selecting the correct menu option diff --git a/pandora_console/extensions/update_pandora.php b/pandora_console/extensions/update_pandora.php new file mode 100644 index 0000000000..8abbd8cfd5 --- /dev/null +++ b/pandora_console/extensions/update_pandora.php @@ -0,0 +1,184 @@ +" . __('Downloaded Packages') . ""; + $list_downloaded_packages = update_pandora_get_list_downloaded_packages('operation'); + $table = null; + $table->width = '80%'; + $table->head = array(__('Packages')); + $table->data = $list_downloaded_packages; + html_print_table($table); + + echo "

" . __('Online Packages') . "

"; + $table = null; + $table->id = 'online_packages'; + $table->width = '80%'; + $table->head = array(__('Packages')); + $table->rowclass[0] = 'spinner_row'; + $table->data[0][0] = __('Get list online Packages') . html_print_image('images/spinner.gif', true); + html_print_table($table); + + update_pandora_print_javascript(); +} + +function update_pandora_administration() { + global $conf_update_pandora; + + ui_print_page_header(__('Update Pandora'), "images/extensions.png", false, "", true); + + if (!update_pandora_check_installation()) { + ui_print_error_message(__('First execution of Update Pandora')); + update_pandora_installation(); + } + + $conf_update_pandora = update_pandora_get_conf(); + + echo "

" . __('Downloaded Packages') . "

"; + $list_downloaded_packages = update_pandora_get_list_downloaded_packages('administration'); + $table = null; + $table->width = '80%'; + $table->size = array('80%', '50px'); + $table->head = array(__('Packages'), __('Action')); + $table->align = array('left', 'center'); + $table->data = array(); + foreach ($list_downloaded_packages as $package) { + $actions = ''; + if (!isset($package['empty'])) { + if (!$package['current']) { + $actions = '' . + html_print_image('images/b_white.png', true, array('alt'=> + __('Install this version'), 'title' => __('Install this version'))) . + ''; + } + else { + $actions = '' . + html_print_image('images/b_yellow.png', true, array('alt'=> + __('Reinstall this version'), 'title' => __('Reinstall this version'))) . + ''; + } + } + $table->data[] = array($package['name'], $actions); + } + html_print_table($table); + + echo "

" . __('Online Package') . "

"; + + echo ''; + echo ' + + + '; + echo ' + + + + + '; + echo '
' . __('Package') . '' . __('Action') . '
' . + __('Get list online Package') . " " . html_print_image('images/spinner.gif', true) . + '
'; + + + ?> + + diff --git a/pandora_console/extensions/update_pandora/functions.ajax.php b/pandora_console/extensions/update_pandora/functions.ajax.php new file mode 100644 index 0000000000..5d20708440 --- /dev/null +++ b/pandora_console/extensions/update_pandora/functions.ajax.php @@ -0,0 +1,233 @@ + 0); + $return['last'] = $last; + $return['correct'] = 1; + $return['package'] = uniqid(); + $return['end'] = 0; + if ($last == 1) { + $return['end'] = 1; + } + + echo json_encode($return); + //////////////////////////////////////////////////////////////////// +} + +function update_pandora_download_package() { + global $config; + + $dir = $config['attachment_store'] . '/update_pandora/'; + + $package = get_parameter('package', ''); + + //TODO Make all code, at the moment. + $url = 'http://sourceforge.net/projects/pandora/files/Pandora%20FMS%204.0.1/Pandora_FMS_4.0.1_OpenSource.i686-0.0.3.vmx.tar.gz/download'; + $url = 'http://sourceforge.net/projects/pandora/files/Nightly/Tarball/pandorafms_console-4.1dev-120330.tar.gz/download'; + $url = 'http://sourceforge.net/projects/pandora/files/Nightly/Tarball/pandorafms_server-4.1dev-120330.tar.gz/download'; + preg_match_all('/Tarball\/(.*.tar.gz)/i', $url, $targz); + $targz = $targz[1][0]; + + $url = 'http://127.0.0.1/test000.tar.gz'; + $url = 'http://127.0.0.1/test001.tar.gz'; + $targz = 'test001.tar.gz'; + //////////////////////////////////////////////////////////////////// + $file = fopen($dir . $targz, "w"); + + $mch = curl_multi_init(); + $c = curl_init(); + + curl_setopt($c, CURLOPT_URL, $url); + curl_setopt($c, CURLOPT_RETURNTRANSFER, true); + curl_setopt($c, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($c, CURLOPT_FILE, $file); + + curl_multi_add_handle($mch ,$c); + $running = null; + do { + curl_multi_exec($mch ,$running); + $info = curl_getinfo ($c); + + $data = array(); + $data['filename'] = $targz; + $data['size'] = $info['download_content_length']; + $data['size_download'] = $info['size_download']; + $data['speed_download'] = $info['speed_download']; + + $info_json = json_encode($data); + + file_put_contents('/tmp/' . $package . '.info.txt', $info_json, LOCK_EX); + + sleep(1); + } + while($running > 0); + + + $return = array('correct' => 1); + echo json_encode($return); +} + +function update_pandora_check_download_package() { + global $config; + + require_once ($config["homedir"] . '/include/functions_graph.php'); + + sleep(1); + + $package = get_parameter('package', ''); + $return = array('correct' => 1, + 'info_download' => "Size: %s/%s bytes Speed: %s bytes/second", + 'progres_bar' => progress_bar(0, 300, 20, '0%', 1, false, "#00ff00"), + 'progres_bar_text' => '0%', + 'percent' => 0); + + $info_json = @file_get_contents('/tmp/' . $package . '.info.txt'); + + $info = json_decode($info_json, true); + + $percent = 0; + $size_download = 0; + $size = 0; + $speed_download = 0; + if ($info['size_download'] > 0) { + $percent = format_numeric( + ($info['size_download'] / $info['size']) * 100, 2); + $return['percent'] = $percent; + $size_download = $info['size_download']; + $size = $info['size']; + $speed_download = $info['speed_download']; + + $return['info_download'] = sprintf($return['info_download'], + format_for_graph($size_download, 2), format_for_graph($size, 2), + format_for_graph($speed_download, 2)); + } + else { + $return['info_download'] = __('Starting: connect to server'); + } + + $img = progress_bar($percent, 300, 20, $percent . '%', 1, false, "#00ff00"); + $return['progres_bar'] = $img; + preg_match_all('/src=[\'\"]([^\"^\']*)[\'\"]/i', $img, $attr); + $return['progres_bar_src'] = $attr[1]; + preg_match_all('/alt=[\'\"]([^\"^\']*)[\'\"]/i', $img, $attr); + $return['progres_bar_alt'] = $attr[1]; + preg_match_all('/title=[\'\"]([^\"^\']*)[\'\"]/i', $img, $attr); + $return['progres_bar_title'] = $attr[1]; + $return['filename'] = $info['filename']; + + echo json_encode($return); +} + +function update_pandora_install_package() { + global $config; + global $conf_update_pandora; + if (empty($conf_update_pandora)) + $conf_update_pandora = update_pandora_get_conf(); + + $dir = $config['attachment_store'] . '/update_pandora/'; + + $package = get_parameter('package', ''); + $filename = get_parameter('filename', ''); + + //Get total files + $command = 'tar tzvf ' . $dir . $filename . '| wc -l > /tmp/' . $package . '.info.txt'; + exec($command, $output, $status); + html_debug_print($command, true); + + $command = 'tar xzvf ' . $dir . $filename . ' -C ' . $config['homedir'] . ' 1>/tmp/' . $package . '.files.info.txt'; + html_debug_print($command, true); + + //Maybe this line run for seconds or minutes + exec($command, $output, $status); + + if (($status == 0) || ($status == 2)) { + $conf_update_pandora['last_installed'] = $filename; + update_pandora_update_conf(); + echo json_encode(array('correct' => 1)); + } + else { + echo json_encode(array('correct' => 0)); + } +} + +function update_pandora_check_install_package() { + global $config; + + require_once ($config["homedir"] . '/include/functions_graph.php'); + + sleep(1); + + $package = get_parameter('package', ''); + $filename = get_parameter('filename', ''); + + //TODO MAYBE THE FILE OF FILES DON'T EXIST AT THE MOMENT + + $files = @file('/tmp/' . $package . '.files.info.txt'); + if (empty($files)) + $files = array(); + $total = (int)file_get_contents('/tmp/' . $package . '.info.txt'); + + $return = array('correct' => 1, + 'info' => "
%s
", + 'src' => progress_bar(0, 300, 20, '0%', 1, false, "#0000ff"), + 'alt' => '0%', + 'percent' => 0); + + $percent = 0; + if ((count($files) > 0) && ($total > 0)) { + $percent = format_numeric((count($files) / $total) * 100, 2); + if ($percent > 100) + $percent = 100; + } + + $files_txtbox = (string)implode("
", $files); + $return['info'] = sprintf($return['info'], $files_txtbox); + $img = progress_bar($percent, 300, 20, $percent . '%', 1, false, "#0000ff"); + $return['percent'] = $percent; + preg_match_all('/src=[\'\"]([^\"^\']*)[\'\"]/i', $img, $attr); + $return['src'] = $attr[1]; + preg_match_all('/alt=[\'\"]([^\"^\']*)[\'\"]/i', $img, $attr); + $return['alt'] = $attr[1]; + preg_match_all('/title=[\'\"]([^\"^\']*)[\'\"]/i', $img, $attr); + $return['title'] = $attr[1]; + + if ($percent == 100) { + unlink('/tmp/' . $package . '.files.info.txt'); + unlink('/tmp/' . $package . '.info.txt'); + } + + echo json_encode($return); +} + +?> diff --git a/pandora_console/extensions/update_pandora/functions.php b/pandora_console/extensions/update_pandora/functions.php new file mode 100644 index 0000000000..d00b81bba0 --- /dev/null +++ b/pandora_console/extensions/update_pandora/functions.php @@ -0,0 +1,393 @@ + 'update_pandora_conf_url')); + + if (empty($row)) { + //The url of update manager. + $conf_update_pandora = array('url' => 'http://192.168.70.213/pandora.tar.gz', + 'last_installed' => '', + 'last_contact' => ''); + + $values = array('token' => 'update_pandora_conf_url', + 'value' => $conf_update_pandora['url']); + $return = db_process_sql_insert('tconfig', $values); + $values = array('token' => 'update_pandora_conf_last_installed', + 'value' => $conf_update_pandora['last_installed']); + $return = db_process_sql_insert('tconfig', $values); + $values = array('token' => 'update_pandora_conf_last_contact', + 'value' => $conf_update_pandora['last_contact']); + $return = db_process_sql_insert('tconfig', $values); + + ui_print_result_message($return, __('Succesful store conf data in DB.'), + __('Unsuccesful store conf data in DB.')); + } + else { + ui_print_message(__('Conf data have been in the DB.')); + } + + $dir = $config['attachment_store'] . '/update_pandora/'; + if (!is_dir($dir)) { + $result = mkdir($dir); + + ui_print_result_message($result, __('Succesful create a dir to save package in Pandora Console'), + __('Unsuccesful create a dir to save package in Pandora Console')); + } + else { + ui_print_message(__('The directory for save package have been in Pandora Console.')); + } +} + +function update_pandora_update_conf() { + global $config; + global $conf_update_pandora; + + $values = array('value' => $conf_update_pandora['last_installed']); + $return = db_process_sql_update('tconfig', $values, + array('token' => 'update_pandora_conf_last_installed')); + $values = array('value' => $conf_update_pandora['last_contact']); + $return = db_process_sql_insert('tconfig', $values, + array('token' => 'update_pandora_conf_last_contact')); + + return $return; +} + +function update_pandora_get_list_downloaded_packages($mode = 'operation') { + global $config; + global $conf_update_pandora; + + $dir = dir($conf_update_pandora['dir']); + + $packages = array(); + while (false !== ($entry = $dir->read())) { + if (is_file($conf_update_pandora['dir'] . $entry) + && is_readable($conf_update_pandora['dir'] . $entry)) { + if (strstr($entry, '.tar.gz') !== false) { + + if ($mode == 'operation') { + $packages[] = $entry; + } + else { + if ($conf_update_pandora['last_installed'] == $entry) { + $packages[] = array('name' => $entry, + 'current' => true); + } + else { + $packages[] = array('name' => $entry, + 'current' => false); + } + + + } + } + } + } + + if (empty($packages)) { + if ($mode == 'operation') { + $packages[] = array('name' => + __('There are not downloaded packages in your Pandora Console.')); + } + else { + $packages[] = array('empty' => true, 'name' => + __('There are not downloaded packages in your Pandora Console.')); + } + } + + return $packages; +} + +function update_pandora_print_javascript() { + $extension_php_file = 'extensions/update_pandora'; + + ?> + + + + diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 1d360a3e2a..772c46dce7 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -28,7 +28,7 @@ include_once($config['homedir'] . '/include/functions_users.php'); function db_select_engine() { global $config; - + switch ($config["dbtype"]) { case "mysql": require_once ($config['homedir'] . '/include/db/mysql.php'); diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 2fad48514a..be4c68b536 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -40,25 +40,27 @@ require_once ($config['homedir'].'/include/functions_ui.php'); * Prints the print_r with < pre > tags */ function html_debug_print ($var, $file = '') { + $more_info = ''; + if (is_string($var)) { + $more_info = 'size: ' . strlen($var); + } + elseif (is_bool($var)) { + $more_info = 'val: ' . + ($var ? 'true' : 'false'); + } + elseif (is_null($var)) { + $more_info = 'is null'; + } + elseif (is_array($var)) { + $more_info = count($var); + } + if ($file === true) $file = '/tmp/logDebug'; + if (strlen($file) > 0) { $f = fopen($file, "a"); ob_start(); - $more_info = ''; - if (is_string($var)) { - $more_info = 'size: ' . strlen($var); - } - elseif (is_bool($var)) { - $more_info = 'val: ' . - ($var ? 'true' : 'false'); - } - elseif (is_null($var)) { - $more_info = 'is null'; - } - elseif (is_array($var)) { - $more_info = count($var); - } echo date("Y/m/d H:i:s") . " (" . gettype($var) . ") " . $more_info . "\n"; print_r($var); echo "\n\n"; @@ -67,6 +69,9 @@ function html_debug_print ($var, $file = '') { fclose($f); } else { + echo "
" .
+			date("Y/m/d H:i:s") . " (" . gettype($var) . ") " . $more_info .
+			"
"; echo "
";print_r($var);echo "
"; } }