1<?
2if(!defined('DOKU_INC')) define('DOKU_INC', dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME']))).'/');
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
4
5# If you would like to use a custom PEAR directory for the libraries needed by
6# this plugin, set it below.  If you have File_Archive installed in a default
7# PEAR location (i.e. it was installed by your webhost), you comment out the
8# next line of PHP:
9define('CUSTOM_PEAR', DOKU_PLUGIN.'zip/pear/');
10
11# Set this to the location this plugin should use for tmp files:
12define('TMP_DIR', DOKU_PLUGIN.'zip/tmp/');
13
14if (defined('CUSTOM_PEAR'))  ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.CUSTOM_PEAR);
15
16foreach(explode(PATH_SEPARATOR, ini_get('include_path')) as $file) {
17	if (file_exists($file."/File/Archive.php")) {
18		define("HAS_PEAR", true);
19		require_once('File/Archive.php');
20		break;
21	}
22}
23require_once(DOKU_PLUGIN.'admin.php');
24
25# Check to see if we can use cache:
26/*foreach(explode(PATH_SEPARATOR, ini_get('include_path')) as $file) {
27	if (file_exists($file."/Cache/Lite.php")) {
28		require_once('Cache/Lite.php');
29		define("USE_CACHE", true);
30		break;
31	}
32}*/
33if (!defined("USE_CACHE")) {
34	define("USE_CACHE", false);
35}
36
37
38class admin_plugin_zip extends DokuWiki_Admin_Plugin {
39
40	function getInfo() {
41		return array(
42			'author' => 'Andrew Pilsch',
43			'email' => 'andrew@pilsch.com',
44			'date' => '2006-05-31',
45			'name' => 'admin plugin zip',
46			'desc' => 'A plugin to create a zip archive of wiki data and to restore the wiki from a previous backup',
47			'url' => 'http://wiki.pilsch.com/DokuZip',
48		);
49	}
50
51	function getMenuSort() {
52		return 50;
53	}
54
55	function handle() {
56		if (!HAS_PEAR) {
57			$this->msg = "It Appears You Have Not Installed the PEAR Libraries Needed For doku-zip";
58			return;
59		}
60		$action = $_REQUEST['zip_action'];
61		if (!isset($action)) {
62			return null;
63		}
64
65		if ($action == 'backup') {
66			$this->msg = $this->zip_create_backup();
67		} else if ($action == 'restore') {
68			$this->msg = $this->zip_restore_backup();
69		}
70	}
71
72	function zip_create_backup() {
73		global $conf;
74		File_Archive::setOption("zipCompressionLevel",9);
75		$stamp = date("Ymd");
76		$zip_name = "{$conf['title']}-$stamp.zip";
77		if (USE_CACHE) {
78			$cache = new Cache_Lite(
79				array(
80					'lifeTime' => 3600,
81					'cacheDir' => TMP_DIR
82				)
83			);
84			File_Archive::setOption('cache', $cache);
85		} else {
86			if (!isset($_COOKIE['doku_zip_backup'])) {
87				setcookie('doku_zip_backup', time(), time()+3600, '/');
88			} else if(file_exists(TMP_DIR.$zip_name)) {
89
90				# This prevents the problem with double sending zip
91				# files that seems to be a result of the way the
92				# doku admin plugin architecture is constructed.
93				if (isset($_COOKIE['doku_zip_now'])) return;
94				setcookie('doku_zip_now', time(), time()+5, '/');
95
96				$_REQUEST['action'] = '';
97				$dir = opendir(TMP_DIR);
98				while($file = readdir($dir)) {
99					if (preg_match("@.zip$@", $file) && $zip_name != $file) {
100						unlink(TMP_DIR.$file);
101					}
102				}
103				closedir($dir);
104				File_Archive::extract(
105					File_Archive::read(TMP_DIR.$zip_name."/"),
106					File_Archive::toArchive($zip_name,
107						File_Archive::toOutput()
108					)
109				);
110				exit;
111			}
112		}
113
114		if (substr($conf['savedir'],0,1) == "/") {
115			$dir = $conf['savedir'];
116		} else {
117			$dir = DOKU_INC.'/'.$conf['savedir'];
118		}
119
120		# This prevents the problem with double sending zip
121		# files that seems to be a result of the way the
122		# doku admin plugin architecture is constructed.
123		if (isset($_COOKIE['doku_zip_now'])) return;
124		setcookie('doku_zip_now', time(), time()+5, '/');
125
126		if (!USE_CACHE) {
127			File_Archive::extract(
128				File_Archive::filter(
129					File_Archive::predOr(
130						File_Archive::predEreg('\.txt$'),
131						File_Archive::predEreg('\.log$'),
132						File_Archive::predEreg('\.gz$')
133					),
134					File_Archive::read($dir)
135				),
136				$dest =TMP_DIR.$zip_name
137			);
138
139			$dir = opendir(TMP_DIR);
140			while($file = readdir($dir)) {
141				if (preg_match("@.zip$@", $file) && $zip_name != $file) {
142					unlink(TMP_DIR.$file);
143				}
144			}
145			closedir($dir);
146		}
147
148		File_Archive::extract(
149			File_Archive::filter(
150				File_Archive::predOr(
151					File_Archive::predEreg('\.txt$'),
152					File_Archive::predEreg('\.log$'),
153					File_Archive::predEreg('\.gz$')
154				),
155
156				File_Archive::read($dir)
157			),
158
159			File_Archive::toArchive($zip_name,
160				File_Archive::toOutput()
161			)
162		);
163		exit;
164	}
165
166	function zip_restore_backup() {
167		global $conf;
168
169                if (substr($conf['savedir'],0,1) == "/") {
170                        $dir = $conf['savedir'];
171                } else {
172                        $dir = DOKU_INC.'/'.$conf['savedir'];
173                }
174
175		if ($_FILES['zip_file']['error'] != 0) {
176			return "Upload Error";
177		}
178
179		if (preg_match("@zip@", $_FILES['zip_file']['type'])) {
180			$tmp = TMP_DIR . time().md5($_FILES['zip_file']['name']).'.zip';
181			move_uploaded_file($_FILES['zip_file']['tmp_name'], $tmp);
182			File_Archive::extract(
183				File_Archive::read("$tmp/", $dir."/"),
184				File_Archive::toFiles()
185			);
186			unlink($tmp);
187		} else {
188			print '<pre>'; print_r($_FILES); print '</pre>';
189			return "Not a Zip";
190		}
191		return "Data Restored";
192	}
193
194	function html() {
195		print $this->locale_xhtml('intro');
196
197		if (isset($this->msg)) {
198?>
199<span id="zip_message"><?=$this->msg?></span>
200<?
201		}
202
203		print $this->zip_create_backup_form();
204		print "<div class='zip_space'></div>";
205		print $this->zip_create_restore_form();
206	}
207
208	function zip_create_backup_form() {
209?>
210<span class="zip_form_box">
211	<span class="zip_form_title"><?=$this->getLang('create_form_title')?></span>
212	<center>
213	<form method="post" action="<?=$_REQUEST['id']?>?do=<?=$_REQUEST['do']?>&page=<?=$_REQUEST['page']?>">
214		<input type="hidden" name="do" value="<?=$_REQUEST['do']?>" />
215		<input type="hidden" name="page" value="<?=$_REQUEST['page']?>" />
216		<input type="hidden" name="id" value="<?$_REQUEST['id']?>" />
217		<input type="hidden" name="zip_action" value="backup"/>
218
219		<input type="submit" value="<?=$this->getLang('create_form_button')?>" />
220	</form>
221	</center>
222</span>
223<?
224	}
225
226	function zip_create_restore_form() {
227?>
228<span class="zip_form_box">
229	<span class="zip_form_title"><?=$this->getLang('restore_form_title')?></span>
230	<center>
231	<form method="POST" action="<?=$_REQUEST['id']?>?do=<?=$_REQUEST['do']?>&page=<?=$_REQUEST['page']?>" enctype="multipart/form-data">
232		<input type="hidden" name="MAX_FILE_SIZE" value="3000000" />
233		<input type="hidden" name="do" value="<?=$_REQUEST['do']?>" />
234		<input type="hidden" name="page" value="<?=$_REQUEST['page']?>" />
235		<input type="hidden" name="id" value="<?$_REQUEST['id']?>" />
236		<input type="hidden" name="zip_action" value="restore"/>
237
238	<!--<label for="zip_file"><?=$this->getLang('restore_form_label')?></label>-->
239		<input type="file" name="zip_file"/><br/><br/>
240		<input type="submit" value="<?=$this->getLang('restore_form_button')?>"/>
241	</form>
242	</center>
243<?
244	}
245
246}
247?>
248