Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php /* api.php ( lilURL implementation ) */
- require_once 'includes/conf.php'; // <- site-specific settings
- require_once 'includes/lilurl.php'; // <- lilURL class file
- $lilurl = new lilURL();
- $msg = '';
- // if the form has been submitted
- if ( isset($_POST['longurl']) )
- {
- // escape bad characters from the user's url
- $longurl = trim(mysql_escape_string($_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) )
- {
- if ( REWRITE ) // mod_rewrite style link
- {
- $url = 'http://'.$_SERVER['SERVER_NAME'].'/s/'.$lilurl->get_id($longurl);
- }
- else // regular GET style link
- {
- $url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?id='.$lilurl->get_id($longurl);
- }
- // Return a 201 w/ msg body = url
- header('HTTP/1.0 201 Created');
- $msg = $url;
- }
- elseif ( !$protocol_ok )
- {
- // Return 501 - Not Implemented (for unknown protocols)
- header('HTTP/1.0 501 Not Implemented');
- $msg = 'Invalid protocol!';
- }
- else
- {
- // Return a good ole 500 - Server Error
- header('HTTP/1.0 500 Server Error');
- $msg = 'Creation of your ur1 failed for some reason.';
- }
- }
- else // if the form hasn't been submitted, look for an id to redirect to
- {
- if ( isSet($_GET['id']) ) // check GET first
- {
- $id = mysql_escape_string($_GET['id']);
- }
- elseif ( REWRITE ) // check the URI if we're using mod_rewrite
- {
- $explodo = explode('/', $_SERVER['REQUEST_URI']);
- $id = mysql_escape_string($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('HTTP/1.0 200 OK');
- $msg = $location;
- }
- else
- {
- header('HTTP/1.0 404 Not Found');
- $msg = 'Sorry, but that ur1 isn\'t in our database.';
- }
- }
- }
- ?><?php echo $msg; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement