<commit_before><?php

// Failback _() function if gettext if not supported
if (!function_exists("_"))
{
  function _($s)
  {
    return $s;
  }
}

class Casimir {
  public $version;
	public $base_url;
	public $short;
	public $msg;
	public $ok;
	public $access_key;

	function __construct() {
	  $this->version = '1.1';
    mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die(_('Could not connect to database'));
    mysql_select_db(MYSQL_DATABASE) or die(_('Could not select database'));
    $current_dir = dirname($_SERVER['PHP_SELF']);
    if ($current_dir == '/') $current_dir = '';
    $this->base_url = 'http://'.$_SERVER['SERVER_NAME'].$current_dir.'/';
    $this->short = '';
    $this->msg = '';
    $this->ok = true;
    $this->access_key = '';
    if (isset($_GET['access_key'])) {
      $this->access_key = $_GET['access_key'];
    } elseif (isset($_POST['access_key'])) {
      $this->access_key = $_POST['access_key'];
    }
    $this->setLocale();
	}

	private function tryLocale($l)
	{
		if (empty($l)) return false;

		// First, test if we support this locale
		if (
		     // We may support it with country code: xx_XX
				 (!file_exists("locale/$l/LC_MESSAGES/casimir.mo")) &&
				 // Or as a country-less language code: xx
				 (!file_exists("locale/" . preg_replace("/_.*/", "", $l) . "/LC_MESSAGES/casimir.mo"))
			 )
			return false;

		if (!@setlocale(LC_ALL, "$l.utf8"))
			return false;

		putenv("LC_ALL=$l");

		bindtextdomain("casimir", "locale");
		textdomain("casimir");

		return true;
	}

	private function setLocale()
	{
		// Check if user has provided its list of preferred languages
		if (empty($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
			return false;

		// Let's try if we support user preferred language
		if (
				 (class_exists("Locale")) &&
				 ($this->tryLocale(Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE'])))
			 )
		return true;

		// Get list of preferred languages as provided by user to try them all
		$languages = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
		if ((!empty($languages)) && (is_array($languages)))
			// Iterate over languages to find the first we support
			foreach ($languages as $l)
			{
			  // Convert Accept-Language language to a locale one
				$l = preg_replace(array("/;.*/", "/-/"), array("", "_"), $l);
				if ($this->tryLocale($l))
					return true;
			}

		return false;
	}

  function handleRequest() {
		if (preg_match("#^.*/\??([^=]+)$#i", $_SERVER['REQUEST_URI'], $regs)) {
		  $this->short = mysql_real_escape_string($regs[1]);
		} else {
		  $this->short = '';
		}
		if ($this->short != '' && $this->short != basename($_SERVER['PHP_SELF'])) {
		  if ($location = $this->getLong($this->short)) {
		  	$this->updateUses($this->short);
		    header('Status: 301 Moved Permanently', false, 301);
		    header('Location: '.$location);
		    exit;
		  } else {
		    $this->ok = false;
		    $this->msg = _('Sorry, but this short URL isn\'t in our database.');
		  }
		}

		if (defined('ACCESS_KEY') && ACCESS_KEY != '' && ACCESS_KEY != $this->access_key) {
		  $this->ok = false;
		  $this->msg = _('This Casimir instance is protected, you need an access key!');
		} else {
		  if (isset($_POST['long'])) {
		    list($this->ok, $this->short, $this->msg) = $this->addUrl($_POST['long'], isset($_POST['short']) && !is_null($_POST['short']) && $_POST['short'] != 'null' ? $_POST['short'] : '');
		  } elseif (isset($_GET['long'])) {
		    list($this->ok, $this->short, $this->msg) = $this->addUrl($_GET['long'], isset($_GET['short']) && !is_null($_GET['short']) && $_GET['short'] != 'null' ? $_GET['short'] : '');
		  }
		}
  }

  function showForm() {
    if ($this->msg != '') {
      echo '<p class="'.($this->ok ? 'success' : 'error').'">'.$this->msg.'</p>';
    }
    ?>
    <form action="<?php echo $this->base_url; ?>" method="post">
      <?php
      if (defined('ACCESS_KEY') && ACCESS_KEY != '') {
        ?>
        <input type="hidden" name="access_key" id="access_key" value="<?php echo $this->access_key; ?>" />
        <?php
      }
      ?>
      <dl>
        <dt><label for="long"><?php echo _("Enter a long URL:"); ?></label></dt>
        <dd><input type="text" name="long" id="long" size="80" value="<?php echo ($this->ok ? '' : (isset($_POST['long']) ? $_POST['long'] : (isset($_GET['long']) ? $_GET['long'] : ''))); ?>" /></dd>
        <dt><label for="short"><?php echo _("Optionally, define your own short URL:"); ?></label></dt>
        <dd><?php echo $this->base_url.(USE_REWRITE ? '' : '?'); ?><input type="text" name="short" id="short" size="20" maxlength="255" value="<?php echo ($this->ok ? '' : (isset($_POST['short']) ? $_POST['short'] : (isset($_GET['short']) ? $_GET['short'] : ''))); ?>" /></dd>
        <dt></dt>
        <dd class="center"><input type="submit" name="submit" id="submit" value="<?php echo _("Create!"); ?>" /></dd>
      </dl>
    </form>
  	<?php
  }

  function showBookmarklet() {
  	ob_start();
  	?>
    <a href="javascript:var url='<?php echo $this->base_url; ?>?<?php if (defined('ACCESS_KEY') && ACCESS_KEY != '' && ACCESS_KEY == $this->access_key) { echo 'access_key='.ACCESS_KEY.'&'; } ?>long='+encodeURIComponent(location.host=='maps.google.com'?document.getElementById('link').href:location.href);var short=prompt('<?php echo _("Do you want to define your own short URL? (leave empty if you don\'t)"); ?>','');if(short!=''){url=url+'&amp;short='+short;}location.href=url;">+<?php echo INSTANCE_NAME; ?></a>
    <?php
  	return ob_get_clean();
  }

  function getShort($long) {
    $q = 'SELECT short_url FROM casimir WHERE long_url="'.trim(mysql_real_escape_string($long)).'" ORDER BY creation_date DESC LIMIT 0,1';
    $result = mysql_query($q);
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      return $row['short_url'];
    } else {
      return false;
    }
  }

  function getLong($short) {
    $q = 'SELECT long_url FROM casimir WHERE short_url="'.trim(mysql_real_escape_string($short)).'"';
    $result = mysql_query($q);
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      return $row['long_url'];
    } else {
      return false;
    }
  }

  function addUrl($long, $short = '') {
    $long = trim(mysql_real_escape_string($long));
    if ($long == '') {
      return array(false, '', _('You must at least enter a long URL!'));
    } elseif (!preg_match("#^https?://#", $long)) {
      return array(false, '', _('Your URL must start with either "http://" or "https://"!'));
    } elseif (substr($long, 0, strlen($this->base_url)) == $this->base_url) {
      return array(false, '', _('This is already a shorten URL!'));
    }

    $existing_short = $this->getShort($long);
    $short = trim(mysql_real_escape_string($short));
    if ($short != '') {
    	if (!preg_match("#^[a-zA-Z0-9_-]+$#", $short)) {
        return array(false, '', _('This short URL is not authorized!'));
    	} elseif (strlen($short) > 50) {
        return array(false, '', _('This short URL is not short enough! Hint: 50 chars allowed...'));
    	}
    }
    $existing_long = $this->getLong($short);
    switch(true) {
    	case ($short == '' && $existing_short):
    		$short = $existing_short;
        $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
        return array(true, $short, _('A short URL already exists for this long URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
    		break;
    	case ($short == '' && !$existing_short):
	      $short = $this->getRandomShort();

	      $query = 'INSERT INTO casimir (short_url, long_url, creation_date) VALUES ("'.$short.'", "'.$long.'", NOW())';
	      if (mysql_query($query)) {
	        $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
	        return array(true, $short, _('Congratulations, you created this new short URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
	      } else {
	        return array(false, $short, sprintf(_('Something went wrong: %s'), mysql_error()));
	      }
    		break;
    	case ($short != '' && $existing_long && $long == $existing_long):
    	  $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
        return array(true, $short, _('This short URL already exists and is associated with the same long URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
    		break;
    	case ($short != '' && $existing_long && $existing_long != $long):
        return array(false, $short, _('This short URL already exists and is associated with this other long URL:') . '<br /><a href="'.$existing_long.'">'.$existing_long.'</a>');
    		break;
    	case ($short != '' && !$existing_short):
	      $query = 'INSERT INTO casimir (short_url, long_url, creation_date) VALUES ("'.$short.'", "'.$long.'", NOW())';
        if (mysql_query($query)) {
          $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
	        return array(true, $short, _('Congratulations, you created this new short URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
        } else {
          return array(false, $short, sprintf(_('Something went wrong: %s'), mysql_error()));
        }
    		break;
    	case ($short != '' && !$existing_long):
    		// Same as previous???
	      $query = 'INSERT INTO casimir (short_url, long_url, creation_date, title_url ) VALUES ("'.$short.'", "'.$long.'", NOW())';
        if (mysql_query($query)) {
          $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
	        return array(true, $short, _('Congratulations, you created this new short URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
        } else {
          return array(false, $short, sprintf(_('Something went wrong: %s'), mysql_error()));
        }
     		break;
 		}
 		return array(false, '', _('This should never happen...'));
  }

  function getRandomShort() {
    $allowed_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  	$short = '';
  	while(strlen($short) < 4 || $this->getLong($short)) {
  		$pos = rand(0, strlen($allowed_chars) - 1);
  		$short .= substr($allowed_chars, $pos, 1);
  	}
  	return $short;
  }

  function updateUses($short) {
    $query = 'INSERT INTO casimir_stats (short_url, use_date) VALUES ("'.trim(mysql_real_escape_string($short)).'", NOW())';
    mysql_query($query);
    $query = 'UPDATE casimir SET last_use_date=NOW(), uses=uses+1 WHERE short_url="'.trim(mysql_real_escape_string($short)).'"';<commit_msg>Fix inserting wrong number of columns when creating a new short link for an already existing URL.<commit_after><?php

// Failback _() function if gettext if not supported
if (!function_exists("_"))
{
  function _($s)
  {
    return $s;
  }
}

class Casimir {
  public $version;
	public $base_url;
	public $short;
	public $msg;
	public $ok;
	public $access_key;

	function __construct() {
	  $this->version = '1.1';
    mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die(_('Could not connect to database'));
    mysql_select_db(MYSQL_DATABASE) or die(_('Could not select database'));
    $current_dir = dirname($_SERVER['PHP_SELF']);
    if ($current_dir == '/') $current_dir = '';
    $this->base_url = 'http://'.$_SERVER['SERVER_NAME'].$current_dir.'/';
    $this->short = '';
    $this->msg = '';
    $this->ok = true;
    $this->access_key = '';
    if (isset($_GET['access_key'])) {
      $this->access_key = $_GET['access_key'];
    } elseif (isset($_POST['access_key'])) {
      $this->access_key = $_POST['access_key'];
    }
    $this->setLocale();
	}

	private function tryLocale($l)
	{
		if (empty($l)) return false;

		// First, test if we support this locale
		if (
		     // We may support it with country code: xx_XX
				 (!file_exists("locale/$l/LC_MESSAGES/casimir.mo")) &&
				 // Or as a country-less language code: xx
				 (!file_exists("locale/" . preg_replace("/_.*/", "", $l) . "/LC_MESSAGES/casimir.mo"))
			 )
			return false;

		if (!@setlocale(LC_ALL, "$l.utf8"))
			return false;

		putenv("LC_ALL=$l");

		bindtextdomain("casimir", "locale");
		textdomain("casimir");

		return true;
	}

	private function setLocale()
	{
		// Check if user has provided its list of preferred languages
		if (empty($_SERVER["HTTP_ACCEPT_LANGUAGE"]))
			return false;

		// Let's try if we support user preferred language
		if (
				 (class_exists("Locale")) &&
				 ($this->tryLocale(Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE'])))
			 )
		return true;

		// Get list of preferred languages as provided by user to try them all
		$languages = explode(",", $_SERVER["HTTP_ACCEPT_LANGUAGE"]);
		if ((!empty($languages)) && (is_array($languages)))
			// Iterate over languages to find the first we support
			foreach ($languages as $l)
			{
			  // Convert Accept-Language language to a locale one
				$l = preg_replace(array("/;.*/", "/-/"), array("", "_"), $l);
				if ($this->tryLocale($l))
					return true;
			}

		return false;
	}

  function handleRequest() {
		if (preg_match("#^.*/\??([^=]+)$#i", $_SERVER['REQUEST_URI'], $regs)) {
		  $this->short = mysql_real_escape_string($regs[1]);
		} else {
		  $this->short = '';
		}
		if ($this->short != '' && $this->short != basename($_SERVER['PHP_SELF'])) {
		  if ($location = $this->getLong($this->short)) {
		  	$this->updateUses($this->short);
		    header('Status: 301 Moved Permanently', false, 301);
		    header('Location: '.$location);
		    exit;
		  } else {
		    $this->ok = false;
		    $this->msg = _('Sorry, but this short URL isn\'t in our database.');
		  }
		}

		if (defined('ACCESS_KEY') && ACCESS_KEY != '' && ACCESS_KEY != $this->access_key) {
		  $this->ok = false;
		  $this->msg = _('This Casimir instance is protected, you need an access key!');
		} else {
		  if (isset($_POST['long'])) {
		    list($this->ok, $this->short, $this->msg) = $this->addUrl($_POST['long'], isset($_POST['short']) && !is_null($_POST['short']) && $_POST['short'] != 'null' ? $_POST['short'] : '');
		  } elseif (isset($_GET['long'])) {
		    list($this->ok, $this->short, $this->msg) = $this->addUrl($_GET['long'], isset($_GET['short']) && !is_null($_GET['short']) && $_GET['short'] != 'null' ? $_GET['short'] : '');
		  }
		}
  }

  function showForm() {
    if ($this->msg != '') {
      echo '<p class="'.($this->ok ? 'success' : 'error').'">'.$this->msg.'</p>';
    }
    ?>
    <form action="<?php echo $this->base_url; ?>" method="post">
      <?php
      if (defined('ACCESS_KEY') && ACCESS_KEY != '') {
        ?>
        <input type="hidden" name="access_key" id="access_key" value="<?php echo $this->access_key; ?>" />
        <?php
      }
      ?>
      <dl>
        <dt><label for="long"><?php echo _("Enter a long URL:"); ?></label></dt>
        <dd><input type="text" name="long" id="long" size="80" value="<?php echo ($this->ok ? '' : (isset($_POST['long']) ? $_POST['long'] : (isset($_GET['long']) ? $_GET['long'] : ''))); ?>" /></dd>
        <dt><label for="short"><?php echo _("Optionally, define your own short URL:"); ?></label></dt>
        <dd><?php echo $this->base_url.(USE_REWRITE ? '' : '?'); ?><input type="text" name="short" id="short" size="20" maxlength="255" value="<?php echo ($this->ok ? '' : (isset($_POST['short']) ? $_POST['short'] : (isset($_GET['short']) ? $_GET['short'] : ''))); ?>" /></dd>
        <dt></dt>
        <dd class="center"><input type="submit" name="submit" id="submit" value="<?php echo _("Create!"); ?>" /></dd>
      </dl>
    </form>
  	<?php
  }

  function showBookmarklet() {
  	ob_start();
  	?>
    <a href="javascript:var url='<?php echo $this->base_url; ?>?<?php if (defined('ACCESS_KEY') && ACCESS_KEY != '' && ACCESS_KEY == $this->access_key) { echo 'access_key='.ACCESS_KEY.'&'; } ?>long='+encodeURIComponent(location.host=='maps.google.com'?document.getElementById('link').href:location.href);var short=prompt('<?php echo _("Do you want to define your own short URL? (leave empty if you don\'t)"); ?>','');if(short!=''){url=url+'&amp;short='+short;}location.href=url;">+<?php echo INSTANCE_NAME; ?></a>
    <?php
  	return ob_get_clean();
  }

  function getShort($long) {
    $q = 'SELECT short_url FROM casimir WHERE long_url="'.trim(mysql_real_escape_string($long)).'" ORDER BY creation_date DESC LIMIT 0,1';
    $result = mysql_query($q);
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      return $row['short_url'];
    } else {
      return false;
    }
  }

  function getLong($short) {
    $q = 'SELECT long_url FROM casimir WHERE short_url="'.trim(mysql_real_escape_string($short)).'"';
    $result = mysql_query($q);
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_array($result);
      return $row['long_url'];
    } else {
      return false;
    }
  }

  function addUrl($long, $short = '') {
    $long = trim(mysql_real_escape_string($long));
    if ($long == '') {
      return array(false, '', _('You must at least enter a long URL!'));
    } elseif (!preg_match("#^https?://#", $long)) {
      return array(false, '', _('Your URL must start with either "http://" or "https://"!'));
    } elseif (substr($long, 0, strlen($this->base_url)) == $this->base_url) {
      return array(false, '', _('This is already a shorten URL!'));
    }

    $existing_short = $this->getShort($long);
    $short = trim(mysql_real_escape_string($short));
    if ($short != '') {
    	if (!preg_match("#^[a-zA-Z0-9_-]+$#", $short)) {
        return array(false, '', _('This short URL is not authorized!'));
    	} elseif (strlen($short) > 50) {
        return array(false, '', _('This short URL is not short enough! Hint: 50 chars allowed...'));
    	}
    }
    $existing_long = $this->getLong($short);
    switch(true) {
    	case ($short == '' && $existing_short):
    		$short = $existing_short;
        $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
        return array(true, $short, _('A short URL already exists for this long URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
    		break;
    	case ($short == '' && !$existing_short):
	      $short = $this->getRandomShort();

	      $query = 'INSERT INTO casimir (short_url, long_url, creation_date) VALUES ("'.$short.'", "'.$long.'", NOW())';
	      if (mysql_query($query)) {
	        $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
	        return array(true, $short, _('Congratulations, you created this new short URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
	      } else {
	        return array(false, $short, sprintf(_('Something went wrong: %s'), mysql_error()));
	      }
    		break;
    	case ($short != '' && $existing_long && $long == $existing_long):
    	  $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
        return array(true, $short, _('This short URL already exists and is associated with the same long URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
    		break;
    	case ($short != '' && $existing_long && $existing_long != $long):
        return array(false, $short, _('This short URL already exists and is associated with this other long URL:') . '<br /><a href="'.$existing_long.'">'.$existing_long.'</a>');
    		break;
    	case ($short != '' && !$existing_short):
	      $query = 'INSERT INTO casimir (short_url, long_url, creation_date) VALUES ("'.$short.'", "'.$long.'", NOW())';
        if (mysql_query($query)) {
          $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
	        return array(true, $short, _('Congratulations, you created this new short URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
        } else {
          return array(false, $short, sprintf(_('Something went wrong: %s'), mysql_error()));
        }
    		break;
    	case ($short != '' && !$existing_long):
    		// Same as previous???
	      $query = 'INSERT INTO casimir (short_url, long_url, creation_date) VALUES ("'.$short.'", "'.$long.'", NOW())';
        if (mysql_query($query)) {
          $short_url = $this->base_url.(USE_REWRITE ? '' : '?').$short;
	        return array(true, $short, _('Congratulations, you created this new short URL:') . '<br /><a href="'.$short_url.'">'.$short_url.'</a>');
        } else {
          return array(false, $short, sprintf(_('Something went wrong: %s'), mysql_error()));
        }
     		break;
 		}
 		return array(false, '', _('This should never happen...'));
  }

  function getRandomShort() {
    $allowed_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  	$short = '';
  	while(strlen($short) < 4 || $this->getLong($short)) {
  		$pos = rand(0, strlen($allowed_chars) - 1);
  		$short .= substr($allowed_chars, $pos, 1);
  	}
  	return $short;
  }

  function updateUses($short) {
    $query = 'INSERT INTO casimir_stats (short_url, use_date) VALUES ("'.trim(mysql_real_escape_string($short)).'", NOW())';
    mysql_query($query);
    $query = 'UPDATE casimir SET last_use_date=NOW(), uses=uses+1 WHERE short_url="'.trim(mysql_real_escape_string($short)).'"';
{'fraction_non_alphanumeric': 0.21509686038744155, 'fraction_numerical': 0.003912587078919744, 'mean_word_length': 3.4765057667663393, 'pattern_counts': {'":': 0, '<': 131, '<?xml version=': 0, '>': 235, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '2077689', 'n_tokens_mistral': 7374, 'n_tokens_neox': 7018, 'n_words': 1580}