[r1419]: / branches / 2.6 / cgi-bin / yabb2 / Admin / Checkspace.pm  Maximize  Restore  History

Download this file

503 lines (461 with data), 18.8 kB

  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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
###############################################################################
# Checkspace.pm #
# $Date: 02.20.14 $ #
###############################################################################
# YaBB: Yet another Bulletin Board #
# Open-Source Community Software for Webmasters #
# Version: YaBB 2.6.0 #
# Packaged: February 20, 2014 #
# Distributed by: http://www.yabbforum.com #
# =========================================================================== #
# Copyright (c) 2000-2014 YaBB (www.yabbforum.com) - All Rights Reserved. #
# Software by: The YaBB Development Team #
# with assistance from the YaBB community. #
###############################################################################
use CGI::Carp qw(fatalsToBrowser);
use English '-no_match_vars';
our $VERSION = '2.6.0';
$checkspacepmver = 'YaBB 2.6.0 $Revision: 1419 $';
if ( $action eq 'detailedversion' ) { return 1; }
sub checkspace {
is_admin_or_gmod();
# Free Disk Space Checking
if ( $OSNAME =~ /Win/sm ) {
open my $fh, q{-|}, 'DIR /-C' or croak "Can't open pipe: $!";
my @x = <$fh>;
close $fh or croak "Can't close pipe: $!";
my $lastline = pop @x;
# should look like: 17 Directory(s), 21305790464 Bytes free
return -1
if $lastline !~ m/byte/ism;
# error trapping if output fails. The word byte should be in the line
if ( $lastline =~ /^\s+(\d+)\s+(.+?)\s+(\d+)\s+(.+?)\n$/xsm ) {
$FreeBytes = $3 - 100_000;
} # 100000 bytes reserve
if ( $FreeBytes >= 1_073_741_824 ) {
$yyfreespace =
sprintf( '%.2f', $FreeBytes / ( 1024 * 1024 * 1024 ) ) . ' GB';
}
elsif ( $FreeBytes >= 1_048_576 ) {
$yyfreespace =
sprintf( '%.2f', $FreeBytes / ( 1024 * 1024 ) ) . ' MB';
}
else {
$yyfreespace = sprintf( '%.2f', $FreeBytes / 1024 ) . ' KB';
}
@disk_space = $yyfreespace;
}
else {
open my $dsh, q{-|}, 'df -k .' or croak "Can't open pipe: $!";
@disk_space = <$dsh>;
close $dsh or croak "Can't close pipe: $!";
map { $_ =~ s/ +/ /gsm } @disk_space;
open my $ffh, q{-|}, 'find . -noleaf -type f -printf "%s-"'
or croak "Can't open pipe: $!";
my @find = <$ffh>;
close $ffh or croak "Can't close pipe: $!";
}
$hostusername = $hostusername
|| ( split / +/sm, qx{ls -l YaBB.$yyext} )[2];
my @quota = qx{quota -u $hostusername -v};
$quota[0] =~ s/^ +//sm;
$quota[0] =~ s/ /&nbsp;/gsm;
$quota[1] =~ s/^ +//sm;
$quota[1] =~ s/ /&nbsp;/gsm;
my $quota_select = qq~$quota[0]<br />$quota[1]~;
if ( $quota[2] ) {
if ( !$enable_quota ) { $ds = ( split / +/sm, $disk_space[1], 2 )[0]; }
$my_q_select =
isselected( $i == $enable_quota
|| ( $ds && $quota[$i] =~ /^$ds/sm ) );
$quota_select .= q~<br /><select name="enable_quota_value" id="enable_quota_value">~;
for my $i ( 2 .. ( @quota - 1 ) ) {
$quota[$i] =~ s/^ +//sm;
$quota[$i] =~ s/ +/&nbsp;&nbsp;/gsm;
$quota_select .=
qq~<option value="$i" ~ . $my_q_select . qq~>$quota[$i]</option>~;
}
$quota_select .= '</select>';
}
# }
$ch_spc = ischecked($enable_freespace_check);
$ch_enable_q = ischecked($enable_quota);
@settings = (
{
name => $settings_txt{'checkspace'},
id => 'checkspace',
items => [
{ header => $settings_txt{'freedisk'}, },
{
description =>
qq~<label for="enable_quota">$admin_txt{'quota'}</label>~,
input_html =>
q~<input type="checkbox" name="enable_quota" id="enable_quota" value="1" ~
. (
!$quota[2] ? 'disabled="disabled" '
: $ch_enable_q
)
. q~/>~,
name => 'enable_quota',
validate => 'boolean',
depends_on => (
!$quota[2] ? []
: [
'!enable_freespace_check',
'(findfile_time==0||',
'findfile_time==||',
'findfile_maxsize==0||',
'findfile_maxsize==)'
]
),
},
{
description =>
qq~<label for="enable_quota_value">$admin_txt{'quota_value'}</label>~,
input_html => (
$quota[2] ? $quota_select
: q~<input type="text" disabled="disabled" name="enable_quota_value" id="enable_quota_value" value="" style="display:none" />~
),
name => 'enable_quota_value',
validate => 'number,null',
depends_on => ['enable_quota'],
},
{
description =>
qq~<label for="hostusername">$admin_txt{'quotahostuser'}</label>~,
input_html =>
qq~<input type="text" name="hostusername" id="hostusername" size="20" value="$hostusername" />~,
name => 'hostusername',
validate => 'text,null',
depends_on => ['enable_quota'],
},
{
description =>
qq~<label for="findfile_time">$admin_txt{'findtime'}</label>~,
input_html =>
q~<input type="text" name="findfile_time" id="findfile_time" size="4" value="~
. (
@find ? qq~$findfile_time"~ : '0" disabled="disabled"'
)
. qq~ /> $admin_txt{'537'}~,
name => 'findfile_time',
validate => 'number,null',
depends_on => (
@find ? [ '!enable_quota', '!enable_freespace_check' ]
: []
),
},
{
description =>
qq~<label for="findfile_root">$admin_txt{'findroot'}</label>~,
input_html =>
qq~<input type="text" name="findfile_root" id="findfile_root" size="40" value="$findfile_root" ~
. ( @find ? q{} : 'disabled="disabled" ' ) . q~/>~,
name => 'findfile_root',
validate => 'text,null',
depends_on => (
@find ? [ '!enable_quota', '!enable_freespace_check' ]
: []
),
},
{
description =>
qq~<label for="findfile_maxsize">$admin_txt{'findmax'}</label>~,
input_html =>
qq~<input type="text" name="findfile_maxsize" id="findfile_maxsize" size="10" value="$findfile_maxsize" ~
. ( @find ? q{} : 'disabled="disabled" ' )
. q~/> MB~,
name => 'findfile_maxsize',
validate => 'number,null',
depends_on => (
@find ? [ '!enable_quota', '!enable_freespace_check' ]
: []
),
},
{
description =>
qq~<label for="enable_freespace_check">$admin_txt{'diskspacecheck'}</label>~,
input_html =>
qq~<input type="checkbox" name="enable_freespace_check" id="enable_freespace_check" value="1" $ch_spc /><pre>@disk_space</pre>~,
name => 'enable_freespace_check',
validate => 'boolean',
depends_on => [
'!enable_quota', '(findfile_time==0||',
'findfile_time==||', 'findfile_maxsize==0||',
'findfile_maxsize==)'
],
},
],
},
);
chsettings();
$action_area = 'checkspace';
AdminTemplate();
exit;
}
sub chsettings {
is_admin_or_gmod();
$yytitle = $admin_txt{'checkspclabel'};
$page = 'checkspace';
my @requireorder; # an array for the correct order of the requirements
my %requirements; # a hash that says "Y is required by X"
$yymain .= qq~
<div class="bordercolor rightboxdiv">
<table class="border-space pad-cell" style="margin-bottom:.5em">
<tr>
<td class="titlebg"><b>$yytitle</b></td>
</tr><tr>
<td class="windowbg2"><div class="pad-more">$admin_txt{'347'}</div></td>
</tr>
</table>
</div>
<form action="$adminurl?action=checkspace_save" method="post" accept-charset="$yymycharset">
~;
foreach my $tab (@settings) {
$yymain .= qq~
<div class="bordercolor rightboxdiv">
<table class="section border-space pad-cell" style="margin-bottom:.5em" id="tab_$tab->{'id'}">
<col span="2" style="width:50%" />
<tr>
<td class="titlebg" colspan="2">
$admin_img{'prefimg'} <b>$tab->{'name'}</b>
</td>
</tr>~;
foreach my $item ( @{ $tab->{'items'} } ) {
if ( $item->{'header'} ) {
$yymain .= qq~<tr>
<td class="catbg" colspan="2">
<span class="small">$item->{'header'}</span>
</td>
</tr>~;
}
elsif ( $item->{'two_rows'} && $item->{'input_html'} ) {
$yymain .= qq~<tr>
<td class="windowbg2" colspan="2">
$item->{'description'}
</td>
</tr><tr>
<td class="windowbg2" colspan="2">
$item->{'input_html'}
</td>
</tr>~;
}
elsif ( $item->{'input_html'} ) {
$yymain .= qq~<tr>
<td class="windowbg2 vtop">
$item->{'description'}
</td>
<td class="windowbg2 vtop">
$item->{'input_html'}
</td>
</tr>~;
}
# Handle settings that require other settings
if ( $item->{'depends_on'} && $item->{'name'} ) {
foreach my $require ( @{ $item->{'depends_on'} } ) {
# This is somewhat messy, but it works well.
# We strip off the possible options: inverse, equal, and not equal
# Then we attach those to this current option in the detailed string for requirements
# While this data does not really belong with the value, it transfers nicely.
# We then remove it and reuse it later.
my ( $inverse, $realname, $remainder ) =
$require =~ m{(\(?\!?)(\w+)(.*)}xsm;
if ( !$requirements{$realname} ) {
push @requireorder, $realname;
}
push
@{ $requirements{$realname} },
$inverse . $item->{'name'} . $remainder;
}
}
}
$yymain .= q~
</table>
</div>~;
}
my %requirejs;
my $dependicies = q{};
my $onloadevents;
foreach my $ritem (@requireorder) {
$dependicies .= qq~
function handleDependent_$ritem() {
var isChecked = document.getElementsByName("$ritem")[0].checked;
var itemValue = document.getElementsByName("$ritem")[0].value;\n~;
foreach my $require ( @{ $requirements{$ritem} } ) {
# && or ||, ( and )
my $AndOr = $require =~ s/\)//xsm ? ')' : q{};
$AndOr .= $require =~ s/\|\|//xsm ? ' ||' : ' &&';
my $C = $require =~ s/\(//xsm ? '(' : q{};
# Is false
if ( $require =~ s/^\!//xsm ) {
$requirejs{$require} .=
qq~$C\!document.getElementsByName("$ritem")[0].checked$AndOr ~;
}
# Is equal to
elsif ( $require =~ s/\=\=(.*)$//xsm ) {
$requirejs{$require} .=
qq~$C\document.getElementsByName("$ritem")[0].value == '$1'$AndOr ~;
}
# Is not equal to
elsif ( $require =~ s/\!\=(.*)$//xsm ) {
$requirejs{$require} .=
qq~$C\document.getElementsByName("$ritem")[0].value != '$1'$AndOr ~;
}
# Is true
else {
$requirejs{$require} .=
qq~$C\document.getElementsByName("$ritem")[0].checked$AndOr ~;
}
$dependicies .= qq~ checkDependent("$require");\n~;
}
$dependicies .= qq~ };
document.getElementsByName("$ritem")[0].onclick = handleDependent_$ritem;
document.getElementsByName("$ritem")[0].onkeyup = handleDependent_$ritem;
~;
$onloadevents .= qq~handleDependent_$ritem(); ~;
}
# Hidden "feature": jump directly to a tab by default via the URL bar.
$INFO{'tab'} =~ s/\W//gxsm;
$default_tab = $INFO{'tab'} || $settings[0]->{'id'};
$yymain .= qq~
<div class="bordercolor rightboxdiv">
<table class="border-space pad-cell">
<tr>
<td class="titlebg" colspan="2">
$admin_img{'prefimg'} <b>$admin_txt{'10'}</b>
</td>
</tr><tr>
<td class="catbg center pad-cell" colspan="2">
<input class="button" type="submit" value="$admin_txt{'10'}" />
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|\$)');
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
function changeToTab(tab) {
var elements = getElementsByClass('section');
var i;
for(i = 0; i < elements.length; i++) {
if(elements[i].id == 'tab_' + tab) {
elements[i].style.display = '';
}
else {
elements[i].style.display = 'none';
}
}
var elm = getElementsByClass('curtab')[0];
if(elm) {
elm.className = '';
}
document.getElementById('button_' + tab).className = 'curtab';
}
var removables = getElementsByClass('js_remove_me');
var i;
for(i = 0; i < removables.length; i++) {
removables[i].innerHTML = '';
}
changeToTab('$default_tab'); // Focus default tab
function checkDependent(eid) {
var elm = document.getElementsByName(eid)[0];\n~;
# Loop through each item that depends on something else
foreach my $name ( keys %requirejs ) {
my $logic = $requirejs{$name};
$logic =~ s/ (&&|\|\|) $//sm;
$yymain .= qq~
if (eid == "$name" && ($logic)) {
elm.disabled = false;
} else if (eid == "$name") {
elm.disabled = true;
}\n~;
}
$yymain .= qq~
}
$dependicies
window.onload = function(){ $onloadevents};
function undisableAll(node) {
var elements = document.getElementsByTagName("input");
for(var i = 0; i < elements.length; i++) {
elements[i].disabled = false;
}
elements = document.getElementsByTagName("textarea");
for( i = 0; i < elements.length; i++) {
elements[i].disabled = false;
}
elements = document.getElementsByTagName("select");
for( i = 0; i < elements.length; i++) {
elements[i].disabled = false;
}
}
// -->
</script>~;
return;
}
sub ischecked {
my ($inp) = @_;
# Return a ref so we can be used like ${ischecked($var)} inside a string
if ( $inp == 1 ) { return 'checked="checked"'; }
return;
}
sub isselected {
my ($inp) = @_;
# Return a ref so we can be used like ${ischecked($var)} inside a string
if ( $inp == 1 ) { return 'selected="selected"'; }
return;
}
sub checkspace_save {
is_admin_or_gmod();
$enable_quota = $FORM{'enable_quota'} || '0';
$enable_quota_value = $FORM{'enable_quota_value'} || '0';
$hostusername = $FORM{'hostusername'} || q{};
$findfile_time = $FORM{'findfile_time'} || 0;
$findfile_root = $FORM{'findfile_root'} || q{};
$findfile_maxsize = $FORM{'findfile_maxsize'} || 0;
$findfile_space = $FORM{'findfile_space'} || '1<>0';
$enable_freespace_check = $FORM{'enable_freespace_check'} || 0;
if ( $enable_quota && $enable_quota_value > 1 && $hostusername ) {
$enable_quota = $enable_quota_value;
$findfile_maxsize = 0;
$enable_freespace_check = 0;
}
elsif (-d "$findfile_root"
&& $findfile_maxsize > 0
&& !$enable_freespace_check )
{
$findfile_space = '1<>0';
$enable_quota = 0;
}
elsif ($enable_freespace_check) {
$findfile_maxsize = 0;
$enable_quota = 0;
}
elsif ( !-d "$findfile_root" || !$findfile_maxsize ) {
$findfile_time = 0;
$findfile_maxsize = 0;
}
require Admin::NewSettings;
SaveSettingsTo('Settings.pm');
if ( $action eq 'checkspace_save' ) {
$yySetLocation = qq~$adminurl?action=newsettings;page=advanced~;
redirectexit();
}
return;
}
1;