<?php /*>*/ if (!defined('PmWiki')) exit();
/*
 * applet - Embed java applets into PmWiki 2.0 pages
 * Copyright 2005-2007 by D.Faure (dfaure@cpan.org)
 * Thanks to Shayne Steele (steele <AT> cs <DOT> fsu <DOT> edu) for the
 * article at: http://ww2.cs.fsu.edu/~steele/XHTML/appletObject.html
 *
 * Modifications for clickable screenshots and alignment on page (see "# fsmod" below) by Frank Schweickert (appletimage <AT> verbis.net)
 *   requires JavaScript, DHTML capable browser, tested with Firefox 3 and IE 7
 *   usage: (:applet clickimg="picture.gif" clickalt="alternative text" clickcaption="caption beneath applet/image" clickalign="[left|center|right]" codebase=...
 *    optional in config.php: $AppletImageUploadLinkFmt   -  title of an upload link beneath the running applet, for as long as no image has been uploaded yet.
 *                            $AppletImageClickMeHintFmt  -  text beneath the still image to indicate that a mouse click will start the applet
 *                            $AppletImageDefaultAlign    -  [left|center|right] alignment of the applet/image when parameter clickalign is omitted
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * See http://www.pmwiki.org/wiki/Cookbook/Applet for info.
 */
$RecipeInfo['AppletImage']['Version'] = '2008-12-13';                                                      # fsmod

Markup('applet', '<split',                                                                            # fsmod: earlier to deal with image caption wiki syntax "|..."
       "/\\(:applet(\\s+.*)\\s*:\\)/e", "AppletMarkup(PSS('$1'))");


function AppletMarkup($args) {
  return Applet(ParseArgs($args, '(?>([\\w\\.]+)[:=])'));
}

function Applet($opt) {
  global $FarmPubDirUrl, $AppletRootCodebase, $AppletClassid, $AppletCabUrl,
         $AppletNotHandledFmt, $AppletParams,
         $UploadDir, $UploadUrlFmt, $UploadPrefixFmt,                                                       # fsmod
         $AppletImageUploadLinkFmt, $AppletImageClickMeHintFmt, $AppletImageDefaultAlign;                   # fsmod
  $pagename = ResolvePageName($pagename);              # determine image upload directory                   # fsmod
  $Group = PageVar($pagename, '$Group');                                                                    # fsmod
  eval("\$MyUploadDir = \"$UploadDir$UploadPrefixFmt/\";");                                                 # fsmod
  eval("\$MyUploadUrl = \"$UploadUrlFmt$UploadPrefixFmt/\";");                                              # fsmod

  SDV($AppletRootCodebase, "$FarmPubDirUrl/applets");
  SDV($AppletClassid, "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93");
  SDV($AppletCabUrl, "http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab");
  SDV($AppletNotHandledFmt, "<strong>
  This browser does not have a Java Plug-in.<br />
  <a href=\"http://java.sun.com/products/plugin/downloads/index.html\">
  Get the latest Java Plug-in here.</a>
</strong>\n");
  $opt = array_merge((array)$AppletParams, (array)$opt);
  unset($opt['#'], $opt['-'], $opt['+'], $opt['']);

  $code    = @$opt['code'];    unset($opt['code']);
  $archive = @$opt['archive']; unset($opt['archive']);
  $width   = @$opt['width'];   unset($opt['width']);
  $height  = @$opt['height'];  unset($opt['height']);
  $name  = @$opt['name'];  unset($opt['name']);             # e.g. for scriptable applets             # fsmod
  $id  = @$opt['id'];  unset($opt['id']);                                                             # fsmod

  $codebase = @preg_replace('%^(?!http://)/?%', "$AppletRootCodebase/", $opt['codebase']);            # fsmod  allow for absolute urls
  unset($opt['codebase']);

  $img = @$opt['clickimg']; unset($opt['clickimg']);                                                  # fsmod
  $alt = @$opt['clickalt']; unset($opt['clickalt']);                                                  # fsmod
  $caption = @$opt['clickcaption']; unset($opt['clickcaption']);                                            # fsmod
  $align = @$opt['clickalign']; unset($opt['clickalign']);                                            # fsmod
  if (!$align) @$align = $AppletImageDefaultAlign;                                                    # fsmod
  if (!preg_match("/(left|center|right)/",$align)) $align="";                                         # fsmod                                                                     # fsmod

  $out = array();

  $out[] = "
<!--[if !IE]> Firefox and others will use outer object -->
<object classid=\"java:$code\"";
  if($archive) $out[] = "        archive=\"$archive\"";
  $out[] = "        codebase=\"$codebase\"";                                                        # fsmod
  if ($id) $out[] = "        id=\"$id\"";                                                           # fsmod  (added id/name)
  if ($name) $out[] = "        name=\"$name\"";                                                     # fsmod
  $out[] = "        width=\"$width\" height=\"$height\"
        type=\"application/x-java-applet\">";

  foreach($opt as $n => $v) $out[] = "  <param name=\"$n\" value=\"$v\" />";

  $out[] = "<!--<![endif]-->
  <!-- MSIE (Microsoft Internet Explorer) will use inner object -->
  <object classid=\"$AppletClassid\"
          codebase=\"$AppletCabUrl\"";                                                              # fsmod
  if ($id) $out[] = "          id=\"$id\"  ";                                                       # fsmod  (added id/name)
  if ($name) $out[] = "          name=\"$name\"";                                                   # fsmod
  $out[] = "          width=\"$width\" height=\"$height\">
    <param name=\"code\" value=\"$code\" />
    <param name=\"codebase\" value=\"$codebase\" />";
  if($archive) $out[] = "    <param name=\"archive\" value=\"$archive\" />";
  foreach($opt as $n => $v) $out[] = "    <param name=\"$n\" value=\"$v\" />";
  $out[] =   "    $AppletNotHandledFmt
  </object>
<!--[if !IE]> close outer object -->
</object>
<!--<![endif]-->";


### fsmod start ###

  SDV($AppletImageUploadLinkFmt, "'-Upload a preview image for this applet-'");
  SDV($AppletImageClickMeHintFmt, "Click on the image to start the applet.");

  if ($img && file_exists($MyUploadDir.$img)) {
  	if (isset($_GET['action']))
  	  $ret = "\n".$MyUploadUrl.$img."\"$alt\"|$caption";  # show plain image for non-browse action (e.g. print, edit preview,...)
  	else {   # e.g. print action, display clickable image
      $object = ''.preg_replace('/[\s\r\n]+/',' ',str_replace('"', '\"', implode(" ", $out)));  # remove line breaks and escape quotes in object tag for use in innerHTML="..."
      $ret=Keep('<img alt=\''.$alt.'\' src=\''.$MyUploadUrl.$img.'\' onclick=\'javascript:parentNode.innerHTML="'.$object.'"\'>');
      if ($AppletImageClickMeHintFmt) $ret .= Keep("<p>").$AppletImageClickMeHintFmt.Keep("</p>");
      $ret = Keep("<div>").$ret.Keep("</div>");  # the innerHTML of this <div> is to be replaced by $object
      if ($caption) $ret .= Keep("<p>").$caption.Keep("</p>");
    }
  }
  else { # either clickimage property or image file missing
    $ret = Keep("<:block>".implode("\n", $out));  # when image file is missing show running applet
    if ($img && !file_exists($MyUploadDir.$img) && $AppletImageUploadLinkFmt && !isset($_GET['action']))
      $ret.=Keep("<p>")."[[(Attach:)$img | $AppletImageUploadLinkFmt]]".Keep("</p>");  # (in on-line view) an upload link for an image
    if ($caption) $ret .= Keep("<p>").$caption.Keep("</p>");
  }
  if ($align) $ret = Keep("<div align='$align'>").$ret.Keep("</div>");
  return $ret;
#### fsmod end ###

}