Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/include/txp_log.php - 368 lines - 11303 bytes - Summary - Text - Print

Description: Visitor logs panel.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * https://textpattern.com/
   6   *
   7   * Copyright (C) 2020 The Textpattern Development Team
   8   *
   9   * This file is part of Textpattern.
  10   *
  11   * Textpattern is free software; you can redistribute it and/or
  12   * modify it under the terms of the GNU General Public License
  13   * as published by the Free Software Foundation, version 2.
  14   *
  15   * Textpattern is distributed in the hope that it will be useful,
  16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18   * GNU General Public License for more details.
  19   *
  20   * You should have received a copy of the GNU General Public License
  21   * along with Textpattern. If not, see <https://www.gnu.org/licenses/>.
  22   */
  23  
  24  /**
  25   * Visitor logs panel.
  26   *
  27   * @package Admin\Log
  28   */
  29  
  30  use Textpattern\Search\Filter;
  31  
  32  if (!defined('txpinterface')) {
  33      die('txpinterface is undefined.');
  34  }
  35  
  36  if ($event == 'log') {
  37      if (get_pref('logging') === 'none' || !intval(get_pref('expire_logs_after'))) {
  38          require_privs();
  39      }
  40  
  41      require_privs('log');
  42  
  43      $available_steps = array(
  44          'log_list'          => false,
  45          'log_change_pageby' => true,
  46          'log_multi_edit'    => true,
  47      );
  48  
  49      if ($step && bouncer($step, $available_steps)) {
  50          $step();
  51      } else {
  52          log_list();
  53      }
  54  }
  55  
  56  /**
  57   * The main panel listing all log hits.
  58   *
  59   * @param string|array $message The activity message
  60   */
  61  
  62  function log_list($message = '')
  63  {
  64      global $event, $expire_logs_after;
  65  
  66      pagetop(gTxt('tab_logs'), $message);
  67  
  68      extract(gpsa(array(
  69          'page',
  70          'sort',
  71          'dir',
  72          'crit',
  73          'search_method',
  74      )));
  75  
  76      if ($sort === '') {
  77          $sort = get_pref('log_sort_column', 'time');
  78      } else {
  79          if (!in_array($sort, array('page', 'refer', 'method', 'status'))) {
  80              $sort = 'time';
  81          }
  82  
  83          set_pref('log_sort_column', $sort, 'log', PREF_HIDDEN, '', 0, PREF_PRIVATE);
  84      }
  85  
  86      if ($dir === '') {
  87          $dir = get_pref('log_sort_dir', 'desc');
  88      } else {
  89          $dir = ($dir == 'asc') ? "asc" : "desc";
  90          set_pref('log_sort_dir', $dir, 'log', PREF_HIDDEN, '', 0, PREF_PRIVATE);
  91      }
  92  
  93      $expire_logs_after = assert_int($expire_logs_after);
  94  
  95      safe_delete('txp_log', "time < DATE_SUB(NOW(), INTERVAL $expire_logs_after DAY)");
  96  
  97      switch ($sort) {
  98          case 'page':
  99              $sort_sql = "page $dir";
 100              break;
 101          case 'refer':
 102              $sort_sql = "refer $dir";
 103              break;
 104          case 'method':
 105              $sort_sql = "method $dir";
 106              break;
 107          case 'status':
 108              $sort_sql = "status $dir";
 109              break;
 110          default:
 111              $sort = 'time';
 112              $sort_sql = "time $dir";
 113              break;
 114      }
 115  
 116      $switch_dir = ($dir == 'desc') ? 'asc' : 'desc';
 117  
 118      $search = new Filter(
 119          $event,
 120          array(
 121              'time' => array(
 122                  'column' => 'txp_log.time',
 123                  'label'  => gTxt('time'),
 124              ),
 125              'page' => array(
 126                  'column' => 'txp_log.page',
 127                  'label'  => gTxt('page'),
 128              ),
 129              'refer' => array(
 130                  'column' => 'txp_log.refer',
 131                  'label'  => gTxt('referrer'),
 132              ),
 133              'method' => array(
 134                  'column' => 'txp_log.method',
 135                  'label'  => gTxt('method'),
 136              ),
 137              'status' => array(
 138                  'column' => 'txp_log.status',
 139                  'label'  => gTxt('status'),
 140                  'type'   => 'integer',
 141              ),
 142          )
 143      );
 144  
 145      list($criteria, $crit, $search_method) = $search->getFilter(array('status' => array('can_list' => true)));
 146  
 147      $search_render_options = array('placeholder' => 'search_logs');
 148  
 149      $total = safe_count('txp_log', "$criteria");
 150  
 151      $paginator = new \Textpattern\Admin\Paginator();
 152      $limit = $paginator->getLimit();
 153      list($page, $offset, $numPages) = pager($total, $limit, $page);
 154  
 155      $searchBlock =
 156          n.tag(
 157              $search->renderForm('log_list', $search_render_options),
 158              'div',
 159              array(
 160                  'class' => 'txp-layout-4col-3span',
 161                  'id'    => $event.'_control',
 162              )
 163          );
 164  
 165      $contentBlock ='';
 166  
 167      if ($total < 1) {
 168          $contentBlock .=
 169              graf(
 170                  span(null, array('class' => 'ui-icon ui-icon-info')).' '.
 171                  gTxt($crit === '' ? 'no_refers_recorded' : 'no_results_found'),
 172                  array('class' => 'alert-block information')
 173              );
 174      } else {
 175          $rs = safe_rows_start(
 176              "*, UNIX_TIMESTAMP(time) AS uTime",
 177              'txp_log',
 178              "$criteria ORDER BY $sort_sql LIMIT $offset, $limit"
 179          );
 180  
 181          if ($rs) {
 182              $contentBlock .= n.tag_start('form', array(
 183                      'class'  => 'multi_edit_form',
 184                      'id'     => 'log_form',
 185                      'name'   => 'longform',
 186                      'method' => 'post',
 187                      'action' => 'index.php',
 188                  )).
 189                  n.tag_start('div', array(
 190                      'class'      => 'txp-listtables',
 191                      'tabindex'   => 0,
 192                      'aria-label' => gTxt('list'),
 193                  )).
 194                  n.tag_start('table', array('class' => 'txp-list')).
 195                  n.tag_start('thead').
 196                  tr(
 197                      hCell(
 198                          fInput('checkbox', 'select_all', 0, '', '', '', '', '', 'select_all'),
 199                          '',
 200                          ' class="txp-list-col-multi-edit" scope="col" title="'.gTxt('toggle_all_selected').'"'
 201                      ).
 202                      column_head(
 203                          'time',
 204                          'time',
 205                          'log',
 206                          true,
 207                          $switch_dir,
 208                          $crit,
 209                          $search_method,
 210                          (('time' == $sort) ? "$dir " : '').'txp-list-col-time'
 211                      ).
 212                      column_head(
 213                          'page',
 214                          'page',
 215                          'log',
 216                          true,
 217                          $switch_dir,
 218                          $crit,
 219                          $search_method,
 220                          (('page' == $sort) ? "$dir " : '').'txp-list-col-page'
 221                      ).
 222                      column_head(
 223                          'referrer',
 224                          'refer',
 225                          'log',
 226                          true,
 227                          $switch_dir,
 228                          $crit,
 229                          $search_method,
 230                          (('refer' == $sort) ? "$dir " : '').'txp-list-col-refer'
 231                      ).
 232                      column_head(
 233                          'method',
 234                          'method',
 235                          'log',
 236                          true,
 237                          $switch_dir,
 238                          $crit,
 239                          $search_method,
 240                          (('method' == $sort) ? "$dir " : '').'txp-list-col-method'
 241                      ).
 242                      column_head(
 243                          'status',
 244                          'status',
 245                          'log',
 246                          true,
 247                          $switch_dir,
 248                          $crit,
 249                          $search_method,
 250                          (('status' == $sort) ? "$dir " : '').'txp-list-col-status'
 251                      )
 252                  ).
 253                  n.tag_end('thead').
 254                  n.tag_start('tbody');
 255  
 256              while ($a = nextRow($rs)) {
 257                  extract($a, EXTR_PREFIX_ALL, 'log');
 258  
 259                  if ($log_refer) {
 260                      $log_refer = href(txpspecialchars(soft_wrap(preg_replace('#^http://#', '', $log_refer), 30)), txpspecialchars($log_refer), ' rel="external noopener" target="_blank"');
 261                  }
 262  
 263                  if ($log_page) {
 264                      $log_anchor = preg_replace('/\/$/', '', $log_page);
 265                      $log_anchor = soft_wrap(substr($log_anchor, 1), 30);
 266                      $log_page = href('/'.txpspecialchars($log_anchor), rtrim(hu, '/').txpspecialchars($log_page), ' rel="external noopener" target="_blank"');
 267  
 268                      if ($log_method == 'POST') {
 269                          $log_page = strong($log_page);
 270                      }
 271                  }
 272  
 273                  $contentBlock .= tr(
 274                      td(
 275                          fInput('checkbox', 'selected[]', $log_id),
 276                          '',
 277                          'txp-list-col-multi-edit'
 278                      ).
 279                      hCell(
 280                          gTime($log_uTime),
 281                          '',
 282                          ' class="txp-list-col-time" scope="row"'
 283                      ).
 284                      td(
 285                          $log_page,
 286                          '',
 287                          'txp-list-col-page'
 288                      ).
 289                      td(
 290                          $log_refer,
 291                          '',
 292                          'txp-list-col-refer'
 293                      ).
 294                      td(
 295                          txpspecialchars($log_method),
 296                          '',
 297                          'txp-list-col-method'
 298                      ).
 299                      td(
 300                          $log_status,
 301                          '',
 302                          'txp-list-col-status'
 303                      )
 304                  );
 305              }
 306  
 307              $contentBlock .=
 308                  n.tag_end('tbody').
 309                  n.tag_end('table').
 310                  n.tag_end('div'). // End of .txp-listtables.
 311                  log_multiedit_form($page, $sort, $dir, $crit, $search_method).
 312                  tInput().
 313                  n.tag_end('form');
 314          }
 315      }
 316  
 317      $createBlock = '';
 318      $pageBlock = $paginator->render().
 319          nav_form('log', $page, $numPages, $sort, $dir, $crit, $search_method, $total, $limit);
 320  
 321      $table = new \Textpattern\Admin\Table($event);
 322      echo $table->render(compact('total', 'crit') + array('heading' => 'tab_logs'), $searchBlock, $createBlock, $contentBlock, $pageBlock);
 323  }
 324  
 325  /**
 326   * Saves a new pageby value to the server.
 327   */
 328  
 329  function log_change_pageby()
 330  {
 331      Txp::get('\Textpattern\Admin\Paginator')->change();
 332      log_list();
 333  }
 334  
 335  /**
 336   * Renders a multi-edit widget.
 337   *
 338   * @param  int    $page          The page number
 339   * @param  string $sort          The current sorting value
 340   * @param  string $dir           The current sorting direction
 341   * @param  string $crit          The current search criteria
 342   * @param  string $search_method The current search method
 343   * @return string HTML
 344   */
 345  
 346  function log_multiedit_form($page, $sort, $dir, $crit, $search_method)
 347  {
 348      $methods = array('delete' => gTxt('delete'));
 349  
 350      return multi_edit($methods, 'log', 'log_multi_edit', $page, $sort, $dir, $crit, $search_method);
 351  }
 352  
 353  /**
 354   * Processes multi-edit actions.
 355   */
 356  
 357  function log_multi_edit()
 358  {
 359      $deleted = event_multi_edit('txp_log', 'id');
 360  
 361      if ($deleted) {
 362          $message = gTxt('logs_deleted', array('{list}' => $deleted));
 363  
 364          return log_list($message);
 365      }
 366  
 367      return log_list();
 368  }

title

Description

title

Description

title

Description

title

title

Body