PageRenderTime 209ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/html/AppCode/expressionengine/libraries/Referrer.php

https://github.com/w3bg/www.hsifin.com
PHP | 144 lines | 80 code | 25 blank | 39 comment | 27 complexity | eb8e68ebe5e5ba1f61f44bde8307138e MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * ExpressionEngine - by EllisLab
  4. *
  5. * @package ExpressionEngine
  6. * @author ExpressionEngine Dev Team
  7. * @copyright Copyright (c) 2003 - 2010, EllisLab, Inc.
  8. * @license http://expressionengine.com/user_guide/license.html
  9. * @link http://expressionengine.com
  10. * @since Version 2.0
  11. * @filesource
  12. */
  13. // ------------------------------------------------------------------------
  14. /**
  15. * ExpressionEngine Referrer Class
  16. *
  17. * @package ExpressionEngine
  18. * @subpackage Core
  19. * @category Core
  20. * @author ExpressionEngine Dev Team
  21. * @link http://expressionengine.com
  22. */
  23. class EE_Referrer {
  24. /**
  25. * Constructor
  26. */
  27. function EE_Referrer()
  28. {
  29. // Make a local reference to the ExpressionEngine super object
  30. $this->EE =& get_instance();
  31. }
  32. // --------------------------------------------------------------------
  33. /**
  34. * Log Referrer data
  35. *
  36. * @access public
  37. * @return bool
  38. */
  39. function log_referrer()
  40. {
  41. // Is the nation of the user banend?
  42. if ($this->EE->session->nation_ban_check(FALSE) === FALSE)
  43. {
  44. return;
  45. }
  46. if ($this->EE->config->item('log_referrers') == 'n' OR ! isset($_SERVER['HTTP_REFERER']))
  47. {
  48. return;
  49. }
  50. // Load the typography helper so we can do entity_decode()
  51. $this->EE->load->helper('typography');
  52. $site_url = $this->EE->config->item('site_url');
  53. $ref = ( ! isset($_SERVER['HTTP_REFERER'])) ? '' : $this->EE->security->xss_clean(entity_decode($_SERVER['HTTP_REFERER']));
  54. $test_ref = strtolower($ref); // Yes, a copy, not a reference
  55. $domain = ( ! $this->EE->config->item('cookie_domain')) ? '' : $this->EE->config->item('cookie_domain');
  56. // Throttling - Ten hits a minute is the limit
  57. $query = $this->EE->db->query("SELECT COUNT(*) AS count
  58. FROM exp_referrers
  59. WHERE site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."'
  60. AND (ref_from = '".$this->EE->db->escape_str($ref)."' OR ref_ip = '".$this->EE->input->ip_address()."')
  61. AND ref_date > '".($this->EE->localize->now-60)."'");
  62. if ($query->row('count') > 10)
  63. {
  64. return FALSE;
  65. }
  66. if (stristr($ref, '{') !== FALSE OR stristr($ref, '}') !== FALSE)
  67. {
  68. return FALSE;
  69. }
  70. if ( ! preg_match("#^http://\w+\.\w+\.\w*#", $ref))
  71. {
  72. if (substr($test_ref, 0, 7) == 'http://' AND substr($test_ref, 0, 11) != 'http://www.')
  73. {
  74. $test_ref = preg_replace("#^http://(.+?)#", "http://www.\\1", $test_ref);
  75. }
  76. }
  77. if ( ! preg_match("#^http://\w+\.\w+\.\w*#", $site_url))
  78. {
  79. if (substr($site_url, 0, 7) == 'http://' AND substr($site_url, 0, 11) != 'http://www.')
  80. {
  81. $site_url = preg_replace("#^http://(.+?)#", "http://www.\\1", $site_url);
  82. }
  83. }
  84. if ($test_ref != ''
  85. && strncasecmp($test_ref, $site_url, strlen($site_url)) != 0
  86. && ($domain == '' OR stristr($test_ref, $domain) === FALSE)
  87. && ($this->EE->blacklist->whitelisted == 'y' OR $this->EE->blacklist->blacklisted == 'n'))
  88. {
  89. // INSERT into database
  90. $ref_to = $this->EE->security->xss_clean($this->EE->functions->fetch_current_uri());
  91. if (stristr($ref_to, '{') !== FALSE OR stristr($ref_to, '}') !== FALSE)
  92. {
  93. return FALSE;
  94. }
  95. $insert_data = array ( 'ref_from' => $ref,
  96. 'ref_to' => $ref_to,
  97. 'ref_ip' => $this->EE->input->ip_address(),
  98. 'ref_date' => $this->EE->localize->now,
  99. 'ref_agent' => substr($this->EE->input->user_agent(), 0, 100), // db field is 100 chararacters, truncate for MySQL strict mode compat
  100. 'site_id' => $this->EE->config->item('site_id')
  101. );
  102. $this->EE->db->query($this->EE->db->insert_string('exp_referrers', $insert_data));
  103. // Prune Database
  104. srand(time());
  105. if ((rand() % 100) < 5)
  106. {
  107. $max = ( ! is_numeric($this->EE->config->item('max_referrers'))) ? 500 : $this->EE->config->item('max_referrers');
  108. $query = $this->EE->db->query("SELECT MAX(ref_id) as ref_id FROM exp_referrers WHERE site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."'");
  109. $row = $query->row_array();
  110. if (isset($row['ref_id'] ) && $row['ref_id'] > $max)
  111. {
  112. $this->EE->db->query("DELETE FROM exp_referrers WHERE site_id = '".$this->EE->db->escape_str($this->EE->config->item('site_id'))."' AND ref_id < ".($row['ref_id'] -$max)."");
  113. }
  114. }
  115. }
  116. }
  117. }
  118. /* End of file Referrer.php */
  119. /* Location: ./system/expressionengine/libraries/Referrer.php */