0 0 5
Echedey López Romero - 6 months ago 2018-09-23 14:31:42
echedey@localhost.localdomain
Initial commit
5 files changed with 441 insertions and 0 deletions:
↑ Collapse Diff ↑
 
new file 100644
1
 
This is a fork from lilURL (http://lilurl.sourceforge.net) with some
2
 
modern and personal changes.
3
 

	
4
 
-----------------------------------------------------------------------
5
 

	
6
 
To install:
7
 

	
8
 
1. Create a MySQL database and user for lilURL. 
9
 

	
10
 
2. Import the lilurl.sql file:
11
 

	
12
 
      (( like so:
13
 
      
14
 
         mysql -u <lilurl_user> -p <lilurl_db> < lilurl.sql
15
 
      
16
 
      ))
17
 

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

	
20
 
4. Set up mod_rewrite, if necessary
21
 

	
22
 
      (( a .htaccess file with the lines:
23
 
   
24
 
         RewriteEngine On
25
 
         RewriteRule (.*) index.php
26
 
   
27
 
        should suffice ))
28
 

	
29
 
5. Buy 15 donuts and eat them all in one sitting.
 
new file 100755
1
 
<?php /* conf.php ( config file ) */
2
 

	
3
 
// page title
4
 
define('PAGE_TITLE', 'lilURL');
5
 

	
6
 
// MySQL connection info
7
 
define('MYSQL_USER', '');
8
 
define('MYSQL_PASS', '');
9
 
define('MYSQL_DB', '');
10
 
define('MYSQL_HOST', '');
11
 

	
12
 
// MySQL tables
13
 
define('URL_TABLE', 'lil_urls');
14
 

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

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

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

	
24
 
?>
 
new file 100755
1
 
<?php /* lilurl.php ( lilURL class file ) */
2
 

	
3
 
class lilURL
4
 
{
5
 
	// constructor
6
 
	function lilURL()
7
 
	{
8
 
		// open mysql connection
9
 
		mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB) or die('Could not connect to database');	
10
 
	}
11
 

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

	
19
 
		if ( mysqli_num_rows($result) )
20
 
		{
21
 
			$row = mysqli_fetch_array($result);
22
 
			return $row['id'];
23
 
		}
24
 
		else
25
 
		{
26
 
			return -1;
27
 
		}
28
 
	}
29
 

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

	
37
 
		if ( mysqli_num_rows($result) )
38
 
		{
39
 
			$row = mysqli_fetch_array($result);
40
 
			return $row['url'];
41
 
		}
42
 
		else
43
 
		{
44
 
			return -1;
45
 
		}
46
 
	}
47
 
	
48
 
	// add a url to the database
49
 
	function add_url($url)
50
 
	{
51
 
		$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
52
 
		// check to see if the url's already in there
53
 
		$id = $this->get_id($url);
54
 
		
55
 
		// if it is, return true
56
 
		if ( $id != -1 )
57
 
		{
58
 
			return true;
59
 
		}
60
 
		else // otherwise, put it in
61
 
		{
62
 
			$id = $this->get_next_id($this->get_last_id());
63
 
			$q = 'INSERT INTO '.URL_TABLE.' (id, url, date) VALUES ("'.$id.'", "'.$url.'", NOW())';
64
 

	
65
 
			return mysqli_query($connect, $q);
66
 
		}
67
 
	}
68
 

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

	
76
 
		if ( mysqli_num_rows($result) )
77
 
		{
78
 
			$row = mysqli_fetch_array($result);
79
 
			return $row['id'];
80
 
		}
81
 
		else
82
 
		{
83
 
			return -1;
84
 
		}
85
 
	}	
86
 

	
87
 
	// return the next id
88
 
	function get_next_id($last_id)
89
 
	{
90
 
		$connect = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);
91
 
		// if the last id is -1 (non-existant), start at the begining with 0
92
 
		if ( $last_id == -1 )
93
 
		{
94
 
			$next_id = 0;
95
 
		}
96
 
		else
97
 
		{
98
 
			// loop through the id string until we find a character to increment
99
 
			for ( $x = 1; $x <= strlen($last_id); $x++ )
100
 
			{
101
 
				$pos = strlen($last_id) - $x;
102
 

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

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

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

	
131
 
		return $next_id;
132
 
	}
133
 

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

	
142
 
		$id .= 0;
143
 

	
144
 
		return $id;
145
 
	}
146
 

	
147
 
	// increment a character to the next alphanumeric value and return the modified id
148
 
	function increment_id($id, $pos)
149
 
	{		
150
 
		$char = $id[$pos];
151
 
		
152
 
		// add 1 to numeric values
153
 
		if ( is_numeric($char) )
154
 
		{
155
 
			if ( $char < 9 )
156
 
			{
157
 
				$new_char = $char + 1;
158
 
			}
159
 
			else // if we're at 9, it's time to move to the alphabet
160
 
			{
161
 
				$new_char = 'a';
162
 
			}
163
 
		}
164
 
		else // move it up the alphabet
165
 
		{
166
 
			$new_char = chr(ord($char) + 1);
167
 
		}
168
 

	
169
 
		$id[$pos] = $new_char;
170
 
		
171
 
		// set all characters after the one we're modifying to 0
172
 
		if ( $pos != (strlen($id) - 1) )
173
 
		{
174
 
			for ( $x = ($pos + 1); $x < strlen($id); $x++ )
175
 
			{
176
 
				$id[$x] = 0;
177
 
			}
178
 
		}
179
 

	
180
 
		return $id;
181
 
	}
182
 

	
183
 
}
184
 

	
185
 
?>
 
new file 100755
1
 
<?php /* index.php ( lilURL implementation ) */
2
 

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

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

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

	
16
 
	// set the protocol to not ok by default
17
 
	$protocol_ok = false;
18
 
	
19
 
	// if there's a list of allowed protocols, 
20
 
	// check to make sure that the user's url uses one of them
21
 
	if ( count($allowed_protocols) )
22
 
	{
23
 
		foreach ( $allowed_protocols as $ap )
24
 
		{
25
 
			if ( strtolower(substr($longurl, 0, strlen($ap))) == strtolower($ap) )
26
 
			{
27
 
				$protocol_ok = true;
28
 
				break;
29
 
			}
30
 
		}
31
 
	}
32
 
	else // if there's no protocol list, screw all that
33
 
	{
34
 
		$protocol_ok = true;
35
 
	}
36
 
		
37
 
	// add the url to the database
38
 
	if ( $protocol_ok && $lilurl->add_url($longurl) )
39
 
	{
40
 
		$url = 'http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']).'?/'.$lilurl->get_id($longurl);
41
 

	
42
 
		$msg = '<p class="success">Your lil URL is: <a href="'.$url.'">'.$url.'</a></p>';
43
 
	}
44
 
	elseif ( !$protocol_ok )
45
 
	{
46
 
		$msg = '<p class="error">Invalid protocol!</p>';
47
 
	}
48
 
	else
49
 
	{
50
 
		$msg = '<p class="error">Creation of your lil URL failed for some reason.</p>';
51
 
	}
52
 
}
53
 
else // if the form hasn't been submitted, look for an id to redirect to
54
 
{
55
 
	if ( REWRITE ) // check the URI if we're using mod_rewrite
56
 
	{
57
 
		$explodo = explode('/', $_SERVER['REQUEST_URI']);
58
 
		$id = mysqli_escape_string($connect, $explodo[count($explodo)-1]);
59
 
	}
60
 
	else // otherwise, just make it empty
61
 
	{
62
 
		$id = '';
63
 
	}
64
 
	
65
 
	// if the id isn't empty and it's not this file, redirect to it's url
66
 
	if ( $id != '' && $id != basename($_SERVER['PHP_SELF']) )
67
 
	{
68
 
		$location = $lilurl->get_url($id);
69
 
		
70
 
		if ( $location != -1 )
71
 
		{
72
 
			header('Location: '.$location, TRUE, 301);
73
 
		}
74
 
		else
75
 
		{
76
 
			$msg = '<p class="error">Sorry, but that lil URL isn\'t in our database.</p>';
77
 
		}
78
 
	}
79
 
}
80
 

	
81
 
// print the form
82
 

	
83
 
?>
84
 

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

	
88
 
<html>
89
 

	
90
 
	<head>
91
 
		<title><?php echo PAGE_TITLE; ?></title>
92
 
		
93
 
		<style type="text/css">
94
 
		body {
95
 
			font: .8em "Trebuchet MS", Verdana, Arial, Sans-Serif;
96
 
			text-align: center;
97
 
			color: #333;
98
 
			background-color: #fff;
99
 
			margin-top: 5em;
100
 
		}
101
 
		
102
 
		h1 {
103
 
			font-size: 2em;
104
 
			padding: 0;
105
 
			margin: 0;
106
 
		}
107
 

	
108
 
		h4 {
109
 
			font-size: 1em;
110
 
			font-weight: bold;
111
 
		}
112
 
		
113
 
		form {
114
 
			width: 28em;
115
 
			background-color: #eee;
116
 
			border: 1px solid #ccc;
117
 
			margin-left: auto;
118
 
			margin-right: auto;
119
 
			padding: 1em;
120
 
		}
121
 

	
122
 
		fieldset {
123
 
			border: 0;
124
 
			margin: 0;
125
 
			padding: 0;
126
 
		}
127
 
		
128
 
		a {
129
 
			color: #09c;
130
 
			text-decoration: none;
131
 
			font-weight: bold;
132
 
		}
133
 

	
134
 
		a:visited {
135
 
			color: #07a;
136
 
		}
137
 

	
138
 
		a:hover {
139
 
			color: #c30;
140
 
		}
141
 

	
142
 
		.error, .success {
143
 
			font-size: 1.2em;
144
 
			font-weight: bold;
145
 
		}
146
 
		
147
 
		.error {
148
 
			color: #ff0000;
149
 
		}
150
 
		
151
 
		.success {
152
 
			color: #000;
153
 
		}
154
 
		
155
 
		</style>
156
 

	
157
 
	</head>
158
 
	
159
 
	<body onload="document.getElementById('longurl').focus()">
160
 
		
161
 
		<h1><?php echo PAGE_TITLE; ?></h1>
162
 
		
163
 
		<?php echo $msg; ?>
164
 
		
165
 
		<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
166
 
		
167
 
			<fieldset>
168
 
				<label for="longurl">Enter a long URL:</label>
169
 
				<input type="text" name="longurl" id="longurl" />
170
 
				<input type="submit" name="submit" id="submit" value="Make it lil&#180;!" />
171
 
			</fieldset>
172
 
		
173
 
		</form>
174
 

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

	
179
 
</html>
180
 
		
 
new file 100644
1
 
-- MySQL dump 8.22
2
 
--
3
 
-- Host: localhost    Database: lilurl
4
 
---------------------------------------------------------
5
 
-- Server version	3.23.57
6
 

	
7
 
--
8
 
-- Table structure for table 'lil_urls'
9
 
--
10
 

	
11
 
CREATE TABLE lil_urls (
12
 
  id varchar(255) NOT NULL default '',
13
 
  url text,
14
 
  date timestamp NOT NULL,
15
 
  PRIMARY KEY  (id)
16
 
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
17
 

	
18
 
--
19
 
-- Dumping data for table 'lil_urls'
20
 
--
21
 

	
22
 

	
23
 

	
0 comments (0 inline, 0 general)