1<?php
2/**
3 * Media Component of mediasyntax plugin: displays media, e.g. an image in a page
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Esther Brunner <wikidesign@gmail.com>
7 * @author     Christopher Smith <chris@jalakai.co.uk>
8 * @author     Gina Häußge, Michael Klier <dokuwiki@chimeric.de>
9 * @author     Thorsten Staerk <dev@staerk.de>
10 */
11
12// to show an image we need to insert an html syntax like
13// <img src="/dokuwiki/lib/exe/fetch.php?w=200&amp;tok=234ffe&amp;media=test.jpg" class="media" alt="" width="200" />
14class syntax_plugin_mediasyntax_media extends DokuWiki_Syntax_Plugin
15{
16  function getType() { return 'substition'; }
17
18  function getSort() { return 99; }
19
20  function getPType() { return 'block'; }
21
22  function connectTo($mode)
23  {
24    $this->Lexer->addSpecialPattern("\[\[Image:.+?\]\]", $mode, 'plugin_mediasyntax_media');
25    $this->Lexer->addSpecialPattern("\[\[File:.+?\]\]", $mode, 'plugin_mediasyntax_media');
26  }
27
28  function handle($match, $state, $pos, Doku_Handler $handler)
29  // This first gets called with $state=1 and $match is the entryPattern that matched.
30  // Then it (the function handle) gets called with $state=3 and $match is the text
31  // between the entryPattern and the exitPattern.
32  // Then it gets called with $state=4 and $match is the exitPattern.
33  // What this delivers is what is handed over as $data to the function render.
34  {
35    return array($match, $state, $pos);
36  }
37
38  function render($mode, Doku_Renderer $renderer, $data)
39  // $data is the return value of handle
40  // $data[0] is always $match
41  // $data[1] is always $state
42  // $data[3] is always $pos
43  {
44    $match=$data[0];  // e.g. [[Image:foo.png|50px]]
45    $end=preg_replace("/^\[\[Image:|^\[\[File:/","",$match);  // e.g. foo.png|50px]]
46    $start=preg_replace("/\]\]$/","",$end);  // e.g.. foo.png|50px
47    $filename=preg_replace("/\|.*$/","",$start);  // e.g. foo.png
48    $extension=preg_replace("/.*\./","",$filename); // e.g. png
49    $pipe=preg_replace("/.*\|/","",$start);  // e.g. 50px
50
51    $img_exts = array("png", "jpg", "jpeg", "gif");
52
53    if($mode == 'xhtml')
54    {
55      if (in_array(strtolower($extension), $img_exts))
56      {
57        $renderer->doc.= '<img src="'.DOKU_BASE.'lib/exe/fetch.php?media='.$filename.'" width='.$pipe.' />';
58      }
59      else
60      {
61        $renderer->doc.= '<a href="'.DOKU_BASE.'lib/exe/fetch.php?media='.$filename.'">File:'.$filename.'</a>';
62      }
63    }
64    return false;
65  }
66
67}
68// vim:ts=4:sw=4:et:enc=utf-8:
69