1<?php
2	/**
3
4	*/
5	// must be run within Dokuwiki
6	if(!defined('DOKU_INC')) die();
7
8	if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9	require_once(DOKU_PLUGIN.'action.php');
10
11	class action_plugin_epub extends DokuWiki_Action_Plugin {
12		private $helper;
13
14
15		/**
16			* Register callbacks
17		*/
18		function register(Doku_Event_Handler $controller) {
19			$controller->register_hook( 'TPL_METAHEADER_OUTPUT', 'AFTER', $this, 'loadScript');
20            $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'create_ebook_button');
21			$controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, 'get_epub');
22			$controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, 'check_scriptLoaded');
23            $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'prevent_cache');
24		}
25
26		/**
27
28		*/
29
30        function create_ebook_button($event,$param) {
31             global $INFO;
32             global $ACT;
33             if(!$this->getConf('permalink')) return;
34             if($ACT != 'show') return;
35             if(!$this->helper) {
36            $this->helper = $this->loadHelper('epub', true);
37            }
38			if (!$this->helper->is_inCache($INFO['id']))  return;
39
40            $auth = auth_quickaclcheck($INFO['id']);
41            if($auth) {
42                $page_data = $this->helper->get_page_data($INFO['id']);
43                if(!$page_data) return;
44                $ebook = $page_data['epub'];
45                $link = ml($ebook);
46                $title = $page_data['title'];
47                echo  $this->getLang('download');  //The most recent ebook for this page is:
48                echo " <a href='$link' title='$title'>$title ($ebook)</a>.<br />";
49                echo  $this->getLang('download_click');   // To download it, click on the link.
50                echo $this->getLang('download_alt');    //    If you have an ebook reader plugin installed, right-click on the link and select 'Save . . . As'.";
51            }
52
53
54        }
55		function get_epub($event, $param) {
56			global $ID;
57			global $USERINFO;
58			if(!isset($USERINFO)) return;
59			$user = $USERINFO['name'];
60			global $ACT;
61			global $INFO;
62
63			if($ACT != 'show') return;
64            if(!$this->helper) {
65			    $this->helper = $this->loadHelper('epub', true);
66            }
67			if (!$this->helper->is_inCache($INFO['id']))  return;  //cache set in syntax.php
68
69			if(strpos($INFO['id'],'epub') === false) return;
70			$wiki_file = wikiFN($INFO['id']);
71			if(!@file_exists($wiki_file)) return;
72	            $epub_group = $this->getConf('group');
73        		$groups=$USERINFO['grps'];
74			$auth = auth_quickaclcheck('epub:*');
75
76
77			if($auth < 8 && !in_array($epub_group,$groups)) return;
78			$auth = auth_quickaclcheck($INFO['id']);
79			if($auth < 4) return;
80
81			$client=$INFO['client'];
82
83       $button_name = $this->getLang('button_start'); //"Start"; //$this->getLang('btn_generate');
84       $button="<form class='button'>";
85       $button .= "<div class='no' id='show_throbberbutton'><input type='button' value='$button_name' class='button' title='start'  onclick=\"_epub_show_throbber('$user','$client');\"/>";
86       $button .="&nbsp;&nbsp;";
87	   $button .=    $this->getLang('label_start');   //"Click the Start Button to Create your eBook";
88	   $button .="</div></form>";
89       echo $button;
90       $id = $INFO['id'];
91       $button_name = $this->getLang('button_remove');
92       $button="<p><form class='button'>";
93       $button .= "<div class='no' id='epub_remove_button'><input type='button' value='$button_name' class='button' title='start'  onclick=\"epub_remove_creator('$id');\"/></div></form>";
94	   $button .= '</br>'. $this->locale_xhtml('remove');
95       echo $button;
96
97		}
98
99	 function prevent_cache(&$event) {
100     	  global $INFO;
101             if(!$this->helper) {
102                $this->helper = $this->loadHelper('epub', true);
103            }
104            if (!$this->helper->is_inCache($INFO['id']))  return;  //cache set in syntax.php
105          $event->preventDefault();
106     }
107
108    function loadScript(&$event) {
109    echo <<<SCRIPT
110    <script type="text/javascript">
111    //<![CDATA[
112    function epub_LoadScript( url )
113    {
114     document.write( '<scr' + 'ipt type="text/javascript" src="' + url + '"><\/scr' + 'ipt>' ) ;
115    }
116//]]>
117  </script>
118SCRIPT;
119    }
120
121    function check_scriptLoaded(&$event) {
122   $url = DOKU_URL . 'lib/plugins/epub/script.js';
123    echo <<<SCRIPT
124    <script type="text/javascript">
125    //<![CDATA[
126
127      if(!window._epub_show_throbber){
128        epub_LoadScript("$url");
129      }
130
131    //]]>
132
133    </script>
134SCRIPT;
135
136
137    }
138
139		function write_debug($what) {
140			return;
141			$what = print_r($what,true);
142			$handle = fopen('epub-action.txt', 'a');
143			fwrite($handle,"$what\n");
144			fclose($handle);
145		}
146	}
147
148	//Setup VIM: ex: et ts=4 enc=utf-8 :
149