forked from Taggic/langdelete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.php
204 lines (177 loc) · 7.39 KB
/
admin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Delete unnecessary languages -> administration function
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Taggic <[email protected]>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
/**
* All DokuWiki plugins to extend the admin function
* need to inherit from this class
*/
class admin_plugin_langdelete extends DokuWiki_Admin_Plugin {
const DEFAULT_LANG = 'en'; // this must be kept
/**
* return sort order for position in admin menu
*/
function getMenuSort() {
return 20;
}
/**
* handle user request
*
* Initializes internal vars and handles modifications
*
* @author Taggic <[email protected]>
*/
function handle() {
global $ID;
}
/**
* langdelete Output function
*
* print a table with all found lanuage folders
*
* @author Taggic <[email protected]>
*/
function html() {
global $ID;
global $conf; // access DW configuration array
// langdelete__intro
echo $this->locale_xhtml('intro');
// input guidance
echo '<a name="langdelete_inputbox"></a>'.NL;
echo $this->locale_xhtml('guide');
echo '<br />'.NL;
// input form
$this->_html_form();
echo '<br />'.NL;
$lang_keep = $_REQUEST['langdelete_w'];
$dryrun = $_REQUEST['dryrun'];
// language given?
if (!empty($lang_keep)) {
echo '<h2>'.$this->getLang('h2_output').'</h2>'.NL;
if ($dryrun==true) {
msg($this->getLang('langdelete_willmsg'), 2);
} else {
msg($this->getLang('langdelete_delmsg'), 0);
}
echo '<br />'.NL;
$arr_langs = explode(',', $lang_keep);
$arr_langs[] = self::DEFAULT_LANG; // add 'en'
$arr_langs[] = $conf['lang']; // add current lang
// print_r($arr_langs);
$lang_keep = array_unique($arr_langs);
echo '<div class="langdelete__result">';
$this->_list_language_dirs(DOKU_INC.'inc', 0, $lang_keep, $dryrun);
$this->_list_language_dirs(DOKU_INC.'lib', 0, $lang_keep, $dryrun);
echo '</div>';
if ($dryrun==true) {
echo '<br />'.NL;
msg($this->getLang('langdelete_attention'), 2);
echo '<br />'.NL;
echo '<a href="#langdelete_inputbox">'.$this->getLang('backto_inputbox').'</a>'.NL;
}
}
}
/**
* Display the form with input control to let the user specify,
* which languages to be kept beside en
*
* @author Taggic <[email protected]>
*/
function _html_form(){
global $ID, $conf;
echo '<div id="langdelete__form">'.NL;
echo '<form action="'.wl($ID).'" method="post">';
echo '<input type="hidden" name="do" value="admin" />'.NL;
echo '<input type="hidden" name="page" value="langdelete" />'.NL;
echo '<input type="hidden" name="sectok" value="'.getSecurityToken().'" />'.NL;
echo '<fieldset class="langdelete__fieldset"><legend>'.$this->getLang('i_legend').'</legend>'.NL;
echo '<label class="formTitle">'.$this->getLang('i_using').':</label>';
echo '<div class="box">'.$conf['lang'].'</div>'.NL;
echo '<label class="formTitle" for="langdelete_w">'.$this->getLang('i_shouldkeep').':</label>';
echo '<input type="text" name="langdelete_w" class="edit" value="'.hsc($_REQUEST['langdelete_w']).'" />'.NL;
echo '<label class="formTitle" for="option">'.$this->getLang('i_runoption').':</label>';
echo '<div class="box">'.NL;
echo '<input type="checkbox" name="dryrun" checked="checked" /> ';
echo '<label for "dryrun">'.$this->getLang('i_dryrun').'</label>'.NL;
echo '</div>'.NL;
echo '<input type="submit" value="'.$this->getLang('btn_start').'" class="button"/>'.NL;
echo '</fieldset>'.NL;
echo '</form>'.NL;
echo '</div>'.NL;
}
/**
* This function will read the full structure of a directory.
* It's recursive becuase it doesn't stop with the one directory,
* it just keeps going through all of the directories in the folder you specify.
*/
function _list_language_dirs($path, $level,$lang_keep,$dryrun){
// Directories to ignore when listing output. Many hosts
// will deny PHP access to the cgi-bin.
$ignore = array( 'cgi-bin', '.', '..' );
// Open the directory to the handle $dh
$dh = @opendir( $path );
// Loop through the directory
while( false !== ($file = readdir($dh)) ){
if( !in_array( $file, $ignore ) ){
// Check that this file is not to be ignored
if( is_dir( "$path/$file" ) ){
// Its a directory, so we need to keep reading down...
$cFlag = false;
foreach ($lang_keep as $f) {
if (stripos( substr("$path/$file",strlen("$path/$file")-3,strlen("$path/$file")),$f)>0) {
$cFlag = true;
break;
}
}
if (stripos("$path/$file",'lang')>0) echo "$path/$file".'<br />';
if ((stripos("$path/$file",'lang/')>0) && ($cFlag == false)) {
$dir = $path.'/'.$file;
if ($dryrun==true) {
echo '<strong>'.substr($dir,strlen(DOKU_INC),strlen($dir)-strlen(DOKU_INC)).'</strong><br />';
} else {
// now delete the lanuage sub-folder
$this->rrmdir($dir);
}
} else {
// Re-call this same function but on a new directory.
// this is what makes function recursive.
$this->_list_language_dirs( "$path/$file", ($level+1), $lang_keep,$dryrun );
}
}
}
}
// Close the directory handle
closedir( $dh );
}
/**
* This function will delete all folders and files which are in the specified directory.
* This is necessary due to only empty directories can be deleted.
*/
function rrmdir($dir) {
// replace "//" in $dir if existing
$dir = str_replace('//', '/', $dir);
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != '.' && $object != '..') {
if (filetype($dir.'/'.$object) == 'dir') {
$this->rrmdir($dir.'/'.$object);
echo '<strong>'.$dir.'/'.$object.' ->directory removed</strong><br />';
} else {
chmod($dir.'/'.$object, 0755);
$result = @unlink($dir.'/'.$object);
if($result === true) echo $dir.'/'.$object.' -> file deleted<br />';
else echo $dir.'/'.$object.'<span style="color:red;"><b> -> file not deleted</b></span><br />';
}
}
}
reset($objects);
@rmdir($dir);
}
}
}