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