1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4
5class helper_plugin_epub extends Dokuwiki_Plugin {
6	private $script_file;
7	private $cache;
8    function __construct() {
9            $this->script_file = metaFN('epub:cache', '.ser');
10            $this->cache = unserialize(io_readFile($this->script_file,false));
11            if(!$this->cache) $this->cache = array();
12
13    }
14
15	function msg($text) {
16	    if(is_array($text)) {
17		   $text = '<pre>' . print_r($text,true) . '</pre>';
18		}
19		echo "$text\n";
20	}
21
22
23    function is_inCache($id) {
24         $md5 = md5($id);
25         if(isset($this->cache[$md5])) return true;
26         return false;
27    }
28
29	function remove_page($id) {
30	     $md5 = md5($id);
31         $this->delete_page($md5);
32    }
33
34    function delete_page($md5) {
35		 unset($this->cache[$md5]);
36         if(isset($this->cache['current_books'][$md5])) {
37            unset($this->cache['current_books'][$md5]);
38         }
39		 io_saveFile($this->script_file,serialize($this->cache));
40	}
41
42    function delete_media($md5) {
43        $epub = $this->cache['current_books'][$md5]['epub'];
44        $file =  mediaFN($this->cache['current_books'][$md5]['epub']);
45        if(file_exists($file)) {
46            if(unlink($file)) {
47               return "Removed: " . $epub;
48            }
49            else return "error unlinking $epub";
50        }
51        return "File not found: " . $this->cache['current_books'][$md5]['epub'] ;
52    }
53    function writeCache($id) {
54         if(!$this->is_inCache($id)) {
55            $this->cache[md5($id)] = $id;
56            io_saveFile($this->script_file,serialize($this->cache));
57             return true;
58         }
59         return false;
60    }
61
62    function getCache() {
63         return  $this->cache;
64    }
65
66
67    function addBook($id,$epub,$title) {
68         $md5 = md5($id);
69         if(!$this->is_inCache($id)) {
70            $this->cache[$md5] = $id;
71         }
72         if(!isset($this->cache['current_books'])) {
73            $this->cache['current_books'] = array();
74         }
75         $this->cache['current_books'][$md5] = array('title'=>$title, 'epub'=>$epub);
76         io_saveFile($this->script_file,serialize($this->cache));
77    }
78
79    /**
80      * removes dokuwiki cache for page where Start button appears,
81      * so that Start can always be activated
82    */
83    function delete_dw_cachefiles($id) {
84       global $conf;
85       $data = wikiFN($id).$_SERVER['HTTP_HOST'].$_SERVER['SERVER_PORT'];
86       $_md5=md5($data);
87       $file = $conf['cachedir'].'/'.$_md5{0}.'/'.$_md5;
88       $types = array('metadata','i','xhtml');
89       foreach ($types as $type) {
90          $name = $file . ".$type";
91           if(file_exists($name)) {
92               unlink($name);
93           }
94       }
95    }
96
97    function get_page_data($id) {
98         $md5 = md5($id);
99         if(isset($this->cache['current_books'][$md5])) {
100            return $this->cache['current_books'][$md5];
101         }
102         return false;
103    }
104
105    /**
106    *  return configuration values
107    */
108    function get_conf($which) {
109        return $this->getConf($which);
110    }
111}
112
113