Changeset - 958790b1f34c
[Not reviewed]
0 0 5
Echedey López Romero - 8 months ago 2018-09-23 14:31:42
echedey@localhost.localdomain
Initial commit
5 files changed with 441 insertions and 0 deletions:
0 comments (0 inline, 0 general)
README
Show inline comments
 
new file 100644
 
This is a fork from lilURL (http://lilurl.sourceforge.net) with some
 
modern and personal changes.
 

	
 
-----------------------------------------------------------------------
 

	
 
To install:
 

	
 
1. Create a MySQL database and user for lilURL. 
 

	
 
2. Import the lilurl.sql file:
 

	
 
      (( like so:
 
      
 
         mysql -u <lilurl_user> -p <lilurl_db> < lilurl.sql
 
      
 
      ))
 

	
 
3. Edit the configuration file includes/conf.php to suit your needs.
 

	
 
4. Set up mod_rewrite, if necessary
 

	
 
      (( a .htaccess file with the lines:
 
   
 
         RewriteEngine On
 
         RewriteRule (.*) index.php
 
   
 
        should suffice ))
 

	
 
5. Buy 15 donuts and eat them all in one sitting.
includes/conf.php
Show inline comments
 
new file 100755
 
<?php /* conf.php ( config file ) */
 

	
 
// page title
 
define('PAGE_TITLE', 'lilURL');
 

	
 
// MySQL connection info
 
define('MYSQL_USER', '');
 
define('MYSQL_PASS', '');
 
define('MYSQL_DB', '');
 
define('MYSQL_HOST', '');
 

	
 
// MySQL tables
 
define('URL_TABLE', 'lil_urls');
 

	
 
// use mod_rewrite?
 
define('REWRITE', true);
 

	
 
// allow urls that begin with these strings
 
$allowed_protocols = array('http:', 'https:', 'mailto:');
 

	
 
// uncomment the line below to skip the protocol check
 
// $allowed_procotols = array();
 

	
 
?>
includes/lilurl.php
Show inline comments
 
new file 100755
 
<?php /* lilurl.php ( lilURL class file ) */
 

	
 
class lilURL
 
{
 
	// constructor
 
	function lilURL()
 
	{
 
		// open mysql connection
 
		mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB) or die('Could not connect to database');	
 
	}
 

	
 
	// return the id for a given url (or -1 if the url doesn't exist)
 
	function get_id($url)
 
	{
 
		$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
 
		$q = 'SELECT id FROM '.URL_TABLE.' WHERE (url="'.$url.'")';
 
		$result = mysqli_query($connect, $q);
 

	
 
		if ( mysqli_num_rows($result) )
 
		{
 
			$row = mysqli_fetch_array($result);
 
			return $row['id'];
 
		}
 
		else
 
		{
 
			return -1;
 
		}
 
	}
 

	
 
	// return the url for a given id (or -1 if the id doesn't exist)
 
	function get_url($id)
 
	{
 
		$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
 
		$q = 'SELECT url FROM '.URL_TABLE.' WHERE (id="'.$id.'")';
 
		$result = mysqli_query($connect, $q);
 

	
 
		if ( mysqli_num_rows($result) )
 
		{
 
			$row = mysqli_fetch_array($result);
 
			return $row['url'];
 
		}
 
		else
 
		{
 
			return -1;
 
		}
 
	}
 
	
 
	// add a url to the database
 
	function add_url($url)
 
	{
 
		$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
 
		// check to see if the url's already in there
 
		$id = $this->get_id($url);
 
		
 
		// if it is, return true
 
		if ( $id != -1 )
 
		{
 
			return true;
 
		}
 
		else // otherwise, put it in
 
		{
 
			$id = $this->get_next_id($this->get_last_id());
 
			$q = 'INSERT INTO '.URL_TABLE.' (id, url, date) VALUES ("'.$id.'", "'.$url.'", NOW())';
 

	
 
			return mysqli_query($connect, $q);
 
		}
 
	}
 

	
 
	// return the most recent id (or -1 if no ids exist)
 
	function get_last_id()
 
	{
 
		$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
 
		$q = 'SELECT id FROM '.URL_TABLE.' ORDER BY date DESC LIMIT 1';
 
		$result = mysqli_query($connect, $q);
 

	
 
		if ( mysqli_num_rows($result) )
 
		{
 
			$row = mysqli_fetch_array($result);
 
			return $row['id'];
 
		}
 
		else
 
		{
 
			return -1;
 
		}
 
	}	
 

	
 
	// return the next id
 
	function get_next_id($last_id)
 
	{
 
		$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
 
		// if the last id is -1 (non-existant), start at the begining with 0
 
		if ( $last_id == -1 )
 
		{
 
			$next_id = 0;
 
		}
 
		else
 
		{
 
			// loop through the id string until we find a character to increment
 
			for ( $x = 1; $x <= strlen($last_id); $x++ )
 
			{
 
				$pos = strlen($last_id) - $x;
 

	
 
				if ( $last_id[$pos] != 'z' )
 
				{
 
					$next_id = $this->increment_id($last_id, $pos);
 
					break; // <- kill the for loop once we've found our char
 
				}
 
			}
 

	
 
			// if every character was already at its max value (z),
 
			// append another character to the string
 
			if ( !isSet($next_id) )
 
			{
 
				$next_id = $this->append_id($last_id);
 
			}
 
		}
 

	
 
		// check to see if the $next_id we made already exists, and if it does, 
 
		// loop the function until we find one that doesn't
 
		//
 
		// (this is basically a failsafe to get around the potential dangers of
 
		//  my kludgey use of a timestamp to pick the most recent id)
 
		$q = 'SELECT id FROM '.URL_TABLE.' WHERE (id="'.$next_id.'")';
 
		$result = mysqli_query($connect, $q);
 
		
 
		if ( mysqli_num_rows($result) )
 
		{
 
			$next_id = $this->get_next_id($next_id);
 
		}
 

	
 
		return $next_id;
 
	}
 

	
 
	// make every character in the string 0, and then add an additional 0 to that
 
	function append_id($id)
 
	{
 
		for ( $x = 0; $x < strlen($id); $x++ )
 
		{
 
			$id[$x] = 0;
 
		}
 

	
 
		$id .= 0;
 

	
 
		return $id;
 
	}
 

	
 
	// increment a character to the next alphanumeric value and return the modified id
 
	function increment_id($id, $pos)
 
	{		
 
		$char = $id[$pos];
 
		
 
		// add 1 to numeric values
 
		if ( is_numeric($char) )
 
		{
 
			if ( $char < 9 )
 
			{
 
				$new_char = $char + 1;
 
			}
 
			else // if we're at 9, it's time to move to the alphabet
 
			{
 
				$new_char = 'a';
 
			}
 
		}
 
		else // move it up the alphabet
 
		{
 
			$new_char = chr(ord($char) + 1);
 
		}
 

	
 
		$id[$pos] = $new_char;
 
		
 
		// set all characters after the one we're modifying to 0
 
		if ( $pos != (strlen($id) - 1) )
 
		{
 
			for ( $x = ($pos + 1); $x < strlen($id); $x++ )
 
			{
 
				$id[$x] = 0;
 
			}
 
		}
 

	
 
		return $id;
 
	}
 

	
 
}
 

	
 
?>
index.php
Show inline comments
 
new file 100755
 
<?php /* index.php ( lilURL implementation ) */
 

	
 
require_once 'includes/conf.php'; // <- site-specific settings
 
require_once 'includes/lilurl.php'; // <- lilURL class file
 

	
 
$lilurl = new lilURL();
 
$msg = '';
 
$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
 

	
 
// if the form has been submitted
 
if ( isset($_POST['longurl']) )
 
{
 
	// escape bad characters from the user's url
 
	$longurl = trim(mysqli_escape_string($connect, $_POST['longurl']));
 

	
 
	// set the protocol to not ok by default
 
	$protocol_ok = false;
 
	
 
	// if there's a list of allowed protocols, 
 
	// check to make sure that the user's url uses one of them
 
	if ( count($allowed_protocols) )
 
	{
 
		foreach ( $allowed_protocols as $ap )
 
		{
 
			if ( strtolower(substr($longurl, 0, strlen($ap))) == strtolower($ap) )
 
			{
 
				$protocol_ok = true;
 
				break;
 
			}
 
		}
 
	}
 
	else // if there's no protocol list, screw all that
 
	{
 
		$protocol_ok = true;
 
	}
 
		
 
	// add the url to the database
 
	if ( $protocol_ok && $lilurl->add_url($longurl) )
 
	{
 
		$url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'?/'.$lilurl->get_id($longurl);
 

	
 
		$msg = '<p class="success">Your lil URL is: <a href="'.$url.'">'.$url.'</a></p>';
 
	}
 
	elseif ( !$protocol_ok )
 
	{
 
		$msg = '<p class="error">Invalid protocol!</p>';
 
	}
 
	else
 
	{
 
		$msg = '<p class="error">Creation of your lil URL failed for some reason.</p>';
 
	}
 
}
 
else // if the form hasn't been submitted, look for an id to redirect to
 
{
 
	if ( REWRITE ) // check the URI if we're using mod_rewrite
 
	{
 
		$explodo = explode('/', $_SERVER['REQUEST_URI']);
 
		$id = mysqli_escape_string($connect, $explodo[count($explodo)-1]);
 
	}
 
	else // otherwise, just make it empty
 
	{
 
		$id = '';
 
	}
 
	
 
	// if the id isn't empty and it's not this file, redirect to it's url
 
	if ( $id != '' && $id != basename($_SERVER['PHP_SELF']) )
 
	{
 
		$location = $lilurl->get_url($id);
 
		
 
		if ( $location != -1 )
 
		{
 
			header('Location: '.$location, TRUE, 301);
 
		}
 
		else
 
		{
 
			$msg = '<p class="error">Sorry, but that lil URL isn\'t in our database.</p>';
 
		}
 
	}
 
}
 

	
 
// print the form
 

	
 
?>
 

	
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 

	
 
<html>
 

	
 
	<head>
 
		<title><?php echo PAGE_TITLE; ?></title>
 
		
 
		<style type="text/css">
 
		body {
 
			font: .8em "Trebuchet MS", Verdana, Arial, Sans-Serif;
 
			text-align: center;
 
			color: #333;
 
			background-color: #fff;
 
			margin-top: 5em;
 
		}
 
		
 
		h1 {
 
			font-size: 2em;
 
			padding: 0;
 
			margin: 0;
 
		}
 

	
 
		h4 {
 
			font-size: 1em;
 
			font-weight: bold;
 
		}
 
		
 
		form {
 
			width: 28em;
 
			background-color: #eee;
 
			border: 1px solid #ccc;
 
			margin-left: auto;
 
			margin-right: auto;
 
			padding: 1em;
 
		}
 

	
 
		fieldset {
 
			border: 0;
 
			margin: 0;
 
			padding: 0;
 
		}
 
		
 
		a {
 
			color: #09c;
 
			text-decoration: none;
 
			font-weight: bold;
 
		}
 

	
 
		a:visited {
 
			color: #07a;
 
		}
 

	
 
		a:hover {
 
			color: #c30;
 
		}
 

	
 
		.error, .success {
 
			font-size: 1.2em;
 
			font-weight: bold;
 
		}
 
		
 
		.error {
 
			color: #ff0000;
 
		}
 
		
 
		.success {
 
			color: #000;
 
		}
 
		
 
		</style>
 

	
 
	</head>
 
	
 
	<body onload="document.getElementById('longurl').focus()">
 
		
 
		<h1><?php echo PAGE_TITLE; ?></h1>
 
		
 
		<?php echo $msg; ?>
 
		
 
		<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
 
		
 
			<fieldset>
 
				<label for="longurl">Enter a long URL:</label>
 
				<input type="text" name="longurl" id="longurl" />
 
				<input type="submit" name="submit" id="submit" value="Make it lil&#180;!" />
 
			</fieldset>
 
		
 
		</form>
 

	
 
		<h4>Powered by <a href="http://lilurl.sourceforge.net">lilURL</a> with some <a href="https://echedeylr.tk/lilURL">changes</a>.</h4>
 
	
 
	</body>
 

	
 
</html>
 
		
lilurl.sql
Show inline comments
 
new file 100644
 
-- MySQL dump 8.22
 
--
 
-- Host: localhost    Database: lilurl
 
---------------------------------------------------------
 
-- Server version	3.23.57
 

	
 
--
 
-- Table structure for table 'lil_urls'
 
--
 

	
 
CREATE TABLE lil_urls (
 
  id varchar(255) NOT NULL default '',
 
  url text,
 
  date timestamp NOT NULL,
 
  PRIMARY KEY  (id)
 
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 

	
 
--
 
-- Dumping data for table 'lil_urls'
 
--
 

	
 

	
 

	
0 comments (0 inline, 0 general)