C:\xampp\htdocs\kptv2\admin\spaw2\plugins\spawfm\class\spawfm.class.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
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
<?php
error_reporting
(E_ALL);
class 
SpawFm
{
  var 
$thumbnails_enabled false;
  var 
$current_dir false;
  var 
$current_dir_data false;
  var 
$current_type false;
  var 
$image_ext = array('.gif''.png''.jpg''.jpeg');
  var 
$error_msg false;
  
  function 
SpawFm()
  {
  }
  
  
  function 
normalizeDir($dir$stripslashes false$noleadslash false)
  {
    if (!
strlen($dir))
      return 
false;
    
$dir str_replace('\\''/'$dir);
    if (!
$noleadslash and !preg_match('#[a-z]+://#i'$dir))
      
$dir preg_replace('|^/*(.*)|''/$1'$dir);
    
$dir SpawFm::addTrailingSlash($dir);
    if (
$stripslashes
      return 
SpawVars::stripSlashes($dir);
    return 
$dir;
  }
  
  function 
addTrailingSlash($dir)
  {
    return 
preg_replace('|/*$|'''$dir).'/';
  }
  
  
  function 
getFileIcon($filename)
  {
    
$icons SpawFm::getFileIconsDefinition($filename);
    return 
$icons['icon'];
  }
  
  function 
getFileIconBig($filename)
  {
    
$icons SpawFm::getFileIconsDefinition($filename);
    return 
$icons['icon_big'];
  }
  
  function 
getFileIconsDefinition($filename
  {
    global 
$config;
    static 
$spawfm_ftypes_icons;
    
$icons false;
    
$ext SpawFm::getFileExtension($filename);
    if (!isset(
$spawfm_ftypes_icons[$ext])) {
      
// search for icon definition for this extension 
      
$icons_def $config->getConfigValue('PG_SPAWFM_FILETYPES_ICONS');
      foreach (
$icons_def as $definition) {
        if (
in_array($ext$definition['extensions'])) {
          
$icons = array(
            
'icon'      => $definition['icon'],
            
'icon_big'  => $definition['icon_big'],
          );
          break;
        }
      }
      
      
// set default icon if definition not found
      
if (!$icons) {
        
$icons $config->getConfigValue('PG_SPAWFM_FILETYPES_ICON_DEFAULT');
      }
      
$spawfm_ftypes_icons[$ext] = $icons;
    } else {
      
$icons $spawfm_ftypes_icons[$ext];
    }
    return 
$icons;
  }
  
  function 
getFileThumbnail($filename)
  {
    
$icons SpawFm::getFileIconsDefinition($filename);
    return 
'../plugins/spawfm/img/'.$icons['icon_big'];
  }
  
  function 
getFileSize($file)
  {
    
$fsize = @filesize($this->getCurrentFsDir().$file);
    if (!
$fsize)
      
$fsize 0;
    return 
$fsize;
  }
   
  function 
getFileDate($file)
  {
    
$fdate = @filemtime($this->getCurrentFsDir().$file);
    if (!
$fdate)
      
$fdate 0;
    return 
$fdate;
  }
  
  function 
isImage($file)
  {
    
$ext $this->getFileExtension($file);
    if (
in_array($ext$this->image_ext)) {
      return 
true;
    }
    return 
false;
  }
  
  function 
isThumbailsModePossible($dir_path)
  {
    
// check required system tools for image resizing
    // check if current directory is writable
    
return true;
  }
  
  function 
getCurrentDir()
  {
    return !empty(
$this->current_dir_data['dir']) ? $this->current_dir_data['dir'] : false;
  }
  
  function 
getCurrentFsDir()
  {
    return !empty(
$this->current_dir_data['fsdir']) ? $this->current_dir_data['fsdir'] : false;
  }
  
  function 
setCurrentDirData($dir_data)
  {
    
$this->current_dir_data $dir_data;
  }
  
  function 
getCurrentDirData()
  {
    return 
$this->current_dir_data;
  }
  
  function 
unsetCurrentDirData()
  {
    
$this->current_dir_data false;
  }
  
  function 
getCurrentDirSetting($name)
  {
    
$data $this->getCurrentDirData();
    if (isset(
$data['params'][$name])) {
      return 
$data['params'][$name];
    }
    return 
false;
  }
  
  function 
setCurrentType($type)
  {
    
$this->current_type $type;
  }
  function 
getCurrentType()
  {
    return 
$this->current_type;
  }
  
  
// returns allowed extensions for selected directory and type
  
function getAllowedExtensions()
  {
    global 
$config;
    
$allowed_ext = array();
    if (!
$this->getCurrentDir() or !$curr_dir_data $this->getCurrentDirData()) {
      return 
$allowed_ext;
    }
    
    
$filetypes $config->getConfigValue('PG_SPAWFM_FILETYPES');
    
    if (
$curr_type $this->getCurrentType()) {
      
$allowed_ext $filetypes[$curr_type];
    } elseif (!empty(
$curr_dir_data['params']['allowed_filetypes'])) {
      if (!
is_array($curr_dir_data['params']['allowed_filetypes'])) {
        
$curr_dir_data['params']['allowed_filetypes'] = array($curr_dir_data['params']['allowed_filetypes']);
      }
      if (
sizeof($curr_dir_data['params']['allowed_filetypes'])) {
        foreach (
$curr_dir_data['params']['allowed_filetypes'] as $ftype) {
          if (
strlen($ftype) and $ftype{0} == '.') { // extension specified
            
$allowed_ext[] = $ftype;
          } elseif (isset(
$filetypes[$ftype])) { // filetype group specified
            
$allowed_ext array_merge($allowed_ext$filetypes[$ftype]);
          }
        }
      }
    }
    return 
$allowed_ext;
  }
  
  
// returns list of files in current directory
  
function getFilesList()
  {
    global 
$lang;
    
$files = array();
    
    if (!
$this->getCurrentFsDir()) {
      return 
$files;
    }

    
$allowed_ext $this->getAllowedExtensions();
    if (
$dh = @opendir($this->getCurrentFsDir())) {
      while (
false !== ($file readdir($dh))) {
        if (!
is_dir($this->getCurrentFsDir().$file)) {  
          
$ext SpawFm::getFileExtension($file);
          if (
in_array('.*'$allowed_ext) or in_array($ext$allowed_ext)) {
            
$files[] = $file;
          }
        }
      }
      
closedir($dh);
    } else {
      return 
false;
    }
    
    
// reorder files by title
    
natcasesort($files);
    
    
// load files' details
    
foreach ($files as $key=>$file) {
      
$ext SpawFm::getFileExtension($file);
      if (!
strlen($fdescr $lang->m($ext'filetypes'))) {
        
$fdescr strtoupper(substr($ext1)).' '.
                  
$lang->m('filetype_suffix''file_details');
      }
      
// additional info
      
if ($imgsize = @getimagesize($this->getCurrentFsDir().$file)) {
        
$other $lang->m('img_dimensions''file_details').': '.$imgsize[0].'x'.$imgsize[1];
      } else {
        
$other '';
      }
      
      
// get thumbail
      // TO DO
      
      
$files[$key] = array(
        
'type'      => 'F',
        
'name'      => $file,
        
'size'      => $this->getFileSize($file),
        
'date'      => $this->getFileDate($file),
        
'fdescr'    => $fdescr,
        
'icon'      => '../plugins/spawfm/img/'.$this->getFileIcon($file),
        
'icon_big'  => '../plugins/spawfm/img/'.$this->getFileIconBig($file),
        
'thumb'     => $this->getFileThumbnail($file),
        
'other'     => $other
      
);
    }
    
    return 
$files;
  }
  
  
// returns list of files in current directory
  
function getDirectoriesList()
  {
    global 
$lang$config;
    
$directories = array();
    
    if (!
$this->getCurrentFsDir()) {
      return 
$directories;
    }

    if (
$dh = @opendir($this->getCurrentFsDir())) {
      while (
false !== ($file readdir($dh))) {
        if (
is_dir($this->getCurrentFsDir().$file) and $file{0} != '.') {
          
$directories[] = $file;
        }
      }
      
closedir($dh);
    } else {
      return 
false;
    }
    
    
// sort
    
natcasesort($directories);
    
    
// load details
    
foreach ($directories as $key=>$file) {
      
$directories[$key] = array(
        
'name'      => $file,
        
'date'      => $this->getFileDate($file),
        
'size'      => $this->getFileSize($file),
        
'descr'     => $lang->m('file_folder''file_details'),
        
'icon'      => '../plugins/spawfm/img/'.$config->getConfigValueElement('PG_SPAWFM_FILETYPES_ICON_FOLDER''icon'),
        
'icon_big'  => '../plugins/spawfm/img/'.$config->getConfigValueElement('PG_SPAWFM_FILETYPES_ICON_FOLDER''icon_big'),
        
'thumb'     => $this->getFileThumbnail($file),
        
'other'     => ''
      
);
    }
    
    return 
$directories;
  }
  
  function 
setThumbnailsEnabled($status false)
  {
    
$this->thumbnails_enabled $status;
  }
  
  function 
getThumbnailsEnabled()
  {
    return 
$this->thumbnails_enabled;
  }
  
  
// static helper methods
  
function getFileExtension($filename)
  {
    return 
strtolower(strrchr($filename'.'));
  }
  
  
// adds slashes before single quotes
  
function escJsStr($str)
  {
    return 
addcslashes($str'\'');
  }
  
  
// strip leading slash
  
function stripLeadSlash($dir)
  {
    if (
strlen($dir) and ($dir{0} == '/' or $dir{0} == '\\')) {
      return 
substr($dir1);
    }
    return 
$dir;
  }
  
  
// escape HTML special chars
  
function escapeHtml($str)
  {
    return 
str_replace(
      array(
'<''>''"'), 
      array(
'&lt;''&gt;''&quot;'), 
      
$str
    
);
  }
  
  function 
error()
  {
    return 
false !== $this->getError();
  }
  
  function 
getError()
  {
    return 
$this->error_msg;
  }
  
  function 
setError($msg)
  {
    
$this->error_msg $msg;
  }
  
  function 
deleteFile($del_file)
  {
    global 
$lang;
  
    
// filter file/dir name
    
$del_file basename($del_file);
    
    if (
file_exists($this->getCurrentFsDir().$del_file)) {
      if (
is_dir($this->getCurrentFsDir().$del_file)) {
        if (!
$this->getCurrentDirSetting('recursive') or
            !
$this->getCurrentDirSetting('allow_modify_subdirectories')) 
        {
          
$this->setError($lang->m('error_delete_subdirectories_forbidden''spawfm'));
        } else {
          
// check if directory is empty
          
$empty true;
          if (
$dh opendir($this->getCurrentFsDir().$del_file)) {
            while ((
$file readdir($dh)) !== false) {
              if (
$file != '.' and $file != '..') {
                
$empty false;
                break;
              }
            }
            
closedir($dh);
          }
        
          if (!
$empty) {
            
$this->setError($lang->m('error_delete_subdirectories_not_empty''spawfm'));
          } elseif (!@
rmdir($this->getCurrentFsDir().$del_file)) {
            
$this->setError($lang->m('error_delete_subdirectories_failed''spawfm'));
          }
        }
      } else {
        if (!
$this->getCurrentDirSetting('allow_modify')) {
          
$this->setError($lang->m('error_delete_forbidden''spawfm'));
        } elseif (!@
unlink($this->getCurrentFsDir().$del_file)) {
          
$this->setError($lang->m('error_delete_failed''spawfm'));
        }
      }
    }
    
    return !
$this->error();
  }
  
  function 
renameFile($ren_old_name$ren_new_name)
  {
    global 
$lang;
    
    
// cleanup/filter file/directory names
    
$ren_old_name basename($ren_old_name);
    
$ren_new_name basename(trim($ren_new_name));
    
    
// check if file/directory can be renamed
    
if (!file_exists($this->getCurrentFsDir().$ren_old_name)) {
      
$this->setError($lang->m('error_rename_file_missing''spawfm'));
    } elseif (
is_dir($this->getCurrentFsDir().$ren_old_name) and 
              !
$this->getCurrentDirSetting('allow_modify_subdirectories')) 
    {
      
$this->setError($lang->m('error_rename_directories_forbidden''spawfm'));
    } elseif (!
is_dir($this->getCurrentFsDir().$ren_old_name) and 
              !
$this->getCurrentDirSetting('allow_modify')) 
    {
      
$this->setError($lang->m('error_rename_forbidden''spawfm'));
    } elseif (
file_exists($this->getCurrentFsDir().$ren_new_name)) {
      
$this->setError(str_replace('[*FILE*]'$ren_new_name$lang->m('error_rename_file_exists''spawfm')));
    } else {
      
// check if filetype doesn't change
      
if ($this->getFileExtension($ren_old_name) != $this->getFileExtension($ren_new_name)) {
        
$this->setError($lang->m('error_rename_extension_changed''spawfm'));
      
// check if new file name is secure (for files only)
      
} elseif (!is_dir($this->getCurrentFsDir().$ren_old_name) and !$this->isSecureFile($ren_new_name)) {
        
$this->setError($lang->m('error_bad_filetype''spawfm'));
      } elseif (!@
rename($this->getCurrentFsDir().$ren_old_name$this->getCurrentFsDir().$ren_new_name)) {
        
$this->setError($lang->m('error_rename_failed''spawfm'));
      }
    }
    
    return !
$this->error();
  }
  
  function 
createDirectory($dir_name)
  {
    global 
$lang;
  
    if (
$this->getCurrentDirSetting('recursive') and 
        
$this->getCurrentDirSetting('allow_create_subdirectories'))
    {
      
// filter dir name
      
$dir_name trim(basename($dir_name));
      
      if (
preg_match('#[:<>|?*"/\\\\]+#'$dir_name)) {
        
$this->setError($lang->m('error_create_directories_name_invalid''spawfm'));
      }
      
// check if name is not used already
      
elseif (file_exists($this->getCurrentFsDir().$dir_name)) {
        
$this->setError($lang->m('error_create_directories_name_used''spawfm'));
      } else {
        
// chmod created directory if specified
        
if (strlen($this->getCurrentDirSetting('chmod_to'))) {
          
$res = @mkdir($this->getCurrentFsDir().$dir_name$this->getCurrentDirSetting('chmod_to'));
        } else {
          
$res = @mkdir($this->getCurrentFsDir().$dir_name);
        }
        if (!
$res) {
          
$this->setError($lang->m('error_create_directories_failed''spawfm'));
        }
      }    
    } else {
      
$this->setError($lang->m('error_create_directories_forbidden''spawfm'));
    }
    
    return !
$this->error();
  }
  
  function 
uploadFile($uplfile)
  {
    global 
$lang;
  
    
// check if upload is allowed
    
if (!$this->getCurrentDirSetting('allow_upload')) {
      
$this->setError($lang->m('error_upload_forbidden''spawfm'));
    } else {
      if (
is_uploaded_file($uplfile['tmp_name'])) {
        
// check filetype
        
$ext SpawFm::getFileExtension($uplfile['name']);
        
$allowed_ext $this->getAllowedExtensions();
        if ((
in_array('.*'$allowed_ext) or in_array($ext$allowed_ext)) and $this->isSecureFile($uplfile['name'])) {
          
// check filesize
          
if (!$this->getCurrentDirSetting('max_upload_filesize') or 
              
$uplfile['size'] <= $this->getCurrentDirSetting('max_upload_filesize'))
          {
            
$ok true;
            
$err = array();
            
/*
              check image dimensions: try to read image dimensions (this step is 
              omitted if getimagesize() does not recognize file as image or fails 
              to read it's dimensions
            */
            
if (($this->getCurrentDirSetting('max_img_width') or
                
$this->getCurrentDirSetting('max_img_height')) and 
                
$imgsize = @getimagesize($uplfile['tmp_name'])) 
            {
              
// check if dimensions not too big if specified   
              
if ($this->getCurrentDirSetting('max_img_width') and 
                  
$imgsize[0] > $this->getCurrentDirSetting('max_img_width')) 
              {
                
$ok false;
                
$err[] = str_replace('[*MAXWIDTH*]'$this->getCurrentDirSetting('max_img_width'), $lang->m('error_img_width_max''spawfm'));
              }
              if (
$this->getCurrentDirSetting('max_img_height') and 
                  
$imgsize[1] > $this->getCurrentDirSetting('max_img_height')) 
              {
                
$ok false;
                
$err[] = str_replace('[*MAXHEIGHT*]'$this->getCurrentDirSetting('max_img_height'), $lang->m('error_img_height_max''spawfm'));
              }
            }
            if (!
$ok) {
              
$this->setError(implode('<br />'$err));
            } else {
              
// proceed saving uploaded file
              
$uplfile_name $uplfile['name'];
              
$i 1;
              
// pick unused file name
              
while (file_exists($this->getCurrentFsDir().$uplfile_name)) {
                
$uplfile_name ereg_replace('(.*)(\.[a-zA-Z]+)$''\1_'.$i.'\2'$uplfile['name']);
                
$i++;
              }
              if (!@
move_uploaded_file($uplfile['tmp_name'], $this->getCurrentFsDir().$uplfile_name)) {
                
$this->setError($lang->m('error_upload_failed''spawfm'));
              } else {
                if (
strlen($this->getCurrentDirSetting('chmod_to'))) {
                  
// chmod uploaded file
                  
if (!@chmod($this->getCurrentFsDir().$uplfile_name$this->getCurrentDirSetting('chmod_to'))) {
                    
$this->setError($lang->m('error_chmod_uploaded_file''spawfm'));
                  }
                }
              }
            }
          } else {
            
$this->setError($lang->m('error_max_filesize''spawfm').' '.round($this->getCurrentDirSetting('max_upload_filesize') / 10242).' KB');
          }
        } else {
          
$this->setError($lang->m('error_bad_filetype''spawfm'));
        }
      } else {
        if (
$uplfile['error'] == or $uplfile['error'] == 2) {
          
$this->setError($lang->m('error_upload_file_too_big''spawfm'));
        } elseif (
$uplfile['error'] == 3) {
          
$this->setError($lang->m('error_upload_file_incomplete''spawfm'));
        } else {
          
$this->setError($lang->m('error_upload_failed''spawfm'));
        }
      }    
    }
    
    return 
$this->error() ? false $uplfile_name;
  }
  
  function 
isSecureFile($filename)
  {
    
$unsafe_ext $this->getCurrentDirSetting('forbid_extensions');
    if (!empty(
$unsafe_ext)) {
      
$filename strtolower($filename);
      
$arr explode('.'$filename);
      foreach (
$unsafe_ext as $ext) {
        
$ext strtolower($ext);
        if ((
$this->getCurrentDirSetting('forbid_extensions_strict') and in_array($ext$arr)) or
            
$ext == $arr[sizeof($arr)-1])
        {
          return 
false;
        }
      }
    }
    return 
true;
  }
}
?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586