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

NathanGibbs3 / BASE / 584

pending completion
584

push

travis-ci-com

NathanGibbs3
20230412 Fix CI build breakage.
         Related Issue(s) #158

2 of 2 new or added lines in 1 file covered. (100.0%)

2594 of 16816 relevant lines covered (15.43%)

20.97 hits per line

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

12.03
/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();
22✔
44
                if ( method_exists($this, $SCname) ){
22✔
45
                        $SCargs = func_get_args();
22✔
46
                        call_user_func_array(array($this, $SCname), $SCargs);
22✔
47
                }else{
8✔
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
        }
16✔
57
        function QueryState(){ // PHP 4x constructor.
58
                $this->ReadState();
22✔
59
                if ( $this->num_result_rows == '' ){
22✔
60
                        $this->num_result_rows = -1;
22✔
61
                }
8✔
62
                if ( $this->current_view == '' ){
22✔
63
                        $this->current_view = -1;
22✔
64
                }
8✔
65
        }
16✔
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
                // Add all actions on Alert DB. Skip Archive action on Archive DB.
135
                if (
136
                        !ChkCookie ('archive', 1)
×
137
                        || !preg_match("/^archive_alert(2)?$/", $action)
×
138
                ){
139
                        $this->valid_action_list[ count($this->valid_action_list) ] = $action;
×
140
                }
141
        }
142
  function AddValidActionOp($action_op)
143
  {
144
     $this->valid_action_op_list[ count($this->valid_action_op_list) ] = $action_op;
×
145
  }
146

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

235
    if ( ($this->num_result_rows > 0) && ($this->num_result_rows > $show_rows) )
×
236
    {
237
       echo "<!-- Query Result Browsing Buttons -->\n".
238
            "<P><CENTER>\n".
239
            "<TABLE BORDER=1>\n".
240
            "   <TR><TD ALIGN=CENTER>"._QUERYRESULTS."<BR>&nbsp\n";
241

242
        if ( isset($show_rows) ){ // Issue #5
×
243
                $tmp = $show_rows;
×
244
        }else{
245
                $tmp = 1;
×
246
        }
247
        $tmp_num_views = ($this->num_result_rows / $tmp);
×
248
     $tmp_top = $tmp_bottom = $max_scroll_buttons / 2;
×
249

250
     if ( ($this->current_view - ($max_scroll_buttons/2)) >= 0 )
×
251
        $tmp_bottom = $this->current_view - $max_scroll_buttons/2;
×
252
     else
253
        $tmp_bottom = 0;
×
254

255
     if ( ($this->current_view + ($max_scroll_buttons/2)) <= $tmp_num_views )
×
256
        $tmp_top = $this->current_view + $max_scroll_buttons/2;
×
257
     else
258
        $tmp_top = $tmp_num_views;
×
259

260
     /* Show a '<<' symbol of have scrolled beyond the 0 view */
261
     if ( $tmp_bottom != 0 )
×
262
        echo ' << ';
×
263

264
     for ( $i = $tmp_bottom; $i < $tmp_top; $i++)
×
265
     {
266
         if ( $i != $this->current_view )
×
267
            echo '<INPUT TYPE="submit" NAME="submit" VALUE="'.$i.'">'."\n";
×
268
         else
269
            echo '['.$i.'] '."\n";
×
270
     }  
271
    
272
     /* Show a '>>' symbol if last view is not visible */
273
     if ( ($tmp_top) < $tmp_num_views )
×
274
        echo ' >> ';
×
275

276
     echo "  </TD></TR>\n</TABLE>\n</CENTER>\n\n";
×
277
   }
278
        }
279
        function PrintAlertActionButtons(){
280
                if ( count($this->valid_action_list) == 0 ){
×
281
                        return;
×
282
                }
283
    echo "\n\n<!-- Alert Action Buttons -->\n". 
284
         "<CENTER>\n".
285
         " <TABLE BORDER=1>\n".
286
         "  <TR>\n".
287
         "   <TD ALIGN=CENTER>"._ACTION."<BR>\n".
288
         "\n".   
289
         "    <SELECT NAME=\"action\">\n".
290
         '      <OPTION VALUE=" "         '.chk_select($this->action," ").'>'._DISPACTION."\n";
×
291
     
292
                foreach ( $this->valid_action_list as $key => $val ){ // Issue #153
×
293
       echo '    <OPTION VALUE="'.$val.'" '.
×
294
              chk_select($this->action,$val).'>'.
×
295
              GetActionDesc($val)."\n";
×
296
                }
297
    echo "    </SELECT>\n".
298
         "    <INPUT TYPE=\"text\" NAME=\"action_arg\" VALUE=\"".$this->action_arg."\">\n";
×
299

300
                foreach ( $this->valid_action_op_list as $key => $val ){ // Issue #153
×
301
       echo "    <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"".$val."\">\n";
×
302
                }
303
                PrintFramedBoxFooter(1,2);
×
304
    echo "</CENTER>\n\n";
×
305
        }
306
        function ReadState(){
307
     $this->current_canned_query = ImportHTTPVar("caller", VAR_LETTER | VAR_USCORE);
22✔
308
     $this->num_result_rows      = ImportHTTPVar("num_result_rows", VAR_DIGIT | VAR_SCORE);
22✔
309
     $this->current_sort_order   = ImportHTTPVar("sort_order", VAR_LETTER | VAR_USCORE);
22✔
310
     $this->current_view         = ImportHTTPVar("current_view", VAR_DIGIT);
22✔
311
     $this->action_arg           = ImportHTTPVar("action_arg", VAR_ALPHA | VAR_PERIOD | VAR_USCORE | VAR_SCORE | VAR_AT);
22✔
312
     $this->action_chk_lst       = ImportHTTPVar("action_chk_lst", VAR_DIGIT | VAR_PUNC);   /* array */
22✔
313
     $this->action_lst           = ImportHTTPVar("action_lst", VAR_DIGIT | VAR_PUNC | VAR_SCORE);   /* array */
6✔
314
     $this->action               = ImportHTTPVar("action", VAR_ALPHA | VAR_USCORE);
22✔
315
  }
16✔
316

317
  function SaveState()
318
  {
319
     echo "<!-- Saving Query State -->\n";
×
320
     ExportHTTPVar("caller", $this->current_canned_query);
×
321
     ExportHTTPVar("num_result_rows", $this->num_result_rows);
×
322
     // The below line is commented to fix bug #1761605 please verify this doesnt break anything else -- Kevin Johnson
323
     //ExportHTTPVar("sort_order", $this->current_sort_order);
324
     ExportHTTPVar("current_view", $this->current_view);
×
325
  }
326

327
  function SaveStateGET()
328
  {
329
     return "?caller=".$this->current_canned_query.
×
330
            "&amp;num_result_rows=".$this->num_result_rows.
×
331
            "&amp;current_view=".$this->current_view;
×
332
  }
333

334
  function DumpState()
335
  {
336
    echo "<B>"._QUERYSTATE."</B><BR>
×
337
          caller = '$this->current_canned_query'<BR>
×
338
          num_result_rows = '$this->num_result_rows'<BR>
×
339
          sort_order = '$this->current_sort_order'<BR>
×
340
          current_view = '$this->current_view'<BR>
×
341
          action_arg = '$this->action_arg'<BR>
×
342
          action = '$this->action'<BR>";
×
343
  }
344
}
345
?>
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