<?php
/*
 *      index.php
 *        
 *      Copyright 2009 Julius Beckmann
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

// MySQL login 
define('DB_USER''username');
define('DB_PASS''password');
define('DB_HOST''localhost');
define('DB_DATABASE''database');

// The Url of this page
define('URL_BASE''http://juliusbeckmann.de/url_shortener/');


require_once(
'class.url_shortener.php');

// Connect to MySQL
if (!mysql_connect(DB_HOSTDB_USERDB_PASS))
    die(
'Database connect failed: '.mysql_error());
if (!
mysql_select_db(DB_DATABASE))
    die(
'Database connect failed: '.mysql_error());

// Create and prepare our url_shortener
$us = new url_shortener;
// Disallow links to our own site
$us->url_parts_forbidden[] = URL_BASE;


/*
 * It is YOUR turn to make safe magiq quotes does not break your urls
 * The class will not do this for you.
 */

// Handle redirect
$redirect_error false;
if(
$_GET['__key__']) {
    
$red $us->get_redirect($_GET['__key__']);
    if(
$red) {
        
// Hint: FIRST do the header()
        // and afterwards log the hit.
        
header('Location: '.$red['url'], true302);
        @
ob_flush();
        @
flush();
        
$us->log_redirect($_GET['__key__']);
        die();
    }else{
        
$redirect_error true;
        echo 
'Could not find the a link for your request.';
    }
}

?>
<html>
    <head>
        <title>URL Shorten Class written in PHP and MySQL - by Julius Beckmann</title>
    </head>
    <body>
<?php

// Handle new redirect inserts
if($_POST['url']) {
    
$ins $us->new_redirect($_POST['url']);
    if(
$ins) {
        echo 
'Inserted your link: <br/>';
        echo 
'URL: '.$ins['url'].'<br/>';
        echo 
'KEY: '.$ins['key'].'<br/>';
        echo 
'Short link: '.URL_BASE.$ins['key'].'<br/>';
    }else{
        echo 
'Could not insert your link, please try again.<br/>
        Your link needs to look like this: <b>http(s)://somesite.com/maybe_a_page.html</b>'
;
    }
}

// Our shorten Form
echo '<form action="./" method="POST">
    Shorten your link: 
    <input name="url" value="put your url here">
    <input type="submit" value="Shorten">
</form>'
;

echo 
'<br/>
    We have already '
.$us->count_urls().' URLs shortened!
<br/><br/>'
;
?>
<p>This page is a example page for my URL shortener Class.
<br/>
Please read the documentation before using it:<br/>
<a href="http://juliusbeckmann.de/classes/">http://juliusbeckmann.de/classes/</a>
<br/>
You can get the sources here:<br/>
<a href="http://juliusbeckmann.de/classes/src/url_shortener/url_shortener.zip">http://juliusbeckmann.de/classes/src/url_shortener/url_shortener.zip</a>
</p>
    
    </body>
</html>