1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Etienne Mauvais <emauvaisfr@yahoo.fr>
5 */
6
7// must be run within Dokuwiki
8if(!defined('DOKU_INC')) die();
9
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'action.php');
12
13class action_plugin_lytebox extends DokuWiki_Action_Plugin {
14
15    /**
16     * return some info
17     */
18    function getInfo() {
19        return array(
20                'author' => 'Etienne M.',
21                'email'  => 'emauvaisfr@yahoo.fr',
22                'date'   => @file_get_contents(DOKU_PLUGIN.'lytebox/VERSION'),
23                'name'   => 'lytebox Plugin',
24                'desc'   => 'Affiche les images dans lytebox / Displays the pictures using lytebox',
25                'url'    => 'http://www.dokuwiki.org/fr:plugin:lytebox',
26                );
27    }
28
29    /**
30     * Constructor
31     */
32    function action_plugin_lytebox() {
33      $this->setupLocale();
34    }
35
36    /**
37     * register the eventhandlers
38     */
39    function register(&$contr) {
40        //$contr->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
41        $contr->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, '_handle_tpl_metaheader',      array());
42        $contr->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE',   $this, '_handle_tpl_content_display', array());
43    }
44
45    function _handle_tpl_metaheader(&$event, $param) {
46      // Adding lytebox stylesheet
47      $event->data["link"][] = array (
48        "type" => "text/css",
49        "rel" => "stylesheet",
50        "href" => DOKU_BASE."lib/plugins/lytebox/lytebox.css",
51      );
52
53      // Adding lytebox JavaScript File
54      $event->data["script"][] = array (
55        "type" => "text/javascript",
56        "src" => DOKU_BASE."lib/plugins/lytebox/lytebox.js",
57        "_data" => "",
58      );
59    }
60
61    function _handle_tpl_content_display(&$event, $param) {
62      $event->data = preg_replace_callback(
63                            '/<a href="(.*?)" class="media".*?title="(.*?)".*?><img src="(.*?)" class="(.*?)".*?(title|alt)="(.*?)"(.*?)\/><\/a>/',
64                            array('action_plugin_lytebox','_callback'),
65                            $event->data
66                           );
67    }
68
69    function _callback($matches) {
70      //1 : a_href
71      //2 : a_title
72      //3 : img_src
73      //4 : img_media
74      //5 : img_alt_title_tag
75      //6 : img_alt_title
76      //7 : img_reste
77
78      //S'il n'y a pas "_detail" dans le lien ni de /lib/exe/fetch.php (image link), on ne fait rien
79      if (strpos(strtolower($matches[1]),"/_detail/")) $type="int";
80      else if (strpos(strtolower($matches[1]),"/lib/exe/fetch.php")) $type="ext";
81      else return $matches[0];
82
83      //print "<pre>".print_r($matches,true)."</pre>";
84
85      $rel="lytebox[0]";
86      $rev="";
87
88      //Pour les images internes
89      if ($type=="int") {
90        preg_match("/(.*?)\?/",$matches[3],$img_src);
91      }
92      //Pour les images externes
93      else {
94        preg_match("/(.*)/",$matches[3],$img_src);
95      }
96
97      if ($img_src) $img_src = $img_src[1];
98      else $img_src=$matches[3];
99
100      $a_title=$matches[2];
101      //Pour les images internes
102      if ($type=="int") {
103        $a_title = "<a href=\"".preg_replace("/_media/","_detail",$img_src)."\" target=\"_blank\">$a_title</a>";
104      }
105      //Pour les images externes
106      else {
107        $a_title = "<a href=\"$img_src\" target=\"_blank\">$a_title</a>";
108      }
109      if ($matches[6]) $a_title = $matches[6]."<br />".$a_title;
110      else $matches[6]="&nbsp;";
111
112      return "<a href=\"$img_src\" class=\"media\" title=\"".htmlspecialchars($a_title)."\" rel=\"$rel\" $rev><img src=\"$matches[3]\" class=\"$matches[4]\" alt=\"$matches[6]\" title=\"$matches[6]\" $matches[7] /></a>";
113    }
114}
115
116// vim:ts=4:sw=4:et:enc=utf-8:
117