• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NathanGibbs3 / BASE / 590

pending completion
590

push

travis-ci-com

NathanGibbs3
20230420 Fix CI build breakage. 2

2755 of 16977 relevant lines covered (16.23%)

21.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

19.72
/includes/base_state_query.inc.php
1
<?php
2
// Basic Analysis and Security Engine (BASE)
3
// Copyright (C) 2019-2023 Nathan Gibbs
4
// Copyright (C) 2004 BASE Project Team
5
// Copyright (C) 2000 Carnegie Mellon University
6
//
7
//   For license info: See the file 'base_main.php'
8
//
9
//       Project Lead: Nathan Gibbs
10
// Built upon work by: Kevin Johnson & the BASE Project Team
11
//                     Roman Danyliw <rdd@cert.org>, <roman@danyliw.com>
12
//
13
//            Purpose: Manages necessary state information for query results.
14
//
15
//          Author(s): Nathan Gibbs
16
//                     Kevin Johnson
17
// Ensure the conf file has been loaded.  Prevent direct access to this file.
18
defined( '_BASE_INC' ) or die( 'Accessing this file directly is not allowed.' );
19

20
include_once("$BASE_path/base_common.php");
21
include_once("$BASE_path/includes/base_db.inc.php");
22
include_once("$BASE_path/includes/base_constants.inc.php");
23
include_once("$BASE_path/includes/base_action.inc.php");
24
// include_once("$BASE_path/includes/base_capabilities.php"); //Commented out by Kevin for testing
25

26
class QueryState {
27
        var $canned_query_list = NULL;
28
        var $num_result_rows = -1;
29
        var $current_canned_query = "";
30
        var $current_sort_order = "";
31
        var $current_view = -1;
32
        var $show_rows_on_screen = -1;
33
        var $valid_action_list = array();
34
        var $action;
35
        var $valid_action_op_list = array();
36
        var $action_arg;
37
        var $action_lst;
38
        var $action_chk_lst;
39
        var $action_sql;
40

41
        function __construct(){ // PHP 5+ constructor Shim.
42
                // Class/Method agnostic shim code.
43
                $SCname = get_class();
80✔
44
                if ( method_exists($this, $SCname) ){
80✔
45
                        $SCargs = func_get_args();
80✔
46
                        call_user_func_array(array($this, $SCname), $SCargs);
80✔
47
                }else{
24✔
48
                        // @codeCoverageIgnoreStart
49
                        // Should never execute.
50
                        trigger_error( // Will need to add this message to the TD.
51
                                "Class: $SCname No Legacy Constructor.\n",
52
                                E_USER_ERROR
53
                        );
54
                        // @codeCoverageIgnoreEnd
55
                }
56
        }
56✔
57
        function QueryState(){ // PHP 4x constructor.
58
                $this->ReadState();
80✔
59
                if ( $this->num_result_rows == '' ){
80✔
60
                        $this->num_result_rows = -1;
80✔
61
                }
24✔
62
                if ( $this->current_view == '' ){
80✔
63
                        $this->current_view = -1;
80✔
64
                }
24✔
65
        }
56✔
66
  function AddCannedQuery($caller, $caller_num, $caller_desc, $caller_sort)
67
  {
68
    $this->canned_query_list [$caller] = array($caller_num, $caller_desc, $caller_sort);
×
69
  }
70

71
  function PrintCannedQueryList()
72
  {
73
    echo "<BR><B>"._VALIDCANNED."</B>\n<PRE>\n";
×
74
    print_r($this->canned_query_list);
×
75
    echo "</PRE>\n";
×
76
  }
77

78
  function isCannedQuery()
79
  {
80
    return ( $this->current_canned_query != ""); 
×
81
  }
82

83
  /* returns the name of the current canned query (e.g. "last_tcp") */
84
  function GetCurrentCannedQuery()
85
  {
86
    return $this->current_canned_query;
×
87
  }
88

89
  function GetCurrentCannedQueryCnt()
90
  {
91
    return $this->canned_query_list[$this->current_canned_query][0];
×
92
  }
93

94
  function GetCurrentCannedQueryDesc()
95
  {
96
    return $this->canned_query_list[$this->current_canned_query][0]." ".
×
97
           $this->canned_query_list[$this->current_canned_query][1];
×
98
  }
99

100
  function GetCurrentCannedQuerySort()
101
  {
102
    if ( $this->isCannedQuery() )
×
103
      return $this->canned_query_list[$this->current_canned_query][2];
×
104
    else
105
      return "";
×
106
  }
107

108
  function isValidCannedQuery($potential_caller)
109
  {
110
    if ( $this->canned_query_list == NULL )
×
111
       return false;
×
112

113
    return in_array($potential_caller, array_keys($this->canned_query_list));
×
114
  }
115

116
  function GetCurrentView()
117
  {
118
    return $this->current_view;
×
119
  }
120

121
  function GetCurrentSort()
122
  {
123
    return $this->current_sort_order;
×
124
  }
125

126
  /* returns the number of rows to display for a single screen of the
127
   * query results
128
   */
129
  function GetDisplayRowCnt()
130
  {
131
    return $this->show_rows_on_screen;
×
132
  }
133
        function AddValidAction( $action ){
134
                GLOBAL $archive_exists, $Mail;
42✔
135
                $AAF = 0; // Archive Action Flag
60✔
136
                $MAF = 0; // Mail Action Flag
60✔
137
                if ( preg_match("/^archive_alert(2)?$/", $action) ){
60✔
138
                        $AAF = 1;
60✔
139
                }
18✔
140
                if ( preg_match("/^(csv|email)_alert(2)?$/", $action) ){
60✔
141
                        $MAF = 1;
60✔
142
                }
18✔
143
                $Pass = true;
60✔
144
                if (
145
                        ( $Mail == 0 && $MAF == 1 ) // No Mail
48✔
146
                        || ( $archive_exists == 0 && $AAF == 1 ) // Alert DB.
48✔
147
                        || ( ChkArchive() && $AAF == 1 ) // Archive DB.
60✔
148
                ){
18✔
149
                        $Pass = false;
40✔
150
                }
12✔
151
                if ( $Pass ){
60✔
152
                        $this->valid_action_list[ count($this->valid_action_list) ] = $action;
60✔
153
                }
18✔
154
                return $Pass;
60✔
155
        }
156
  function AddValidActionOp($action_op)
157
  {
158
     $this->valid_action_op_list[ count($this->valid_action_op_list) ] = $action_op;
×
159
  }
160

161
  function SetActionSQL($sql)
162
  {
163
     $this->action_sql = $sql;
×
164
  }
165
        function RunAction($submit, $which_page, $db){
166
        GLOBAL $show_rows, $debug_mode;
167
                if ( IsValidActionOp($submit, $this->valid_action_op_list) ){
×
168
                        ActOnSelectedAlerts(
×
169
                                $this->action, $this->valid_action_list, $submit,
×
170
                                $this->valid_action_op_list, $this->action_arg, $which_page,
×
171
                                $this->action_chk_lst, $this->action_lst, $show_rows,
×
172
                                $this->num_result_rows, $this->action_sql,
×
173
                                $this->current_canned_query, $db
×
174
                        );
175
                        if ( $debug_mode > 0 ){ // Issue #100 fix.
×
176
                                sleep(60);
×
177
                        }
178
                }
179
        }
180
        function GetNumResultRows( $cnt_sql = '', $db = NULL ){
181
                if ( !($this->isCannedQuery()) && ($this->num_result_rows == -1) ){
×
182
                        $this->current_view = 0;
×
183
                        $result = $db->baseExecute($cnt_sql);
×
184
                        if ( $result ){
×
185
                                $rows = $result->baseFetchRow();
×
186
                                $this->num_result_rows = $rows[0];
×
187
                                $result->baseFreeRows();
×
188
                        }else{
189
                                $this->num_result_rows = 0;
×
190
                        }
191
                }else{
192
                        if ( $this->isValidCannedQuery($this->current_canned_query) ){
×
193
                                foreach ( $this->canned_query_list as $key => $val ){
×
194
                                        // Issue #153
195
                                        if ( $this->current_canned_query == $key ){
×
196
                                                $this->current_view = 0;
×
197
                                                $this->num_result_rows = $val[0];
×
198
                                        }
199
                                }
200
                        }
201
                }
202
        }
203
  function MoveView($submit)
204
  {
205
    if ( is_numeric($submit) )
×
206
      $this->current_view = $submit;
×
207
  }
208
        function ExecuteOutputQuery( $sql, $db ){
209
                GLOBAL $show_rows;
210
                if ( $this->isCannedQuery() ){
×
211
                        $RowCnt = $this->GetCurrentCannedQueryCnt();
×
212
                        $Start = 0;
×
213
                }else{
214
                        if ( isset($show_rows) ){
×
215
                                $RowCnt = $show_rows;
×
216
                        }else{ // Issue #5
217
                                $RowCnt = 0;
×
218
                        }
219
                        $Start = $this->current_view * $RowCnt;
×
220
                }
221
                $this->show_rows_on_screen = $RowCnt;
×
222
                return $db->baseExecute($sql, $Start, $RowCnt );
×
223
        }
224
        function PrintResultCnt(){
225
                GLOBAL $show_rows;
226
                $Pfx = NLI("<div style='text-align:center;margin:auto;'>",2);
×
227
                $Sfx = "</div>";
×
228
                if ( $this->num_result_rows != 0 ){
×
229
                        if ( $this->isCannedQuery() ){
×
230
                                print $Pfx._DISPLAYING." ".
×
231
                                $this->GetCurrentCannedQueryDesc().$Sfx;
×
232
                        }else{
233
                                printf( $Pfx._DISPLAYINGTOTAL.$Sfx,
×
234
                  ($this->current_view * $show_rows)+1,
×
235
                  (($this->current_view * $show_rows) + $show_rows-1) < $this->num_result_rows ? 
×
236
                  (($this->current_view * $show_rows) + $show_rows) : $this->num_result_rows, 
×
237
                  $this->num_result_rows);
×
238
                        }
239
                }else{
240
                        print $Pfx.'<b>'._NOALERTS.'</b>'.$Sfx;
×
241
                }
242
        }
243
        function PrintBrowseButtons(){
244
                GLOBAL $show_rows, $max_scroll_buttons;
245
    /* Don't print browsing buttons for canned query */
246
    if ( $this->isCannedQuery() )
×
247
       return;
×
248

249
    if ( ($this->num_result_rows > 0) && ($this->num_result_rows > $show_rows) )
×
250
    {
251
       echo "<!-- Query Result Browsing Buttons -->\n".
252
            "<P><CENTER>\n".
253
            "<TABLE BORDER=1>\n".
254
            "   <TR><TD ALIGN=CENTER>"._QUERYRESULTS."<BR>&nbsp\n";
255

256
        if ( isset($show_rows) ){ // Issue #5
×
257
                $tmp = $show_rows;
×
258
        }else{
259
                $tmp = 1;
×
260
        }
261
        $tmp_num_views = ($this->num_result_rows / $tmp);
×
262
     $tmp_top = $tmp_bottom = $max_scroll_buttons / 2;
×
263

264
     if ( ($this->current_view - ($max_scroll_buttons/2)) >= 0 )
×
265
        $tmp_bottom = $this->current_view - $max_scroll_buttons/2;
×
266
     else
267
        $tmp_bottom = 0;
×
268

269
     if ( ($this->current_view + ($max_scroll_buttons/2)) <= $tmp_num_views )
×
270
        $tmp_top = $this->current_view + $max_scroll_buttons/2;
×
271
     else
272
        $tmp_top = $tmp_num_views;
×
273

274
     /* Show a '<<' symbol of have scrolled beyond the 0 view */
275
     if ( $tmp_bottom != 0 )
×
276
        echo ' << ';
×
277

278
     for ( $i = $tmp_bottom; $i < $tmp_top; $i++)
×
279
     {
280
         if ( $i != $this->current_view )
×
281
            echo '<INPUT TYPE="submit" NAME="submit" VALUE="'.$i.'">'."\n";
×
282
         else
283
            echo '['.$i.'] '."\n";
×
284
     }  
285
    
286
     /* Show a '>>' symbol if last view is not visible */
287
     if ( ($tmp_top) < $tmp_num_views )
×
288
        echo ' >> ';
×
289

290
     echo "  </TD></TR>\n</TABLE>\n</CENTER>\n\n";
×
291
   }
292
        }
293
        function PrintAlertActionButtons(){
294
                if ( count($this->valid_action_list) == 0 ){
×
295
                        return;
×
296
                }
297
    echo "\n\n<!-- Alert Action Buttons -->\n". 
298
         "<CENTER>\n".
299
         " <TABLE BORDER=1>\n".
300
         "  <TR>\n".
301
         "   <TD ALIGN=CENTER>"._ACTION."<BR>\n".
302
         "\n".   
303
         "    <SELECT NAME=\"action\">\n".
304
         '      <OPTION VALUE=" "         '.chk_select($this->action," ").'>'._DISPACTION."\n";
×
305
     
306
                foreach ( $this->valid_action_list as $key => $val ){ // Issue #153
×
307
       echo '    <OPTION VALUE="'.$val.'" '.
×
308
              chk_select($this->action,$val).'>'.
×
309
              GetActionDesc($val)."\n";
×
310
                }
311
    echo "    </SELECT>\n".
312
         "    <INPUT TYPE=\"text\" NAME=\"action_arg\" VALUE=\"".$this->action_arg."\">\n";
×
313

314
                foreach ( $this->valid_action_op_list as $key => $val ){ // Issue #153
×
315
       echo "    <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"".$val."\">\n";
×
316
                }
317
                PrintFramedBoxFooter(1,2);
×
318
    echo "</CENTER>\n\n";
×
319
        }
320
        function ReadState(){
321
     $this->current_canned_query = ImportHTTPVar("caller", VAR_LETTER | VAR_USCORE);
80✔
322
     $this->num_result_rows      = ImportHTTPVar("num_result_rows", VAR_DIGIT | VAR_SCORE);
80✔
323
     $this->current_sort_order   = ImportHTTPVar("sort_order", VAR_LETTER | VAR_USCORE);
80✔
324
     $this->current_view         = ImportHTTPVar("current_view", VAR_DIGIT);
80✔
325
     $this->action_arg           = ImportHTTPVar("action_arg", VAR_ALPHA | VAR_PERIOD | VAR_USCORE | VAR_SCORE | VAR_AT);
80✔
326
     $this->action_chk_lst       = ImportHTTPVar("action_chk_lst", VAR_DIGIT | VAR_PUNC);   /* array */
80✔
327
     $this->action_lst           = ImportHTTPVar("action_lst", VAR_DIGIT | VAR_PUNC | VAR_SCORE);   /* array */
24✔
328
     $this->action               = ImportHTTPVar("action", VAR_ALPHA | VAR_USCORE);
80✔
329
  }
56✔
330

331
  function SaveState()
332
  {
333
     echo "<!-- Saving Query State -->\n";
×
334
     ExportHTTPVar("caller", $this->current_canned_query);
×
335
     ExportHTTPVar("num_result_rows", $this->num_result_rows);
×
336
     // The below line is commented to fix bug #1761605 please verify this doesnt break anything else -- Kevin Johnson
337
     //ExportHTTPVar("sort_order", $this->current_sort_order);
338
     ExportHTTPVar("current_view", $this->current_view);
×
339
  }
340

341
  function SaveStateGET()
342
  {
343
     return "?caller=".$this->current_canned_query.
×
344
            "&amp;num_result_rows=".$this->num_result_rows.
×
345
            "&amp;current_view=".$this->current_view;
×
346
  }
347

348
  function DumpState()
349
  {
350
    echo "<B>"._QUERYSTATE."</B><BR>
×
351
          caller = '$this->current_canned_query'<BR>
×
352
          num_result_rows = '$this->num_result_rows'<BR>
×
353
          sort_order = '$this->current_sort_order'<BR>
×
354
          current_view = '$this->current_view'<BR>
×
355
          action_arg = '$this->action_arg'<BR>
×
356
          action = '$this->action'<BR>";
×
357
  }
358
}
359
?>
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc