Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/publish/log.php - 146 lines - 3952 bytes - Summary - Text - Print

Description: Log visitors.

   1  <?php
   2  
   3  /*
   4   * Textpattern Content Management System
   5   * http://textpattern.com
   6   *
   7   * Copyright (C) 2005 Dean Allen
   8   * Copyright (C) 2016 The Textpattern Development Team
   9   *
  10   * This file is part of Textpattern.
  11   *
  12   * Textpattern is free software; you can redistribute it and/or
  13   * modify it under the terms of the GNU General Public License
  14   * as published by the Free Software Foundation, version 2.
  15   *
  16   * Textpattern is distributed in the hope that it will be useful,
  17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19   * GNU General Public License for more details.
  20   *
  21   * You should have received a copy of the GNU General Public License
  22   * along with Textpattern. If not, see <http://www.gnu.org/licenses/>.
  23   */
  24  
  25  /**
  26   * Log visitors.
  27   *
  28   * @package Log
  29   */
  30  
  31  /**
  32   * Adds a row to the visitor logs.
  33   *
  34   * This function follows the site's logging preferences. If $logging preference
  35   * is set to 'refer', only referrer hits are logged. If $logging is set to
  36   * 'none' or '$nolog' global to TRUE, the function will ignore all hits.
  37   *
  38   * If the $status parameter is set to 404, the hit isn't logged.
  39   *
  40   * @param int $status HTTP status code
  41   * @example
  42   * log_hit(200);
  43   */
  44  
  45  function log_hit($status)
  46  {
  47      global $nolog, $logging;
  48      callback_event('log_hit');
  49  
  50      if (!isset($nolog) && $status != 404) {
  51          if ($logging == 'refer') {
  52              logit('refer', $status);
  53          } elseif ($logging == 'all') {
  54              logit('', $status);
  55          }
  56      }
  57  }
  58  
  59  /**
  60   * Writes a record to the visitor log using the current visitor's information.
  61   *
  62   * This function is used by log_hit(). See it before trying to use this one.
  63   *
  64   * The hit is ignore if $r is set to 'refer' and the HTTP REFERER header
  65   * is empty.
  66   *
  67   * @param  string $r      Type of record to write, e.g. refer
  68   * @param  int    $status HTTP status code
  69   * @access private
  70   * @see    log_hit()
  71   */
  72  
  73  function logit($r = '', $status = 200)
  74  {
  75      global $prefs, $pretext;
  76  
  77      if (!isset($pretext['request_uri'])) {
  78          return;
  79      }
  80  
  81      $host = $ip = (string) remote_addr();
  82      $protocol = false;
  83      $referer = serverSet('HTTP_REFERER');
  84  
  85      if ($referer) {
  86          foreach (do_list(LOG_REFERER_PROTOCOLS) as $option) {
  87              if (strpos($referer, $option.'://') === 0) {
  88                  $protocol = $option;
  89                  $referer = substr($referer, strlen($protocol) + 3);
  90                  break;
  91              }
  92          }
  93  
  94          if (!$protocol || ($protocol === 'https' && PROTOCOL !== 'https://')) {
  95              $referer = '';
  96          } elseif (preg_match('/^[^\.]*\.?'.preg_quote(preg_replace('/^www\./', '', SITE_HOST), '/').'/i', $referer)) {
  97              $referer = '';
  98          } else {
  99              $referer = $protocol.'://'.clean_url($referer);
 100          }
 101      }
 102  
 103      if ($r == 'refer' && !$referer) {
 104          return;
 105      }
 106  
 107      if (!empty($prefs['use_dns'])) {
 108          // A crude rDNS cache.
 109          if (($h = safe_field("host", 'txp_log', "ip = '".doSlash($ip)."' LIMIT 1")) !== false) {
 110              $host = $h;
 111          } else {
 112              // Double-check the rDNS.
 113              $host = @gethostbyaddr($ip);
 114  
 115              if ($host !== $ip && @gethostbyname($host) !== $ip) {
 116                  $host = $ip;
 117              }
 118          }
 119      }
 120  
 121      insert_logit(array(
 122          'uri'    => $pretext['request_uri'],
 123          'ip'     => $ip,
 124          'host'   => $host,
 125          'status' => $status,
 126          'method' => serverSet('REQUEST_METHOD'),
 127          'ref'    => $referer,
 128      ));
 129  }
 130  
 131  /**
 132   * Inserts a log record into the database.
 133   *
 134   * @param array $in Input array consisting 'uri', 'ip', 'host', 'ref', 'status', 'method'
 135   * @see   log_hit()
 136   */
 137  
 138  function insert_logit($in)
 139  {
 140      $in = doSlash($in);
 141      extract($in);
 142      safe_insert(
 143          'txp_log',
 144          "time = NOW(), page = '$uri', ip = '$ip', host = '$host', refer = '$ref', status = '$status', method = '$method'"
 145      );
 146  }

title

Description

title

Description

title

Description

title

title

Body