1<?php
2/**
3 * Imageflow Plugin
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     i-net software <tools@inetsoftware.de>
7 * @author     Gerry Weissbach <gweissbach@inetsoftware.de>
8 */
9
10use dokuwiki\Extension\Event;
11
12// must be run within Dokuwiki
13if(!defined('DOKU_INC')) die();
14
15if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16require_once(DOKU_PLUGIN.'action.php');
17
18class action_plugin_popupviewer extends DokuWiki_Action_Plugin {
19
20    function getInfo(){
21        return array_merge(confToHash(dirname(__FILE__).'/plugin.info.txt'), array(
22                'name' => 'PopUpViewer Action Component',
23                'desc' => 'Delivers pages back to the browser'
24                ));
25    }
26
27    function register(Doku_Event_Handler $controller) {
28        // Support given via AJAX
29        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'ajax_viewer_provider');
30        $controller->register_hook('MULTIORPHAN_INSTRUCTION_LINKED', 'BEFORE', $this, 'multiorphan_link_check');
31    }
32
33    function ajax_viewer_provider( Doku_Event &$event ) {
34        global $JSINFO;
35        global $INFO;
36        global $ID;
37        global $ACT;
38
39        if ( $event->data != '_popup_load_file' && $event->data != '_popup_load_image_meta' ) {
40            return;
41        }
42
43        // Registers ACT
44        if (isset($_SERVER['HTTP_X_DOKUWIKI_DO'])){
45            $ACT = trim(strtolower($_SERVER['HTTP_X_DOKUWIKI_DO']));
46        } elseif (!empty($_REQUEST['idx'])) {
47            $ACT = 'index';
48        } elseif (isset($_REQUEST['do'])) {
49            $ACT = $_REQUEST['do'];
50        } else {
51            $ACT = 'show';
52        }
53
54        $event->preventDefault();
55        $event->stopPropagation();
56
57        $data = "";
58        $head = array(
59            'script' => array(array()),
60            'link' => array(array()),
61            'meta' => array(array()),
62            'popupscript' => array()
63        );
64        $ID = getID('id');
65
66        if (auth_quickaclcheck($ID) < AUTH_READ) {
67            echo "read permission error";
68            return;
69        }
70
71        switch($event->data) {
72            case '_popup_load_file' :
73                $INFO = pageinfo();
74                $JSINFO['id'] = $ID;
75                $JSINFO['namespace'] = (string) $INFO['namespace'];
76                Event::createAndTrigger('POPUPVIEWER_DOKUWIKI_STARTED',$head,null,true);
77
78                $script = 'var JSINFO = ' . json_encode($JSINFO) . ';';
79
80                if ( $this->getConf('allowpopupscript') ) {
81                    $popupscript = p_get_metadata($ID, 'popupscript', true);
82                    $script .= "try{(function($){".$popupscript."}(jQuery))}catch(e){alert('Could not execute popupscript: '+e);}";
83
84                    if ( ($google =& plugin_load('action', 'googleanalytics')) ) {
85                        $dest = str_replace(":", "/", $ID);
86                        if ( isset($_REQUEST['do']) ) {
87                            $dest .= "?do=".$_REQUEST['do'];
88                        }
89                        $script .= "if(window.ga) window.ga('send', 'event', 'wiki-action', 'popupviewer', '".$dest."', { nonInteraction: false} );";
90                    }
91                }
92
93                $head['popupscript'][] = array( 'type'=>'text/popupscript', '_data'=> $script );
94
95                $data = '<div class="dokuwiki" style="padding-bottom: 10px;">' . p_wiki_xhtml($ID,'',true) . '</div>';
96                break;
97            case '_popup_load_image_meta' :
98
99                global $SRC;
100                $SRC = mediaFN($ID);
101                $title = hsc(tpl_img_getTag('IPTC.Headline'));
102                $caption = hsc(tpl_img_getTag('IPTC.Caption'));
103
104                if ( !empty($title) ) { $title = "<h3 class=\"title\">$title</h3>"; }
105                if ( !empty($caption) ) { $caption = "<div class=\"text\"><p>$caption</p></div>"; }
106                $data = preg_replace("%(\n|\r)%", '', nl2br($title.$caption));
107                break;
108        }
109
110        header('Content-Type: text/html; charset=utf-8');
111
112        if ( !empty($head['popupscript']) ) {
113            Event::createAndTrigger('TPL_METAHEADER_OUTPUT',$head,'_tpl_metaheaders_action',true);
114        }
115
116        print $data;
117        return;
118    }
119
120    function multiorphan_link_check(Doku_Event &$event) {
121
122        $instructions = $event->data['instructions'];
123        if ( !strstr($instructions[0], 'popupviewer') ) {
124            return;
125        }
126
127        $event->data['entryID'] = $id = cleanID($instructions[1][0]);
128
129        $page   = resolve_id(getNS($event->data['checkNamespace']),$id);
130        $file   = mediaFN($page);
131
132        $event->data['exists'] = $exists = @file_exists($file) && @is_file($file);
133        $event->data['type']   = $exists ? 'media' : 'pages';
134
135        if ( !$exists ) {
136            resolve_pageid(getNS($event->data['checkNamespace']),$id,$event->data['exists']);
137        }
138    }
139}
140