1<?php
2/**
3 * DokuWiki Plugin directorylist (Action Component)
4 *
5 * @author  alexwenzel <alexander.wenzel.berlin@gmail.com>
6 */
7
8// must be run within Dokuwiki
9if (!defined('DOKU_INC')) die();
10
11class Action_Plugin_Directorylist_Directorylist extends Dokuwiki_Action_Plugin
12{
13	/**
14	 * Register Event to catch download action
15	 * @param  Doku_Event_Handler $controller
16	 * @return void
17	 */
18	public function register(Doku_Event_Handler $controller)
19	{
20		$controller->register_hook(ACTION_ACT_PREPROCESS, BEFORE, $this, 'handle_event');
21	}
22
23	/**
24	 * Checks the action for the keyword to download files
25	 * @see http://www.php.net/manual/en/function.finfo-open.php
26	 * @param  Doku_Event $event
27	 * @return void
28	 */
29	public function handle_event(Doku_Event &$event)
30	{
31		// check if we need to download a file
32		if ($event->data === 'download' && ! empty($_GET['file'])) {
33
34			// get filename
35			$file = ($_GET['file']);
36
37			//  get mimetype of this file
38			$finfo = new finfo(FILEINFO_MIME);
39			$mimetype = $finfo->file($file);
40
41			//  force download
42			header("Content-disposition: attachment; filename=".basename($file));
43			header("Content-type: ".$mimetype);
44			readfile($file);
45			die();
46		}
47	}
48}