<commit_before><?php
/*
 * This file is part of the Reditype package.
 *
 * (c) 2009-2010 digital Wranglers <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * rtSiteToolkit provides a set of worker methods for dealing with the multi-site separation.
 *
 * @package    rtCorePlugin
 * @subpackage toolkit
 * @author     <NAME> <<EMAIL>>
 */
class rtSiteToolkit
{
  /**
   * Simply runs a check on the multi-site configuration setting.
   *
   * @return boolean
   */
  public static function isMultiSiteEnabled()
  {
    return sfConfig::get('app_rt_enable_multi_site', false);
  }

  /**
   * @static
   * @return rtSite
   */
  public static function getCurrentSite()
  {
    return Doctrine::getTable('rtSite')->findOneBy('Domain', self::getCurrentDomain());
  }

  /**
   * Return the domain and port as a string. For example: www.my-domain.com:8080
   *
   * Note: Only non-port 80 ports will be returned.
   *
   * @param array $source the array source to retrieve URL from, defalt is $_SERVER
   * @return string
   */
  public static function getCurrentDomain(array $source = null, $include_protocol = false)
  {
    if ( $source === null ) $source = $_SERVER;

    $domain = '';

    if(isset($source['HTTP_HOST']))
    {
      $domain = $source['HTTP_HOST'];
    }
    else
    {
      $domain = $source['SERVER_NAME'];
      if ( isset( $source['SERVER_PORT'] ) && $source['SERVER_PORT'] != 80 )
      {
        $domain .= ":{$source['SERVER_PORT']}";
      }
    }

    if($include_protocol)
    {
      $protocol = explode('/', $source['SERVER_PROTOCOL']);
      $domain = strtolower($protocol[0]) . '://' . $domain;
    }

    return self::cleanDomainString($domain);
  }

  /**
   * Cleans the domain string of "www" subdomains as these are concidered to be the same as the
   * bas domain without "www".
   *
   * @param string $string
   */
  public static function cleanDomainString($string)
  {
    if(substr($string, 0, 4) === 'www.')
    {
      $string = substr($string, 4);
    }

    return $string;
  }

  public static function checkSiteReference($object, $route = null, $redirect_always = false)
  {
    if(!rtSiteToolkit::isMultiSiteEnabled())
    {
      return;
    }

    $context = sfContext::getInstance();

    if(is_null($route))
    {
      $route = $object->getTable()->getTableName() . '_show';
      $route = $context->getRouting()->generate($route,$object);
    }

    if(!$object->rtSite || is_null($object->rtSite->getDomain()))
    {
      if($redirect_always)
      {
        $context->getController()->redirect($route);<commit_msg>Removed atou cleaning of www.<commit_after><?php
/*
 * This file is part of the Reditype package.
 *
 * (c) 2009-2010 digital Wranglers <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * rtSiteToolkit provides a set of worker methods for dealing with the multi-site separation.
 *
 * @package    rtCorePlugin
 * @subpackage toolkit
 * @author     <NAME> <<EMAIL>>
 */
class rtSiteToolkit
{
  /**
   * Simply runs a check on the multi-site configuration setting.
   *
   * @return boolean
   */
  public static function isMultiSiteEnabled()
  {
    return sfConfig::get('app_rt_enable_multi_site', false);
  }

  /**
   * @static
   * @return rtSite
   */
  public static function getCurrentSite()
  {
    return Doctrine::getTable('rtSite')->findOneBy('Domain', self::getCurrentDomain());
  }

  /**
   * Return the domain and port as a string. For example: www.my-domain.com:8080
   *
   * Note: Only non-port 80 ports will be returned.
   *
   * @param array $source the array source to retrieve URL from, defalt is $_SERVER
   * @return string
   */
  public static function getCurrentDomain(array $source = null, $include_protocol = false)
  {
    if ( $source === null ) $source = $_SERVER;

    $domain = '';

    if(isset($source['HTTP_HOST']))
    {
      $domain = $source['HTTP_HOST'];
    }
    else
    {
      $domain = $source['SERVER_NAME'];
      if ( isset( $source['SERVER_PORT'] ) && $source['SERVER_PORT'] != 80 )
      {
        $domain .= ":{$source['SERVER_PORT']}";
      }
    }

    if($include_protocol)
    {
      $protocol = explode('/', $source['SERVER_PROTOCOL']);
      $domain = strtolower($protocol[0]) . '://' . $domain;
    }

    return self::cleanDomainString($domain);
  }

  /**
   * Cleans the domain string of "www" subdomains as these are concidered to be the same as the
   * bas domain without "www".
   *
   * @param string $string
   */
  public static function cleanDomainString($string)
  {
    if (sfConfig::get('app_rt_domain_clean_www', false) && substr($string, 0, 4) === 'www.') {
      $string = substr($string, 4);
    }

    return $string;
  }

  public static function checkSiteReference($object, $route = null, $redirect_always = false)
  {
    if(!rtSiteToolkit::isMultiSiteEnabled())
    {
      return;
    }

    $context = sfContext::getInstance();

    if(is_null($route))
    {
      $route = $object->getTable()->getTableName() . '_show';
      $route = $context->getRouting()->generate($route,$object);
    }

    if(!$object->rtSite || is_null($object->rtSite->getDomain()))
    {
      if($redirect_always)
      {
        $context->getController()->redirect($route);
{'fraction_non_alphanumeric': 0.1480797636632201, 'fraction_numerical': 0.007385524372230428, 'mean_word_length': 2.9830882352941175, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2869626', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1652, 'n_words': 430}