<?php
/*
description : Display XML/SWF Slideshow
author      : Ikuo Obataya
email       : ikuo_obataya@symplus.co.jp
lastupdate  : 2008-03-22
depends     : cache (2008-03-22)
license     : GPL 2 (http://www.gnu.org/licenses/gpl.html)
*/
if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
  require_once(DOKU_INC.'inc/init.php');
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  require_once(DOKU_PLUGIN.'syntax.php');
if (!class_exists('plugin_cache')) @require(DOKU_PLUGIN.'cache/plugin_cache.php');

class syntax_plugin_slideshow extends DokuWiki_Syntax_Plugin {
  var $swfLoc = '';
  var $xmlCache;
  // Constructor
  function syntax_plugin_slideshow(){
    $this->swfLoc = DOKU_BASE.'lib/plugins/slideshow/slideshow.swf';
    $this->xmlCache = new plugin_cache("slideshow",'',"xml");
  }
  function getInfo(){
    return array(
      'author' => 'Ikuo Obataya',
      'email'  => 'ikuo_obataya@symplus.co.jp',
      'date'  => '2008-03-22',
      'name'  => 'XML/SWF Slideshow Plugin',
      'desc'  => 'Create SWF Slideshow by www.maani.us
      <slideshow (filename)>script</slideshow>',
      'url'  => 'http://wiki.symplus.co.jp/computer/en/slideshow_plugin',
    );
  }
  function getType(){  return 'protected';  }
  function getSort(){  return 917;  }
  function connectTo($mode) {
    $this->Lexer->addEntryPattern('<slideshow.*?>(?=.*?</slideshow>)',$mode,'plugin_slideshow');
  }

  // Exit
  function postConnect() {
    $this->Lexer->addExitPattern('</slideshow>','plugin_slideshow');
  }

  // Handling lexer
  function handle($match, $state, $pos) {
    switch ($state) {
      case DOKU_LEXER_ENTER :
        return array($state,  substr($match,9,-1));
      case DOKU_LEXER_UNMATCHED :  return array($state, $match);
      case DOKU_LEXER_EXIT :       return array($state, '');
    }
    return array();
  }
  // Render
  function render($mode, &$renderer, $data){
    if ($mode!='xhtml')
      return false;

    global $conf;
    @list($state, $match) = $data;
    $args = explode(" ",$match);
    $license      = $this->getConf('license');
    
    $tryFN = preg_replace("/{{([^|}]+).*}}/",'$1',$args[1]);
    $filename = mediaFN($tryFN);
    if(is_Dir($filename) || !file_exists($filename)){
      $idx = 1;
      $filename="";
     }else{
      $idx = 2;
     }
      
    switch ($state) {
      case DOKU_LEXER_ENTER:
        if      ($args[1]=="image_list") { return true;}
        else if ($args[1]=="clear_cache"){$this->xmlCache->ClearCache(); return true;}

        $width    = (!empty($args[$idx])) ? urlencode($args[$idx]) : $this->getConf('default_width');$idx++;
        $height   = (!empty($args[$idx])) ? urlencode($args[$idx]) : $this->getConf('default_height');$idx++;
        $oid      = (!empty($args[$idx])) ? urlencode($args[$idx]) : 'slideshow';$idx++;
        $bgcolor  = (!empty($args[$idx])) ? urlencode($args[$idx]) : $this->getConf('default_bgcolor');$idx++;
        $align    = (!empty($args[$idx])) ? urlencode($args[$idx]) : '';

        $width_height = sprintf(' WIDTH="%s" HEIGHT="%s" '.NL,$width,$height);
        $align_def = sprintf(' ALIGN="%s" ',$align);
        $id_def = sprintf(' id="%s" '.NL,$oid);

        $renderer->doc.= NL.'<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '.NL;
        $renderer->doc.= 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '.NL;
        $renderer->doc.= $width_height;
        $renderer->doc.= $id_def;
        $renderer->doc.= $align_def.'>'.NL;
        $renderer->doc.= '<PARAM NAME=quality VALUE=high>'.NL;
        $renderer->doc.= '<PARAM NAME=bgcolor VALUE='.$bgcolor.'>'.NL;
        $renderer->doc.= '<EMBED quality=high'.NL;
        $renderer->doc.= ' bgcolor='.$bgcolor.''.NL;
        $renderer->doc.= $width_height;
        $renderer->doc.= ' NAME="'.$oid.'" ALIGN="'.$align.'"'.NL;
        $renderer->doc.= ' TYPE="application/x-shockwave-flash"'.NL;
        $renderer->doc.= ' PLUGINSPAGE="http://www.macromedia.com/getflashplayer"'.NL;
        if (!empty($filename)){
          // static file
          $content = @io_readFile($filename,false);
          $fetchPath = $this->createXmlFile($renderer->_xmlEntities($content),$width,$height,false);
          $src = $this->swfLoc.'?xml_source='.urlencode($fetchPath).'&license='.$license;
 
          $renderer->doc.= ' src="'.$src.'"></EMBED>';
          $renderer->doc.='<PARAM NAME=movie VALUE="'.$src.'"></OBJECT>';
        }
        break;
      case DOKU_LEXER_UNMATCHED:
        if (empty($match))
          return;
        // direct content
        $fetchPath = $this->createXmlFile($renderer->_xmlEntities($match),$width,$height,true);
        $src = $this->swfLoc.'?xml_source='.urlencode($fetchPath).'&license='.$license;

        $renderer->doc.= ' src="'.$src.'"></EMBED>'.NL;
        $renderer->doc.= '<PARAM NAME=movie VALUE="'.$src.'"></OBJECT>';
        break;
      case DOKU_LEXER_EXIT:
        break;
      }
    return true;
  }

 /**
  * Replace Media file link of {{XXX}} to fetchable URL
  */
  function replaceMediaLinks($content){
     $new_content = preg_replace_callback(
       "/{{([^|}]+).*}}/",
       create_function('$matches','return ml($matches[1],"cache=nocache");'),
       $content);
    return $new_content;
  }
 /**
  * Replace Wiki link of [[XXX]] to URL
  */
  function replaceWikiLinks($content){
    $new_content = preg_replace_callback(
       "/\[\[([^|\]]+).*\]\]/",
       create_function('$matches','return wl($matches[1]);'),
       $content);
    return $new_content;    
  }
 /**
  * Replace included source
  */
  function replaceIncludedSrc($content){
    $new_content = preg_replace_callback(
       "/<include>([^<]+?)<\/include>/",
       create_function('$matches','return @include $matches[1];'),
       $content);
    return $new_content;    
  }
 /**
  * Create XML content in the plugin's media directory
  */
  function createXmlFile(&$data,$w,$h,$needTag) {
    global $conf;
    $hash = md5(serialize($data).$w.$h);
    $savePath = $this->xmlCache->GetMediaPath($hash);
    if (!file_exists($savePath)){
      $content = $this->cleanXml($data);
      $content = $this->replaceMediaLinks($content);
      $content = $this->replaceWikiLinks($content);
      $content = $this->replaceIncludedSrc($content);
      if ($needTag===true){
        $content = sprintf('<slideshow>%s</slideshow>',$content);
      }
      if(io_saveFile($savePath, $content)){
        chmod($savePath,$conf['fmode']);
      }
    }
    return $this->xmlCache->GetMediaLink($hash);
  }

 /**
  * Convert Xml entity
  */
  function cleanXml($content){
    $s = array("&lt;","&gt;","&#039;","&quot;","&apos;");
    $r = array("<",">","'","'",'"');
    $cx = str_replace($s,$r,$content);
    return $cx;
  }
}
?>