<?php

	include_once(dirname(__FILE__).'/../lib/config.inc.php');
	include_once(dirname(__FILE__).'/../lib/functions.inc.php');
	include_once(dirname(__FILE__).'/../lib/UrlShortener.class.php');

	$urlShortener = new UrlShortener();

	$urlParam = getRequestParameter("url");
	
	header("Content-type: text/plain");
	if (strlen($urlParam) == 0) {
		echo "";
	} else {
		if (str_startswith($urlParam, SERVICE_BASE_URL) == true) {
			// expand
			$shortId = substr($urlParam, strlen(SERVICE_BASE_URL));
			$expandedEntry = $urlShortener->expandUrl($shortId, true);
			if ($expandedEntry != NULL) {
				echo $expandedEntry['url'];
			} else {
				echo "";
			}
		} else {
			// shorten
			$shortId = $urlShortener->shortenUrl($urlParam, NULL);
			if ($shortId != NULL) {
				echo SERVICE_BASE_URL . $shortId;
			} else {
				echo "";
			}
		}
	}	
