<?php
//===========================================================================\\
// VISIOLIST is a proud derivative work of Aardvark Topsites                \\
// Copyright (c) 2000-2009 Jeremy Scheff.  All rights reserved.              \\
//---------------------------------------------------------------------------\\
// http://www.aardvarktopsitesphp.com/                http://www.avatic.com/ \\
//---------------------------------------------------------------------------\\
// This program is free software; you can redistribute it and/or modify it  \\
// under the terms of the GNU General Public License as published by the    \\
// Free Software Foundation; either version 2 of the License, or (at your    \\
// option) any later version.                                                \\
//                                                                          \\
// This program is distributed in the hope that it will be useful, but      \\
// WITHOUT ANY WARRANTY; without even the implied warranty of                \\
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General \\
// Public License for more details.                                          \\
//===========================================================================\\
if (!defined('VISIOLIST')) {
  die("This file cannot be accessed directly.");
}
class in extends in_out {
    function in() {
    global $CONF, $DB, $LNG, $FORM, $TMPL;
    if (isset($_SERVER['HTTP_REFERER'])) {
      $referer = $DB->escape($_SERVER['HTTP_REFERER'], 1);
    }
    $go_to_rankings = 0;
    if (isset($FORM['a']) && $FORM['a'] == 'in' && isset($FORM['u'])) {
      $go_to_rankings = 1;
      $username = $DB->escape($FORM['u']);
    }
    else {
      // Get user by referer?
      $good_referer = 0;
      if (isset($referer) && !isset($FORM['a']) && mb_strpos($referer, $CONF['list_url']) === FALSE) {
        // Make sure it's not a search engine
        if (mb_strpos($referer, 'http://www.google.com/search') === FALSE && mb_strpos($referer, 'http://search.yahoo.com') === FALSE && mb_strpos($referer, 'http://search.msn.com') === FALSE) {
          $good_referer = 1;
        }
      }
      if ($good_referer) {
        $username = $this->get_username($referer);
      }
      else {
        $username = '';
      }
    }
    if ($username) {
      list($username_sql, $username_active) = $DB->fetch("SELECT username, active FROM {$CONF['sql_prefix']}_sites WHERE username = '{$username}'", __FILE__, __LINE__);
      if ($username_sql) {
        if ($CONF['gateway'] && !isset($FORM['sid'])) {
          $this->gateway($username);
        }
        else {
          $not_blacklisted = new join_edit;
          if ($CONF['gateway']) {
            $valid = $this->check($username);
          }
          else {
            $valid = 1;
          }
// Recaptcha Mod
require_once($CONF['path'].'/sources/recaptchalib.php');
$privatekey = "YOUR KEY HERE";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
  unset($go_to_rankings);
  unset($valid);
  $TMPL['captcha_error'] = '<div class="error">Captcha was incorrect</div>';
          $this->gateway($username);
}
// Recaptcha Mod
          // Plugin hook - Might be used for captcha validation
          eval (PluginManager::getPluginManager ()->pluginHooks ('in_before_valid'));
          if ($valid && $not_blacklisted->check_ban('vote')) {
            // Site made inactive due inactivity? Make it active again
            if ($username_active == 3) {
                $DB->query("UPDATE {$CONF['sql_prefix']}_sites SET active = 1 WHERE username = '{$username}'", __FILE__, __LINE__);
            }
            $this->record($username, 'in');
           
            eval (PluginManager::getPluginManager ()->pluginHooks ('in_valid'));
          }
        }
      }
    }
    if ($go_to_rankings) {
      $vote_url = "{$CONF['list_url']}/";
      // Plugin hook, redirect after voting. simply call $vote_url via a plugin
      eval (PluginManager::getPluginManager ()->pluginHooks ('in_redirect'));
      header("HTTP/1.1 301 Moved Permanently");
      header("Location: {$vote_url}");
      exit;
    }
  }
  static public function check($username) {
    global $CONF, $FORM;
    require_once("{$CONF['path']}/sources/misc/session.php");
    $session = new session;
    list($type, $data) = $session->get($FORM['sid']);
    $session->delete($FORM['sid']);
    if ($type == 'gateway' && $data == $username) {
      return 1;
    }
    else {
      return 0;
    }
  }
  static public function gateway($username) {
    global $DB, $LNG, $CONF, $FORM, $TMPL;
    if (empty($CONF['google_friendly_links'])) {
        header('X-Robots-Tag: noindex');
    }
    eval (PluginManager::getPluginManager ()->pluginHooks ('in_gateway_start'));
    require_once("{$CONF['path']}/sources/misc/session.php");
    $session = new session;
    $TMPL['sid'] = $session->create('gateway', $username);
    $TMPL['username'] = $username;
    $TMPL['gateway_top'] = '';
    $TMPL['gateway_bottom'] = '';
    $result = $DB->query("SELECT * FROM {$CONF['sql_prefix']}_sites WHERE username = '{$username}'", __FILE__, __LINE__);
    while ($row = $DB->fetch_array($result)) {
        $TMPL = array_merge($TMPL, $row);
    }
  // Recaptcha Mod
  require_once($CONF['path'].'/sources/recaptchalib.php');
  $publickey = "YOUR KEY HERE";
  $TMPL['captcha_code'] = recaptcha_get_html($publickey);
  // Recaptcha Mod
$result = $DB->query("SELECT title FROM {$CONF['sql_prefix']}_sites WHERE username = '{$username}' AND active='1'", __FILE__, __LINE__);
while (list($title) = $DB->fetch_array($result)) {
$TMPL['gateway_title'] = $title;
}
    eval (PluginManager::getPluginManager ()->pluginHooks ('in_gateway'));
    echo base::do_skin('gateway');
    exit;
  }
  static public function get_username($url) {
    global $CONF, $DB;
    $url = in::short_url($url);
    $count = 0;
    $username = '';
    while (!$username) {
      list($username) = $DB->fetch("SELECT username FROM {$CONF['sql_prefix']}_sites WHERE short_url = '{$url}'", __FILE__, __LINE__);
      if (!$username) {
        $url = in::short_url("{$url}.");
      }
      $count++;
      if ($count >= 10) {
        $username = 0;
        break;
      }
    }
    return $username;
  }
  static public function short_url($url) {
    // Lowercase
    $url = mb_strtolower($url);
    // Get rid of www.
    $url = preg_replace('/\/\/www./', '//', $url);
    // Get rid of trailing slash
    $url = preg_replace('/\/$/', '', $url);
    // Get rid of page after the last slash
    preg_match('/^(https?:\/\/.+)\/(.+)/', $url, $matches);
    if (!isset($matches[0])) {
      // Just a domain with a slash at the end
      $url = preg_replace('/^(https?:\/\/.+)\//', '\\1', $url);
    }
    else {
      // All other URLs
      // Check to see if after the trailing slash is a file or a directory
      if (mb_strpos($matches[2], '.')) { $url = $matches[1]; }
    }
    return $url;
  }
}
?>