Commit 7dad536e by Vitalik

Init

parents
Pipeline #169 skipped
.htaccess
config.php
\ No newline at end of file
# MBA Strategy Shortener
## Getting Started
```bash
$ git clone ssh://git@gitlab.mbastrategy.com:10022/root/mbas-shortener.git
$ cd mbas-shortener
$ cp config.php.example config.php
$ php install.php
```
## PHP Routing from Subdirectory
### Apache (.htaccess)
```
RewriteEngine On
RewriteBase /my-site/app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
```
### Nginx
```nginx
server {
location / {
try_files $uri $uri/ /my-site/app/index.php?$args;
}
}
```
\ No newline at end of file
<?php
return array(
'database' => 'dibi',
'username' => 'root',
'password' => 'z',
);
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>URL Shortener</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="col-lg-10 col-lg-offset-1">
<div class="page-header clearfix">
<h1 class="text-muted">MBAStrategy Shortener</h1>
</div>
<div class="row">
<div class="col-xs-12 col-md-8 col-lg-7">
<div class="well">
<form action="#">
<label for="long_url"><strong>Paste your long URL here:</strong></label>
<div class="input-group">
<input id="long_url" type="text"
class="form-control"
placeholder="http(s)://..."
tabindex="1"
/>
<span class="input-group-btn">
<button type="submit" class="btn btn-default" id="shorten">Shorten URL</button>
</span>
</div>
</form>
</div>
</div>
<div class="col-xs-12 col-md-4 col-lg-5">
<div id="short_url_block" class="well well-xs hide">
<label for="short_url"><strong>Copy to clipboard:</strong></label>
<div class="input-group">
<input id="short_url" type="text"
class="form-control"
tabindex="2"
title="Press CTRL-C to copy"
data-toggle="tooltip"
data-placement="top"
data-trigger="focus"
onclick="$(this).select()"
/>
<span class="input-group-btn">
<button id="remove_url" type="button"
class="btn btn-default"
title="Remove URL"
data-toggle="tooltip"
>
<i class="glyphicon glyphicon-trash text-danger"></i>
</button>
<button id="copy_short_url" type="button"
class="btn btn-default"
title="Copy to clipboard"
data-toggle="tooltip"
data-clipboard-target="#short_url"
>
<i class="glyphicon glyphicon-copy"></i>
</button>
</span>
</div>
</div>
</div>
</div>
<table class="table responsive no-wrap" width="100%">
<thead>
<tr>
<th></th>
<th>LONG URL</th>
<th>CREATED</th>
<th>SHORT URL</th>
<th class="text-right">CLICKS</th>
</tr>
</thead>
</table>
</div>
</div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="//cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/1.5.5/clipboard.min.js"></script>
<script>
var SITE_PATH = location.host + location.pathname.replace('create.php', '');
var SITE_URL = location.protocol + '//' + SITE_PATH;
jQuery(function ($) {
$('[data-toggle="tooltip"]').tooltip();
new Clipboard('#copy_short_url');
var table = $('table').DataTable({
ajax: SITE_URL + 'index.php',
responsive: true,
ordering: false,
columns: [
{ data: 'id' },
{ data: 'long_url' },
{ data: 'created' },
{ data: 'short_url' },
{ data: 'clicks' },
],
columnDefs: [
{
render: function (data, type, row) {
return [
'<input type="checkbox" class="show-url"',
' data-id="', data, '" data-short-url="', row.short_url, '"',
'>'
].join('');
},
targets: 0
},
{
render: function (data, type, row) {
var text = data;
if (text.length > 40) {
text = text.toString().slice(0, 40) + '...';
}
return [
'<a href="', data, '" target="_blank">',
text,
'</a>'
].join('');
},
targets: 1
},
{
render: function (data) {
var url = SITE_URL + data,
text = SITE_PATH + data;
return [
'<a href="', url, '" target="_blank">',
text,
'</a>'
].join('');
},
targets: 3
},
{
className: 'text-right',
targets: 4
}
]
});
$('#shorten').on('submit click', function (e) {
e.preventDefault();
var long_url = $('#long_url').val();
$.ajax({
type: 'POST',
url: SITE_URL + 'index.php',
data: {
long_url: long_url
},
dataType: 'json',
success: function (data) {
if (data != false) {
table.ajax.reload();
show_short_url(data.id, data.short_url);
}
}
})
});
$('#remove_url').on('click', function (e) {
e.preventDefault();
var id = $(e.currentTarget).data('id');
if (!id) return;
$.ajax({
type: 'POST',
url: SITE_URL + 'index.php',
data: {
id: id
},
success: function () {
table.ajax.reload();
$('#short_url_block').addClass('hide');
}
});
});
$('table').on('click', '.show-url', function (e) {
var id = $(e.target).data('id'),
url = $(e.target).data('short-url');
show_short_url(id, url);
});
});
function show_short_url(id, url) {
$('#remove_url').data('id', id);
$('#short_url_block').removeClass('hide');
$('#short_url').val(SITE_URL + url)
.select()
.focus();
}
</script>
</body>
</html>
\ No newline at end of file
<?php
function connect_db()
{
$config = require PATH_BASE . '/config.php';
$link = mysql_connect('localhost', $config['username'], $config['password']);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($config['database'], $link);
return $link;
}
function load_links()
{
$list = array();
$db = connect_db();
$sql = "SELECT id,
long_url, short_url,
DATE_FORMAT(created, '%d %b %Y') AS created,
clicks
FROM links
ORDER BY id DESC";
$result = mysql_query($sql, $db);
while ($row = mysql_fetch_object($result)) {
$list[] = $row;
}
mysql_free_result($result);
mysql_close($db);
return $list;
}
function isset_url($short_url)
{
return (bool) get_url($short_url);
}
function get_url($short_url)
{
$db = connect_db();
$sql = "SELECT id, long_url
FROM links
WHERE short_url = '$short_url'
LIMIT 1";
$result = mysql_query($sql);
$row = mysql_fetch_object($result);
if (empty($row->id)) $row = false;
mysql_free_result($result);
mysql_close($db);
return $row;
}
function delete_url($id)
{
$db = connect_db();
$sql = "DELETE FROM links WHERE id = $id";
mysql_query($sql);
mysql_close($db);
return true;
}
function url_click($id)
{
$db = connect_db();
$sql = "UPDATE links
SET clicks = clicks + 1
WHERE id = $id";
mysql_query($sql);
mysql_close($db);
return true;
}
function validate_url($url)
{
if (empty($url)) return false;
preg_match("/((ftp|http|https):\\/\\/)?/", $url, $matches);
if (empty($matches[0])) {
$url = 'http://' . $url;
}
$url = filter_var($url, FILTER_SANITIZE_URL);
$url = filter_var($url, FILTER_VALIDATE_URL);
return $url;
}
function create($long_url)
{
$long_url = validate_url($long_url);
if (!$long_url) return false;
$short_url = md5($long_url);
$short_url = substr($short_url, 0, 8);
if (isset_url($short_url)) return false;
$sql = "INSERT INTO links (long_url, short_url)
VALUES(
'$long_url',
'$short_url'
);";
$db = connect_db();
$result = mysql_query($sql);
$id = mysql_insert_id();
mysql_close($db);
return array(
'id' => $id,
'short_url' => $short_url
);
}
\ No newline at end of file
<?php
define('PATH_BASE', __DIR__);
require_once PATH_BASE . '/helper.php';
if (count($_GET)) {
if (isset($_GET['_'])) {
die(json_encode(array('data' => load_links())));
}
$hash = str_replace('/', '', key($_GET));
$urlData = get_url($hash);
if (!empty($urlData)) {
url_click($urlData->id);
header('Location: ' . $urlData->long_url);
}
}
if (isset($_POST['long_url'])) {
$long_url = $_POST['long_url'];
$data = create($long_url);
die(json_encode($data));
}
if (isset($_POST['id'])) {
delete_url($_POST['id']);
die(json_encode(true));
}
\ No newline at end of file
<?php
if (!file_exists(__DIR__ . '/config.php')) {
die("Copy `config.php.example` file to `config.php` file\n");
}
$config = require __DIR__ . '/config.php';
$link = mysql_connect(
'localhost',
$config['username'],
$config['password']
);
if (!$link) {
die("Could not connect: " . mysql_error());
}
$db_selected = mysql_select_db($config['database'], $link);
if (!$db_selected) {
$sql = "CREATE DATABASE {$config['database']}";
if (mysql_query($sql, $link)) {
echo "Database `{$config['database']}` created successfully\n";
} else {
echo "Error creating database: " . mysql_error() . "\n";
}
}
$sql = "CREATE TABLE IF NOT EXISTS `{$config['database']}`.`links` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`long_url` text NOT NULL,
`short_url` text NOT NULL,
`created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`clicks` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
if (mysql_query($sql, $link)) {
echo "Table `links` created successfully\n";
} else {
echo "Error creating table: " . mysql_error() . "\n";
}
mysql_close($link);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment