<?php if (!defined('PmWiki')) exit();
/*  Copyright 2004, 2006 Nils Knappmeier
    Copyright 2004 Patrick R. Michaud (pmichaud@pobox.com)
    Copyright 2006 Hagan Fox
    Copyright 2006 Andy Kaplan-Myrth

    This file is addlink.php; 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.  

    Addlink.php creates an "add link" bookmarklet that makes it easy
    to bookmark pages you find while surfing the web into a wiki
    page.  This script was originally authored for PmWiki by Nils 
    Knappmeier, and updated for PmWiki 2 by Patrick R. Michaud on
    2004-11-30.  Modified by Hagan Fox on 2006-01-24 and 2006-04-09.
    Modified by Andy Kaplan-Myrth on 2006-05-31 to work with Kind-ofBlog.

    Install the script by copying the it to the cookbook/ directory
    and adding the following to the configuration file:

    ## Enable the AddLink Bookmarklet recipe.
    if ($action == 'edit' || $action == 'browse' || $action == 'addlink') {
      include_once("$FarmD/cookbook/kob-addlink.php"); }
      $KoBGroup = "Blog"; # Set this to your blog's Group

*/

# Add a page variable to show the version.
$FmtPV['$AddlinkVersion'] = "'20060531-kob'";

# Add the (:addlink [PageName]:) markup and HandleAddLink actions.
Markup('addlink', 'inline', '/\\(:addlink\\s*(.*?):\\)/e', 
  "Keep(CreateBookmarklet(\$pagename,'$1'))");
$HandleActions['addlink'] = 'HandleAddLink';

# Function to create the bookmarklet
function CreateBookmarklet($pagename, $linkpage) {
  global $ScriptUrl, $KoBGroup;
  if ($linkpage) $pagename = MakePageName($pagename, $linkpage);
  $mozlink = "<a href=\"javascript:var t=new Date();var d=t.getDate()+'';var m=t.getMonth()+1+'';var y=t.getYear()+1900+'';if(d.length==1)d='0'+d;if(m.length==1)m='0'+m;selection=document.getSelection();if(!document.getSelection())selection=prompt('Text:');tags=prompt('Tags:');document.location.href='$ScriptUrl/$KoBGroup/'+y+m+d+'?action=addlink&url='+encodeURIComponent(document.location.href)+'&selected='+encodeURIComponent(selection)+'&title='+encodeURIComponent(document.title)+'&tags='+tags,'bookmark'\" title=\"$[Blog this page]\">$[Blog this page]</a>";
  $ielink = "<a href=\"javascript:var t=new Date();var d=t.getDate()+'';var m=t.getMonth()+1+'';var y=t.getYear()+'';if(d.length==1)d='0'+d;if(m.length==1)m='0'+m;selection=document.selection.createRange().text;if(!selection)selection=prompt('Text:');tags=prompt('Tags:');document.location.href='$ScriptUrl/$KoBGroup/'+y+m+d+'?action=addlink&url='+encodeURIComponent(document.location.href)+'&selected='+encodeURIComponent(selection)+'&title='+encodeURIComponent(document.title)+'&tags='+tags,'bookmark'\" title=\"$[Blog this page]\">$[Blog this page]</a>";
  return FmtPageName("$mozlink (Moz.)<br />$ielink (IE)", $pagename);

}

# Use the site's default edit page.
if ($action=='addlink') {
  $action = 'edit';
  $OldEditHandler = $HandleActions['edit'];
  $HandleActions['edit'] = 'HandleAddLink';
}

# Function to handle ?action=addlink (prepends the url to the page and
# then passes control to the edit function).
function HandleAddLink($pagename) {
  global  $OldEditHandler, $EnableAddLinkToEnd;
  $posttime = date("Y-n-j G:i");
  Lock(2);
  $page = RetrieveAuthPage($pagename, 'edit');
  if (!$page) Abort("?cannot edit $pagename");
  $text = addslashes($page['text']);
  if (@$_REQUEST['url']) {
    if (IsEnabled($EnableAddLinkToEnd,0))
      $text .= "\n\n(:blogentry title=\"{$_REQUEST['title']}\" time=\"$posttime\" permalink=\"$pagename\":)\n(:tags {$_REQUEST['tags']}:)\n\n{$_REQUEST['selected']} -> [[{$_REQUEST['url']}|link]]\n\n(:blogentryend:)\n\n";
     else $text = "\n(:blogentry title=\"{$_REQUEST['title']}\" time=\"$posttime\" permalink=\"$pagename\":)\n(:tags {$_REQUEST['tags']}:)\n\n{$_REQUEST['selected']} -> [[{$_REQUEST['url']}|link]]\n\n(:blogentryend:)\n\n" . $text;

  }
  $action = 'edit';
  $_POST['text'] = $text;
  $OldEditHandler($pagename);
}