1<?php
2/**
3 * AJAX call handler for Dokukiwix plugin
4 * Most of this file is based on the indexer plugin by Andreas Gohr.
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Andreas Gohr <andi@splitbrain.org>
8 * @author     Yann Hamon <yann@mandragor.org>
9 */
10
11//fix for Opera XMLHttpRequests
12if(!count($_POST) && $HTTP_RAW_POST_DATA){
13  parse_str($HTTP_RAW_POST_DATA, $_POST);
14}
15
16if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
17if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
18
19define("dokukiwix_plugin", 1);
20
21
22$archivePath = '';
23
24require_once(DOKU_PLUGIN.'dokukiwix/common.php');
25require_once(DOKU_INC.'inc/init.php');
26require_once(DOKU_INC.'inc/common.php');
27require_once(DOKU_INC.'inc/pageutils.php');
28require_once(DOKU_INC.'inc/auth.php');
29require_once(DOKU_INC.'inc/search.php');
30require_once(DOKU_INC.'inc/html.php');
31require_once(DOKU_INC.'inc/template.php');
32require_once(DOKU_INC.'inc/actions.php');
33
34$lock = $conf['lockdir'].'/_dokukiwix.lock';
35
36//close sesseion
37session_write_close();
38
39header('Content-Type: text/plain; charset=utf-8');
40
41//we only work for admins!
42if (auth_quickaclcheck($conf['start']) < AUTH_ADMIN){
43    die('access denied');
44}
45
46//call the requested function
47$call = 'ajax_'.$_POST['call'];
48if(function_exists($call)){
49    $call();
50}else{
51    print "The called function '".htmlspecialchars($call)."' does not exist!";
52}
53
54
55/**
56 * Searches for pages
57 *
58 * @author Andreas Gohr <andi@splitbrain.org>
59 * @author     Yann Hamon <yann@mandragor.org>
60 */
61function ajax_pagelist(){
62    global $conf;
63    $data = array();
64    search($data,$conf['datadir'],'search_allpages',array());
65
66    foreach($data as $val){
67        print $val['id']."\n";
68    }
69}
70
71/**
72 * Startup routines, called after the lock is created
73 * Creates the directory structure, copyes CSS files, ...
74 *
75 * @author     Yann Hamon <yann@mandragor.org>
76 */
77function ajax_dokukiwix_start(){
78  global $lock;
79  global $conf;
80
81  if (file_exists($lock))
82      $archivePath = DOKU_PLUGIN.'dokukiwix/archive/'.file_get_contents($lock).'/';
83  else
84      die('Critical error: The lock file has been removed!');
85
86  io_mkdir_p($archivePath.'images/_extern/');
87  io_mkdir_p($archivePath.'pages/');
88  io_mkdir_p($archivePath.'css/');
89
90  copy(DOKU_INC.'lib/tpl/offline/offline.css', $archivePath.'css/offline.css');
91
92  $home_page = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html><head><meta http-equiv="refresh" content="0;url=./pages/'.$conf['start'].'.html"></head><body></body></html>';
93
94  io_saveFile($archivePath.'index.html', $home_page,$info);
95}
96
97/**
98 * Creation of the lock file, that file containing the
99 * date and time the lock was created.
100 *
101 * @author     Yann Hamon <yann@mandragor.org>
102 */
103function ajax_createLock(){
104    global $lock;
105
106    // try to aquire a lock
107    if(!file_exists($lock)){
108        if ($dokukiwix_fp = fopen($lock, 'w+')) {
109            fwrite($dokukiwix_fp, date('Y-m-d_H:i'));
110            fclose($dokukiwix_fp);
111        }
112    }
113    else {
114        print '1';
115    }
116}
117
118/**
119 * Delete the lock file.
120 *
121 * @author     Andreas Gohr  <andi@splitbrain.org>
122 */
123function ajax_removeLock(){
124    global $lock;
125    unlink($lock);
126
127    print '1';
128}
129
130
131/**
132 * Build and save the static HTML for the requested page
133 *
134 * @author Yann Hamon <yann.hamon@gmail.com>
135 */
136function ajax_buildOfflinePage(){
137    global $conf;
138    global $_POST;
139    global $lock;
140    global $archivePath;
141
142    if (file_exists($lock))
143        $archivePath = DOKU_PLUGIN.'dokukiwix/archive/'.file_get_contents($lock).'/';
144    else
145        die('Critical error: The lock file has been removed!');
146
147    if(!$_POST['page']){
148        print 1;
149        exit;
150    }
151
152    // keep running
153    @ignore_user_abort(true);
154
155    global $ID, $ACT;
156    $ID = $_POST['page'];
157    $ACT = 'show';
158    $conf['template']='offline';
159
160    // We put the translated wiki page in the buffer $data
161    ob_start();
162    include(template('main.php'));
163    $data = ob_get_contents();
164    ob_end_clean();
165
166    io_saveFile($archivePath.'pages/'.str_replace(':', '/', $_POST['page']).'.html', $data);
167
168    print 1;
169}
170
171
172//Setup VIM: ex: et ts=4 enc=utf-8 :
173?>
174