1<?php
2/**
3 * DokuWiki Plugin unusedmedias (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Johan Guilbaud <guilbaud.johan@gmail.com>
7 * @version 1.0 (10/10/2011)
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) die();
12
13if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
14if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
15if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
16
17require_once DOKU_PLUGIN.'admin.php';
18
19//safe mode has to be disabled ! (safe mode is deprecated by php)
20@set_time_limit(0);
21
22class admin_plugin_unusedmedias_list extends DokuWiki_Admin_Plugin {
23
24	private $orphans_medias = array();
25	private $error 			= "";
26	private $ok				= "";
27
28    public function getMenuSort() { return FIXME; }
29    public function forAdminOnly() { return true; }
30
31    public function handle() {
32    	global $conf, $ID;
33
34        //$this->_debug_print_r($data);
35        //$this->_debug_print_r($_REQUEST);
36
37
38        //If delete requested
39        if(isset($_REQUEST['media_id_to_delete'])){
40
41        	if(checkSecurityToken($_REQUEST['sectok'])){
42
43	        	$mediaToDelete = $_REQUEST['media_id_to_delete'];
44
45	        	$file = mediaFN($mediaToDelete);
46
47	        	if(file_exists($file)){
48	        		if(media_inuse($mediaToDelete)===false){
49
50					    // trigger an event - MEDIA_DELETE_FILE
51					    $data['id']   = $mediaToDelete;
52					    $data['name'] = basename($file);
53					    $data['path'] = $file;
54					    $data['size'] = (@file_exists($file)) ? filesize($file) : 0;
55
56					    $data['unl'] = false;
57					    $data['del'] = false;
58					    $evt = new Doku_Event('MEDIA_DELETE_FILE',$data);
59					    if ($evt->advise_before()) {
60					        $data['unl'] = @unlink($file);
61					        if($data['unl']){
62					            addMediaLogEntry(time(), $mediaToDelete, DOKU_CHANGE_TYPE_DELETE);
63					            $data['del'] = io_sweepNS($mediaToDelete,'mediadir');
64					        }
65					    }
66					    $evt->advise_after();
67					    unset($evt);
68
69						$this->ok = sprintf($this->getLang('delete_file_ok'),$mediaToDelete);
70						;
71	        		}else{
72	        			$this->error = sprintf($this->getLang('delete_file_in_use'),$mediaToDelete);
73	        		}
74
75	        	}else{
76	        		$this->error = sprintf($this->getLang('delete_file_not_found'),$mediaToDelete);
77	        	}
78
79        	}//end of csrf check
80        }
81
82
83        //Searching for orphaned medias
84
85        $data = array();
86
87        //getting all medias
88        search($data,$conf['mediadir'],'search_media', array('showmsg'=>true,'depth'=>500),str_replace(':', '/', getNS($ID)));
89
90
91
92
93        //check if they are (still) in use or not.
94        foreach($data as $media){
95        	$isUsed = media_inuse($media['id']);
96
97        	if($isUsed === false){
98        		$this->orphans_medias[$media['id']] = $media;
99        	}
100        }
101
102
103    }
104
105    public function html() {
106    	global $lang, $ID;
107
108        ptln('<h1>' . $this->getLang('title') . ' : '.getNS($ID).'</h1>');
109
110        if($this->error != ""){
111        	ptln("<div class='error'>".$this->error."</div>");
112        }
113    	if($this->ok != ""){
114        	ptln("<div class='success'>".$this->ok."</div>");
115        }
116
117
118        if(count($this->orphans_medias)>0){
119	        ptln('<table class="inline">');
120				ptln('<tr><th class="centeralign">ID</strong></th><th>Actions</th></tr>');
121				$i=0;
122		        foreach($this->orphans_medias as $id => $media){
123		        	ptln('<tr>');
124		        	ptln('<td>' . $id . '</td>');
125		        	$link = ml($id,'',true);
126		        	$btn_view =  ' <a href="'.$link.'" target="_blank">
127		        					<img src="'.DOKU_BASE.'lib/images/magnifier.png" alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" />
128		        				   </a>';
129
130		        	$btn_delete = ' <form id="unusedmedias_form_'.$i.'_delete" method="post" action="">
131		        						<input type="hidden" name="media_id_to_delete" value="'.$id.'" />
132		        						<input type="hidden" name="sectok" value="'.getSecurityToken().'" />
133		        					</form>
134		        					<a href="#" class="btn_media_delete" title="'.$id.'" onclick=\'if(confirm("'.$this->getLang('js_confirm_delete').'")){document.getElementById("unusedmedias_form_'.$i.'_delete").submit();}return false;\'>
135		        						<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" title="'.$lang['btn_delete'].'" class="btn" />
136		        					</a>';
137
138
139		        	ptln('<td>  '.$btn_view.' '.$btn_delete.'</td>');
140		        	ptln('</tr>');
141		        	$i++;
142		        }
143		   	ptln('</table>');
144        }else{
145        	ptln('<div>'.$this->getLang('nomatches').'</div>');
146        }
147
148
149
150    }
151
152
153
154
155    /**
156     * Debug method used only during development.
157     * @param array $array
158     */
159    private function _debug_print_r($array){
160    	echo str_replace("\n","<br />",print_r($array,true));
161    	echo "<br />";
162    }
163}
164
165