os-sim/www/av_asset/common/providers/get_suggestions.php
<?php
/**
 * get_suggestions.php
 *
 * File get_suggestions.php is used to:
 * - Build JSON data that will be returned in response to the Ajax request made by Av_suggestion plugin
 *
 * License:
 *
 * Copyright (c) 2003-2006 ossim.net
 * Copyright (c) 2007-2013 AlienVault
 * All rights reserved.
 *
 * This package is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 dated June, 1991.
 * You may not use, modify or distribute this program under any other version
 * of the GNU General Public License.
 *
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this package; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
 * MA  02110-1301  USA
 *
 *
 * On Debian GNU/Linux systems, the complete text of the GNU General
 * Public License can be found in `/usr/share/common-licenses/GPL-2'.
 *
 * Otherwise you can read it here: http://www.gnu.org/licenses/gpl-2.0.txt
 *
 * @package    ossim-framework\Assets
 * @autor      AlienVault INC
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt
 * @copyright  2003-2006 ossim.net
 * @copyright  2007-2013 AlienVault
 * @link       https://www.alienvault.com/
 */

require_once 'av_init.php';

$asset_id   = POST('asset_id');
$asset_type = POST('asset_type');

Session::logcheck_by_asset_type($asset_type);

// Close session write for real background loading
session_write_close();

if (!valid_hex32($asset_id))
{
    Util::response_bad_request(_('Sorry, asset data was not loaded due to a validation error'));
}

// Check Asset Type
$asset_types = array(
    'asset'   => 'Asset_host',
    'network' => 'Asset_net',
    'group'   => 'Asset_group'
);


try
{

    $db   = new ossim_db();
    $conn = $db->connect();

    if (isset($_POST['asset_id']) && isset($_POST['asset_type']))
    {
        if (!array_key_exists($asset_type, $asset_types))
        {
            Av_exception::throw_error(Av_exception::USER_ERROR, _('Error! Invalid Asset Type'));
        }

        $class_name = $asset_types[$_POST['asset_type']];

        // Check Asset Permission
        if (method_exists($class_name, 'is_allowed') && !$class_name::is_allowed($conn, $asset_id))
        {
            $error = sprintf(_('Error! %s is not allowed'), ucwords($asset_type));

            Av_exception::throw_error(Av_exception::USER_ERROR, $error);
        }

        $asset_object = $class_name::get_object($conn, $asset_id);

        $filters = array(
            'level'       => 'error,warning,info',
            'order_by'    => 'message_level',
            'only_unread' => 'true'
        );

        $pagination = array(
            'page_rows' => 10,
            'page'      => 1
        );

        list($message_list, $total) = $asset_object->get_suggestions($filters, $pagination);
    }
    else
    {
        Av_exception::throw_error(Av_exception::USER_ERROR, _('Error retrieving information'));
    }
}
catch(Exception $e)
{
    $db->close();

    Util::response_bad_request($e->getMessage());
}


// Wiki Parser
$wiki = new Wikiparser();

//DATA
$data = array();

foreach ($message_list as $msg)
{
    $_res = array(
        'component_id'                => $msg['component_id'],
        'component_ip'                => $msg['component_ip'],
        'component_name'              => $msg['component_name'],
        'component_type'              => $msg['component_type'],
        'creation_time'               => $msg['creation_time'],
        'id'                          => $msg['id'],
        'message_action_role'         => $msg['message_action_role'],
        'message_actions'             => $wiki->parse($msg['message_actions']),
        'alternative_actions'         => $wiki->parse($msg['message_alternative_actions']),
        'message_description'         => $wiki->parse($msg['message_description']),
        'message_expire'              => $msg['message_expire'],
        'message_id'                  => $msg['message_id'],
        'message_level'               => $msg['message_level'],
        'message_role'                => $msg['message_role'],
        'message_source'              => $msg['message_source'],
        'message_title'               => $msg['message_title'],
        'message_type'                => $msg['message_type'],
        'suppressed'                  => $msg['suppressed'],
        'suppressed_time'             => $msg['suppressed_time'],
        'viewed'                      => $msg['viewed']
    );

    $data[] = $_res;
}


echo json_encode($data);

$db->close();

/* End of file get_suggestions.php */
/* Location: /av_asset/common/providers/get_suggestions.php */
