<?php if (!defined('PmWiki')) exit();

/*	=== Bloge-ShortUrl ===
 *	Copyright 2009 Eemeli Aro <eemeli@gmail.com>
 *
 *	Get short URLs on your own site
 *
 *	Developed and tested using PmWiki 2.2.x
 *
 *	To use, add the following to a configuration file:

		include_once("$FarmD/cookbook/bloge-shorturl.php");

 *	This is a part of the Bloge bundle of recipes, but may be used by itself.
 *	For more information, please see the online documentation at
 *		http://www.pmwiki.org/wiki/Cookbook/Bloge and at
 *		http://www.pmwiki.org/wiki/Cookbook/Bloge-ShortUrl
 *
 *	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.
 */

$RecipeInfo['Bloge-ShortUrl']['Version'] = '2009-08-28';

SDV($ShortUrlPage, 'Site.ShortUrl');
SDV($ShortUrlGroup, 's');
SDV($ShortUrlPrefix, PUE(empty($EnablePathInfo)
	? "$ScriptUrl?n=$ShortUrlGroup."
	: "$ScriptUrl/$ShortUrlGroup/"));

if (preg_match("#^{$ShortUrlGroup}[./]#i", $pagename))
	$HandleActions['browse'] = 'HandleShortUrl';

$FmtPV['$ShortUrl'] = 'ShortUrlGetCode($pn,$su_urlpage,"url")';

## Short URL Auto-Discovery <http://wiki.snaplog.com/short_url>
SDV($HTMLHeaderFmt['shorturl'], 'function:ShortUrlHeader');
function ShortUrlHeader($pagename) { echo ShortUrlGetCode($pagename, $urlpage, 'link'); }

$EditFunctions[] = 'ShortUrlAdd';

function ShortUrlGetPage($code, &$urlpage) {
	global $ShortUrlPage;
	if (empty($urlpage)) $urlpage = ReadPage($ShortUrlPage, READPAGE_CURRENT);
	if (empty($urlpage['text'])) return FALSE;

	if (preg_match("/^$code +(.*)$/m", $urlpage['text'], $m)) return $m[1];
	else return FALSE;
}

function ShortUrlGetCode($pagename, &$urlpage, $fmt='code') {
	global $ShortUrlPage, $ShortUrlPrefix;
	if (empty($urlpage)) $urlpage = ReadPage($ShortUrlPage, READPAGE_CURRENT);
	if (empty($urlpage['text']) || !preg_match("/^([a-z0-9]+) +$pagename$/m", $urlpage['text'], $m))
		return FALSE;
	else switch($fmt) {
		case 'code':	return $m[1];
		case 'url':		return $ShortUrlPrefix.$m[1];
		case 'link':	return "\n<link rel=\"shorturl\" href=\"$ShortUrlPrefix{$m[1]}\" />";
		default:		return $m[1];
	}
}

function HandleShortUrl($pagename, $auth='read') {
	global $ShortUrlPage;
	$code = preg_replace(array('#^.*?[./]#','/[^a-z0-9]/'), '', strtolower($pagename));
	if (empty($code)) Abort("ShortUrl requires a valid code");

	$urlpage = RetrieveAuthPage($ShortUrlPage, $auth, FALSE, READPAGE_CURRENT);
	if (!$urlpage) Abort("ShortUrl error: can't read code map");

	$pagename = ShortUrlGetPage($code, $urlpage);
	if (!$pagename) Abort("ShortUrl code '$code' not found");
	Redirect($pagename);
}

function ShortUrlAdd($pagename, &$page, &$new) {
	global $IsPagePosted, $ShortUrlPage, $ShortUrlFilter;
	if (!$IsPagePosted) return;

	if (!empty($ShortUrlFilter) && !count(MatchPageNames($pagename, $ShortUrlFilter))) return;

	$urlpage = ReadPage($ShortUrlPage);
	$code = ShortUrlGetCode($pagename, $urlpage);
	if ($code || empty($urlpage['time'])) return;

	if (preg_match('/^(.*?\n)?(([a-z0-9]+) .*)$/s', $urlpage['text'], $m)) {
		$code = $m[3];
		do $code = base_convert( base_convert($code, 36, 10) + 1, 10, 36);
			while (ShortUrlGetPage($code, $urlpage));
		$urlpage['text'] = "{$m[1]}$code  $pagename\n{$m[2]}";
	} else {
		$code = '2';
		$urlpage['text'] = empty($urlpage['text'])
			? "[[Cookbook:Bloge-ShortUrl]] short URL map\n\n[@\n$code  $pagename\n@]\n"
			: "$code  $pagename\n{$urlpage['text']}";
	}
	WritePage($ShortUrlPage, $urlpage);
}