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

NathanGibbs3 / BASE / 624

pending completion
624

push

travis-ci-com

NathanGibbs3
Merge branch 'devel'

562 of 562 new or added lines in 28 files covered. (100.0%)

3145 of 17504 relevant lines covered (17.97%)

23.22 hits per line

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

33.54
/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/includes/base_db.inc.php");
21
include_once("$BASE_path/includes/base_constants.inc.php");
22
include_once("$BASE_path/includes/base_action.inc.php");
23

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

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

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

76
        function isCannedQuery(){
77
                $Ret = false;
462✔
78
                if (
79
                        !is_null($this->canned_query_list)
462✔
80
                        && LoadedString($this->current_canned_query)
462✔
81
                ){
168✔
82
                        $Ret = true;
154✔
83
                }
56✔
84
                return $Ret;
462✔
85
        }
86

87
  /* returns the name of the current canned query (e.g. "last_tcp") */
88
  function GetCurrentCannedQuery()
89
  {
90
    return $this->current_canned_query;
×
91
  }
92

93
        function GetCurrentCannedQueryCnt(){
94
                $Ret = 0;
66✔
95
                if ( $this->isCannedQuery() == true ){
66✔
96
                        $Ret = $this->canned_query_list[$this->current_canned_query][0];
22✔
97
                }
8✔
98
                return $Ret;
66✔
99
        }
100

101
        function GetCurrentCannedQueryDesc(){
102
                $Ret = '';
66✔
103
                if ( $this->isCannedQuery() == true ){
66✔
104
                        $Ret = $this->canned_query_list[$this->current_canned_query][0]
22✔
105
                        . ' '. $this->canned_query_list[$this->current_canned_query][1];
22✔
106
                }
8✔
107
                return $Ret;
66✔
108
        }
109

110
        function GetCurrentCannedQuerySort(){
111
                $Ret = '';
66✔
112
                if ( $this->isCannedQuery() == true ){
66✔
113
                        $Ret = $this->canned_query_list[$this->current_canned_query][2];
22✔
114
                }
8✔
115
                return $Ret;
66✔
116
        }
117

118
        function isValidCannedQuery( $caller = '' ){
119
                $Ret = false;
198✔
120
                if ( $this->isCannedQuery() == true && LoadedString($caller) ){
198✔
121
                        $Ret = in_array($caller, array_keys($this->canned_query_list));
22✔
122
                }
8✔
123
                return $Ret;
198✔
124
        }
125

126
  function GetCurrentView()
127
  {
128
    return $this->current_view;
×
129
  }
130

131
  function GetCurrentSort()
132
  {
133
    return $this->current_sort_order;
×
134
  }
135

136
  /* returns the number of rows to display for a single screen of the
137
   * query results
138
   */
139
  function GetDisplayRowCnt()
140
  {
141
    return $this->show_rows_on_screen;
×
142
  }
143
        function AddValidAction( $action ){
144
                GLOBAL $archive_exists, $BCR, $BASE_VERSION, $BASE_installID;
48✔
145
                if (
146
                        !getenv('TRAVIS')
66✔
147
                        && !(
24✔
148
                                $BASE_VERSION == '0.0.0 (Joette)'
149
                                && $BASE_installID == 'Test Runner'
30✔
150
                        )
151
                ){ // Production
24✔
152
                        $Archive = $BCR->GetCap('BASE_ADB');
×
153
                        $Mail = $BCR->GetCap('Mail');
×
154
                }else{ // Test Conditions
155
                        $Mail = true;
66✔
156
                }
157
                $AAF = 0; // Archive Action Flag
66✔
158
                $MAF = 0; // Mail Action Flag
66✔
159
                if ( preg_match("/^archive_alert(2)?$/", $action) ){
66✔
160
                        $AAF = 1;
66✔
161
                }
24✔
162
                if ( preg_match("/^(csv|email)_alert(2)?$/", $action) ){
66✔
163
                        $MAF = 1;
66✔
164
                }
24✔
165
                $Pass = true;
66✔
166
                if (
167
                        ( !$Mail && $MAF == 1 ) // No Mail
54✔
168
                        || ( $archive_exists == 0 && $AAF == 1 ) // Alert DB.
54✔
169
                        || ( ChkArchive() && $AAF == 1 ) // Archive DB.
66✔
170
                ){
24✔
171
                        $Pass = false;
44✔
172
                }
16✔
173
                if ( $Pass ){
66✔
174
                        $this->valid_action_list[ count($this->valid_action_list) ] = $action;
66✔
175
                }
24✔
176
                return $Pass;
66✔
177
        }
178
  function AddValidActionOp($action_op)
179
  {
180
     $this->valid_action_op_list[ count($this->valid_action_op_list) ] = $action_op;
×
181
  }
182

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

271
    if ( ($this->num_result_rows > 0) && ($this->num_result_rows > $show_rows) )
×
272
    {
273
       echo "<!-- Query Result Browsing Buttons -->\n".
274
            "<P><CENTER>\n".
275
            "<TABLE BORDER=1>\n".
276
            "   <TR><TD ALIGN=CENTER>"._QUERYRESULTS."<BR>&nbsp\n";
277

278
        if ( isset($show_rows) ){ // Issue #5
×
279
                $tmp = $show_rows;
×
280
        }else{
281
                $tmp = 1;
×
282
        }
283
        $tmp_num_views = ($this->num_result_rows / $tmp);
×
284
     $tmp_top = $tmp_bottom = $max_scroll_buttons / 2;
×
285

286
     if ( ($this->current_view - ($max_scroll_buttons/2)) >= 0 )
×
287
        $tmp_bottom = $this->current_view - $max_scroll_buttons/2;
×
288
     else
289
        $tmp_bottom = 0;
×
290

291
     if ( ($this->current_view + ($max_scroll_buttons/2)) <= $tmp_num_views )
×
292
        $tmp_top = $this->current_view + $max_scroll_buttons/2;
×
293
     else
294
        $tmp_top = $tmp_num_views;
×
295

296
     /* Show a '<<' symbol of have scrolled beyond the 0 view */
297
     if ( $tmp_bottom != 0 )
×
298
        echo ' << ';
×
299

300
     for ( $i = $tmp_bottom; $i < $tmp_top; $i++)
×
301
     {
302
         if ( $i != $this->current_view )
×
303
            echo '<INPUT TYPE="submit" NAME="submit" VALUE="'.$i.'">'."\n";
×
304
         else
305
            echo '['.$i.'] '."\n";
×
306
     }  
307
    
308
     /* Show a '>>' symbol if last view is not visible */
309
     if ( ($tmp_top) < $tmp_num_views )
×
310
        echo ' >> ';
×
311

312
     echo "  </TD></TR>\n</TABLE>\n</CENTER>\n\n";
×
313
   }
314
        }
315
        function PrintAlertActionButtons(){
316
                if ( count($this->valid_action_list) == 0 ){
×
317
                        return;
×
318
                }
319
    echo "\n\n<!-- Alert Action Buttons -->\n". 
320
         "<CENTER>\n".
321
         " <TABLE BORDER=1>\n".
322
         "  <TR>\n".
323
         "   <TD ALIGN=CENTER>"._ACTION."<BR>\n".
324
         "\n".   
325
         "    <SELECT NAME=\"action\">\n".
326
         '      <OPTION VALUE=" "         '.chk_select($this->action," ").'>'._DISPACTION."\n";
×
327
     
328
                foreach ( $this->valid_action_list as $key => $val ){ // Issue #153
×
329
       echo '    <OPTION VALUE="'.$val.'" '.
×
330
              chk_select($this->action,$val).'>'.
×
331
              GetActionDesc($val)."\n";
×
332
                }
333
    echo "    </SELECT>\n".
334
         "    <INPUT TYPE=\"text\" NAME=\"action_arg\" VALUE=\"".$this->action_arg."\">\n";
×
335

336
                foreach ( $this->valid_action_op_list as $key => $val ){ // Issue #153
×
337
       echo "    <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"".$val."\">\n";
×
338
                }
339
                PrintFramedBoxFooter(1,2);
×
340
    echo "</CENTER>\n\n";
×
341
        }
342
        function ReadState(){
343
     $this->current_canned_query = ImportHTTPVar("caller", VAR_LETTER | VAR_USCORE);
22✔
344
     $this->num_result_rows      = ImportHTTPVar("num_result_rows", VAR_DIGIT | VAR_SCORE);
22✔
345
     $this->current_sort_order   = ImportHTTPVar("sort_order", VAR_LETTER | VAR_USCORE);
22✔
346
     $this->current_view         = ImportHTTPVar("current_view", VAR_DIGIT);
22✔
347
     $this->action_arg           = ImportHTTPVar("action_arg", VAR_ALPHA | VAR_PERIOD | VAR_USCORE | VAR_SCORE | VAR_AT);
22✔
348
     $this->action_chk_lst       = ImportHTTPVar("action_chk_lst", VAR_DIGIT | VAR_PUNC);   /* array */
22✔
349
     $this->action_lst           = ImportHTTPVar("action_lst", VAR_DIGIT | VAR_PUNC | VAR_SCORE);   /* array */
6✔
350
     $this->action               = ImportHTTPVar("action", VAR_ALPHA | VAR_USCORE);
22✔
351
  }
16✔
352

353
  function SaveState()
354
  {
355
     echo "<!-- Saving Query State -->\n";
×
356
     ExportHTTPVar("caller", $this->current_canned_query);
×
357
     ExportHTTPVar("num_result_rows", $this->num_result_rows);
×
358
     // The below line is commented to fix bug #1761605 please verify this doesnt break anything else -- Kevin Johnson
359
     //ExportHTTPVar("sort_order", $this->current_sort_order);
360
     ExportHTTPVar("current_view", $this->current_view);
×
361
  }
362

363
  function SaveStateGET()
364
  {
365
     return "?caller=".$this->current_canned_query.
×
366
            "&amp;num_result_rows=".$this->num_result_rows.
×
367
            "&amp;current_view=".$this->current_view;
×
368
  }
369

370
  function DumpState()
371
  {
372
    echo "<B>"._QUERYSTATE."</B><BR>
×
373
          caller = '$this->current_canned_query'<BR>
×
374
          num_result_rows = '$this->num_result_rows'<BR>
×
375
          sort_order = '$this->current_sort_order'<BR>
×
376
          current_view = '$this->current_view'<BR>
×
377
          action_arg = '$this->action_arg'<BR>
×
378
          action = '$this->action'<BR>";
×
379
  }
380
}
381
?>
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