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